Changeset View
Changeset View
Standalone View
Standalone View
source/blender/gpu/intern/gpu_vertex_format.c
| Show All 17 Lines | |||||
| */ | */ | ||||
| /** \file | /** \file | ||||
| * \ingroup gpu | * \ingroup gpu | ||||
| * | * | ||||
| * GPU vertex format | * GPU vertex format | ||||
| */ | */ | ||||
| #include "GPU_shader_interface.h" | |||||
| #include "GPU_vertex_format.h" | #include "GPU_vertex_format.h" | ||||
| #include "gpu_vertex_format_private.h" | #include "gpu_vertex_format_private.h" | ||||
| #include <stddef.h> | #include <stddef.h> | ||||
| #include <string.h> | #include <string.h> | ||||
| #include "BLI_ghash.h" | #include "BLI_ghash.h" | ||||
| #include "BLI_string.h" | #include "BLI_string.h" | ||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| #include "GPU_shader.h" | |||||
| #include "gpu_shader_private.h" | |||||
| #define PACK_DEBUG 0 | #define PACK_DEBUG 0 | ||||
| #if PACK_DEBUG | #if PACK_DEBUG | ||||
| # include <stdio.h> | # include <stdio.h> | ||||
| #endif | #endif | ||||
| void GPU_vertformat_clear(GPUVertFormat *format) | void GPU_vertformat_clear(GPUVertFormat *format) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 341 Lines • ▼ Show 20 Lines | |||||
| #if PACK_DEBUG | #if PACK_DEBUG | ||||
| show_pack(0, 0, end_padding); | show_pack(0, 0, end_padding); | ||||
| putchar('\n'); | putchar('\n'); | ||||
| #endif | #endif | ||||
| format->stride = offset + end_padding; | format->stride = offset + end_padding; | ||||
| format->packed = true; | format->packed = true; | ||||
| } | } | ||||
| static uint calc_input_component_size(const GPUShaderInput *input) | static uint calc_component_size(const GLenum gl_type) | ||||
| { | { | ||||
| int size = input->size; | switch (gl_type) { | ||||
| switch (input->gl_type) { | |||||
| case GL_FLOAT_VEC2: | case GL_FLOAT_VEC2: | ||||
| case GL_INT_VEC2: | case GL_INT_VEC2: | ||||
| case GL_UNSIGNED_INT_VEC2: | case GL_UNSIGNED_INT_VEC2: | ||||
| return size * 2; | return 2; | ||||
| case GL_FLOAT_VEC3: | case GL_FLOAT_VEC3: | ||||
| case GL_INT_VEC3: | case GL_INT_VEC3: | ||||
| case GL_UNSIGNED_INT_VEC3: | case GL_UNSIGNED_INT_VEC3: | ||||
| return size * 3; | return 3; | ||||
| case GL_FLOAT_VEC4: | case GL_FLOAT_VEC4: | ||||
| case GL_FLOAT_MAT2: | case GL_FLOAT_MAT2: | ||||
| case GL_INT_VEC4: | case GL_INT_VEC4: | ||||
| case GL_UNSIGNED_INT_VEC4: | case GL_UNSIGNED_INT_VEC4: | ||||
| return size * 4; | return 4; | ||||
| case GL_FLOAT_MAT3: | case GL_FLOAT_MAT3: | ||||
| return size * 9; | return 9; | ||||
| case GL_FLOAT_MAT4: | case GL_FLOAT_MAT4: | ||||
| return size * 16; | return 16; | ||||
| case GL_FLOAT_MAT2x3: | case GL_FLOAT_MAT2x3: | ||||
| case GL_FLOAT_MAT3x2: | case GL_FLOAT_MAT3x2: | ||||
| return size * 6; | return 6; | ||||
| case GL_FLOAT_MAT2x4: | case GL_FLOAT_MAT2x4: | ||||
| case GL_FLOAT_MAT4x2: | case GL_FLOAT_MAT4x2: | ||||
| return size * 8; | return 8; | ||||
| case GL_FLOAT_MAT3x4: | case GL_FLOAT_MAT3x4: | ||||
| case GL_FLOAT_MAT4x3: | case GL_FLOAT_MAT4x3: | ||||
| return size * 12; | return 12; | ||||
| default: | default: | ||||
| return size; | return 1; | ||||
| } | } | ||||
| } | } | ||||
| static void get_fetch_mode_and_comp_type(int gl_type, | static void get_fetch_mode_and_comp_type(int gl_type, | ||||
| GPUVertCompType *r_comp_type, | GPUVertCompType *r_comp_type, | ||||
| GPUVertFetchMode *r_fetch_mode) | GPUVertFetchMode *r_fetch_mode) | ||||
| { | { | ||||
| switch (gl_type) { | switch (gl_type) { | ||||
| Show All 27 Lines | case GL_UNSIGNED_INT_VEC4: | ||||
| *r_comp_type = GPU_COMP_U32; | *r_comp_type = GPU_COMP_U32; | ||||
| *r_fetch_mode = GPU_FETCH_INT; | *r_fetch_mode = GPU_FETCH_INT; | ||||
| break; | break; | ||||
| default: | default: | ||||
| BLI_assert(0); | BLI_assert(0); | ||||
| } | } | ||||
| } | } | ||||
| void GPU_vertformat_from_interface(GPUVertFormat *format, const GPUShaderInterface *shaderface) | void GPU_vertformat_from_shader(GPUVertFormat *format, const GPUShader *shader) | ||||
| { | { | ||||
| const char *name_buffer = shaderface->name_buffer; | GPU_vertformat_clear(format); | ||||
| GPUVertAttr *attr = &format->attrs[0]; | |||||
| for (int i = 0; i < GPU_NUM_SHADERINTERFACE_BUCKETS; i++) { | GLint attr_len; | ||||
| const GPUShaderInput *input = shaderface->attr_buckets[i]; | glGetProgramiv(shader->program, GL_ACTIVE_ATTRIBUTES, &attr_len); | ||||
| if (input == NULL) { | |||||
| continue; | |||||
| } | |||||
| const GPUShaderInput *next = input; | for (int i = 0; i < attr_len; i++) { | ||||
| while (next != NULL) { | char name[256]; | ||||
| input = next; | GLenum gl_type; | ||||
| next = input->next; | GLint size; | ||||
| glGetActiveAttrib(shader->program, i, sizeof(name), NULL, &size, &gl_type, name); | |||||
| /* OpenGL attributes such as `gl_VertexID` have a location of -1. */ | /* Ignore OpenGL names like `gl_BaseInstanceARB`, `gl_InstanceID` and `gl_VertexID`. */ | ||||
| if (input->location < 0) { | if (glGetAttribLocation(shader->program, name) == -1) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| format->name_len++; /* multiname support */ | format->name_len++; /* multiname support */ | ||||
| format->attr_len++; | format->attr_len++; | ||||
| GPUVertCompType comp_type; | GPUVertCompType comp_type; | ||||
| GPUVertFetchMode fetch_mode; | GPUVertFetchMode fetch_mode; | ||||
| get_fetch_mode_and_comp_type(input->gl_type, &comp_type, &fetch_mode); | get_fetch_mode_and_comp_type(gl_type, &comp_type, &fetch_mode); | ||||
| GPUVertAttr *attr = &format->attrs[input->location]; | |||||
| attr->names[attr->name_len++] = copy_attr_name(format, name_buffer + input->name_offset); | attr->names[attr->name_len++] = copy_attr_name(format, name); | ||||
| attr->offset = 0; /* offsets & stride are calculated later (during pack) */ | attr->offset = 0; /* offsets & stride are calculated later (during pack) */ | ||||
| attr->comp_len = calc_input_component_size(input); | attr->comp_len = calc_component_size(gl_type) * size; | ||||
| attr->sz = attr->comp_len * 4; | attr->sz = attr->comp_len * 4; | ||||
| attr->fetch_mode = fetch_mode; | attr->fetch_mode = fetch_mode; | ||||
| attr->comp_type = comp_type; | attr->comp_type = comp_type; | ||||
| attr->gl_comp_type = convert_comp_type_to_gl(comp_type); | attr->gl_comp_type = convert_comp_type_to_gl(comp_type); | ||||
| } | attr += 1; | ||||
| } | } | ||||
| } | } | ||||