Skip to content

Commit

Permalink
Sokol Renderer Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pbdot committed Dec 29, 2024
1 parent e3d5c4e commit 2420caf
Show file tree
Hide file tree
Showing 20 changed files with 626 additions and 289 deletions.
36 changes: 35 additions & 1 deletion libraries/sokol/sokol_gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -4424,6 +4424,8 @@ typedef struct sg_gl_attachments_info {
SOKOL_GFX_API_DECL const void* sg_d3d11_device(void);
// D3D11: return ID3D11DeviceContext
SOKOL_GFX_API_DECL const void* sg_d3d11_device_context(void);
// D3D11: return ID3D11DepthStencilView
SOKOL_GFX_API_DECL const void sg_d3d11_clear_depth(float value);
// D3D11: get internal buffer resource objects
SOKOL_GFX_API_DECL sg_d3d11_buffer_info sg_d3d11_query_buffer_info(sg_buffer buf);
// D3D11: get internal image resource objects
Expand Down Expand Up @@ -4484,6 +4486,10 @@ SOKOL_GFX_API_DECL sg_gl_shader_info sg_gl_query_shader_info(sg_shader shd);
// GL: get internal pass resource objects
SOKOL_GFX_API_DECL sg_gl_attachments_info sg_gl_query_attachments_info(sg_attachments atts);

SOKOL_GFX_API_DECL void sg_gl_clear_depth(float value);

SOKOL_GFX_API_DECL void sg_gl_read_pixels(int x, int y, int width, int height, int format, int type, void* data);

#ifdef __cplusplus
} // extern "C"

Expand Down Expand Up @@ -5721,6 +5727,7 @@ typedef struct {
struct {
ID3D11RenderTargetView* render_view;
ID3D11RenderTargetView* resolve_view;
ID3D11DepthStencilView* depth_stencil_view;
} cur_pass;
// on-demand loaded d3dcompiler_47.dll handles
HINSTANCE d3dcompiler_dll;
Expand Down Expand Up @@ -7108,7 +7115,8 @@ _SOKOL_PRIVATE void _sg_dummy_update_image(_sg_image_t* img, const sg_image_data
_SG_XMACRO(glDeleteSamplers, void, (GLsizei n, const GLuint* samplers)) \
_SG_XMACRO(glBindBufferBase, void, (GLenum target, GLuint index, GLuint buffer)) \
_SG_XMACRO(glTexImage2DMultisample, void, (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height, GLboolean fixedsamplelocations)) \
_SG_XMACRO(glTexImage3DMultisample, void, (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedsamplelocations))
_SG_XMACRO(glTexImage3DMultisample, void, (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedsamplelocations)) \
_SG_XMACRO(glReadPixels, void, (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void * data))

// generate GL function pointer typedefs
#define _SG_XMACRO(name, ret, args) typedef ret (GL_APIENTRY* PFN_ ## name) args;
Expand Down Expand Up @@ -11432,6 +11440,9 @@ _SOKOL_PRIVATE void _sg_d3d11_begin_pass(const sg_pass* pass) {
_sg.d3d11.cur_pass.render_view = (ID3D11RenderTargetView*) swapchain->d3d11.render_view;
_sg.d3d11.cur_pass.resolve_view = (ID3D11RenderTargetView*) swapchain->d3d11.resolve_view;
}

_sg.d3d11.cur_pass.depth_stencil_view = dsv;

// apply the render-target- and depth-stencil-views
_sg_d3d11_OMSetRenderTargets(_sg.d3d11.ctx, SG_MAX_COLOR_ATTACHMENTS, rtvs, dsv);
_sg_stats_add(d3d11.pass.num_om_set_render_targets, 1);
Expand Down Expand Up @@ -19512,6 +19523,12 @@ SOKOL_API_IMPL const void* sg_d3d11_device_context(void) {
#endif
}

SOKOL_API_IMPL const void sg_d3d11_clear_depth(float value) {
#if defined(SOKOL_D3D11)
_sg.d3d11.ctx->ClearDepthStencilView(_sg.d3d11.cur_pass.depth_stencil_view, D3D11_CLEAR_DEPTH, value, 0);
#endif
}

SOKOL_API_IMPL sg_d3d11_buffer_info sg_d3d11_query_buffer_info(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
sg_d3d11_buffer_info res;
Expand Down Expand Up @@ -19876,6 +19893,23 @@ SOKOL_API_IMPL sg_wgpu_attachments_info sg_wgpu_query_attachments_info(sg_attach
return res;
}

SOKOL_API_IMPL void sg_gl_clear_depth(float value)
{
#if defined(SOKOL_GLCORE)
float depth = value;
glClearBufferfv(GL_DEPTH, 0, &depth);
#endif
}

SOKOL_API_IMPL void sg_gl_read_pixels(int x, int y, int width, int height, int format, int type, void* data)
{
#if defined(SOKOL_GLCORE)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glReadPixels(x, y, width, height, format, type, data);
_SG_GL_CHECK_ERROR();
#endif
}

SOKOL_API_IMPL sg_gl_buffer_info sg_gl_query_buffer_info(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
sg_gl_buffer_info res;
Expand Down
77 changes: 45 additions & 32 deletions source_files/edge/e_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ GameFlags default_game_flags = {

GameFlags global_flags;

bool mus_pause_stop = false;
bool mus_pause_stop = false;

std::string branding_file;
std::string configuration_file;
Expand Down Expand Up @@ -268,57 +268,59 @@ class StartupProgress

if (gamma_correction.f_ < 0)
{
int col = (1.0f + gamma_correction.f_) * 255;
int col = (1.0f + gamma_correction.f_) * 255;
RGBAColor unit_col = epi::MakeRGBA(col, col, col);
epi::SetRGBAAlpha(unit_col, HUDGetAlpha());

StartUnitBatch(false);

RendererVertex *glvert = BeginRenderUnit(GL_QUADS, 4, GL_MODULATE, 0, (GLuint)kTextureEnvironmentDisable, 0, 0, kBlendingNegativeGamma);
RendererVertex *glvert = BeginRenderUnit(GL_QUADS, 4, GL_MODULATE, 0, (GLuint)kTextureEnvironmentDisable, 0,
0, kBlendingNegativeGamma);

float x1 = 0;
float x2 = current_screen_width;

float y1 = current_screen_height;
float y2 = 0;

glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x1, y1, 0}};
glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x2, y1, 0}};
glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x2, y2, 0}};
glvert->rgba = unit_col;
glvert->position = {{x1, y2, 0}};
glvert->rgba = unit_col;
glvert->position = {{x1, y2, 0}};

