joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1 | /* |
| 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" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 12 | #include "GrGLProgramDataManager.h" |
| 13 | #include "GrTextureAccess.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 14 | |
| 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: |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame^] | 25 | ProcessorSubclass::ProcessorSubclass(const GrBackendProcessorFactory&, const GrProcessor&) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 26 | |
| 27 | These objects are created by the factory object returned by the GrProcessor::getFactory(). |
| 28 | */ |
| 29 | |
| 30 | class GrGLProcessor { |
| 31 | public: |
| 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 73 | const char* name() const { return fFactory.name(); } |
| 74 | |
| 75 | static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*) {} |
| 76 | |
| 77 | protected: |
| 78 | const GrBackendProcessorFactory& fFactory; |
| 79 | }; |
| 80 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 81 | class GrGLFPBuilder; |
| 82 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 83 | class GrGLFragmentProcessor : public GrGLProcessor { |
| 84 | public: |
| 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. |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame^] | 96 | @param processor The processor that generated this program stage. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 97 | @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 |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame^] | 104 | the processor to communicate back similar known info about its output. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 105 | @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. |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 108 | TODO this should take a struct |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 109 | */ |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 110 | virtual void emitCode(GrGLFPBuilder* builder, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame^] | 111 | const GrFragmentProcessor&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 112 | const char* outputColor, |
| 113 | const char* inputColor, |
| 114 | const TransformedCoordsArray& coords, |
| 115 | const TextureSamplerArray& samplers) = 0; |
| 116 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame^] | 117 | /** 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 125 | private: |
| 126 | typedef GrGLProcessor INHERITED; |
| 127 | }; |
| 128 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 129 | class GrGLXferProcessor : public GrGLFragmentProcessor { |
| 130 | public: |
| 131 | GrGLXferProcessor(const GrBackendProcessorFactory& factory) |
| 132 | : INHERITED(factory) { |
| 133 | } |
| 134 | |
| 135 | virtual ~GrGLXferProcessor() {} |
| 136 | |
| 137 | private: |
| 138 | typedef GrGLFragmentProcessor INHERITED; |
| 139 | }; |
| 140 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 141 | #endif |