blob: 404f275b30b08858adad0a8d8e84b112073be05b [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
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000011#include "GrAllocator.h"
bsalomon@google.coma469c282012-10-24 18:28:34 +000012#include "GrEffect.h"
tomhudson@google.com6a820b62012-05-24 15:10:14 +000013#include "GrGLProgram.h"
tomhudson@google.com9c639a42012-05-14 19:58:06 +000014#include "GrGLShaderBuilder.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000015#include "GrGLShaderVar.h"
16#include "GrGLSL.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000017
bsalomon@google.com44706382012-04-19 13:27:04 +000018struct GrGLInterface;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000019class GrGLTexture;
tomhudson@google.com168e6342012-04-18 17:49:20 +000020
21/** @file
bsalomon@google.com374e7592012-10-23 17:30:45 +000022 This file contains specializations for OpenGL of the shader stages declared in
bsalomon@google.comd698f772012-10-25 13:22:00 +000023 include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitting the
bsalomon@google.coma469c282012-10-24 18:28:34 +000024 GLSL code that implements a GrEffect and for uploading uniforms at draw time. They also
bsalomon@google.com374e7592012-10-23 17:30:45 +000025 must have a function:
bsalomon@google.coma469c282012-10-24 18:28:34 +000026 static inline StageKey GenKey(const GrEffect&, const GrGLCaps&)
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000027 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 +000028 that their GrGLEffects would emit the same GLSL code.
tomhudson@google.com168e6342012-04-18 17:49:20 +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
bsalomon@google.comd698f772012-10-25 13:22:00 +000033class GrGLEffect {
tomhudson@google.com168e6342012-04-18 17:49:20 +000034
35public:
bsalomon@google.coma469c282012-10-24 18:28:34 +000036 typedef GrEffect::StageKey StageKey;
bsalomon@google.comb505a122012-05-31 18:40:36 +000037 enum {
38 // the number of bits in StageKey available to GenKey
bsalomon@google.com396e61f2012-10-25 19:00:29 +000039 kProgramStageKeyBits = GrBackendEffectFactory::kProgramStageKeyBits,
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.comf271cc72012-10-24 19:35:13 +000053 @param effect The effect that generated this program stage.
bsalomon@google.coma469c282012-10-24 18:28:34 +000054 @param key The key that was computed by StageKey() from the generating GrEffect.
bsalomon@google.com374e7592012-10-23 17:30:45 +000055 @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.comf271cc72012-10-24 19:35:13 +000064 the effect to communicate back similar known info about its output.
bsalomon@google.coma469c282012-10-24 18:28:34 +000065 @param samplers One entry for each GrTextureAccess of the GrEffect that generated the
bsalomon@google.comd698f772012-10-25 13:22:00 +000066 GrGLEffect. These can be passed to the builder to emit texture
bsalomon@google.com374e7592012-10-23 17:30:45 +000067 reads in the generated code.
bsalomon@google.comd4726202012-08-03 14:34:46 +000068 */
bsalomon@google.com374e7592012-10-23 17:30:45 +000069 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comf271cc72012-10-24 19:35:13 +000070 const GrEffect& effect,
bsalomon@google.com96182212012-10-24 17:34:46 +000071 StageKey key,
bsalomon@google.com374e7592012-10-23 17:30:45 +000072 const char* vertexCoords,
73 const char* outputColor,
74 const char* inputColor,
75 const TextureSamplerArray& samplers) = 0;
tomhudson@google.com168e6342012-04-18 17:49:20 +000076
bsalomon@google.comd698f772012-10-25 13:22:00 +000077 /** A GrGLEffect instance can be reused with any GrEffect that produces the same stage
bsalomon@google.coma469c282012-10-24 18:28:34 +000078 key; this function reads data from a stage and uploads any uniform variables required
bsalomon@google.com374e7592012-10-23 17:30:45 +000079 by the shaders created in emitCode(). */
bsalomon@google.com021fc732012-10-25 12:47:42 +000080 virtual void setData(const GrGLUniformManager&, const GrEffect&);
tomhudson@google.com168e6342012-04-18 17:49:20 +000081
bsalomon@google.com289efe02012-05-21 20:57:59 +000082 const char* name() const { return fFactory.name(); }
83
bsalomon@google.coma469c282012-10-24 18:28:34 +000084 static StageKey GenTextureKey(const GrEffect&, const GrGLCaps&);
twiz@google.coma5e65ec2012-08-02 15:15:16 +000085
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000086protected:
87
bsalomon@google.com396e61f2012-10-25 19:00:29 +000088 const GrBackendEffectFactory& fFactory;
tomhudson@google.com168e6342012-04-18 17:49:20 +000089};
90
bsalomon@google.com374e7592012-10-23 17:30:45 +000091/**
bsalomon@google.comd698f772012-10-25 13:22:00 +000092 * This allows program stages that implemented an older set of virtual functions on GrGLEffect
bsalomon@google.com374e7592012-10-23 17:30:45 +000093 * to continue to work by change their parent class to this class. New program stages should not use
94 * this interface. It will be removed once older stages are modified to implement emitCode().
95 */
bsalomon@google.comaa600932012-10-25 13:29:20 +000096class GrGLLegacyEffect : public GrGLEffect {
bsalomon@google.com374e7592012-10-23 17:30:45 +000097public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000098 GrGLLegacyEffect(const GrBackendEffectFactory& factory) : GrGLEffect(factory) {}
bsalomon@google.com374e7592012-10-23 17:30:45 +000099
100 virtual void setupVariables(GrGLShaderBuilder* builder) {};
101 virtual void emitVS(GrGLShaderBuilder* builder,
102 const char* vertexCoords) = 0;
103 virtual void emitFS(GrGLShaderBuilder* builder,
104 const char* outputColor,
105 const char* inputColor,
106 const TextureSamplerArray&) = 0;
107
108 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.coma469c282012-10-24 18:28:34 +0000109 const GrEffect&,
bsalomon@google.com96182212012-10-24 17:34:46 +0000110 StageKey,
bsalomon@google.com374e7592012-10-23 17:30:45 +0000111 const char* vertexCoords,
112 const char* outputColor,
113 const char* inputColor,
114 const TextureSamplerArray& samplers) {
115 this->setupVariables(builder);
116 this->emitVS(builder, vertexCoords);
117 this->emitFS(builder, outputColor, inputColor, samplers);
118 }
119};
120
tomhudson@google.com168e6342012-04-18 17:49:20 +0000121#endif