Skip to content

Commit 98fa60b

Browse files
committed
- Updates to storage buffer
- Added storage buffer mapping
1 parent c5520b4 commit 98fa60b

File tree

3 files changed

+142
-62
lines changed

3 files changed

+142
-62
lines changed

gs.h

+22-6
Original file line numberDiff line numberDiff line change
@@ -4960,6 +4960,7 @@ typedef struct gs_uuid_t
49604960

49614961
#define GS_WINDOW_FLAGS_NO_RESIZE 0x01
49624962
#define GS_WINDOW_FLAGS_FULLSCREEN 0x02
4963+
#define GS_WINDOW_FLAGS_INVISIBLE 0x03
49634964

49644965
// Should have an internal resource cache of window handles (controlled by the platform api)
49654966

@@ -5511,6 +5512,8 @@ GS_API_DECL void gs_platform_add_event(gs_platform_event_t* evt);
55115512
// Platform Window
55125513
GS_API_DECL uint32_t gs_platform_window_create(const gs_platform_window_desc_t* desc);
55135514
GS_API_DECL uint32_t gs_platform_main_window();
5515+
GS_API_DECL void gs_platform_window_make_current(uint32_t hndl); // Binds context (main thread only)
5516+
GS_API_DECL void gs_platform_window_make_current_raw(void* win); // Binds context (can be on separate thread with raw handle)
55145517

55155518
typedef struct gs_platform_file_stats_s
55165519
{
@@ -6257,9 +6260,14 @@ typedef struct gs_graphics_bind_uniform_buffer_desc_t {
62576260
} range;
62586261
} gs_graphics_bind_uniform_buffer_desc_t;
62596262

6263+
// All this needs to be unified...
62606264
typedef struct gs_graphics_bind_storage_buffer_desc_t {
62616265
gs_handle(gs_graphics_storage_buffer_t) buffer;
62626266
uint32_t binding;
6267+
struct {
6268+
size_t offset; // Specify an offset for ranged binds.
6269+
size_t size; // Specify size for ranged binds.
6270+
} range;
62636271
} gs_graphics_bind_storage_buffer_desc_t;
62646272

