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
1147bb38283072dc87dc93220cd2f370ca109972ffjoshualitt#include "GrGLProgramDataManager.h"
12eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt#include "GrProcessor.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:
20cfc18867d982119d9dc2888bf09f1093012daaddjvanverth        static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, 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*/
29eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt// TODO delete this and make TextureSampler its own thing
30b0a8a377f832c59cee939ad721e1f87d378b7142joshualittclass GrGLProcessor {
31168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.compublic:
3223e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    typedef GrGLProgramDataManager::UniformHandle UniformHandle;
3323e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
3423e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    /**
35b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt     * Passed to GrGLProcessors so they can add transformed coordinates to their shader code.
3623e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt     */
3723e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    typedef GrShaderVar TransformedCoords;
3823e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    typedef SkTArray<GrShaderVar> TransformedCoordsArray;
3923e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
4023e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    /**
41b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt     * Passed to GrGLProcessors so they can add texture reads to their shader code.
4223e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt     */
4323e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    class TextureSampler {
4423e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    public:
4523e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        TextureSampler(UniformHandle uniform, const GrTextureAccess& access)
4623e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt            : fSamplerUniform(uniform)
4723e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt            , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture()->config())) {
4823e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt            SkASSERT(0 != fConfigComponentMask);
4923e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt            memcpy(fSwizzle, access.getSwizzle(), 5);
5023e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        }
5123e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
5223e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        // bitfield of GrColorComponentFlags present in the texture's config.
5323e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        uint32_t configComponentMask() const { return fConfigComponentMask; }
5423e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        // this is .abcd
5523e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        const char* swizzle() const { return fSwizzle; }
5623e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
5723e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    private:
5823e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        UniformHandle fSamplerUniform;
5923e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        uint32_t      fConfigComponentMask;
6023e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        char          fSwizzle[5];
6123e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
6223e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt        friend class GrGLShaderBuilder;
6323e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    };
6423e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt
6523e280d1f227d94f6b3dfd0b47359cca1569e1b4joshualitt    typedef SkTArray<TextureSampler> TextureSamplerArray;
66b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt};
67b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt
681598899975ecc85b003a59740b588d1ddbcedb09joshualittclass GrGLFPBuilder;
691598899975ecc85b003a59740b588d1ddbcedb09joshualitt
70eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualittclass GrGLFragmentProcessor {
71b0a8a377f832c59cee939ad721e1f87d378b7142joshualittpublic:
72eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt    GrGLFragmentProcessor() {}
73f06df1bb9ab201a78bfc906a9e95326c6e15a119bsalomon@google.com
74b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    virtual ~GrGLFragmentProcessor() {}
75d8f856c32b679d9f5a9926feac005e2c0186f83ftomhudson@google.com
76eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt    typedef GrGLProgramDataManager::UniformHandle UniformHandle;
77eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt    typedef GrGLProcessor::TransformedCoordsArray TransformedCoordsArray;
78eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt    typedef GrGLProcessor::TextureSamplerArray TextureSamplerArray;
79eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt
80374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com    /** Called when the program stage should insert its code into the shaders. The code in each
81374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        shader will be in its own block ({}) and so locally scoped names will not collide across
82374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        stages.
83374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com
84374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        @param builder      Interface used to emit code in the shaders.
8587f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        @param processor    The processor that generated this program stage.
86b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        @param key          The key that was computed by GenKey() from the generating GrProcessor.
87374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        @param outputColor  A predefined vec4 in the FS in which the stage should place its output
88374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            color (or coverage).
89374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com        @param inputColor   A vec4 that holds the input color to the stage in the FS. This may be
90374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            NULL in which case the implied input is solid white (all ones).
91374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            TODO: Better system for communicating optimization info (e.g. input
92374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                            color is solid white, trans black, known to be opaque, etc.) that allows
9387f48d997ec29e5eeaa7567355775e93465dd60djoshualitt                            the processor to communicate back similar known info about its output.
94b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        @param samplers     Contains one entry for each GrTextureAccess of the GrProcessor. These
95b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                            can be passed to the builder to emit texture reads in the generated
96b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                            code.
97a5305a110ab5201d5dadd40cbe711582d5ac4996joshualitt        TODO this should take a struct
98d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com        */
991598899975ecc85b003a59740b588d1ddbcedb09joshualitt    virtual void emitCode(GrGLFPBuilder* builder,
10087f48d997ec29e5eeaa7567355775e93465dd60djoshualitt                          const GrFragmentProcessor&,
101374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                          const char* outputColor,
102374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                          const char* inputColor,
10377af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com                          const TransformedCoordsArray& coords,
104374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com                          const TextureSamplerArray& samplers) = 0;
105374e75956e7a56bbbd2da5509f9c4117512515d2bsalomon@google.com
10687f48d997ec29e5eeaa7567355775e93465dd60djoshualitt    /** A GrGLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
10787f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        the same stage key; this function reads data from a GrFragmentProcessor and uploads any
10887f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        uniform variables required by the shaders created in emitCode(). The GrFragmentProcessor
10987f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        parameter is guaranteed to be of the same type that created this GrGLFragmentProcessor and
11087f48d997ec29e5eeaa7567355775e93465dd60djoshualitt        to have an identical processor key as the one that created this GrGLFragmentProcessor.  */
11187f48d997ec29e5eeaa7567355775e93465dd60djoshualitt    // TODO update this to pass in GrFragmentProcessor
11287f48d997ec29e5eeaa7567355775e93465dd60djoshualitt    virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) {}
11387f48d997ec29e5eeaa7567355775e93465dd60djoshualitt
114cfc18867d982119d9dc2888bf09f1093012daaddjvanverth    static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {}
115eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt
116261dc569b6a53729bea6e4e7a0cf2afa980eb82dcommit-bot@chromium.orgprivate:
117b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    typedef GrGLProcessor INHERITED;
118168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com};
119168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
120168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com#endif
121