blob: 49865d42c42c745e3c0173625dcc32cab792857c [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
Brian Salomon4cfae3b2020-07-23 10:33:24 -040010#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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(
Ethan Nicholas16464c32020-04-06 13:53:05 -040028 const GrFragmentProcessor* owner,
29 uint32_t visibility,
30 GrSLType type,
31 const char* name,
32 bool mangleName,
33 int arrayCount,
34 const char** outName) {
egdaniel7ea439b2015-12-03 09:20:44 -080035 SkASSERT(name && strlen(name));
Robert Phillipsfe8da172018-01-24 14:52:02 +000036 SkASSERT(valid_name(name));
egdaniel7ea439b2015-12-03 09:20:44 -080037 SkASSERT(0 != visibility);
egdaniel7ea439b2015-12-03 09:20:44 -080038
egdaniel7ea439b2015-12-03 09:20:44 -080039 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
40 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
41 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
42 // the names will mismatch. I think the correct solution is to have all GPs which need the
43 // uniform view matrix, they should upload the view matrix in their setData along with regular
44 // uniforms.
45 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 }
John Stilesaaea0d92020-10-26 13:09:37 -040049 SkString resolvedName = fProgramBuilder->nameVariable(prefix, name, mangleName);
Ethan Nicholas16464c32020-04-06 13:53:05 -040050 GLUniformInfo& uni = fUniforms.push_back(GrGLProgramDataManager::GLUniformInfo{
51 {
52 GrShaderVar{std::move(resolvedName), type, GrShaderVar::TypeModifier::Uniform,
53 arrayCount},
54 visibility, owner, SkString(name)
55 },
56 -1
Ben Wagnerdabd98c2020-03-25 15:17:18 -040057 });
egdaniel7ea439b2015-12-03 09:20:44 -080058
59 if (outName) {
60 *outName = uni.fVariable.c_str();
61 }
62 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
63}
64
Chris Dalton1b1b0d52020-03-03 12:00:59 -070065GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(
66 const GrBackendFormat& backendFormat, GrSamplerState, const GrSwizzle& swizzle,
67 const char* name, const GrShaderCaps* shaderCaps) {
egdaniel09aa1fc2016-04-20 07:09:46 -070068 SkASSERT(name && strlen(name));
Brian Salomon101b8442016-11-18 11:58:54 -050069
John Stilesaaea0d92020-10-26 13:09:37 -040070 constexpr char prefix = 'u';
71 SkString mangleName = fProgramBuilder->nameVariable(prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -050072
Chris Dalton1b1b0d52020-03-03 12:00:59 -070073 GrTextureType type = backendFormat.textureType();
Greg Daniel9a51a862018-11-30 10:18:14 -050074
Ethan Nicholas16464c32020-04-06 13:53:05 -040075 fSamplers.push_back(GrGLProgramDataManager::GLUniformInfo{
76 {
77 GrShaderVar{std::move(mangleName), GrSLCombinedSamplerTypeForTextureType(type),
78 GrShaderVar::TypeModifier::Uniform},
79 kFragment_GrShaderFlag, nullptr, SkString(name)
80 },
81 -1
Ben Wagnerdabd98c2020-03-25 15:17:18 -040082 });
83
Brian Salomon280fa3d2020-08-27 17:16:49 -040084 fSamplerSwizzles.push_back(swizzle);
85 SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
egdaniel09aa1fc2016-04-20 07:09:46 -070086 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
87}
88
cdalton5e58cee2016-02-11 12:49:47 -080089void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
Michael Ludwig45191342020-03-24 12:29:39 -040090 for (const UniformInfo& uniform : fUniforms.items()) {
91 if (uniform.fVisibility & visibility) {
92 uniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
Brian Salomonf9f45122016-11-29 11:59:17 -050093 out->append(";");
egdaniel7ea439b2015-12-03 09:20:44 -080094 }
95 }
Michael Ludwig45191342020-03-24 12:29:39 -040096 for (const UniformInfo& sampler : fSamplers.items()) {
97 if (sampler.fVisibility & visibility) {
98 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -070099 out->append(";\n");
100 }
101 }
egdaniel7ea439b2015-12-03 09:20:44 -0800102}
103
104void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) {
105 if (caps.bindUniformLocationSupport()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500106 int currUniform = 0;
Ethan Nicholas16464c32020-04-06 13:53:05 -0400107 for (GLUniformInfo& uniform : fUniforms.items()) {
Michael Ludwig45191342020-03-24 12:29:39 -0400108 GL_CALL(BindUniformLocation(programID, currUniform, uniform.fVariable.c_str()));
109 uniform.fLocation = currUniform;
110 ++currUniform;
egdaniel7ea439b2015-12-03 09:20:44 -0800111 }
Ethan Nicholas16464c32020-04-06 13:53:05 -0400112 for (GLUniformInfo& sampler : fSamplers.items()) {
Michael Ludwig45191342020-03-24 12:29:39 -0400113 GL_CALL(BindUniformLocation(programID, currUniform, sampler.fVariable.c_str()));
114 sampler.fLocation = currUniform;
115 ++currUniform;
Brian Salomonf9f45122016-11-29 11:59:17 -0500116 }
egdaniel7ea439b2015-12-03 09:20:44 -0800117 }
118}
119
Brian Osman2c60a382019-06-26 15:19:30 -0400120void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) {
121 if (!caps.bindUniformLocationSupport() || force) {
Ethan Nicholas16464c32020-04-06 13:53:05 -0400122 for (GLUniformInfo& uniform : fUniforms.items()) {
egdaniel7ea439b2015-12-03 09:20:44 -0800123 GrGLint location;
Michael Ludwig45191342020-03-24 12:29:39 -0400124 GL_CALL_RET(location, GetUniformLocation(programID, uniform.fVariable.c_str()));
125 uniform.fLocation = location;
egdaniel7ea439b2015-12-03 09:20:44 -0800126 }
Ethan Nicholas16464c32020-04-06 13:53:05 -0400127 for (GLUniformInfo& sampler : fSamplers.items()) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700128 GrGLint location;
Michael Ludwig45191342020-03-24 12:29:39 -0400129 GL_CALL_RET(location, GetUniformLocation(programID, sampler.fVariable.c_str()));
130 sampler.fLocation = location;
egdaniel09aa1fc2016-04-20 07:09:46 -0700131 }
egdaniel7ea439b2015-12-03 09:20:44 -0800132 }
133}
134
135const GrGLGpu* GrGLUniformHandler::glGpu() const {
136 GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
137 return glPB->gpu();
138}