Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions 75_EnvmapImportanceSamplingTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
include(common RESULT_VARIABLE RES)
if(NOT RES)
message(FATAL_ERROR "common.cmake not found. Should be in {repo_root}/cmake directory")
endif()

nbl_create_executable_project("" "" "" "" "${NBL_EXECUTABLE_PROJECT_CREATION_PCH_TARGET}")

if(NBL_EMBED_BUILTIN_RESOURCES)
set(_BR_TARGET_ ${EXECUTABLE_NAME}_builtinResourceData)
set(RESOURCE_DIR "app_resources")

get_filename_component(_SEARCH_DIRECTORIES_ "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE)
get_filename_component(_OUTPUT_DIRECTORY_SOURCE_ "${CMAKE_CURRENT_BINARY_DIR}/src" ABSOLUTE)
get_filename_component(_OUTPUT_DIRECTORY_HEADER_ "${CMAKE_CURRENT_BINARY_DIR}/include" ABSOLUTE)

file(GLOB_RECURSE BUILTIN_RESOURCE_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE_DIR}" CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE_DIR}/*")
foreach(RES_FILE ${BUILTIN_RESOURCE_FILES})
LIST_BUILTIN_RESOURCE(RESOURCES_TO_EMBED "${RES_FILE}")
endforeach()

ADD_CUSTOM_BUILTIN_RESOURCES(${_BR_TARGET_} RESOURCES_TO_EMBED "${_SEARCH_DIRECTORIES_}" "${RESOURCE_DIR}" "nbl::this_example::builtin" "${_OUTPUT_DIRECTORY_HEADER_}" "${_OUTPUT_DIRECTORY_SOURCE_}")

LINK_BUILTIN_RESOURCES_TO_TARGET(${EXECUTABLE_NAME} ${_BR_TARGET_})
endif()

add_dependencies(${EXECUTABLE_NAME} argparse)
target_include_directories(${EXECUTABLE_NAME} PUBLIC $<TARGET_PROPERTY:argparse,INTERFACE_INCLUDE_DIRECTORIES>)

Comment on lines +1 to +28
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnastaZIuk review and okay the CMake

enable_testing()

add_test(NAME NBL_IMAGE_HASH_RUN_TESTS
COMMAND "$<TARGET_FILE:${EXECUTABLE_NAME}>" --test hash
WORKING_DIRECTORY "$<TARGET_FILE_DIR:${EXECUTABLE_NAME}>"
COMMAND_EXPAND_LISTS
)

