Skip to content

Commit f38122a

Browse files
committed
Add feature check before using depth clamp for opengl/metal
1 parent a88e1e6 commit f38122a

File tree

6 files changed

+125
-12
lines changed

6 files changed

+125
-12
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1600"
4+
version = "1.7">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES"
8+
buildArchitectures = "Automatic">
9+
<BuildActionEntries>
10+
<BuildActionEntry
11+
buildForTesting = "YES"
12+
buildForRunning = "YES"
13+
buildForProfiling = "YES"
14+
buildForArchiving = "YES"
15+
buildForAnalyzing = "YES">
16+
<BuildableReference
17+
BuildableIdentifier = "primary"
18+
BlueprintIdentifier = "8D1107260486CEB800E47090"
19+
BuildableName = "love.app"
20+
BlueprintName = "love-macosx"
21+
ReferencedContainer = "container:love.xcodeproj">
22+
</BuildableReference>
23+
</BuildActionEntry>
24+
</BuildActionEntries>
25+
</BuildAction>
26+
<TestAction
27+
buildConfiguration = "Debug"
28+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
29+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
30+
shouldUseLaunchSchemeArgsEnv = "YES"
31+
shouldAutocreateTestPlan = "YES">
32+
</TestAction>
33+
<LaunchAction
34+
buildConfiguration = "Debug"
35+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
36+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
37+
launchStyle = "0"
38+
useCustomWorkingDirectory = "NO"
39+
ignoresPersistentStateOnLaunch = "NO"
40+
debugDocumentVersioning = "YES"
41+
debugServiceExtension = "internal"
42+
allowLocationSimulation = "YES">
43+
<BuildableProductRunnable
44+
runnableDebuggingMode = "0">
45+
<BuildableReference
46+
BuildableIdentifier = "primary"
47+
BlueprintIdentifier = "8D1107260486CEB800E47090"
48+
BuildableName = "love.app"
49+
BlueprintName = "love-macosx"
50+
ReferencedContainer = "container:love.xcodeproj">
51+
</BuildableReference>
52+
</BuildableProductRunnable>
53+
<CommandLineArguments>
54+
<CommandLineArgument
55+
argument = " /Users/phanisrikar/Desktop/OSS/ --renderers opengl"
56+
isEnabled = "YES">
57+
</CommandLineArgument>
58+
</CommandLineArguments>
59+
</LaunchAction>
60+
<ProfileAction
61+
buildConfiguration = "Release"
62+
shouldUseLaunchSchemeArgsEnv = "YES"
63+
savedToolIdentifier = ""
64+
useCustomWorkingDirectory = "NO"
65+
debugDocumentVersioning = "YES">
66+
<BuildableProductRunnable
67+
runnableDebuggingMode = "0">
68+
<BuildableReference
69+
BuildableIdentifier = "primary"
70+
BlueprintIdentifier = "8D1107260486CEB800E47090"
71+
BuildableName = "love.app"
72+
BlueprintName = "love-macosx"
73+
ReferencedContainer = "container:love.xcodeproj">
74+
</BuildableReference>
75+
</BuildableProductRunnable>
76+
</ProfileAction>
77+
<AnalyzeAction
78+
buildConfiguration = "Debug">
79+
</AnalyzeAction>
80+
<ArchiveAction
81+
buildConfiguration = "Release"
82+
revealArchiveInOrganizer = "YES">
83+
</ArchiveAction>
84+
</Scheme>

src/modules/graphics/Graphics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class Graphics : public Module
163163
FEATURE_TEXEL_BUFFER,
164164
FEATURE_COPY_TEXTURE_TO_BUFFER,
165165
FEATURE_INDIRECT_DRAW,
166+
FEATURE_DEPTH_CLAMP,
166167
FEATURE_MAX_ENUM
167168
};
168169

src/modules/graphics/metal/Graphics.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ static bool isClampOne(SamplerState::WrapMode w)
10381038
[encoder setDepthStencilState:mtlstate];
10391039

10401040
// 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];
1041+
[encoder setDepthClipMode: state.depthClampEnable ? MTLDepthClipModeClamp : MTLDepthClipModeClip];
10421042
}
10431043