EndRenderUnit(4);

FinishUnitBatch();
}
else if (gamma_correction.f_ > 0)
{
int col = gamma_correction.f_ * 255;
int col = gamma_correction.f_ * 255;
RGBAColor unit_col = epi::MakeRGBA(col, col, col);
epi::SetRGBAAlpha(unit_col, HUDGetAlpha());

StartUnitBatch(false);

RendererVertex *glvert = BeginRenderUnit(GL_QUADS, 4, GL_MODULATE, 0, (GLuint)kTextureEnvironmentDisable, 0, 0, kBlendingPositiveGamma);
RendererVertex *glvert = BeginRenderUnit(GL_QUADS, 4, GL_MODULATE, 0, (GLuint)kTextureEnvironmentDisable, 0,
0, kBlendingPositiveGamma);

float x1 = 0;
float x2 = current_screen_width;

float y1 = current_screen_height;
float y2 = 0;

glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x1, y1, 0}};
glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x2, y1, 0}};
glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x2, y2, 0}};
glvert->rgba = unit_col;
glvert->position = {{x1, y2, 0}};
glvert->rgba = unit_col;
glvert->position = {{x1, y2, 0}};

EndRenderUnit(4);

Expand Down Expand Up @@ -636,6 +638,8 @@ void EdgeDisplay(void)

HUDFrameSetup();

bool draw_menu = true;

if (!playing_movie)
{
switch (game_state)
Expand All @@ -652,6 +656,8 @@ void EdgeDisplay(void)
#endif
if (need_save_screenshot)
{
// don't draw menu in save game shots
draw_menu = false;
CreateSaveScreenshot();
need_save_screenshot = false;
}
Expand Down Expand Up @@ -701,7 +707,10 @@ void EdgeDisplay(void)
DisplayPauseImage();

// menus go directly to the screen
MenuDrawer(); // menu is drawn even on top of everything (except console)
if (draw_menu)
{
MenuDrawer(); // menu is drawn even on top of everything (except console)
}
}
else
MovieDrawer();
Expand All @@ -724,57 +733,59 @@ void EdgeDisplay(void)

