blob: 6401e2b324abdacfc36fe11fdafd1e24017bb2e3 [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:
joshualitt87f48d92014-12-04 10:41:40 -080025 ProcessorSubclass::ProcessorSubclass(const GrBackendProcessorFactory&, const GrProcessor&)
joshualittb0a8a372014-09-23 09:50:21 -070026
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
joshualittb0a8a372014-09-23 09:50:21 -070073 const char* name() const { return fFactory.name(); }
74
75 static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*) {}
76
77protected:
78 const GrBackendProcessorFactory& fFactory;
79};
80
joshualitt15988992014-10-09 15:04:05 -070081class GrGLFPBuilder;
82
joshualittb0a8a372014-09-23 09:50:21 -070083class GrGLFragmentProcessor : public GrGLProcessor {
84public:
85 GrGLFragmentProcessor(const GrBackendProcessorFactory& factory)
86 : INHERITED(factory) {
87 }
88
89 virtual ~GrGLFragmentProcessor() {}
90
91 /** Called when the program stage should insert its code into the shaders. The code in each
92 shader will be in its own block ({}) and so locally scoped names will not collide across
93 stages.
94
95 @param builder Interface used to emit code in the shaders.
joshualitt87f48d92014-12-04 10:41:40 -080096 @param processor The processor that generated this program stage.
joshualittb0a8a372014-09-23 09:50:21 -070097 @param key The key that was computed by GenKey() from the generating GrProcessor.
98 @param outputColor A predefined vec4 in the FS in which the stage should place its output
99 color (or coverage).
100 @param inputColor A vec4 that holds the input color to the stage in the FS. This may be
101 NULL in which case the implied input is solid white (all ones).
102 TODO: Better system for communicating optimization info (e.g. input
103 color is solid white, trans black, known to be opaque, etc.) that allows
joshualitt87f48d92014-12-04 10:41:40 -0800104 the processor to communicate back similar known info about its output.
joshualittb0a8a372014-09-23 09:50:21 -0700105 @param samplers Contains one entry for each GrTextureAccess of the GrProcessor. These
106 can be passed to the builder to emit texture reads in the generated
107 code.
joshualitta5305a12014-10-10 17:47:00 -0700108 TODO this should take a struct
joshualittb0a8a372014-09-23 09:50:21 -0700109 */
joshualitt15988992014-10-09 15:04:05 -0700110 virtual void emitCode(GrGLFPBuilder* builder,
joshualitt87f48d92014-12-04 10:41:40 -0800111 const GrFragmentProcessor&,
joshualittb0a8a372014-09-23 09:50:21 -0700112 const char* outputColor,
113 const char* inputColor,
114 const TransformedCoordsArray& coords,
115 const TextureSamplerArray& samplers) = 0;
116
joshualitt87f48d92014-12-04 10:41:40 -0800117 /** A GrGLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
118 the same stage key; this function reads data from a GrFragmentProcessor and uploads any
119 uniform variables required by the shaders created in emitCode(). The GrFragmentProcessor
120 parameter is guaranteed to be of the same type that created this GrGLFragmentProcessor and
121 to have an identical processor key as the one that created this GrGLFragmentProcessor. */
122 // TODO update this to pass in GrFragmentProcessor
123 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) {}
124
joshualittb0a8a372014-09-23 09:50:21 -0700125private:
126 typedef GrGLProcessor INHERITED;
127};
128
egdaniel378092f2014-12-03 10:40:13 -0800129class GrGLXferProcessor : public GrGLFragmentProcessor {
130public:
131 GrGLXferProcessor(const GrBackendProcessorFactory& factory)
132 : INHERITED(factory) {
133 }
134
135 virtual ~GrGLXferProcessor() {}
136
137private:
138 typedef GrGLFragmentProcessor INHERITED;
139};
140
joshualittb0a8a372014-09-23 09:50:21 -0700141#endif