10441044
if (dirtyState & STATEBIT_STENCIL)
@@ -2281,7 +2281,9 @@ static inline void advanceVertexOffsets(const VertexAttributes &attributes, Buff
22812281
else
22822282
capabilities.features[FEATURE_INDIRECT_DRAW] = false;
22832283

2284-
static_assert(FEATURE_MAX_ENUM == 13, "Graphics::initCapabilities must be updated when adding a new graphics feature!");
2284+
capabilities.features[FEATURE_DEPTH_CLAMP] = true;
2285+
2286+
static_assert(FEATURE_MAX_ENUM == 14, "Graphics::initCapabilities must be updated when adding a new graphics feature!");
22852287

22862288
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
22872289
capabilities.limits[LIMIT_POINT_SIZE] = 511;

src/modules/graphics/opengl/Graphics.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,13 +1455,15 @@ void Graphics::setDepthMode(CompareMode compare, bool write)
14551455

14561456
void Graphics::setDepthClamp(bool enable)
14571457
{
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;
1458+
if(capabilities.features[FEATURE_DEPTH_CLAMP]) {
1459+
DisplayState &state = states.back();
1460+
1461+
if(state.depthClampEnable != enable)
1462+
flushBatchedDraws();
1463+
1464+
gl.setEnableState(OpenGL::ENABLE_DEPTH_CLAMP, enable);
1465+
state.depthClampEnable = enable;
1466+
}
14651467
}
14661468

14671469
void Graphics::setFrontFaceWinding(Winding winding)
@@ -1620,7 +1622,12 @@ void Graphics::initCapabilities()
16201622
capabilities.features[FEATURE_TEXEL_BUFFER] = gl.isBufferUsageSupported(BUFFERUSAGE_TEXEL);
16211623
capabilities.features[FEATURE_COPY_TEXTURE_TO_BUFFER] = gl.isCopyTextureToBufferSupported();
16221624
capabilities.features[FEATURE_INDIRECT_DRAW] = capabilities.features[FEATURE_GLSL4];
1623-
static_assert(FEATURE_MAX_ENUM == 13, "Graphics::initCapabilities must be updated when adding a new graphics feature!");
1625+
capabilities.features[FEATURE_DEPTH_CLAMP] = hasExtension("GL_EXT_depth_clamp") ||
1626+
hasExtension("GL_NV_depth_clamp") ||
1627+
hasExtension("GL_AMD_depth_clamp_separate") ||
1628+
hasExtension("GL_ARB_depth_clamp") ||
1629+
GLAD_VERSION_3_2;
1630+
static_assert(FEATURE_MAX_ENUM == 14, "Graphics::initCapabilities must be updated when adding a new graphics feature!");
16241631

16251632
capabilities.limits[LIMIT_POINT_SIZE] = gl.getMaxPointSize();
16261633
capabilities.limits[LIMIT_TEXTURE_SIZE] = gl.getMax2DTextureSize();
@@ -1749,6 +1756,23 @@ bool Graphics::isPixelFormatSupported(PixelFormat format, uint32 usage)
17491756
return (usage & pixelFormatUsage[format][readable ? 1 : 0]) == usage;
17501757
}
17511758

1759+
bool Graphics::hasExtension(const char* name)
1760+
{
1761+
GLint numExts = 0;
1762+
glGetIntegerv(GL_NUM_EXTENSIONS, &numExts);
1763+
for (GLint i = 0; i < numExts; ++i) {
1764+
const char* ext = (const char*)glGetStringi(GL_EXTENSIONS, i);
1765+
::printf("extension: %s \n", ext);
1766+
if (strcmp(ext, name) == 0) return true;
1767+
}
1768+
// fallback for older contexts
1769+
const char* all = (const char*)glGetString(GL_EXTENSIONS);
1770+
if (all) {
1771+
return strstr(all, name) != nullptr;
1772+
}
1773+
return false;
1774+
}
1775+
17521776
} // opengl
17531777
} // graphics
17541778
} // love

src/modules/graphics/opengl/Graphics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ class Graphics final : public love::graphics::Graphics
146146
void setRenderTargetsInternal(const RenderTargets &rts, int pixelw, int pixelh, bool hasSRGBtexture) override;
147147
void initCapabilities() override;
148148
void getAPIStats(int &shaderswitches) const override;
149+
150+
bool hasExtension(const char* name);
149151

150152
void endPass(bool presenting);
151153
GLuint bindCachedFBO(const RenderTargets &targets);

src/modules/graphics/wrap_Graphics.cpp

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

2894-
int w_setDepthClampEnable(lua_State *L)
2894+
int w_setDepthClamp(lua_State *L)
28952895
{
28962896
instance()->setDepthClamp(luax_checkboolean(L, 1));
28972897
return 0;
@@ -4087,7 +4087,7 @@ static const luaL_Reg functions[] =
40874087
{ "getPointSize", w_getPointSize },
40884088
{ "setDepthMode", w_setDepthMode },
40894089
{ "getDepthMode", w_getDepthMode },
4090-
{ "setDepthClampEnable", w_setDepthClampEnable },
4090+
{ "setDepthClamp", w_setDepthClamp },
40914091
{ "setMeshCullMode", w_setMeshCullMode },
40924092
{ "getMeshCullMode", w_getMeshCullMode },
40934093
{ "setFrontFaceWinding", w_setFrontFaceWinding },

0 commit comments

Comments
 (0)