Files
dolphin/Source/Core/VideoBackends/OGL/OGLNativeVertexFormat.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.9 KiB
C++
Raw Normal View History

// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2008-12-08 05:25:12 +00:00
#include "Common/CommonTypes.h"
2021-06-26 12:48:28 -07:00
#include "Common/EnumMap.h"
#include "Common/GL/GLUtil.h"
#include "Common/MsgHandler.h"
2008-12-08 05:25:12 +00:00
2023-01-27 13:21:09 +13:00
#include "VideoBackends/OGL/OGLGfx.h"
#include "VideoBackends/OGL/OGLVertexManager.h"
2014-02-17 05:18:15 -05:00
#include "VideoBackends/OGL/ProgramShaderCache.h"
#include "VideoCommon/NativeVertexFormat.h"
#include "VideoCommon/VertexShaderGen.h"
2008-12-08 05:25:12 +00:00
// Here's some global state. We only use this to keep track of what we've sent to the OpenGL state
// machine.
2008-12-08 05:25:12 +00:00
namespace OGL
{
std::unique_ptr<NativeVertexFormat>
2023-01-27 13:21:09 +13:00
OGLGfx::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl)
{
return std::make_unique<GLVertexFormat>(vtx_decl);
}
2008-12-08 05:25:12 +00:00
2021-06-26 12:48:28 -07:00
static inline GLuint VarToGL(ComponentFormat t)
{
2021-06-26 12:48:28 -07:00
static constexpr Common::EnumMap<GLuint, ComponentFormat::Float> lookup = {
GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, GL_FLOAT,
};
return lookup[t];
}
2008-12-08 05:25:12 +00:00
static void SetPointer(ShaderAttrib attrib, u32 stride, const AttributeFormat& format)
{
if (!format.enable)
return;
glEnableVertexAttribArray(static_cast<GLuint>(attrib));
if (format.integer)
glVertexAttribIPointer(static_cast<GLuint>(attrib), format.components, VarToGL(format.type),
stride, (u8*)nullptr + format.offset);
else
glVertexAttribPointer(static_cast<GLuint>(attrib), format.components, VarToGL(format.type),
true, stride, (u8*)nullptr + format.offset);
}
GLVertexFormat::GLVertexFormat(const PortableVertexDeclaration& vtx_decl)
: NativeVertexFormat(vtx_decl)
{
const u32 vertex_stride = vtx_decl.stride;
2008-12-08 05:25:12 +00:00
// We will not allow vertex components causing uneven strides.
2012-12-15 14:43:01 +01:00
if (vertex_stride & 3)
PanicAlertFmt("Uneven vertex stride: {}", vertex_stride);
VertexManager* const vm = static_cast<VertexManager*>(g_vertex_manager.get());
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// the element buffer is bound directly to the vao, so we must it set for every vao
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->GetIndexBufferHandle());
glBindBuffer(GL_ARRAY_BUFFER, vm->GetVertexBufferHandle());
2008-12-08 05:25:12 +00:00
SetPointer(ShaderAttrib::Position, vertex_stride, vtx_decl.position);
for (u32 i = 0; i < 3; i++)
SetPointer(ShaderAttrib::Normal + i, vertex_stride, vtx_decl.normals[i]);
2008-12-08 05:25:12 +00:00
for (u32 i = 0; i < 2; i++)
SetPointer(ShaderAttrib::Color0 + i, vertex_stride, vtx_decl.colors[i]);
2008-12-08 05:25:12 +00:00
for (u32 i = 0; i < 8; i++)
SetPointer(ShaderAttrib::TexCoord0 + i, vertex_stride, vtx_decl.texcoords[i]);
2008-12-08 05:25:12 +00:00
SetPointer(ShaderAttrib::PositionMatrix, vertex_stride, vtx_decl.posmtx);
// Other code shouldn't have to worry about its vertex formats being randomly unbound
ProgramShaderCache::ReBindVertexFormat();
}
2008-12-26 17:02:46 +00:00
GLVertexFormat::~GLVertexFormat()
{
2019-03-05 23:02:40 +10:00
ProgramShaderCache::InvalidateVertexFormatIfBound(VAO);
glDeleteVertexArrays(1, &VAO);
}
} // namespace OGL