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 "GrGLProgramDataManager.h" 12 #include "GrProcessor.h" 13 #include "GrTextureAccess.h" 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 GrGLSLCaps&, 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: 25 ProcessorSubclass::ProcessorSubclass(const GrBackendProcessorFactory&, const GrProcessor&) 26 27 These objects are created by the factory object returned by the GrProcessor::getFactory(). 28 */ 29 // TODO delete this and make TextureSampler its own thing 30 class GrGLProcessor { 31 public: 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: TextureSampler(UniformHandle uniform,const GrTextureAccess & access)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. configComponentMask()53 uint32_t configComponentMask() const { return fConfigComponentMask; } 54 // this is .abcd swizzle()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; 66 }; 67 68 class GrGLFPBuilder; 69 70 class GrGLFragmentProcessor { 71 public: GrGLFragmentProcessor()72 GrGLFragmentProcessor() {} 73 ~GrGLFragmentProcessor()74 virtual ~GrGLFragmentProcessor() {} 75 76 typedef GrGLProgramDataManager::UniformHandle UniformHandle; 77 typedef GrGLProcessor::TransformedCoordsArray TransformedCoordsArray; 78 typedef GrGLProcessor::TextureSamplerArray TextureSamplerArray; 79 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. 85 @param processor The processor that generated this program stage. 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 93 the processor to communicate back similar known info about its output. 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. 97 TODO this should take a struct 98 */ 99 virtual void emitCode(GrGLFPBuilder* builder, 100 const GrFragmentProcessor&, 101 const char* outputColor, 102 const char* inputColor, 103 const TransformedCoordsArray& coords, 104 const TextureSamplerArray& samplers) = 0; 105 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 setData(const GrGLProgramDataManager &,const GrProcessor &)112 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) {} 113 GenKey(const GrProcessor &,const GrGLSLCaps &,GrProcessorKeyBuilder *)114 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {} 115 116 private: 117 typedef GrGLProcessor INHERITED; 118 }; 119 120 #endif 121