blob: ca4fa2459f97a1c1ca9b823120fae1d2bdcfeafa [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:
joshualitt841a6b52014-12-04 06:00:41 -080025 EffectSubclass::EffectSubclass(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
joshualitt841a6b52014-12-04 06:00:41 -080073 /** 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 parameter is guaranteed to be of the
76 same type that created this GrGLProcessor and to have an identical effect key as the one
77 that created this GrGLProcessor. */
78 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) {}
79
joshualittb0a8a372014-09-23 09:50:21 -070080 const char* name() const { return fFactory.name(); }
81
82 static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*) {}
83
84protected:
85 const GrBackendProcessorFactory& fFactory;
86};
87
joshualitt15988992014-10-09 15:04:05 -070088class GrGLFPBuilder;
89
joshualittb0a8a372014-09-23 09:50:21 -070090class 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.
joshualitt841a6b52014-12-04 06:00:41 -0800103 @param effect The effect that generated this program stage.
joshualittb0a8a372014-09-23 09:50:21 -0700104 @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
joshualitt841a6b52014-12-04 06:00:41 -0800111 the effect to communicate back similar known info about its output.
joshualittb0a8a372014-09-23 09:50:21 -0700112 @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.
joshualitta5305a12014-10-10 17:47:00 -0700115 TODO this should take a struct
joshualittb0a8a372014-09-23 09:50:21 -0700116 */
joshualitt15988992014-10-09 15:04:05 -0700117 virtual void emitCode(GrGLFPBuilder* builder,
joshualitt841a6b52014-12-04 06:00:41 -0800118 const GrFragmentProcessor& effect,
joshualittb0a8a372014-09-23 09:50:21 -0700119 const char* outputColor,
120 const char* inputColor,
121 const TransformedCoordsArray& coords,
122 const TextureSamplerArray& samplers) = 0;
123
124private:
125 typedef GrGLProcessor INHERITED;
126};
127
egdaniel378092f2014-12-03 10:40:13 -0800128class GrGLXferProcessor : public GrGLFragmentProcessor {
129public:
130 GrGLXferProcessor(const GrBackendProcessorFactory& factory)
131 : INHERITED(factory) {
132 }
133
134 virtual ~GrGLXferProcessor() {}
135
136private:
137 typedef GrGLFragmentProcessor INHERITED;
138};
139
joshualittb0a8a372014-09-23 09:50:21 -0700140#endif