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