blob: e9836af2b176d9401ed1815613a43cbaffab4432 [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
38 UniformInfo& uni = fUniforms.push_back();
39 uni.fVariable.setType(type);
Brian Salomon99938a82016-11-21 13:41:08 -050040 uni.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
egdaniel7ea439b2015-12-03 09:20:44 -080041 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
42 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
43 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
44 // the names will mismatch. I think the correct solution is to have all GPs which need the
45 // uniform view matrix, they should upload the view matrix in their setData along with regular
46 // uniforms.
47 char prefix = 'u';
Robert Phillipsfe8da172018-01-24 14:52:02 +000048 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
egdaniel7ea439b2015-12-03 09:20:44 -080049 prefix = '\0';
50 }
51 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
52 uni.fVariable.setArrayCount(arrayCount);
53 uni.fVisibility = visibility;
Brian Salomon101b8442016-11-18 11:58:54 -050054 uni.fLocation = -1;
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
Brian Salomon101b8442016-11-18 11:58:54 -050073 UniformInfo& sampler = fSamplers.push_back();
Brian Salomon60dd8c72018-07-30 10:24:13 -040074 sampler.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type));
Brian Salomon99938a82016-11-21 13:41:08 -050075 sampler.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
Brian Salomon101b8442016-11-18 11:58:54 -050076 sampler.fVariable.setName(mangleName);
77 sampler.fLocation = -1;
Greg Daniel0f70be82018-10-08 17:35:08 +000078 sampler.fVisibility = kFragment_GrShaderFlag;
Brian Salomon68ba1172019-06-05 11:15:08 -040079 if (shaderCaps->textureSwizzleAppliedInShader()) {
80 fSamplerSwizzles.push_back(swizzle);
81 SkASSERT(fSamplers.count() == fSamplerSwizzles.count());
82 }
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 {
Michael Ludwig45191342020-03-24 12:29:39 -040087 for (const UniformInfo& uniform : fUniforms.items()) {
88 if (uniform.fVisibility & visibility) {
89 uniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
Brian Salomonf9f45122016-11-29 11:59:17 -050090 out->append(";");
egdaniel7ea439b2015-12-03 09:20:44 -080091 }
92 }
Michael Ludwig45191342020-03-24 12:29:39 -040093 for (const UniformInfo& sampler : fSamplers.items()) {
94 if (sampler.fVisibility & visibility) {
95 sampler.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;
Michael Ludwig45191342020-03-24 12:29:39 -0400104 for (UniformInfo& uniform : fUniforms.items()) {
105 GL_CALL(BindUniformLocation(programID, currUniform, uniform.fVariable.c_str()));
106 uniform.fLocation = currUniform;
107 ++currUniform;
egdaniel7ea439b2015-12-03 09:20:44 -0800108 }
Michael Ludwig45191342020-03-24 12:29:39 -0400109 for (UniformInfo& sampler : fSamplers.items()) {
110 GL_CALL(BindUniformLocation(programID, currUniform, sampler.fVariable.c_str()));
111 sampler.fLocation = currUniform;
112 ++currUniform;
Brian Salomonf9f45122016-11-29 11:59:17 -0500113 }
egdaniel7ea439b2015-12-03 09:20:44 -0800114 }
115}
116
Brian Osman2c60a382019-06-26 15:19:30 -0400117void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) {
118 if (!caps.bindUniformLocationSupport() || force) {
Michael Ludwig45191342020-03-24 12:29:39 -0400119 for (UniformInfo& uniform : fUniforms.items()) {
egdaniel7ea439b2015-12-03 09:20:44 -0800120 GrGLint location;
Michael Ludwig45191342020-03-24 12:29:39 -0400121 GL_CALL_RET(location, GetUniformLocation(programID, uniform.fVariable.c_str()));
122 uniform.fLocation = location;
egdaniel7ea439b2015-12-03 09:20:44 -0800123 }
Michael Ludwig45191342020-03-24 12:29:39 -0400124 for (UniformInfo& sampler : fSamplers.items()) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700125 GrGLint location;
Michael Ludwig45191342020-03-24 12:29:39 -0400126 GL_CALL_RET(location, GetUniformLocation(programID, sampler.fVariable.c_str()));
127 sampler.fLocation = location;
egdaniel09aa1fc2016-04-20 07:09:46 -0700128 }
egdaniel7ea439b2015-12-03 09:20:44 -0800129 }
130}
131
132const GrGLGpu* GrGLUniformHandler::glGpu() const {
133 GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder;
134 return glPB->gpu();
135}