reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef GrGpuGLShaders_DEFINED |
| 19 | #define GrGpuGLShaders_DEFINED |
| 20 | |
| 21 | #include "GrGpuGL.h" |
| 22 | |
| 23 | // Programmable OpenGL or OpenGL ES 2.0 |
| 24 | class GrGpuGLShaders : public GrGpuGL { |
| 25 | public: |
| 26 | GrGpuGLShaders(); |
| 27 | virtual ~GrGpuGLShaders(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 28 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | virtual void resetContext(); |
| 30 | |
| 31 | // type of colors used by a program |
| 32 | enum ColorType { |
| 33 | kNone_ColorType, |
| 34 | kAttrib_ColorType, |
| 35 | kUniform_ColorType, |
| 36 | }; |
| 37 | protected: |
| 38 | // overrides from GrGpu |
| 39 | virtual bool flushGraphicsState(PrimitiveType type); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 40 | virtual void setupGeometry(int* startVertex, |
| 41 | int* startIndex, |
| 42 | int vertexCount, |
| 43 | int indexCount); |
| 44 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | private: |
| 46 | void resetContextHelper(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 47 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | // sets the texture matrix uniform for currently bound program |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 49 | void flushTexMatrix(GLint location, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | GrGLTexture::Orientation orientation); |
| 51 | // sets the MVP matrix uniform for currently bound program |
| 52 | void flushMatrix(GLint location); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 53 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | void flushTwoPointRadial(GLint paramsLocation, const GrSamplerState&); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 55 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | // reads shader from array and compiles it with GL, returns shader ID or 0 if failed |
| 57 | GLuint loadShader(GLenum type, const char* src); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 58 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | struct ProgramData; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 60 | // creates a GL program with two shaders attached. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 61 | // Gets the relevant uniform locations. |
| 62 | // Sets the texture sampler if present to texture 0 |
| 63 | // Binds the program |
| 64 | // returns true if succeeded. |
| 65 | bool createProgram(GLuint vshader, |
| 66 | GLuint fshader, |
| 67 | bool hasTexMatrix, |
| 68 | bool hasTexCoords, |
| 69 | ColorType colorType, |
| 70 | bool twoPointRadial, |
| 71 | ProgramData* program); |
| 72 | |
| 73 | // called at flush time to setup the appropriate program |
| 74 | void flushProgram(PrimitiveType type); |
| 75 | |
| 76 | enum Programs { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 77 | // use vertex coordinates |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 78 | kTextureVertCoords_Program = 0, |
| 79 | kTextureVertCoordsProj_Program, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 80 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 81 | // use separate tex coords |
| 82 | kTextureTexCoords_Program, |
| 83 | kTextureTexCoordsProj_Program, |
| 84 | |
| 85 | // constant color texture, no proj |
| 86 | // verts as a tex coords |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 87 | kTextureVertCoordsNoColor_Program, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 88 | |
| 89 | // constant color texture, no proj |
| 90 | // separate tex coords |
| 91 | kTextureTexCoordsNoColor_Program, |
| 92 | |
| 93 | // special program for text glyphs |
| 94 | kText_Program, |
| 95 | |
| 96 | // programs for radial texture lookup |
| 97 | kRadialTextureVertCoords_Program, |
| 98 | kRadialTextureTexCoords_Program, |
| 99 | |
| 100 | // programs for sweep texture lookup |
| 101 | kSweepTextureVertCoords_Program, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 102 | kSweepTextureTexCoords_Program, |
| 103 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | // programs for two-point radial lookup |
| 105 | kTwoPointRadialTextureVertCoords_Program, |
| 106 | kTwoPointRadialTextureTexCoords_Program, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 107 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | // color only drawing |
| 109 | kNoTexture_Program, |
| 110 | |
| 111 | kProgramCount |
| 112 | }; |
| 113 | |
| 114 | // Records per-program information |
| 115 | // we can specify the attribute locations so that they are constant |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 116 | // across our shaders. But the driver determines the uniform locations |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | // at link time. We don't need to remember the sampler uniform location |
| 118 | // because we will bind a texture slot to it and never change it |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 119 | // Uniforms are program-local so we can't rely on fHWState to hold the |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 120 | // previous uniform state after a program change. |
| 121 | struct ProgramData { |
| 122 | // IDs |
| 123 | GLuint fVShaderID; |
| 124 | GLuint fFShaderID; |
| 125 | GLuint fProgramID; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 126 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | // shader uniform locations (-1 if shader doesn't use them) |
| 128 | GLint fMatrixLocation; |
| 129 | GLint fTexMatrixLocation; |
| 130 | GLint fColorLocation; |
| 131 | GLint fTwoPointParamsLocation; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 132 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | ColorType fColorType; |
| 134 | |
| 135 | // these reflect the current values of uniforms |
| 136 | // (GL uniform values travel with program) |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 137 | GrMatrix fViewMatrix; |
| 138 | GrMatrix fTextureMatrices[kNumStages]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | GrColor fColor; |
| 140 | GrGLTexture::Orientation fTextureOrientation; |
| 141 | GrScalar fRadial2CenterX1; |
| 142 | GrScalar fRadial2Radius0; |
| 143 | bool fRadial2PosRoot; |
| 144 | }; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 145 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 146 | ProgramData fPrograms[kProgramCount]; |
| 147 | Programs fHWProgram; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 148 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | GrGLTexture::Orientation fTextureOrientation; |
| 150 | |
| 151 | typedef GrGpuGL INHERITED; |
| 152 | }; |
| 153 | |
| 154 | #endif |