mirror of
https://bitbucket.org/CPMADevs/cnq3
synced 2025-02-22 03:41:21 +00:00
86 lines
3.2 KiB
HLSL
86 lines
3.2 KiB
HLSL
/*
|
|
===========================================================================
|
|
Copyright (C) 2024 Gian 'myT' Schellenbaum
|
|
|
|
This file is part of Challenge Quake 3 (CNQ3).
|
|
|
|
Challenge Quake 3 is free software; you can redistribute it
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
published by the Free Software Foundation; either version 2 of the License,
|
|
or (at your option) any later version.
|
|
|
|
Challenge Quake 3 is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with Challenge Quake 3. If not, see <https://www.gnu.org/licenses/>.
|
|
===========================================================================
|
|
*/
|
|
// volumetric lighting: accumulates in-scattered ambient light
|
|
|
|
|
|
#include "common.hlsli"
|
|
#include "scene_view.h.hlsli"
|
|
|
|
|
|
cbuffer RootConstants
|
|
{
|
|
float3 centerPosition;
|
|
uint materialTextureAIndex;
|
|
float3 worldScale;
|
|
uint scatterExtTextureIndex;
|
|
uint ambientLightTextureAIndex;
|
|
uint ambientLightTextureBIndex;
|
|
uint ambientSamplerIndex;
|
|
uint isLightGridAvailable;
|
|
}
|
|
|
|
[numthreads(4, 4, 4)]
|
|
void cs(uint3 id : SV_DispatchThreadID)
|
|
{
|
|
RWTexture3D<float4> scatterExtTexture = ResourceDescriptorHeap[scatterExtTextureIndex];
|
|
uint3 textureSize = GetTextureSize(scatterExtTexture);
|
|
if(any(id >= textureSize))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if(isLightGridAvailable != 0)
|
|
{
|
|
SceneView scene = GetSceneView();
|
|
RWTexture3D<float4> materialTextureA = ResourceDescriptorHeap[materialTextureAIndex];
|
|
Texture3D ambientLightTextureA = ResourceDescriptorHeap[ambientLightTextureAIndex];
|
|
Texture3D ambientLightTextureB = ResourceDescriptorHeap[ambientLightTextureBIndex];
|
|
SamplerState ambientSampler = SamplerDescriptorHeap[ambientSamplerIndex];
|
|
float3 ambientTextureSize = float3(GetTextureSize(ambientLightTextureA));
|
|
|
|
float3 positionWS = scene.FroxelIndexToWorldSpace(id, textureSize);
|
|
float3 normalWS = normalize(scene.cameraPosition - positionWS);
|
|
float4 scatterAbs = materialTextureA[id];
|
|
float3 scattering = scatterAbs.rgb;
|
|
float extinction = Brightness(scattering) + scatterAbs.a;
|
|
float3 ambientTC = AABoxWorldSpaceToTC(positionWS, centerPosition, ambientTextureSize, worldScale);
|
|
float4 ambientA = ambientLightTextureA.SampleLevel(ambientSampler, ambientTC, 0);
|
|
float4 ambientB = ambientLightTextureB.SampleLevel(ambientSampler, ambientTC, 0);
|
|
float3 ambientColor = AmbientColor(ambientA, ambientB, normalWS, scene.ambientColor);
|
|
float3 inScattering = scattering * ambientColor * scene.ambientIntensity;
|
|
|
|
scatterExtTexture[id] = float4(inScattering, extinction);
|
|
}
|
|
else
|
|
{
|
|
SceneView scene = GetSceneView();
|
|
RWTexture3D<float4> materialTextureA = ResourceDescriptorHeap[materialTextureAIndex];
|
|
|
|
float3 positionWS = scene.FroxelIndexToWorldSpace(id, textureSize);
|
|
float3 normalWS = normalize(scene.cameraPosition - positionWS);
|
|
float4 scatterAbs = materialTextureA[id];
|
|
float3 scattering = scatterAbs.rgb;
|
|
float extinction = Brightness(scattering) + scatterAbs.a;
|
|
float3 inScattering = scattering * scene.ambientColor * scene.ambientIntensity;
|
|
|
|
scatterExtTexture[id] = float4(inScattering, extinction);
|
|
}
|
|
}
|