egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/gl/GrGLUniformHandler.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #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" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 15 | |
| 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 Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 19 | bool 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 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 27 | GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray( |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 28 | const GrFragmentProcessor* owner, |
| 29 | uint32_t visibility, |
| 30 | GrSLType type, |
| 31 | const char* name, |
| 32 | bool mangleName, |
| 33 | int arrayCount, |
| 34 | const char** outName) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 35 | SkASSERT(name && strlen(name)); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 36 | SkASSERT(valid_name(name)); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 37 | SkASSERT(0 != visibility); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 38 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 39 | // 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. |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 45 | SkString resolvedName; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 46 | char prefix = 'u'; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 47 | if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 48 | prefix = '\0'; |
| 49 | } |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 50 | fProgramBuilder->nameVariable(&resolvedName, prefix, name, mangleName); |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 51 | GLUniformInfo& uni = fUniforms.push_back(GrGLProgramDataManager::GLUniformInfo{ |
| 52 | { |
| 53 | GrShaderVar{std::move(resolvedName), type, GrShaderVar::TypeModifier::Uniform, |
| 54 | arrayCount}, |
| 55 | visibility, owner, SkString(name) |
| 56 | }, |
| 57 | -1 |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 58 | }); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 59 | |
| 60 | if (outName) { |
| 61 | *outName = uni.fVariable.c_str(); |
| 62 | } |
| 63 | return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1); |
| 64 | } |
| 65 | |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 66 | GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler( |
| 67 | const GrBackendFormat& backendFormat, GrSamplerState, const GrSwizzle& swizzle, |
| 68 | const char* name, const GrShaderCaps* shaderCaps) { |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 69 | SkASSERT(name && strlen(name)); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 70 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 71 | SkString mangleName; |
| 72 | char prefix = 'u'; |
| 73 | fProgramBuilder->nameVariable(&mangleName, prefix, name, true); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 74 | |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 75 | GrTextureType type = backendFormat.textureType(); |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 76 | |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 77 | fSamplers.push_back(GrGLProgramDataManager::GLUniformInfo{ |
| 78 | { |
| 79 | GrShaderVar{std::move(mangleName), GrSLCombinedSamplerTypeForTextureType(type), |
| 80 | GrShaderVar::TypeModifier::Uniform}, |
| 81 | kFragment_GrShaderFlag, nullptr, SkString(name) |
| 82 | }, |
| 83 | -1 |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 84 | }); |
| 85 | |
Brian Salomon | 68ba117 | 2019-06-05 11:15:08 -0400 | [diff] [blame] | 86 | if (shaderCaps->textureSwizzleAppliedInShader()) { |
| 87 | fSamplerSwizzles.push_back(swizzle); |
| 88 | SkASSERT(fSamplers.count() == fSamplerSwizzles.count()); |
| 89 | } |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 90 | return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1); |
| 91 | } |
| 92 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 93 | void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 94 | for (const UniformInfo& uniform : fUniforms.items()) { |
| 95 | if (uniform.fVisibility & visibility) { |
| 96 | uniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 97 | out->append(";"); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 100 | for (const UniformInfo& sampler : fSamplers.items()) { |
| 101 | if (sampler.fVisibility & visibility) { |
| 102 | sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 103 | out->append(";\n"); |
| 104 | } |
| 105 | } |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) { |
| 109 | if (caps.bindUniformLocationSupport()) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 110 | int currUniform = 0; |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 111 | for (GLUniformInfo& uniform : fUniforms.items()) { |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 112 | GL_CALL(BindUniformLocation(programID, currUniform, uniform.fVariable.c_str())); |
| 113 | uniform.fLocation = currUniform; |
| 114 | ++currUniform; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 115 | } |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 116 | for (GLUniformInfo& sampler : fSamplers.items()) { |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 117 | GL_CALL(BindUniformLocation(programID, currUniform, sampler.fVariable.c_str())); |
| 118 | sampler.fLocation = currUniform; |
| 119 | ++currUniform; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 120 | } |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Brian Osman | 2c60a38 | 2019-06-26 15:19:30 -0400 | [diff] [blame] | 124 | void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) { |
| 125 | if (!caps.bindUniformLocationSupport() || force) { |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 126 | for (GLUniformInfo& uniform : fUniforms.items()) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 127 | GrGLint location; |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 128 | GL_CALL_RET(location, GetUniformLocation(programID, uniform.fVariable.c_str())); |
| 129 | uniform.fLocation = location; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 130 | } |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 131 | for (GLUniformInfo& sampler : fSamplers.items()) { |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 132 | GrGLint location; |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 133 | GL_CALL_RET(location, GetUniformLocation(programID, sampler.fVariable.c_str())); |
| 134 | sampler.fLocation = location; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 135 | } |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | const GrGLGpu* GrGLUniformHandler::glGpu() const { |
| 140 | GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder; |
| 141 | return glPB->gpu(); |
| 142 | } |