blob: a4fad753ec668ae276f6c0e725f3d41b83d4c9b9 [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
joshualitt47bb3822014-10-07 16:43:25 -070011#include "GrGLProgramDataManager.h"
joshualitteb2a6762014-12-04 11:35:33 -080012#include "GrProcessor.h"
joshualitt47bb3822014-10-07 16:43:25 -070013#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*/
joshualitteb2a6762014-12-04 11:35:33 -080029// TODO delete this and make TextureSampler its own thing
joshualittb0a8a372014-09-23 09:50:21 -070030class GrGLProcessor {
31public:
joshualittb0a8a372014-09-23 09:50:21 -070032 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
33
34 /**
35 * Passed to GrGLProcessors so they can add transformed coordinates to their shader code.
36 */
37 typedef GrShaderVar TransformedCoords;
38 typedef SkTArray<GrShaderVar> TransformedCoordsArray;
39
40 /**
41 * Passed to GrGLProcessors so they can add texture reads to their shader code.
42 */
43 class TextureSampler {
44 public:
45 TextureSampler(UniformHandle uniform, const GrTextureAccess& access)
46 : fSamplerUniform(uniform)
47 , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture()->config())) {
48 SkASSERT(0 != fConfigComponentMask);
49 memcpy(fSwizzle, access.getSwizzle(), 5);
50 }
51
52 // bitfield of GrColorComponentFlags present in the texture's config.
53 uint32_t configComponentMask() const { return fConfigComponentMask; }
54 // this is .abcd
55 const char* swizzle() const { return fSwizzle; }
56
57 private:
58 UniformHandle fSamplerUniform;
59 uint32_t fConfigComponentMask;
60 char fSwizzle[5];
61
62 friend class GrGLShaderBuilder;
63 };
64
65 typedef SkTArray<TextureSampler> TextureSamplerArray;
joshualittb0a8a372014-09-23 09:50:21 -070066};
67
joshualitt15988992014-10-09 15:04:05 -070068class GrGLFPBuilder;
69
joshualitteb2a6762014-12-04 11:35:33 -080070class GrGLFragmentProcessor {
joshualittb0a8a372014-09-23 09:50:21 -070071public:
joshualitteb2a6762014-12-04 11:35:33 -080072 GrGLFragmentProcessor() {}
joshualittb0a8a372014-09-23 09:50:21 -070073
74 virtual ~GrGLFragmentProcessor() {}
75
joshualitteb2a6762014-12-04 11:35:33 -080076 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
77 typedef GrGLProcessor::TransformedCoordsArray TransformedCoordsArray;
78 typedef GrGLProcessor::TextureSamplerArray TextureSamplerArray;
79
joshualittb0a8a372014-09-23 09:50:21 -070080 /** Called when the program stage should insert its code into the shaders. The code in each
81 shader will be in its own block ({}) and so locally scoped names will not collide across
82 stages.
83
84 @param builder Interface used to emit code in the shaders.
joshualitt87f48d92014-12-04 10:41:40 -080085 @param processor The processor that generated this program stage.
joshualittb0a8a372014-09-23 09:50:21 -070086 @param key The key that was computed by GenKey() from the generating GrProcessor.
87 @param outputColor A predefined vec4 in the FS in which the stage should place its output
88 color (or coverage).
89 @param inputColor A vec4 that holds the input color to the stage in the FS. This may be
90 NULL in which case the implied input is solid white (all ones).
91 TODO: Better system for communicating optimization info (e.g. input
92 color is solid white, trans black, known to be opaque, etc.) that allows
joshualitt87f48d92014-12-04 10:41:40 -080093 the processor to communicate back similar known info about its output.
joshualittb0a8a372014-09-23 09:50:21 -070094 @param samplers Contains one entry for each GrTextureAccess of the GrProcessor. These
95 can be passed to the builder to emit texture reads in the generated
96 code.
joshualitta5305a12014-10-10 17:47:00 -070097 TODO this should take a struct
joshualittb0a8a372014-09-23 09:50:21 -070098 */
joshualitt15988992014-10-09 15:04:05 -070099 virtual void emitCode(GrGLFPBuilder* builder,
joshualitt87f48d92014-12-04 10:41:40 -0800100 const GrFragmentProcessor&,
joshualittb0a8a372014-09-23 09:50:21 -0700101 const char* outputColor,
102 const char* inputColor,
103 const TransformedCoordsArray& coords,
104 const TextureSamplerArray& samplers) = 0;
105
joshualitt87f48d92014-12-04 10:41:40 -0800106 /** A GrGLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
107 the same stage key; this function reads data from a GrFragmentProcessor and uploads any
108 uniform variables required by the shaders created in emitCode(). The GrFragmentProcessor
109 parameter is guaranteed to be of the same type that created this GrGLFragmentProcessor and
110 to have an identical processor key as the one that created this GrGLFragmentProcessor. */
111 // TODO update this to pass in GrFragmentProcessor
112 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) {}
113
joshualitteb2a6762014-12-04 11:35:33 -0800114 static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*) {}
115
joshualittb0a8a372014-09-23 09:50:21 -0700116private:
117 typedef GrGLProcessor INHERITED;
118};
119
joshualittb0a8a372014-09-23 09:50:21 -0700120#endif