blob: f1543135165ec19162f28cb3e7b555efc1bd5908 [file] [log] [blame]
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
kkinnunen7510b222014-07-30 00:04:16 -07008#ifndef GrGLProgramDataManager_DEFINED
9#define GrGLProgramDataManager_DEFINED
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000010
11#include "gl/GrGLShaderVar.h"
12#include "gl/GrGLSL.h"
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000013#include "GrAllocator.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000014
15#include "SkTArray.h"
16
bsalomon861e1032014-12-16 07:33:49 -080017class GrGLGpu;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000018class SkMatrix;
kkinnunendddc18a2014-08-03 23:19:46 -070019class GrGLProgram;
joshualitt30ba4362014-08-21 20:18:45 -070020class GrGLProgramBuilder;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000021
kkinnunen7510b222014-07-30 00:04:16 -070022/** Manages the resources used by a shader program.
23 * The resources are objects the program uses to communicate with the
24 * application code.
25 */
26class GrGLProgramDataManager : public SkRefCnt {
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000027public:
28 // Opaque handle to a uniform
kkinnunenec56e452014-08-25 22:21:16 -070029 class ShaderResourceHandle {
30 public:
31 bool isValid() const { return -1 != fValue; }
32 ShaderResourceHandle()
33 : fValue(-1) {
34 }
35 protected:
36 ShaderResourceHandle(int value)
37 : fValue(value) {
38 SkASSERT(isValid());
39 }
40 int fValue;
41 };
42
43 class UniformHandle : public ShaderResourceHandle {
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000044 public:
kkinnunendddc18a2014-08-03 23:19:46 -070045 /** Creates a reference to an unifrom of a GrGLShaderBuilder.
46 * The ref can be used to set the uniform with corresponding the GrGLProgramDataManager.*/
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000047 static UniformHandle CreateFromUniformIndex(int i);
kkinnunenec56e452014-08-25 22:21:16 -070048 UniformHandle() { }
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000049 bool operator==(const UniformHandle& other) const { return other.fValue == fValue; }
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000050 private:
kkinnunenec56e452014-08-25 22:21:16 -070051 UniformHandle(int value) : ShaderResourceHandle(value) { }
kkinnunendddc18a2014-08-03 23:19:46 -070052 int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; }
53 int toShaderBuilderIndex() const { return toProgramDataIndex(); }
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000054
kkinnunendddc18a2014-08-03 23:19:46 -070055 friend class GrGLProgramDataManager; // For accessing toProgramDataIndex().
joshualitt30ba4362014-08-21 20:18:45 -070056 friend class GrGLProgramBuilder; // For accessing toShaderBuilderIndex().
joshualittabb52a12015-01-13 15:02:10 -080057 friend class GrGLGeometryProcessor;
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000058 };
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000059
joshualitt47bb3822014-10-07 16:43:25 -070060 struct UniformInfo {
61 GrGLShaderVar fVariable;
62 uint32_t fVisibility;
63 GrGLint fLocation;
kkinnunenec56e452014-08-25 22:21:16 -070064 };
65
joshualitt47bb3822014-10-07 16:43:25 -070066 // This uses an allocator rather than array so that the GrGLShaderVars don't move in memory
67 // after they are inserted. Users of GrGLShaderBuilder get refs to the vars and ptrs to their
68 // name strings. Otherwise, we'd have to hand out copies.
69 typedef GrTAllocator<UniformInfo> UniformInfoArray;
70
bsalomon861e1032014-12-16 07:33:49 -080071 GrGLProgramDataManager(GrGLGpu*, const UniformInfoArray&);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000072
73 /** Functions for uploading uniform values. The varities ending in v can be used to upload to an
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000074 * array of uniforms. arrayCount must be <= the array count of the uniform.
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000075 */
76 void setSampler(UniformHandle, GrGLint texUnit) const;
77 void set1f(UniformHandle, GrGLfloat v0) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000078 void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000079 void set2f(UniformHandle, GrGLfloat, GrGLfloat) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000080 void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000081 void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000082 void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000083 void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000084 void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000085 // matrices are column-major, the first three upload a single matrix, the latter three upload
86 // arrayCount matrices into a uniform array.
87 void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const;
88 void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000089 void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const;
90 void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000091
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000092 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform
93 void setSkMatrix(UniformHandle, const SkMatrix&) const;
94
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000095private:
96 enum {
97 kUnusedUniform = -1,
98 };
99
100 struct Uniform {
101 GrGLint fVSLocation;
102 GrGLint fFSLocation;
kkinnunendddc18a2014-08-03 23:19:46 -0700103 SkDEBUGCODE(
104 GrSLType fType;
105 int fArrayCount;
106 );
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000107 };
108
joshualitt7d022d62015-05-12 12:03:50 -0700109 SkDEBUGCODE(void printUnused(const Uniform&) const;)
joshualittaf2d56d2015-05-11 06:21:34 -0700110
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000111 SkTArray<Uniform, true> fUniforms;
bsalomon861e1032014-12-16 07:33:49 -0800112 GrGLGpu* fGpu;
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000113
114 typedef SkRefCnt INHERITED;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000115};
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000116#endif