Debian Codegraph @

Source/Core/VideoBackends/OGL/OGLNativeVertexFormat.cpp

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "Common/CommonTypes.h"
5 #include "Common/EnumMap.h"
6 #include "Common/MsgHandler.h"
7
8 #include "VideoBackends/OGL/OGLGfx.h"
9 #include "VideoBackends/OGL/OGLVertexManager.h"
10 #include "VideoBackends/OGL/ProgramShaderCache.h"
11
12 #include "VideoCommon/NativeVertexFormat.h"
13 #include "VideoCommon/VertexShaderGen.h"
14
15 // Here's some global state. We only use this to keep track of what we've sent to the OpenGL state
16 // machine.
17
18 namespace OGL
19 {
20 std::unique_ptr<NativeVertexFormat>
21 OGLGfx::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl)
22 {
23 return std::make_unique<GLVertexFormat>(vtx_decl);
24 }
25
26 static inline GLuint VarToGL(ComponentFormat t)
27 {
28 static constexpr Common::EnumMap<GLuint, ComponentFormat::InvalidFloat7> lookup = {
29 GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT,
30 GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_FLOAT,
31 };
32 return lookup[t];
33 }
34
35 static void SetPointer(ShaderAttrib attrib, u32 stride, const AttributeFormat& format)
36 {
37 if (!format.enable)
38 return;
39
40 glEnableVertexAttribArray(static_cast<GLuint>(attrib));
41 if (format.integer)
42 glVertexAttribIPointer(static_cast<GLuint>(attrib), format.components, VarToGL(format.type),
43 stride, (u8*)nullptr + format.offset);
44 else
45 glVertexAttribPointer(static_cast<GLuint>(attrib), format.components, VarToGL(format.type),
46 true, stride, (u8*)nullptr + format.offset);
47 }
48
49 GLVertexFormat::GLVertexFormat(const PortableVertexDeclaration& vtx_decl)
50 : NativeVertexFormat(vtx_decl)
51 {
52 const u32 vertex_stride = vtx_decl.stride;
53
54 // We will not allow vertex components causing uneven strides.
55 if (vertex_stride & 3)
56 PanicAlertFmt("Uneven vertex stride: {}", vertex_stride);
57
58 VertexManager* const vm = static_cast<VertexManager*>(g_vertex_manager.get());
59
60 glGenVertexArrays(1, &VAO);
61 glBindVertexArray(VAO);
62
63 // the element buffer is bound directly to the vao, so we must it set for every vao
64 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->GetIndexBufferHandle());
65 glBindBuffer(GL_ARRAY_BUFFER, vm->GetVertexBufferHandle());
66
67 SetPointer(ShaderAttrib::Position, vertex_stride, vtx_decl.position);
68
69 for (u32 i = 0; i < 3; i++)
70 SetPointer(ShaderAttrib::Normal + i, vertex_stride, vtx_decl.normals[i]);
71
72 for (u32 i = 0; i < 2; i++)
73 SetPointer(ShaderAttrib::Color0 + i, vertex_stride, vtx_decl.colors[i]);
74
75 for (u32 i = 0; i < 8; i++)
76 SetPointer(ShaderAttrib::TexCoord0 + i, vertex_stride, vtx_decl.texcoords[i]);
77
78 SetPointer(ShaderAttrib::PositionMatrix, vertex_stride, vtx_decl.posmtx);
79
80 // Other code shouldn't have to worry about its vertex formats being randomly unbound
81 ProgramShaderCache::ReBindVertexFormat();
82 }
83
84 GLVertexFormat::~GLVertexFormat()
85 {
86 ProgramShaderCache::InvalidateVertexFormatIfBound(VAO);
87 glDeleteVertexArrays(1, &VAO);
88 }
89 } // namespace OGL