gfx/skia/trunk/src/gpu/gl/GrGLUniformManager.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/gpu/gl/GrGLUniformManager.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +/*
     1.5 + * Copyright 2012 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef GrGLUniformManager_DEFINED
    1.12 +#define GrGLUniformManager_DEFINED
    1.13 +
    1.14 +#include "gl/GrGLShaderVar.h"
    1.15 +#include "gl/GrGLSL.h"
    1.16 +#include "GrAllocator.h"
    1.17 +
    1.18 +#include "SkTArray.h"
    1.19 +
    1.20 +class GrGpuGL;
    1.21 +class SkMatrix;
    1.22 +
    1.23 +/** Manages a program's uniforms.
    1.24 +*/
    1.25 +class GrGLUniformManager {
    1.26 +public:
    1.27 +    // Opaque handle to a uniform
    1.28 +    class UniformHandle {
    1.29 +    public:
    1.30 +        static UniformHandle CreateFromUniformIndex(int i);
    1.31 +
    1.32 +        bool isValid() const { return 0 != fValue; }
    1.33 +
    1.34 +        bool operator==(const UniformHandle& other) const { return other.fValue == fValue; }
    1.35 +
    1.36 +        UniformHandle()
    1.37 +            : fValue(0) {
    1.38 +        }
    1.39 +
    1.40 +    private:
    1.41 +        UniformHandle(int value)
    1.42 +            : fValue(~value) {
    1.43 +            SkASSERT(isValid());
    1.44 +        }
    1.45 +
    1.46 +        int toUniformIndex() const { SkASSERT(isValid()); return ~fValue; }
    1.47 +
    1.48 +        int fValue;
    1.49 +        friend class GrGLUniformManager; // For accessing toUniformIndex().
    1.50 +    };
    1.51 +
    1.52 +    GrGLUniformManager(GrGpuGL* gpu);
    1.53 +
    1.54 +    UniformHandle appendUniform(GrSLType type, int arrayCount = GrGLShaderVar::kNonArray);
    1.55 +
    1.56 +    /** Functions for uploading uniform values. The varities ending in v can be used to upload to an
    1.57 +     *  array of uniforms. arrayCount must be <= the array count of the uniform.
    1.58 +     */
    1.59 +    void setSampler(UniformHandle, GrGLint texUnit) const;
    1.60 +    void set1f(UniformHandle, GrGLfloat v0) const;
    1.61 +    void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
    1.62 +    void set2f(UniformHandle, GrGLfloat, GrGLfloat) const;
    1.63 +    void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
    1.64 +    void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const;
    1.65 +    void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
    1.66 +    void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const;
    1.67 +    void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
    1.68 +    // matrices are column-major, the first three upload a single matrix, the latter three upload
    1.69 +    // arrayCount matrices into a uniform array.
    1.70 +    void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const;
    1.71 +    void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const;
    1.72 +    void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const;
    1.73 +    void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const;
    1.74 +
    1.75 +    // convenience method for uploading a SkMatrix to a 3x3 matrix uniform
    1.76 +    void setSkMatrix(UniformHandle, const SkMatrix&) const;
    1.77 +
    1.78 +    struct BuilderUniform {
    1.79 +        GrGLShaderVar fVariable;
    1.80 +        uint32_t      fVisibility;
    1.81 +    };
    1.82 +    // This uses an allocator rather than array so that the GrGLShaderVars don't move in memory
    1.83 +    // after they are inserted. Users of GrGLShaderBuilder get refs to the vars and ptrs to their
    1.84 +    // name strings. Otherwise, we'd have to hand out copies.
    1.85 +    typedef GrTAllocator<BuilderUniform> BuilderUniformArray;
    1.86 +
    1.87 +    /**
    1.88 +     * Called by the GrGLShaderBuilder to know if the manager is using
    1.89 +     * BindUniformLocation. In that case getUniformLocations must be called
    1.90 +     * before the program is linked.
    1.91 +     */
    1.92 +    bool isUsingBindUniform() const { return fUsingBindUniform; }
    1.93 +
    1.94 +    /**
    1.95 +     * Called by the GrGLShaderBuilder to get GL locations for all uniforms.
    1.96 +     */
    1.97 +    void getUniformLocations(GrGLuint programID, const BuilderUniformArray& uniforms);
    1.98 +
    1.99 +    /**
   1.100 +     * Called by the GrGLShaderBuilder to access the array by the handle (index).
   1.101 +     */
   1.102 +    const BuilderUniform& getBuilderUniform(const BuilderUniformArray&, GrGLUniformManager::UniformHandle) const;
   1.103 +
   1.104 +private:
   1.105 +    enum {
   1.106 +        kUnusedUniform = -1,
   1.107 +    };
   1.108 +
   1.109 +    struct Uniform {
   1.110 +        GrGLint     fVSLocation;
   1.111 +        GrGLint     fFSLocation;
   1.112 +        GrSLType    fType;
   1.113 +        int         fArrayCount;
   1.114 +    };
   1.115 +
   1.116 +    bool fUsingBindUniform;
   1.117 +    SkTArray<Uniform, true> fUniforms;
   1.118 +    GrGpuGL* fGpu;
   1.119 +};
   1.120 +
   1.121 +#endif

mercurial