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" |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame^] | 13 | #include "GrPrimitiveProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 14 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLUniformHandler.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 16 | |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 17 | class GrGLSLFragmentProcessor; |
| 18 | class GrGLSLPrimitiveProcessor; |
| 19 | class GrGLSLXferProcessor; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 20 | class GrPipeline; |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 21 | class GrPrimitiveProcessor; |
| 22 | class GrRenderTargetProxy; |
| 23 | class GrResourceIOProcessor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame^] | 26 | * This class manages a GPU program and records per-program information. It also records the vertex |
| 27 | * and instance attribute layouts that are to be used with the program. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 28 | */ |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 29 | class GrGLProgram : public SkRefCnt { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 30 | public: |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame^] | 31 | /** |
| 32 | * This class has its own Attribute representation as it does not need the name and we don't |
| 33 | * want to worry about copying the name string to memory with life time of GrGLProgram. |
| 34 | * Additionally, these store the attribute location. |
| 35 | */ |
| 36 | struct Attribute { |
| 37 | GrVertexAttribType fType; |
| 38 | int fOffset; |
| 39 | GrGLint fLocation; |
| 40 | GrPrimitiveProcessor::Attribute::InputRate fInputRate; |
| 41 | }; |
| 42 | |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 43 | using UniformHandle = GrGLSLProgramDataManager::UniformHandle; |
| 44 | using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray; |
| 45 | using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray; |
| 46 | |
| 47 | GrGLProgram(GrGLGpu*, |
| 48 | const GrGLSLBuiltinUniformHandles&, |
| 49 | GrGLuint programID, |
| 50 | const UniformInfoArray& uniforms, |
| 51 | const UniformInfoArray& textureSamplers, |
| 52 | const UniformInfoArray& texelBuffers, |
| 53 | const VaryingInfoArray&, // used for NVPR only currently |
| 54 | std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor, |
| 55 | std::unique_ptr<GrGLSLXferProcessor> xferProcessor, |
| 56 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame^] | 57 | int fragmentProcessorCnt, |
| 58 | std::unique_ptr<Attribute[]>, |
| 59 | int attributeCnt, |
| 60 | int vertexStride, |
| 61 | int instanceStride); |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 62 | |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 63 | ~GrGLProgram(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 65 | /** |
| 66 | * Call to abandon GL objects owned by this program. |
| 67 | */ |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 68 | void abandon(); |
| 69 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 70 | /** |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 71 | * Gets the GL program ID for this program. |
| 72 | */ |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 73 | GrGLuint programID() const { return fProgramID; } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 76 | * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device |
| 77 | * space and to make device space positions have the correct origin for processors that require |
| 78 | * them. |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 79 | */ |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 80 | struct RenderTargetState { |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 81 | SkISize fRenderTargetSize; |
| 82 | GrSurfaceOrigin fRenderTargetOrigin; |
| 83 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 84 | RenderTargetState() { this->invalidate(); } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 85 | void invalidate() { |
bsalomon@google.com | 45a412e | 2013-02-13 16:13:13 +0000 | [diff] [blame] | 86 | fRenderTargetSize.fWidth = -1; |
| 87 | fRenderTargetSize.fHeight = -1; |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 88 | fRenderTargetOrigin = (GrSurfaceOrigin) -1; |
| 89 | } |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 90 | |
| 91 | /** |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 92 | * Gets a float4 that adjusts the position from Skia device coords to GL's normalized device |
| 93 | * 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] | 94 | * applied as such: |
| 95 | * pos.x = dot(v.xy, pos.xz) |
robertphillips | ef4ba3d | 2015-09-17 11:21:06 -0700 | [diff] [blame] | 96 | * pos.y = dot(v.zw, pos.yz) |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 97 | */ |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 98 | void getRTAdjustmentVec(float* destVec) { |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 99 | destVec[0] = 2.f / fRenderTargetSize.fWidth; |
| 100 | destVec[1] = -1.f; |
| 101 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 102 | destVec[2] = -2.f / fRenderTargetSize.fHeight; |
| 103 | destVec[3] = 1.f; |
| 104 | } else { |
| 105 | destVec[2] = 2.f / fRenderTargetSize.fHeight; |
| 106 | destVec[3] = -1.f; |
| 107 | } |
| 108 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /** |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 112 | * This function uploads uniforms, calls each GrGL*Processor's setData, and retrieves the |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 113 | * textures that need to be bound on each unit. It is the caller's responsibility to ensure |
| 114 | * the program is bound before calling, and to bind the outgoing textures to their respective |
| 115 | * 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] | 116 | */ |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 117 | void setData(const GrPrimitiveProcessor&, const GrPipeline&); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 118 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 119 | /** |
| 120 | * This function retrieves the textures that need to be used by each GrGL*Processor, and |
| 121 | * ensures that any textures requiring mipmaps have their mipmaps correctly built. |
| 122 | */ |
| 123 | void generateMipmaps(const GrPrimitiveProcessor&, const GrPipeline&); |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame^] | 124 | int vertexStride() const { return fVertexStride; } |
| 125 | int instanceStride() const { return fInstanceStride; } |
| 126 | |
| 127 | int numAttributes() const { return fAttributeCnt; } |
| 128 | const Attribute& attribute(int i) const { return fAttributes[i]; } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 129 | |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 130 | private: |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 131 | // 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] | 132 | void setFragmentData(const GrPrimitiveProcessor&, const GrPipeline&, int* nextTexSamplerIdx, |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 133 | int* nextTexelBufferIdx); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 134 | |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 135 | // 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] | 136 | void setRenderTargetState(const GrPrimitiveProcessor&, const GrRenderTargetProxy*); |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 137 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 138 | // 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] | 139 | void bindTextures(const GrResourceIOProcessor&, int* nextSamplerIdx, int* nextTexelBufferIdx); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 140 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 141 | // Helper for generateMipmaps() that ensures mipmaps are up to date |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 142 | void generateMipmaps(const GrResourceIOProcessor&); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 143 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 144 | // these reflect the current values of uniforms (GL uniform values travel with program) |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 145 | RenderTargetState fRenderTargetState; |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 146 | GrGLSLBuiltinUniformHandles fBuiltinUniformHandles; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 147 | GrGLuint fProgramID; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 148 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 149 | // the installed effects |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame^] | 150 | std::unique_ptr<GrGLSLPrimitiveProcessor> fPrimitiveProcessor; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 151 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 152 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; |
| 153 | int fFragmentProcessorCnt; |
skia.committer@gmail.com | 9681eeb | 2014-05-30 03:06:10 +0000 | [diff] [blame] | 154 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame^] | 155 | std::unique_ptr<Attribute[]> fAttributes; |
| 156 | int fAttributeCnt; |
| 157 | int fVertexStride; |
| 158 | int fInstanceStride; |
| 159 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 160 | GrGLGpu* fGpu; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 161 | GrGLProgramDataManager fProgramDataManager; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 162 | |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 163 | int fNumTextureSamplers; |
| 164 | int fNumTexelBuffers; |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 165 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 166 | typedef SkRefCnt INHERITED; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 169 | #endif |