Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef GFX_OGLSHADERPROGRAM_H |
michael@0 | 7 | #define GFX_OGLSHADERPROGRAM_H |
michael@0 | 8 | |
michael@0 | 9 | #include "GLContext.h" // for fast inlines of glUniform* |
michael@0 | 10 | #include "gfx3DMatrix.h" // for gfx3DMatrix |
michael@0 | 11 | #include "gfxTypes.h" |
michael@0 | 12 | #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc |
michael@0 | 13 | #include "mozilla/RefPtr.h" // for RefPtr |
michael@0 | 14 | #include "mozilla/gfx/Matrix.h" // for Matrix4x4 |
michael@0 | 15 | #include "mozilla/gfx/Rect.h" // for Rect |
michael@0 | 16 | #include "mozilla/gfx/Types.h" |
michael@0 | 17 | #include "nsDebug.h" // for NS_ASSERTION |
michael@0 | 18 | #include "nsPoint.h" // for nsIntPoint |
michael@0 | 19 | #include "nsTArray.h" // for nsTArray |
michael@0 | 20 | #include "mozilla/layers/CompositorTypes.h" |
michael@0 | 21 | |
michael@0 | 22 | #include <string> |
michael@0 | 23 | |
michael@0 | 24 | struct gfxRGBA; |
michael@0 | 25 | struct nsIntRect; |
michael@0 | 26 | |
michael@0 | 27 | namespace mozilla { |
michael@0 | 28 | namespace layers { |
michael@0 | 29 | |
michael@0 | 30 | class Layer; |
michael@0 | 31 | |
michael@0 | 32 | enum ShaderFeatures { |
michael@0 | 33 | ENABLE_RENDER_COLOR=0x01, |
michael@0 | 34 | ENABLE_TEXTURE_RECT=0x02, |
michael@0 | 35 | ENABLE_TEXTURE_EXTERNAL=0x04, |
michael@0 | 36 | ENABLE_TEXTURE_YCBCR=0x08, |
michael@0 | 37 | ENABLE_TEXTURE_COMPONENT_ALPHA=0x10, |
michael@0 | 38 | ENABLE_TEXTURE_NO_ALPHA=0x20, |
michael@0 | 39 | ENABLE_TEXTURE_RB_SWAP=0x40, |
michael@0 | 40 | ENABLE_OPACITY=0x80, |
michael@0 | 41 | ENABLE_BLUR=0x100, |
michael@0 | 42 | ENABLE_COLOR_MATRIX=0x200, |
michael@0 | 43 | ENABLE_MASK_2D=0x400, |
michael@0 | 44 | ENABLE_MASK_3D=0x800 |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | class KnownUniform { |
michael@0 | 48 | public: |
michael@0 | 49 | enum KnownUniformName { |
michael@0 | 50 | NotAKnownUniform = -1, |
michael@0 | 51 | |
michael@0 | 52 | LayerTransform = 0, |
michael@0 | 53 | MaskQuadTransform, |
michael@0 | 54 | LayerQuadTransform, |
michael@0 | 55 | MatrixProj, |
michael@0 | 56 | TextureTransform, |
michael@0 | 57 | RenderTargetOffset, |
michael@0 | 58 | LayerOpacity, |
michael@0 | 59 | Texture, |
michael@0 | 60 | YTexture, |
michael@0 | 61 | CbTexture, |
michael@0 | 62 | CrTexture, |
michael@0 | 63 | BlackTexture, |
michael@0 | 64 | WhiteTexture, |
michael@0 | 65 | MaskTexture, |
michael@0 | 66 | RenderColor, |
michael@0 | 67 | TexCoordMultiplier, |
michael@0 | 68 | TexturePass2, |
michael@0 | 69 | |
michael@0 | 70 | KnownUniformCount |
michael@0 | 71 | }; |
michael@0 | 72 | |
michael@0 | 73 | KnownUniform() |
michael@0 | 74 | { |
michael@0 | 75 | mName = NotAKnownUniform; |
michael@0 | 76 | mNameString = nullptr; |
michael@0 | 77 | mLocation = -1; |
michael@0 | 78 | memset(&mValue, 0, sizeof(mValue)); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | bool UpdateUniform(int32_t i1) { |
michael@0 | 82 | if (mLocation == -1) return false; |
michael@0 | 83 | if (mValue.i1 != i1) { |
michael@0 | 84 | mValue.i1 = i1; |
michael@0 | 85 | return true; |
michael@0 | 86 | } |
michael@0 | 87 | return false; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | bool UpdateUniform(float f1) { |
michael@0 | 91 | if (mLocation == -1) return false; |
michael@0 | 92 | if (mValue.f1 != f1) { |
michael@0 | 93 | mValue.f1 = f1; |
michael@0 | 94 | return true; |
michael@0 | 95 | } |
michael@0 | 96 | return false; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | bool UpdateUniform(float f1, float f2) { |
michael@0 | 100 | if (mLocation == -1) return false; |
michael@0 | 101 | if (mValue.f16v[0] != f1 || |
michael@0 | 102 | mValue.f16v[1] != f2) |
michael@0 | 103 | { |
michael@0 | 104 | mValue.f16v[0] = f1; |
michael@0 | 105 | mValue.f16v[1] = f2; |
michael@0 | 106 | return true; |
michael@0 | 107 | } |
michael@0 | 108 | return false; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | bool UpdateUniform(float f1, float f2, float f3, float f4) { |
michael@0 | 112 | if (mLocation == -1) return false; |
michael@0 | 113 | if (mValue.f16v[0] != f1 || |
michael@0 | 114 | mValue.f16v[1] != f2 || |
michael@0 | 115 | mValue.f16v[2] != f3 || |
michael@0 | 116 | mValue.f16v[3] != f4) |
michael@0 | 117 | { |
michael@0 | 118 | mValue.f16v[0] = f1; |
michael@0 | 119 | mValue.f16v[1] = f2; |
michael@0 | 120 | mValue.f16v[2] = f3; |
michael@0 | 121 | mValue.f16v[3] = f4; |
michael@0 | 122 | return true; |
michael@0 | 123 | } |
michael@0 | 124 | return false; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | bool UpdateUniform(int cnt, const float *fp) { |
michael@0 | 128 | if (mLocation == -1) return false; |
michael@0 | 129 | switch (cnt) { |
michael@0 | 130 | case 1: |
michael@0 | 131 | case 2: |
michael@0 | 132 | case 3: |
michael@0 | 133 | case 4: |
michael@0 | 134 | case 16: |
michael@0 | 135 | if (memcmp(mValue.f16v, fp, sizeof(float) * cnt) != 0) { |
michael@0 | 136 | memcpy(mValue.f16v, fp, sizeof(float) * cnt); |
michael@0 | 137 | return true; |
michael@0 | 138 | } |
michael@0 | 139 | return false; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | NS_NOTREACHED("cnt must be 1 2 3 4 or 16"); |
michael@0 | 143 | return false; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | KnownUniformName mName; |
michael@0 | 147 | const char *mNameString; |
michael@0 | 148 | int32_t mLocation; |
michael@0 | 149 | |
michael@0 | 150 | union { |
michael@0 | 151 | int i1; |
michael@0 | 152 | float f1; |
michael@0 | 153 | float f16v[16]; |
michael@0 | 154 | } mValue; |
michael@0 | 155 | }; |
michael@0 | 156 | |
michael@0 | 157 | class ShaderConfigOGL |
michael@0 | 158 | { |
michael@0 | 159 | public: |
michael@0 | 160 | ShaderConfigOGL() : |
michael@0 | 161 | mFeatures(0) {} |
michael@0 | 162 | |
michael@0 | 163 | void SetRenderColor(bool aEnabled); |
michael@0 | 164 | void SetTextureTarget(GLenum aTarget); |
michael@0 | 165 | void SetRBSwap(bool aEnabled); |
michael@0 | 166 | void SetNoAlpha(bool aEnabled); |
michael@0 | 167 | void SetOpacity(bool aEnabled); |
michael@0 | 168 | void SetYCbCr(bool aEnabled); |
michael@0 | 169 | void SetComponentAlpha(bool aEnabled); |
michael@0 | 170 | void SetColorMatrix(bool aEnabled); |
michael@0 | 171 | void SetBlur(bool aEnabled); |
michael@0 | 172 | void SetMask2D(bool aEnabled); |
michael@0 | 173 | void SetMask3D(bool aEnabled); |
michael@0 | 174 | |
michael@0 | 175 | bool operator< (const ShaderConfigOGL& other) const { |
michael@0 | 176 | return mFeatures < other.mFeatures; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | public: |
michael@0 | 180 | void SetFeature(int aBitmask, bool aState) { |
michael@0 | 181 | if (aState) |
michael@0 | 182 | mFeatures |= aBitmask; |
michael@0 | 183 | else |
michael@0 | 184 | mFeatures &= (~aBitmask); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | int mFeatures; |
michael@0 | 188 | }; |
michael@0 | 189 | |
michael@0 | 190 | static inline ShaderConfigOGL |
michael@0 | 191 | ShaderConfigFromTargetAndFormat(GLenum aTarget, |
michael@0 | 192 | gfx::SurfaceFormat aFormat) |
michael@0 | 193 | { |
michael@0 | 194 | ShaderConfigOGL config; |
michael@0 | 195 | config.SetTextureTarget(aTarget); |
michael@0 | 196 | config.SetRBSwap(aFormat == gfx::SurfaceFormat::B8G8R8A8 || |
michael@0 | 197 | aFormat == gfx::SurfaceFormat::B8G8R8X8); |
michael@0 | 198 | config.SetNoAlpha(aFormat == gfx::SurfaceFormat::B8G8R8X8 || |
michael@0 | 199 | aFormat == gfx::SurfaceFormat::R8G8B8X8 || |
michael@0 | 200 | aFormat == gfx::SurfaceFormat::R5G6B5); |
michael@0 | 201 | return config; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | /** |
michael@0 | 205 | * This struct represents the shaders that make up a program and the uniform |
michael@0 | 206 | * and attribute parmeters that those shaders take. |
michael@0 | 207 | * It is used by ShaderProgramOGL. |
michael@0 | 208 | * Use the factory method GetProfileFor to create instances. |
michael@0 | 209 | */ |
michael@0 | 210 | struct ProgramProfileOGL |
michael@0 | 211 | { |
michael@0 | 212 | /** |
michael@0 | 213 | * Factory method; creates an instance of this class for the given |
michael@0 | 214 | * ShaderConfigOGL |
michael@0 | 215 | */ |
michael@0 | 216 | static ProgramProfileOGL GetProfileFor(ShaderConfigOGL aConfig); |
michael@0 | 217 | |
michael@0 | 218 | /** |
michael@0 | 219 | * These two methods lookup the location of a uniform and attribute, |
michael@0 | 220 | * respectively. Returns -1 if the named uniform/attribute does not |
michael@0 | 221 | * have a location for the shaders represented by this profile. |
michael@0 | 222 | */ |
michael@0 | 223 | GLint LookupAttributeLocation(const char* aName) |
michael@0 | 224 | { |
michael@0 | 225 | for (uint32_t i = 0; i < mAttributes.Length(); ++i) { |
michael@0 | 226 | if (strcmp(mAttributes[i].mName, aName) == 0) { |
michael@0 | 227 | return mAttributes[i].mLocation; |
michael@0 | 228 | } |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | return -1; |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | // represents the name and location of a uniform or attribute |
michael@0 | 235 | struct Argument |
michael@0 | 236 | { |
michael@0 | 237 | Argument(const char* aName) : |
michael@0 | 238 | mName(aName) {} |
michael@0 | 239 | const char* mName; |
michael@0 | 240 | GLint mLocation; |
michael@0 | 241 | }; |
michael@0 | 242 | |
michael@0 | 243 | // the source code for the program's shaders |
michael@0 | 244 | std::string mVertexShaderString; |
michael@0 | 245 | std::string mFragmentShaderString; |
michael@0 | 246 | |
michael@0 | 247 | KnownUniform mUniforms[KnownUniform::KnownUniformCount]; |
michael@0 | 248 | nsTArray<Argument> mAttributes; |
michael@0 | 249 | nsTArray<const char *> mDefines; |
michael@0 | 250 | uint32_t mTextureCount; |
michael@0 | 251 | |
michael@0 | 252 | ProgramProfileOGL() : |
michael@0 | 253 | mTextureCount(0) |
michael@0 | 254 | {} |
michael@0 | 255 | }; |
michael@0 | 256 | |
michael@0 | 257 | |
michael@0 | 258 | #if defined(DEBUG) |
michael@0 | 259 | #define CHECK_CURRENT_PROGRAM 1 |
michael@0 | 260 | #define ASSERT_THIS_PROGRAM \ |
michael@0 | 261 | do { \ |
michael@0 | 262 | GLuint currentProgram; \ |
michael@0 | 263 | mGL->GetUIntegerv(LOCAL_GL_CURRENT_PROGRAM, ¤tProgram); \ |
michael@0 | 264 | NS_ASSERTION(currentProgram == mProgram, \ |
michael@0 | 265 | "SetUniform with wrong program active!"); \ |
michael@0 | 266 | } while (0) |
michael@0 | 267 | #else |
michael@0 | 268 | #define ASSERT_THIS_PROGRAM \ |
michael@0 | 269 | do { } while (0) |
michael@0 | 270 | #endif |
michael@0 | 271 | |
michael@0 | 272 | /** |
michael@0 | 273 | * Represents an OGL shader program. The details of a program are represented |
michael@0 | 274 | * by a ProgramProfileOGL |
michael@0 | 275 | */ |
michael@0 | 276 | class ShaderProgramOGL |
michael@0 | 277 | { |
michael@0 | 278 | public: |
michael@0 | 279 | typedef mozilla::gl::GLContext GLContext; |
michael@0 | 280 | |
michael@0 | 281 | ShaderProgramOGL(GLContext* aGL, const ProgramProfileOGL& aProfile); |
michael@0 | 282 | |
michael@0 | 283 | ~ShaderProgramOGL(); |
michael@0 | 284 | |
michael@0 | 285 | bool HasInitialized() { |
michael@0 | 286 | NS_ASSERTION(mProgramState != STATE_OK || mProgram > 0, "Inconsistent program state"); |
michael@0 | 287 | return mProgramState == STATE_OK; |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | void Activate(); |
michael@0 | 291 | |
michael@0 | 292 | bool Initialize(); |
michael@0 | 293 | |
michael@0 | 294 | GLint CreateShader(GLenum aShaderType, const char *aShaderSource); |
michael@0 | 295 | |
michael@0 | 296 | /** |
michael@0 | 297 | * Creates a program and stores its id. |
michael@0 | 298 | */ |
michael@0 | 299 | bool CreateProgram(const char *aVertexShaderString, |
michael@0 | 300 | const char *aFragmentShaderString); |
michael@0 | 301 | |
michael@0 | 302 | /** |
michael@0 | 303 | * Lookup the location of an attribute |
michael@0 | 304 | */ |
michael@0 | 305 | GLint AttribLocation(const char* aName) { |
michael@0 | 306 | return mProfile.LookupAttributeLocation(aName); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | /** |
michael@0 | 310 | * The following set of methods set a uniform argument to the shader program. |
michael@0 | 311 | * Not all uniforms may be set for all programs, and such uses will throw |
michael@0 | 312 | * an assertion. |
michael@0 | 313 | */ |
michael@0 | 314 | void SetLayerTransform(const gfx::Matrix4x4& aMatrix) { |
michael@0 | 315 | SetMatrixUniform(KnownUniform::LayerTransform, aMatrix); |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | void SetMaskLayerTransform(const gfx::Matrix4x4& aMatrix) { |
michael@0 | 319 | SetMatrixUniform(KnownUniform::MaskQuadTransform, aMatrix); |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | void SetLayerQuadRect(const nsIntRect& aRect) { |
michael@0 | 323 | gfx3DMatrix m; |
michael@0 | 324 | m._11 = float(aRect.width); |
michael@0 | 325 | m._22 = float(aRect.height); |
michael@0 | 326 | m._41 = float(aRect.x); |
michael@0 | 327 | m._42 = float(aRect.y); |
michael@0 | 328 | SetMatrixUniform(KnownUniform::LayerQuadTransform, m); |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | void SetLayerQuadRect(const gfx::Rect& aRect) { |
michael@0 | 332 | gfx3DMatrix m; |
michael@0 | 333 | m._11 = aRect.width; |
michael@0 | 334 | m._22 = aRect.height; |
michael@0 | 335 | m._41 = aRect.x; |
michael@0 | 336 | m._42 = aRect.y; |
michael@0 | 337 | SetMatrixUniform(KnownUniform::LayerQuadTransform, m); |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | void SetProjectionMatrix(const gfx::Matrix4x4& aMatrix) { |
michael@0 | 341 | SetMatrixUniform(KnownUniform::MatrixProj, aMatrix); |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | // sets this program's texture transform, if it uses one |
michael@0 | 345 | void SetTextureTransform(const gfx::Matrix4x4& aMatrix) { |
michael@0 | 346 | SetMatrixUniform(KnownUniform::TextureTransform, aMatrix); |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | void SetRenderOffset(const nsIntPoint& aOffset) { |
michael@0 | 350 | float vals[4] = { float(aOffset.x), float(aOffset.y), 0.0f, 0.0f }; |
michael@0 | 351 | SetUniform(KnownUniform::RenderTargetOffset, 4, vals); |
michael@0 | 352 | } |
michael@0 | 353 | |
michael@0 | 354 | void SetRenderOffset(float aX, float aY) { |
michael@0 | 355 | float vals[4] = { aX, aY, 0.0f, 0.0f }; |
michael@0 | 356 | SetUniform(KnownUniform::RenderTargetOffset, 4, vals); |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | void SetLayerOpacity(float aOpacity) { |
michael@0 | 360 | SetUniform(KnownUniform::LayerOpacity, aOpacity); |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | void SetTextureUnit(GLint aUnit) { |
michael@0 | 364 | SetUniform(KnownUniform::Texture, aUnit); |
michael@0 | 365 | } |
michael@0 | 366 | void SetYTextureUnit(GLint aUnit) { |
michael@0 | 367 | SetUniform(KnownUniform::YTexture, aUnit); |
michael@0 | 368 | } |
michael@0 | 369 | |
michael@0 | 370 | void SetCbTextureUnit(GLint aUnit) { |
michael@0 | 371 | SetUniform(KnownUniform::CbTexture, aUnit); |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | void SetCrTextureUnit(GLint aUnit) { |
michael@0 | 375 | SetUniform(KnownUniform::CrTexture, aUnit); |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | void SetYCbCrTextureUnits(GLint aYUnit, GLint aCbUnit, GLint aCrUnit) { |
michael@0 | 379 | SetUniform(KnownUniform::YTexture, aYUnit); |
michael@0 | 380 | SetUniform(KnownUniform::CbTexture, aCbUnit); |
michael@0 | 381 | SetUniform(KnownUniform::CrTexture, aCrUnit); |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | void SetBlackTextureUnit(GLint aUnit) { |
michael@0 | 385 | SetUniform(KnownUniform::BlackTexture, aUnit); |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | void SetWhiteTextureUnit(GLint aUnit) { |
michael@0 | 389 | SetUniform(KnownUniform::WhiteTexture, aUnit); |
michael@0 | 390 | } |
michael@0 | 391 | |
michael@0 | 392 | void SetMaskTextureUnit(GLint aUnit) { |
michael@0 | 393 | SetUniform(KnownUniform::MaskTexture, aUnit); |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | void SetRenderColor(const gfxRGBA& aColor) { |
michael@0 | 397 | SetUniform(KnownUniform::RenderColor, aColor); |
michael@0 | 398 | } |
michael@0 | 399 | |
michael@0 | 400 | void SetRenderColor(const gfx::Color& aColor) { |
michael@0 | 401 | SetUniform(KnownUniform::RenderColor, aColor); |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | void SetTexCoordMultiplier(float aWidth, float aHeight) { |
michael@0 | 405 | float f[] = {aWidth, aHeight}; |
michael@0 | 406 | SetUniform(KnownUniform::TexCoordMultiplier, 2, f); |
michael@0 | 407 | } |
michael@0 | 408 | |
michael@0 | 409 | // Set whether we want the component alpha shader to return the color |
michael@0 | 410 | // vector (pass 1, false) or the alpha vector (pass2, true). With support |
michael@0 | 411 | // for multiple render targets we wouldn't need two passes here. |
michael@0 | 412 | void SetTexturePass2(bool aFlag) { |
michael@0 | 413 | SetUniform(KnownUniform::TexturePass2, aFlag ? 1 : 0); |
michael@0 | 414 | } |
michael@0 | 415 | |
michael@0 | 416 | // the names of attributes |
michael@0 | 417 | static const char* const VertexCoordAttrib; |
michael@0 | 418 | static const char* const TexCoordAttrib; |
michael@0 | 419 | |
michael@0 | 420 | protected: |
michael@0 | 421 | RefPtr<GLContext> mGL; |
michael@0 | 422 | // the OpenGL id of the program |
michael@0 | 423 | GLuint mProgram; |
michael@0 | 424 | ProgramProfileOGL mProfile; |
michael@0 | 425 | enum { |
michael@0 | 426 | STATE_NEW, |
michael@0 | 427 | STATE_OK, |
michael@0 | 428 | STATE_ERROR |
michael@0 | 429 | } mProgramState; |
michael@0 | 430 | |
michael@0 | 431 | #ifdef CHECK_CURRENT_PROGRAM |
michael@0 | 432 | static int sCurrentProgramKey; |
michael@0 | 433 | #endif |
michael@0 | 434 | |
michael@0 | 435 | void SetUniform(KnownUniform::KnownUniformName aKnownUniform, float aFloatValue) |
michael@0 | 436 | { |
michael@0 | 437 | ASSERT_THIS_PROGRAM; |
michael@0 | 438 | NS_ASSERTION(aKnownUniform >= 0 && aKnownUniform < KnownUniform::KnownUniformCount, "Invalid known uniform"); |
michael@0 | 439 | |
michael@0 | 440 | KnownUniform& ku(mProfile.mUniforms[aKnownUniform]); |
michael@0 | 441 | if (ku.UpdateUniform(aFloatValue)) { |
michael@0 | 442 | mGL->fUniform1f(ku.mLocation, aFloatValue); |
michael@0 | 443 | } |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | void SetUniform(KnownUniform::KnownUniformName aKnownUniform, const gfxRGBA& aColor) |
michael@0 | 447 | { |
michael@0 | 448 | ASSERT_THIS_PROGRAM; |
michael@0 | 449 | NS_ASSERTION(aKnownUniform >= 0 && aKnownUniform < KnownUniform::KnownUniformCount, "Invalid known uniform"); |
michael@0 | 450 | |
michael@0 | 451 | KnownUniform& ku(mProfile.mUniforms[aKnownUniform]); |
michael@0 | 452 | if (ku.UpdateUniform(aColor.r, aColor.g, aColor.b, aColor.a)) { |
michael@0 | 453 | mGL->fUniform4fv(ku.mLocation, 1, ku.mValue.f16v); |
michael@0 | 454 | } |
michael@0 | 455 | } |
michael@0 | 456 | |
michael@0 | 457 | void SetUniform(KnownUniform::KnownUniformName aKnownUniform, const gfx::Color& aColor) { |
michael@0 | 458 | ASSERT_THIS_PROGRAM; |
michael@0 | 459 | NS_ASSERTION(aKnownUniform >= 0 && aKnownUniform < KnownUniform::KnownUniformCount, "Invalid known uniform"); |
michael@0 | 460 | |
michael@0 | 461 | KnownUniform& ku(mProfile.mUniforms[aKnownUniform]); |
michael@0 | 462 | if (ku.UpdateUniform(aColor.r, aColor.g, aColor.b, aColor.a)) { |
michael@0 | 463 | mGL->fUniform4fv(ku.mLocation, 1, ku.mValue.f16v); |
michael@0 | 464 | } |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | void SetUniform(KnownUniform::KnownUniformName aKnownUniform, int aLength, float *aFloatValues) |
michael@0 | 468 | { |
michael@0 | 469 | ASSERT_THIS_PROGRAM; |
michael@0 | 470 | NS_ASSERTION(aKnownUniform >= 0 && aKnownUniform < KnownUniform::KnownUniformCount, "Invalid known uniform"); |
michael@0 | 471 | |
michael@0 | 472 | KnownUniform& ku(mProfile.mUniforms[aKnownUniform]); |
michael@0 | 473 | if (ku.UpdateUniform(aLength, aFloatValues)) { |
michael@0 | 474 | switch (aLength) { |
michael@0 | 475 | case 1: mGL->fUniform1fv(ku.mLocation, 1, ku.mValue.f16v); break; |
michael@0 | 476 | case 2: mGL->fUniform2fv(ku.mLocation, 1, ku.mValue.f16v); break; |
michael@0 | 477 | case 3: mGL->fUniform3fv(ku.mLocation, 1, ku.mValue.f16v); break; |
michael@0 | 478 | case 4: mGL->fUniform4fv(ku.mLocation, 1, ku.mValue.f16v); break; |
michael@0 | 479 | default: |
michael@0 | 480 | NS_NOTREACHED("Bogus aLength param"); |
michael@0 | 481 | } |
michael@0 | 482 | } |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | void SetUniform(KnownUniform::KnownUniformName aKnownUniform, GLint aIntValue) { |
michael@0 | 486 | ASSERT_THIS_PROGRAM; |
michael@0 | 487 | NS_ASSERTION(aKnownUniform >= 0 && aKnownUniform < KnownUniform::KnownUniformCount, "Invalid known uniform"); |
michael@0 | 488 | |
michael@0 | 489 | KnownUniform& ku(mProfile.mUniforms[aKnownUniform]); |
michael@0 | 490 | if (ku.UpdateUniform(aIntValue)) { |
michael@0 | 491 | mGL->fUniform1i(ku.mLocation, aIntValue); |
michael@0 | 492 | } |
michael@0 | 493 | } |
michael@0 | 494 | |
michael@0 | 495 | void SetMatrixUniform(KnownUniform::KnownUniformName aKnownUniform, const float *aFloatValues) { |
michael@0 | 496 | ASSERT_THIS_PROGRAM; |
michael@0 | 497 | NS_ASSERTION(aKnownUniform >= 0 && aKnownUniform < KnownUniform::KnownUniformCount, "Invalid known uniform"); |
michael@0 | 498 | |
michael@0 | 499 | KnownUniform& ku(mProfile.mUniforms[aKnownUniform]); |
michael@0 | 500 | if (ku.UpdateUniform(16, aFloatValues)) { |
michael@0 | 501 | mGL->fUniformMatrix4fv(ku.mLocation, 1, false, ku.mValue.f16v); |
michael@0 | 502 | } |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | void SetMatrixUniform(KnownUniform::KnownUniformName aKnownUniform, const gfx3DMatrix& aMatrix) { |
michael@0 | 506 | SetMatrixUniform(aKnownUniform, &aMatrix._11); |
michael@0 | 507 | } |
michael@0 | 508 | |
michael@0 | 509 | void SetMatrixUniform(KnownUniform::KnownUniformName aKnownUniform, const gfx::Matrix4x4& aMatrix) { |
michael@0 | 510 | SetMatrixUniform(aKnownUniform, &aMatrix._11); |
michael@0 | 511 | } |
michael@0 | 512 | }; |
michael@0 | 513 | |
michael@0 | 514 | |
michael@0 | 515 | } /* layers */ |
michael@0 | 516 | } /* mozilla */ |
michael@0 | 517 | |
michael@0 | 518 | #endif /* GFX_OGLSHADERPROGRAM_H */ |