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