GPU: Use GPUShader setters for uniforms removing uses of ShaderInterface

This commit is contained in:
Clément Foucault 2020-08-20 12:26:29 +02:00
parent 5e65498e3d
commit 14fcd46ca7
4 changed files with 40 additions and 93 deletions

View File

@ -103,13 +103,11 @@ void immVertex2iv(uint attr_id, const int data[2]);
/* Provide uniform values that don't change for the entire draw call. */
void immUniform1i(const char *name, int x);
void immUniform4iv(const char *name, const int data[4]);
void immUniform1f(const char *name, float x);
void immUniform2f(const char *name, float x, float y);
void immUniform2fv(const char *name, const float data[2]);
void immUniform3f(const char *name, float x, float y, float z);
void immUniform3fv(const char *name, const float data[3]);
void immUniformArray3fv(const char *name, const float *data, int count);
void immUniform4f(const char *name, float x, float y, float z, float w);
void immUniform4fv(const char *name, const float data[4]);
void immUniformArray4fv(const char *bare_name, const float *data, int count);

View File

@ -749,123 +749,77 @@ void immVertex2iv(uint attr_id, const int data[2])
/* --- generic uniform functions --- */
#if 0
# if TRUST_NO_ONE
# define GET_UNIFORM \
const GPUShaderInput *uniform = GPU_shaderinterface_uniform(imm.shader_interface, name); \
assert(uniform);
# else
# define GET_UNIFORM \
const GPUShaderInput *uniform = GPU_shaderinterface_uniform(imm.shader_interface, name);
# endif
#else
/* NOTE: It is possible to have uniform fully optimized out from the shader.
* In this case we can't assert failure or allow NULL-pointer dereference.
* TODO(sergey): How can we detect existing-but-optimized-out uniform but still
* catch typos in uniform names passed to immUniform*() functions? */
# define GET_UNIFORM \
const GPUShaderInput *uniform = GPU_shaderinterface_uniform(imm.shader_interface, name); \
if (uniform == NULL) \
return;
#endif
void immUniform1f(const char *name, float x)
{
GET_UNIFORM
glUniform1f(uniform->location, x);
GPU_shader_uniform_1f(imm.bound_program, name, x);
}
void immUniform2f(const char *name, float x, float y)
{
GET_UNIFORM
glUniform2f(uniform->location, x, y);
GPU_shader_uniform_2f(imm.bound_program, name, x, y);
}
void immUniform2fv(const char *name, const float data[2])
{
GET_UNIFORM
glUniform2fv(uniform->location, 1, data);
GPU_shader_uniform_2fv(imm.bound_program, name, data);
}
void immUniform3f(const char *name, float x, float y, float z)
{
GET_UNIFORM
glUniform3f(uniform->location, x, y, z);
GPU_shader_uniform_3f(imm.bound_program, name, x, y, z);
}
void immUniform3fv(const char *name, const float data[3])
{
GET_UNIFORM
glUniform3fv(uniform->location, 1, data);
}
/* can increase this limit or move to another file */
#define MAX_UNIFORM_NAME_LEN 60
/* Note array index is not supported for name (i.e: "array[0]"). */
void immUniformArray3fv(const char *name, const float *data, int count)
{
GET_UNIFORM
glUniform3fv(uniform->location, count, data);
GPU_shader_uniform_3fv(imm.bound_program, name, data);
}
void immUniform4f(const char *name, float x, float y, float z, float w)
{
GET_UNIFORM
glUniform4f(uniform->location, x, y, z, w);
GPU_shader_uniform_4f(imm.bound_program, name, x, y, z, w);
}
void immUniform4fv(const char *name, const float data[4])
{
GET_UNIFORM
glUniform4fv(uniform->location, 1, data);
GPU_shader_uniform_4fv(imm.bound_program, name, data);
}
/* Note array index is not supported for name (i.e: "array[0]"). */
void immUniformArray4fv(const char *name, const float *data, int count)
{
GET_UNIFORM
glUniform4fv(uniform->location, count, data);
GPU_shader_uniform_4fv_array(imm.bound_program, name, count, (float(*)[4])data);
}
void immUniformMatrix4fv(const char *name, const float data[4][4])
{
GET_UNIFORM
glUniformMatrix4fv(uniform->location, 1, GL_FALSE, (float *)data);
GPU_shader_uniform_mat4(imm.bound_program, name, data);
}
void immUniform1i(const char *name, int x)
{
GET_UNIFORM
glUniform1i(uniform->location, x);
}
void immUniform4iv(const char *name, const int data[4])
{
GET_UNIFORM
glUniform4iv(uniform->location, 1, data);
GPU_shader_uniform_1i(imm.bound_program, name, x);
}
void immBindTexture(const char *name, GPUTexture *tex)
{
GET_UNIFORM
GPU_texture_bind(tex, uniform->binding);
int binding = GPU_shader_get_texture_binding(imm.bound_program, name);
GPU_texture_bind(tex, binding);
}
void immBindTextureSampler(const char *name, GPUTexture *tex, eGPUSamplerState state)
{
GET_UNIFORM
GPU_texture_bind_ex(tex, state, uniform->binding, true);
int binding = GPU_shader_get_texture_binding(imm.bound_program, name);
GPU_texture_bind_ex(tex, state, binding, true);
}
/* --- convenience functions for setting "uniform vec4 color" --- */
void immUniformColor4f(float r, float g, float b, float a)
{
int32_t uniform_loc = GPU_shaderinterface_uniform_builtin(imm.shader_interface,
GPU_UNIFORM_COLOR);
int32_t uniform_loc = GPU_shader_get_builtin_uniform(imm.bound_program, GPU_UNIFORM_COLOR);
BLI_assert(uniform_loc != -1);
glUniform4f(uniform_loc, r, g, b, a);
float data[4] = {r, g, b, a};
GPU_shader_uniform_vector(imm.bound_program, uniform_loc, 4, 1, data);
}
void immUniformColor4fv(const float rgba[4])

