2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2017-02-01 15:56:13 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-06-24 08:43:46 +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
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
#include "VideoBackends/OGL/VertexManager.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/NativeVertexFormat.h"
|
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
// Here's some global state. We only use this to keep track of what we've sent to the OpenGL state
|
|
|
|
// machine.
|
|
|
|
|
2010-11-26 09:25:08 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
2017-02-18 08:14:30 +00:00
|
|
|
std::unique_ptr<NativeVertexFormat>
|
2016-06-24 08:43:46 +00:00
|
|
|
VertexManager::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-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 18:16:04 +00:00
|
|
|
static inline GLuint VarToGL(VarType t)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
static const GLuint lookup[5] = {GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT,
|
|
|
|
GL_FLOAT};
|
|
|
|
return lookup[t];
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
static void SetPointer(u32 attrib, u32 stride, const AttributeFormat& format)
|
2014-01-24 13:46:05 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
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);
|
2014-01-24 13:46:05 +00:00
|
|
|
}
|
|
|
|
|
2015-11-21 09:32:07 +00:00
|
|
|
GLVertexFormat::GLVertexFormat(const PortableVertexDeclaration& _vtx_decl)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
this->vtx_decl = _vtx_decl;
|
|
|
|
u32 vertex_stride = _vtx_decl.stride;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// We will not allow vertex components causing uneven strides.
|
|
|
|
if (vertex_stride & 3)
|
|
|
|
PanicAlert("Uneven vertex stride: %i", vertex_stride);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
VertexManager* const vm = static_cast<VertexManager*>(g_vertex_manager.get());
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
glGenVertexArrays(1, &VAO);
|
|
|
|
glBindVertexArray(VAO);
|
2017-07-20 05:25:29 +00:00
|
|
|
ProgramShaderCache::BindVertexFormat(this);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// the element buffer is bound directly to the vao, so we must it set for every vao
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->m_index_buffers);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vm->m_vertex_buffers);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
SetPointer(SHADER_POSITION_ATTRIB, vertex_stride, _vtx_decl.position);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
SetPointer(SHADER_NORM0_ATTRIB + i, vertex_stride, _vtx_decl.normals[i]);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
SetPointer(SHADER_COLOR0_ATTRIB + i, vertex_stride, _vtx_decl.colors[i]);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
SetPointer(SHADER_TEXTURE0_ATTRIB + i, vertex_stride, _vtx_decl.texcoords[i]);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
SetPointer(SHADER_POSMTX_ATTRIB, vertex_stride, _vtx_decl.posmtx);
|
2008-12-26 17:02:46 +00:00
|
|
|
}
|
|
|
|
|
2015-11-21 09:32:07 +00:00
|
|
|
GLVertexFormat::~GLVertexFormat()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
glDeleteVertexArrays(1, &VAO);
|
2015-11-21 09:32:07 +00:00
|
|
|
}
|
2008-12-26 17:02:46 +00:00
|
|
|
}
|