set(OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/auto-gen")

set(SM 6_8)
set(JSON [=[
[
{
"INPUT": "app_resources/test.comp.hlsl",
"KEY": "test",
}]
]=])
string(CONFIGURE "${JSON}" JSON)

set(COMPILE_OPTIONS
-I "${CMAKE_CURRENT_SOURCE_DIR}"
-T lib_${SM}
)

NBL_CREATE_NSC_COMPILE_RULES(
TARGET ${EXECUTABLE_NAME}SPIRV
LINK_TO ${EXECUTABLE_NAME}
BINARY_DIR ${OUTPUT_DIRECTORY}
MOUNT_POINT_DEFINE NBL_THIS_EXAMPLE_BUILD_MOUNT_POINT
COMMON_OPTIONS ${COMPILE_OPTIONS}
OUTPUT_VAR KEYS
INCLUDE nbl/this_example/builtin/build/spirv/keys.hpp
NAMESPACE nbl::this_example::builtin::build
INPUTS ${JSON}
)

NBL_CREATE_RESOURCE_ARCHIVE(
NAMESPACE nbl::this_example::builtin::build
TARGET ${EXECUTABLE_NAME}_builtinsBuild
LINK_TO ${EXECUTABLE_NAME}
BIND ${OUTPUT_DIRECTORY}
BUILTINS ${KEYS}
)
38 changes: 38 additions & 0 deletions 75_EnvmapImportanceSamplingTest/app_resources/common.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef _ENVMAP_IMPORTANCE_SAMPLING_SEARCH_H_INCLUDED_
#define _ENVMAP_IMPORTANCE_SAMPLING_SEARCH_H_INCLUDED_

#include <nbl/builtin/hlsl/cpp_compat/basic.h>
#include <nbl/builtin/hlsl/cpp_compat/vector.hlsl>

using namespace nbl;
using namespace nbl::hlsl;

NBL_CONSTEXPR uint32_t WorkgroupSize = 128;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

64 to be more representative of path tracing workloads


struct STestPushConstants
{
uint32_t warpWidth : 16;
uint32_t warpHeight : 16;
float32_t eps;
uint64_t outputAddress;
float32_t avgLuma;
};

struct TestOutput
{
float32_t3 L;
float32_t jacobian;
float32_t pdf;
float32_t deferredPdf;
};

struct TestSample
{
TestOutput directOutput;
TestOutput cachedOutput;
float32_t2 xi;
};

using test_sample_t = TestSample;

#endif // _COOPERATIVE_BINARY_SEARCH_H_INCLUDED_
24 changes: 24 additions & 0 deletions 75_EnvmapImportanceSamplingTest/app_resources/present.frag.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (C) 2024-2024 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h

#pragma wave shader_stage(fragment)

// vertex shader is provided by the fullScreenTriangle extension
#include <nbl/builtin/hlsl/ext/FullScreenTriangle/SVertexAttributes.hlsl>
using namespace nbl::hlsl::ext::FullScreenTriangle;

[[vk::binding(0, 3)]] Texture2D<float32_t2> warpMap;
[[vk::combinedImageSampler]][[vk::binding(1, 3)]] Texture2D<float32_t4> envMap;
[[vk::combinedImageSampler]][[vk::binding(1, 3)]] SamplerState envMapSampler;

[[vk::location(0)]] float32_t4 main(SVertexAttributes vxAttr) : SV_Target0
{
uint width;
uint height;
warpMap.GetDimensions(width, height);
float32_t2 uv = warpMap.Load(uint32_t3(width * vxAttr.uv.x, height * vxAttr.uv.y, 0));
float32_t4 color = envMap.Sample(envMapSampler, uv);

return float32_t4(color.xyz, 1.0);
}
125 changes: 125 additions & 0 deletions 75_EnvmapImportanceSamplingTest/app_resources/test.comp.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include "common.hlsl"

#include <nbl/builtin/hlsl/sampling/hierarchical_image.hlsl>
#include <nbl/builtin/hlsl/random/pcg.hlsl>
#include <nbl/builtin/hlsl/random/dim_adaptor_recursive.hlsl>
#include <nbl/builtin/hlsl/tgmath.hlsl>
#include <nbl/builtin/hlsl/sampling/warps/spherical.hlsl>
#include <nbl/builtin/hlsl/cpp_compat.hlsl>

[[vk::push_constant]] STestPushConstants pc;

[[vk::combinedImageSampler]][[vk::binding(0, 0)]] Texture2D<float32_t> lumaMap;
[[vk::combinedImageSampler]][[vk::binding(0, 0)]] SamplerState lumaSampler;

[[vk::combinedImageSampler]][[vk::binding(1, 0)]] Texture2D<float32_t2> warpMap;
[[vk::combinedImageSampler]][[vk::binding(1, 0)]] SamplerState warpSampler;

using namespace nbl::hlsl::sampling::hierarchical_image;


struct LuminanceAccessor
{
template <typename ValT, typename IndexT
NBL_FUNC_REQUIRES(
concepts::same_as<IndexT, float32_t2> &&
concepts::same_as<ValT, float32_t>
)
void get(IndexT index, NBL_REF_ARG(ValT) val)
{
val = lumaMap.SampleLevel(lumaSampler, index, 0);
}

float32_t load(uint32_t2 coord, uint32_t level)
{
return lumaMap.Load(uint32_t3(coord, level));
}

};

struct WarpmapAccessor
{
void gatherUv(float32_t2 xi, NBL_REF_ARG(matrix<float, 4, 2>) uvs, NBL_REF_ARG(float32_t2) interpolant) NBL_CONST_MEMBER_FUNC
{
float32_t2 texelCoord = xi * float32_t2(pc.warpWidth - 1, pc.warpHeight - 1);
interpolant = frac(texelCoord);
uint32_t2 uv = texelCoord / float32_t2(pc.warpWidth, pc.warpHeight);
const float32_t4 reds = warpMap.GatherRed(warpSampler, uv);
const float32_t4 greens = warpMap.GatherGreen(warpSampler, uv);
Comment on lines +47 to +48
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this even compile? HLSL GatherRed takes a sampler object and then a floating point coordinate

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the / float32_t2(pc.warpWidth, pc.warpHeight) division shouldn't be there


Comment on lines +46 to +49
Copy link
Copy Markdown
Member

@devshgraphicsprogramming devshgraphicsprogramming Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

youre corner sampling, so you need to add half a pixel to texelCoord when you make texelCoord out of it before the frac

Right now you're gathering the wrong pixels - for the same floor(texelCoord) you're getting different values from your gather

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would actually make sense to pull this logic out of the warpmap accessor, and have the warpmap accessor just report the size of the warpmap and let the Warpmap sampler deal with corner sampling and computing the interpolant while this function would just take uint16_t coord instead of xi and interpolant arguments

uvs = transpose(matrix<float, 2, 4>(
reds,
greens
));

}
};


template <typename WarpSamplerT>
TestOutput GenerateTestOutput(NBL_CONST_REF_ARG(WarpSamplerT) hImage, float32_t2 xi)
{
using sample_type = typename WarpSamplerT::sample_type;

const sample_type sample = hImage.generate(xi);
const float3 L = sample.value();

float eps_x = pc.eps;
float eps_y = pc.eps;

const sample_type sample_plus_du = hImage.generate(xi + float32_t2(0.5f * eps_x, 0));
const float3 L_plus_du = sample_plus_du.value();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when PR 1001 comes, you'll be getting these via forwardPdf then it will be useful to test that backwardPdf matches if the WarpSamplerT is BackwardTractableSampler

In either case you'll want to test that forwardWeight == backwardWeight

const sample_type sample_plus_dv = hImage.generate(xi + float32_t2(0, 0.5f * eps_y));
const float3 L_plus_dv = sample_plus_dv.value();

const sample_type sample_minus_du = hImage.generate(xi - float32_t2(0.5f * eps_x, 0));
const float3 L_minus_du = sample_minus_du.value();
const sample_type sample_minus_dv = hImage.generate(xi - float32_t2(0, 0.5f * eps_y));
const float3 L_minus_dv = sample_minus_dv.value();

float jacobian = length(cross(L_plus_du - L_minus_du, L_plus_dv - L_minus_dv)) / (eps_x * eps_y);

TestOutput testOutput;
testOutput.L = L;
testOutput.jacobian = jacobian;
testOutput.pdf = sample.pdf();
Comment on lines +64 to +85
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sample is a HLSL keyword, use _sample instead

return testOutput;
}

float32_t2 convertToFloat01(uint32_t2 xi_uint)
{
return float32_t2(xi_uint) / promote<float32_t2>(float32_t(numeric_limits<uint32_t>::max));
}

[numthreads(WorkgroupSize, 1, 1)]
[shader("compute")]
void main(uint32_t3 threadID : SV_DispatchThreadID)
{
const LuminanceAccessor luminanceAccessor;
const WarpmapAccessor warpmapAccessor;

using direct_hierarchical_image_type = sampling::HierarchicalWarpSampler<float, LuminanceAccessor, sampling::warp::Spherical<float> >;

float32_t eps = pc.eps;

random::PCG32 pcg = random::PCG32::construct(threadID.x);
uint32_t2 xi_uint = random::DimAdaptorRecursive<random::PCG32, 2>::__call(pcg);

float32_t2 xi = convertToFloat01(xi_uint);

xi.x = hlsl::clamp(xi.x, eps, 1.f - eps);
xi.y = hlsl::clamp(xi.y, eps, 1.f - eps);

test_sample_t testSample;
testSample.xi = xi;

uint32_t2 warpResolution = { pc.warpWidth, pc.warpHeight };
const direct_hierarchical_image_type directHImage = direct_hierarchical_image_type::create(luminanceAccessor, pc.avgLuma, warpResolution, warpResolution.x != warpResolution.y);

testSample.directOutput = GenerateTestOutput(directHImage, xi);

using cached_hierarchical_image_type = sampling::WarpmapSampler<float, LuminanceAccessor, WarpmapAccessor, sampling::warp::Spherical<float> >;
Comment on lines +101 to +121
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Spherical warp you're using, is not aware of the corner sampling, so the hierarchical sampler outputs uv for corner sampling and you're warping them like regular UVs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok as long as you make the HierarchicalWarpGenerator (to be renamed HierarchicalSampler) handle all the corner sampling for you transparently and return and accept corner sampled as [0,1]^2 relative to the corners, this can stay as is in the examples

const cached_hierarchical_image_type cachedHImage = cached_hierarchical_image_type::create(luminanceAccessor, warpmapAccessor, warpResolution, pc.avgLuma);
testSample.cachedOutput = GenerateTestOutput(cachedHImage, xi);
vk::RawBufferStore<test_sample_t>(pc.outputAddress + threadID.x * sizeof(test_sample_t), testSample);
}
28 changes: 28 additions & 0 deletions 75_EnvmapImportanceSamplingTest/config.json.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"enableParallelBuild": true,
"threadsPerBuildProcess" : 2,
"isExecuted": false,
"scriptPath": "",
"cmake": {
"configurations": [ "Release", "Debug", "RelWithDebInfo" ],
"buildModes": [],
"requiredOptions": []
},
"profiles": [
{
"backend": "vulkan",
"platform": "windows",
"buildModes": [],
"runConfiguration": "Release",
"gpuArchitectures": []
}
],
"dependencies": [],
"data": [
{
"dependencies": [],
"command": [""],
"outputs": []
}
]
}
7 changes: 7 additions & 0 deletions 75_EnvmapImportanceSamplingTest/imagesTestList.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; This is the testing suite for various Nabla loaders/writers (JPG/PNG/TGA/BMP/DDS/KTX).
; BMP is currently unsupported for now.
; 16-bit PNG & 8-bit RLE (compressed) TGA is not supported.
; For licensing attribution, see LICENSE.

; JPG, colored & 8-bit grayscale
../../media/envmap/envmap_1.exr
Loading