if (gamma_correction.f_ < 0)
{
int col = (1.0f + gamma_correction.f_) * 255;
int col = (1.0f + gamma_correction.f_) * 255;
RGBAColor unit_col = epi::MakeRGBA(col, col, col);
epi::SetRGBAAlpha(unit_col, HUDGetAlpha());

StartUnitBatch(false);

RendererVertex *glvert = BeginRenderUnit(GL_QUADS, 4, GL_MODULATE, 0, (GLuint)kTextureEnvironmentDisable, 0, 0, kBlendingNegativeGamma);
RendererVertex *glvert = BeginRenderUnit(GL_QUADS, 4, GL_MODULATE, 0, (GLuint)kTextureEnvironmentDisable, 0, 0,
kBlendingNegativeGamma);

float x1 = 0;
float x2 = current_screen_width;

float y1 = current_screen_height;
float y2 = 0;

glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x1, y1, 0}};
glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x2, y1, 0}};
glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x2, y2, 0}};
glvert->rgba = unit_col;
glvert->position = {{x1, y2, 0}};
glvert->rgba = unit_col;
glvert->position = {{x1, y2, 0}};

EndRenderUnit(4);

FinishUnitBatch();
}
else if (gamma_correction.f_ > 0)
{
int col = gamma_correction.f_ * 255;
int col = gamma_correction.f_ * 255;
RGBAColor unit_col = epi::MakeRGBA(col, col, col);
epi::SetRGBAAlpha(unit_col, HUDGetAlpha());

StartUnitBatch(false);

RendererVertex *glvert = BeginRenderUnit(GL_QUADS, 4, GL_MODULATE, 0, (GLuint)kTextureEnvironmentDisable, 0, 0, kBlendingPositiveGamma);
RendererVertex *glvert = BeginRenderUnit(GL_QUADS, 4, GL_MODULATE, 0, (GLuint)kTextureEnvironmentDisable, 0, 0,
kBlendingPositiveGamma);

float x1 = 0;
float x2 = current_screen_width;

float y1 = current_screen_height;
float y2 = 0;

glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x1, y1, 0}};
glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x2, y1, 0}};
glvert->rgba = unit_col;
glvert->rgba = unit_col;
glvert++->position = {{x2, y2, 0}};
glvert->rgba = unit_col;
glvert->position = {{x1, y2, 0}};
glvert->rgba = unit_col;
glvert->position = {{x1, y2, 0}};

EndRenderUnit(4);

Expand All @@ -784,14 +795,16 @@ void EdgeDisplay(void)
if (m_screenshot_required)
{
m_screenshot_required = false;
TakeScreenshot(true);
render_backend->OnFrameFinished([]() -> void { TakeScreenshot(true); });
}
else if (screenshot_rate && (game_state >= kGameStateLevel))
{
EPI_ASSERT(single_tics);

if (level_time_elapsed % screenshot_rate == 0)
TakeScreenshot(false);
{
render_backend->OnFrameFinished([]() -> void { TakeScreenshot(false); });
}
}

FinishFrame(); // page flip or blit buffer
Expand Down
5 changes: 4 additions & 1 deletion source_files/edge/i_video.cc
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,9 @@ bool SetScreenSize(DisplayMode *mode)
#endif

render_state->ClearColor(kRGBABlack);
#ifndef EDGE_SOKOL
render_state->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#endif

#ifndef SOKOL_D3D11
SDL_GL_SwapWindow(program_window);
Expand Down Expand Up @@ -395,7 +397,8 @@ static void SwapBuffers(void)
#ifndef SOKOL_D3D11
// move me and other SDL_GL to backend
SDL_GL_SwapWindow(program_window);
#endif
#endif

}

void FinishFrame(void)
Expand Down
7 changes: 4 additions & 3 deletions source_files/edge/m_menu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,6 @@ static void QuickSaveResponse(int ch)
{
M_DoSave(quicksave_page, quicksave_slot);
StartSoundEffect(sound_effect_swtchx);
CreateSaveScreenshot();
}
}

Expand All @@ -1132,7 +1131,7 @@ static void MenuQuickSave(void)
MenuReadSaveStrings();
MenuSetupNextMenu(&SaveMenuDefinition);
quicksave_slot = -2; // means to pick a slot now
CreateSaveScreenshot();
need_save_screenshot = true;
return;
}

Expand All @@ -1142,12 +1141,14 @@ static void MenuQuickSave(void)
epi::StringFormat(language["QuickSaveOver"], save_extended_information_slots[quicksave_slot].description));

StartMenuMessage(s.c_str(), QuickSaveResponse, true);

need_save_screenshot = true;
}
else
{
M_DoSave(quicksave_page, quicksave_slot);
StartSoundEffect(sound_effect_swtchx);
CreateSaveScreenshot();
need_save_screenshot = true;
}
}

Expand Down
Loading

0 comments on commit 2420caf

Please sign in to comment.