tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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. |
| 6 | */ |
| 7 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 8 | #ifndef GrGLEffect_DEFINED |
| 9 | #define GrGLEffect_DEFINED |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 11 | #include "GrBackendEffectFactory.h" |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 12 | #include "GrGLShaderBuilder.h" |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 13 | #include "GrGLShaderVar.h" |
| 14 | #include "GrGLSL.h" |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 15 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 16 | class GrGLTexture; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 17 | |
| 18 | /** @file |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 19 | This file contains specializations for OpenGL of the shader stages declared in |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 20 | include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitting the |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 21 | GLSL code that implements a GrEffect and for uploading uniforms at draw time. If they don't |
| 22 | always emit the same GLSL code, they must have a function: |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 23 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 24 | that is used to implement a program cache. When two GrEffects produce the same key this means |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 25 | that their GrGLEffects would emit the same GLSL code. |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 27 | The GrGLEffect subclass must also have a constructor of the form: |
| 28 | EffectSubclass::EffectSubclass(const GrBackendEffectFactory&, const GrDrawEffect&) |
| 29 | The effect held by the GrDrawEffect is guaranteed to be of the type that generated the |
| 30 | GrGLEffect subclass instance. |
| 31 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 32 | These objects are created by the factory object returned by the GrEffect::getFactory(). |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 33 | */ |
| 34 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 35 | class GrDrawEffect; |
| 36 | |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 37 | class GrGLEffect { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 38 | |
| 39 | public: |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 40 | typedef GrBackendEffectFactory::EffectKey EffectKey; |
| 41 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 42 | enum { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 43 | kNoEffectKey = GrBackendEffectFactory::kNoEffectKey, |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 44 | // the number of bits in EffectKey available to GenKey |
| 45 | kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 48 | typedef GrGLShaderBuilder::TransformedCoordsArray TransformedCoordsArray; |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 49 | typedef GrGLShaderBuilder::TextureSamplerArray TextureSamplerArray; |
| 50 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 51 | GrGLEffect(const GrBackendEffectFactory&); |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 52 | |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 53 | virtual ~GrGLEffect(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 55 | /** Called when the program stage should insert its code into the shaders. The code in each |
| 56 | shader will be in its own block ({}) and so locally scoped names will not collide across |
| 57 | stages. |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 58 | |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 59 | @param builder Interface used to emit code in the shaders. |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 60 | @param drawEffect A wrapper on the effect that generated this program stage. |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 61 | @param key The key that was computed by GenKey() from the generating GrEffect. |
| 62 | Only the bits indicated by GrBackendEffectFactory::kEffectKeyBits are |
| 63 | guaranteed to match the value produced by GenKey(); |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 64 | @param outputColor A predefined vec4 in the FS in which the stage should place its output |
| 65 | color (or coverage). |
| 66 | @param inputColor A vec4 that holds the input color to the stage in the FS. This may be |
| 67 | NULL in which case the implied input is solid white (all ones). |
| 68 | TODO: Better system for communicating optimization info (e.g. input |
| 69 | color is solid white, trans black, known to be opaque, etc.) that allows |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 70 | the effect to communicate back similar known info about its output. |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 71 | @param samplers One entry for each GrTextureAccess of the GrEffect that generated the |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 72 | GrGLEffect. These can be passed to the builder to emit texture |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 73 | reads in the generated code. |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 74 | */ |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 75 | virtual void emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 76 | const GrDrawEffect& drawEffect, |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 77 | EffectKey key, |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 78 | const char* outputColor, |
| 79 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 80 | const TransformedCoordsArray& coords, |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 81 | const TextureSamplerArray& samplers) = 0; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 82 | |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 83 | /** A GrGLEffect instance can be reused with any GrEffect that produces the same stage |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 84 | key; this function reads data from a stage and uploads any uniform variables required |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 85 | by the shaders created in emitCode(). The GrEffect installed in the GrEffectStage is |
| 86 | guaranteed to be of the same type that created this GrGLEffect and to have an identical |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 87 | EffectKey as the one that created this GrGLEffect. Effects that use local coords have |
| 88 | to consider whether the GrEffectStage's coord change matrix should be used. When explicit |
| 89 | local coordinates are used it can be ignored. */ |
| 90 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 92 | const char* name() const { return fFactory.name(); } |
| 93 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 94 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { return 0; } |
| 95 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 96 | static EffectKey GenTextureKey(const GrDrawEffect&, const GrGLCaps&); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 97 | static EffectKey GenTransformKey(const GrDrawEffect&); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 98 | static EffectKey GenAttribKey(const GrDrawEffect& stage); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 99 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 100 | protected: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 101 | const GrBackendEffectFactory& fFactory; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 104 | #endif |