GPU: Move gpu_vertex_format.c to C++

This commit is contained in:
Clément Foucault 2020-07-27 23:56:43 +02:00
parent 18caaff149
commit 959529d4b8
6 changed files with 46 additions and 50 deletions

View File

@ -83,7 +83,7 @@ set(SRC
intern/gpu_texture_smoke.cc
intern/gpu_uniformbuffer.c
intern/gpu_vertex_buffer.c
intern/gpu_vertex_format.c
intern/gpu_vertex_format.cc
intern/gpu_viewport.c
GPU_attr_binding.h

View File

@ -42,7 +42,7 @@ extern "C" {
#define GPU_MAX_SAFE_ATTR_NAME 12
typedef enum {
GPU_COMP_I8,
GPU_COMP_I8 = 0,
GPU_COMP_U8,
GPU_COMP_I16,
GPU_COMP_U16,
@ -52,17 +52,21 @@ typedef enum {
GPU_COMP_F32,
GPU_COMP_I10,
/* Warning! adjust GPUVertAttr if changing. */
} GPUVertCompType;
typedef enum {
GPU_FETCH_FLOAT,
GPU_FETCH_FLOAT = 0,
GPU_FETCH_INT,
GPU_FETCH_INT_TO_FLOAT_UNIT, /* 127 (ubyte) -> 0.5 (and so on for other int types) */
GPU_FETCH_INT_TO_FLOAT, /* 127 (any int type) -> 127.0 */
/* Warning! adjust GPUVertAttr if changing. */
} GPUVertFetchMode;
typedef struct GPUVertAttr {
/* GPUVertFetchMode */
uint fetch_mode : 2;
/* GPUVertCompType */
uint comp_type : 3;
/* 1 to 4 or 8 or 12 or 16 */
uint comp_len : 5;
@ -72,8 +76,6 @@ typedef struct GPUVertAttr {
uint offset : 11;
/* up to GPU_VERT_ATTR_MAX_NAMES */
uint name_len : 3;
uint gl_comp_type;
/* -- 8 Bytes -- */
uchar names[GPU_VERT_ATTR_MAX_NAMES];
} GPUVertAttr;

View File

@ -37,6 +37,7 @@
#include "gpu_context_private.h"
#include "gpu_primitive_private.h"
#include "gpu_shader_private.h"
#include "gpu_vertex_format_private.h"
#include <limits.h>
#include <stdlib.h>
@ -440,6 +441,7 @@ static void create_bindings(GPUVertBuf *verts,
}
const GLvoid *pointer = (const GLubyte *)0 + offset + v_first * stride;
const GLenum type = convert_comp_type_to_gl(static_cast<GPUVertCompType>(a->comp_type));
for (uint n_idx = 0; n_idx < a->name_len; n_idx++) {
const char *name = GPU_vertformat_attr_name_get(format, a, n_idx);
@ -452,19 +454,13 @@ static void create_bindings(GPUVertBuf *verts,
*attr_mask &= ~(1 << input->location);
if (a->comp_len == 16 || a->comp_len == 12 || a->comp_len == 8) {
#if TRUST_NO_ONE
assert(a->fetch_mode == GPU_FETCH_FLOAT);
assert(a->gl_comp_type == GL_FLOAT);
#endif
BLI_assert(a->fetch_mode == GPU_FETCH_FLOAT);
BLI_assert(a->comp_type == GPU_COMP_F32);
for (int i = 0; i < a->comp_len / 4; i++) {
glEnableVertexAttribArray(input->location + i);
glVertexAttribDivisor(input->location + i, (use_instancing) ? 1 : 0);
glVertexAttribPointer(input->location + i,
4,
a->gl_comp_type,
GL_FALSE,
stride,
(const GLubyte *)pointer + i * 16);
glVertexAttribPointer(
input->location + i, 4, type, GL_FALSE, stride, (const GLubyte *)pointer + i * 16);
}
}
else {
@ -474,15 +470,13 @@ static void create_bindings(GPUVertBuf *verts,
switch (a->fetch_mode) {
case GPU_FETCH_FLOAT:
case GPU_FETCH_INT_TO_FLOAT:
glVertexAttribPointer(
input->location, a->comp_len, a->gl_comp_type, GL_FALSE, stride, pointer);
glVertexAttribPointer(input->location, a->comp_len, type, GL_FALSE, stride, pointer);
break;
case GPU_FETCH_INT_TO_FLOAT_UNIT:
glVertexAttribPointer(
input->location, a->comp_len, a->gl_comp_type, GL_TRUE, stride, pointer);
glVertexAttribPointer(input->location, a->comp_len, type, GL_TRUE, stride, pointer);
break;
case GPU_FETCH_INT:
glVertexAttribIPointer(input->location, a->comp_len, a->gl_comp_type, stride, pointer);
glVertexAttribIPointer(input->location, a->comp_len, type, stride, pointer);
break;
}
}

View File

@ -364,17 +364,18 @@ static void immDrawSetup(void)
const GLvoid *pointer = (const GLubyte *)0 + offset;
const uint loc = read_attr_location(&imm.attr_binding, a_idx);
const GLenum type = convert_comp_type_to_gl(static_cast<GPUVertCompType>(a->comp_type));
switch (a->fetch_mode) {
case GPU_FETCH_FLOAT:
case GPU_FETCH_INT_TO_FLOAT:
glVertexAttribPointer(loc, a->comp_len, a->gl_comp_type, GL_FALSE, stride, pointer);
glVertexAttribPointer(loc, a->comp_len, type, GL_FALSE, stride, pointer);
break;
case GPU_FETCH_INT_TO_FLOAT_UNIT:
glVertexAttribPointer(loc, a->comp_len, a->gl_comp_type, GL_TRUE, stride, pointer);
glVertexAttribPointer(loc, a->comp_len, type, GL_TRUE, stride, pointer);
break;
case GPU_FETCH_INT:
glVertexAttribIPointer(loc, a->comp_len, a->gl_comp_type, stride, pointer);
glVertexAttribIPointer(loc, a->comp_len, type, stride, pointer);
}
}

View File

@ -63,21 +63,29 @@ void GPU_vertformat_copy(GPUVertFormat *dest, const GPUVertFormat *src)
memcpy(dest, src, sizeof(GPUVertFormat));
}
static GLenum convert_comp_type_to_gl(GPUVertCompType type)
GLenum convert_comp_type_to_gl(GPUVertCompType type)
{
static const GLenum table[] = {
[GPU_COMP_I8] = GL_BYTE,
[GPU_COMP_U8] = GL_UNSIGNED_BYTE,
[GPU_COMP_I16] = GL_SHORT,
[GPU_COMP_U16] = GL_UNSIGNED_SHORT,
[GPU_COMP_I32] = GL_INT,
[GPU_COMP_U32] = GL_UNSIGNED_INT,
[GPU_COMP_F32] = GL_FLOAT,
[GPU_COMP_I10] = GL_INT_2_10_10_10_REV,
};
return table[type];
switch (type) {
case GPU_COMP_I8:
return GL_BYTE;
case GPU_COMP_U8:
return GL_UNSIGNED_BYTE;
case GPU_COMP_I16:
return GL_SHORT;
case GPU_COMP_U16:
return GL_UNSIGNED_SHORT;
case GPU_COMP_I32:
return GL_INT;
case GPU_COMP_U32:
return GL_UNSIGNED_INT;
case GPU_COMP_F32:
return GL_FLOAT;
case GPU_COMP_I10:
return GL_INT_2_10_10_10_REV;
default:
BLI_assert(0);
return GL_FLOAT;
}
}
static uint comp_sz(GPUVertCompType type)
@ -94,7 +102,7 @@ static uint attr_sz(const GPUVertAttr *a)
if (a->comp_type == GPU_COMP_I10) {
return 4; /* always packed as 10_10_10_2 */
}
return a->comp_len * comp_sz(a->comp_type);
return a->comp_len * comp_sz(static_cast<GPUVertCompType>(a->comp_type));
}
static uint attr_align(const GPUVertAttr *a)
@ -102,7 +110,7 @@ static uint attr_align(const GPUVertAttr *a)
if (a->comp_type == GPU_COMP_I10) {
return 4; /* always packed as 10_10_10_2 */
}
uint c = comp_sz(a->comp_type);
uint c = comp_sz(static_cast<GPUVertCompType>(a->comp_type));
if (a->comp_len == 3 && c <= 2) {
return 4 * c; /* AMD HW can't fetch these well, so pad it out (other vendors too?) */
}
@ -185,7 +193,6 @@ uint GPU_vertformat_attr_add(GPUVertFormat *format,
attr->names[attr->name_len++] = copy_attr_name(format, name);
attr->comp_type = comp_type;
attr->gl_comp_type = convert_comp_type_to_gl(comp_type);
attr->comp_len = (comp_type == GPU_COMP_I10) ?
4 :
comp_len; /* system needs 10_10_10_2 to be 4 or BGRA */
@ -279,7 +286,7 @@ void GPU_vertformat_attr_rename(GPUVertFormat *format, int attr_id, const char *
/* Encode 8 original bytes into 11 safe bytes. */
static void safe_bytes(char out[11], const char data[8])
{
char safe_chars[63] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
char safe_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
uint64_t in = *(uint64_t *)data;
for (int i = 0; i < 11; i++) {
@ -368,14 +375,6 @@ static void show_pack(uint a_idx, uint sz, uint pad)
void VertexFormat_pack(GPUVertFormat *format)
{
/* For now, attributes are packed in the order they were added,
* making sure each attribute is naturally aligned (add padding where necessary)
* Later we can implement more efficient packing w/ reordering
* (keep attribute ID order, adjust their offsets to reorder in buffer). */
/* TODO: realloc just enough to hold the final combo string. And just enough to
* hold used attributes, not all 16. */
GPUVertAttr *a0 = &format->attrs[0];
a0->offset = 0;
uint offset = a0->sz;
@ -512,7 +511,6 @@ void GPU_vertformat_from_shader(GPUVertFormat *format, const GPUShader *shader)
attr->sz = attr->comp_len * 4;
attr->fetch_mode = fetch_mode;
attr->comp_type = comp_type;
attr->gl_comp_type = convert_comp_type_to_gl(comp_type);
attr += 1;
}
}

View File

@ -33,6 +33,7 @@ extern "C" {
void VertexFormat_pack(GPUVertFormat *format);
uint padding(uint offset, uint alignment);
uint vertex_buffer_size(const GPUVertFormat *format, uint vertex_len);
GLenum convert_comp_type_to_gl(GPUVertCompType type);
#ifdef __cplusplus
}