blob: 1cc3df24b09f8294c4757e1c55f74786ac86598c [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"
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000012#include "GrGLProgramEffects.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
tomhudson@google.com168e6342012-04-18 17:49:20 +000017/** @file
bsalomon@google.com374e7592012-10-23 17:30:45 +000018 This file contains specializations for OpenGL of the shader stages declared in
bsalomon@google.comd698f772012-10-25 13:22:00 +000019 include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitting the
bsalomon@google.com77af6802013-10-02 13:04:56 +000020 GLSL code that implements a GrEffect and for uploading uniforms at draw time. If they don't
21 always emit the same GLSL code, they must have a function:
bsalomon@google.comc7818882013-03-20 19:19:53 +000022 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&)
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000023 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 +000024 that their GrGLEffects would emit the same GLSL code.
tomhudson@google.com168e6342012-04-18 17:49:20 +000025
bsalomon@google.comc7818882013-03-20 19:19:53 +000026 The GrGLEffect subclass must also have a constructor of the form:
27 EffectSubclass::EffectSubclass(const GrBackendEffectFactory&, const GrDrawEffect&)
28 The effect held by the GrDrawEffect is guaranteed to be of the type that generated the
29 GrGLEffect subclass instance.
30
bsalomon@google.coma469c282012-10-24 18:28:34 +000031 These objects are created by the factory object returned by the GrEffect::getFactory().
tomhudson@google.com168e6342012-04-18 17:49:20 +000032*/
33
bsalomon@google.comc7818882013-03-20 19:19:53 +000034class GrDrawEffect;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000035class GrGLTexture;
36class GrGLVertexEffect;
bsalomon@google.comc7818882013-03-20 19:19:53 +000037
bsalomon@google.comd698f772012-10-25 13:22:00 +000038class GrGLEffect {
tomhudson@google.com168e6342012-04-18 17:49:20 +000039
40public:
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000041 typedef GrBackendEffectFactory::EffectKey EffectKey;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000042 typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000043 typedef GrGLProgramEffects::TextureSampler TextureSampler;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000044 typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000045
bsalomon@google.comb505a122012-05-31 18:40:36 +000046 enum {
bsalomon@google.comdbe49f72012-11-05 16:36:02 +000047 kNoEffectKey = GrBackendEffectFactory::kNoEffectKey,
bsalomon@google.com46fba0d2012-10-25 21:42:05 +000048 // the number of bits in EffectKey available to GenKey
49 kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits,
bsalomon@google.comb505a122012-05-31 18:40:36 +000050 };
51
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000052 GrGLEffect(const GrBackendEffectFactory& factory)
53 : fFactory(factory)
54 , fIsVertexEffect(false) {
55 }
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000056
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000057 virtual ~GrGLEffect() {}
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000058
bsalomon@google.com374e7592012-10-23 17:30:45 +000059 /** Called when the program stage should insert its code into the shaders. The code in each
60 shader will be in its own block ({}) and so locally scoped names will not collide across
61 stages.
tomhudson@google.com6a820b62012-05-24 15:10:14 +000062
bsalomon@google.com374e7592012-10-23 17:30:45 +000063 @param builder Interface used to emit code in the shaders.
bsalomon@google.comc7818882013-03-20 19:19:53 +000064 @param drawEffect A wrapper on the effect that generated this program stage.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000065 @param key The key that was computed by GenKey() from the generating GrEffect.
66 Only the bits indicated by GrBackendEffectFactory::kEffectKeyBits are
67 guaranteed to match the value produced by GenKey();
bsalomon@google.com374e7592012-10-23 17:30:45 +000068 @param outputColor A predefined vec4 in the FS in which the stage should place its output
69 color (or coverage).
70 @param inputColor A vec4 that holds the input color to the stage in the FS. This may be
71 NULL in which case the implied input is solid white (all ones).
72 TODO: Better system for communicating optimization info (e.g. input
73 color is solid white, trans black, known to be opaque, etc.) that allows
bsalomon@google.comf271cc72012-10-24 19:35:13 +000074 the effect to communicate back similar known info about its output.
bsalomon@google.coma469c282012-10-24 18:28:34 +000075 @param samplers One entry for each GrTextureAccess of the GrEffect that generated the
bsalomon@google.comd698f772012-10-25 13:22:00 +000076 GrGLEffect. These can be passed to the builder to emit texture
bsalomon@google.com374e7592012-10-23 17:30:45 +000077 reads in the generated code.
bsalomon@google.comd4726202012-08-03 14:34:46 +000078 */
bsalomon@google.com374e7592012-10-23 17:30:45 +000079 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000080 const GrDrawEffect& drawEffect,
bsalomon@google.com46fba0d2012-10-25 21:42:05 +000081 EffectKey key,
bsalomon@google.com374e7592012-10-23 17:30:45 +000082 const char* outputColor,
83 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000084 const TransformedCoordsArray& coords,
bsalomon@google.com374e7592012-10-23 17:30:45 +000085 const TextureSamplerArray& samplers) = 0;
tomhudson@google.com168e6342012-04-18 17:49:20 +000086
bsalomon@google.comd698f772012-10-25 13:22:00 +000087 /** A GrGLEffect instance can be reused with any GrEffect that produces the same stage
bsalomon@google.coma469c282012-10-24 18:28:34 +000088 key; this function reads data from a stage and uploads any uniform variables required
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000089 by the shaders created in emitCode(). The GrEffect installed in the GrEffectStage is
90 guaranteed to be of the same type that created this GrGLEffect and to have an identical
bsalomon@google.comc7818882013-03-20 19:19:53 +000091 EffectKey as the one that created this GrGLEffect. Effects that use local coords have
92 to consider whether the GrEffectStage's coord change matrix should be used. When explicit
93 local coordinates are used it can be ignored. */
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000094 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) {}
tomhudson@google.com168e6342012-04-18 17:49:20 +000095
bsalomon@google.com289efe02012-05-21 20:57:59 +000096 const char* name() const { return fFactory.name(); }
97
bsalomon@google.com77af6802013-10-02 13:04:56 +000098 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { return 0; }
99
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000100 /** Used by the system when generating shader code, to see if this effect can be downcasted to
101 the internal GrGLVertexEffect type */
102 bool isVertexEffect() const { return fIsVertexEffect; }
103
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000104protected:
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000105 const GrBackendEffectFactory& fFactory;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000106
107private:
108 friend class GrGLVertexEffect; // to set fIsVertexEffect
109
110 bool fIsVertexEffect;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000111};
112
tomhudson@google.com168e6342012-04-18 17:49:20 +0000113#endif