Revert "Gawain: Optimize out extra level on top of ShaderInput"

This reverts commit 5514d2df1c.
This commit is contained in:
Clément Foucault 2017-10-06 01:50:16 +02:00
parent 5514d2df1c
commit 9ab3db11c7
2 changed files with 39 additions and 16 deletions

View File

@ -33,7 +33,6 @@ typedef enum {
} Gwn_UniformBuiltin;
typedef struct Gwn_ShaderInput {
struct Gwn_ShaderInput* next;
const char* name;
unsigned name_hash;
GLenum gl_type;
@ -42,13 +41,18 @@ typedef struct Gwn_ShaderInput {
GLint location;
} Gwn_ShaderInput;
typedef struct Gwn_ShaderInput_Entry {
struct Gwn_ShaderInput_Entry* next;
Gwn_ShaderInput* shader_input;
} Gwn_ShaderInput_Entry;
#define GWN_NUM_SHADERINTERFACE_BUCKETS 1009
typedef struct Gwn_ShaderInterface {
uint16_t uniform_ct;
uint16_t attrib_ct;
Gwn_ShaderInput* uniform_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS];
Gwn_ShaderInput* attrib_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS];
Gwn_ShaderInput_Entry* uniform_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS];
Gwn_ShaderInput_Entry* attrib_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS];
Gwn_ShaderInput* builtin_uniforms[GWN_NUM_UNIFORMS];
Gwn_ShaderInput inputs[0]; // dynamic size, uniforms followed by attribs
} Gwn_ShaderInterface;

View File

@ -69,20 +69,22 @@ GWN_INLINE void set_input_name(Gwn_ShaderInput* input, const char* name)
}
GWN_INLINE void shader_input_to_bucket(Gwn_ShaderInput* input,
Gwn_ShaderInput* buckets[GWN_NUM_SHADERINTERFACE_BUCKETS])
Gwn_ShaderInput_Entry* buckets[GWN_NUM_SHADERINTERFACE_BUCKETS])
{
Gwn_ShaderInput_Entry* entry = malloc(sizeof(Gwn_ShaderInput_Entry));
const unsigned bucket_index = input->name_hash % GWN_NUM_SHADERINTERFACE_BUCKETS;
input->next = buckets[bucket_index];
buckets[bucket_index] = input;
entry->next = buckets[bucket_index];
entry->shader_input = input;
buckets[bucket_index] = entry;
}
GWN_INLINE Gwn_ShaderInput* buckets_lookup(Gwn_ShaderInput* const buckets[GWN_NUM_SHADERINTERFACE_BUCKETS],
GWN_INLINE Gwn_ShaderInput* buckets_lookup(Gwn_ShaderInput_Entry* const buckets[GWN_NUM_SHADERINTERFACE_BUCKETS],
const char *name)
{
const unsigned name_hash = hash_string(name);
const unsigned bucket_index = name_hash % GWN_NUM_SHADERINTERFACE_BUCKETS;
Gwn_ShaderInput* input = buckets[bucket_index];
if (input == NULL)
const Gwn_ShaderInput_Entry* entry = buckets[bucket_index];
if (entry == NULL)
{
// Requested uniform is not found at all.
return NULL;
@ -90,22 +92,22 @@ GWN_INLINE Gwn_ShaderInput* buckets_lookup(Gwn_ShaderInput* const buckets[GWN_NU
// Optimization bit: if there is no hash collision detected when constructing shader interface
// it means we can only request the single possible uniform. Surely, it's possible we request
// uniform which causes hash collision, but that will be detected in debug builds.
if (input->next == NULL)
if (entry->next == NULL)
{
if (name_hash == input->name_hash)
if (name_hash == entry->shader_input->name_hash)
{
#if TRUST_NO_ONE
assert(match(input->name, name));
assert(match(entry->shader_input->name, name));
#endif
return input;
return entry->shader_input;
}
return NULL;
}
// Work through possible collisions.
while (input != NULL)
while (entry != NULL)
{
Gwn_ShaderInput* uniform = input;
input = input->next;
Gwn_ShaderInput* uniform = entry->shader_input;
entry = entry->next;
#if SUPPORT_LEGACY_GLSL
if (uniform->name == NULL) continue;
#endif
@ -121,6 +123,20 @@ GWN_INLINE Gwn_ShaderInput* buckets_lookup(Gwn_ShaderInput* const buckets[GWN_NU
return NULL; // not found
}
GWN_INLINE void buckets_free(Gwn_ShaderInput_Entry* buckets[GWN_NUM_SHADERINTERFACE_BUCKETS])
{
for (unsigned bucket_index = 0; bucket_index < GWN_NUM_SHADERINTERFACE_BUCKETS; ++bucket_index)
{
Gwn_ShaderInput_Entry *entry = buckets[bucket_index];
while (entry != NULL)
{
Gwn_ShaderInput_Entry *entry_next = entry->next;
free(entry);
entry = entry_next;
}
}
}
// keep these in sync with Gwn_UniformBuiltin order
#define FIRST_MAT4_UNIFORM GWN_UNIFORM_MODELVIEW
#define LAST_MAT4_UNIFORM GWN_UNIFORM_PROJECTION_INV
@ -316,6 +332,9 @@ Gwn_ShaderInterface* GWN_shaderinterface_create(GLint program)
void GWN_shaderinterface_discard(Gwn_ShaderInterface* shaderface)
{
// Free memory used by buckets and has entries.
buckets_free(shaderface->uniform_buckets);
buckets_free(shaderface->attrib_buckets);
// Free memory used by shader interface by its self.
free(shaderface);
}