GrGLProcessor.h revision 87f48d997ec29e5eeaa7567355775e93465dd60d
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
8b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt#ifndef GrGLProcessor_DEFINED
9b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt#define GrGLProcessor_DEFINED
10168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
11b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt#include "GrBackendProcessorFactory.h"
1247bb38283072dc87dc93220cd2f370ca109972ffjoshualitt#include "GrGLProgramDataManager.h"
1347bb38283072dc87dc93220cd2f370ca109972ffjoshualitt#include "GrTextureAccess.h"
14168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
15168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com/** @file
16374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com    This file contains specializations for OpenGL of the shader stages declared in
17b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    include/gpu/GrProcessor.h. Objects of type GrGLProcessor are responsible for emitting the
18b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    GLSL code that implements a GrProcessor and for uploading uniforms at draw time. If they don't
1977af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    always emit the same GLSL code, they must have a function:
20b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*)
21b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    that is used to implement a program cache. When two GrProcessors produce the same key this means
22b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    that their GrGLProcessors would emit the same GLSL code.
23374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com
24b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    The GrGLProcessor subclass must also have a constructor of the form:
2587f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        ProcessorSubclass::ProcessorSubclass(const GrBackendProcessorFactory&, const GrProcessor&)
26c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com
27b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    These objects are created by the factory object returned by the GrProcessor::getFactory().
28168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com*/
29168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
30b0a8a377f832c59cee939ad721e1f87d378b7142joshualittclass GrGLProcessor {
31168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.compublic:
32b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    GrGLProcessor(const GrBackendProcessorFactory& factory)
33b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        : fFactory(factory) {
34b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    }
35b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt
3623e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    typedef GrGLProgramDataManager::UniformHandle UniformHandle;
3723e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
3823e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    /**
39b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt     * Passed to GrGLProcessors so they can add transformed coordinates to their shader code.
4023e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt     */
4123e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    typedef GrShaderVar TransformedCoords;
4223e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    typedef SkTArray<GrShaderVar> TransformedCoordsArray;
4323e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
4423e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    /**
45b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt     * Passed to GrGLProcessors so they can add texture reads to their shader code.
4623e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt     */
4723e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    class TextureSampler {
4823e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    public:
4923e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        TextureSampler(UniformHandle uniform, const GrTextureAccess& access)
5023e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt            : fSamplerUniform(uniform)
5123e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt            , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture()->config())) {
5223e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt            SkASSERT(0 != fConfigComponentMask);
5323e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt            memcpy(fSwizzle, access.getSwizzle(), 5);
5423e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        }
5523e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
5623e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        // bitfield of GrColorComponentFlags present in the texture's config.
5723e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        uint32_t configComponentMask() const { return fConfigComponentMask; }
5823e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        // this is .abcd
5923e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        const char* swizzle() const { return fSwizzle; }
6023e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
6123e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    private:
6223e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        UniformHandle fSamplerUniform;
6323e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        uint32_t      fConfigComponentMask;
6423e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        char          fSwizzle[5];
6523e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
6623e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        friend class GrGLShaderBuilder;
6723e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    };
6823e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
6923e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    typedef SkTArray<TextureSampler> TextureSamplerArray;
702eaaefd7e6a58339b3f93333f1e9cc92252cc303bsalomon@google.com
71b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    virtual ~GrGLProcessor() {}
72b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt
73b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    const char* name() const { return fFactory.name(); }
74b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt
75b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*) {}
76b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt
77b0a8a377f832c59cee939ad721e1f87d378b7142joshualittprotected:
78b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    const GrBackendProcessorFactory& fFactory;
79b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt};
80b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt
811598899975ecc85b003a59740b588d1ddbcedb09joshualittclass GrGLFPBuilder;
821598899975ecc85b003a59740b588d1ddbcedb09joshualitt
83b0a8a377f832c59cee939ad721e1f87d378b7142joshualittclass GrGLFragmentProcessor : public GrGLProcessor {
84b0a8a377f832c59cee939ad721e1f87d378b7142joshualittpublic:
85b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    GrGLFragmentProcessor(const GrBackendProcessorFactory& factory)
86b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        : INHERITED(factory) {
87261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.org    }
88f06df1bb9ab201a78bfc906a9e95326c6e15a119bsalomon@google.com
89b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    virtual ~GrGLFragmentProcessor() {}
90d8f856c32b679d9f5a9926feac005e2c0186f83ftomhudson@google.com
91374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com    /** Called when the program stage should insert its code into the shaders. The code in each
92374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        shader will be in its own block ({}) and so locally scoped names will not collide across
93374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        stages.
94374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com
95374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        @param builder      Interface used to emit code in the shaders.
9687f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        @param processor    The processor that generated this program stage.
97b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        @param key          The key that was computed by GenKey() from the generating GrProcessor.
98374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        @param outputColor  A predefined vec4 in the FS in which the stage should place its output
99374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            color (or coverage).
100374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        @param inputColor   A vec4 that holds the input color to the stage in the FS. This may be
101374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            NULL in which case the implied input is solid white (all ones).
102374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            TODO: Better system for communicating optimization info (e.g. input
103374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            color is solid white, trans black, known to be opaque, etc.) that allows
10487f48d997ec29e5eeaa7567355775e93465dd60djoshualitt                            the processor to communicate back similar known info about its output.
105b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        @param samplers     Contains one entry for each GrTextureAccess of the GrProcessor. These
106b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                            can be passed to the builder to emit texture reads in the generated
107b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                            code.
108a5305a110ab5201d5dadd40cbe711582d5ac4996joshualitt        TODO this should take a struct
109d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com        */
1101598899975ecc85b003a59740b588d1ddbcedb09joshualitt    virtual void emitCode(GrGLFPBuilder* builder,
11187f48d997ec29e5eeaa7567355775e93465dd60djoshualitt                          const GrFragmentProcessor&,
112374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                          const char* outputColor,
113374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                          const char* inputColor,
11477af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com                          const TransformedCoordsArray& coords,
115374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                          const TextureSamplerArray& samplers) = 0;
116374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com
11787f48d997ec29e5eeaa7567355775e93465dd60djoshualitt    /** A GrGLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
11887f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        the same stage key; this function reads data from a GrFragmentProcessor and uploads any
11987f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        uniform variables required by the shaders created in emitCode(). The GrFragmentProcessor
12087f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        parameter is guaranteed to be of the same type that created this GrGLFragmentProcessor and
12187f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        to have an identical processor key as the one that created this GrGLFragmentProcessor.  */
12287f48d997ec29e5eeaa7567355775e93465dd60djoshualitt    // TODO update this to pass in GrFragmentProcessor
12387f48d997ec29e5eeaa7567355775e93465dd60djoshualitt    virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) {}
12487f48d997ec29e5eeaa7567355775e93465dd60djoshualitt
125261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.orgprivate:
126b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    typedef GrGLProcessor INHERITED;
127168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com};
128168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
129378092f3d10b1dd62967f419c35cfefec7c10ee7egdanielclass GrGLXferProcessor : public GrGLFragmentProcessor {
130378092f3d10b1dd62967f419c35cfefec7c10ee7egdanielpublic:
131378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel    GrGLXferProcessor(const GrBackendProcessorFactory& factory)
132378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel        : INHERITED(factory) {
133378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel    }
134378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel
135378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel    virtual ~GrGLXferProcessor() {}
136378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel
137378092f3d10b1dd62967f419c35cfefec7c10ee7egdanielprivate:
138378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel    typedef GrGLFragmentProcessor INHERITED;
139378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel};
140378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel
141168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com#endif
142