summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/OGL/NativeVertexFormat.cpp
blob: df24809ae044fc3e0438abb6109e1cc264c8cd76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 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/ProgramShaderCache.h"
#include "VideoBackends/OGL/VertexManager.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>
VertexManager::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)
{
  this->vtx_decl = _vtx_decl;
  u32 vertex_stride = _vtx_decl.stride;

  // We will not allow vertex components causing uneven strides.
  if (vertex_stride & 3)
    PanicAlert("Uneven vertex stride: %i", 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()
{
  glDeleteVertexArrays(1, &VAO);
}
}