summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/OGL/OGLNativeVertexFormat.cpp
diff options
context:
space:
mode:
authorShawn Hoffman <godisgovernment@gmail.com>2020-09-15 05:00:24 -0700
committerShawn Hoffman <godisgovernment@gmail.com>2021-01-27 14:29:48 -0800
commitff4f67492bff68d8b34aebc9ea6c85ca34e02b71 (patch)
treef5274db377aa0af509a3095ecfbae266b008d6ae /Source/Core/VideoBackends/OGL/OGLNativeVertexFormat.cpp
parent3193f3d5c4c699ac5396a587ac782ef014bad49e (diff)
normalize common filenames in VideoBackends/OGL
Diffstat (limited to 'Source/Core/VideoBackends/OGL/OGLNativeVertexFormat.cpp')
-rw-r--r--Source/Core/VideoBackends/OGL/OGLNativeVertexFormat.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/Source/Core/VideoBackends/OGL/OGLNativeVertexFormat.cpp b/Source/Core/VideoBackends/OGL/OGLNativeVertexFormat.cpp
new file mode 100644
index 0000000000..09114fb974
--- /dev/null
+++ b/Source/Core/VideoBackends/OGL/OGLNativeVertexFormat.cpp
@@ -0,0 +1,86 @@
+// Copyright 2008 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#include "Common/CommonTypes.h"
+#include "Common/GL/GLUtil.h"
+#include "Common/MsgHandler.h"
+
+#include "VideoBackends/OGL/OGLRender.h"
+#include "VideoBackends/OGL/OGLVertexManager.h"
+#include "VideoBackends/OGL/ProgramShaderCache.h"
+
+#include "VideoCommon/NativeVertexFormat.h"
+#include "VideoCommon/VertexShaderGen.h"
+
+// Here's some global state. We only use this to keep track of what we've sent to the OpenGL state
+// machine.
+
+namespace OGL
+{
+std::unique_ptr<NativeVertexFormat>
+Renderer::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl)
+{
+ return std::make_unique<GLVertexFormat>(vtx_decl);
+}
+
+static inline GLuint VarToGL(VarType t)
+{
+ static const GLuint lookup[5] = {GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT,
+ GL_FLOAT};
+ 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*)nullptr + format.offset);
+ else
+ glVertexAttribPointer(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;
+
+ // We will not allow vertex components causing uneven strides.
+ 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);
+ ProgramShaderCache::BindVertexFormat(this);
+
+ // 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());
+
+ SetPointer(SHADER_POSITION_ATTRIB, vertex_stride, vtx_decl.position);
+
+ for (int i = 0; i < 3; i++)
+ SetPointer(SHADER_NORM0_ATTRIB + i, vertex_stride, vtx_decl.normals[i]);
+
+ for (int i = 0; i < 2; i++)
+ SetPointer(SHADER_COLOR0_ATTRIB + i, vertex_stride, vtx_decl.colors[i]);
+
+ for (int i = 0; i < 8; i++)
+ SetPointer(SHADER_TEXTURE0_ATTRIB + i, vertex_stride, vtx_decl.texcoords[i]);
+
+ SetPointer(SHADER_POSMTX_ATTRIB, vertex_stride, vtx_decl.posmtx);
+}
+
+GLVertexFormat::~GLVertexFormat()
+{
+ ProgramShaderCache::InvalidateVertexFormatIfBound(VAO);
+ glDeleteVertexArrays(1, &VAO);
+}
+} // namespace OGL