2012-05-13 17:09:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011-2011 Gregory hainaut
|
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
*
|
|
|
|
* This Program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This Program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
2012-09-09 18:16:11 +00:00
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
2012-05-13 17:09:18 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-03-24 14:03:02 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef ENABLE_OGL_DEBUG_MEM_BW
|
|
|
|
extern uint32 g_vertex_upload_byte;
|
|
|
|
#endif
|
|
|
|
|
2012-05-13 17:09:18 +00:00
|
|
|
struct GSInputLayoutOGL {
|
|
|
|
GLint size;
|
|
|
|
GLenum type;
|
|
|
|
GLboolean normalize;
|
|
|
|
GLsizei stride;
|
|
|
|
const GLvoid* offset;
|
|
|
|
};
|
|
|
|
|
2012-05-13 17:22:36 +00:00
|
|
|
class GSBufferOGL {
|
2013-06-26 20:09:07 +00:00
|
|
|
const size_t m_stride;
|
2012-05-13 17:22:36 +00:00
|
|
|
size_t m_start;
|
|
|
|
size_t m_count;
|
|
|
|
size_t m_limit;
|
2014-03-24 09:49:45 +00:00
|
|
|
const GLenum m_target;
|
|
|
|
GLuint m_buffer_name;
|
|
|
|
uint8* m_buffer_ptr;
|
|
|
|
const bool m_buffer_storage;
|
2012-05-13 17:22:36 +00:00
|
|
|
|
2014-03-24 09:49:45 +00:00
|
|
|
public:
|
|
|
|
GSBufferOGL(GLenum target, size_t stride) :
|
2012-05-13 17:22:36 +00:00
|
|
|
m_stride(stride)
|
|
|
|
, m_start(0)
|
|
|
|
, m_count(0)
|
|
|
|
, m_limit(0)
|
|
|
|
, m_target(target)
|
2014-03-24 09:49:45 +00:00
|
|
|
, m_buffer_storage((theApp.GetConfig("ogl_vertex_storage", 0) == 1) && GLLoader::found_GL_ARB_buffer_storage)
|
2012-05-13 17:22:36 +00:00
|
|
|
{
|
2014-03-24 09:49:45 +00:00
|
|
|
gl_GenBuffers(1, &m_buffer_name);
|
2012-05-13 17:22:36 +00:00
|
|
|
// Opengl works best with 1-4MB buffer.
|
2013-08-17 09:05:41 +00:00
|
|
|
// Warning m_limit is the number of object (not the size in Bytes)
|
2014-03-24 09:49:45 +00:00
|
|
|
m_limit = 2 * 2 * 1024 * 1024 / m_stride;
|
|
|
|
|
|
|
|
if (m_buffer_storage) {
|
2014-03-29 10:24:39 +00:00
|
|
|
#ifndef ENABLE_GLES
|
2014-03-24 09:49:45 +00:00
|
|
|
bind();
|
2015-04-23 19:10:43 +00:00
|
|
|
// coherency will be done by flushing
|
|
|
|
const GLbitfield common_flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT;
|
|
|
|
const GLbitfield map_flags = common_flags | GL_MAP_FLUSH_EXPLICIT_BIT;
|
|
|
|
const GLbitfield create_flags = common_flags
|
2015-04-20 07:25:58 +00:00
|
|
|
// | GL_CLIENT_STORAGE_BIT
|
|
|
|
;
|
2014-03-24 09:49:45 +00:00
|
|
|
|
|
|
|
gl_BufferStorage(m_target, m_stride*m_limit, NULL, create_flags );
|
|
|
|
m_buffer_ptr = (uint8*) gl_MapBufferRange(m_target, 0, m_stride*m_limit, map_flags);
|
2014-03-29 10:24:39 +00:00
|
|
|
#endif
|
2014-03-24 09:49:45 +00:00
|
|
|
} else {
|
|
|
|
m_buffer_ptr = NULL;
|
|
|
|
}
|
2012-05-13 17:22:36 +00:00
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2014-03-24 09:49:45 +00:00
|
|
|
~GSBufferOGL() {
|
|
|
|
if (m_buffer_storage) {
|
|
|
|
bind();
|
|
|
|
gl_UnmapBuffer(m_target);
|
|
|
|
}
|
|
|
|
gl_DeleteBuffers(1, &m_buffer_name);
|
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2013-06-26 20:09:07 +00:00
|
|
|
void allocate() { allocate(m_limit); }
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2012-05-13 17:22:36 +00:00
|
|
|
void allocate(size_t new_limit)
|
|
|
|
{
|
2014-03-24 09:49:45 +00:00
|
|
|
if (!m_buffer_storage) {
|
|
|
|
m_start = 0;
|
|
|
|
m_limit = new_limit;
|
|
|
|
gl_BufferData(m_target, m_limit * m_stride, NULL, GL_STREAM_DRAW);
|
|
|
|
}
|
2012-05-13 17:22:36 +00:00
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2012-05-13 17:22:36 +00:00
|
|
|
void bind()
|
|
|
|
{
|
2014-03-24 09:49:45 +00:00
|
|
|
gl_BindBuffer(m_target, m_buffer_name);
|
2012-05-13 17:22:36 +00:00
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2013-06-26 20:09:07 +00:00
|
|
|
void subdata_upload(const void* src, uint32 count)
|
|
|
|
{
|
|
|
|
m_count = count;
|
|
|
|
|
|
|
|
// Current GPU buffer is really too small need to allocate a new one
|
|
|
|
if (m_count > m_limit) {
|
2013-08-17 09:05:41 +00:00
|
|
|
//fprintf(stderr, "Allocate a new buffer\n %d", m_stride);
|
2013-06-26 20:09:07 +00:00
|
|
|
allocate(std::max<int>(m_count * 3 / 2, m_limit));
|
|
|
|
|
|
|
|
} else if (m_count > (m_limit - m_start) ) {
|
2013-08-17 09:05:41 +00:00
|
|
|
//fprintf(stderr, "Orphan the buffer %d\n", m_stride);
|
|
|
|
|
2013-06-26 20:09:07 +00:00
|
|
|
// Not enough left free room. Just go back at the beginning
|
|
|
|
m_start = 0;
|
|
|
|
// Orphan the buffer to avoid synchronization
|
|
|
|
allocate(m_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl_BufferSubData(m_target, m_stride * m_start, m_stride * m_count, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
void map_upload(const void* src, uint32 count)
|
2012-05-13 17:22:36 +00:00
|
|
|
{
|
|
|
|
void* dst;
|
2015-04-20 07:25:58 +00:00
|
|
|
|
|
|
|
m_count = count;
|
2015-04-21 19:44:50 +00:00
|
|
|
size_t offset = m_start*m_stride;
|
|
|
|
size_t length = m_count*m_stride;
|
2015-04-20 07:25:58 +00:00
|
|
|
|
|
|
|
// Get the pointer of the buffer
|
|
|
|
{
|
|
|
|
// It would need some protection of the data. For the moment finger cross!
|
|
|
|
if (m_count > m_limit) {
|
|
|
|
fprintf(stderr, "Buffer (%x) too small! Please report it upstream\n", m_target);
|
|
|
|
ASSERT(0);
|
|
|
|
} else if (m_count > (m_limit - m_start) ) {
|
|
|
|
//fprintf(stderr, "Wrap buffer (%x)\n", m_target);
|
|
|
|
// Wrap at startup
|
|
|
|
m_start = 0;
|
2015-04-21 19:44:50 +00:00
|
|
|
offset = 0;
|
2014-03-24 09:49:45 +00:00
|
|
|
}
|
2015-04-20 07:25:58 +00:00
|
|
|
|
2015-04-21 19:44:50 +00:00
|
|
|
dst = m_buffer_ptr + offset;
|
2015-04-20 07:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// FIXME which one to use. Note dst doesn't have any aligment guarantee
|
|
|
|
// because it depends of the offset
|
|
|
|
if (m_target == GL_ARRAY_BUFFER) {
|
2015-04-21 19:44:50 +00:00
|
|
|
GSVector4i::storent(dst, src, length);
|
2015-04-20 07:25:58 +00:00
|
|
|
} else {
|
2015-04-21 19:44:50 +00:00
|
|
|
memcpy(dst, src, length);
|
2012-05-13 17:09:18 +00:00
|
|
|
}
|
2015-04-20 07:25:58 +00:00
|
|
|
#else
|
2015-04-21 19:44:50 +00:00
|
|
|
memcpy(dst, src, length);
|
|
|
|
#endif
|
|
|
|
#if 1
|
|
|
|
gl_FlushMappedBufferRange(m_target, offset, length);
|
2015-04-20 07:25:58 +00:00
|
|
|
#endif
|
2012-05-13 17:22:36 +00:00
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2013-07-12 21:12:34 +00:00
|
|
|
#ifdef ENABLE_GLES
|
|
|
|
void upload(const void* src, uint32 count, uint32 basevertex = 0)
|
|
|
|
#else
|
2013-06-26 20:09:07 +00:00
|
|
|
void upload(const void* src, uint32 count)
|
2013-07-12 21:12:34 +00:00
|
|
|
#endif
|
2013-06-26 20:09:07 +00:00
|
|
|
{
|
2013-07-12 21:12:34 +00:00
|
|
|
#ifdef ENABLE_GLES
|
|
|
|
// Emulate gl_DrawElementsBaseVertex... Maybe GLES 4 you know!
|
|
|
|
if (basevertex) {
|
|
|
|
uint32* data = (uint32*) src;
|
|
|
|
for (uint32 i = 0; i < count; i++) {
|
|
|
|
data[i] += basevertex;
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 14:03:02 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_OGL_DEBUG_MEM_BW
|
|
|
|
g_vertex_upload_byte += count*m_stride;
|
2013-07-12 21:12:34 +00:00
|
|
|
#endif
|
2014-03-24 09:49:45 +00:00
|
|
|
if (m_buffer_storage) {
|
2015-04-20 07:25:58 +00:00
|
|
|
map_upload(src, count);
|
2012-05-13 17:22:36 +00:00
|
|
|
} else {
|
2015-04-20 07:25:58 +00:00
|
|
|
subdata_upload(src, count);
|
2014-03-24 09:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2012-05-13 17:22:36 +00:00
|
|
|
void EndScene()
|
|
|
|
{
|
|
|
|
m_start += m_count;
|
|
|
|
m_count = 0;
|
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2012-05-13 17:22:36 +00:00
|
|
|
void Draw(GLenum mode)
|
|
|
|
{
|
|
|
|
glDrawArrays(mode, m_start, m_count);
|
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2012-05-13 17:22:36 +00:00
|
|
|
void Draw(GLenum mode, GLint basevertex)
|
|
|
|
{
|
2013-07-12 21:12:34 +00:00
|
|
|
#ifdef ENABLE_GLES
|
|
|
|
glDrawElements(mode, m_count, GL_UNSIGNED_INT, (void*)(m_start * m_stride));
|
|
|
|
#else
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_DrawElementsBaseVertex(mode, m_count, GL_UNSIGNED_INT, (void*)(m_start * m_stride), basevertex);
|
2013-07-12 21:12:34 +00:00
|
|
|
#endif
|
2012-05-13 17:22:36 +00:00
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2012-05-13 17:22:36 +00:00
|
|
|
void Draw(GLenum mode, GLint basevertex, int offset, int count)
|
|
|
|
{
|
2013-07-12 21:12:34 +00:00
|
|
|
#ifdef ENABLE_GLES
|
|
|
|
glDrawElements(mode, count, GL_UNSIGNED_INT, (void*)((m_start + offset) * m_stride));
|
|
|
|
#else
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_DrawElementsBaseVertex(mode, count, GL_UNSIGNED_INT, (void*)((m_start + offset) * m_stride), basevertex);
|
2013-07-12 21:12:34 +00:00
|
|
|
#endif
|
2012-05-13 17:22:36 +00:00
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2012-05-13 17:22:36 +00:00
|
|
|
size_t GetStart() { return m_start; }
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2014-03-24 09:49:45 +00:00
|
|
|
void debug()
|
2012-05-13 17:22:36 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "data buffer: start %d, count %d\n", m_start, m_count);
|
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
2012-05-13 17:22:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class GSVertexBufferStateOGL {
|
|
|
|
GSBufferOGL *m_vb;
|
|
|
|
GSBufferOGL *m_ib;
|
2012-05-13 17:09:18 +00:00
|
|
|
|
|
|
|
GLuint m_va;
|
|
|
|
GLenum m_topology;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSVertexBufferStateOGL(size_t stride, GSInputLayoutOGL* layout, uint32 layout_nbr)
|
|
|
|
{
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_GenVertexArrays(1, &m_va);
|
2012-05-13 17:09:18 +00:00
|
|
|
|
|
|
|
m_vb = new GSBufferOGL(GL_ARRAY_BUFFER, stride);
|
|
|
|
m_ib = new GSBufferOGL(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32));
|
|
|
|
|
|
|
|
bind();
|
2014-03-24 09:49:45 +00:00
|
|
|
// Note: index array are part of the VA state so it need to be bound only once.
|
2012-05-13 17:09:18 +00:00
|
|
|
m_ib->bind();
|
|
|
|
|
|
|
|
m_vb->allocate();
|
|
|
|
m_ib->allocate();
|
|
|
|
set_internal_format(layout, layout_nbr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bind()
|
|
|
|
{
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_BindVertexArray(m_va);
|
2012-05-13 17:09:18 +00:00
|
|
|
m_vb->bind();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_internal_format(GSInputLayoutOGL* layout, uint32 layout_nbr)
|
|
|
|
{
|
2013-01-10 13:13:59 +00:00
|
|
|
for (uint32 i = 0; i < layout_nbr; i++) {
|
2012-05-13 17:09:18 +00:00
|
|
|
// Note this function need both a vertex array object and a GL_ARRAY_BUFFER buffer
|
2014-10-02 07:33:41 +00:00
|
|
|
gl_EnableVertexAttribArray(i);
|
2012-05-13 17:09:18 +00:00
|
|
|
switch (layout[i].type) {
|
|
|
|
case GL_UNSIGNED_SHORT:
|
|
|
|
case GL_UNSIGNED_INT:
|
2014-01-31 21:30:54 +00:00
|
|
|
if (layout[i].normalize) {
|
2014-10-02 07:33:41 +00:00
|
|
|
gl_VertexAttribPointer(i, layout[i].size, layout[i].type, layout[i].normalize, layout[i].stride, layout[i].offset);
|
2014-01-31 21:30:54 +00:00
|
|
|
} else {
|
|
|
|
// Rule: when shader use integral (not normalized) you must use gl_VertexAttribIPointer (note the extra I)
|
2014-10-02 07:33:41 +00:00
|
|
|
gl_VertexAttribIPointer(i, layout[i].size, layout[i].type, layout[i].stride, layout[i].offset);
|
2014-01-31 21:30:54 +00:00
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-10-02 07:33:41 +00:00
|
|
|
gl_VertexAttribPointer(i, layout[i].size, layout[i].type, layout[i].normalize, layout[i].stride, layout[i].offset);
|
2012-05-13 17:09:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EndScene()
|
|
|
|
{
|
|
|
|
m_vb->EndScene();
|
|
|
|
m_ib->EndScene();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawPrimitive() { m_vb->Draw(m_topology); }
|
|
|
|
|
|
|
|
void DrawIndexedPrimitive() { m_ib->Draw(m_topology, m_vb->GetStart() ); }
|
|
|
|
|
|
|
|
void DrawIndexedPrimitive(int offset, int count) { m_ib->Draw(m_topology, m_vb->GetStart(), offset, count ); }
|
|
|
|
|
|
|
|
void SetTopology(GLenum topology) { m_topology = topology; }
|
|
|
|
|
|
|
|
void UploadVB(const void* vertices, size_t count) { m_vb->upload(vertices, count); }
|
|
|
|
|
2013-07-12 21:12:34 +00:00
|
|
|
void UploadIB(const void* index, size_t count) {
|
|
|
|
#ifdef ENABLE_GLES
|
|
|
|
m_ib->upload(index, count, m_vb->GetStart());
|
|
|
|
#else
|
|
|
|
m_ib->upload(index, count);
|
|
|
|
#endif
|
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
|
|
|
|
~GSVertexBufferStateOGL()
|
|
|
|
{
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_DeleteVertexArrays(1, &m_va);
|
2012-05-13 17:22:36 +00:00
|
|
|
delete m_vb;
|
|
|
|
delete m_ib;
|
2012-05-13 17:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void debug()
|
|
|
|
{
|
|
|
|
string topo;
|
|
|
|
switch (m_topology) {
|
|
|
|
case GL_POINTS:
|
|
|
|
topo = "point";
|
|
|
|
break;
|
|
|
|
case GL_LINES:
|
|
|
|
topo = "line";
|
|
|
|
break;
|
2014-03-24 09:49:45 +00:00
|
|
|
case GL_TRIANGLES:
|
2012-05-13 17:09:18 +00:00
|
|
|
topo = "triangle";
|
|
|
|
break;
|
|
|
|
case GL_TRIANGLE_STRIP:
|
|
|
|
topo = "triangle strip";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_vb->debug();
|
|
|
|
m_ib->debug();
|
|
|
|
fprintf(stderr, "primitives of %s\n", topo.c_str());
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|