• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // ProgramMtl.h:
7 //    Defines the class interface for ProgramMtl, implementing ProgramImpl.
8 //
9 
10 #ifndef LIBANGLE_RENDERER_METAL_PROGRAMMTL_H_
11 #define LIBANGLE_RENDERER_METAL_PROGRAMMTL_H_
12 
13 #import <Metal/Metal.h>
14 
15 #include <array>
16 
17 #include "common/Optional.h"
18 #include "common/utilities.h"
19 #include "libANGLE/renderer/ProgramImpl.h"
20 #include "libANGLE/renderer/glslang_wrapper_utils.h"
21 #include "libANGLE/renderer/metal/mtl_command_buffer.h"
22 #include "libANGLE/renderer/metal/mtl_glslang_utils.h"
23 #include "libANGLE/renderer/metal/mtl_resources.h"
24 #include "libANGLE/renderer/metal/mtl_state_cache.h"
25 
26 namespace rx
27 {
28 class ContextMtl;
29 
30 class ProgramMtl : public ProgramImpl
31 {
32   public:
33     ProgramMtl(const gl::ProgramState &state);
34     ~ProgramMtl() override;
35 
36     void destroy(const gl::Context *context) override;
37 
38     std::unique_ptr<LinkEvent> load(const gl::Context *context,
39                                     gl::BinaryInputStream *stream,
40                                     gl::InfoLog &infoLog) override;
41     void save(const gl::Context *context, gl::BinaryOutputStream *stream) override;
42     void setBinaryRetrievableHint(bool retrievable) override;
43     void setSeparable(bool separable) override;
44 
45     std::unique_ptr<LinkEvent> link(const gl::Context *context,
46                                     const gl::ProgramLinkedResources &resources,
47                                     gl::InfoLog &infoLog) override;
48     GLboolean validate(const gl::Caps &caps, gl::InfoLog *infoLog) override;
49 
50     void setUniform1fv(GLint location, GLsizei count, const GLfloat *v) override;
51     void setUniform2fv(GLint location, GLsizei count, const GLfloat *v) override;
52     void setUniform3fv(GLint location, GLsizei count, const GLfloat *v) override;
53     void setUniform4fv(GLint location, GLsizei count, const GLfloat *v) override;
54     void setUniform1iv(GLint location, GLsizei count, const GLint *v) override;
55     void setUniform2iv(GLint location, GLsizei count, const GLint *v) override;
56     void setUniform3iv(GLint location, GLsizei count, const GLint *v) override;
57     void setUniform4iv(GLint location, GLsizei count, const GLint *v) override;
58     void setUniform1uiv(GLint location, GLsizei count, const GLuint *v) override;
59     void setUniform2uiv(GLint location, GLsizei count, const GLuint *v) override;
60     void setUniform3uiv(GLint location, GLsizei count, const GLuint *v) override;
61     void setUniform4uiv(GLint location, GLsizei count, const GLuint *v) override;
62     void setUniformMatrix2fv(GLint location,
63                              GLsizei count,
64                              GLboolean transpose,
65                              const GLfloat *value) override;
66     void setUniformMatrix3fv(GLint location,
67                              GLsizei count,
68                              GLboolean transpose,
69                              const GLfloat *value) override;
70     void setUniformMatrix4fv(GLint location,
71                              GLsizei count,
72                              GLboolean transpose,
73                              const GLfloat *value) override;
74     void setUniformMatrix2x3fv(GLint location,
75                                GLsizei count,
76                                GLboolean transpose,
77                                const GLfloat *value) override;
78     void setUniformMatrix3x2fv(GLint location,
79                                GLsizei count,
80                                GLboolean transpose,
81                                const GLfloat *value) override;
82     void setUniformMatrix2x4fv(GLint location,
83                                GLsizei count,
84                                GLboolean transpose,
85                                const GLfloat *value) override;
86     void setUniformMatrix4x2fv(GLint location,
87                                GLsizei count,
88                                GLboolean transpose,
89                                const GLfloat *value) override;
90     void setUniformMatrix3x4fv(GLint location,
91                                GLsizei count,
92                                GLboolean transpose,
93                                const GLfloat *value) override;
94     void setUniformMatrix4x3fv(GLint location,
95                                GLsizei count,
96                                GLboolean transpose,
97                                const GLfloat *value) override;
98 
99     void getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const override;
100     void getUniformiv(const gl::Context *context, GLint location, GLint *params) const override;
101     void getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const override;
102 
103     // Calls this before drawing, changedPipelineDesc is passed when vertex attributes desc and/or
104     // shader program changed.
105     angle::Result setupDraw(const gl::Context *glContext,
106                             mtl::RenderCommandEncoder *cmdEncoder,
107                             const Optional<mtl::RenderPipelineDesc> &changedPipelineDesc,
108                             bool forceTexturesSetting);
109 
110   private:
111     template <int cols, int rows>
112     void setUniformMatrixfv(GLint location,
113                             GLsizei count,
114                             GLboolean transpose,
115                             const GLfloat *value);
116     template <class T>
117     void getUniformImpl(GLint location, T *v, GLenum entryPointType) const;
118 
119     template <typename T>
120     void setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType);
121 
122     angle::Result initDefaultUniformBlocks(const gl::Context *glContext);
123 
124     angle::Result commitUniforms(ContextMtl *context, mtl::RenderCommandEncoder *cmdEncoder);
125     angle::Result updateTextures(const gl::Context *glContext,
126                                  mtl::RenderCommandEncoder *cmdEncoder,
127                                  bool forceUpdate);
128 
129     void reset(ContextMtl *context);
130     void linkResources(const gl::ProgramLinkedResources &resources);
131     angle::Result linkImpl(const gl::Context *glContext,
132                            const gl::ProgramLinkedResources &resources,
133                            gl::InfoLog &infoLog);
134 
135     angle::Result createMslShader(const gl::Context *glContext,
136                                   gl::ShaderType shaderType,
137                                   gl::InfoLog &infoLog,
138                                   const std::string &translatedSource);
139 
140     // State for the default uniform blocks.
141     struct DefaultUniformBlock final : private angle::NonCopyable
142     {
143         DefaultUniformBlock();
144         ~DefaultUniformBlock();
145 
146         // Shadow copies of the shader uniform data.
147         angle::MemoryBuffer uniformData;
148 
149         // Since the default blocks are laid out in std140, this tells us where to write on a call
150         // to a setUniform method. They are arranged in uniform location order.
151         std::vector<sh::BlockMemberInfo> uniformLayout;
152     };
153 
154     gl::ShaderBitSet mDefaultUniformBlocksDirty;
155     gl::ShaderBitSet mSamplerBindingsDirty;
156     gl::ShaderMap<DefaultUniformBlock> mDefaultUniformBlocks;
157 
158     gl::ShaderMap<std::string> mTranslatedMslShader;
159 
160     gl::ShaderMap<mtl::TranslatedShaderInfo> mMslShaderTranslateInfo;
161 
162     mtl::RenderPipelineCache mMetalRenderPipelineCache;
163 };
164 
165 }  // namespace rx
166 
167 #endif /* LIBANGLE_RENDERER_METAL_PROGRAMMTL_H_ */
168