📄 shader_api.c
字号:
_mesa_lookup_shader_program_err(ctx, program, "glGetUniformLocation"); if (!shProg) return -1; if (shProg->LinkStatus == GL_FALSE) { _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(program)"); return -1; } /* XXX we should return -1 if the uniform was declared, but not * actually used. */ return _mesa_lookup_uniform(shProg->Uniforms, name);}/** * Called via ctx->Driver.ShaderSource() */static void_mesa_shader_source(GLcontext *ctx, GLuint shader, const GLchar *source){ struct gl_shader *sh; sh = _mesa_lookup_shader_err(ctx, shader, "glShaderSource"); if (!sh) return; /* free old shader source string and install new one */ if (sh->Source) { _mesa_free((void *) sh->Source); } sh->Source = source; sh->CompileStatus = GL_FALSE;}/** * Called via ctx->Driver.CompileShader() */static void_mesa_compile_shader(GLcontext *ctx, GLuint shaderObj){ struct gl_shader *sh; sh = _mesa_lookup_shader_err(ctx, shaderObj, "glCompileShader"); if (!sh) return; sh->CompileStatus = _slang_compile(ctx, sh);}/** * Called via ctx->Driver.LinkProgram() */static void_mesa_link_program(GLcontext *ctx, GLuint program){ struct gl_shader_program *shProg; shProg = _mesa_lookup_shader_program_err(ctx, program, "glLinkProgram"); if (!shProg) return; FLUSH_VERTICES(ctx, _NEW_PROGRAM); _slang_link(ctx, program, shProg);}/** * Called via ctx->Driver.UseProgram() */void_mesa_use_program(GLcontext *ctx, GLuint program){ struct gl_shader_program *shProg; if (ctx->Shader.CurrentProgram && ctx->Shader.CurrentProgram->Name == program) { /* no-op */ return; } FLUSH_VERTICES(ctx, _NEW_PROGRAM); if (program) { shProg = _mesa_lookup_shader_program_err(ctx, program, "glUseProgram"); if (!shProg) { return; } if (!shProg->LinkStatus) { _mesa_error(ctx, GL_INVALID_OPERATION, "glUseProgram"); return; } } else { shProg = NULL; } _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, shProg);}/** * Update the vertex and fragment program's TexturesUsed arrays. */static voidupdate_textures_used(struct gl_program *prog){ GLuint s; memset(prog->TexturesUsed, 0, sizeof(prog->TexturesUsed)); for (s = 0; s < MAX_SAMPLERS; s++) { if (prog->SamplersUsed & (1 << s)) { GLuint u = prog->SamplerUnits[s]; GLuint t = prog->SamplerTargets[s]; assert(u < MAX_TEXTURE_IMAGE_UNITS); prog->TexturesUsed[u] |= (1 << t); } }}static GLbooleanis_sampler_type(GLenum type){ switch (type) { case GL_SAMPLER_1D: case GL_SAMPLER_2D: case GL_SAMPLER_3D: case GL_SAMPLER_CUBE: case GL_SAMPLER_1D_SHADOW: case GL_SAMPLER_2D_SHADOW: case GL_SAMPLER_2D_RECT_ARB: case GL_SAMPLER_2D_RECT_SHADOW_ARB: case GL_SAMPLER_1D_ARRAY_EXT: case GL_SAMPLER_2D_ARRAY_EXT: return GL_TRUE; default: return GL_FALSE; }}/** * Check if the type given by userType is allowed to set a uniform of the * target type. Generally, equivalence is required, but setting Boolean * uniforms can be done with glUniformiv or glUniformfv. */static GLbooleancompatible_types(GLenum userType, GLenum targetType){ if (userType == targetType) return GL_TRUE; if (targetType == GL_BOOL && (userType == GL_FLOAT || userType == GL_INT)) return GL_TRUE; if (targetType == GL_BOOL_VEC2 && (userType == GL_FLOAT_VEC2 || userType == GL_INT_VEC2)) return GL_TRUE; if (targetType == GL_BOOL_VEC3 && (userType == GL_FLOAT_VEC3 || userType == GL_INT_VEC3)) return GL_TRUE; if (targetType == GL_BOOL_VEC4 && (userType == GL_FLOAT_VEC4 || userType == GL_INT_VEC4)) return GL_TRUE; if (is_sampler_type(targetType) && userType == GL_INT) return GL_TRUE; return GL_FALSE;}/** * Set the value of a program's uniform variable. * \param program the program whose uniform to update * \param location the location/index of the uniform * \param type the datatype of the uniform * \param count the number of uniforms to set * \param elems number of elements per uniform * \param values the new values */static voidset_program_uniform(GLcontext *ctx, struct gl_program *program, GLint location, GLenum type, GLsizei count, GLint elems, const void *values){ if (!compatible_types(type, program->Parameters->Parameters[location].DataType)) { _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)"); return; } if (program->Parameters->Parameters[location].Type == PROGRAM_SAMPLER) { /* This controls which texture unit which is used by a sampler */ GLuint texUnit, sampler; /* data type for setting samplers must be int */ if (type != GL_INT || count != 1) { _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(only glUniform1i can be used " "to set sampler uniforms)"); return; } sampler = (GLuint) program->Parameters->ParameterValues[location][0]; texUnit = ((GLuint *) values)[0]; /* check that the sampler (tex unit index) is legal */ if (texUnit >= ctx->Const.MaxTextureImageUnits) { _mesa_error(ctx, GL_INVALID_VALUE, "glUniform1(invalid sampler/tex unit index)"); return; } /* This maps a sampler to a texture unit: */ program->SamplerUnits[sampler] = texUnit; update_textures_used(program); FLUSH_VERTICES(ctx, _NEW_TEXTURE); } else { /* ordinary uniform variable */ GLsizei k, i; if (count * elems > (GLint) program->Parameters->Parameters[location].Size) { _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(count too large)"); return; } for (k = 0; k < count; k++) { GLfloat *uniformVal = program->Parameters->ParameterValues[location + k]; if (type == GL_INT || type == GL_INT_VEC2 || type == GL_INT_VEC3 || type == GL_INT_VEC4) { const GLint *iValues = ((const GLint *) values) + k * elems; for (i = 0; i < elems; i++) { uniformVal[i] = (GLfloat) iValues[i]; } } else { const GLfloat *fValues = ((const GLfloat *) values) + k * elems; for (i = 0; i < elems; i++) { uniformVal[i] = fValues[i]; } } } }}/** * Called via ctx->Driver.Uniform(). */static void_mesa_uniform(GLcontext *ctx, GLint location, GLsizei count, const GLvoid *values, GLenum type){ struct gl_shader_program *shProg = ctx->Shader.CurrentProgram; GLint elems; if (!shProg || !shProg->LinkStatus) { _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)"); return; } if (location == -1) return; /* The standard specifies this as a no-op */ if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) { _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location)"); return; } if (count < 0) { _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(count < 0)"); return; } switch (type) { case GL_FLOAT: case GL_INT: elems = 1; break; case GL_FLOAT_VEC2: case GL_INT_VEC2: elems = 2; break; case GL_FLOAT_VEC3: case GL_INT_VEC3: elems = 3; break; case GL_FLOAT_VEC4: case GL_INT_VEC4: elems = 4; break; default: _mesa_problem(ctx, "Invalid type in _mesa_uniform"); return; } FLUSH_VERTICES(ctx, _NEW_PROGRAM); /* A uniform var may be used by both a vertex shader and a fragment * shader. We may need to update one or both shader's uniform here: */ if (shProg->VertexProgram) { GLint loc = shProg->Uniforms->Uniforms[location].VertPos; if (loc >= 0) { set_program_uniform(ctx, &shProg->VertexProgram->Base, loc, type, count, elems, values); } } if (shProg->FragmentProgram) { GLint loc = shProg->Uniforms->Uniforms[location].FragPos; if (loc >= 0) { set_program_uniform(ctx, &shProg->FragmentProgram->Base, loc, type, count, elems, values); } }}static voidget_matrix_dims(GLenum type, GLint *rows, GLint *cols){ switch (type) { case GL_FLOAT_MAT2: *rows = *cols = 2; break; case GL_FLOAT_MAT2x3: *rows = 3; *cols = 2; break; case GL_FLOAT_MAT2x4: *rows = 4; *cols = 2; break; case GL_FLOAT_MAT3: *rows = 3; *cols = 3; break; case GL_FLOAT_MAT3x2: *rows = 2; *cols = 3; break; case GL_FLOAT_MAT3x4: *rows = 4; *cols = 3; break; case GL_FLOAT_MAT4: *rows = 4; *cols = 4; break; case GL_FLOAT_MAT4x2: *rows = 2; *cols = 4; break; case GL_FLOAT_MAT4x3: *rows = 3; *cols = 4; break; default: *rows = *cols = 0; }}static voidset_program_uniform_matrix(GLcontext *ctx, struct gl_program *program, GLuint location, GLuint count, GLuint rows, GLuint cols, GLboolean transpose, const GLfloat *values){ GLuint mat, row, col; GLuint dst = location, src = 0; GLint nr, nc; /* check that the number of rows, columns is correct */ get_matrix_dims(program->Parameters->Parameters[location].DataType, &nr, &nc); if (rows != nr || cols != nc) { _mesa_error(ctx, GL_INVALID_OPERATION, "glUniformMatrix(matrix size mismatch"); return; } /* * Note: the _columns_ of a matrix are stored in program registers, not * the rows. So, the loops below look a little funny. * XXX could optimize this a bit... */ /* loop over matrices */ for (mat = 0; mat < count; mat++) { /* each matrix: */ for (col = 0; col < cols; col++) { GLfloat *v = program->Parameters->ParameterValues[dst]; for (row = 0; row < rows; row++) { if (transpose) { v[row] = values[src + row * cols + col]; } else { v[row] = values[src + col * rows + row]; } } dst++; } src += rows * cols; /* next matrix */ }}/** * Called by ctx->Driver.UniformMatrix(). * Note: cols=2, rows=4 ==> array[2] of vec4 */static void_mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows, GLenum matrixType, GLint location, GLsizei count, GLboolean transpose, const GLfloat *values){ struct gl_shader_program *shProg = ctx->Shader.CurrentProgram; if (!shProg || !shProg->LinkStatus) { _mesa_error(ctx, GL_INVALID_OPERATION, "glUniformMatrix(program not linked)"); return; } if (location == -1) return; /* The standard specifies this as a no-op */ if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) { _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix(location)"); return; } if (values == NULL) { _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix"); return; } FLUSH_VERTICES(ctx, _NEW_PROGRAM); if (shProg->VertexProgram) { GLint loc = shProg->Uniforms->Uniforms[location].VertPos; if (loc >= 0) { set_program_uniform_matrix(ctx, &shProg->VertexProgram->Base, loc, count, rows, cols, transpose, values); } } if (shProg->FragmentProgram) { GLint loc = shProg->Uniforms->Uniforms[location].FragPos; if (loc >= 0) { set_program_uniform_matrix(ctx, &shProg->FragmentProgram->Base, loc, count, rows, cols, transpose, values); } }}static void_mesa_validate_program(GLcontext *ctx, GLuint program){ struct gl_shader_program *shProg; shProg = _mesa_lookup_shader_program_err(ctx, program, "glValidateProgram"); if (!shProg) { return; } if (!shProg->LinkStatus) { shProg->Validated = GL_FALSE; return; } /* From the GL spec, a program is invalid if any of these are true: any two active samplers in the current program object are of different types, but refer to the same texture image unit, any active sampler in the current program object refers to a texture image unit where fixed-function fragment processing accesses a texture target that does not match the sampler type, or the sum of the number of active samplers in the program and the number of texture image units enabled for fixed-function fragment processing exceeds the combined limit on the total number of texture image units allowed. */ shProg->Validated = GL_TRUE;}/** * Plug in Mesa's GLSL functions into the device driver function table. */void_mesa_init_glsl_driver_functions(struct dd_function_table *driver){ driver->AttachShader = _mesa_attach_shader; driver->BindAttribLocation = _mesa_bind_attrib_location; driver->CompileShader = _mesa_compile_shader; driver->CreateProgram = _mesa_create_program; driver->CreateShader = _mesa_create_shader; driver->DeleteProgram2 = _mesa_delete_program2; driver->DeleteShader = _mesa_delete_shader; driver->DetachShader = _mesa_detach_shader; driver->GetActiveAttrib = _mesa_get_active_attrib; driver->GetActiveUniform = _mesa_get_active_uniform; driver->GetAttachedShaders = _mesa_get_attached_shaders; driver->GetAttribLocation = _mesa_get_attrib_location; driver->GetHandle = _mesa_get_handle; driver->GetProgramiv = _mesa_get_programiv; driver->GetProgramInfoLog = _mesa_get_program_info_log; driver->GetShaderiv = _mesa_get_shaderiv; driver->GetShaderInfoLog = _mesa_get_shader_info_log; driver->GetShaderSource = _mesa_get_shader_source; driver->GetUniformfv = _mesa_get_uniformfv; driver->GetUniformiv = _mesa_get_uniformiv; driver->GetUniformLocation = _mesa_get_uniform_location; driver->IsProgram = _mesa_is_program; driver->IsShader = _mesa_is_shader; driver->LinkProgram = _mesa_link_program; driver->ShaderSource = _mesa_shader_source; driver->Uniform = _mesa_uniform; driver->UniformMatrix = _mesa_uniform_matrix; driver->UseProgram = _mesa_use_program; driver->ValidateProgram = _mesa_validate_program;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -