junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 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. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 9 | #ifndef GrGLProgram_DEFINED |
| 10 | #define GrGLProgram_DEFINED |
| 11 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 12 | #include "GrGLProgramDataManager.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 13 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLUniformHandler.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 15 | |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame^] | 16 | class GrGLSLFragmentProcessor; |
| 17 | class GrGLSLPrimitiveProcessor; |
| 18 | class GrGLSLXferProcessor; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 19 | class GrPipeline; |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame^] | 20 | class GrPrimitiveProcessor; |
| 21 | class GrRenderTargetProxy; |
| 22 | class GrResourceIOProcessor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * This class manages a GPU program and records per-program information. |
| 26 | * We can specify the attribute locations so that they are constant |
| 27 | * across our shaders. But the driver determines the uniform locations |
| 28 | * at link time. We don't need to remember the sampler uniform location |
| 29 | * because we will bind a texture slot to it and never change it |
| 30 | * Uniforms are program-local so we can't rely on fHWState to hold the |
| 31 | * previous uniform state after a program change. |
| 32 | */ |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 33 | class GrGLProgram : public SkRefCnt { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 34 | public: |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame^] | 35 | using UniformHandle = GrGLSLProgramDataManager::UniformHandle; |
| 36 | using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray; |
| 37 | using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray; |
| 38 | |
| 39 | GrGLProgram(GrGLGpu*, |
| 40 | const GrGLSLBuiltinUniformHandles&, |
| 41 | GrGLuint programID, |
| 42 | const UniformInfoArray& uniforms, |
| 43 | const UniformInfoArray& textureSamplers, |
| 44 | const UniformInfoArray& texelBuffers, |
| 45 | const VaryingInfoArray&, // used for NVPR only currently |
| 46 | std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor, |
| 47 | std::unique_ptr<GrGLSLXferProcessor> xferProcessor, |
| 48 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors, |
| 49 | int fragmentProcessorCnt); |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 50 | |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 51 | ~GrGLProgram(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 52 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 53 | /** |
| 54 | * Call to abandon GL objects owned by this program. |
| 55 | */ |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 56 | void abandon(); |
| 57 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 58 | /** |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 59 | * Gets the GL program ID for this program. |
| 60 | */ |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 61 | GrGLuint programID() const { return fProgramID; } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 64 | * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device |
| 65 | * space and to make device space positions have the correct origin for processors that require |
| 66 | * them. |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 67 | */ |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 68 | struct RenderTargetState { |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 69 | SkISize fRenderTargetSize; |
| 70 | GrSurfaceOrigin fRenderTargetOrigin; |
| 71 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 72 | RenderTargetState() { this->invalidate(); } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 73 | void invalidate() { |
bsalomon@google.com | 45a412e | 2013-02-13 16:13:13 +0000 | [diff] [blame] | 74 | fRenderTargetSize.fWidth = -1; |
| 75 | fRenderTargetSize.fHeight = -1; |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 76 | fRenderTargetOrigin = (GrSurfaceOrigin) -1; |
| 77 | } |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 78 | |
| 79 | /** |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 80 | * Gets a float4 that adjusts the position from Skia device coords to GL's normalized device |
| 81 | * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 82 | * applied as such: |
| 83 | * pos.x = dot(v.xy, pos.xz) |
robertphillips | ef4ba3d | 2015-09-17 11:21:06 -0700 | [diff] [blame] | 84 | * pos.y = dot(v.zw, pos.yz) |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 85 | */ |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 86 | void getRTAdjustmentVec(float* destVec) { |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 87 | destVec[0] = 2.f / fRenderTargetSize.fWidth; |
| 88 | destVec[1] = -1.f; |
| 89 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 90 | destVec[2] = -2.f / fRenderTargetSize.fHeight; |
| 91 | destVec[3] = 1.f; |
| 92 | } else { |
| 93 | destVec[2] = 2.f / fRenderTargetSize.fHeight; |
| 94 | destVec[3] = -1.f; |
| 95 | } |
| 96 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /** |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 100 | * This function uploads uniforms, calls each GrGL*Processor's setData, and retrieves the |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 101 | * textures that need to be bound on each unit. It is the caller's responsibility to ensure |
| 102 | * the program is bound before calling, and to bind the outgoing textures to their respective |
| 103 | * units upon return. (Each index in the array corresponds to its matching GL texture unit.) |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 104 | */ |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 105 | void setData(const GrPrimitiveProcessor&, const GrPipeline&); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 106 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 107 | /** |
| 108 | * This function retrieves the textures that need to be used by each GrGL*Processor, and |
| 109 | * ensures that any textures requiring mipmaps have their mipmaps correctly built. |
| 110 | */ |
| 111 | void generateMipmaps(const GrPrimitiveProcessor&, const GrPipeline&); |
| 112 | |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame^] | 113 | private: |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 114 | // A helper to loop over effects, set the transforms (via subclass) and bind textures |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 115 | void setFragmentData(const GrPrimitiveProcessor&, const GrPipeline&, int* nextTexSamplerIdx, |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 116 | int* nextTexelBufferIdx); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 117 | |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 118 | // Helper for setData() that sets the view matrix and loads the render target height uniform |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 119 | void setRenderTargetState(const GrPrimitiveProcessor&, const GrRenderTargetProxy*); |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 120 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 121 | // Helper for setData() that binds textures and texel buffers to the appropriate texture units |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 122 | void bindTextures(const GrResourceIOProcessor&, int* nextSamplerIdx, int* nextTexelBufferIdx); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 123 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 124 | // Helper for generateMipmaps() that ensures mipmaps are up to date |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 125 | void generateMipmaps(const GrResourceIOProcessor&); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 126 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 127 | // these reflect the current values of uniforms (GL uniform values travel with program) |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 128 | RenderTargetState fRenderTargetState; |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame^] | 129 | GrGLSLBuiltinUniformHandles fBuiltinUniformHandles; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 130 | GrGLuint fProgramID; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 131 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 132 | // the installed effects |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 133 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
| 134 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 135 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; |
| 136 | int fFragmentProcessorCnt; |
skia.committer@gmail.com | 9681eeb | 2014-05-30 03:06:10 +0000 | [diff] [blame] | 137 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 138 | GrGLGpu* fGpu; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 139 | GrGLProgramDataManager fProgramDataManager; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 140 | |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 141 | int fNumTextureSamplers; |
| 142 | int fNumTexelBuffers; |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 143 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 144 | typedef SkRefCnt INHERITED; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 147 | #endif |