forked from FrictionalGames/AmnesiaTheDarkDescent
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Michael Pollind <[email protected]>
- Loading branch information
Showing
17 changed files
with
449 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include "graphics/ForgeHandles.h" | ||
#include "graphics/ForgeRenderer.h" | ||
#include "graphics/IndexPool.h" | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
namespace hpl { | ||
class Image; | ||
class TextureDescriptorPool; | ||
class ImageBindlessPool { | ||
public: | ||
static uint32_t constexpr MinimumFrameCount = ForgeRenderer::SwapChainLength * 4; | ||
|
||
struct ImageReserveEntry { | ||
uint16_t m_prev = UINT16_MAX; | ||
uint16_t m_next = UINT16_MAX; | ||
uint32_t m_handle = IndexPool::InvalidHandle; | ||
uint32_t m_frameCount = 0; | ||
Image* m_image = nullptr; | ||
}; | ||
ImageBindlessPool(); | ||
ImageBindlessPool(TextureDescriptorPool* pool, uint32_t reserveSize); | ||
~ImageBindlessPool(); | ||
void reset(const ForgeRenderer::Frame& frame); // reset and prepare for the next frame | ||
uint32_t request(Image* image); | ||
|
||
private: | ||
|
||
TextureDescriptorPool* m_texturePool = nullptr; | ||
std::vector<ImageReserveEntry> m_pool; | ||
uint32_t m_allocSize = 0; | ||
|
||
uint16_t m_headIndex = UINT16_MAX; | ||
uint16_t m_tailIndex = UINT16_MAX; | ||
|
||
uint32_t m_currentFrame = 0; | ||
}; | ||
} // namespace hpl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "scene_light_cluster_resource.h.fsl" | ||
#include "math_utils.h.fsl" | ||
|
||
|
||
NUM_THREADS(LIGHT_CLUSTER_WIDTH, LIGHT_CLUSTER_HEIGHT, 1) | ||
void CS_MAIN(SV_GroupThreadID(uint3) threadInGroupId, SV_GroupID(uint3) groupId) | ||
{ | ||
INIT_MAIN; | ||
const float invClusterWidth = 1.0f / float(LIGHT_CLUSTER_WIDTH); | ||
const float invClusterHeight = 1.0f / float(LIGHT_CLUSTER_HEIGHT); | ||
ViewportInfo viewInfo = Get(viewports)[PRIMARY_VIEWPORT_INDEX]; | ||
float2 size = viewInfo.rect.zw; | ||
float4x4 viewProjMat = mul(viewInfo.projMat, viewInfo.viewMat); | ||
|
||
// aspect ratio is hard coded to 4/3 | ||
const float aspectRatio = 4.0/3.0; | ||
SpotLight lightData = Get(spotLights)[groupId.x]; | ||
|
||
float4 sphere = boundingSphereForSpotlight(lightData.lightPos, lightData.direction, lightData.radius, lightData.angle); | ||
|
||
float4 lightPosWorldSpace = float4(sphere.xyz, 1.0f); | ||
float4 lightPosClipSpace = mul(viewProjMat, lightPosWorldSpace); | ||
float invLightPosW = 1.0f / lightPosClipSpace.w; | ||
float3 lightPos = lightPosClipSpace.xyz * invLightPosW; | ||
|
||
float fov = 2.0 * atan((2.0 * viewInfo.zNear)/viewInfo.projMat[1][1]); | ||
float projRadius = 2.0f * sphere.w * (1 / tan(fov * 0.5f)) * invLightPosW; | ||
projRadius *= size.x > size.y ? aspectRatio : 1 / aspectRatio; | ||
|
||
// Early exit light if it's behind the camera | ||
if (lightPosClipSpace.w < 0.0f && -lightPosClipSpace.w > sphere.w) { | ||
RETURN(); | ||
} | ||
|
||
lightPos.x *= aspectRatio; | ||
|
||
// Cluster coordinates in post perspective clip space | ||
float clusterLeft = float(threadInGroupId.x) * invClusterWidth; | ||
float clusterTop = float(threadInGroupId.y) * invClusterHeight; | ||
float clusterRight = clusterLeft + invClusterWidth; | ||
float clusterBottom = clusterTop + invClusterHeight; | ||
|
||
// Transform coordinates from range [0..1] to range [-1..1] | ||
clusterLeft = clusterLeft * 2.0f - 1.0f; | ||
clusterTop = clusterTop * 2.0f - 1.0f; | ||
clusterRight = clusterRight * 2.0f - 1.0f; | ||
clusterBottom = clusterBottom * 2.0f - 1.0f; | ||
|
||
clusterLeft *= aspectRatio; | ||
clusterRight *= aspectRatio; | ||
|
||
float clusterCenterX = (clusterLeft + clusterRight) * 0.5f; | ||
float clusterCenterY = (clusterTop + clusterBottom) * 0.5f; | ||
float clusterRadius = distance(float2(clusterLeft, clusterTop), float2(clusterRight, clusterBottom)) * 0.5f; | ||
|
||
// Check if the light projection overlaps the cluster: add the light bit to this cluster coords | ||
float distanceToCenter = distance(float2(clusterCenterX, clusterCenterY), lightPos.xy); | ||
|
||
if (distanceToCenter - clusterRadius < abs(projRadius)) | ||
{ | ||
// Increase light count on this cluster | ||
uint lightArrayPos = 0; | ||
AtomicAdd(Get(lightClustersCount)[LIGHT_CLUSTER_COUNT_POS(threadInGroupId.x, threadInGroupId.y)], 1, lightArrayPos); | ||
|
||
// Add light id to cluster | ||
AtomicExchange(Get(lightClusters)[LIGHT_CLUSTER_DATA_POS(lightArrayPos, threadInGroupId.x, threadInGroupId.y)], LIGHT_ENCODE(LIGHT_TYPE_SPOT_LIGHT, groupId.x), lightArrayPos); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.