2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrGLProcessor_DEFINED
9 #define GrGLProcessor_DEFINED
11 #include "GrBackendProcessorFactory.h"
12 #include "GrGLProgramEffects.h"
13 #include "GrGLShaderVar.h"
17 This file contains specializations for OpenGL of the shader stages declared in
18 include/gpu/GrProcessor.h. Objects of type GrGLProcessor are responsible for emitting the
19 GLSL code that implements a GrProcessor and for uploading uniforms at draw time. If they don't
20 always emit the same GLSL code, they must have a function:
21 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*)
22 that is used to implement a program cache. When two GrProcessors produce the same key this means
23 that their GrGLProcessors would emit the same GLSL code.
25 The GrGLProcessor subclass must also have a constructor of the form:
26 EffectSubclass::EffectSubclass(const GrBackendEffectFactory&, const GrProcessor&)
28 These objects are created by the factory object returned by the GrProcessor::getFactory().
33 GrGLProcessor(const GrBackendProcessorFactory& factory)
37 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
40 * Passed to GrGLProcessors so they can add transformed coordinates to their shader code.
42 typedef GrShaderVar TransformedCoords;
43 typedef SkTArray<GrShaderVar> TransformedCoordsArray;
46 * Passed to GrGLProcessors so they can add texture reads to their shader code.
48 class TextureSampler {
50 TextureSampler(UniformHandle uniform, const GrTextureAccess& access)
51 : fSamplerUniform(uniform)
52 , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture()->config())) {
53 SkASSERT(0 != fConfigComponentMask);
54 memcpy(fSwizzle, access.getSwizzle(), 5);
57 // bitfield of GrColorComponentFlags present in the texture's config.
58 uint32_t configComponentMask() const { return fConfigComponentMask; }
60 const char* swizzle() const { return fSwizzle; }
63 UniformHandle fSamplerUniform;
64 uint32_t fConfigComponentMask;
67 friend class GrGLShaderBuilder;
70 typedef SkTArray<TextureSampler> TextureSamplerArray;
72 virtual ~GrGLProcessor() {}
74 /** A GrGLProcessor instance can be reused with any GrProcessor that produces the same stage
75 key; this function reads data from a GrProcessor and uploads any uniform variables required
76 by the shaders created in emitCode(). The GrProcessor installed in the GrDrawEffect is
77 guaranteed to be of the same type that created this GrGLProcessor and to have an identical
78 effect key as the one that created this GrGLProcessor. Effects that use local coords have
79 to consider whether the GrProcessorStage's coord change matrix should be used. When explicit
80 local coordinates are used it can be ignored. */
81 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) {}
83 const char* name() const { return fFactory.name(); }
85 static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*) {}
88 const GrBackendProcessorFactory& fFactory;
91 class GrGLFragmentProcessor : public GrGLProcessor {
93 GrGLFragmentProcessor(const GrBackendProcessorFactory& factory)
94 : INHERITED(factory) {
97 virtual ~GrGLFragmentProcessor() {}
99 /** Called when the program stage should insert its code into the shaders. The code in each
100 shader will be in its own block ({}) and so locally scoped names will not collide across
103 @param builder Interface used to emit code in the shaders.
104 @param effect The effect that generated this program stage.
105 @param key The key that was computed by GenKey() from the generating GrProcessor.
106 @param outputColor A predefined vec4 in the FS in which the stage should place its output
108 @param inputColor A vec4 that holds the input color to the stage in the FS. This may be
109 NULL in which case the implied input is solid white (all ones).
110 TODO: Better system for communicating optimization info (e.g. input
111 color is solid white, trans black, known to be opaque, etc.) that allows
112 the effect to communicate back similar known info about its output.
113 @param samplers Contains one entry for each GrTextureAccess of the GrProcessor. These
114 can be passed to the builder to emit texture reads in the generated
117 virtual void emitCode(GrGLProgramBuilder* builder,
118 const GrFragmentProcessor& effect,
119 const GrProcessorKey& key,
120 const char* outputColor,
121 const char* inputColor,
122 const TransformedCoordsArray& coords,
123 const TextureSamplerArray& samplers) = 0;
126 typedef GrGLProcessor INHERITED;