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