Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 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 | |
| 25 | RenderTargetState() { this->invalidate(); } |
| 26 | 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 | */ |
| 39 | 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; |
| 52 | GrDawnProgram(const GrDawnUniformHandler::UniformInfoArray& uniforms, |
| 53 | uint32_t geometryUniformSize, |
| 54 | uint32_t fragmentUniformSize) |
| 55 | : fDataManager(uniforms, geometryUniformSize, fragmentUniformSize) { |
| 56 | } |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 57 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
| 58 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
| 59 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; |
| 60 | int fFragmentProcessorCnt; |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 61 | dawn::BindGroupLayout fBindGroupLayout; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 62 | dawn::RenderPipeline fRenderPipeline; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 63 | GrDawnProgramDataManager fDataManager; |
| 64 | RenderTargetState fRenderTargetState; |
| 65 | BuiltinUniformHandles fBuiltinUniformHandles; |
| 66 | |
| 67 | void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 68 | dawn::BindGroup setData(GrDawnGpu* gpu, const GrRenderTarget*, GrSurfaceOrigin origin, |
| 69 | const GrPrimitiveProcessor&, const GrPipeline&, |
| 70 | const GrTextureProxy* const primProcTextures[]); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | class GrDawnProgramBuilder : public GrGLSLProgramBuilder { |
| 74 | public: |
| 75 | static sk_sp<GrDawnProgram> Build(GrDawnGpu*, |
| 76 | GrRenderTarget* renderTarget, |
| 77 | GrSurfaceOrigin origin, |
| 78 | const GrPipeline&, |
| 79 | const GrPrimitiveProcessor&, |
| 80 | const GrTextureProxy* const primProcProxies[], |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 81 | GrPrimitiveType primitiveType, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 82 | dawn::TextureFormat colorFormat, |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 83 | bool hasDepthStencil, |
| 84 | dawn::TextureFormat depthStencilFormat, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 85 | GrProgramDesc* desc); |
| 86 | const GrCaps* caps() const override; |
| 87 | GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; } |
| 88 | const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; } |
| 89 | GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; } |
| 90 | |
| 91 | GrDawnGpu* gpu() const { return fGpu; } |
| 92 | |
| 93 | private: |
| 94 | GrDawnProgramBuilder(GrDawnGpu*, |
| 95 | GrRenderTarget*, |
| 96 | GrSurfaceOrigin, |
| 97 | const GrPrimitiveProcessor&, |
| 98 | const GrTextureProxy* const primProcProxies[], |
| 99 | const GrPipeline&, |
| 100 | GrProgramDesc*); |
| 101 | dawn::ShaderModule createShaderModule(const GrGLSLShaderBuilder&, SkSL::Program::Kind, |
| 102 | SkSL::Program::Inputs* inputs); |
| 103 | GrDawnGpu* fGpu; |
| 104 | GrDawnVaryingHandler fVaryingHandler; |
| 105 | GrDawnUniformHandler fUniformHandler; |
| 106 | |
| 107 | typedef GrGLSLProgramBuilder INHERITED; |
| 108 | }; |
| 109 | #endif |