blob: 5f698325fbbe3d0d50811af0ae47bdf151a342be [file] [log] [blame]
joshualittb0a8a372014-09-23 09:50:21 -07001/*
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
8#ifndef GrGLProcessor_DEFINED
9#define GrGLProcessor_DEFINED
10
11#include "GrBackendProcessorFactory.h"
joshualitt47bb3822014-10-07 16:43:25 -070012#include "GrGLProgramDataManager.h"
13#include "GrTextureAccess.h"
joshualittb0a8a372014-09-23 09:50:21 -070014
15/** @file
16 This file contains specializations for OpenGL of the shader stages declared in
17 include/gpu/GrProcessor.h. Objects of type GrGLProcessor are responsible for emitting the
18 GLSL code that implements a GrProcessor and for uploading uniforms at draw time. If they don't
19 always emit the same GLSL code, they must have a function:
20 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*)
21 that is used to implement a program cache. When two GrProcessors produce the same key this means
22 that their GrGLProcessors would emit the same GLSL code.
23
24 The GrGLProcessor subclass must also have a constructor of the form:
25 EffectSubclass::EffectSubclass(const GrBackendEffectFactory&, const GrProcessor&)
26
27 These objects are created by the factory object returned by the GrProcessor::getFactory().
28*/
29
30class GrGLProcessor {
31public:
32 GrGLProcessor(const GrBackendProcessorFactory& factory)
33 : fFactory(factory) {
34 }
35
36 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
37
38 /**
39 * Passed to GrGLProcessors so they can add transformed coordinates to their shader code.
40 */
41 typedef GrShaderVar TransformedCoords;
42 typedef SkTArray<GrShaderVar> TransformedCoordsArray;
43
44 /**
45 * Passed to GrGLProcessors so they can add texture reads to their shader code.
46 */
47 class TextureSampler {
48 public:
49 TextureSampler(UniformHandle uniform, const GrTextureAccess& access)
50 : fSamplerUniform(uniform)
51 , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture()->config())) {
52 SkASSERT(0 != fConfigComponentMask);
53 memcpy(fSwizzle, access.getSwizzle(), 5);
54 }
55
56 // bitfield of GrColorComponentFlags present in the texture's config.
57 uint32_t configComponentMask() const { return fConfigComponentMask; }
58 // this is .abcd
59 const char* swizzle() const { return fSwizzle; }
60
61 private:
62 UniformHandle fSamplerUniform;
63 uint32_t fConfigComponentMask;
64 char fSwizzle[5];
65
66 friend class GrGLShaderBuilder;
67 };
68
69 typedef SkTArray<TextureSampler> TextureSamplerArray;
70
71 virtual ~GrGLProcessor() {}
72
73 /** A GrGLProcessor instance can be reused with any GrProcessor that produces the same stage
74 key; this function reads data from a GrProcessor and uploads any uniform variables required
75 by the shaders created in emitCode(). The GrProcessor installed in the GrDrawEffect is
76 guaranteed to be of the same type that created this GrGLProcessor and to have an identical
77 effect key as the one that created this GrGLProcessor. Effects that use local coords have
78 to consider whether the GrProcessorStage's coord change matrix should be used. When explicit
79 local coordinates are used it can be ignored. */
80 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) {}
81
82 const char* name() const { return fFactory.name(); }
83
84 static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*) {}
85
86protected:
87 const GrBackendProcessorFactory& fFactory;
88};
89
joshualitt15988992014-10-09 15:04:05 -070090class GrGLFPBuilder;
91
joshualittb0a8a372014-09-23 09:50:21 -070092class GrGLFragmentProcessor : public GrGLProcessor {
93public:
94 GrGLFragmentProcessor(const GrBackendProcessorFactory& factory)
95 : INHERITED(factory) {
96 }
97
98 virtual ~GrGLFragmentProcessor() {}
99
100 /** Called when the program stage should insert its code into the shaders. The code in each
101 shader will be in its own block ({}) and so locally scoped names will not collide across
102 stages.
103
104 @param builder Interface used to emit code in the shaders.
105 @param effect The effect that generated this program stage.
106 @param key The key that was computed by GenKey() from the generating GrProcessor.
107 @param outputColor A predefined vec4 in the FS in which the stage should place its output
108 color (or coverage).
109 @param inputColor A vec4 that holds the input color to the stage in the FS. This may be
110 NULL in which case the implied input is solid white (all ones).
111 TODO: Better system for communicating optimization info (e.g. input
112 color is solid white, trans black, known to be opaque, etc.) that allows
113 the effect to communicate back similar known info about its output.
114 @param samplers Contains one entry for each GrTextureAccess of the GrProcessor. These
115 can be passed to the builder to emit texture reads in the generated
116 code.
117 */
joshualitt15988992014-10-09 15:04:05 -0700118 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700119 const GrFragmentProcessor& effect,
120 const GrProcessorKey& key,
121 const char* outputColor,
122 const char* inputColor,
123 const TransformedCoordsArray& coords,
124 const TextureSamplerArray& samplers) = 0;
125
126private:
127 typedef GrGLProcessor INHERITED;
128};
129
130#endif