62656273
typedef struct gs_graphics_bind_uniform_desc_t {
@@ -6452,8 +6460,9 @@ typedef struct gs_graphics_t
64526460

64536461
// Util
64546462
void* (* storage_buffer_map_get)(gs_handle(gs_graphics_storage_buffer_t) hndl);
6455-
void* (* storage_buffer_lock)(gs_handle(gs_graphics_storage_buffer_t) hndl);
6456-
void (* storage_buffer_unlock)(gs_handle(gs_graphics_storage_buffer_t) hndl);
6463+
void* (* storage_buffer_lock)(gs_handle(gs_graphics_storage_buffer_t) hndl, size_t offset, size_t sz);
6464+
void (* storage_buffer_unlock)(gs_handle(gs_graphics_storage_buffer_t) hndl);
6465+
void (* storage_buffer_get_data)(gs_handle(gs_graphics_storage_buffer_t) hndl, size_t offset, size_t stride, void* out);
64576466

64586467
// Submission (Main Thread)
64596468
void (* command_buffer_submit)(gs_command_buffer_t* cb);
@@ -6515,8 +6524,9 @@ GS_API_DECL size_t gs_graphics_uniform_size_query(gs_handle(gs_graphics_uniform_
65156524

65166525
// Util
65176526
GS_API_DECL void* gs_graphics_storage_buffer_map_get(gs_handle(gs_graphics_storage_buffer_t) hndl);
6518-
GS_API_DECL void* gs_graphics_storage_buffer_lock(gs_handle(gs_graphics_storage_buffer_t) hndl);
6519-
GS_API_DECL void gs_graphics_storage_buffer_unlock(gs_handle(gs_graphics_storage_buffer_t) hndl);
6527+
GS_API_DECL void* gs_graphics_storage_buffer_lock(gs_handle(gs_graphics_storage_buffer_t) hndl, size_t offset, size_t sz);
6528+
GS_API_DECL void gs_graphics_storage_buffer_unlock(gs_handle(gs_graphics_storage_buffer_t) hndl);
6529+
GS_API_DECL void gs_graphics_storage_buffer_get_data(gs_handle(gs_graphics_storage_buffer_t) hndl, size_t offset, size_t stride, void* out);
65206530

65216531
// Resource In-Flight Update
65226532
GS_API_DECL void gs_graphics_texture_request_update(gs_command_buffer_t* cb, gs_handle(gs_graphics_texture_t) hndl, gs_graphics_texture_desc_t* desc);
@@ -7004,9 +7014,15 @@ gs_grapics_storage_buffer_unlock(gs_handle(gs_graphics_storage_buffer_t) hndl)
70047014
}
70057015

70067016
GS_API_DECL void*
7007-
gs_grapics_storage_buffer_lock(gs_handle(gs_graphics_storage_buffer_t) hndl)
7017+
gs_graphics_storage_buffer_lock(gs_handle(gs_graphics_storage_buffer_t) hndl, size_t offset, size_t sz)
70087018
{
7009-
return gs_graphics()->api.storage_buffer_lock(hndl);
7019+
return gs_graphics()->api.storage_buffer_lock(hndl, offset, sz);
7020+
}
7021+
7022+
GS_API_DECL void
7023+
gs_graphics_storage_buffer_get_data(gs_handle(gs_graphics_storage_buffer_t) hndl, size_t offset, size_t sz, void* out)
7024+
{
7025+
return gs_graphics()->api.storage_buffer_get_data(hndl, offset, sz, out);
70107026
}
70117027

70127028
/*=============================

impl/gs_graphics_impl.h

+49-3
Original file line numberDiff line numberDiff line change
@@ -1512,10 +1512,16 @@ gs_grapics_storage_buffer_unlock_impl(gs_handle(gs_graphics_storage_buffer_t) hn
15121512
}
15131513
}
15141514
}
1515+
1516+
// Check for persistence here?...
1517+
if (sbo->map) {
1518+
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); // Do this via DSA instead...
1519+
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
1520+
}
15151521
}
15161522

15171523
GS_API_DECL void*
1518-
gs_grapics_storage_buffer_lock_impl(gs_handle(gs_graphics_storage_buffer_t) hndl)
1524+
gs_grapics_storage_buffer_lock_impl(gs_handle(gs_graphics_storage_buffer_t) hndl, size_t offset, size_t sz)
15191525
{
15201526
// Lock
15211527
gsgl_data_t* ogl = (gsgl_data_t*)gs_subsystem(graphics)->user_data;
@@ -1529,6 +1535,7 @@ gs_grapics_storage_buffer_lock_impl(gs_handle(gs_graphics_storage_buffer_t) hndl
15291535
if (sbo->sync) {
15301536
glDeleteSync(sbo->sync);
15311537
}
1538+
// Not sure if this is correct...
15321539
sbo->sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
15331540
if (sbo->sync) {
15341541
while (1) {
@@ -1538,9 +1545,40 @@ gs_grapics_storage_buffer_lock_impl(gs_handle(gs_graphics_storage_buffer_t) hndl
15381545
}
15391546
}
15401547
}
1548+
if (!sbo->map) {
1549+
// Get buffer size
1550+
glBindBuffer(GL_SHADER_STORAGE_BUFFER, sbo->buffer);
1551+
size_t buffer_sz = 0;
1552+
glGetBufferParameteriv(GL_SHADER_STORAGE_BUFFER, GL_BUFFER_SIZE, &buffer_sz);
1553+
gs_println("SZ: %zu, requested: %zu", buffer_sz, sz);
1554+
sbo->map = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, (GLintptr)offset, (GLsizeiptr)sz, GL_MAP_READ_BIT);
1555+
GLenum err = glGetError();
1556+
if (err) {
1557+
gs_println("GL ERROR: 0x%x: %s", err, glGetString(err));
1558+
}
1559+
}
15411560
return sbo->map;
15421561
}
15431562

1563+
GS_API_DECL void
1564+
gs_storage_buffer_get_data_impl(gs_handle(gs_graphics_storage_buffer_t) hndl, size_t offset, size_t stride, void* out)
1565+
{
1566+
// Lock
1567+
gsgl_data_t* ogl = (gsgl_data_t*)gs_subsystem(graphics)->user_data;
1568+
if (!gs_slot_array_handle_valid(ogl->storage_buffers, hndl.id)) {
1569+
gs_log_warning("Storage buffer handle invalid: %zu", hndl.id);
1570+
return NULL;
1571+
}
1572+
gsgl_storage_buffer_t* sbo = gs_slot_array_getp(ogl->storage_buffers, hndl.id);
1573+
glBindBuffer(GL_SHADER_STORAGE_BUFFER, sbo->buffer);
1574+
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, stride, out);
1575+
// GLenum err = glGetError();
1576+
// if (err) {
1577+
// gs_println("GL ERROR: 0x%x: %s", err, glGetString(err));
1578+
// }
1579+
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
1580+
}
1581+
15441582
#define __ogl_push_command(CB, OP_CODE, ...)\
15451583
do {\
15461584
gsgl_data_t* DATA = (gsgl_data_t*)gs_subsystem(graphics)->user_data;\
@@ -1799,6 +1837,8 @@ void gs_graphics_apply_bindings(gs_command_buffer_t* cb, gs_graphics_bind_desc_t
17991837
gs_byte_buffer_write(&cb->commands, gs_graphics_bind_type, GS_GRAPHICS_BIND_STORAGE_BUFFER);
18001838
gs_byte_buffer_write(&cb->commands, uint32_t, decl->buffer.id);
18011839
gs_byte_buffer_write(&cb->commands, uint32_t, decl->binding);
1840+
gs_byte_buffer_write(&cb->commands, size_t, decl->range.offset);
1841+
gs_byte_buffer_write(&cb->commands, size_t, decl->range.size);
18021842
}
18031843
);
18041844
};
@@ -2315,6 +2355,8 @@ void gs_graphics_command_buffer_submit_impl(gs_command_buffer_t* cb)
23152355
{
23162356
gs_byte_buffer_readc(&cb->commands, uint32_t, sb_slot_id);
23172357
gs_byte_buffer_readc(&cb->commands, uint32_t, binding);
2358+
gs_byte_buffer_readc(&cb->commands, size_t, range_offset);
2359+
gs_byte_buffer_readc(&cb->commands, size_t, range_size);
23182360

23192361
// Grab storage buffer from id
23202362
if (!sb_slot_id || !gs_slot_array_exists(ogl->storage_buffers, sb_slot_id)) {
@@ -2383,7 +2425,9 @@ void gs_graphics_command_buffer_submit_impl(gs_command_buffer_t* cb)
23832425

23842426
// This is required
23852427
CHECK_GL_CORE(
2386-
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, sbo->buffer);
2428+
// glBindBufferRange(GL_SHADER_STORAGE_BUFFER, binding, sbo->buffer,
2429+
// range_offset, range_size ? range_size : sbo->size - range_offset);
2430+
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, sbo->buffer);
23872431
);
23882432

23892433
} break;
@@ -2557,6 +2601,7 @@ void gs_graphics_command_buffer_submit_impl(gs_command_buffer_t* cb)
25572601
// Memory barrier (TODO(john): make this specifically set in the pipeline state)
25582602
glDispatchCompute(num_x_groups, num_y_groups, num_z_groups);
25592603
glMemoryBarrier(GL_ALL_BARRIER_BITS);
2604+
// glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
25602605
)
25612606
} break;
25622607

@@ -2909,7 +2954,8 @@ gs_graphics_init(gs_graphics_t* graphics)
29092954
// Util
29102955
graphics->api.storage_buffer_map_get = gs_graphics_storage_buffer_map_get_impl;
29112956
graphics->api.storage_buffer_lock = gs_grapics_storage_buffer_lock_impl;
2912-
graphics->api.storage_buffer_unlock = gs_grapics_storage_buffer_unlock_impl;
2957+
graphics->api.storage_buffer_unlock = gs_grapics_storage_buffer_unlock_impl;
2958+
graphics->api.storage_buffer_get_data = gs_storage_buffer_get_data_impl;
29132959

29142960
// Submission (Main Thread)
29152961
graphics->api.command_buffer_submit = gs_graphics_command_buffer_submit_impl;

impl/gs_platform_impl.h

+71-53
Original file line numberDiff line numberDiff line change
@@ -1700,78 +1700,82 @@ gs_platform_window_create_internal(const gs_platform_window_desc_t* desc)
17001700

17011701
// Grab window hints from application desc
17021702
u32 window_hints = desc->flags;
1703+
bool visible = ~window_hints & GS_WINDOW_FLAGS_INVISIBLE;
17031704

17041705
// Set whether or not the screen is resizable
1705-
glfwWindowHint(GLFW_RESIZABLE, (window_hints & GS_WINDOW_FLAGS_NO_RESIZE) != GS_WINDOW_FLAGS_NO_RESIZE);
1706+
glfwWindowHint(GLFW_RESIZABLE, (window_hints & GS_WINDOW_FLAGS_NO_RESIZE) != GS_WINDOW_FLAGS_NO_RESIZE);
1707+
glfwWindowHint(GLFW_VISIBLE, visible);
1708+
GLFWwindow* window = NULL;
1709+
1710+
#define CONSTRUCT_WINDOW(W, H, T, M, I)\
1711+
do {\
1712+
window = glfwCreateWindow(W, H, T, M, I);\
1713+
win.hndl = window;\
1714+
} while (0)
1715+
1716+
if (visible) {
1717+
// Set multi-samples
1718+
if (desc->num_samples) {
1719+
glfwWindowHint(GLFW_SAMPLES, desc->num_samples);
1720+
}
1721+
else {
1722+
glfwWindowHint(GLFW_SAMPLES, 0);
1723+
}
17061724

1707-
// Set multi-samples
1708-
if (desc->num_samples) {
1709-
glfwWindowHint(GLFW_SAMPLES, desc->num_samples);
1710-
}
1711-
else {
1712-
glfwWindowHint(GLFW_SAMPLES, 0);
1713-
}
1725+
// Get monitor if fullscreen
1726+
GLFWmonitor* monitor = NULL;
1727+
if ((window_hints & GS_WINDOW_FLAGS_FULLSCREEN) == GS_WINDOW_FLAGS_FULLSCREEN) {
1728+
int monitor_count;
1729+
GLFWmonitor** monitors = glfwGetMonitors(&monitor_count);
1730+
if (desc->monitor_index < monitor_count) {
1731+
monitor = monitors[desc->monitor_index];
1732+
}
1733+
}
1734+
CONSTRUCT_WINDOW(desc->width, desc->height, desc->title, monitor, NULL);
17141735

1715-
// Get monitor if fullscreen
1716-
GLFWmonitor* monitor = NULL;
1717-
if ((window_hints & GS_WINDOW_FLAGS_FULLSCREEN) == GS_WINDOW_FLAGS_FULLSCREEN)
1718-
{
1719-
int monitor_count;
1720-
GLFWmonitor** monitors = glfwGetMonitors(&monitor_count);
1721-
if (desc->monitor_index < monitor_count)
1722-
{
1723-
monitor = monitors[desc->monitor_index];
1724-
}
1736+
// Callbacks for window
1737+
glfwMakeContextCurrent(window);
1738+
glfwSetKeyCallback(window, &__glfw_key_callback);
1739+
glfwSetCharCallback(window, &__glfw_char_callback);
1740+
glfwSetMouseButtonCallback(window, &__glfw_mouse_button_callback);
1741+
glfwSetCursorEnterCallback(window, &__glfw_mouse_cursor_enter_callback);
1742+
glfwSetCursorPosCallback(window, &__glfw_mouse_cursor_position_callback);
1743+
glfwSetScrollCallback(window, &__glfw_mouse_scroll_wheel_callback);
1744+
1745+
// Cache all necessary window information
1746+
int32_t wx = 0, wy = 0, fx = 0, fy = 0, wpx = 0, wpy = 0;
1747+
glfwGetWindowSize((GLFWwindow*)win.hndl, &wx, &wy);
1748+
glfwGetFramebufferSize((GLFWwindow*)win.hndl, &fx, &fy);
1749+
glfwGetWindowPos((GLFWwindow*)win.hndl, &wpx, &wpy);
1750+
win.window_size = gs_v2((float)wx, (float)wy);
1751+
win.window_position = gs_v2((float)wpx, (float)wpy);
1752+
win.framebuffer_size = gs_v2((float)fx, (float)fy);
1753+
}
1754+
else {
1755+
void* mwin = gs_platform_raw_window_handle(gs_platform_main_window());
1756+
CONSTRUCT_WINDOW(1, 1, desc->title, 0, mwin);
17251757
}
17261758

1727-
GLFWwindow* window = glfwCreateWindow(desc->width, desc->height, desc->title, monitor, NULL);
1728-
if (window == NULL)
1729-
{
1759+
if (window == NULL) {
17301760
gs_log_error("Failed to create window.");
17311761
glfwTerminate();
17321762
return win;
1733-
}
1734-
1735-
win.hndl = window;
1736-
1737-
// Callbacks for window
1738-
glfwMakeContextCurrent(window);
1739-
glfwSetKeyCallback(window, &__glfw_key_callback);
1740-
glfwSetCharCallback(window, &__glfw_char_callback);
1741-
glfwSetMouseButtonCallback(window, &__glfw_mouse_button_callback);
1742-
glfwSetCursorEnterCallback(window, &__glfw_mouse_cursor_enter_callback);
1743-
glfwSetCursorPosCallback(window, &__glfw_mouse_cursor_position_callback);
1744-
glfwSetScrollCallback(window, &__glfw_mouse_scroll_wheel_callback);
1745-
1746-
// Cache all necessary window information
1747-
int32_t wx = 0, wy = 0, fx = 0, fy = 0, wpx = 0, wpy = 0;
1748-
glfwGetWindowSize((GLFWwindow*)win.hndl, &wx, &wy);
1749-
glfwGetFramebufferSize((GLFWwindow*)win.hndl, &fx, &fy);
1750-
glfwGetWindowPos((GLFWwindow*)win.hndl, &wpx, &wpy);
1751-
win.window_size = gs_v2((float)wx, (float)wy);
1752-
win.window_position = gs_v2((float)wpx, (float)wpy);
1753-
win.framebuffer_size = gs_v2((float)fx, (float)fy);
1763+
}
17541764

17551765
// Need to make sure this is ONLY done once.
1756-
if (gs_slot_array_empty(gs_subsystem(platform)->windows))
1757-
{
1758-
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
1759-
{
1766+
if (gs_slot_array_empty(gs_subsystem(platform)->windows)) {
1767+
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
17601768
gs_log_warning("Failed to initialize GLFW.");
17611769
return win;
17621770
}
17631771

1764-
switch (gs_subsystem(platform)->settings.video.driver)
1765-
{
1766-
case GS_PLATFORM_VIDEO_DRIVER_TYPE_OPENGL:
1767-
{
1772+
switch (gs_subsystem(platform)->settings.video.driver) {
1773+
case GS_PLATFORM_VIDEO_DRIVER_TYPE_OPENGL: {
17681774
gs_log_info("OpenGL Version: %s", glGetString(GL_VERSION));
1769-
if (gs_subsystem(platform)->settings.video.graphics.debug)
1770-
{
1775+
if (gs_subsystem(platform)->settings.video.graphics.debug) {
17711776
glDebugMessageCallback(__gs_platform_gl_debug, NULL);
17721777
}
17731778
} break;
1774-
17751779
default: break;
17761780
}
17771781
}
@@ -1842,6 +1846,20 @@ gs_platform_window_swap_buffer(uint32_t handle)
18421846
glfwSwapBuffers((GLFWwindow*)win->hndl);
18431847
}
18441848

1849+
GS_API_DECL void
1850+
gs_platform_window_make_current(uint32_t hndl)
1851+
{
1852+
gs_platform_t* platform = gs_subsystem(platform);
1853+
gs_platform_window_t* win = gs_slot_array_getp(platform->windows, hndl);
1854+
glfwMakeContextCurrent(win);
1855+
}
1856+
1857+
GS_API_DECL void
1858+
gs_platform_window_make_current_raw(void* win)
1859+
{
1860+
glfwMakeContextCurrent(win);
1861+
}
1862+
18451863
GS_API_DECL gs_vec2
18461864
gs_platform_window_sizev(uint32_t handle)
18471865
{

0 commit comments

Comments
 (0)