Skip to content

Commit

Permalink
Volumetric: Fixed volumetric scattering.
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonJoker committed Jul 23, 2023
1 parent 8c0d8b4 commit 2295565
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 155 deletions.
2 changes: 1 addition & 1 deletion include/Core/Castor3D/Buffer/StagedUploadData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ namespace castor3d
VkDeviceSize currentSize{};
VkDeviceSize buffersCount{};

FrameBuffers( ashes::SemaphorePtr psemaphore = {}
explicit FrameBuffers( ashes::SemaphorePtr psemaphore = {}
, BuffersRanges pbuffers = {}
, bool pused = true )noexcept
: buffers{ std::move( pbuffers ) }
Expand Down
2 changes: 0 additions & 2 deletions include/Core/Castor3D/Buffer/UniformBufferPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ namespace castor3d

private:
RenderDevice const & m_device;
uint32_t m_maxUboElemCount{ 0u };
uint32_t m_maxUboSize{ 0u };
uint32_t m_maxPoolUboCount{ 100u };
uint32_t m_currentUboIndex{ 0u };
std::map< uint32_t, BufferArray > m_buffers;
castor::String m_debugName;
Expand Down
1 change: 0 additions & 1 deletion include/Core/Castor3D/Buffer/UniformBufferPool.inl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace castor3d

if ( itB == it->second.end() )
{
CU_Require( m_currentUboIndex < m_maxPoolUboCount - 1u );
itB = doCreatePoolBuffer( flags, it->second );
CU_Require( itB != it->second.end() );
}
Expand Down
4 changes: 1 addition & 3 deletions include/Core/Castor3D/Shader/ShaderAppendBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ namespace castor3d
C3D_API ShaderAppendBuffer( Engine & engine
, RenderDevice const & device
, VkDeviceSize size
, castor::String name
, crg::AccessState wantedState = { VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT } );
, castor::String name );
/**
*\~english
*\brief Creates the descriptor set layout binding at given point.
Expand Down Expand Up @@ -113,7 +112,6 @@ namespace castor3d
private:
RenderDevice const & m_device;
VkDeviceSize m_size;
crg::AccessState m_wantedState;
ashes::BufferBasePtr m_buffer;
};
}
Expand Down
10 changes: 9 additions & 1 deletion include/Core/Castor3D/Shader/Shaders/GlslLighting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ namespace castor3d::shader
, sdw::Vec3 const & radiance
, sdw::UInt const & receivesShadows
, sdw::Vec3 & output );
C3D_API void doApplyVolumetric( ShadowData const & shadows
, sdw::Int const shadowMapIndex
, sdw::Vec2 const & lightIntensity
, LightSurface const & lightSurface
, sdw::Vec3 & output
, bool multiply );

C3D_API virtual void doInitialiseBackground( BackgroundModel & background );
C3D_API virtual sdw::Vec3 doComputeRadiance( Light const & light
Expand All @@ -192,7 +198,9 @@ namespace castor3d::shader
, LightSurface const & lightSurface
, sdw::Float const & isLit
, sdw::Vec2 & output );
C3D_API virtual void doComputeScatteringTerm( sdw::Vec3 const & radiance
C3D_API virtual void doComputeScatteringTerm( ShadowData const & shadows
, sdw::Int const shadowMapIndex
, sdw::Vec3 const & radiance
, sdw::Vec2 const & lightIntensity
, BlendComponents const & components
, LightSurface const & lightSurface
Expand Down
2 changes: 1 addition & 1 deletion source/Core/Castor3D/Buffer/DirectUploadData.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Castor3D/Buffer/DirectUploadData.hpp"

#include "Castor3D/Buffer/GpuBUffer.hpp"
#include "Castor3D/Buffer/GpuBuffer.hpp"
#include "Castor3D/Miscellaneous/Logger.hpp"
#include "Castor3D/Render/RenderDevice.hpp"
#include "Castor3D/Render/RenderSystem.hpp"
Expand Down
2 changes: 1 addition & 1 deletion source/Core/Castor3D/Buffer/StagedUploadData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace castor3d
totalSize += buffer->getSize();
}

m_buffers = {};
m_buffers = { FrameBuffers{}, FrameBuffers{} };
log::info << " Staging Buffers total allocated size: " << totalSize << " bytes" << std::endl;
}

Expand Down
14 changes: 14 additions & 0 deletions source/Core/Castor3D/Buffer/UploadData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ namespace castor3d
std::vector< ImageDataRange > * pendingImages;
traceUpload( "Start upload" << std::endl );
doPreprocess( pendingBuffers, pendingImages );
#if C3D_DebugUpload
VkDeviceSize size{};

for ( auto & upload : *pendingBuffers )
Expand All @@ -115,6 +116,19 @@ namespace castor3d

doPostprocess();
traceUpload( "End upload, total size: " << size << std::endl );
#else
for ( auto & upload : *pendingBuffers )
{
doUpload( upload );
}

for ( auto & upload : *pendingImages )
{
doUpload( upload );
}

doPostprocess();
#endif
m_pendingBuffers.clear();
m_pendingImages.clear();
}
Expand Down
104 changes: 0 additions & 104 deletions source/Core/Castor3D/Material/Texture/TextureLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,100 +340,6 @@ namespace castor3d
return result;
}

static void update( MipView & view
, VkImage image
, uint32_t baseArrayLayer
, uint32_t layerCount
, uint32_t baseMipLevel
, uint32_t levelCount )
{
view.view->update( image
, baseArrayLayer
, layerCount
, baseMipLevel
, levelCount );

for ( auto & level : view.levels )
{
level->update( image
, baseArrayLayer
, layerCount
, baseMipLevel++
, 1u );
}
}

static void update( MipView & view
, VkImage image
, uint32_t & baseArrayLayer
, uint32_t baseMipLevel
, uint32_t levelCount )
{
update( view
, image
, baseArrayLayer++
, 1u
, baseMipLevel
, levelCount );
}

static void update( CubeView & view
, VkImage image
, uint32_t & baseArrayLayer
, uint32_t baseMipLevel
, uint32_t levelCount )
{
update( view.view
, image
, baseArrayLayer
, 6u
, baseMipLevel
, levelCount );

for ( auto & face : view.faces )
{
update( face
, image
, baseArrayLayer
, baseMipLevel
, levelCount );
}
}

template< typename ViewT >
static void update( ArrayView< ViewT > & view
, VkImage image
, uint32_t levelCount )
{
uint32_t baseArrayLayer = 0u;

for ( auto & layer : view.layers )
{
update( layer
, image
, baseArrayLayer
, 0u
, levelCount );
}
}

template< typename ViewT >
static void update( SliceView< ViewT > & view
, VkImage image
, uint32_t levelCount )
{
uint32_t baseSlice = 0u;

for ( auto & layer : view.slices )
{
update( layer
, image
, baseSlice
, 0u
, levelCount );
}
}

static void update( MipView & view
, VkExtent3D const & extent
, VkFormat format
Expand Down Expand Up @@ -612,16 +518,6 @@ namespace castor3d
}
}

static auto updateMipLevels( bool genNeeded
, uint32_t mipLevels
, MipView & mipView )
{
updateMipLevels( genNeeded
, mipLevels
, mipView.view->getLevelCount()
, mipView );
}

static castor::ImageLayout::Type convert( VkImageCreateFlags flags
, VkImageType imageType
, VkExtent3D const & extent
Expand Down
2 changes: 0 additions & 2 deletions source/Core/Castor3D/Scene/Background/Background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,6 @@ namespace castor3d
{
auto result = buffer.clone();
auto dstFormat = result->getFormat();
bool changed{};

switch ( dstFormat )
{
Expand Down Expand Up @@ -641,7 +640,6 @@ namespace castor3d
, result->getConstPtr()
, result->getFormat()
, result->getAlign() );
changed = true;
}

if ( generateMips
Expand Down
4 changes: 1 addition & 3 deletions source/Core/Castor3D/Shader/ShaderAppendBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ namespace castor3d
ShaderAppendBuffer::ShaderAppendBuffer( Engine & engine
, RenderDevice const & device
, VkDeviceSize size
, castor::String name
, crg::AccessState wantedState )
, castor::String name )
: m_device{ device }
, m_size{ ashes::getAlignedSize( size + sizeof( uint32_t )
, m_device.renderSystem.getValue( GpuMin::eBufferMapSize ) ) }
, m_wantedState{ std::move( wantedState ) }
, m_buffer{ makeBufferBase( m_device
, m_size
, ( VK_BUFFER_USAGE_STORAGE_BUFFER_BIT )
Expand Down
85 changes: 53 additions & 32 deletions source/Core/Castor3D/Shader/Shaders/GlslLighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,11 @@ namespace castor3d::shader
, lightSurface
, radiance
, output );
doComputeScatteringTerm( radiance
, light.base().intensity()
, components
, lightSurface
, output.scattering );
auto shadows = m_writer.declLocale( "shadows"
, m_shadowModel.getDirectionalShadows() );

if ( m_shadowModel.isEnabled() )
{
auto shadows = m_writer.declLocale( "shadows"
, m_shadowModel.getDirectionalShadows() );
doApplyShadows( shadows
, light.shadowMapIndex()
, light.base().intensity()
Expand All @@ -249,6 +244,13 @@ namespace castor3d::shader
, output );
}

doComputeScatteringTerm( shadows.base()
, light.shadowMapIndex()
, radiance
, light.base().intensity()
, components
, lightSurface
, output.scattering );
parentOutput.diffuse += max( vec3( 0.0_f ), output.diffuse );
parentOutput.specular += max( vec3( 0.0_f ), output.specular );
parentOutput.scattering += max( vec3( 0.0_f ), output.scattering );
Expand Down Expand Up @@ -711,26 +713,6 @@ namespace castor3d::shader
}
FI;
#endif

if ( m_enableVolumetric
&& m_directionalTransform
&& m_directionalCascadeIndex
&& m_directionalCascadeCount )
{
IF( m_writer, shadows.base().volumetricSteps() != 0_u
&& shadowMapIndex >= 0_i )
{
auto volumetric = m_writer.declLocale( "volumetric"
, m_shadowModel.computeVolumetric( shadows.base()
, lightSurface
, *m_directionalTransform
, *m_directionalCascadeIndex
, *m_directionalCascadeCount ) );
output.scattering *= volumetric
* lightIntensity.x();
}
FI;
}
}
FI;
}
Expand Down Expand Up @@ -885,6 +867,41 @@ namespace castor3d::shader
FI;
}

void LightingModel::doApplyVolumetric( ShadowData const & shadows
, sdw::Int const shadowMapIndex
, sdw::Vec2 const & lightIntensity
, LightSurface const & lightSurface
, sdw::Vec3 & output
, bool multiply )
{
if ( m_enableVolumetric
&& m_directionalTransform
&& m_directionalCascadeIndex
&& m_directionalCascadeCount )
{
IF( m_writer, shadows.volumetricSteps() != 0_u
&& shadowMapIndex >= 0_i )
{
auto volumetric = m_writer.declLocale( "volumetric"
, m_shadowModel.computeVolumetric( shadows
, lightSurface
, *m_directionalTransform
, *m_directionalCascadeIndex
, *m_directionalCascadeCount ) );

if ( multiply )
{
output *= vec3( volumetric * lightIntensity.x() );
}
else
{
output = vec3( volumetric * lightIntensity.x() );
}
}
FI;
}
}

void LightingModel::doInitialiseBackground( BackgroundModel & pbackground )
{
}
Expand All @@ -904,16 +921,20 @@ namespace castor3d::shader
{
}

void LightingModel::doComputeScatteringTerm( sdw::Vec3 const & radiance
void LightingModel::doComputeScatteringTerm( ShadowData const & shadows
, sdw::Int const shadowMapIndex
, sdw::Vec3 const & radiance
, sdw::Vec2 const & lightIntensity
, BlendComponents const & components
, LightSurface const & lightSurface
, sdw::Vec3 & output )
{
if ( m_enableVolumetric )
{
output = radiance;
}
doApplyVolumetric( shadows
, shadowMapIndex
, lightIntensity
, lightSurface
, output
, false /*multiply*/ );
}

void LightingModel::doInitLightSpecifics( LightSurface const & lightSurface
Expand Down
Loading

0 comments on commit 2295565

Please sign in to comment.