1 /* 2 * Copyright 2019 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrDawnProgramBuilder_DEFINED 9 #define GrDawnProgramBuilder_DEFINED 10 11 #include "src/gpu/dawn/GrDawnProgramDataManager.h" 12 #include "src/gpu/dawn/GrDawnUniformHandler.h" 13 #include "src/gpu/dawn/GrDawnVaryingHandler.h" 14 #include "src/sksl/SkSLCompiler.h" 15 #include "dawn/dawncpp.h" 16 #include "src/gpu/glsl/GrGLSLProgramBuilder.h" 17 18 class GrPipeline; 19 20 struct GrDawnProgram : public SkRefCnt { 21 struct RenderTargetState { 22 SkISize fRenderTargetSize; 23 GrSurfaceOrigin fRenderTargetOrigin; 24 RenderTargetStateGrDawnProgram::RenderTargetState25 RenderTargetState() { this->invalidate(); } invalidateGrDawnProgram::RenderTargetState26 void invalidate() { 27 fRenderTargetSize.fWidth = -1; 28 fRenderTargetSize.fHeight = -1; 29 fRenderTargetOrigin = (GrSurfaceOrigin) -1; 30 } 31 32 /** 33 * Gets a float4 that adjusts the position from Skia device coords to GL's normalized device 34 * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is 35 * applied as such: 36 * pos.x = dot(v.xy, pos.xz) 37 * pos.y = dot(v.zw, pos.yz) 38 */ getRTAdjustmentVecGrDawnProgram::RenderTargetState39 void getRTAdjustmentVec(float* destVec) { 40 destVec[0] = 2.f / fRenderTargetSize.fWidth; 41 destVec[1] = -1.f; 42 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { 43 destVec[2] = -2.f / fRenderTargetSize.fHeight; 44 destVec[3] = 1.f; 45 } else { 46 destVec[2] = 2.f / fRenderTargetSize.fHeight; 47 destVec[3] = -1.f; 48 } 49 } 50 }; 51 typedef GrGLSLBuiltinUniformHandles BuiltinUniformHandles; GrDawnProgramGrDawnProgram52 GrDawnProgram(const GrDawnUniformHandler::UniformInfoArray& uniforms, 53 uint32_t geometryUniformSize, 54 uint32_t fragmentUniformSize) 55 : fDataManager(uniforms, geometryUniformSize, fragmentUniformSize) { 56 } 57 dawn::ShaderModule fVSModule; 58 dawn::ShaderModule fFSModule; 59 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; 60 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; 61 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; 62 int fFragmentProcessorCnt; 63 dawn::Buffer fGeometryUniformBuffer; 64 dawn::Buffer fFragmentUniformBuffer; 65 dawn::PipelineLayout fPipelineLayout; 66 dawn::BindGroup fBindGroup; 67 dawn::ColorStateDescriptor fColorState; 68 dawn::DepthStencilStateDescriptor fDepthStencilState; 69 GrDawnProgramDataManager fDataManager; 70 RenderTargetState fRenderTargetState; 71 BuiltinUniformHandles fBuiltinUniformHandles; 72 73 void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin); 74 void setData(const GrPrimitiveProcessor&, const GrRenderTarget*, GrSurfaceOrigin, 75 const GrPipeline&); 76 }; 77 78 class GrDawnProgramBuilder : public GrGLSLProgramBuilder { 79 public: 80 static sk_sp<GrDawnProgram> Build(GrDawnGpu*, 81 GrRenderTarget* renderTarget, 82 GrSurfaceOrigin origin, 83 const GrPipeline&, 84 const GrPrimitiveProcessor&, 85 const GrTextureProxy* const primProcProxies[], 86 dawn::TextureFormat colorFormat, 87 bool hasDepthStencil, 88 dawn::TextureFormat depthStencilFormat, 89 GrProgramDesc* desc); 90 const GrCaps* caps() const override; uniformHandler()91 GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; } uniformHandler()92 const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; } varyingHandler()93 GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; } 94 gpu()95 GrDawnGpu* gpu() const { return fGpu; } 96 97 private: 98 GrDawnProgramBuilder(GrDawnGpu*, 99 GrRenderTarget*, 100 GrSurfaceOrigin, 101 const GrPrimitiveProcessor&, 102 const GrTextureProxy* const primProcProxies[], 103 const GrPipeline&, 104 GrProgramDesc*); 105 dawn::ShaderModule createShaderModule(const GrGLSLShaderBuilder&, SkSL::Program::Kind, 106 SkSL::Program::Inputs* inputs); 107 GrDawnGpu* fGpu; 108 GrDawnVaryingHandler fVaryingHandler; 109 GrDawnUniformHandler fUniformHandler; 110 111 typedef GrGLSLProgramBuilder INHERITED; 112 }; 113 #endif 114