Skip to content
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

Tmp2 #525

Open
wants to merge 17 commits into
base: tmp
Choose a base branch
from
Open

Tmp2 #525

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
1 change: 1 addition & 0 deletions examples_tests/22.RaytracedAO/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ endif()

set(EXTRA_SOURCES
../../src/nbl/ext/DebugDraw/CDraw3DLine.cpp
../../src/nbl/ext/EnvmapImportanceSampling/EnvmapImportanceSampling.cpp
Renderer.cpp
CommandLineHandler.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion examples_tests/22.RaytracedAO/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ You can switch between those sensors using `PAGE UP/DOWN` Keys defined in more d
| tonemapper | Tonemapper Settings for Denoiser | string | "ACES=0.4,0.8"
| cropOffsetX, cropOffsetY | Used to control the offset for cropping cubemap renders (instead of highQualityEdges) | int | 0 |
| cropWidth, cropHeight | Used to control the size for cropping cubemap renders (instead of highQualityEdges) | int | width-cropOffsetX, height-cropOffsetY
| envmapRegularizationFactor | if RIS is enabled then paths will be guided towards envmap based on this regularization factor.<br><br>1.0 is based on product of envmap and bxdf<br>0.0 is based only on bxdf<br><br>But 1.0 is never a valid value to use.<br>Valid Range is [0.2, 0.8] | float | 0.0 |
| envmapRegularizationFactor | Fractional blend between guiding paths based on just the BxDF (0.0) or the product of the BxDF and the Environment Map (1.0)<br>Valid parameter ranges are between 0.0 and 0.8 as guiding fully by the product produces extreme fireflies from indirect light or local lights. | float | 0.5 |

### Example of a sensor using all new properties described above.
```xml
Expand Down
428 changes: 22 additions & 406 deletions examples_tests/22.RaytracedAO/Renderer.cpp

Large diffs are not rendered by default.

30 changes: 4 additions & 26 deletions examples_tests/22.RaytracedAO/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#undef PI

#include "nbl/ext/MitsubaLoader/CMitsubaLoader.h"
#include "nbl/ext/EnvmapImportanceSampling/EnvmapImportanceSampling.h"

#include <ISceneManager.h>

Expand All @@ -23,7 +24,7 @@ class Renderer : public nbl::core::IReferenceCounted, public nbl::core::Interfac
public:
#include "rasterizationCommon.h"
#include "raytraceCommon.h"
#include "warpCommon.h"
#include "nbl/builtin/glsl/ext/EnvmapImportanceSampling/parameters.glsl"
#ifdef __cplusplus
#undef uint
#undef vec4
Expand Down Expand Up @@ -129,7 +130,7 @@ class Renderer : public nbl::core::IReferenceCounted, public nbl::core::Interfac
void finalizeScene(InitializationData& initData);

//
nbl::core::smart_refctd_ptr<nbl::video::IGPUImageView> createTexture(uint32_t width, uint32_t height, nbl::asset::E_FORMAT format, uint32_t layers=0u);
nbl::core::smart_refctd_ptr<nbl::video::IGPUImageView> createTexture(uint32_t width, uint32_t height, nbl::asset::E_FORMAT format, uint32_t mipLevels=1u, uint32_t layers=0u);
nbl::core::smart_refctd_ptr<nbl::video::IGPUImageView> createScreenSizedTexture(nbl::asset::E_FORMAT format, uint32_t layers=0u);

//
Expand Down Expand Up @@ -246,31 +247,8 @@ class Renderer : public nbl::core::IReferenceCounted, public nbl::core::Interfac
nbl::core::smart_refctd_ptr<nbl::video::IGPURenderpassIndependentPipeline> blendEnvPipeline;
nbl::core::smart_refctd_ptr<nbl::video::IGPUDescriptorSetLayout> blendEnvDescriptorSet;
nbl::core::smart_refctd_ptr<nbl::video::IGPUMeshBuffer> blendEnvMeshBuffer;

// Shader and Resources for Generating Luminance MipMaps from EnvMap
static constexpr uint32_t MipCountLuminance = MipCountEnvmap;
nbl::core::smart_refctd_ptr<nbl::video::IGPUImageView> m_luminanceMipMaps[MipCountLuminance];
uint32_t m_lumaWorkGroups[2];
nbl::core::smart_refctd_ptr<nbl::video::IGPUDescriptorSetLayout> m_lumaDSLayout;
nbl::core::smart_refctd_ptr<nbl::video::IGPUDescriptorSet> m_lumaDS[MipCountLuminance - 1];
nbl::core::smart_refctd_ptr<nbl::video::IGPUPipelineLayout> m_lumaPipelineLayout;
nbl::core::smart_refctd_ptr<IGPUSpecializedShader> m_lumaGPUShader;
nbl::core::smart_refctd_ptr<nbl::video::IGPUComputePipeline> m_lumaPipeline;

// Shader and Resources for EnvironmentalMap Sample Warping
nbl::core::smart_refctd_ptr<nbl::video::IGPUImageView> m_warpMap; // Warps Sample based on EnvMap Luminance

nbl::core::smart_refctd_ptr<nbl::video::IGPUDescriptorSetLayout> m_warpDSLayout;
nbl::core::smart_refctd_ptr<nbl::video::IGPUDescriptorSet> m_warpDS;
nbl::core::smart_refctd_ptr<nbl::video::IGPUPipelineLayout> m_warpPipelineLayout;
nbl::core::smart_refctd_ptr<IGPUSpecializedShader> m_warpGPUShader;
nbl::core::smart_refctd_ptr<nbl::video::IGPUComputePipeline> m_warpPipeline;

void initWarpingResources();
void deinitWarpingResources();

// returns if RIS should be enabled based on variance calculations
bool computeWarpMap(float envMapRegularizationFactor);
nbl::ext::EnvmapImportanceSampling::EnvmapImportanceSampling m_envMapImportanceSampling;

std::future<bool> compileShadersFuture;
};
Expand Down
8 changes: 6 additions & 2 deletions examples_tests/22.RaytracedAO/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ int main(int argc, char** argv)
mainSensorData.denoiserInfo.bloomIntensity = film.denoiserBloomIntensity;
mainSensorData.denoiserInfo.tonemapperArgs = std::string(film.denoiserTonemapperArgs);
mainSensorData.fileFormat = film.fileFormat;
mainSensorData.envmapRegFactor = core::clamp(film.envmapRegularizationFactor, 0.2f, 0.8f);
mainSensorData.envmapRegFactor = core::clamp(film.envmapRegularizationFactor, 0.0f, 0.8f);
mainSensorData.outputFilePath = std::filesystem::path(film.outputFilePath);
if(!isFileExtensionCompatibleWithFormat(mainSensorData.outputFilePath.extension().string(), mainSensorData.fileFormat))
{
Expand Down Expand Up @@ -621,7 +621,11 @@ int main(int argc, char** argv)
{
mainSensorData.width = film.cropWidth;
mainSensorData.height = film.cropHeight;
assert(film.cropOffsetX == 0 && film.cropOffsetY == 0);

if(film.cropOffsetX != 0 || film.cropOffsetY != 0)
{
std::cout << "[WARN] CropOffsets are non-zero. cropping is not supported for non cubemap renders." << std::endl;
}

mainSensorData.staticCamera = smgr->addCameraSceneNode(nullptr);
auto& staticCamera = mainSensorData.staticCamera;
Expand Down
63 changes: 27 additions & 36 deletions examples_tests/22.RaytracedAO/raytraceCommon.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
#define _RAYTRACE_COMMON_GLSL_INCLUDED_

#include "virtualGeometry.glsl"
#include "warpCommon.h"
#include <nbl/builtin/glsl/sampling/envmap.glsl>

#include <nbl/builtin/glsl/limits/numeric.glsl>

// #define ONLY_BXDF_SAMPLING
// #define ONLY_ENV_SAMPLING

layout(push_constant, row_major) uniform PushConstants
{
RaytraceShaderCommonData_t cummon;
Expand Down Expand Up @@ -50,7 +46,7 @@ layout(set = 2, binding = 6, r32ui) restrict uniform uimage2DArray normalAOV;
// environment emitter
layout(set = 2, binding = 7) uniform sampler2D envMap;
layout(set = 2, binding = 8) uniform sampler2D warpMap;
layout(set = 2, binding = 9) uniform sampler2D luminance[MAX_LUMINANCE_LEVELS];
layout(set = 2, binding = 9) uniform sampler2D luminance;

void clear_raycount()
{
Expand Down Expand Up @@ -260,25 +256,16 @@ vec3 load_normal_and_prefetch_textures(
return geomNormal;
}

vec3 nbl_glsl_unormSphericalToCartesian(in vec2 uv, out float sinTheta)
{
vec3 dir;
nbl_glsl_sincos((uv.x-0.5)*2.f*nbl_glsl_PI,dir.y,dir.x);
nbl_glsl_sincos(uv.y*nbl_glsl_PI,sinTheta,dir.z);
dir.xy *= sinTheta;
return dir;
}

// return regularized pdf of sample
float Envmap_regularized_deferred_pdf(in vec3 rayDirection)
{
const ivec2 luminanceMapSize = textureSize(luminance[0], 0);
uint lastLuminanceMip = uint(log2(luminanceMapSize.x)); // TODO: later turn into push constant
const vec2 envmapUV = nbl_glsl_sampling_generateUVCoordFromDirection(rayDirection);
const ivec2 luminanceMapSize = textureSize(luminance, 0);
int lastLuminanceMip = int(log2(luminanceMapSize.x)); // TODO: later turn into push constant
const vec2 envmapUV = nbl_glsl_sampling_envmap_generateUVCoordFromDirection(rayDirection);

float sinTheta = length(rayDirection.zx);
float sumLum = texelFetch(luminance[lastLuminanceMip], ivec2(0), 0).r;
float lum = textureLod(luminance[0], envmapUV, 0).r;
float sumLum = texelFetch(luminance, ivec2(0), lastLuminanceMip).r;
float lum = textureLod(luminance, envmapUV, 0).r;
float bigfactor = float(luminanceMapSize.x*luminanceMapSize.y)/sumLum;
return bigfactor*(lum/(sinTheta*2.0f*nbl_glsl_PI*nbl_glsl_PI));
}
Expand Down Expand Up @@ -309,7 +296,7 @@ void Envmap_generateRegularizedSample_and_pdf(out float pdf, out nbl_glsl_LightS
const vec2 uv = yDiff*interpolant.y+yVals[0];

float sinTheta;
const vec3 L = nbl_glsl_unormSphericalToCartesian(uv, sinTheta);
const vec3 L = nbl_glsl_sampling_envmap_generateDirectionFromUVCoord(uv, sinTheta);
lightSample = nbl_glsl_createLightSample(L, interaction);

const float detInterpolJacobian = determinant(mat2(
Expand All @@ -322,36 +309,41 @@ void Envmap_generateRegularizedSample_and_pdf(out float pdf, out nbl_glsl_LightS
}

#include <nbl/builtin/glsl/sampling/quantized_sequence.glsl>
mat2x3 rand6d(in uvec3 scramble_key, in int _sample, int depth)
mat2x3 rand6d(in uvec3 scramble_keys[2], in int _sample, int depth)
{
mat2x3 retVal;
// decrement depth because first vertex is rasterized and picked with a different sample sequence
--depth;
//
int offset = int(_sample)*SAMPLE_SEQUENCE_STRIDE+depth;
int eachStrategyStride = SAMPLE_SEQUENCE_STRIDE/2; // get this from cpp side?
int eachStrategyStride = SAMPLE_SEQUENCE_STRIDE/SAMPLING_STRATEGY_COUNT;

const nbl_glsl_sampling_quantized3D quant1 = texelFetch(quantizedSampleSequence, offset).xy;
const nbl_glsl_sampling_quantized3D quant2 = texelFetch(quantizedSampleSequence, offset + eachStrategyStride).xy;
retVal[0] = nbl_glsl_sampling_decodeSample3Dimensions(quant1,scramble_key);
retVal[1] = nbl_glsl_sampling_decodeSample3Dimensions(quant2,scramble_key);
retVal[0] = nbl_glsl_sampling_decodeSample3Dimensions(quant1,scramble_keys[0]);
retVal[1] = nbl_glsl_sampling_decodeSample3Dimensions(quant2,scramble_keys[1]);
return retVal;
}

nbl_glsl_MC_quot_pdf_aov_t gen_sample_ray(
out vec3 direction,
in uvec3 scramble_key,
in uvec3 scramble_keys[2],
in uint sampleID, in uint depth,
in nbl_glsl_MC_precomputed_t precomp,
in nbl_glsl_MC_instr_stream_t gcs,
in nbl_glsl_MC_instr_stream_t rnps
)
{
mat2x3 rand = rand6d(scramble_key,int(sampleID),int(depth));
mat2x3 rand = rand6d(scramble_keys,int(sampleID),int(depth));

// (1) BXDF Sample and Weight
nbl_glsl_LightSample bxdfSample;
nbl_glsl_MC_quot_pdf_aov_t bxdfCosThroughput = nbl_glsl_MC_runGenerateAndRemainderStream(precomp,gcs,rnps,rand[0],bxdfSample);

nbl_glsl_LightSample outSample;
nbl_glsl_MC_quot_pdf_aov_t result;

#ifndef ONLY_BXDF_SAMPLING
float bxdfWeight = 0;

float p_bxdf_bxdf = bxdfCosThroughput.pdf; // BxDF PDF evaluated with BxDF sample (returned from
Expand Down Expand Up @@ -407,10 +399,7 @@ nbl_glsl_MC_quot_pdf_aov_t gen_sample_ray(
}

const float bxdfChoiceProb = w_bxdf/w_sum;
#endif

nbl_glsl_LightSample outSample;
nbl_glsl_MC_quot_pdf_aov_t result;
#endif // ifdef TRADE_REGISTERS_FOR_IEEE754_ACCURACY

float rcpChoiceProb;
float w_star_over_p_env = w_sum;
Expand All @@ -429,13 +418,11 @@ nbl_glsl_MC_quot_pdf_aov_t gen_sample_ray(

result.quotient *= w_star_over_p_env;
result.pdf /= w_star_over_p_env;
#endif // ifndef ONLY_BXDF_SAMPLING

#ifdef ONLY_BXDF_SAMPLING
outSample = bxdfSample;
result = bxdfCosThroughput;
#elif defined(ONLY_ENV_SAMPLING)
outSample = envmapSample;
result = envmapSampleThroughput;
#endif

// russian roulette
Expand Down Expand Up @@ -479,12 +466,16 @@ void generate_next_rays(
vec3 nextThroughput[MAX_RAYS_GENERATED];
float nextAoVThroughputScale[MAX_RAYS_GENERATED];
{
const uvec3 scramble_key = uvec3(nbl_glsl_xoroshiro64star(scramble_state),nbl_glsl_xoroshiro64star(scramble_state),nbl_glsl_xoroshiro64star(scramble_state));
const uvec3 scramble_keys[2] = {
uvec3(nbl_glsl_xoroshiro64star(scramble_state),nbl_glsl_xoroshiro64star(scramble_state),nbl_glsl_xoroshiro64star(scramble_state)),
uvec3(nbl_glsl_xoroshiro64star(scramble_state),nbl_glsl_xoroshiro64star(scramble_state),nbl_glsl_xoroshiro64star(scramble_state))
};

for (uint i=0u; i<maxRaysToGen; i++)
{
maxT[i] = 0.f;
// TODO: When generating NEE rays, advance the dimension, NOT the sampleID
const nbl_glsl_MC_quot_pdf_aov_t result = gen_sample_ray(direction[i],scramble_key,sampleID+i,vertex_depth,precomputed,gcs,rnps);
const nbl_glsl_MC_quot_pdf_aov_t result = gen_sample_ray(direction[i],scramble_keys,sampleID+i,vertex_depth,precomputed,gcs,rnps);
albedo += result.aov.albedo/float(maxRaysToGen);
worldspaceNormal += result.aov.normal/float(maxRaysToGen);

Expand Down Expand Up @@ -549,7 +540,7 @@ struct Contribution

void Contribution_initMiss(out Contribution contrib, in float aovThroughputScale)
{
vec2 uv = nbl_glsl_sampling_generateUVCoordFromDirection(-normalizedV);
vec2 uv = nbl_glsl_sampling_envmap_generateUVCoordFromDirection(-normalizedV);
// funny little trick borrowed from things like Progressive Photon Mapping
const float bias = 0.0625f*(1.f-aovThroughputScale)*pow(pc.cummon.rcpFramesDispatched,0.08f);
contrib.albedo = contrib.color = textureGrad(envMap, uv, vec2(bias*0.5,0.f), vec2(0.f,bias)).rgb;
Expand Down
25 changes: 0 additions & 25 deletions examples_tests/22.RaytracedAO/warpCommon.h

This file was deleted.

3 changes: 3 additions & 0 deletions include/nbl/asset/filters/CBlitImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,10 @@ class CBlitImageFilter : public CImageFilter<CBlitImageFilter<Swizzle,Dither,Nor
});
// we'll only get here if we have to do coverage adjustment
if (needsNormalization && lastPass)
{
state->normalization.finalize<double>();
storeToImage(core::rational<>(inv_cvg_num,inv_cvg_den),axis,outOffsetLayer);
}
};
// filter in X-axis
filterAxis(IImage::ET_1D,kernelX);
Expand Down
5 changes: 3 additions & 2 deletions include/nbl/asset/filters/CSwizzleAndConvertImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class CSwizzleAndConvertImageFilterBase : public CSwizzleableAndDitherableFilter
static inline void normalizationPrepass(E_FORMAT rInFormat, const ExecutionPolicy& policy, state_type* state, const core::vectorSIMDu32& blockDims)
{
if constexpr (!std::is_void_v<Normalization>)
{
{
assert(kInFormat==EF_UNKNOWN || rInFormat==EF_UNKNOWN);
state->normalization.initialize<decodeBufferType>();
state->normalization.initialize<encodeBufferType>();
auto perOutputRegion = [policy,&blockDims,&state,rInFormat](const CMatchedSizeInOutImageFilterCommon::CommonExecuteData& commonExecuteData, CBasicImageFilterCommon::clip_region_functor_t& clip) -> bool
{
auto normalizePrepass = [&commonExecuteData,&blockDims,&state,rInFormat](uint32_t readBlockArrayOffset, core::vectorSIMDu32 readBlockPos)
Expand All @@ -84,6 +84,7 @@ class CSwizzleAndConvertImageFilterBase : public CSwizzleableAndDitherableFilter
return true;
};
CMatchedSizeInOutImageFilterCommon::commonExecute(state,perOutputRegion);
state->normalization.finalize<encodeBufferType>();
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion include/nbl/asset/filters/NormalizationStates.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class CDerivativeMapNormalizationState : public impl::CDerivativeMapNormalizatio
static_assert(std::is_floating_point_v<Tenc>, "Integer encode types not supported yet!");
if constexpr (isotropic)
{
const float isotropicMax = core::max(core::max(maxAbsPerChannel[0],maxAbsPerChannel[1]),core::max(maxAbsPerChannel[2],maxAbsPerChannel[3]));
float isotropicMax = core::max(core::max(maxAbsPerChannel[0].load(),maxAbsPerChannel[1].load()),core::max(maxAbsPerChannel[2].load(),maxAbsPerChannel[3].load()));
for (auto i=0u; i<4u; i++)
maxAbsPerChannel[i] = isotropicMax;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#version 430 core
#ifndef _NBL_GLSL_EXT_ENVMAP_SAMPLING_GEN_LUMA_INCLUDED_
#define _NBL_GLSL_EXT_ENVMAP_SAMPLING_GEN_LUMA_INCLUDED_

#include "warpCommon.h"
#include <nbl/builtin/glsl/ext/EnvmapImportanceSampling/parameters.glsl>
#include <nbl/builtin/glsl/math/functions.glsl>

layout(local_size_x = LUMA_MIP_MAP_GEN_WORKGROUP_DIM, local_size_y = LUMA_MIP_MAP_GEN_WORKGROUP_DIM) in;
Expand Down Expand Up @@ -35,8 +36,8 @@ void main()
if(pc.data.calcLuma > 0)
{
float sinTheta = sin(nbl_glsl_PI * (float(pixelCoord.y + 0.5f) / envMapSize.y));
vec4 envMapSample = texelFetch(envMap, pixelCoord, 0);
float luma = dot(pc.data.luminanceScales, envMapSample);
vec3 envMapSample = texelFetch(envMap, pixelCoord, 0).rgb;
float luma = dot(pc.data.luminanceScales, vec4(envMapSample, 1.0f));
if(pc.data.sinFactor > 0)
luma *= sinTheta;
imageStore(srcLuminance, pixelCoord, vec4(luma));
Expand All @@ -54,4 +55,6 @@ void main()
}
}
}
}
}

#endif
Loading