GWN: Port to GPU module: Replace GWN prefix by GPU
This commit is contained in:
@@ -23,10 +23,10 @@
|
||||
* ***** END GPL LICENSE BLOCK *****
|
||||
*/
|
||||
|
||||
/** \file blender/gpu/intern/gwn_shader_interface.c
|
||||
/** \file blender/gpu/intern/gpu_shader_interface.c
|
||||
* \ingroup gpu
|
||||
*
|
||||
* Gawain shader interface (C --> GLSL)
|
||||
* GPU shader interface (C --> GLSL)
|
||||
*/
|
||||
|
||||
#include "gpu_batch_private.h"
|
||||
@@ -42,46 +42,46 @@
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
static const char* BuiltinUniform_name(Gwn_UniformBuiltin u)
|
||||
static const char* BuiltinUniform_name(GPUUniformBuiltin u)
|
||||
{
|
||||
static const char* names[] = {
|
||||
[GWN_UNIFORM_NONE] = NULL,
|
||||
[GPU_UNIFORM_NONE] = NULL,
|
||||
|
||||
[GWN_UNIFORM_MODEL] = "ModelMatrix",
|
||||
[GWN_UNIFORM_VIEW] = "ViewMatrix",
|
||||
[GWN_UNIFORM_MODELVIEW] = "ModelViewMatrix",
|
||||
[GWN_UNIFORM_PROJECTION] = "ProjectionMatrix",
|
||||
[GWN_UNIFORM_VIEWPROJECTION] = "ViewProjectionMatrix",
|
||||
[GWN_UNIFORM_MVP] = "ModelViewProjectionMatrix",
|
||||
[GPU_UNIFORM_MODEL] = "ModelMatrix",
|
||||
[GPU_UNIFORM_VIEW] = "ViewMatrix",
|
||||
[GPU_UNIFORM_MODELVIEW] = "ModelViewMatrix",
|
||||
[GPU_UNIFORM_PROJECTION] = "ProjectionMatrix",
|
||||
[GPU_UNIFORM_VIEWPROJECTION] = "ViewProjectionMatrix",
|
||||
[GPU_UNIFORM_MVP] = "ModelViewProjectionMatrix",
|
||||
|
||||
[GWN_UNIFORM_MODEL_INV] = "ModelMatrixInverse",
|
||||
[GWN_UNIFORM_VIEW_INV] = "ViewMatrixInverse",
|
||||
[GWN_UNIFORM_MODELVIEW_INV] = "ModelViewMatrixInverse",
|
||||
[GWN_UNIFORM_PROJECTION_INV] = "ProjectionMatrixInverse",
|
||||
[GWN_UNIFORM_VIEWPROJECTION_INV] = "ViewProjectionMatrixInverse",
|
||||
[GPU_UNIFORM_MODEL_INV] = "ModelMatrixInverse",
|
||||
[GPU_UNIFORM_VIEW_INV] = "ViewMatrixInverse",
|
||||
[GPU_UNIFORM_MODELVIEW_INV] = "ModelViewMatrixInverse",
|
||||
[GPU_UNIFORM_PROJECTION_INV] = "ProjectionMatrixInverse",
|
||||
[GPU_UNIFORM_VIEWPROJECTION_INV] = "ViewProjectionMatrixInverse",
|
||||
|
||||
[GWN_UNIFORM_NORMAL] = "NormalMatrix",
|
||||
[GWN_UNIFORM_WORLDNORMAL] = "WorldNormalMatrix",
|
||||
[GWN_UNIFORM_CAMERATEXCO] = "CameraTexCoFactors",
|
||||
[GWN_UNIFORM_ORCO] = "OrcoTexCoFactors",
|
||||
[GPU_UNIFORM_NORMAL] = "NormalMatrix",
|
||||
[GPU_UNIFORM_WORLDNORMAL] = "WorldNormalMatrix",
|
||||
[GPU_UNIFORM_CAMERATEXCO] = "CameraTexCoFactors",
|
||||
[GPU_UNIFORM_ORCO] = "OrcoTexCoFactors",
|
||||
|
||||
[GWN_UNIFORM_COLOR] = "color",
|
||||
[GWN_UNIFORM_EYE] = "eye",
|
||||
[GWN_UNIFORM_CALLID] = "callId",
|
||||
[GPU_UNIFORM_COLOR] = "color",
|
||||
[GPU_UNIFORM_EYE] = "eye",
|
||||
[GPU_UNIFORM_CALLID] = "callId",
|
||||
|
||||
[GWN_UNIFORM_CUSTOM] = NULL,
|
||||
[GWN_NUM_UNIFORMS] = NULL,
|
||||
[GPU_UNIFORM_CUSTOM] = NULL,
|
||||
[GPU_NUM_UNIFORMS] = NULL,
|
||||
};
|
||||
|
||||
return names[u];
|
||||
}
|
||||
|
||||
GWN_INLINE bool match(const char* a, const char* b)
|
||||
GPU_INLINE bool match(const char* a, const char* b)
|
||||
{
|
||||
return strcmp(a, b) == 0;
|
||||
}
|
||||
|
||||
GWN_INLINE uint hash_string(const char *str)
|
||||
GPU_INLINE uint hash_string(const char *str)
|
||||
{
|
||||
uint i = 0, c;
|
||||
while ((c = *str++)) {
|
||||
@@ -90,7 +90,7 @@ GWN_INLINE uint hash_string(const char *str)
|
||||
return i;
|
||||
}
|
||||
|
||||
GWN_INLINE void set_input_name(Gwn_ShaderInterface* shaderface, Gwn_ShaderInput* input,
|
||||
GPU_INLINE void set_input_name(GPUShaderInterface* shaderface, GPUShaderInput* input,
|
||||
const char* name, uint32_t name_len)
|
||||
{
|
||||
input->name_offset = shaderface->name_buffer_offset;
|
||||
@@ -98,20 +98,20 @@ GWN_INLINE void set_input_name(Gwn_ShaderInterface* shaderface, Gwn_ShaderInput*
|
||||
shaderface->name_buffer_offset += name_len + 1; /* include NULL terminator */
|
||||
}
|
||||
|
||||
GWN_INLINE void shader_input_to_bucket(Gwn_ShaderInput* input,
|
||||
Gwn_ShaderInput* buckets[GWN_NUM_SHADERINTERFACE_BUCKETS])
|
||||
GPU_INLINE void shader_input_to_bucket(GPUShaderInput* input,
|
||||
GPUShaderInput* buckets[GPU_NUM_SHADERINTERFACE_BUCKETS])
|
||||
{
|
||||
const uint bucket_index = input->name_hash % GWN_NUM_SHADERINTERFACE_BUCKETS;
|
||||
const uint bucket_index = input->name_hash % GPU_NUM_SHADERINTERFACE_BUCKETS;
|
||||
input->next = buckets[bucket_index];
|
||||
buckets[bucket_index] = input;
|
||||
}
|
||||
|
||||
GWN_INLINE const Gwn_ShaderInput* buckets_lookup(Gwn_ShaderInput* const buckets[GWN_NUM_SHADERINTERFACE_BUCKETS],
|
||||
GPU_INLINE const GPUShaderInput* buckets_lookup(GPUShaderInput* const buckets[GPU_NUM_SHADERINTERFACE_BUCKETS],
|
||||
const char *name_buffer, const char *name)
|
||||
{
|
||||
const uint name_hash = hash_string(name);
|
||||
const uint bucket_index = name_hash % GWN_NUM_SHADERINTERFACE_BUCKETS;
|
||||
const Gwn_ShaderInput* input = buckets[bucket_index];
|
||||
const uint bucket_index = name_hash % GPU_NUM_SHADERINTERFACE_BUCKETS;
|
||||
const GPUShaderInput* input = buckets[bucket_index];
|
||||
if (input == NULL) {
|
||||
/* Requested uniform is not found at all. */
|
||||
return NULL;
|
||||
@@ -129,7 +129,7 @@ GWN_INLINE const Gwn_ShaderInput* buckets_lookup(Gwn_ShaderInput* const buckets[
|
||||
return NULL;
|
||||
}
|
||||
/* Work through possible collisions. */
|
||||
const Gwn_ShaderInput* next = input;
|
||||
const GPUShaderInput* next = input;
|
||||
while (next != NULL) {
|
||||
input = next;
|
||||
next = input->next;
|
||||
@@ -143,37 +143,37 @@ GWN_INLINE const Gwn_ShaderInput* buckets_lookup(Gwn_ShaderInput* const buckets[
|
||||
return NULL; /* not found */
|
||||
}
|
||||
|
||||
GWN_INLINE void buckets_free(Gwn_ShaderInput* buckets[GWN_NUM_SHADERINTERFACE_BUCKETS])
|
||||
GPU_INLINE void buckets_free(GPUShaderInput* buckets[GPU_NUM_SHADERINTERFACE_BUCKETS])
|
||||
{
|
||||
for (uint bucket_index = 0; bucket_index < GWN_NUM_SHADERINTERFACE_BUCKETS; ++bucket_index) {
|
||||
Gwn_ShaderInput *input = buckets[bucket_index];
|
||||
for (uint bucket_index = 0; bucket_index < GPU_NUM_SHADERINTERFACE_BUCKETS; ++bucket_index) {
|
||||
GPUShaderInput *input = buckets[bucket_index];
|
||||
while (input != NULL) {
|
||||
Gwn_ShaderInput *input_next = input->next;
|
||||
GPUShaderInput *input_next = input->next;
|
||||
free(input);
|
||||
input = input_next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool setup_builtin_uniform(Gwn_ShaderInput* input, const char* name)
|
||||
static bool setup_builtin_uniform(GPUShaderInput* input, const char* name)
|
||||
{
|
||||
/* TODO: reject DOUBLE, IMAGE, ATOMIC_COUNTER gl_types */
|
||||
|
||||
/* detect built-in uniforms (name must match) */
|
||||
for (Gwn_UniformBuiltin u = GWN_UNIFORM_NONE + 1; u < GWN_UNIFORM_CUSTOM; ++u) {
|
||||
for (GPUUniformBuiltin u = GPU_UNIFORM_NONE + 1; u < GPU_UNIFORM_CUSTOM; ++u) {
|
||||
const char* builtin_name = BuiltinUniform_name(u);
|
||||
if (match(name, builtin_name)) {
|
||||
input->builtin_type = u;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
input->builtin_type = GWN_UNIFORM_CUSTOM;
|
||||
input->builtin_type = GPU_UNIFORM_CUSTOM;
|
||||
return false;
|
||||
}
|
||||
|
||||
static const Gwn_ShaderInput* add_uniform(Gwn_ShaderInterface* shaderface, const char* name)
|
||||
static const GPUShaderInput* add_uniform(GPUShaderInterface* shaderface, const char* name)
|
||||
{
|
||||
Gwn_ShaderInput* input = malloc(sizeof(Gwn_ShaderInput));
|
||||
GPUShaderInput* input = malloc(sizeof(GPUShaderInput));
|
||||
|
||||
input->location = glGetUniformLocation(shaderface->program, name);
|
||||
|
||||
@@ -186,13 +186,13 @@ static const Gwn_ShaderInput* add_uniform(Gwn_ShaderInterface* shaderface, const
|
||||
setup_builtin_uniform(input, name);
|
||||
|
||||
shader_input_to_bucket(input, shaderface->uniform_buckets);
|
||||
if (input->builtin_type != GWN_UNIFORM_NONE &&
|
||||
input->builtin_type != GWN_UNIFORM_CUSTOM)
|
||||
if (input->builtin_type != GPU_UNIFORM_NONE &&
|
||||
input->builtin_type != GPU_UNIFORM_CUSTOM)
|
||||
{
|
||||
shaderface->builtin_uniforms[input->builtin_type] = input;
|
||||
}
|
||||
#if DEBUG_SHADER_INTERFACE
|
||||
printf("Gwn_ShaderInterface %p, program %d, uniform[] '%s' at location %d\n", shaderface,
|
||||
printf("GPUShaderInterface %p, program %d, uniform[] '%s' at location %d\n", shaderface,
|
||||
shaderface->program,
|
||||
name,
|
||||
input->location);
|
||||
@@ -200,14 +200,14 @@ static const Gwn_ShaderInput* add_uniform(Gwn_ShaderInterface* shaderface, const
|
||||
return input;
|
||||
}
|
||||
|
||||
Gwn_ShaderInterface* GWN_shaderinterface_create(int32_t program)
|
||||
GPUShaderInterface* GPU_shaderinterface_create(int32_t program)
|
||||
{
|
||||
Gwn_ShaderInterface* shaderface = calloc(1, sizeof(Gwn_ShaderInterface));
|
||||
GPUShaderInterface* shaderface = calloc(1, sizeof(GPUShaderInterface));
|
||||
shaderface->program = program;
|
||||
|
||||
#if DEBUG_SHADER_INTERFACE
|
||||
printf("%s {\n", __func__); /* enter function */
|
||||
printf("Gwn_ShaderInterface %p, program %d\n", shaderface, program);
|
||||
printf("GPUShaderInterface %p, program %d\n", shaderface, program);
|
||||
#endif
|
||||
|
||||
GLint max_attrib_name_len, attr_len;
|
||||
@@ -223,7 +223,7 @@ Gwn_ShaderInterface* GWN_shaderinterface_create(int32_t program)
|
||||
|
||||
/* Attributes */
|
||||
for (uint32_t i = 0; i < attr_len; ++i) {
|
||||
Gwn_ShaderInput* input = malloc(sizeof(Gwn_ShaderInput));
|
||||
GPUShaderInput* input = malloc(sizeof(GPUShaderInput));
|
||||
GLsizei remaining_buffer = name_buffer_len - shaderface->name_buffer_offset;
|
||||
char* name = shaderface->name_buffer + shaderface->name_buffer_offset;
|
||||
GLsizei name_len = 0;
|
||||
@@ -250,7 +250,7 @@ Gwn_ShaderInterface* GWN_shaderinterface_create(int32_t program)
|
||||
}
|
||||
/* Uniform Blocks */
|
||||
for (uint32_t i = 0; i < ubo_len; ++i) {
|
||||
Gwn_ShaderInput* input = malloc(sizeof(Gwn_ShaderInput));
|
||||
GPUShaderInput* input = malloc(sizeof(GPUShaderInput));
|
||||
GLsizei remaining_buffer = name_buffer_len - shaderface->name_buffer_offset;
|
||||
char* name = shaderface->name_buffer + shaderface->name_buffer_offset;
|
||||
GLsizei name_len = 0;
|
||||
@@ -268,20 +268,20 @@ Gwn_ShaderInterface* GWN_shaderinterface_create(int32_t program)
|
||||
#endif
|
||||
}
|
||||
/* Builtin Uniforms */
|
||||
for (Gwn_UniformBuiltin u = GWN_UNIFORM_NONE + 1; u < GWN_UNIFORM_CUSTOM; ++u) {
|
||||
for (GPUUniformBuiltin u = GPU_UNIFORM_NONE + 1; u < GPU_UNIFORM_CUSTOM; ++u) {
|
||||
const char* builtin_name = BuiltinUniform_name(u);
|
||||
if (glGetUniformLocation(program, builtin_name) != -1) {
|
||||
add_uniform((Gwn_ShaderInterface*)shaderface, builtin_name);
|
||||
add_uniform((GPUShaderInterface*)shaderface, builtin_name);
|
||||
}
|
||||
}
|
||||
/* Batches ref buffer */
|
||||
shaderface->batches_len = GWN_SHADERINTERFACE_REF_ALLOC_COUNT;
|
||||
shaderface->batches = calloc(shaderface->batches_len, sizeof(Gwn_Batch*));
|
||||
shaderface->batches_len = GPU_SHADERINTERFACE_REF_ALLOC_COUNT;
|
||||
shaderface->batches = calloc(shaderface->batches_len, sizeof(GPUBatch*));
|
||||
|
||||
return shaderface;
|
||||
}
|
||||
|
||||
void GWN_shaderinterface_discard(Gwn_ShaderInterface* shaderface)
|
||||
void GPU_shaderinterface_discard(GPUShaderInterface* shaderface)
|
||||
{
|
||||
/* Free memory used by buckets and has entries. */
|
||||
buckets_free(shaderface->uniform_buckets);
|
||||
@@ -292,7 +292,7 @@ void GWN_shaderinterface_discard(Gwn_ShaderInterface* shaderface)
|
||||
/* Remove this interface from all linked Batches vao cache. */
|
||||
for (int i = 0; i < shaderface->batches_len; ++i) {
|
||||
if (shaderface->batches[i] != NULL) {
|
||||
gwn_batch_remove_interface_ref(shaderface->batches[i], shaderface);
|
||||
gpu_batch_remove_interface_ref(shaderface->batches[i], shaderface);
|
||||
}
|
||||
}
|
||||
free(shaderface->batches);
|
||||
@@ -300,39 +300,39 @@ void GWN_shaderinterface_discard(Gwn_ShaderInterface* shaderface)
|
||||
free(shaderface);
|
||||
}
|
||||
|
||||
const Gwn_ShaderInput* GWN_shaderinterface_uniform(const Gwn_ShaderInterface* shaderface, const char* name)
|
||||
const GPUShaderInput* GPU_shaderinterface_uniform(const GPUShaderInterface* shaderface, const char* name)
|
||||
{
|
||||
/* TODO: Warn if we find a matching builtin, since these can be looked up much quicker. */
|
||||
const Gwn_ShaderInput* input = buckets_lookup(shaderface->uniform_buckets, shaderface->name_buffer, name);
|
||||
const GPUShaderInput* input = buckets_lookup(shaderface->uniform_buckets, shaderface->name_buffer, name);
|
||||
/* If input is not found add it so it's found next time. */
|
||||
if (input == NULL) {
|
||||
input = add_uniform((Gwn_ShaderInterface*)shaderface, name);
|
||||
input = add_uniform((GPUShaderInterface*)shaderface, name);
|
||||
}
|
||||
return (input->location != -1) ? input : NULL;
|
||||
}
|
||||
|
||||
const Gwn_ShaderInput* GWN_shaderinterface_uniform_builtin(
|
||||
const Gwn_ShaderInterface* shaderface, Gwn_UniformBuiltin builtin)
|
||||
const GPUShaderInput* GPU_shaderinterface_uniform_builtin(
|
||||
const GPUShaderInterface* shaderface, GPUUniformBuiltin builtin)
|
||||
{
|
||||
#if TRUST_NO_ONE
|
||||
assert(builtin != GWN_UNIFORM_NONE);
|
||||
assert(builtin != GWN_UNIFORM_CUSTOM);
|
||||
assert(builtin != GWN_NUM_UNIFORMS);
|
||||
assert(builtin != GPU_UNIFORM_NONE);
|
||||
assert(builtin != GPU_UNIFORM_CUSTOM);
|
||||
assert(builtin != GPU_NUM_UNIFORMS);
|
||||
#endif
|
||||
return shaderface->builtin_uniforms[builtin];
|
||||
}
|
||||
|
||||
const Gwn_ShaderInput* GWN_shaderinterface_ubo(const Gwn_ShaderInterface* shaderface, const char* name)
|
||||
const GPUShaderInput* GPU_shaderinterface_ubo(const GPUShaderInterface* shaderface, const char* name)
|
||||
{
|
||||
return buckets_lookup(shaderface->ubo_buckets, shaderface->name_buffer, name);
|
||||
}
|
||||
|
||||
const Gwn_ShaderInput* GWN_shaderinterface_attr(const Gwn_ShaderInterface* shaderface, const char* name)
|
||||
const GPUShaderInput* GPU_shaderinterface_attr(const GPUShaderInterface* shaderface, const char* name)
|
||||
{
|
||||
return buckets_lookup(shaderface->attrib_buckets, shaderface->name_buffer, name);
|
||||
}
|
||||
|
||||
void GWN_shaderinterface_add_batch_ref(Gwn_ShaderInterface* shaderface, Gwn_Batch* batch)
|
||||
void GPU_shaderinterface_add_batch_ref(GPUShaderInterface* shaderface, GPUBatch* batch)
|
||||
{
|
||||
int i; /* find first unused slot */
|
||||
for (i = 0; i < shaderface->batches_len; ++i) {
|
||||
@@ -343,14 +343,14 @@ void GWN_shaderinterface_add_batch_ref(Gwn_ShaderInterface* shaderface, Gwn_Batc
|
||||
if (i == shaderface->batches_len) {
|
||||
/* Not enough place, realloc the array. */
|
||||
i = shaderface->batches_len;
|
||||
shaderface->batches_len += GWN_SHADERINTERFACE_REF_ALLOC_COUNT;
|
||||
shaderface->batches = realloc(shaderface->batches, sizeof(Gwn_Batch*) * shaderface->batches_len);
|
||||
memset(shaderface->batches + i, 0, sizeof(Gwn_Batch*) * GWN_SHADERINTERFACE_REF_ALLOC_COUNT);
|
||||
shaderface->batches_len += GPU_SHADERINTERFACE_REF_ALLOC_COUNT;
|
||||
shaderface->batches = realloc(shaderface->batches, sizeof(GPUBatch*) * shaderface->batches_len);
|
||||
memset(shaderface->batches + i, 0, sizeof(GPUBatch*) * GPU_SHADERINTERFACE_REF_ALLOC_COUNT);
|
||||
}
|
||||
shaderface->batches[i] = batch;
|
||||
}
|
||||
|
||||
void GWN_shaderinterface_remove_batch_ref(Gwn_ShaderInterface* shaderface, Gwn_Batch* batch)
|
||||
void GPU_shaderinterface_remove_batch_ref(GPUShaderInterface* shaderface, GPUBatch* batch)
|
||||
{
|
||||
for (int i = 0; i < shaderface->batches_len; ++i) {
|
||||
if (shaderface->batches[i] == batch) {
|
||||
|
||||
Reference in New Issue
Block a user