Skip to content

Commit

Permalink
sokol: Only set clip/viewport if required
Browse files Browse the repository at this point in the history
  • Loading branch information
IonAgorria committed Oct 22, 2024
1 parent fa242f5 commit cf3907c
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 162 deletions.
67 changes: 50 additions & 17 deletions Source/Render/sokol/SokolRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ void cSokolRender::ClearAllCommands() {
}
}

ClearCommands(commands);
ClearCommands(swapchainCommands);
}

void cSokolRender::ClearPipelines() {
Expand All @@ -564,41 +564,66 @@ void cSokolRender::ClearPipelines() {
pipelines.clear();
}

void cSokolRender::SetCommandViewportClip(bool replace) {
if (activeCommand.viewport && replace) {
free(activeCommand.viewport);
activeCommand.viewport = nullptr;
}
if (activeCommand.clip && replace) {
free(activeCommand.clip);
activeCommand.clip = nullptr;
}
if (!activeCommand.viewport) {
activeCommand.viewport = new Vect2i[2]{
activeViewport[0],
activeViewport[1]
};
}
if (!activeCommand.clip) {
activeCommand.clip = new Vect2i[2]{
activeClip[0],
activeClip[1]
};
}
}

int cSokolRender::GetClipRect(int *xmin,int *ymin,int *xmax,int *ymax) {
*xmin = activeCommand.clip[0].x;
*ymin = activeCommand.clip[0].y;
*xmax = activeCommand.clip[1].x + activeCommand.clip[0].x;
*ymax = activeCommand.clip[1].y + activeCommand.clip[0].y;
*xmin = activeClip[0].x;
*ymin = activeClip[0].y;
*xmax = activeClip[1].x + activeClip[0].x;
*ymax = activeClip[1].y + activeClip[0].y;
return 0;
}

int cSokolRender::SetClipRect(int xmin,int ymin,int xmax,int ymax) {
int w = xmax-xmin;
int h = ymax-ymin;
if (activeCommand.clip[0].x == xmin && activeCommand.clip[0].y == ymin
&& activeCommand.clip[1].x == w && activeCommand.clip[1].y == h) {
if (activeClip[0].x == xmin && activeClip[0].y == ymin
&& activeClip[1].x == w && activeClip[1].y == h) {
//Nothing to do
return 0;
}
FinishActiveDrawBuffer();
activeCommand.clip[0].x = xmin;
activeCommand.clip[0].y = ymin;
activeCommand.clip[1].x = w;
activeCommand.clip[1].y = h;
activeClip[0].x = xmin;
activeClip[0].y = ymin;
activeClip[1].x = w;
activeClip[1].y = h;
SetCommandViewportClip();
return 0;
}

void cSokolRender::ResetViewport() {
if (activeCommand.viewport[0].x == 0
&& activeCommand.viewport[0].y == 0
&& activeCommand.viewport[1] == ScreenSize) {
if (activeViewport[0].x == 0
&& activeViewport[0].y == 0
&& activeViewport[1] == ScreenSize) {
//Nothing to do
return;
}
FinishActiveDrawBuffer();
activeCommand.viewport[0].x = 0;
activeCommand.viewport[0].y = 0;
activeCommand.viewport[1] = ScreenSize;;
activeViewport[0].x = 0;
activeViewport[0].y = 0;
activeViewport[1] = ScreenSize;
SetCommandViewportClip();
}

bool cSokolRender::SetScreenShot(const char *fname) {
Expand Down Expand Up @@ -719,6 +744,14 @@ void SokolCommand::CreateShaderParams() {
}

void SokolCommand::ClearDrawData() {
if (viewport) {
delete viewport;
viewport = nullptr;
}
if (clip) {
delete clip;
clip = nullptr;
}
if (pass_action) {
delete pass_action;
pass_action = nullptr;
Expand Down
10 changes: 7 additions & 3 deletions Source/Render/sokol/SokolRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ struct SokolCommand {
void* fs_params = nullptr;
size_t vs_params_len = 0;
size_t fs_params_len = 0;
Vect2i viewport[2]; //0 Pos 1 Size
Vect2i clip[2]; //0 Pos 1 Size
Vect2i* viewport = nullptr; //0 Pos 1 Size
Vect2i* clip = nullptr; //0 Pos 1 Size
};

struct SokolRenderTarget final {
Expand Down Expand Up @@ -106,7 +106,7 @@ class cSokolRender: public cInterfaceRenderDevice {
//Renderer state
bool ActiveScene = false;
bool isOrthographicProjSet = false;
std::vector<SokolCommand*> commands;
std::vector<SokolCommand*> swapchainCommands;
sg_sampler sampler;
sg_sampler shadow_sampler;

Expand Down Expand Up @@ -154,6 +154,8 @@ class cSokolRender: public cInterfaceRenderDevice {
sColor4f activeLightAmbient;
sColor4f activeLightSpecular;
Mat4f activeTextureTransform[PERIMETER_SOKOL_TEXTURES];
Vect2i activeViewport[2]; //0 Pos 1 Size
Vect2i activeClip[2]; //0 Pos 1 Size

//Shadow and Light map rendering
SokolRenderTarget* activeRenderTarget = nullptr;
Expand All @@ -172,6 +174,8 @@ class cSokolRender: public cInterfaceRenderDevice {
void SetColorMode(eColorMode color_mode);
void SetMaterial(SOKOL_MATERIAL_TYPE material, const sColor4f& diffuse, const sColor4f& ambient,
const sColor4f& specular, const sColor4f& emissive, float power);
void SetCommandViewportClip(bool replace = true);
std::vector<SokolCommand*>& getActiveCommands();
template<typename T>
void StorePooledResource(
std::unordered_multimap<uint64_t, SokolResourcePooled<T>>& res_pool,
Expand Down
Loading

0 comments on commit cf3907c

Please sign in to comment.