michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "gl/GrGLShaderBuilder.h" michael@0: #include "gl/GrGLProgram.h" michael@0: #include "gl/GrGLUniformHandle.h" michael@0: #include "gl/GrGpuGL.h" michael@0: #include "SkMatrix.h" michael@0: michael@0: #define ASSERT_ARRAY_UPLOAD_IN_BOUNDS(UNI, COUNT) \ michael@0: SkASSERT(arrayCount <= uni.fArrayCount || \ michael@0: (1 == arrayCount && GrGLShaderVar::kNonArray == uni.fArrayCount)) michael@0: michael@0: GrGLUniformManager::GrGLUniformManager(GrGpuGL* gpu) : fGpu(gpu) { michael@0: // skbug.com/2056 michael@0: fUsingBindUniform = fGpu->glInterface()->fFunctions.fBindUniformLocation != NULL; michael@0: } michael@0: michael@0: GrGLUniformManager::UniformHandle GrGLUniformManager::appendUniform(GrSLType type, int arrayCount) { michael@0: int idx = fUniforms.count(); michael@0: Uniform& uni = fUniforms.push_back(); michael@0: SkASSERT(GrGLShaderVar::kNonArray == arrayCount || arrayCount > 0); michael@0: uni.fArrayCount = arrayCount; michael@0: uni.fType = type; michael@0: uni.fVSLocation = kUnusedUniform; michael@0: uni.fFSLocation = kUnusedUniform; michael@0: return GrGLUniformManager::UniformHandle::CreateFromUniformIndex(idx); michael@0: } michael@0: michael@0: void GrGLUniformManager::setSampler(UniformHandle u, GrGLint texUnit) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kSampler2D_GrSLType); michael@0: SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount); michael@0: // FIXME: We still insert a single sampler uniform for every stage. If the shader does not michael@0: // reference the sampler then the compiler may have optimized it out. Uncomment this assert michael@0: // once stages insert their own samplers. michael@0: // SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fFSLocation, texUnit)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fVSLocation, texUnit)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::set1f(UniformHandle u, GrGLfloat v0) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kFloat_GrSLType); michael@0: SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount); michael@0: SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fFSLocation, v0)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fVSLocation, v0)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::set1fv(UniformHandle u, michael@0: int arrayCount, michael@0: const GrGLfloat v[]) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kFloat_GrSLType); michael@0: SkASSERT(arrayCount > 0); michael@0: ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); michael@0: // This assert fires in some instances of the two-pt gradient for its VSParams. michael@0: // Once the uniform manager is responsible for inserting the duplicate uniform michael@0: // arrays in VS and FS driver bug workaround, this can be enabled. michael@0: //SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fFSLocation, arrayCount, v)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fVSLocation, arrayCount, v)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::set2f(UniformHandle u, GrGLfloat v0, GrGLfloat v1) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kVec2f_GrSLType); michael@0: SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount); michael@0: SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fFSLocation, v0, v1)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fVSLocation, v0, v1)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::set2fv(UniformHandle u, michael@0: int arrayCount, michael@0: const GrGLfloat v[]) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kVec2f_GrSLType); michael@0: SkASSERT(arrayCount > 0); michael@0: ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); michael@0: SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fFSLocation, arrayCount, v)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fVSLocation, arrayCount, v)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::set3f(UniformHandle u, GrGLfloat v0, GrGLfloat v1, GrGLfloat v2) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kVec3f_GrSLType); michael@0: SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount); michael@0: SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fFSLocation, v0, v1, v2)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fVSLocation, v0, v1, v2)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::set3fv(UniformHandle u, michael@0: int arrayCount, michael@0: const GrGLfloat v[]) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kVec3f_GrSLType); michael@0: SkASSERT(arrayCount > 0); michael@0: ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); michael@0: SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fFSLocation, arrayCount, v)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fVSLocation, arrayCount, v)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::set4f(UniformHandle u, michael@0: GrGLfloat v0, michael@0: GrGLfloat v1, michael@0: GrGLfloat v2, michael@0: GrGLfloat v3) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kVec4f_GrSLType); michael@0: SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount); michael@0: SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fFSLocation, v0, v1, v2, v3)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fVSLocation, v0, v1, v2, v3)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::set4fv(UniformHandle u, michael@0: int arrayCount, michael@0: const GrGLfloat v[]) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kVec4f_GrSLType); michael@0: SkASSERT(arrayCount > 0); michael@0: ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); michael@0: SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fFSLocation, arrayCount, v)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fVSLocation, arrayCount, v)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::setMatrix3f(UniformHandle u, const GrGLfloat matrix[]) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kMat33f_GrSLType); michael@0: SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount); michael@0: // TODO: Re-enable this assert once texture matrices aren't forced on all effects michael@0: // SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), UniformMatrix3fv(uni.fFSLocation, 1, false, matrix)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), UniformMatrix3fv(uni.fVSLocation, 1, false, matrix)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::setMatrix4f(UniformHandle u, const GrGLfloat matrix[]) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kMat44f_GrSLType); michael@0: SkASSERT(GrGLShaderVar::kNonArray == uni.fArrayCount); michael@0: SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), UniformMatrix4fv(uni.fFSLocation, 1, false, matrix)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), UniformMatrix4fv(uni.fVSLocation, 1, false, matrix)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::setMatrix3fv(UniformHandle u, michael@0: int arrayCount, michael@0: const GrGLfloat matrices[]) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kMat33f_GrSLType); michael@0: SkASSERT(arrayCount > 0); michael@0: ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); michael@0: SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), michael@0: UniformMatrix3fv(uni.fFSLocation, arrayCount, false, matrices)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), michael@0: UniformMatrix3fv(uni.fVSLocation, arrayCount, false, matrices)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::setMatrix4fv(UniformHandle u, michael@0: int arrayCount, michael@0: const GrGLfloat matrices[]) const { michael@0: const Uniform& uni = fUniforms[u.toUniformIndex()]; michael@0: SkASSERT(uni.fType == kMat44f_GrSLType); michael@0: SkASSERT(arrayCount > 0); michael@0: ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); michael@0: SkASSERT(kUnusedUniform != uni.fFSLocation || kUnusedUniform != uni.fVSLocation); michael@0: if (kUnusedUniform != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), michael@0: UniformMatrix4fv(uni.fFSLocation, arrayCount, false, matrices)); michael@0: } michael@0: if (kUnusedUniform != uni.fVSLocation && uni.fVSLocation != uni.fFSLocation) { michael@0: GR_GL_CALL(fGpu->glInterface(), michael@0: UniformMatrix4fv(uni.fVSLocation, arrayCount, false, matrices)); michael@0: } michael@0: } michael@0: michael@0: void GrGLUniformManager::setSkMatrix(UniformHandle u, const SkMatrix& matrix) const { michael@0: GrGLfloat mt[] = { michael@0: matrix.get(SkMatrix::kMScaleX), michael@0: matrix.get(SkMatrix::kMSkewY), michael@0: matrix.get(SkMatrix::kMPersp0), michael@0: matrix.get(SkMatrix::kMSkewX), michael@0: matrix.get(SkMatrix::kMScaleY), michael@0: matrix.get(SkMatrix::kMPersp1), michael@0: matrix.get(SkMatrix::kMTransX), michael@0: matrix.get(SkMatrix::kMTransY), michael@0: matrix.get(SkMatrix::kMPersp2), michael@0: }; michael@0: this->setMatrix3f(u, mt); michael@0: } michael@0: michael@0: michael@0: void GrGLUniformManager::getUniformLocations(GrGLuint programID, const BuilderUniformArray& uniforms) { michael@0: SkASSERT(uniforms.count() == fUniforms.count()); michael@0: int count = fUniforms.count(); michael@0: for (int i = 0; i < count; ++i) { michael@0: SkASSERT(uniforms[i].fVariable.getType() == fUniforms[i].fType); michael@0: SkASSERT(uniforms[i].fVariable.getArrayCount() == fUniforms[i].fArrayCount); michael@0: GrGLint location; michael@0: // TODO: Move the Xoom uniform array in both FS and VS bug workaround here. michael@0: if (fUsingBindUniform) { michael@0: location = i; michael@0: GR_GL_CALL(fGpu->glInterface(), michael@0: BindUniformLocation(programID, location, uniforms[i].fVariable.c_str())); michael@0: } else { michael@0: GR_GL_CALL_RET(fGpu->glInterface(), location, michael@0: GetUniformLocation(programID, uniforms[i].fVariable.c_str())); michael@0: } michael@0: if (GrGLShaderBuilder::kVertex_Visibility & uniforms[i].fVisibility) { michael@0: fUniforms[i].fVSLocation = location; michael@0: } michael@0: if (GrGLShaderBuilder::kFragment_Visibility & uniforms[i].fVisibility) { michael@0: fUniforms[i].fFSLocation = location; michael@0: } michael@0: } michael@0: } michael@0: michael@0: const GrGLUniformManager::BuilderUniform& michael@0: GrGLUniformManager::getBuilderUniform(const BuilderUniformArray& array, UniformHandle handle) const { michael@0: return array[handle.toUniformIndex()]; michael@0: }