blob: 914f8bc6c53198005bdba4b7f722e459640f121f [file] [log] [blame]
egdaniel7ea439b2015-12-03 09:20:44 -08001/*
2 * Copyright 2015 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
8#include "gl/GrGLUniformHandler.h"
9
10#include "gl/GrGLCaps.h"
11#include "gl/GrGLGpu.h"
12#include "gl/builders/GrGLProgramBuilder.h"
13
14#define GL_CALL(X) GR_GL_CALL(this->glGpu()->glInterface(), X)
15#define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->glGpu()->glInterface(), R, X)
16
17GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray(
18 uint32_t visibility,
19 GrSLType type,
20 GrSLPrecision precision,
21 const char* name,
22 bool mangleName,
23 int arrayCount,
24 const char** outName) {
25 SkASSERT(name && strlen(name));
egdaniel7ea439b2015-12-03 09:20:44 -080026 SkASSERT(0 != visibility);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040027 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeTemporarilyAcceptsPrecision(type));
egdaniel7ea439b2015-12-03 09:20:44 -080028
29 UniformInfo& uni = fUniforms.push_back();
30 uni.fVariable.setType(type);
Brian Salomon99938a82016-11-21 13:41:08 -050031 uni.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
egdaniel7ea439b2015-12-03 09:20:44 -080032 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
33 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
34 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
35 // the names will mismatch. I think the correct solution is to have all GPs which need the
36 // uniform view matrix, they should upload the view matrix in their setData along with regular
37 // uniforms.
38 char prefix = 'u';
39 if ('u' == name[0]) {
40 prefix = '\0';
41 }
42 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
43 uni.fVariable.setArrayCount(arrayCount);
44 uni.fVisibility = visibility;
45 uni.fVariable.setPrecision(precision);
Brian Salomon101b8442016-11-18 11:58:54 -050046 uni.fLocation = -1;
egdaniel7ea439b2015-12-03 09:20:44 -080047
48 if (outName) {
49 *outName = uni.fVariable.c_str();
50 }
51 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
52}
53
Brian Salomon101b8442016-11-18 11:58:54 -050054GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(uint32_t visibility,
55 GrSwizzle swizzle,
56 GrSLType type,
57 GrSLPrecision precision,
58 const char* name) {
egdaniel09aa1fc2016-04-20 07:09:46 -070059 SkASSERT(name && strlen(name));
egdaniel09aa1fc2016-04-20 07:09:46 -070060 SkASSERT(0 != visibility);
Brian Salomon101b8442016-11-18 11:58:54 -050061
egdaniel09aa1fc2016-04-20 07:09:46 -070062 SkString mangleName;
63 char prefix = 'u';
64 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -050065
66 UniformInfo& sampler = fSamplers.push_back();
67 SkASSERT(GrSLTypeIsCombinedSamplerType(type));
68 sampler.fVariable.setType(type);
Brian Salomon99938a82016-11-21 13:41:08 -050069 sampler.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
Brian Salomon101b8442016-11-18 11:58:54 -050070 sampler.fVariable.setPrecision(precision);
71 sampler.fVariable.setName(mangleName);
72 sampler.fLocation = -1;
73 sampler.fVisibility = visibility;
74 fSamplerSwizzles.push_back(swizzle);
75 SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
egdaniel09aa1fc2016-04-20 07:09:46 -070076 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
77}
78
Greg Danielbc5d4d72017-05-05 10:28:42 -040079GrGLSLUniformHandler::TexelBufferHandle GrGLUniformHandler::addTexelBuffer(uint32_t visibility,
80 GrSLPrecision precision,
81 const char* name) {
82 SkASSERT(name && strlen(name));
83 SkASSERT(0 != visibility);
84
85 SkString mangleName;
86 char prefix = 'u';
87 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
88
89 UniformInfo& texelBuffer = fTexelBuffers.push_back();
90 texelBuffer.fVariable.setType(kBufferSampler_GrSLType);
91 texelBuffer.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
92 texelBuffer.fVariable.setPrecision(precision);
93 texelBuffer.fVariable.setName(mangleName);
94 texelBuffer.fLocation = -1;
95 texelBuffer.fVisibility = visibility;
96 return GrGLSLUniformHandler::TexelBufferHandle(fTexelBuffers.count() - 1);
97}
98
Brian Salomonf9f45122016-11-29 11:59:17 -050099GrGLSLUniformHandler::ImageStorageHandle GrGLUniformHandler::addImageStorage(
100 uint32_t visibility, GrSLType type, GrImageStorageFormat format, GrSLMemoryModel model,
101 GrSLRestrict restrict, GrIOType ioType, const char* name) {
102 SkASSERT(name && strlen(name));
Brian Salomonf9f45122016-11-29 11:59:17 -0500103 SkASSERT(0 != visibility);
104 SkString mangleName;
105 char prefix = 'u';
106 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
107
108 UniformInfo& imageStorage = fImageStorages.push_back();
109 imageStorage.fVariable.setName(mangleName);
110
111 SkASSERT(GrSLTypeIsImageStorage(type));
112 imageStorage.fVariable.setType(type);
113 imageStorage.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
114 imageStorage.fVariable.setImageStorageFormat(format);
115 imageStorage.fVariable.setMemoryModel(model);
116 imageStorage.fVariable.setRestrict(restrict);
117 imageStorage.fVariable.setIOType(ioType);
118 imageStorage.fVariable.setPrecision(kHigh_GrSLPrecision);
119 imageStorage.fLocation = -1;
120 imageStorage.fVisibility = visibility;
121 return GrGLSLUniformHandler::ImageStorageHandle(fImageStorages.count() - 1);
122}
123
cdalton5e58cee2016-02-11 12:49:47 -0800124void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800125 for (int i = 0; i < fUniforms.count(); ++i) {
126 if (fUniforms[i].fVisibility & visibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500127 fUniforms[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
Brian Salomonf9f45122016-11-29 11:59:17 -0500128 out->append(";");
egdaniel7ea439b2015-12-03 09:20:44 -0800129 }
130 }
egdaniel09aa1fc2016-04-20 07:09:46 -0700131 for (int i = 0; i < fSamplers.count(); ++i) {
Brian Salomon101b8442016-11-18 11:58:54 -0500132 if (fSamplers[i].fVisibility & visibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500133 fSamplers[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -0700134 out->append(";\n");
135 }
136 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400137 for (int i = 0; i < fTexelBuffers.count(); ++i) {
138 if (fTexelBuffers[i].fVisibility & visibility) {
139 fTexelBuffers[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
140 out->append(";\n");
141 }
142 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500143 for (int i = 0; i < fImageStorages.count(); ++i) {
144 if (fImageStorages[i].fVisibility & visibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500145 fImageStorages[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
Brian Salomonf9f45122016-11-29 11:59:17 -0500146 out->append(";");
147 }
148 }
egdaniel7ea439b2015-12-03 09:20:44 -0800149}
150
151void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
152 if (caps.bindUniformLocationSupport()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500153 int currUniform = 0;
154 for (int i = 0; i < fUniforms.count(); ++i, ++currUniform) {
155 GL_CALL(BindUniformLocation(programID, currUniform, fUniforms[i].fVariable.c_str()));
156 fUniforms[i].fLocation = currUniform;
egdaniel7ea439b2015-12-03 09:20:44 -0800157 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500158 for (int i = 0; i < fSamplers.count(); ++i, ++currUniform) {
159 GL_CALL(BindUniformLocation(programID, currUniform, fSamplers[i].fVariable.c_str()));
160 fSamplers[i].fLocation = currUniform;
161 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400162 for (int i = 0; i < fTexelBuffers.count(); ++i, ++currUniform) {
163 GL_CALL(BindUniformLocation(programID, currUniform,
164 fTexelBuffers[i].fVariable.c_str()));
165 fTexelBuffers[i].fLocation = currUniform;
166 }
167 for (int i = 0; i < fImageStorages.count(); ++i, ++currUniform) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500168 GL_CALL(BindUniformLocation(programID, currUniform,
169 fImageStorages[i].fVariable.c_str()));
170 fImageStorages[i].fLocation = currUniform;
egdaniel09aa1fc2016-04-20 07:09:46 -0700171 }
egdaniel7ea439b2015-12-03 09:20:44 -0800172 }
173}
174
175void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
176 if (!caps.bindUniformLocationSupport()) {
177 int count = fUniforms.count();
178 for (int i = 0; i < count; ++i) {
179 GrGLint location;
180 GL_CALL_RET(location, GetUniformLocation(programID, fUniforms[i].fVariable.c_str()));
181 fUniforms[i].fLocation = location;
182 }
egdaniel09aa1fc2016-04-20 07:09:46 -0700183 for (int i = 0; i < fSamplers.count(); ++i) {
184 GrGLint location;
Brian Salomon101b8442016-11-18 11:58:54 -0500185 GL_CALL_RET(location, GetUniformLocation(programID, fSamplers[i].fVariable.c_str()));
egdaniel09aa1fc2016-04-20 07:09:46 -0700186 fSamplers[i].fLocation = location;
187 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400188 for (int i = 0; i < fTexelBuffers.count(); ++i) {
189 GrGLint location;
190 GL_CALL_RET(location, GetUniformLocation(programID,
191 fTexelBuffers[i].fVariable.c_str()));
192 fTexelBuffers[i].fLocation = location;
193 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500194 for (int i = 0; i < fImageStorages.count(); ++i) {
195 GrGLint location;
196 GL_CALL_RET(location, GetUniformLocation(programID,
197 fImageStorages[i].fVariable.c_str()));
198 fImageStorages[i].fLocation = location;
199 }
egdaniel7ea439b2015-12-03 09:20:44 -0800200 }
201}
202
203const GrGLGpu* GrGLUniformHandler::glGpu() const {
204 GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
205 return glPB->gpu();
206}