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" |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 12 | #include "GrGLProgramEffects.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 | |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 16 | class GrGLShaderBuilder; |
| 17 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 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: |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame^] | 23 | static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*) |
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: |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame^] | 28 | EffectSubclass::EffectSubclass(const GrBackendEffectFactory&, const GrEffect&) |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 29 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 30 | 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] | 31 | */ |
| 32 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 33 | class GrGLTexture; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 34 | class GrGLGeometryProcessor; |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 35 | |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 36 | class GrGLEffect { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 37 | |
| 38 | public: |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 39 | typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 40 | typedef GrGLProgramEffects::TextureSampler TextureSampler; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 41 | typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray; |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 42 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 43 | GrGLEffect(const GrBackendEffectFactory& factory) |
| 44 | : fFactory(factory) |
| 45 | , fIsVertexEffect(false) { |
| 46 | } |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 47 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 48 | virtual ~GrGLEffect() {} |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 49 | |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 50 | /** Called when the program stage should insert its code into the shaders. The code in each |
| 51 | shader will be in its own block ({}) and so locally scoped names will not collide across |
| 52 | stages. |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 53 | |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 54 | @param builder Interface used to emit code in the shaders. |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame^] | 55 | @param effect The effect that generated this program stage. |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 56 | @param key The key that was computed by GenKey() from the generating GrEffect. |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 57 | @param outputColor A predefined vec4 in the FS in which the stage should place its output |
| 58 | color (or coverage). |
| 59 | @param inputColor A vec4 that holds the input color to the stage in the FS. This may be |
| 60 | NULL in which case the implied input is solid white (all ones). |
| 61 | TODO: Better system for communicating optimization info (e.g. input |
| 62 | 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] | 63 | the effect to communicate back similar known info about its output. |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 64 | @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] | 65 | GrGLEffect. These can be passed to the builder to emit texture |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 66 | reads in the generated code. |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 67 | */ |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 68 | virtual void emitCode(GrGLProgramBuilder* builder, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame^] | 69 | const GrEffect& effect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 70 | const GrEffectKey& key, |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 71 | const char* outputColor, |
| 72 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 73 | const TransformedCoordsArray& coords, |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 74 | const TextureSamplerArray& samplers) = 0; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 75 | |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 76 | /** A GrGLEffect instance can be reused with any GrEffect that produces the same stage |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 77 | key; this function reads data from a GrEffect and uploads any uniform variables required |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame^] | 78 | by the shaders created in emitCode(). The GrEffect is |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 79 | guaranteed to be of the same type that created this GrGLEffect and to have an identical |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 80 | effect key as the one that created this GrGLEffect. Effects that use local coords have |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 81 | to consider whether the GrEffectStage's coord change matrix should be used. When explicit |
| 82 | local coordinates are used it can be ignored. */ |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame^] | 83 | virtual void setData(const GrGLProgramDataManager&, const GrEffect&) {} |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 84 | |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 85 | const char* name() const { return fFactory.name(); } |
| 86 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame^] | 87 | static void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*) {} |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 88 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 89 | /** Used by the system when generating shader code, to see if this effect can be downcasted to |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 90 | the internal GrGLGeometryProcessor type */ |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 91 | bool isVertexEffect() const { return fIsVertexEffect; } |
| 92 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 93 | protected: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 94 | const GrBackendEffectFactory& fFactory; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 95 | |
| 96 | private: |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 97 | friend class GrGLGeometryProcessor; // to set fIsVertexEffect |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 98 | |
| 99 | bool fIsVertexEffect; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 102 | #endif |