Skip to content

Example 26 box blur #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
19 changes: 19 additions & 0 deletions 26_CentralLimitBoxBlur/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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}" "${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()
50 changes: 50 additions & 0 deletions 26_CentralLimitBoxBlur/app_resources/descriptors.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "nbl/builtin/hlsl/blur/common.hlsl"

[[vk::binding( 0, 0 )]] Texture2D<nbl::hlsl::float32_t4> input;
[[vk::binding( 1, 0 )]] RWTexture2D<nbl::hlsl::float32_t4> output;


// TODO: figure the proper way to do templated BufferAccessors
struct BufferAccessor
{
uint32_t2 chosenAxis;

nbl::hlsl::float32_t get( const uint32_t linearIndex, const uint32_t channel )
{
uint32_t3 texSize;
input.GetDimensions( 0, texSize.x, texSize.y, texSize.z );

uint32_t axisSize = dot( texSize.xy, chosenAxis );

uint32_t2 coordinate = { linearIndex % axisSize, linearIndex / axisSize };
float32_t data = 0.f;
if( all( coordinate < texSize.xy ) )
{
float32_t4 pixel = input[ coordinate.xy ];
data = pixel[ channel ];
}

return data;
}

void set( const uint32_t linearIndex, const uint32_t channel, NBL_CONST_REF_ARG( float32_t ) val )
{
uint32_t2 texSize;
output.GetDimensions( texSize.x, texSize.y );

uint32_t axisSize = dot( texSize, chosenAxis );

uint32_t2 coordinate = { linearIndex % axisSize, linearIndex / axisSize };
if( all( coordinate < texSize ) )
{
output[ coordinate.xy ][ channel ] = val;
}
}
};

BufferAccessor BufferAccessorCtor( uint32_t2 chosenAxis )
{
BufferAccessor ba;
ba.chosenAxis = chosenAxis;
return ba;
}
32 changes: 32 additions & 0 deletions 26_CentralLimitBoxBlur/app_resources/main.comp.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2018-2023 - 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 shader_stage(compute)

#include "nbl/builtin/hlsl/blur/common.hlsl"
#include "descriptors.hlsl"

#include "nbl/builtin/hlsl/blur/box_blur.hlsl"

[[vk::push_constant]]
BoxBlurParams boxBlurParams;

[numthreads( WORKGROUP_SIZE, 1, 1 )]
void main( uint3 invocationID : SV_DispatchThreadID )
{
uint32_t direction = boxBlurParams.getDirection();
uint32_t wrapMode = boxBlurParams.getWrapMode();
nbl::hlsl::float32_t4 borderColor = float32_t4(1.f, 0.f, 1.f, 1.f);
if( boxBlurParams.getWrapMode() == WRAP_MODE_CLAMP_TO_BORDER )
{
borderColor = boxBlurParams.getBorderColor();
}

BufferAccessor textureAccessor = BufferAccessorCtor( boxBlurParams.chosenAxis );

for( uint32_t ch = 0; ch < boxBlurParams.getChannelCount(); ++ch )
{
BoxBlur( ch, boxBlurParams.radius, wrapMode, borderColor, textureAccessor );
}
}
Loading