michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef GrGLEffect_DEFINED michael@0: #define GrGLEffect_DEFINED michael@0: michael@0: #include "GrBackendEffectFactory.h" michael@0: #include "GrGLProgramEffects.h" michael@0: #include "GrGLShaderBuilder.h" michael@0: #include "GrGLShaderVar.h" michael@0: #include "GrGLSL.h" michael@0: michael@0: /** @file michael@0: This file contains specializations for OpenGL of the shader stages declared in michael@0: include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitting the michael@0: GLSL code that implements a GrEffect and for uploading uniforms at draw time. If they don't michael@0: always emit the same GLSL code, they must have a function: michael@0: static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) michael@0: that is used to implement a program cache. When two GrEffects produce the same key this means michael@0: that their GrGLEffects would emit the same GLSL code. michael@0: michael@0: The GrGLEffect subclass must also have a constructor of the form: michael@0: EffectSubclass::EffectSubclass(const GrBackendEffectFactory&, const GrDrawEffect&) michael@0: The effect held by the GrDrawEffect is guaranteed to be of the type that generated the michael@0: GrGLEffect subclass instance. michael@0: michael@0: These objects are created by the factory object returned by the GrEffect::getFactory(). michael@0: */ michael@0: michael@0: class GrDrawEffect; michael@0: class GrGLTexture; michael@0: class GrGLVertexEffect; michael@0: michael@0: class GrGLEffect { michael@0: michael@0: public: michael@0: typedef GrBackendEffectFactory::EffectKey EffectKey; michael@0: typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; michael@0: typedef GrGLProgramEffects::TextureSampler TextureSampler; michael@0: typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray; michael@0: michael@0: enum { michael@0: kNoEffectKey = GrBackendEffectFactory::kNoEffectKey, michael@0: // the number of bits in EffectKey available to GenKey michael@0: kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits, michael@0: }; michael@0: michael@0: GrGLEffect(const GrBackendEffectFactory& factory) michael@0: : fFactory(factory) michael@0: , fIsVertexEffect(false) { michael@0: } michael@0: michael@0: virtual ~GrGLEffect() {} michael@0: michael@0: /** Called when the program stage should insert its code into the shaders. The code in each michael@0: shader will be in its own block ({}) and so locally scoped names will not collide across michael@0: stages. michael@0: michael@0: @param builder Interface used to emit code in the shaders. michael@0: @param drawEffect A wrapper on the effect that generated this program stage. michael@0: @param key The key that was computed by GenKey() from the generating GrEffect. michael@0: Only the bits indicated by GrBackendEffectFactory::kEffectKeyBits are michael@0: guaranteed to match the value produced by GenKey(); michael@0: @param outputColor A predefined vec4 in the FS in which the stage should place its output michael@0: color (or coverage). michael@0: @param inputColor A vec4 that holds the input color to the stage in the FS. This may be michael@0: NULL in which case the implied input is solid white (all ones). michael@0: TODO: Better system for communicating optimization info (e.g. input michael@0: color is solid white, trans black, known to be opaque, etc.) that allows michael@0: the effect to communicate back similar known info about its output. michael@0: @param samplers One entry for each GrTextureAccess of the GrEffect that generated the michael@0: GrGLEffect. These can be passed to the builder to emit texture michael@0: reads in the generated code. michael@0: */ michael@0: virtual void emitCode(GrGLShaderBuilder* builder, michael@0: const GrDrawEffect& drawEffect, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray& coords, michael@0: const TextureSamplerArray& samplers) = 0; michael@0: michael@0: /** A GrGLEffect instance can be reused with any GrEffect that produces the same stage michael@0: key; this function reads data from a stage and uploads any uniform variables required michael@0: by the shaders created in emitCode(). The GrEffect installed in the GrEffectStage is michael@0: guaranteed to be of the same type that created this GrGLEffect and to have an identical michael@0: EffectKey as the one that created this GrGLEffect. Effects that use local coords have michael@0: to consider whether the GrEffectStage's coord change matrix should be used. When explicit michael@0: local coordinates are used it can be ignored. */ michael@0: virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) {} michael@0: michael@0: const char* name() const { return fFactory.name(); } michael@0: michael@0: static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { return 0; } michael@0: michael@0: /** Used by the system when generating shader code, to see if this effect can be downcasted to michael@0: the internal GrGLVertexEffect type */ michael@0: bool isVertexEffect() const { return fIsVertexEffect; } michael@0: michael@0: protected: michael@0: const GrBackendEffectFactory& fFactory; michael@0: michael@0: private: michael@0: friend class GrGLVertexEffect; // to set fIsVertexEffect michael@0: michael@0: bool fIsVertexEffect; michael@0: }; michael@0: michael@0: #endif