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" |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 13 | #include "GrGLProgramDesc.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 | #include "SkXfermode.h" |
| 21 | |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 22 | #include "builders/GrGLProgramBuilder.h" |
| 23 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 24 | class GrGLInstalledProcessors; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 25 | class GrGLProgramBuilder; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 26 | class GrPipeline; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * This class manages a GPU program and records per-program information. |
| 30 | * We can specify the attribute locations so that they are constant |
| 31 | * across our shaders. But the driver determines the uniform locations |
| 32 | * at link time. We don't need to remember the sampler uniform location |
| 33 | * because we will bind a texture slot to it and never change it |
| 34 | * Uniforms are program-local so we can't rely on fHWState to hold the |
| 35 | * previous uniform state after a program change. |
| 36 | */ |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 37 | class GrGLProgram : public SkRefCnt { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 38 | public: |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 39 | typedef GrGLSLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles; |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 40 | |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 41 | ~GrGLProgram(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Call to abandon GL objects owned by this program. |
| 45 | */ |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 46 | void abandon(); |
| 47 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 48 | const GrProgramDesc& getDesc() { return fDesc; } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 49 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 50 | /** |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 51 | * Gets the GL program ID for this program. |
| 52 | */ |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 53 | GrGLuint programID() const { return fProgramID; } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 56 | * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device |
| 57 | * space and to make device space positions have the correct origin for processors that require |
| 58 | * them. |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 59 | */ |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 60 | struct RenderTargetState { |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 61 | SkISize fRenderTargetSize; |
| 62 | GrSurfaceOrigin fRenderTargetOrigin; |
| 63 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 64 | RenderTargetState() { this->invalidate(); } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 65 | void invalidate() { |
bsalomon@google.com | 45a412e | 2013-02-13 16:13:13 +0000 | [diff] [blame] | 66 | fRenderTargetSize.fWidth = -1; |
| 67 | fRenderTargetSize.fHeight = -1; |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 68 | fRenderTargetOrigin = (GrSurfaceOrigin) -1; |
| 69 | } |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 70 | |
| 71 | /** |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 72 | * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device |
| 73 | * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is |
| 74 | * applied as such: |
| 75 | * pos.x = dot(v.xy, pos.xz) |
robertphillips | ef4ba3d | 2015-09-17 11:21:06 -0700 | [diff] [blame] | 76 | * pos.y = dot(v.zw, pos.yz) |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 77 | */ |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 78 | void getRTAdjustmentVec(float* destVec) { |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 79 | destVec[0] = 2.f / fRenderTargetSize.fWidth; |
| 80 | destVec[1] = -1.f; |
| 81 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 82 | destVec[2] = -2.f / fRenderTargetSize.fHeight; |
| 83 | destVec[3] = 1.f; |
| 84 | } else { |
| 85 | destVec[2] = 2.f / fRenderTargetSize.fHeight; |
| 86 | destVec[3] = -1.f; |
| 87 | } |
| 88 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /** |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 92 | * This function uploads uniforms, calls each GrGL*Processor's setData, and retrieves the |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 93 | * textures that need to be bound on each unit. It is the caller's responsibility to ensure |
| 94 | * the program is bound before calling, and to bind the outgoing textures to their respective |
| 95 | * 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] | 96 | */ |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 97 | void setData(const GrPrimitiveProcessor&, const GrPipeline&, |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 98 | SkTArray<const GrTextureAccess*>* textureBindings); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 99 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 100 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 101 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 102 | typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 103 | typedef GrGLProgramDataManager::VaryingInfoArray VaryingInfoArray; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 104 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 105 | GrGLProgram(GrGLGpu*, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 106 | const GrProgramDesc&, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 107 | const BuiltinUniformHandles&, |
| 108 | GrGLuint programID, |
| 109 | const UniformInfoArray&, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 110 | const VaryingInfoArray&, // used for NVPR only currently |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 111 | GrGLSLPrimitiveProcessor* geometryProcessor, |
| 112 | GrGLSLXferProcessor* xferProcessor, |
| 113 | const GrGLSLFragProcs& fragmentProcessors, |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 114 | SkTArray<UniformHandle>* passSamplerUniforms); |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 115 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 116 | // A templated helper to loop over effects, set the transforms(via subclass) and bind textures |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 117 | void setFragmentData(const GrPrimitiveProcessor&, const GrPipeline&, |
| 118 | SkTArray<const GrTextureAccess*>* textureBindings); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 119 | void setTransformData(const GrPrimitiveProcessor&, |
| 120 | const GrFragmentProcessor&, |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 121 | int index); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 122 | |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 123 | // Helper for setData() that sets the view matrix and loads the render target height uniform |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 124 | void setRenderTargetState(const GrPrimitiveProcessor&, const GrPipeline&); |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 125 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 126 | // these reflect the current values of uniforms (GL uniform values travel with program) |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 127 | RenderTargetState fRenderTargetState; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 128 | BuiltinUniformHandles fBuiltinUniformHandles; |
| 129 | GrGLuint fProgramID; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 130 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 131 | // the installed effects |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 132 | SkAutoTDelete<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
| 133 | SkAutoTDelete<GrGLSLXferProcessor> fXferProcessor; |
| 134 | GrGLSLFragProcs fFragmentProcessors; |
skia.committer@gmail.com | 9681eeb | 2014-05-30 03:06:10 +0000 | [diff] [blame] | 135 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 136 | GrProgramDesc fDesc; |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 137 | GrGLGpu* fGpu; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 138 | GrGLProgramDataManager fProgramDataManager; |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 139 | SkTArray<UniformHandle> fSamplerUniforms; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 140 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 141 | friend class GrGLProgramBuilder; |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 142 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 143 | typedef SkRefCnt INHERITED; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 146 | #endif |