1 // 2 // Copyright 2024 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 7 // ProgramExecutableWgpu.h: Implementation of ProgramExecutableImpl. 8 9 #ifndef LIBANGLE_RENDERER_WGPU_PROGRAMEXECUTABLEWGPU_H_ 10 #define LIBANGLE_RENDERER_WGPU_PROGRAMEXECUTABLEWGPU_H_ 11 12 #include "libANGLE/ProgramExecutable.h" 13 #include "libANGLE/renderer/ProgramExecutableImpl.h" 14 #include "libANGLE/renderer/renderer_utils.h" 15 #include "libANGLE/renderer/wgpu/wgpu_pipeline_state.h" 16 17 #include <dawn/webgpu_cpp.h> 18 19 namespace rx 20 { 21 struct TranslatedWGPUShaderModule 22 { 23 wgpu::ShaderModule module; 24 }; 25 26 class ProgramExecutableWgpu : public ProgramExecutableImpl 27 { 28 public: 29 ProgramExecutableWgpu(const gl::ProgramExecutable *executable); 30 ~ProgramExecutableWgpu() override; 31 32 void destroy(const gl::Context *context) override; 33 34 angle::Result updateUniformsAndGetBindGroup(ContextWgpu *context, 35 wgpu::BindGroup *outBindGroup); 36 37 angle::Result resizeUniformBlockMemory(const gl::ShaderMap<size_t> &requiredBufferSize); 38 getSharedDefaultUniformBlock(gl::ShaderType shaderType)39 std::shared_ptr<BufferAndLayout> &getSharedDefaultUniformBlock(gl::ShaderType shaderType) 40 { 41 return mDefaultUniformBlocks[shaderType]; 42 } 43 44 void markDefaultUniformsDirty(); checkDirtyUniforms()45 bool checkDirtyUniforms() { return mDefaultUniformBlocksDirty.any(); } 46 47 void setUniform1fv(GLint location, GLsizei count, const GLfloat *v) override; 48 void setUniform2fv(GLint location, GLsizei count, const GLfloat *v) override; 49 void setUniform3fv(GLint location, GLsizei count, const GLfloat *v) override; 50 void setUniform4fv(GLint location, GLsizei count, const GLfloat *v) override; 51 void setUniform1iv(GLint location, GLsizei count, const GLint *v) override; 52 void setUniform2iv(GLint location, GLsizei count, const GLint *v) override; 53 void setUniform3iv(GLint location, GLsizei count, const GLint *v) override; 54 void setUniform4iv(GLint location, GLsizei count, const GLint *v) override; 55 void setUniform1uiv(GLint location, GLsizei count, const GLuint *v) override; 56 void setUniform2uiv(GLint location, GLsizei count, const GLuint *v) override; 57 void setUniform3uiv(GLint location, GLsizei count, const GLuint *v) override; 58 void setUniform4uiv(GLint location, GLsizei count, const GLuint *v) override; 59 void setUniformMatrix2fv(GLint location, 60 GLsizei count, 61 GLboolean transpose, 62 const GLfloat *value) override; 63 void setUniformMatrix3fv(GLint location, 64 GLsizei count, 65 GLboolean transpose, 66 const GLfloat *value) override; 67 void setUniformMatrix4fv(GLint location, 68 GLsizei count, 69 GLboolean transpose, 70 const GLfloat *value) override; 71 void setUniformMatrix2x3fv(GLint location, 72 GLsizei count, 73 GLboolean transpose, 74 const GLfloat *value) override; 75 void setUniformMatrix3x2fv(GLint location, 76 GLsizei count, 77 GLboolean transpose, 78 const GLfloat *value) override; 79 void setUniformMatrix2x4fv(GLint location, 80 GLsizei count, 81 GLboolean transpose, 82 const GLfloat *value) override; 83 void setUniformMatrix4x2fv(GLint location, 84 GLsizei count, 85 GLboolean transpose, 86 const GLfloat *value) override; 87 void setUniformMatrix3x4fv(GLint location, 88 GLsizei count, 89 GLboolean transpose, 90 const GLfloat *value) override; 91 void setUniformMatrix4x3fv(GLint location, 92 GLsizei count, 93 GLboolean transpose, 94 const GLfloat *value) override; 95 96 void getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const override; 97 void getUniformiv(const gl::Context *context, GLint location, GLint *params) const override; 98 void getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const override; 99 100 TranslatedWGPUShaderModule &getShaderModule(gl::ShaderType type); 101 102 angle::Result getRenderPipeline(ContextWgpu *context, 103 const webgpu::RenderPipelineDesc &desc, 104 wgpu::RenderPipeline *pipelineOut); 105 106 private: 107 angle::CheckedNumeric<size_t> getDefaultUniformAlignedSize(ContextWgpu *context, 108 gl::ShaderType shaderType) const; 109 angle::CheckedNumeric<size_t> calcUniformUpdateRequiredSpace( 110 ContextWgpu *context, 111 gl::ShaderMap<uint64_t> *uniformOffsets) const; 112 // The layout of the resource bind groups (numbering for buffers, textures, samplers) can be 113 // determined once the program is linked, and should be passed in pipeline creation. Fills in 114 // `mPipelineLayout` and `mDefaultBindGroupLayout` if they haven't been already. 115 void genBindingLayoutIfNecessary(ContextWgpu *context); 116 117 gl::ShaderMap<TranslatedWGPUShaderModule> mShaderModules; 118 webgpu::PipelineCache mPipelineCache; 119 // Holds the binding layout of resources (buffers, textures, samplers) required by the linked 120 // shaders. 121 wgpu::PipelineLayout mPipelineLayout; 122 // Holds the binding group layout for the default bind group. 123 wgpu::BindGroupLayout mDefaultBindGroupLayout; 124 // Holds the most recent BindGroup. Note there may be others in the command buffer. 125 wgpu::BindGroup mDefaultBindGroup; 126 127 // Holds layout info for basic GL uniforms, which needs to be laid out in a buffer for WGSL 128 // similarly to a UBO. 129 DefaultUniformBlockMap mDefaultUniformBlocks; 130 gl::ShaderBitSet mDefaultUniformBlocksDirty; 131 }; 132 133 } // namespace rx 134 135 #endif // LIBANGLE_RENDERER_WGPU_PROGRAMEXECUTABLEWGPU_H_ 136