16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-22 17:29:18 +02:00

use pbo for picking buffers, restore picking for scenery nodes

This commit is contained in:
milek7
2018-10-23 17:04:04 +02:00
parent cdd79e605c
commit 0c460e9e0d
10 changed files with 165 additions and 53 deletions

View File

@@ -64,4 +64,19 @@ void gl::buffer::upload(targets target, const void *data, int offset, int size)
glBufferSubData(glenum_target(target), offset, size, data);
}
void gl::buffer::download(targets target, void *data, int offset, int size)
{
bind(target);
if (GLAD_GL_VERSION_3_3)
{
glGetBufferSubData(glenum_target(target), offset, size, data);
}
else
{
void *glbuf = glMapBufferRange(glenum_target(target), offset, size, GL_MAP_READ_BIT);
memcpy(data, glbuf, size);
glUnmapBuffer(glenum_target(target));
}
}
GLuint gl::buffer::binding_points[13];

View File

@@ -40,5 +40,6 @@ namespace gl
void allocate(targets target, int size, GLenum hint);
void upload(targets target, const void *data, int offset, int size);
void download(targets target, void *data, int offset, int size);
};
}

View File

@@ -12,7 +12,8 @@ gl::fence::~fence()
bool gl::fence::is_signalled()
{
GLsizei len = 0;
GLint val;
glGetSynciv(sync, GL_SYNC_STATUS, 1, &val);
return val == GL_SIGNALED;
glGetSynciv(sync, GL_SYNC_STATUS, 1, &len, &val);
return len == 1 && val == GL_SIGNALED;
}

View File

@@ -11,5 +11,8 @@ namespace gl
~fence();
bool is_signalled();
fence(const fence&) = delete;
fence& operator=(const fence&) = delete;
};
}

51
gl/pbo.cpp Normal file
View File

@@ -0,0 +1,51 @@
#include "pbo.h"
void gl::pbo::request_read(int x, int y, int lx, int ly)
{
int s = lx * ly * 4;
if (s != size)
allocate(PIXEL_PACK_BUFFER, s, GL_STREAM_DRAW);
size = s;
data_ready = false;
sync.reset();
bind(PIXEL_PACK_BUFFER);
glReadPixels(x, y, lx, ly, GL_RGBA, GL_UNSIGNED_BYTE, 0);
unbind(PIXEL_PACK_BUFFER);
sync.emplace();
}
bool gl::pbo::read_data(int lx, int ly, uint8_t *data)
{
is_busy();
if (!data_ready)
return false;
int s = lx * ly * 4;
if (s != size)
return false;
download(PIXEL_PACK_BUFFER, data, 0, s);
unbind(PIXEL_PACK_BUFFER);
data_ready = false;
return true;
}
bool gl::pbo::is_busy()
{
if (!sync)
return false;
if (sync->is_signalled())
{
data_ready = true;
sync.reset();
return false;
}
return true;
}

17
gl/pbo.h Normal file
View File

@@ -0,0 +1,17 @@
#include "buffer.h"
#include "fence.h"
#include <optional>
namespace gl {
class pbo : private buffer
{
std::optional<fence> sync;
int size = 0;
bool data_ready;
public:
void request_read(int x, int y, int lx, int ly);
bool read_data(int lx, int ly, uint8_t *data);
bool is_busy();
};
}