From 210f4f3e559696afea64244c29994aed9685d483 Mon Sep 17 00:00:00 2001 From: degasus Date: Fri, 24 Jan 2014 14:46:05 +0100 Subject: [PATCH] PortableVertexFormat: add a struct which hold all needed information for every vertex and use this for position atm, position attribute is hardcoded both in VertexLoader and in backends. v2: fix coding style + cleanup lookup table --- .../VideoBackends/D3D/NativeVertexFormat.cpp | 60 +++++++++---------- .../VideoBackends/OGL/NativeVertexFormat.cpp | 15 ++++- Source/Core/VideoCommon/NativeVertexFormat.h | 11 ++++ Source/Core/VideoCommon/VertexLoader.cpp | 5 ++ 4 files changed, 58 insertions(+), 33 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/NativeVertexFormat.cpp b/Source/Core/VideoBackends/D3D/NativeVertexFormat.cpp index bd6ef40dc6..244df9bb5b 100644 --- a/Source/Core/VideoBackends/D3D/NativeVertexFormat.cpp +++ b/Source/Core/VideoBackends/D3D/NativeVertexFormat.cpp @@ -32,33 +32,27 @@ NativeVertexFormat* VertexManager::CreateNativeVertexFormat() return new D3DVertexFormat(); } -DXGI_FORMAT VarToD3D(VarType t, int size) +static const DXGI_FORMAT d3d_format_lookup[5*4*2] = { - DXGI_FORMAT retval = DXGI_FORMAT_UNKNOWN; - static const DXGI_FORMAT lookup1[5] = { - DXGI_FORMAT_R8_UNORM, DXGI_FORMAT_R8_SNORM, DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_R16_SNORM, DXGI_FORMAT_R32_FLOAT - }; - static const DXGI_FORMAT lookup2[5] = { - DXGI_FORMAT_R8G8_UNORM, DXGI_FORMAT_R8G8_SNORM, DXGI_FORMAT_R16G16_UNORM, DXGI_FORMAT_R16G16_SNORM, DXGI_FORMAT_R32G32_FLOAT - }; - static const DXGI_FORMAT lookup3[5] = { - DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_R32G32B32_FLOAT - }; - static const DXGI_FORMAT lookup4[5] = { - DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_SNORM, DXGI_FORMAT_R16G16B16A16_UNORM, DXGI_FORMAT_R16G16B16A16_SNORM, DXGI_FORMAT_R32G32B32A32_FLOAT - }; + // float formats + DXGI_FORMAT_R8_UNORM, DXGI_FORMAT_R8_SNORM, DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_R16_SNORM, DXGI_FORMAT_R32_FLOAT, + DXGI_FORMAT_R8G8_UNORM, DXGI_FORMAT_R8G8_SNORM, DXGI_FORMAT_R16G16_UNORM, DXGI_FORMAT_R16G16_SNORM, DXGI_FORMAT_R32G32_FLOAT, + DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_R32G32B32_FLOAT, + DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_SNORM, DXGI_FORMAT_R16G16B16A16_UNORM, DXGI_FORMAT_R16G16B16A16_SNORM, DXGI_FORMAT_R32G32B32A32_FLOAT, - switch (size) - { - case 1: retval = lookup1[t]; break; - case 2: retval = lookup2[t]; break; - case 3: retval = lookup3[t]; break; - case 4: retval = lookup4[t]; break; - default: break; - } + // integer formats + DXGI_FORMAT_R8_UINT, DXGI_FORMAT_R8_SINT, DXGI_FORMAT_R16_UINT, DXGI_FORMAT_R16_SINT, DXGI_FORMAT_UNKNOWN, + DXGI_FORMAT_R8G8_UINT, DXGI_FORMAT_R8G8_SINT, DXGI_FORMAT_R16G16_UINT, DXGI_FORMAT_R16G16_SINT, DXGI_FORMAT_UNKNOWN, + DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, + DXGI_FORMAT_R8G8B8A8_UINT, DXGI_FORMAT_R8G8B8A8_SINT, DXGI_FORMAT_R16G16B16A16_UINT, DXGI_FORMAT_R16G16B16A16_SINT, DXGI_FORMAT_UNKNOWN, +}; + +DXGI_FORMAT VarToD3D(VarType t, int size, bool integer) +{ + DXGI_FORMAT retval = d3d_format_lookup[(int)t + 5*(size-1) + 5*4*(int)integer]; if (retval == DXGI_FORMAT_UNKNOWN) { - PanicAlert("VarToD3D: Invalid type/size combo %i , %i", (int)t, size); + PanicAlert("VarToD3D: Invalid type/size combo %i , %i, %i", (int)t, size, (int)integer); } return retval; } @@ -67,12 +61,16 @@ void D3DVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl) { vertex_stride = _vtx_decl.stride; memset(m_elems, 0, sizeof(m_elems)); + const AttributeFormat* format = &_vtx_decl.position; - m_elems[m_num_elems].SemanticName = "POSITION"; - m_elems[m_num_elems].AlignedByteOffset = 0; - m_elems[m_num_elems].Format = DXGI_FORMAT_R32G32B32_FLOAT; - m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; - ++m_num_elems; + if (format->enable) + { + m_elems[m_num_elems].SemanticName = "POSITION"; + m_elems[m_num_elems].AlignedByteOffset = format->offset; + m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer); + m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; + ++m_num_elems; + } for (int i = 0; i < 3; i++) { @@ -81,7 +79,7 @@ void D3DVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl) m_elems[m_num_elems].SemanticName = "NORMAL"; m_elems[m_num_elems].SemanticIndex = i; m_elems[m_num_elems].AlignedByteOffset = _vtx_decl.normal_offset[i]; - m_elems[m_num_elems].Format = VarToD3D(_vtx_decl.normal_gl_type, _vtx_decl.normal_gl_size); + m_elems[m_num_elems].Format = VarToD3D(_vtx_decl.normal_gl_type, _vtx_decl.normal_gl_size, false); m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; ++m_num_elems; } @@ -94,7 +92,7 @@ void D3DVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl) m_elems[m_num_elems].SemanticName = "COLOR"; m_elems[m_num_elems].SemanticIndex = i; m_elems[m_num_elems].AlignedByteOffset = _vtx_decl.color_offset[i]; - m_elems[m_num_elems].Format = VarToD3D(_vtx_decl.color_gl_type, 4); + m_elems[m_num_elems].Format = VarToD3D(_vtx_decl.color_gl_type, 4, false); m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; ++m_num_elems; } @@ -107,7 +105,7 @@ void D3DVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl) m_elems[m_num_elems].SemanticName = "TEXCOORD"; m_elems[m_num_elems].SemanticIndex = i; m_elems[m_num_elems].AlignedByteOffset = _vtx_decl.texcoord_offset[i]; - m_elems[m_num_elems].Format = VarToD3D(_vtx_decl.texcoord_gl_type[i], _vtx_decl.texcoord_size[i]); + m_elems[m_num_elems].Format = VarToD3D(_vtx_decl.texcoord_gl_type[i], _vtx_decl.texcoord_size[i], false); m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; ++m_num_elems; } diff --git a/Source/Core/VideoBackends/OGL/NativeVertexFormat.cpp b/Source/Core/VideoBackends/OGL/NativeVertexFormat.cpp index b23836ac90..49098b24b4 100644 --- a/Source/Core/VideoBackends/OGL/NativeVertexFormat.cpp +++ b/Source/Core/VideoBackends/OGL/NativeVertexFormat.cpp @@ -42,6 +42,18 @@ inline GLuint VarToGL(VarType t) return lookup[t]; } +static void SetPointer(u32 attrib, u32 stride, const AttributeFormat &format) +{ + if (!format.enable) + return; + + glEnableVertexAttribArray(attrib); + if (format.integer) + glVertexAttribIPointer(attrib, format.components, VarToGL(format.type), stride, (u8*)NULL + format.offset); + else + glVertexAttribPointer(attrib, format.components, VarToGL(format.type), true, stride, (u8*)NULL + format.offset); +} + void GLVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl) { this->vtx_decl = _vtx_decl; @@ -60,8 +72,7 @@ void GLVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->m_index_buffers); glBindBuffer(GL_ARRAY_BUFFER, vm->m_vertex_buffers); - glEnableVertexAttribArray(SHADER_POSITION_ATTRIB); - glVertexAttribPointer(SHADER_POSITION_ATTRIB, 3, GL_FLOAT, GL_FALSE, vtx_decl.stride, (u8*)NULL); + SetPointer(SHADER_POSITION_ATTRIB, vertex_stride, vtx_decl.position); for (int i = 0; i < 3; i++) { if (vtx_decl.num_normals > i) { diff --git a/Source/Core/VideoCommon/NativeVertexFormat.h b/Source/Core/VideoCommon/NativeVertexFormat.h index dcae4d81bd..127d8f7b8e 100644 --- a/Source/Core/VideoCommon/NativeVertexFormat.h +++ b/Source/Core/VideoCommon/NativeVertexFormat.h @@ -72,10 +72,21 @@ enum VarType VAR_FLOAT, // GX_F32 = 4 }; +struct AttributeFormat +{ + VarType type; + int components; + int offset; + bool enable; + bool integer; +}; + struct PortableVertexDeclaration { int stride; + AttributeFormat position; + int num_normals; int normal_offset[3]; VarType normal_gl_type; diff --git a/Source/Core/VideoCommon/VertexLoader.cpp b/Source/Core/VideoCommon/VertexLoader.cpp index 45bfec384a..968e98676a 100644 --- a/Source/Core/VideoCommon/VertexLoader.cpp +++ b/Source/Core/VideoCommon/VertexLoader.cpp @@ -587,6 +587,11 @@ void VertexLoader::CompileVertexTranslator() } m_VertexSize += VertexLoader_Position::GetSize(m_VtxDesc.Position, m_VtxAttr.PosFormat, m_VtxAttr.PosElements); nat_offset += 12; + vtx_decl.position.components = 3; + vtx_decl.position.enable = true; + vtx_decl.position.offset = 0; + vtx_decl.position.type = VAR_FLOAT; + vtx_decl.position.integer = false; // Normals vtx_decl.num_normals = 0;