View File

@ -649,14 +649,13 @@ void GPU_matrix_bind(GPUShader *shader)
* call this before a draw call if desired matrices are dirty
* call glUseProgram before this, as glUniform expects program to be bound
*/
const GPUShaderInterface *shaderface = shader->interface;
int32_t MV = GPU_shaderinterface_uniform_builtin(shaderface, GPU_UNIFORM_MODELVIEW);
int32_t P = GPU_shaderinterface_uniform_builtin(shaderface, GPU_UNIFORM_PROJECTION);
int32_t MVP = GPU_shaderinterface_uniform_builtin(shaderface, GPU_UNIFORM_MVP);
int32_t MV = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_MODELVIEW);
int32_t P = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_PROJECTION);
int32_t MVP = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_MVP);
int32_t N = GPU_shaderinterface_uniform_builtin(shaderface, GPU_UNIFORM_NORMAL);
int32_t MV_inv = GPU_shaderinterface_uniform_builtin(shaderface, GPU_UNIFORM_MODELVIEW_INV);
int32_t P_inv = GPU_shaderinterface_uniform_builtin(shaderface, GPU_UNIFORM_PROJECTION_INV);
int32_t N = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_NORMAL);
int32_t MV_inv = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_MODELVIEW_INV);
int32_t P_inv = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_PROJECTION_INV);
if (MV != -1) {
GPU_shader_uniform_vector(shader, MV, 16, 1, (const float *)GPU_matrix_model_view_get(NULL));

View File

@ -565,14 +565,10 @@ void GPU_shader_uniform_float(GPUShader *shader, int location, float value)
GPU_shader_uniform_vector(shader, location, 1, 1, &value);
}
#define GET_UNIFORM \
const GPUShaderInput *uniform = GPU_shaderinterface_uniform(sh->interface, name); \
BLI_assert(uniform);
void GPU_shader_uniform_1i(GPUShader *sh, const char *name, int value)
{
GET_UNIFORM
GPU_shader_uniform_int(sh, uniform->location, value);
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_int(sh, loc, value);
}
void GPU_shader_uniform_1b(GPUShader *sh, const char *name, bool value)
@ -600,44 +596,44 @@ void GPU_shader_uniform_4f(GPUShader *sh, const char *name, float x, float y, fl
void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float x)
{
GET_UNIFORM
GPU_shader_uniform_float(sh, uniform->location, x);
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_float(sh, loc, x);
}
void GPU_shader_uniform_2fv(GPUShader *sh, const char *name, const float data[2])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 2, 1, data);
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 2, 1, data);
}
void GPU_shader_uniform_3fv(GPUShader *sh, const char *name, const float data[3])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 3, 1, data);
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 3, 1, data);
}
void GPU_shader_uniform_4fv(GPUShader *sh, const char *name, const float data[4])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 4, 1, data);
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 4, 1, data);
}
void GPU_shader_uniform_mat4(GPUShader *sh, const char *name, const float data[4][4])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 16, 1, (const float *)data);
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 16, 1, (const float *)data);
}
void GPU_shader_uniform_2fv_array(GPUShader *sh, const char *name, int len, const float (*val)[2])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 2, len, (const float *)val);
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 2, len, (const float *)val);
}
void GPU_shader_uniform_4fv_array(GPUShader *sh, const char *name, int len, const float (*val)[4])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 4, len, (const float *)val);
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 4, len, (const float *)val);
}
/** \} */
@ -657,7 +653,7 @@ static int g_shader_builtin_srgb_transform = 0;
void GPU_shader_set_srgb_uniform(GPUShader *shader)
{
int32_t loc = GPU_shaderinterface_uniform_builtin(shader->interface, GPU_UNIFORM_SRGB_TRANSFORM);
int32_t loc = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_SRGB_TRANSFORM);
if (loc != -1) {
GPU_shader_uniform_vector_int(shader, loc, 1, 1, &g_shader_builtin_srgb_transform);
}