| 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 | |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 12 | #include "builders/GrGLProgramBuilder.h" |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 13 | #include "GrDrawState.h" |
| robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 14 | #include "GrGLContext.h" |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 15 | #include "GrGLProgramDesc.h" |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 16 | #include "GrGLSL.h" |
| bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 17 | #include "GrGLTexture.h" |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 18 | #include "GrGLProgramDataManager.h" |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 19 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 20 | #include "SkString.h" |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 21 | #include "SkXfermode.h" |
| 22 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 23 | class GrGLProcessor; |
| 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; |
| 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: |
| bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 38 | SK_DECLARE_INST_COUNT(GrGLProgram) |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 39 | |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 40 | typedef GrGLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles; |
| kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 41 | |
| bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 42 | virtual ~GrGLProgram(); |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 43 | |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Call to abandon GL objects owned by this program. |
| 46 | */ |
| bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 47 | void abandon(); |
| 48 | |
| joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 49 | const GrProgramDesc& getDesc() { return fDesc; } |
| bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 50 | |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 51 | /** |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 52 | * Gets the GL program ID for this program. |
| 53 | */ |
| kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 54 | GrGLuint programID() const { return fProgramID; } |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 55 | |
| 56 | /** |
| joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 57 | * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device |
| 58 | * space and to make device space positions have the correct origin for processors that require |
| 59 | * them. |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 60 | */ |
| joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 61 | struct RenderTargetState { |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 62 | SkISize fRenderTargetSize; |
| 63 | GrSurfaceOrigin fRenderTargetOrigin; |
| 64 | |
| joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 65 | RenderTargetState() { this->invalidate(); } |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 66 | void invalidate() { |
| bsalomon@google.com | 45a412e | 2013-02-13 16:13:13 +0000 | [diff] [blame] | 67 | fRenderTargetSize.fWidth = -1; |
| 68 | fRenderTargetSize.fHeight = -1; |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 69 | fRenderTargetOrigin = (GrSurfaceOrigin) -1; |
| 70 | } |
| commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 73 | * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device |
| 74 | * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is |
| 75 | * applied as such: |
| 76 | * pos.x = dot(v.xy, pos.xz) |
| 77 | * pos.y = dot(v.zq, pos.yz) |
| 78 | */ |
| 79 | void getRTAdjustmentVec(GrGLfloat* destVec) { |
| 80 | destVec[0] = 2.f / fRenderTargetSize.fWidth; |
| 81 | destVec[1] = -1.f; |
| 82 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 83 | destVec[2] = -2.f / fRenderTargetSize.fHeight; |
| 84 | destVec[3] = 1.f; |
| 85 | } else { |
| 86 | destVec[2] = 2.f / fRenderTargetSize.fHeight; |
| 87 | destVec[3] = -1.f; |
| 88 | } |
| 89 | } |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | /** |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 93 | * This function uploads uniforms and calls each GrGLProcessor's setData. It is called before a |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 94 | * draw occurs using the program after the program has already been bound. It also uses the |
| bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 95 | * GrGLGpu object to bind the textures required by the GrGLProcessors. The color and coverage |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 96 | * stages come from GrGLProgramDesc::Build(). |
| bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 97 | */ |
| joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 98 | void setData(const GrPrimitiveProcessor&, const GrOptDrawState&, const GrBatchTracker&); |
| 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: |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 101 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 102 | typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray; |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 103 | |
| bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 104 | GrGLProgram(GrGLGpu*, |
| joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 105 | const GrProgramDesc&, |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 106 | const BuiltinUniformHandles&, |
| 107 | GrGLuint programID, |
| 108 | const UniformInfoArray&, |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 109 | GrGLInstalledGeoProc* geometryProcessor, |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 110 | GrGLInstalledXferProc* xferProcessor, |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 111 | GrGLInstalledFragProcs* fragmentProcessors); |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 112 | |
| commit-bot@chromium.org | a05fa06 | 2014-05-30 18:55:03 +0000 | [diff] [blame] | 113 | // Sets the texture units for samplers. |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 114 | void initSamplerUniforms(); |
| joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 115 | template <class Proc> |
| 116 | void initSamplers(Proc*, int* texUnitIdx); |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 117 | |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 118 | // A templated helper to loop over effects, set the transforms(via subclass) and bind textures |
| joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 119 | void setFragmentData(const GrPrimitiveProcessor&, const GrOptDrawState&); |
| 120 | virtual void setTransformData(const GrPrimitiveProcessor&, |
| joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 121 | const GrPendingFragmentStage&, |
| 122 | int index, |
| joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 123 | GrGLInstalledFragProc*); |
| joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 124 | template <class Proc> |
| 125 | void bindTextures(const Proc*, const GrProcessor&); |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * Legacy NVPR needs a hook here to flush path tex gen settings. |
| 129 | * TODO when legacy nvpr is removed, remove this call. |
| 130 | */ |
| joshualitt | b77e4b6 | 2015-01-21 10:16:17 -0800 | [diff] [blame] | 131 | virtual void didSetData() {} |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 132 | |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 133 | // Helper for setData() that sets the view matrix and loads the render target height uniform |
| joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 134 | void setRenderTargetState(const GrPrimitiveProcessor&, const GrOptDrawState&); |
| 135 | virtual void onSetRenderTargetState(const GrPrimitiveProcessor&, const GrOptDrawState&); |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 136 | |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 137 | // these reflect the current values of uniforms (GL uniform values travel with program) |
| joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 138 | RenderTargetState fRenderTargetState; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 139 | GrColor fColor; |
| egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 140 | uint8_t fCoverage; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 141 | int fDstCopyTexUnit; |
| 142 | BuiltinUniformHandles fBuiltinUniformHandles; |
| 143 | GrGLuint fProgramID; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 144 | |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 145 | // the installed effects |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 146 | SkAutoTDelete<GrGLInstalledGeoProc> fGeometryProcessor; |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 147 | SkAutoTDelete<GrGLInstalledXferProc> fXferProcessor; |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 148 | SkAutoTUnref<GrGLInstalledFragProcs> fFragmentProcessors; |
| skia.committer@gmail.com | 9681eeb | 2014-05-30 03:06:10 +0000 | [diff] [blame] | 149 | |
| joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 150 | GrProgramDesc fDesc; |
| bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 151 | GrGLGpu* fGpu; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 152 | GrGLProgramDataManager fProgramDataManager; |
| 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 | friend class GrGLProgramBuilder; |
| commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 155 | |
| commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 156 | typedef SkRefCnt INHERITED; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 159 | /* |
| 160 | * Below are slight specializations of the program object for the different types of programs |
| 161 | * The default GrGL programs consist of at the very least a vertex and fragment shader. |
| 162 | * Legacy Nvpr only has a fragment shader, 1.3+ Nvpr ignores the vertex shader, but both require |
| 163 | * specialized methods for setting transform data. Both types of NVPR also require setting the |
| 164 | * projection matrix through a special function call |
| 165 | */ |
| joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 166 | class GrGLNvprProgram : public GrGLProgram { |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 167 | protected: |
| bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 168 | GrGLNvprProgram(GrGLGpu*, |
| joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 169 | const GrProgramDesc&, |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 170 | const BuiltinUniformHandles&, |
| 171 | GrGLuint programID, |
| 172 | const UniformInfoArray&, |
| joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 173 | GrGLInstalledGeoProc*, |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 174 | GrGLInstalledXferProc* xferProcessor, |
| joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 175 | GrGLInstalledFragProcs* fragmentProcessors); |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 176 | |
| joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 177 | private: |
| joshualitt | b77e4b6 | 2015-01-21 10:16:17 -0800 | [diff] [blame] | 178 | void didSetData() SK_OVERRIDE; |
| joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 179 | virtual void setTransformData(const GrPrimitiveProcessor&, |
| joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 180 | const GrPendingFragmentStage&, |
| 181 | int index, |
| 182 | GrGLInstalledFragProc*) SK_OVERRIDE; |
| joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 183 | virtual void onSetRenderTargetState(const GrPrimitiveProcessor&, const GrOptDrawState&); |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 184 | |
| 185 | friend class GrGLNvprProgramBuilder; |
| 186 | |
| joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 187 | typedef GrGLProgram INHERITED; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 190 | #endif |