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 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 11 | #include "GrAllocator.h" |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame^] | 12 | #include "GrEffectStage.h" |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 13 | #include "GrGLProgram.h" |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 14 | #include "GrGLShaderBuilder.h" |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 15 | #include "GrGLShaderVar.h" |
| 16 | #include "GrGLSL.h" |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 4470638 | 2012-04-19 13:27:04 +0000 | [diff] [blame] | 18 | struct GrGLInterface; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 19 | class GrGLTexture; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 20 | |
| 21 | /** @file |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 22 | 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] | 23 | include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitting the |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 24 | GLSL code that implements a GrEffect and for uploading uniforms at draw time. They also |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 25 | must have a function: |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 26 | static inline EffectKey GenKey(const GrEffect&, const GrGLCaps&) |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 27 | 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] | 28 | that their GrGLEffects would emit the same GLSL code. |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +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 | |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 33 | class GrGLEffect { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 34 | |
| 35 | public: |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 36 | typedef GrEffect::EffectKey EffectKey; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 37 | enum { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 38 | // the number of bits in EffectKey available to GenKey |
| 39 | kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 42 | typedef GrGLShaderBuilder::TextureSamplerArray TextureSamplerArray; |
| 43 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 44 | GrGLEffect(const GrBackendEffectFactory&); |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 45 | |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 46 | virtual ~GrGLEffect(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 47 | |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 48 | /** Called when the program stage should insert its code into the shaders. The code in each |
| 49 | shader will be in its own block ({}) and so locally scoped names will not collide across |
| 50 | stages. |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 51 | |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 52 | @param builder Interface used to emit code in the shaders. |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 53 | @param effect The effect that generated this program stage. |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 54 | @param key The key that was computed by EffectKey() from the generating GrEffect. |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 55 | @param vertexCoords A vec2 of texture coordinates in the VS, which may be altered. This will |
| 56 | be removed soon and stages will be responsible for computing their own |
| 57 | coords. |
| 58 | @param outputColor A predefined vec4 in the FS in which the stage should place its output |
| 59 | color (or coverage). |
| 60 | @param inputColor A vec4 that holds the input color to the stage in the FS. This may be |
| 61 | NULL in which case the implied input is solid white (all ones). |
| 62 | TODO: Better system for communicating optimization info (e.g. input |
| 63 | 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] | 64 | the effect to communicate back similar known info about its output. |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 65 | @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] | 66 | GrGLEffect. These can be passed to the builder to emit texture |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 67 | reads in the generated code. |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 68 | */ |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 69 | virtual void emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 70 | const GrEffect& effect, |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 71 | EffectKey key, |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 72 | const char* vertexCoords, |
| 73 | const char* outputColor, |
| 74 | const char* inputColor, |
| 75 | const TextureSamplerArray& samplers) = 0; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 76 | |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 77 | /** 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] | 78 | 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^] | 79 | by the shaders created in emitCode(). The GrEffect installed in the GrEffectStage is |
| 80 | guaranteed to be of the same type that created this GrGLEffect and to have an identical |
| 81 | EffectKey as the one that created this GrGLEffect. */ |
| 82 | virtual void setData(const GrGLUniformManager&, const GrEffectStage&); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 83 | |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 84 | const char* name() const { return fFactory.name(); } |
| 85 | |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 86 | static EffectKey GenTextureKey(const GrEffect&, const GrGLCaps&); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 87 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 88 | protected: |
| 89 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 90 | const GrBackendEffectFactory& fFactory; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 93 | /** |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 94 | * This allows program stages that implemented an older set of virtual functions on GrGLEffect |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 95 | * to continue to work by change their parent class to this class. New program stages should not use |
| 96 | * this interface. It will be removed once older stages are modified to implement emitCode(). |
| 97 | */ |
bsalomon@google.com | aa60093 | 2012-10-25 13:29:20 +0000 | [diff] [blame] | 98 | class GrGLLegacyEffect : public GrGLEffect { |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 99 | public: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 100 | GrGLLegacyEffect(const GrBackendEffectFactory& factory) : GrGLEffect(factory) {} |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 101 | |
| 102 | virtual void setupVariables(GrGLShaderBuilder* builder) {}; |
| 103 | virtual void emitVS(GrGLShaderBuilder* builder, |
| 104 | const char* vertexCoords) = 0; |
| 105 | virtual void emitFS(GrGLShaderBuilder* builder, |
| 106 | const char* outputColor, |
| 107 | const char* inputColor, |
| 108 | const TextureSamplerArray&) = 0; |
| 109 | |
| 110 | virtual void emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 111 | const GrEffect&, |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 112 | EffectKey, |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 113 | const char* vertexCoords, |
| 114 | const char* outputColor, |
| 115 | const char* inputColor, |
| 116 | const TextureSamplerArray& samplers) { |
| 117 | this->setupVariables(builder); |
| 118 | this->emitVS(builder, vertexCoords); |
| 119 | this->emitFS(builder, outputColor, inputColor, samplers); |
| 120 | } |
| 121 | }; |
| 122 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 123 | #endif |