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