2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2017-02-01 15:56:13 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2021-06-26 19:48:28 +00:00
|
|
|
#include "Common/EnumMap.h"
|
2015-09-18 16:40:00 +00:00
|
|
|
#include "Common/GL/GLUtil.h"
|
2017-02-01 15:56:13 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2023-01-27 00:21:09 +00:00
|
|
|
#include "VideoBackends/OGL/OGLGfx.h"
|
2020-09-15 12:00:24 +00:00
|
|
|
#include "VideoBackends/OGL/OGLVertexManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/NativeVertexFormat.h"
|
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2008-10-24 23:08:46 +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
|
|
|
|
2010-11-26 09:25:08 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
2017-02-18 08:14:30 +00:00
|
|
|
std::unique_ptr<NativeVertexFormat>
|
2023-01-27 00:21:09 +00:00
|
|
|
OGLGfx::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl)
|
2008-12-25 15:56:36 +00:00
|
|
|
{
|
2017-02-18 08:14:30 +00:00
|
|
|
return std::make_unique<GLVertexFormat>(vtx_decl);
|
2008-10-24 23:08:46 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2021-06-26 19:48:28 +00:00
|
|
|
static inline GLuint VarToGL(ComponentFormat t)
|
2008-10-28 21:04:14 +00:00
|
|
|
{
|
2021-06-26 19:48:28 +00:00
|
|
|
static constexpr Common::EnumMap<GLuint, ComponentFormat::Float> lookup = {
|
|
|
|
GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, GL_FLOAT,
|
|
|
|
};
|
2008-11-09 13:17:17 +00:00
|
|
|
return lookup[t];
|
2008-10-25 12:35:55 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2022-10-11 21:23:38 +00:00
|
|
|
static void SetPointer(ShaderAttrib attrib, u32 stride, const AttributeFormat& format)
|
2014-01-24 13:46:05 +00:00
|
|
|
{
|
|
|
|
if (!format.enable)
|
|
|
|
return;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-10-11 21:23:38 +00:00
|
|
|
glEnableVertexAttribArray(static_cast<GLuint>(attrib));
|
2014-01-24 13:46:05 +00:00
|
|
|
if (format.integer)
|
2022-10-11 21:23:38 +00:00
|
|
|
glVertexAttribIPointer(static_cast<GLuint>(attrib), format.components, VarToGL(format.type),
|
|
|
|
stride, (u8*)nullptr + format.offset);
|
2014-01-24 13:46:05 +00:00
|
|
|
else
|
2022-10-11 21:23:38 +00:00
|
|
|
glVertexAttribPointer(static_cast<GLuint>(attrib), format.components, VarToGL(format.type),
|
|
|
|
true, stride, (u8*)nullptr + format.offset);
|
2014-01-24 13:46:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
GLVertexFormat::GLVertexFormat(const PortableVertexDeclaration& vtx_decl)
|
|
|
|
: NativeVertexFormat(vtx_decl)
|
2008-10-24 23:08:46 +00:00
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
const u32 vertex_stride = vtx_decl.stride;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-04-03 14:35:49 +00:00
|
|
|
// We will not allow vertex components causing uneven strides.
|
2012-12-15 13:43:01 +00:00
|
|
|
if (vertex_stride & 3)
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Uneven vertex stride: {}", vertex_stride);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2015-12-21 02:49:49 +00:00
|
|
|
VertexManager* const vm = static_cast<VertexManager*>(g_vertex_manager.get());
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2012-12-15 16:28:58 +00:00
|
|
|
glGenVertexArrays(1, &VAO);
|
|
|
|
glBindVertexArray(VAO);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2012-12-15 16:28:58 +00:00
|
|
|
// the element buffer is bound directly to the vao, so we must it set for every vao
|
2017-09-02 22:05:49 +00:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->GetIndexBufferHandle());
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vm->GetVertexBufferHandle());
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2022-10-11 21:23:38 +00:00
|
|
|
SetPointer(ShaderAttrib::Position, vertex_stride, vtx_decl.position);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2022-10-11 21:23:38 +00:00
|
|
|
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
|
|
|
|
2022-10-11 21:23:38 +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
|
|
|
|
2022-10-11 21:23:38 +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
|
|
|
|
2022-10-11 21:23:38 +00:00
|
|
|
SetPointer(ShaderAttrib::PositionMatrix, vertex_stride, vtx_decl.posmtx);
|
2022-11-15 06:38:24 +00:00
|
|
|
|
|
|
|
// Other code shouldn't have to worry about its vertex formats being randomly unbound
|
|
|
|
ProgramShaderCache::ReBindVertexFormat();
|
2008-12-26 17:02:46 +00:00
|
|
|
}
|
|
|
|
|
2015-11-21 09:32:07 +00:00
|
|
|
GLVertexFormat::~GLVertexFormat()
|
|
|
|
{
|
2019-03-05 13:02:40 +00:00
|
|
|
ProgramShaderCache::InvalidateVertexFormatIfBound(VAO);
|
2015-11-21 09:32:07 +00:00
|
|
|
glDeleteVertexArrays(1, &VAO);
|
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
} // namespace OGL
|