Skip to content

Commit adbd5f0

Browse files
author
devsh
committed
display reconstructed (face) normals when per-vertex aren't available
1 parent d51c739 commit adbd5f0

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

12_MeshLoaders/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
1717
public:
1818
inline MeshLoadersApp(const path& _localInputCWD, const path& _localOutputCWD, const path& _sharedInputCWD, const path& _sharedOutputCWD)
1919
: IApplicationFramework(_localInputCWD, _localOutputCWD, _sharedInputCWD, _sharedOutputCWD),
20-
device_base_t({1280,720}, EF_UNKNOWN, _localInputCWD, _localOutputCWD, _sharedInputCWD, _sharedOutputCWD) {}
20+
device_base_t({1280,720}, EF_D32_SFLOAT, _localInputCWD, _localOutputCWD, _sharedInputCWD, _sharedOutputCWD) {}
2121

2222
inline bool onAppInitialized(smart_refctd_ptr<ISystem>&& system) override
2323
{

common/include/nbl/examples/geometry/CSimpleDebugRenderer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
161161

162162
// create pipeline layout
163163
const SPushConstantRange ranges[] = {{
164-
.stageFlags = hlsl::ShaderStage::ESS_VERTEX,
164+
.stageFlags = hlsl::ShaderStage::ESS_VERTEX|hlsl::ShaderStage::ESS_FRAGMENT,
165165
.offset = 0,
166166
.size = sizeof(SInstance::SPushConstants),
167167
}};
@@ -386,7 +386,7 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
386386
const auto* geo = instance.packedGeo;
387387
cmdbuf->bindGraphicsPipeline(geo->pipeline.get());
388388
const auto pc = instance.computePushConstants(viewParams);
389-
cmdbuf->pushConstants(layout,hlsl::ShaderStage::ESS_VERTEX,0,sizeof(pc),&pc);
389+
cmdbuf->pushConstants(layout,hlsl::ShaderStage::ESS_VERTEX|hlsl::ShaderStage::ESS_FRAGMENT,0,sizeof(pc),&pc);
390390
if (geo->indexBuffer)
391391
{
392392
cmdbuf->bindIndexBuffer(geo->indexBuffer,geo->indexType);

common/include/nbl/examples/geometry/SPushConstants.hlsl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ struct SInstanceMatrices
2222

2323
struct SPushConstants
2424
{
25-
// no idea if DXC still has this bug with Push Constant static variables
26-
#ifndef __HLSL_VERSiON
2725
NBL_CONSTEXPR_STATIC_INLINE uint32_t DescriptorCount = 255;
28-
#endif
2926

3027
SInstanceMatrices matrices;
3128
uint32_t positionView : 11;

common/src/nbl/examples/geometry/shaders/unified.hlsl

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,47 @@ using namespace nbl::hlsl;
44
using namespace nbl::hlsl::examples::geometry_creator_scene;
55

66
// for dat sweet programmable pulling
7-
[[vk::binding(0)]] Buffer<float32_t4> utbs[/*SPushConstants::DescriptorCount*/255];
7+
[[vk::binding(0)]] Buffer<float32_t4> utbs[SPushConstants::DescriptorCount];
88

99
//
1010
[[vk::push_constant]] SPushConstants pc;
1111

1212
//
1313
struct SInterpolants
1414
{
15-
float32_t4 position : SV_Position;
16-
float32_t3 meta : COLOR0;
15+
float32_t4 ndc : SV_Position;
16+
float32_t3 meta : COLOR1;
1717
};
1818
#include "nbl/builtin/hlsl/math/linalg/fast_affine.hlsl"
1919

20+
float32_t3 reconstructGeometricNormal(float32_t3 pos)
21+
{
22+
const float32_t2x3 dPos_dScreen = float32_t2x3(
23+
ddx(pos),
24+
ddy(pos)
25+
);
26+
return cross(dPos_dScreen[0],dPos_dScreen[1]);
27+
}
28+
2029
//
2130
[shader("vertex")]
2231
SInterpolants BasicVS(uint32_t VertexIndex : SV_VertexID)
2332
{
2433
const float32_t3 position = utbs[pc.positionView][VertexIndex].xyz;
2534

2635
SInterpolants output;
27-
output.position = math::linalg::promoted_mul(pc.matrices.worldViewProj,position);
28-
output.meta = mul(pc.matrices.normal,utbs[pc.normalView][VertexIndex].xyz);
36+
output.ndc = math::linalg::promoted_mul(pc.matrices.worldViewProj,position);
37+
if (pc.normalView<SPushConstants::DescriptorCount)
38+
output.meta = mul(pc.matrices.normal,utbs[pc.normalView][VertexIndex].xyz);
39+
else
40+
output.meta = mul(inverse(transpose(pc.matrices.normal)),position);
2941
return output;
3042
}
3143
[shader("pixel")]
3244
float32_t4 BasicFS(SInterpolants input) : SV_Target0
3345
{
34-
return float32_t4(normalize(input.meta)*0.5f+promote<float32_t3>(0.5f),1.f);
46+
const float32_t3 normal = pc.normalView<SPushConstants::DescriptorCount ? input.meta:reconstructGeometricNormal(input.meta);
47+
return float32_t4(normalize(normal)*0.5f+promote<float32_t3>(0.5f),1.f);
3548
}
3649

3750
// TODO: do smooth normals on the cone
@@ -41,17 +54,13 @@ SInterpolants ConeVS(uint32_t VertexIndex : SV_VertexID)
4154
const float32_t3 position = utbs[pc.positionView][VertexIndex].xyz;
4255

4356
SInterpolants output;
44-
output.position = math::linalg::promoted_mul(pc.matrices.worldViewProj,position);
57+
output.ndc = math::linalg::promoted_mul(pc.matrices.worldViewProj,position);
4558
output.meta = mul(inverse(transpose(pc.matrices.normal)),position);
4659
return output;
4760
}
4861
[shader("pixel")]
4962
float32_t4 ConeFS(SInterpolants input) : SV_Target0
5063
{
51-
const float32_t2x3 dViewPos_dScreen = float32_t2x3(
52-
ddx(input.meta),
53-
ddy(input.meta)
54-
);
55-
const float32_t3 normal = cross(dViewPos_dScreen[0],dViewPos_dScreen[1]);
64+
const float32_t3 normal = reconstructGeometricNormal(input.meta);
5665
return float32_t4(normalize(normal)*0.5f+promote<float32_t3>(0.5f),1.f);
5766
}

0 commit comments

Comments
 (0)