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( |
| 28 | uint32_t visibility, |
| 29 | GrSLType type, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 30 | const char* name, |
| 31 | bool mangleName, |
| 32 | int arrayCount, |
| 33 | const char** outName) { |
| 34 | SkASSERT(name && strlen(name)); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 35 | SkASSERT(valid_name(name)); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 36 | SkASSERT(0 != visibility); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 37 | |
| 38 | UniformInfo& uni = fUniforms.push_back(); |
| 39 | uni.fVariable.setType(type); |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 40 | uni.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 41 | // 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 Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 48 | 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] | 49 | prefix = '\0'; |
| 50 | } |
| 51 | fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName); |
| 52 | uni.fVariable.setArrayCount(arrayCount); |
| 53 | uni.fVisibility = visibility; |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 54 | uni.fLocation = -1; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 55 | |
| 56 | if (outName) { |
| 57 | *outName = uni.fVariable.c_str(); |
| 58 | } |
| 59 | return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1); |
| 60 | } |
| 61 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 62 | GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(const GrTexture* texture, |
| 63 | const GrSamplerState&, |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 64 | const GrSwizzle& swizzle, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 65 | const char* name, |
| 66 | const GrShaderCaps* shaderCaps) { |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 67 | SkASSERT(name && strlen(name)); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 68 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 69 | SkString mangleName; |
| 70 | char prefix = 'u'; |
| 71 | fProgramBuilder->nameVariable(&mangleName, prefix, name, true); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 72 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 73 | GrTextureType type = texture->texturePriv().textureType(); |
| 74 | |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 75 | UniformInfo& sampler = fSamplers.push_back(); |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 76 | sampler.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type)); |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 77 | sampler.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 78 | sampler.fVariable.setName(mangleName); |
| 79 | sampler.fLocation = -1; |
Greg Daniel | 0f70be8 | 2018-10-08 17:35:08 +0000 | [diff] [blame] | 80 | sampler.fVisibility = kFragment_GrShaderFlag; |
Brian Salomon | 68ba117 | 2019-06-05 11:15:08 -0400 | [diff] [blame] | 81 | if (shaderCaps->textureSwizzleAppliedInShader()) { |
| 82 | fSamplerSwizzles.push_back(swizzle); |
| 83 | SkASSERT(fSamplers.count() == fSamplerSwizzles.count()); |
| 84 | } |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 85 | return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1); |
| 86 | } |
| 87 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 88 | void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 89 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 90 | if (fUniforms[i].fVisibility & visibility) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 91 | fUniforms[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 92 | out->append(";"); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 95 | for (int i = 0; i < fSamplers.count(); ++i) { |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 96 | if (fSamplers[i].fVisibility & visibility) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 97 | fSamplers[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 98 | out->append(";\n"); |
| 99 | } |
| 100 | } |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) { |
| 104 | if (caps.bindUniformLocationSupport()) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 105 | int currUniform = 0; |
| 106 | for (int i = 0; i < fUniforms.count(); ++i, ++currUniform) { |
| 107 | GL_CALL(BindUniformLocation(programID, currUniform, fUniforms[i].fVariable.c_str())); |
| 108 | fUniforms[i].fLocation = currUniform; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 109 | } |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 110 | for (int i = 0; i < fSamplers.count(); ++i, ++currUniform) { |
| 111 | GL_CALL(BindUniformLocation(programID, currUniform, fSamplers[i].fVariable.c_str())); |
| 112 | fSamplers[i].fLocation = currUniform; |
| 113 | } |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Brian Osman | 2c60a38 | 2019-06-26 15:19:30 -0400 | [diff] [blame] | 117 | void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps, bool force) { |
| 118 | if (!caps.bindUniformLocationSupport() || force) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 119 | int count = fUniforms.count(); |
| 120 | for (int i = 0; i < count; ++i) { |
| 121 | GrGLint location; |
| 122 | GL_CALL_RET(location, GetUniformLocation(programID, fUniforms[i].fVariable.c_str())); |
| 123 | fUniforms[i].fLocation = location; |
| 124 | } |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 125 | for (int i = 0; i < fSamplers.count(); ++i) { |
| 126 | GrGLint location; |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 127 | GL_CALL_RET(location, GetUniformLocation(programID, fSamplers[i].fVariable.c_str())); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 128 | fSamplers[i].fLocation = location; |
| 129 | } |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | const GrGLGpu* GrGLUniformHandler::glGpu() const { |
| 134 | GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder; |
| 135 | return glPB->gpu(); |
| 136 | } |