blob: 0fd5722cb8a4071f1536ced2c728c5315b70f3c6 [file] [log] [blame]
tomhudson@google.com168e6342012-04-18 17:49:20 +00001/*
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.com422e81a2012-10-25 14:11:03 +00008#ifndef GrGLEffect_DEFINED
9#define GrGLEffect_DEFINED
tomhudson@google.com168e6342012-04-18 17:49:20 +000010
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000011#include "GrBackendEffectFactory.h"
tomhudson@google.com6a820b62012-05-24 15:10:14 +000012#include "GrGLProgram.h"
tomhudson@google.com9c639a42012-05-14 19:58:06 +000013#include "GrGLShaderBuilder.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000014#include "GrGLShaderVar.h"
15#include "GrGLSL.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000016
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000017class GrEffectStage;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000018class GrGLTexture;
tomhudson@google.com168e6342012-04-18 17:49:20 +000019
20/** @file
bsalomon@google.com374e7592012-10-23 17:30:45 +000021 This file contains specializations for OpenGL of the shader stages declared in
bsalomon@google.comd698f772012-10-25 13:22:00 +000022 include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitting the
bsalomon@google.coma469c282012-10-24 18:28:34 +000023 GLSL code that implements a GrEffect and for uploading uniforms at draw time. They also
bsalomon@google.com374e7592012-10-23 17:30:45 +000024 must have a function:
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000025 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&)
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000026 that is used to implement a program cache. When two GrEffects produce the same key this means
bsalomon@google.com422e81a2012-10-25 14:11:03 +000027 that their GrGLEffects would emit the same GLSL code.
tomhudson@google.com168e6342012-04-18 17:49:20 +000028
bsalomon@google.coma469c282012-10-24 18:28:34 +000029 These objects are created by the factory object returned by the GrEffect::getFactory().
tomhudson@google.com168e6342012-04-18 17:49:20 +000030*/
31
bsalomon@google.comd698f772012-10-25 13:22:00 +000032class GrGLEffect {
tomhudson@google.com168e6342012-04-18 17:49:20 +000033
34public:
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000035 typedef GrBackendEffectFactory::EffectKey EffectKey;
36
bsalomon@google.comb505a122012-05-31 18:40:36 +000037 enum {
bsalomon@google.com46fba0d2012-10-25 21:42:05 +000038 // the number of bits in EffectKey available to GenKey
39 kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits,
bsalomon@google.comb505a122012-05-31 18:40:36 +000040 };
41
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000042 typedef GrGLShaderBuilder::TextureSamplerArray TextureSamplerArray;
43
bsalomon@google.com396e61f2012-10-25 19:00:29 +000044 GrGLEffect(const GrBackendEffectFactory&);
bsalomon@google.com289efe02012-05-21 20:57:59 +000045
bsalomon@google.comd698f772012-10-25 13:22:00 +000046 virtual ~GrGLEffect();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000047
bsalomon@google.com374e7592012-10-23 17:30:45 +000048 /** 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.com6a820b62012-05-24 15:10:14 +000051
bsalomon@google.com374e7592012-10-23 17:30:45 +000052 @param builder Interface used to emit code in the shaders.
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000053 @param stage The effect stage that generated this program stage.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000054 @param key The key that was computed by GenKey() from the generating GrEffect.
55 Only the bits indicated by GrBackendEffectFactory::kEffectKeyBits are
56 guaranteed to match the value produced by GenKey();
bsalomon@google.com374e7592012-10-23 17:30:45 +000057 @param vertexCoords A vec2 of texture coordinates in the VS, which may be altered. This will
58 be removed soon and stages will be responsible for computing their own
59 coords.
60 @param outputColor A predefined vec4 in the FS in which the stage should place its output
61 color (or coverage).
62 @param inputColor A vec4 that holds the input color to the stage in the FS. This may be
63 NULL in which case the implied input is solid white (all ones).
64 TODO: Better system for communicating optimization info (e.g. input
65 color is solid white, trans black, known to be opaque, etc.) that allows
bsalomon@google.comf271cc72012-10-24 19:35:13 +000066 the effect to communicate back similar known info about its output.
bsalomon@google.coma469c282012-10-24 18:28:34 +000067 @param samplers One entry for each GrTextureAccess of the GrEffect that generated the
bsalomon@google.comd698f772012-10-25 13:22:00 +000068 GrGLEffect. These can be passed to the builder to emit texture
bsalomon@google.com374e7592012-10-23 17:30:45 +000069 reads in the generated code.
bsalomon@google.comd4726202012-08-03 14:34:46 +000070 */
bsalomon@google.com374e7592012-10-23 17:30:45 +000071 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000072 const GrEffectStage& stage,
bsalomon@google.com46fba0d2012-10-25 21:42:05 +000073 EffectKey key,
bsalomon@google.com374e7592012-10-23 17:30:45 +000074 const char* vertexCoords,
75 const char* outputColor,
76 const char* inputColor,
77 const TextureSamplerArray& samplers) = 0;
tomhudson@google.com168e6342012-04-18 17:49:20 +000078
bsalomon@google.comd698f772012-10-25 13:22:00 +000079 /** A GrGLEffect instance can be reused with any GrEffect that produces the same stage
bsalomon@google.coma469c282012-10-24 18:28:34 +000080 key; this function reads data from a stage and uploads any uniform variables required
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000081 by the shaders created in emitCode(). The GrEffect installed in the GrEffectStage is
82 guaranteed to be of the same type that created this GrGLEffect and to have an identical
83 EffectKey as the one that created this GrGLEffect. */
84 virtual void setData(const GrGLUniformManager&, const GrEffectStage&);
tomhudson@google.com168e6342012-04-18 17:49:20 +000085
bsalomon@google.com289efe02012-05-21 20:57:59 +000086 const char* name() const { return fFactory.name(); }
87
bsalomon@google.com46fba0d2012-10-25 21:42:05 +000088 static EffectKey GenTextureKey(const GrEffect&, const GrGLCaps&);
twiz@google.coma5e65ec2012-08-02 15:15:16 +000089
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000090 bool requiresTextureMatrix() const { return fRequiresTextureMatrix; }
91
92
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000093protected:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000094 // HACK: This is a temporary field that allows GrGLEffect subclasses to opt into the new
95 // shader gen where a texture matrix is not automatically inserted. It defaults to true and is
96 // set to false in a subclass to opt into the new behavior.
97 bool fRequiresTextureMatrix;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000098
bsalomon@google.com396e61f2012-10-25 19:00:29 +000099 const GrBackendEffectFactory& fFactory;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000100};
101
tomhudson@google.com168e6342012-04-18 17:49:20 +0000102#endif