Files
Seele/res/shaders/lib/Common.slang
T

100 lines
2.1 KiB
Plaintext
Raw Normal View History

2026-03-13 22:30:46 +01:00
import Frustum;
2020-06-02 11:46:18 +02:00
const static float PI = 3.1415926535897932f;
2022-02-24 22:38:26 +01:00
const static uint BLOCK_SIZE = 32;
2024-05-06 18:36:16 +02:00
static const uint64_t MAX_TEXCOORDS = 8;
2024-08-13 22:44:04 +02:00
static const uint32_t TASK_GROUP_SIZE = 32;
static const uint32_t MESH_GROUP_SIZE = 32;
2020-06-02 11:46:18 +02:00
2020-10-03 11:00:10 +02:00
struct ViewParameter
{
float4x4 viewMatrix;
float4x4 inverseViewMatrix;
2020-10-03 11:00:10 +02:00
float4x4 projectionMatrix;
2024-02-23 08:31:21 +01:00
float4x4 inverseProjection;
2024-10-21 22:12:50 +02:00
float4x4 viewProjectionMatrix;
float4x4 inverseViewProjectionMatrix;
2024-10-07 20:14:00 +02:00
float4 cameraPosition_WS;
float4 cameraForward_WS;
2022-02-24 22:38:26 +01:00
float2 screenDimensions;
2024-10-07 20:14:00 +02:00
float2 invScreenDimensions;
uint frameIndex;
float time;
2024-09-16 13:00:53 +02:00
};
2024-10-01 16:56:04 +02:00
ParameterBlock<ViewParameter> pViewParams;
2020-10-03 11:00:10 +02:00
float4 worldToModel(float4x4 inverseTransform, float4 world)
{
float4 model = mul(inverseTransform, world);
model = model / model.w;
return model;
}
float4 viewToWorld(float4 view)
{
2024-10-01 16:56:04 +02:00
float4 world = mul(pViewParams.inverseViewMatrix, view);
world = world / world.w;
return world;
}
2024-05-05 08:31:40 +02:00
float4 viewToModel(float4x4 inverseTransform, float4 view)
{
float4 world = viewToWorld(view);
return worldToModel(inverseTransform, world);
}
2024-02-23 08:31:21 +01:00
float4 clipToView(float4 clip)
{
2024-10-01 16:56:04 +02:00
float4 view = mul(pViewParams.inverseProjection, clip);
2024-02-23 08:31:21 +01:00
view = view / view.w;
return view;
}
2024-05-29 10:40:35 +02:00
float4 clipToWorld(float4 clip)
{
float4 view = clipToView(clip);
return viewToWorld(view);
}
2024-02-23 08:31:21 +01:00
float4 screenToView(float4 screen)
2020-06-02 11:46:18 +02:00
{
2024-10-01 16:56:04 +02:00
float2 texCoord = screen.xy / pViewParams.screenDimensions;
2020-06-02 11:46:18 +02:00
2022-11-17 16:47:42 +01:00
// Convert to clip space
2025-04-13 14:41:42 +02:00
float4 clip = float4( texCoord * 2.0f - 1.0f, screen.z, screen.w);
2024-02-23 08:31:21 +01:00
return clipToView(clip);
2020-06-02 11:46:18 +02:00
}
float4 screenToWorld(float4 screen)
{
float4 view = screenToView(screen);
return viewToWorld(view);
}
float4 screenToModel(float4x4 inverseTransform, float4 screen)
{
float4 world = screenToWorld(screen);
return worldToModel(inverseTransform, world);
}
2024-06-11 14:15:29 +02:00
float4 clipToScreen(float4 clip)
{
2024-06-28 17:46:39 +02:00
float4 ndc = clip / clip.w;
float2 texCoords = (float2(ndc.xy) + 1.0f) / 2.0f;
2024-08-31 16:30:57 +02:00
float oz = 1;
float pz = 0 - 1;
float zf = pz * ndc.z + oz;
2025-04-13 14:41:42 +02:00
return float4(texCoords * pViewParams.screenDimensions, zf, 1.0f);
2024-06-11 14:15:29 +02:00
}