Skip to content

Commit e51ddc6

Browse files
committed
graphics: Added depth clamp enable APIs for opengl and metal
1 parent 36f368f commit e51ddc6

File tree

9 files changed

+52
-0
lines changed

9 files changed

+52
-0
lines changed

src/modules/graphics/Graphics.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ void Graphics::restoreState(const DisplayState &s)
847847

848848
setStencilState(s.stencil);
849849
setDepthMode(s.depthTest, s.depthWrite);
850+
setDepthClamp(s.depthClampEnable);
850851

851852
setColorMask(s.colorMask);
852853
setWireframe(s.wireframe);
@@ -926,6 +927,9 @@ void Graphics::restoreStateChecked(const DisplayState &s)
926927
if (s.depthTest != cur.depthTest || s.depthWrite != cur.depthWrite)
927928
setDepthMode(s.depthTest, s.depthWrite);
928929

930+
if(s.depthClampEnable != cur.depthClampEnable)
931+
setDepthClamp(s.depthClampEnable);
932+
929933
if (s.colorMask != cur.colorMask)
930934
setColorMask(s.colorMask);
931935

src/modules/graphics/Graphics.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,12 @@ class Graphics : public Module
621621
void setDepthMode();
622622
void getDepthMode(CompareMode &compare, bool &write) const;
623623

624+
/**
625+
* Clamps the depth valies to [0, 1] range instead of clipping geomentry
626+
* away at near and far planes
627+
*/
628+
virtual void setDepthClamp(bool enable) = 0;
629+
624630
void setMeshCullMode(CullMode cull);
625631
CullMode getMeshCullMode() const;
626632

@@ -948,6 +954,7 @@ class Graphics : public Module
948954

949955
CompareMode depthTest = COMPARE_ALWAYS;
950956
bool depthWrite = false;
957+
bool depthClampEnable = false;
951958

952959
CullMode meshCullMode = CULL_NONE;
953960
Winding winding = WINDING_CCW;

src/modules/graphics/metal/Graphics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class Graphics final : public love::graphics::Graphics
9696

9797
void setDepthMode(CompareMode compare, bool write) override;
9898

99+
void setDepthClamp(bool enable) override;
100+
99101
void setFrontFaceWinding(Winding winding) override;
100102

101103
void setColorMask(ColorChannelMask mask) override;

src/modules/graphics/metal/Graphics.mm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,9 @@ static bool isClampOne(SamplerState::WrapMode w)
10361036
id<MTLDepthStencilState> mtlstate = getCachedDepthStencilState(depth, state.stencil);
10371037

10381038
[encoder setDepthStencilState:mtlstate];
1039+
1040+
// Also set the depth clamping (depth state change is tirggered for both deptstate and clamp state changes)
1041+
[encoder setDepthClipMode: state.depthClampEnable == false ? MTLDepthClipModeClip : MTLDepthClipModeClamp];
10391042
}
10401043

10411044
if (dirtyState & STATEBIT_STENCIL)
@@ -1842,6 +1845,18 @@ static inline void advanceVertexOffsets(const VertexAttributes &attributes, Buff
18421845
}
18431846
}
18441847

1848+
void Graphics::setDepthClamp(bool enable)
1849+
{
1850+
DisplayState &state = states.back();
1851+
1852+
if(state.depthClampEnable != enable)
1853+
{
1854+
flushBatchedDraws();
1855+
state.depthClampEnable = enable;
1856+
dirtyRenderState |= STATEBIT_DEPTH;
1857+
}
1858+
}
1859+
18451860
void Graphics::setFrontFaceWinding(Winding winding)
18461861
{
18471862
if (states.back().winding != winding)

src/modules/graphics/opengl/Graphics.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,17 @@ void Graphics::setDepthMode(CompareMode compare, bool write)
14531453
}
14541454
}
14551455

1456+
void Graphics::setDepthClamp(bool enable)
1457+
{
1458+
DisplayState &state = states.back();
1459+
1460+
if(state.depthClampEnable != enable)
1461+
flushBatchedDraws();
1462+
1463+
gl.setEnableState(OpenGL::ENABLE_DEPTH_CLAMP, enable);
1464+
state.depthClampEnable = enable;
1465+
}
1466+
14561467
void Graphics::setFrontFaceWinding(Winding winding)
14571468
{
14581469
DisplayState &state = states.back();

src/modules/graphics/opengl/Graphics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class Graphics final : public love::graphics::Graphics
9292

9393
void setDepthMode(CompareMode compare, bool write) override;
9494

95+
void setDepthClamp(bool enable) override;
96+
9597
void setFrontFaceWinding(Winding winding) override;
9698

9799
void setColorMask(ColorChannelMask mask) override;

src/modules/graphics/opengl/OpenGL.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,9 @@ void OpenGL::setEnableState(EnableState enablestate, bool enable)
830830
case ENABLE_FRAMEBUFFER_SRGB:
831831
glstate = GL_FRAMEBUFFER_SRGB;
832832
break;
833+
case ENABLE_DEPTH_CLAMP:
834+
glstate = GL_DEPTH_CLAMP;
835+
break;
833836
case ENABLE_MAX_ENUM:
834837
break;
835838
}

src/modules/graphics/opengl/OpenGL.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class OpenGL
100100
ENABLE_SCISSOR_TEST,
101101
ENABLE_FACE_CULL,
102102
ENABLE_FRAMEBUFFER_SRGB,
103+
ENABLE_DEPTH_CLAMP,
103104
ENABLE_MAX_ENUM
104105
};
105106

src/modules/graphics/wrap_Graphics.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,6 +2891,12 @@ int w_getDepthMode(lua_State *L)
28912891
return 2;
28922892
}
28932893

2894+
int w_setDepthClampEnable(lua_State *L)
2895+
{
2896+
instance()->setDepthClamp(luax_checkboolean(L, 1));
2897+
return 0;
2898+
}
2899+
28942900
int w_setMeshCullMode(lua_State *L)
28952901
{
28962902
const char *str = luaL_checkstring(L, 1);
@@ -4081,6 +4087,7 @@ static const luaL_Reg functions[] =
40814087
{ "getPointSize", w_getPointSize },
40824088
{ "setDepthMode", w_setDepthMode },
40834089
{ "getDepthMode", w_getDepthMode },
4090+
{ "setDepthClampEnable", w_setDepthClampEnable },
40844091
{ "setMeshCullMode", w_setMeshCullMode },
40854092
{ "getMeshCullMode", w_getMeshCullMode },
40864093
{ "setFrontFaceWinding", w_setFrontFaceWinding },

0 commit comments

Comments
 (0)