blob: 24c20c6d57367468a7bf8e0af0be940ab97cc3cc [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.com168e6342012-04-18 17:49:20 +000013#include "GrGLShaderVar.h"
14#include "GrGLSL.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000015
bsalomon848faf02014-07-11 10:01:02 -070016class GrGLShaderBuilder;
17
tomhudson@google.com168e6342012-04-18 17:49:20 +000018/** @file
bsalomon@google.com374e7592012-10-23 17:30:45 +000019 This file contains specializations for OpenGL of the shader stages declared in
bsalomon@google.comd698f772012-10-25 13:22:00 +000020 include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitting the
bsalomon@google.com77af6802013-10-02 13:04:56 +000021 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:
joshualitt49586be2014-09-16 08:21:41 -070023 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*)
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000024 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 +000025 that their GrGLEffects would emit the same GLSL code.
tomhudson@google.com168e6342012-04-18 17:49:20 +000026
bsalomon@google.comc7818882013-03-20 19:19:53 +000027 The GrGLEffect subclass must also have a constructor of the form:
joshualitt49586be2014-09-16 08:21:41 -070028 EffectSubclass::EffectSubclass(const GrBackendEffectFactory&, const GrEffect&)
bsalomon@google.comc7818882013-03-20 19:19:53 +000029
bsalomon@google.coma469c282012-10-24 18:28:34 +000030 These objects are created by the factory object returned by the GrEffect::getFactory().
tomhudson@google.com168e6342012-04-18 17:49:20 +000031*/
32
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000033class GrGLTexture;
joshualitt249af152014-09-15 11:41:13 -070034class GrGLGeometryProcessor;
bsalomon@google.comc7818882013-03-20 19:19:53 +000035
bsalomon@google.comd698f772012-10-25 13:22:00 +000036class GrGLEffect {
tomhudson@google.com168e6342012-04-18 17:49:20 +000037
38public:
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000039 typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000040 typedef GrGLProgramEffects::TextureSampler TextureSampler;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000041 typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000042
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000043 GrGLEffect(const GrBackendEffectFactory& factory)
44 : fFactory(factory)
45 , fIsVertexEffect(false) {
46 }
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000047
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000048 virtual ~GrGLEffect() {}
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000049
bsalomon@google.com374e7592012-10-23 17:30:45 +000050 /** 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.com6a820b62012-05-24 15:10:14 +000053
bsalomon@google.com374e7592012-10-23 17:30:45 +000054 @param builder Interface used to emit code in the shaders.
joshualitt49586be2014-09-16 08:21:41 -070055 @param effect The effect that generated this program stage.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000056 @param key The key that was computed by GenKey() from the generating GrEffect.
bsalomon@google.com374e7592012-10-23 17:30:45 +000057 @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.comf271cc72012-10-24 19:35:13 +000063 the effect to communicate back similar known info about its output.
bsalomon@google.coma469c282012-10-24 18:28:34 +000064 @param samplers One entry for each GrTextureAccess of the GrEffect that generated the
bsalomon@google.comd698f772012-10-25 13:22:00 +000065 GrGLEffect. These can be passed to the builder to emit texture
bsalomon@google.com374e7592012-10-23 17:30:45 +000066 reads in the generated code.
bsalomon@google.comd4726202012-08-03 14:34:46 +000067 */
joshualitt30ba4362014-08-21 20:18:45 -070068 virtual void emitCode(GrGLProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -070069 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -070070 const GrEffectKey& key,
bsalomon@google.com374e7592012-10-23 17:30:45 +000071 const char* outputColor,
72 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000073 const TransformedCoordsArray& coords,
bsalomon@google.com374e7592012-10-23 17:30:45 +000074 const TextureSamplerArray& samplers) = 0;
tomhudson@google.com168e6342012-04-18 17:49:20 +000075
bsalomon@google.comd698f772012-10-25 13:22:00 +000076 /** A GrGLEffect instance can be reused with any GrEffect that produces the same stage
bsalomon63e99f72014-07-21 08:03:14 -070077 key; this function reads data from a GrEffect and uploads any uniform variables required
joshualitt49586be2014-09-16 08:21:41 -070078 by the shaders created in emitCode(). The GrEffect is
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000079 guaranteed to be of the same type that created this GrGLEffect and to have an identical
bsalomon63e99f72014-07-21 08:03:14 -070080 effect key as the one that created this GrGLEffect. Effects that use local coords have
bsalomon@google.comc7818882013-03-20 19:19:53 +000081 to consider whether the GrEffectStage's coord change matrix should be used. When explicit
82 local coordinates are used it can be ignored. */
joshualitt49586be2014-09-16 08:21:41 -070083 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) {}
tomhudson@google.com168e6342012-04-18 17:49:20 +000084
bsalomon@google.com289efe02012-05-21 20:57:59 +000085 const char* name() const { return fFactory.name(); }
86
joshualitt49586be2014-09-16 08:21:41 -070087 static void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*) {}
bsalomon@google.com77af6802013-10-02 13:04:56 +000088
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000089 /** Used by the system when generating shader code, to see if this effect can be downcasted to
joshualitt249af152014-09-15 11:41:13 -070090 the internal GrGLGeometryProcessor type */
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000091 bool isVertexEffect() const { return fIsVertexEffect; }
92
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000093protected:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000094 const GrBackendEffectFactory& fFactory;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000095
96private:
joshualitt249af152014-09-15 11:41:13 -070097 friend class GrGLGeometryProcessor; // to set fIsVertexEffect
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000098
99 bool fIsVertexEffect;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000100};
101
tomhudson@google.com168e6342012-04-18 17:49:20 +0000102#endif