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 8 #include "gl/GrGLUniformHandler.h" 9 10 #include "GrTexturePriv.h" 11 #include "gl/GrGLCaps.h" 12 #include "gl/GrGLGpu.h" 13 #include "gl/builders/GrGLProgramBuilder.h" 14 #include "SkSLCompiler.h" 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 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 27 GrGLSLUniformHandler::UniformHandle GrGLUniformHandler::internalAddUniformArray( 28 uint32_t visibility, 29 GrSLType type, 30 GrSLPrecision precision, 31 const char* name, 32 bool mangleName, 33 int arrayCount, 34 const char** outName) { 35 SkASSERT(name && strlen(name)); 36 SkASSERT(valid_name(name)); 37 SkASSERT(0 != visibility); 38 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeTemporarilyAcceptsPrecision(type)); 39 40 UniformInfo& uni = fUniforms.push_back(); 41 uni.fVariable.setType(type); 42 uni.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier); 43 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use 44 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB 45 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then 46 // the names will mismatch. I think the correct solution is to have all GPs which need the 47 // uniform view matrix, they should upload the view matrix in their setData along with regular 48 // uniforms. 49 char prefix = 'u'; 50 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) { 51 prefix = '\0'; 52 } 53 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName); 54 uni.fVariable.setArrayCount(arrayCount); 55 uni.fVisibility = visibility; 56 uni.fVariable.setPrecision(precision); 57 uni.fLocation = -1; 58 59 if (outName) { 60 *outName = uni.fVariable.c_str(); 61 } 62 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1); 63 } 64 65 GrGLSLUniformHandler::SamplerHandle GrGLUniformHandler::addSampler(const GrTexture* texture, 66 const GrSamplerState&, 67 const char* name, 68 const GrShaderCaps* shaderCaps) { 69 SkASSERT(name && strlen(name)); 70 71 SkString mangleName; 72 char prefix = 'u'; 73 fProgramBuilder->nameVariable(&mangleName, prefix, name, true); 74 75 GrSLPrecision precision = GrSLSamplerPrecision(texture->config()); 76 GrSwizzle swizzle = shaderCaps->configTextureSwizzle(texture->config()); 77 GrTextureType type = texture->texturePriv().textureType(); 78 79 UniformInfo& sampler = fSamplers.push_back(); 80 sampler.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type)); 81 sampler.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier); 82 sampler.fVariable.setPrecision(precision); 83 sampler.fVariable.setName(mangleName); 84 sampler.fLocation = -1; 85 sampler.fVisibility = kFragment_GrShaderFlag; 86 fSamplerSwizzles.push_back(swizzle); 87 SkASSERT(fSamplers.count() == fSamplerSwizzles.count()); 88 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1); 89 } 90 91 void GrGLUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { 92 for (int i = 0; i < fUniforms.count(); ++i) { 93 if (fUniforms[i].fVisibility & visibility) { 94 fUniforms[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out); 95 out->append(";"); 96 } 97 } 98 for (int i = 0; i < fSamplers.count(); ++i) { 99 if (fSamplers[i].fVisibility & visibility) { 100 fSamplers[i].fVariable.appendDecl(fProgramBuilder->shaderCaps(), out); 101 out->append(";\n"); 102 } 103 } 104 } 105 106 void GrGLUniformHandler::bindUniformLocations(GrGLuint programID, const GrGLCaps& caps) { 107 if (caps.bindUniformLocationSupport()) { 108 int currUniform = 0; 109 for (int i = 0; i < fUniforms.count(); ++i, ++currUniform) { 110 GL_CALL(BindUniformLocation(programID, currUniform, fUniforms[i].fVariable.c_str())); 111 fUniforms[i].fLocation = currUniform; 112 } 113 for (int i = 0; i < fSamplers.count(); ++i, ++currUniform) { 114 GL_CALL(BindUniformLocation(programID, currUniform, fSamplers[i].fVariable.c_str())); 115 fSamplers[i].fLocation = currUniform; 116 } 117 } 118 } 119 120 void GrGLUniformHandler::getUniformLocations(GrGLuint programID, const GrGLCaps& caps) { 121 if (!caps.bindUniformLocationSupport()) { 122 int count = fUniforms.count(); 123 for (int i = 0; i < count; ++i) { 124 GrGLint location; 125 GL_CALL_RET(location, GetUniformLocation(programID, fUniforms[i].fVariable.c_str())); 126 fUniforms[i].fLocation = location; 127 } 128 for (int i = 0; i < fSamplers.count(); ++i) { 129 GrGLint location; 130 GL_CALL_RET(location, GetUniformLocation(programID, fSamplers[i].fVariable.c_str())); 131 fSamplers[i].fLocation = location; 132 } 133 } 134 } 135 136 const GrGLGpu* GrGLUniformHandler::glGpu() const { 137 GrGLProgramBuilder* glPB = (GrGLProgramBuilder*) fProgramBuilder; 138 return glPB->gpu(); 139 } 140