blob: 393bb87cb33d4b11cd590b6c74954df5a60a2f56 [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
90class GrGLFragmentProcessor : public GrGLProcessor {
91public:
92 GrGLFragmentProcessor(const GrBackendProcessorFactory& factory)
93 : INHERITED(factory) {
94 }
95
96 virtual ~GrGLFragmentProcessor() {}
97
98 /** Called when the program stage should insert its code into the shaders. The code in each
99 shader will be in its own block ({}) and so locally scoped names will not collide across
100 stages.
101
102 @param builder Interface used to emit code in the shaders.
103 @param effect The effect that generated this program stage.
104 @param key The key that was computed by GenKey() from the generating GrProcessor.
105 @param outputColor A predefined vec4 in the FS in which the stage should place its output
106 color (or coverage).
107 @param inputColor A vec4 that holds the input color to the stage in the FS. This may be
108 NULL in which case the implied input is solid white (all ones).
109 TODO: Better system for communicating optimization info (e.g. input
110 color is solid white, trans black, known to be opaque, etc.) that allows
111 the effect to communicate back similar known info about its output.
112 @param samplers Contains one entry for each GrTextureAccess of the GrProcessor. These
113 can be passed to the builder to emit texture reads in the generated
114 code.
115 */
116 virtual void emitCode(GrGLProgramBuilder* builder,
117 const GrFragmentProcessor& effect,
118 const GrProcessorKey& key,
119 const char* outputColor,
120 const char* inputColor,
121 const TransformedCoordsArray& coords,
122 const TextureSamplerArray& samplers) = 0;
123
124private:
125 typedef GrGLProcessor INHERITED;
126};
127
128#endif