blob: 4a3b56485d7019584907b5326cf8901c4d3d392e [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/gl/GrGLUniformHandler.h"
egdaniel7ea439b2015-12-03 09:20:44 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrTexturePriv.h"
11#include "src/gpu/gl/GrGLCaps.h"
12#include "src/gpu/gl/GrGLGpu.h"
13#include "src/gpu/gl/builders/GrGLProgramBuilder.h"
14#include "src/sksl/SkSLCompiler.h"
egdaniel7ea439b2015-12-03 09:20:44 -080015
16#define GL_CALL(X) GR_GL_CALL(this->glGpu()->glInterface(), X)
17#define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->glGpu()->glInterface(), R, X)
18
Robert Phillipsfe8da172018-01-24 14:52:02 +000019bool valid_name(const char* name) {
20 // disallow unknown names that start with "sk_"
21 if (!strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
22 return !strcmp(name, SkSL::Compiler::RTADJUST_NAME);
23 }
24 return true;
25}
26
egdaniel7ea439b2015-12-03 09:20:44 -080027GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray(
28 uint32_t visibility,
29 GrSLType type,
egdaniel7ea439b2015-12-03 09:20:44 -080030 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);
egdaniel7ea439b2015-12-03 09:20:44 -080037
egdaniel7ea439b2015-12-03 09:20:44 -080038 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
39 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
40 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
41 // the names will mismatch. I think the correct solution is to have all GPs which need the
42 // uniform view matrix, they should upload the view matrix in their setData along with regular
43 // uniforms.
Ben Wagnerdabd98c2020-03-25 15:17:18 -040044 SkString resolvedName;
egdaniel7ea439b2015-12-03 09:20:44 -080045 char prefix = 'u';
Robert Phillipsfe8da172018-01-24 14:52:02 +000046 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
egdaniel7ea439b2015-12-03 09:20:44 -080047 prefix = '\0';
48 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -040049 fProgramBuilder->nameVariable(&resolvedName, prefix, name, mangleName);
50
51 UniformInfo& uni = fUniforms.push_back(GrGLProgramDataManager::UniformInfo{
52 GrShaderVar{std::move(resolvedName), type, GrShaderVar::TypeModifier::Uniform, arrayCount},
53 visibility, -1
54 });
egdaniel7ea439b2015-12-03 09:20:44 -080055
56 if (outName) {
57 *outName = uni.fVariable.c_str();
58 }
59 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
60}
61
Chris Dalton1b1b0d52020-03-03 12:00:59 -070062GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(
63 const GrBackendFormat& backendFormat, GrSamplerState, const GrSwizzle& swizzle,
64 const char* name, const GrShaderCaps* shaderCaps) {
egdaniel09aa1fc2016-04-20 07:09:46 -070065 SkASSERT(name && strlen(name));
Brian Salomon101b8442016-11-18 11:58:54 -050066
egdaniel09aa1fc2016-04-20 07:09:46 -070067 SkString mangleName;
68 char prefix = 'u';
69 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -050070
Chris Dalton1b1b0d52020-03-03 12:00:59 -070071 GrTextureType type = backendFormat.textureType();
Greg Daniel9a51a862018-11-30 10:18:14 -050072
Ben Wagnerdabd98c2020-03-25 15:17:18 -040073 fSamplers.push_back(GrGLProgramDataManager::UniformInfo{
74 GrShaderVar{std::move(mangleName),
75 GrSLCombinedSamplerTypeForTextureType(type),
76 GrShaderVar::TypeModifier::Uniform},
77 kFragment_GrShaderFlag, -1
78 });
79
Brian Salomon68ba1172019-06-05 11:15:08 -040080 if (shaderCaps->textureSwizzleAppliedInShader()) {
81 fSamplerSwizzles.push_back(swizzle);
82 SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
83 }
egdaniel09aa1fc2016-04-20 07:09:46 -070084 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
85}
86
cdalton5e58cee2016-02-11 12:49:47 -080087void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
Michael Ludwig45191342020-03-24 12:29:39 -040088 for (const UniformInfo& uniform : fUniforms.items()) {
89 if (uniform.fVisibility & visibility) {
90 uniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
Brian Salomonf9f45122016-11-29 11:59:17 -050091 out->append(";");
egdaniel7ea439b2015-12-03 09:20:44 -080092 }
93 }
Michael Ludwig45191342020-03-24 12:29:39 -040094 for (const UniformInfo& sampler : fSamplers.items()) {
95 if (sampler.fVisibility & visibility) {
96 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -070097 out->append(";\n");
98 }
99 }
egdaniel7ea439b2015-12-03 09:20:44 -0800100}
101
102void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
103 if (caps.bindUniformLocationSupport()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500104 int currUniform = 0;
Michael Ludwig45191342020-03-24 12:29:39 -0400105 for (UniformInfo& uniform : fUniforms.items()) {
106 GL_CALL(BindUniformLocation(programID, currUniform, uniform.fVariable.c_str()));
107 uniform.fLocation = currUniform;
108 ++currUniform;
egdaniel7ea439b2015-12-03 09:20:44 -0800109 }
Michael Ludwig45191342020-03-24 12:29:39 -0400110 for (UniformInfo& sampler : fSamplers.items()) {
111 GL_CALL(BindUniformLocation(programID, currUniform, sampler.fVariable.c_str()));
112 sampler.fLocation = currUniform;
113 ++currUniform;
Brian Salomonf9f45122016-11-29 11:59:17 -0500114 }
egdaniel7ea439b2015-12-03 09:20:44 -0800115 }
116}
117
Brian Osman2c60a382019-06-26 15:19:30 -0400118void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) {
119 if (!caps.bindUniformLocationSupport() || force) {
Michael Ludwig45191342020-03-24 12:29:39 -0400120 for (UniformInfo& uniform : fUniforms.items()) {
egdaniel7ea439b2015-12-03 09:20:44 -0800121 GrGLint location;
Michael Ludwig45191342020-03-24 12:29:39 -0400122 GL_CALL_RET(location, GetUniformLocation(programID, uniform.fVariable.c_str()));
123 uniform.fLocation = location;
egdaniel7ea439b2015-12-03 09:20:44 -0800124 }
Michael Ludwig45191342020-03-24 12:29:39 -0400125 for (UniformInfo& sampler : fSamplers.items()) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700126 GrGLint location;
Michael Ludwig45191342020-03-24 12:29:39 -0400127 GL_CALL_RET(location, GetUniformLocation(programID, sampler.fVariable.c_str()));
128 sampler.fLocation = location;
egdaniel09aa1fc2016-04-20 07:09:46 -0700129 }
egdaniel7ea439b2015-12-03 09:20:44 -0800130 }
131}
132
133const GrGLGpu* GrGLUniformHandler::glGpu() const {
134 GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
135 return glPB->gpu();
136}