GrGLProcessor.h revision 261dc569b6a53729bea6e4e7a0cf2afa980eb82d
1168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com/*
2168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com * Copyright 2012 Google Inc.
3168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com *
4168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com * Use of this source code is governed by a BSD-style license that can be
5168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com * found in the LICENSE file.
6168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com */
7168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
8422e81aeb1f4078367c85efe591c7df8c33874ecbsalomon@google.com#ifndef GrGLEffect_DEFINED
9422e81aeb1f4078367c85efe591c7df8c33874ecbsalomon@google.com#define GrGLEffect_DEFINED
10168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
112eaaefd7e6a58339b3f93333f1e9cc92252cc303bsalomon@google.com#include "GrBackendEffectFactory.h"
123390b9ac9ad69a6e772c2b957d75d19611239025commit-bot@chromium.org#include "GrGLProgramEffects.h"
139c639a406eec42c8f5169bd78ad93e58f00ea9f6tomhudson@google.com#include "GrGLShaderBuilder.h"
14168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com#include "GrGLShaderVar.h"
15168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com#include "GrGLSL.h"
16168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
17168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com/** @file
18374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com    This file contains specializations for OpenGL of the shader stages declared in
19d698f77c13d97c61109b861eac4d25b14a5de935bsalomon@google.com    include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitting the
2077af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    GLSL code that implements a GrEffect and for uploading uniforms at draw time. If they don't
2177af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    always emit the same GLSL code, they must have a function:
22c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&)
238ea78d83dc4e8243c16eedf8100a3987c54123fabsalomon@google.com    that is used to implement a program cache. When two GrEffects produce the same key this means
24422e81aeb1f4078367c85efe591c7df8c33874ecbsalomon@google.com    that their GrGLEffects would emit the same GLSL code.
25374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com
26c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    The GrGLEffect subclass must also have a constructor of the form:
27c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        EffectSubclass::EffectSubclass(const GrBackendEffectFactory&, const GrDrawEffect&)
28c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    The effect held by the GrDrawEffect is guaranteed to be of the type that generated the
29c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    GrGLEffect subclass instance.
30c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com
31a469c28c3c16214733a25201a286970f57b3d944bsalomon@google.com    These objects are created by the factory object returned by the GrEffect::getFactory().
32168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com*/
33168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
34c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.comclass GrDrawEffect;
35261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.orgclass GrGLTexture;
36261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.orgclass GrGLVertexEffect;
37c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com
38d698f77c13d97c61109b861eac4d25b14a5de935bsalomon@google.comclass GrGLEffect {
39168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
40168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.compublic:
412eaaefd7e6a58339b3f93333f1e9cc92252cc303bsalomon@google.com    typedef GrBackendEffectFactory::EffectKey EffectKey;
423390b9ac9ad69a6e772c2b957d75d19611239025commit-bot@chromium.org    typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray;
433390b9ac9ad69a6e772c2b957d75d19611239025commit-bot@chromium.org    typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray;
442eaaefd7e6a58339b3f93333f1e9cc92252cc303bsalomon@google.com
45b505a128efae9debcaa9642bade90bab5525d477bsalomon@google.com    enum {
46dbe49f735484f8862e378b63d0a074a301093dd0bsalomon@google.com        kNoEffectKey = GrBackendEffectFactory::kNoEffectKey,
4746fba0d79335f17429bb71d87a04d93fb2ee992bbsalomon@google.com        // the number of bits in EffectKey available to GenKey
4846fba0d79335f17429bb71d87a04d93fb2ee992bbsalomon@google.com        kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits,
49b505a128efae9debcaa9642bade90bab5525d477bsalomon@google.com    };
50b505a128efae9debcaa9642bade90bab5525d477bsalomon@google.com
51261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org    GrGLEffect(const GrBackendEffectFactory& factory)
52261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org        : fFactory(factory)
53261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org        , fIsVertexEffect(false) {
54261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org    }
55f06df1bb9ab201a78bfc906a9e95326c6e15a119bsalomon@google.com
563390b9ac9ad69a6e772c2b957d75d19611239025commit-bot@chromium.org    virtual ~GrGLEffect() {}
57d8f856c32b679d9f5a9926feac005e2c0186f83ftomhudson@google.com
58374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com    /** Called when the program stage should insert its code into the shaders. The code in each
59374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        shader will be in its own block ({}) and so locally scoped names will not collide across
60374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        stages.
61374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com
62374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        @param builder      Interface used to emit code in the shaders.
63c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        @param drawEffect   A wrapper on the effect that generated this program stage.
64d8b5faca043100d7a1e4594b4d10e462532af390bsalomon@google.com        @param key          The key that was computed by GenKey() from the generating GrEffect.
65d8b5faca043100d7a1e4594b4d10e462532af390bsalomon@google.com                            Only the bits indicated by GrBackendEffectFactory::kEffectKeyBits are
66d8b5faca043100d7a1e4594b4d10e462532af390bsalomon@google.com                            guaranteed to match the value produced by GenKey();
67374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        @param outputColor  A predefined vec4 in the FS in which the stage should place its output
68374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            color (or coverage).
69374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        @param inputColor   A vec4 that holds the input color to the stage in the FS. This may be
70374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            NULL in which case the implied input is solid white (all ones).
71374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            TODO: Better system for communicating optimization info (e.g. input
72374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            color is solid white, trans black, known to be opaque, etc.) that allows
73f271cc7183fe48ac64d2d9a454eb013c91b42d53bsalomon@google.com                            the effect to communicate back similar known info about its output.
74a469c28c3c16214733a25201a286970f57b3d944bsalomon@google.com        @param samplers     One entry for each GrTextureAccess of the GrEffect that generated the
75d698f77c13d97c61109b861eac4d25b14a5de935bsalomon@google.com                            GrGLEffect. These can be passed to the builder to emit texture
76374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            reads in the generated code.
77d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com        */
78374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com    virtual void emitCode(GrGLShaderBuilder* builder,
79c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                          const GrDrawEffect& drawEffect,
8046fba0d79335f17429bb71d87a04d93fb2ee992bbsalomon@google.com                          EffectKey key,
81374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                          const char* outputColor,
82374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                          const char* inputColor,
8377af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com                          const TransformedCoordsArray& coords,
84374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                          const TextureSamplerArray& samplers) = 0;
85374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com
86d698f77c13d97c61109b861eac4d25b14a5de935bsalomon@google.com    /** A GrGLEffect instance can be reused with any GrEffect that produces the same stage
87a469c28c3c16214733a25201a286970f57b3d944bsalomon@google.com        key; this function reads data from a stage and uploads any uniform variables required
8828a15fb8d603847949a61657ef5cb73ed9915021bsalomon@google.com        by the shaders created in emitCode(). The GrEffect installed in the GrEffectStage is
8928a15fb8d603847949a61657ef5cb73ed9915021bsalomon@google.com        guaranteed to be of the same type that created this GrGLEffect and to have an identical
90c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        EffectKey as the one that created this GrGLEffect. Effects that use local coords have
91c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        to consider whether the GrEffectStage's coord change matrix should be used. When explicit
92c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        local coordinates are used it can be ignored. */
933390b9ac9ad69a6e772c2b957d75d19611239025commit-bot@chromium.org    virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) {}
94168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
95289efe014ad7628de7cf2c5177a42cacd1e335adbsalomon@google.com    const char* name() const { return fFactory.name(); }
96289efe014ad7628de7cf2c5177a42cacd1e335adbsalomon@google.com
9777af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { return 0; }
9877af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com
99261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org    /** Used by the system when generating shader code, to see if this effect can be downcasted to
100261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org        the internal GrGLVertexEffect type */
101261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org    bool isVertexEffect() const { return fIsVertexEffect; }
102261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org
10307eecdca3e331eb4066c53a29305aeea6d692961tomhudson@google.comprotected:
104396e61fe440590744345e0c56970b26ab464591dbsalomon@google.com    const GrBackendEffectFactory& fFactory;
105261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org
106261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.orgprivate:
107261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org    friend class GrGLVertexEffect; // to set fIsVertexEffect
108261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org
109261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org    bool fIsVertexEffect;
110168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com};
111168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
112168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com#endif
113