Skip to content

Commit b08964b

Browse files
committed
Add depth bias support and viewport depth range functionality to VRHI
1 parent f447e83 commit b08964b

16 files changed

Lines changed: 319 additions & 361 deletions

src/vrhi_backend.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,15 @@ bool vhCmdBackendState::BE_PresubmitCommon_PipelineDesc(
420420
graphicsPipelineDesc->renderState.blendState = vhTranslateBlendState( state.stateFlags );
421421
graphicsPipelineDesc->renderState.depthStencilState = vhTranslateDepthStencilState( state.stateFlags, state.frontStencil, state.backStencil );
422422
graphicsPipelineDesc->renderState.rasterState = vhTranslateRasterState( state.stateFlags );
423+
if ( state.viewScissor.z >= 0.0f && state.viewScissor.w >= 0.0f )
424+
{
425+
graphicsPipelineDesc->renderState.rasterState.scissorEnable = true;
426+
}
427+
428+
// Apply depth bias values
429+
graphicsPipelineDesc->renderState.rasterState.depthBias = state.depthBias;
430+
graphicsPipelineDesc->renderState.rasterState.depthBiasClamp = state.depthBiasClamp;
431+
graphicsPipelineDesc->renderState.rasterState.slopeScaledDepthBias = state.slopeScaledDepthBias;
423432

424433
// [TODO] The following fields are not currently populated from vhState:
425434
// - patchControlPoints: tessellation is only supported if we add it.
@@ -1086,7 +1095,7 @@ bool vhCmdBackendState::BE_PreSubmitCommon_State(
10861095
graphicsState->viewport.viewports.push_back( nvrhi::Viewport(
10871096
state.viewRect.x, state.viewRect.x + state.viewRect.z,
10881097
state.viewRect.y, state.viewRect.y + state.viewRect.w,
1089-
0.0f, 1.0f
1098+
state.viewDepthRange.x, state.viewDepthRange.y
10901099
) );
10911100

10921101
graphicsState->viewport.scissorRects.resize( 0 );
@@ -2081,6 +2090,15 @@ void vhCmdBackendState::Handle_vhCmdSetStateStencil( VIDL_vhCmdSetStateStencil*
20812090
state.backStencil = cmd->back;
20822091
}
20832092

2093+
void vhCmdBackendState::Handle_vhCmdSetStateDepthBias( VIDL_vhCmdSetStateDepthBias* cmd )
2094+
{
2095+
BE_CmdRAII cmdRAII( cmd );
2096+
auto& state = backendStates[cmd->id];
2097+
state.depthBias = cmd->bias;
2098+
state.depthBiasClamp = cmd->clamp;
2099+
state.slopeScaledDepthBias = cmd->slopeScaled;
2100+
}
2101+
20842102
void vhCmdBackendState::Handle_vhCmdSetStateVertexBuffer( VIDL_vhCmdSetStateVertexBuffer* cmd )
20852103
{
20862104
BE_CmdRAII cmdRAII( cmd );

src/vrhi_backend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ class vhCmdBackendState : public VIDLHandler
333333

334334
void Handle_vhCmdSetStateStencil( VIDL_vhCmdSetStateStencil* cmd ) override;
335335

336+
void Handle_vhCmdSetStateDepthBias( VIDL_vhCmdSetStateDepthBias* cmd ) override;
337+
336338
void Handle_vhCmdSetStateVertexBuffer( VIDL_vhCmdSetStateVertexBuffer* cmd ) override;
337339

338340
void Handle_vhCmdSetStateIndexBuffer( VIDL_vhCmdSetStateIndexBuffer* cmd ) override;

src/vrhi_generated.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,21 @@ struct VIDL_vhCmdSetStateStencil
511511
: id(_id), front(_front), back(_back) {}
512512
};
513513

514+
struct VIDL_vhCmdSetStateDepthBias
515+
{
516+
static constexpr uint64_t kMagic = 0x3290E5BB;
517+
uint64_t MAGIC = kMagic;
518+
vhStateId id;
519+
int bias;
520+
float clamp;
521+
float slopeScaled;
522+
523+
VIDL_vhCmdSetStateDepthBias() = default;
524+
525+
VIDL_vhCmdSetStateDepthBias(vhStateId _id, int _bias, float _clamp, float _slopeScaled)
526+
: id(_id), bias(_bias), clamp(_clamp), slopeScaled(_slopeScaled) {}
527+
};
528+
514529
struct VIDL_vhCmdSetStateVertexBuffer
515530
{
516531
static constexpr uint64_t kMagic = 0xF0E68F37;
@@ -694,6 +709,7 @@ struct VIDLHandler
694709
virtual void Handle_vhCmdSetStateFlags( VIDL_vhCmdSetStateFlags* cmd ) { (void) cmd; };
695710
virtual void Handle_vhCmdSetStateDebugFlags( VIDL_vhCmdSetStateDebugFlags* cmd ) { (void) cmd; };
696711
virtual void Handle_vhCmdSetStateStencil( VIDL_vhCmdSetStateStencil* cmd ) { (void) cmd; };
712+
virtual void Handle_vhCmdSetStateDepthBias( VIDL_vhCmdSetStateDepthBias* cmd ) { (void) cmd; };
697713
virtual void Handle_vhCmdSetStateVertexBuffer( VIDL_vhCmdSetStateVertexBuffer* cmd ) { (void) cmd; };
698714
virtual void Handle_vhCmdSetStateIndexBuffer( VIDL_vhCmdSetStateIndexBuffer* cmd ) { (void) cmd; };
699715
virtual void Handle_vhCmdSetStateTextures( VIDL_vhCmdSetStateTextures* cmd ) { (void) cmd; };
@@ -860,6 +876,10 @@ struct VIDLHandler
860876
HandleLogFunction("Handle_vhCmdSetStateStencil");
861877
Handle_vhCmdSetStateStencil( (VIDL_vhCmdSetStateStencil*) cmd );
862878
break;
879+
case 0x3290E5BB:
880+
HandleLogFunction("Handle_vhCmdSetStateDepthBias");
881+
Handle_vhCmdSetStateDepthBias( (VIDL_vhCmdSetStateDepthBias*) cmd );
882+
break;
863883
case 0xF0E68F37:
864884
HandleLogFunction("Handle_vhCmdSetStateVertexBuffer");
865885
Handle_vhCmdSetStateVertexBuffer( (VIDL_vhCmdSetStateVertexBuffer*) cmd );

src/vrhi_state.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ void vhCmdSetStateStencil( vhStateId id, uint32_t front, uint32_t back )
7575
vhCmdEnqueue( new VIDL_vhCmdSetStateStencil( id, front, back ) );
7676
}
7777

78+
void vhCmdSetStateDepthBias( vhStateId id, int bias, float clamp, float slopeScaled )
79+
{
80+
vhCmdEnqueue( new VIDL_vhCmdSetStateDepthBias( id, bias, clamp, slopeScaled ) );
81+
}
82+
7883
void vhCmdSetStateVertexBuffer( vhStateId id, uint8_t stream, vhBuffer buffer, uint64_t offset, uint32_t start, uint32_t num )
7984
{
8085
vhCmdEnqueue( new VIDL_vhCmdSetStateVertexBuffer( id, stream, buffer, offset, start, num ) );
@@ -154,6 +159,11 @@ bool vhSetState( vhStateId id, vhState& state, uint64_t dirtyForceMask )
154159
vhCmdSetStateStencil( id, state.frontStencil, state.backStencil );
155160
}
156161

162+
if ( dirty & VRHI_DIRTY_DEPTH_BIAS )
163+
{
164+
vhCmdSetStateDepthBias( id, state.depthBias, state.depthBiasClamp, state.slopeScaledDepthBias );
165+
}
166+
157167
if ( dirty & VRHI_DIRTY_VERTEX_INDEX )
158168
{
159169
for ( uint8_t i = 0; i < ( uint8_t ) state.vertexBindings.size(); ++i )
@@ -351,6 +361,11 @@ nvrhi::DepthStencilState vhTranslateDepthStencilState( uint64_t stateFlags, uint
351361
dsState.depthTestEnable = true;
352362
dsState.depthFunc = fnConvertComparisonFunc( depthFunc );
353363
}
364+
else if ( stateFlags & VRHI_STATE_DEPTH_TEST_ENABLE )
365+
{
366+
dsState.depthTestEnable = true;
367+
dsState.depthFunc = nvrhi::ComparisonFunc::Less; // Default comparison when only enable flag is set
368+
}
354369
else
355370
{
356371
dsState.depthTestEnable = false;
@@ -431,6 +446,7 @@ nvrhi::RasterState vhTranslateRasterState( uint64_t stateFlags )
431446
rasterState.multisampleEnable = ( stateFlags & VRHI_STATE_MSAA ) != 0;
432447
rasterState.antialiasedLineEnable = ( stateFlags & VRHI_STATE_LINEAA ) != 0;
433448
rasterState.conservativeRasterEnable = ( stateFlags & VRHI_STATE_CONSERVATIVE_RASTER ) != 0;
449+
rasterState.depthClipEnable = ( stateFlags & VRHI_STATE_DEPTH_CLIP ) != 0;
434450

435451
return rasterState;
436452
}

test/test.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <windows.h>
3333
#endif // _WIN32
3434
#include "utest.h"
35+
#include "test.h"
3536
#include <vrhi.h>
3637

3738
UTEST( Vrhi, Dummy )
@@ -41,6 +42,22 @@ UTEST( Vrhi, Dummy )
4142

4243
bool g_testInit = false;
4344
bool g_testInitQuiet = true;
45+
bool g_captureActive = false;
46+
47+
void TestEnsureShutdown()
48+
{
49+
if ( g_captureActive )
50+
{
51+
vhCaptureEnd();
52+
g_captureActive = false;
53+
}
54+
55+
if ( g_testInit )
56+
{
57+
vhShutdown( g_testInitQuiet );
58+
g_testInit = false;
59+
}
60+
}
4461

4562
UTEST_STATE();
4663

@@ -64,12 +81,6 @@ int main( int argc, const char* const argv[] )
6481
#endif
6582

6683
int result = utest_main( argc, argv );
67-
68-
if ( g_testInit )
69-
{
70-
vhShutdown( g_testInitQuiet );
71-
g_testInit = false;
72-
}
73-
84+
TestEnsureShutdown();
7485
return result;
7586
}

test/test.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include <glm/glm.hpp>
2626
#include <nvrhi/nvrhi.h>
2727

28+
extern bool g_captureActive;
29+
void TestEnsureShutdown();
30+
2831
#if defined(__cplusplus) && (__cplusplus >= 201103L)
2932
#ifdef __clang__
3033

0 commit comments

Comments
 (0)