blob: 7551e9a039b5d5285bd8a9cb3ee81e083350220c [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"
Robert Phillipsfe8da172018-01-24 14:52:02 +000013#include "SkSLCompiler.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014
15#define GL_CALL(X) GR_GL_CALL(this->glGpu()->glInterface(), X)
16#define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->glGpu()->glInterface(), R, X)
17
Robert Phillipsfe8da172018-01-24 14:52:02 +000018bool valid_name(const char* name) {
19 // disallow unknown names that start with "sk_"
20 if (!strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
21 return !strcmp(name, SkSL::Compiler::RTADJUST_NAME);
22 }
23 return true;
24}
25
egdaniel7ea439b2015-12-03 09:20:44 -080026GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray(
27 uint32_t visibility,
28 GrSLType type,
29 GrSLPrecision precision,
30 const char* name,
31 bool mangleName,
32 int arrayCount,
33 const char** outName) {
34 SkASSERT(name && strlen(name));
Robert Phillipsfe8da172018-01-24 14:52:02 +000035 SkASSERT(valid_name(name));
egdaniel7ea439b2015-12-03 09:20:44 -080036 SkASSERT(0 != visibility);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040037 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeTemporarilyAcceptsPrecision(type));
egdaniel7ea439b2015-12-03 09:20:44 -080038
39 UniformInfo& uni = fUniforms.push_back();
40 uni.fVariable.setType(type);
Brian Salomon99938a82016-11-21 13:41:08 -050041 uni.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
egdaniel7ea439b2015-12-03 09:20:44 -080042 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
43 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
44 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
45 // the names will mismatch. I think the correct solution is to have all GPs which need the
46 // uniform view matrix, they should upload the view matrix in their setData along with regular
47 // uniforms.
48 char prefix = 'u';
Robert Phillipsfe8da172018-01-24 14:52:02 +000049 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
egdaniel7ea439b2015-12-03 09:20:44 -080050 prefix = '\0';
51 }
52 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
53 uni.fVariable.setArrayCount(arrayCount);
54 uni.fVisibility = visibility;
55 uni.fVariable.setPrecision(precision);
Brian Salomon101b8442016-11-18 11:58:54 -050056 uni.fLocation = -1;
egdaniel7ea439b2015-12-03 09:20:44 -080057
58 if (outName) {
59 *outName = uni.fVariable.c_str();
60 }
61 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
62}
63
Greg Daniel0f70be82018-10-08 17:35:08 +000064GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(GrSwizzle swizzle,
Brian Salomon60dd8c72018-07-30 10:24:13 -040065 GrTextureType type,
Brian Salomon101b8442016-11-18 11:58:54 -050066 GrSLPrecision precision,
67 const char* name) {
egdaniel09aa1fc2016-04-20 07:09:46 -070068 SkASSERT(name && strlen(name));
Brian Salomon101b8442016-11-18 11:58:54 -050069
egdaniel09aa1fc2016-04-20 07:09:46 -070070 SkString mangleName;
71 char prefix = 'u';
72 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -050073
74 UniformInfo& sampler = fSamplers.push_back();
Brian Salomon60dd8c72018-07-30 10:24:13 -040075 sampler.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type));
Brian Salomon99938a82016-11-21 13:41:08 -050076 sampler.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
Brian Salomon101b8442016-11-18 11:58:54 -050077 sampler.fVariable.setPrecision(precision);
78 sampler.fVariable.setName(mangleName);
79 sampler.fLocation = -1;
Greg Daniel0f70be82018-10-08 17:35:08 +000080 sampler.fVisibility = kFragment_GrShaderFlag;
Brian Salomon101b8442016-11-18 11:58:54 -050081 fSamplerSwizzles.push_back(swizzle);
82 SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
egdaniel09aa1fc2016-04-20 07:09:46 -070083 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
84}
85
cdalton5e58cee2016-02-11 12:49:47 -080086void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -080087 for (int i = 0; i < fUniforms.count(); ++i) {
88 if (fUniforms[i].fVisibility & visibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -050089 fUniforms[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
Brian Salomonf9f45122016-11-29 11:59:17 -050090 out->append(";");
egdaniel7ea439b2015-12-03 09:20:44 -080091 }
92 }
egdaniel09aa1fc2016-04-20 07:09:46 -070093 for (int i = 0; i < fSamplers.count(); ++i) {
Brian Salomon101b8442016-11-18 11:58:54 -050094 if (fSamplers[i].fVisibility & visibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -050095 fSamplers[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -070096 out->append(";\n");
97 }
98 }
egdaniel7ea439b2015-12-03 09:20:44 -080099}
100
101void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
102 if (caps.bindUniformLocationSupport()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500103 int currUniform = 0;
104 for (int i = 0; i < fUniforms.count(); ++i, ++currUniform) {
105 GL_CALL(BindUniformLocation(programID, currUniform, fUniforms[i].fVariable.c_str()));
106 fUniforms[i].fLocation = currUniform;
egdaniel7ea439b2015-12-03 09:20:44 -0800107 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500108 for (int i = 0; i < fSamplers.count(); ++i, ++currUniform) {
109 GL_CALL(BindUniformLocation(programID, currUniform, fSamplers[i].fVariable.c_str()));
110 fSamplers[i].fLocation = currUniform;
111 }
egdaniel7ea439b2015-12-03 09:20:44 -0800112 }
113}
114
115void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
116 if (!caps.bindUniformLocationSupport()) {
117 int count = fUniforms.count();
118 for (int i = 0; i < count; ++i) {
119 GrGLint location;
120 GL_CALL_RET(location, GetUniformLocation(programID, fUniforms[i].fVariable.c_str()));
121 fUniforms[i].fLocation = location;
122 }
egdaniel09aa1fc2016-04-20 07:09:46 -0700123 for (int i = 0; i < fSamplers.count(); ++i) {
124 GrGLint location;
Brian Salomon101b8442016-11-18 11:58:54 -0500125 GL_CALL_RET(location, GetUniformLocation(programID, fSamplers[i].fVariable.c_str()));
egdaniel09aa1fc2016-04-20 07:09:46 -0700126 fSamplers[i].fLocation = location;
127 }
egdaniel7ea439b2015-12-03 09:20:44 -0800128 }
129}
130
131const GrGLGpu* GrGLUniformHandler::glGpu() const {
132 GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
133 return glPB->gpu();
134}