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"
|
2015-09-18 16:40:00 +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"
|
2019-02-15 01:59:50 +00:00
|
|
|
#include "VideoBackends/OGL/Render.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#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>
|
2019-02-15 01:59:50 +00:00
|
|
|
Renderer::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
|
|
|
{
|
2014-01-24 13:18:55 +00:00
|
|
|
static const GLuint lookup[5] = {GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT,
|
|
|
|
GL_FLOAT};
|
2008-12-08 05:25:12 +00:00
|
|
|
return lookup[t];
|
|
|
|
}
|
|
|
|
|
2014-01-24 13:46:05 +00:00
|
|
|
static void SetPointer(u32 attrib, u32 stride, const AttributeFormat& format)
|
|
|
|
{
|
|
|
|
if (!format.enable)
|
|
|
|
return;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-24 13:46:05 +00:00
|
|
|
glEnableVertexAttribArray(attrib);
|
|
|
|
if (format.integer)
|
2014-03-09 20:14:26 +00:00
|
|
|
glVertexAttribIPointer(attrib, format.components, VarToGL(format.type), stride,
|
|
|
|
(u8*)nullptr + format.offset);
|
2014-01-24 13:46:05 +00:00
|
|
|
else
|
2014-03-09 20:14:26 +00:00
|
|
|
glVertexAttribPointer(attrib, format.components, VarToGL(format.type), true, stride,
|
|
|
|
(u8*)nullptr + format.offset);
|
2014-01-24 13:46:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
GLVertexFormat::GLVertexFormat(const PortableVertexDeclaration& vtx_decl)
|
|
|
|
: NativeVertexFormat(vtx_decl)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
u32 vertex_stride = vtx_decl.stride;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-04-03 14:35:49 +00:00
|
|
|
// We will not allow vertex components causing uneven strides.
|
2013-10-29 05:23:17 +00:00
|
|
|
if (vertex_stride & 3)
|
2012-12-15 13:43:01 +00:00
|
|
|
PanicAlert("Uneven vertex stride: %i", vertex_stride);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2015-12-21 02:49:49 +00:00
|
|
|
VertexManager* const vm = static_cast<VertexManager*>(g_vertex_manager.get());
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2012-12-15 16:28:58 +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
|
|
|
|
2012-12-15 16:28:58 +00:00
|
|
|
// the element buffer is bound directly to the vao, so we must it set for every vao
|
2017-09-02 22:05:49 +00:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->GetIndexBufferHandle());
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vm->GetVertexBufferHandle());
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
SetPointer(SHADER_POSITION_ATTRIB, vertex_stride, vtx_decl.position);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-08-15 18:09:53 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
2019-02-15 01:59:50 +00:00
|
|
|
SetPointer(SHADER_NORM0_ATTRIB + i, vertex_stride, vtx_decl.normals[i]);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2014-08-15 18:09:53 +00:00
|
|
|
for (int i = 0; i < 2; i++)
|
2019-02-15 01:59:50 +00:00
|
|
|
SetPointer(SHADER_COLOR0_ATTRIB + i, vertex_stride, vtx_decl.colors[i]);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2014-08-15 18:09:53 +00:00
|
|
|
for (int i = 0; i < 8; i++)
|
2019-02-15 01:59:50 +00:00
|
|
|
SetPointer(SHADER_TEXTURE0_ATTRIB + i, vertex_stride, vtx_decl.texcoords[i]);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2019-02-15 01:59:50 +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()
|
|
|
|
{
|
2019-03-05 13:02:40 +00:00
|
|
|
ProgramShaderCache::InvalidateVertexFormatIfBound(VAO);
|
2015-11-21 09:32:07 +00:00
|
|
|
glDeleteVertexArrays(1, &VAO);
|
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
} // namespace OGL
|