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 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 11 | #include "GrGLProgramDataManager.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 12 | #include "GrProcessor.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 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 | */ |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 29 | // TODO delete this and make TextureSampler its own thing |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 30 | class GrGLProcessor { |
| 31 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 32 | 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; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 68 | class GrGLFPBuilder; |
| 69 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 70 | class GrGLFragmentProcessor { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 71 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 72 | GrGLFragmentProcessor() {} |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 73 | |
| 74 | virtual ~GrGLFragmentProcessor() {} |
| 75 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 76 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| 77 | typedef GrGLProcessor::TransformedCoordsArray TransformedCoordsArray; |
| 78 | typedef GrGLProcessor::TextureSamplerArray TextureSamplerArray; |
| 79 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 80 | /** 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. |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 85 | @param processor The processor that generated this program stage. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 86 | @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 |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 93 | the processor to communicate back similar known info about its output. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 94 | @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. |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 97 | TODO this should take a struct |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 98 | */ |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 99 | virtual void emitCode(GrGLFPBuilder* builder, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 100 | const GrFragmentProcessor&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 101 | const char* outputColor, |
| 102 | const char* inputColor, |
| 103 | const TransformedCoordsArray& coords, |
| 104 | const TextureSamplerArray& samplers) = 0; |
| 105 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 106 | /** 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 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 114 | static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*) {} |
| 115 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 116 | private: |
| 117 | typedef GrGLProcessor INHERITED; |
| 118 | }; |
| 119 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 120 | #endif |