From 1fb79ac4f922c3f9c3ecf8f4533c12ec7bdeb37d Mon Sep 17 00:00:00 2001 From: Theresa Foley <10618364+tangent-vector@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:30:43 -0800 Subject: Add an example for using the reflection API (#5839) * Add an example for using the reflection API The example program is meant to accompany a document that goes into more detail about the mental model behind the reflection API and the way this program drives it. Ideally this program can land before the document goes live, and then the document can be published with a link to the example. After that, the example could be updated to include links into the live document. Along with adding the example program, this change also adds some convenience functions to the reflection API to avoid cases where the program would otherwise need to cast between `slang::ParameterCategory` and `SlangParameterCategory`. * format code * fixup: error noticed by clang --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- examples/reflection-api/raster-simple.slang | 88 +++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 examples/reflection-api/raster-simple.slang (limited to 'examples/reflection-api/raster-simple.slang') diff --git a/examples/reflection-api/raster-simple.slang b/examples/reflection-api/raster-simple.slang new file mode 100644 index 000000000..a44410e64 --- /dev/null +++ b/examples/reflection-api/raster-simple.slang @@ -0,0 +1,88 @@ +// raster-simple.slang + +struct AssembledVertex +{ + float3 position : POSITION; + float3 normal : NORMAL; + float2 uv : TEXCOORD; +}; + +struct RasterVertex +{ + float3 worldPosition; + float3 worldNormal; + float2 uv; +}; + +struct Model +{ + float3x4 modelToWorld; + float3x4 modelToWorld_inverseTranspose; +} + +struct Material +{ + Texture2D albedoMap; + Texture2D normalMap; + Texture2D glossMap; + SamplerState sampler; + float2 uvScale; + float2 uvBias; +} + +struct Camera +{ + float3x4 worldToView; + float3x4 worldToView_inverseTranspose; + + float4x4 viewToProj; +} + +struct DirectionalLight +{ + float3 intensity; + float3 direction; +} + +struct Environment +{ + TextureCube environmentMap; + DirectionalLight light; +} + +uniform Model model; +uniform ParameterBlock material; +uniform ConstantBuffer camera; +uniform ParameterBlock environment; + +[shader("vertex")] +[require(sm_6_0)] +void vertexMain( + in AssembledVertex assembledVertex : A, + out RasterVertex rasterVertex : R, + in uint vertexID : SV_VertexID, + out float4 projPosition : SV_Position) +{ + float3 worldPosition = mul(model.modelToWorld, float4(assembledVertex.position,1)); + + rasterVertex.worldPosition = worldPosition; + rasterVertex.worldNormal = mul(model.modelToWorld_inverseTranspose, float4(assembledVertex.normal,0)); + rasterVertex.uv = assembledVertex.uv; + + float3 viewPosition = mul(camera.worldToView, float4(worldPosition,1)); + projPosition = mul(camera.viewToProj, float4(viewPosition,1)); +} + +[shader("fragment")] +[require(sm_6_0)] +float4 fragmentMain( + in RasterVertex vertex : R) + : SV_Target0 +{ + float3 normal = vertex.worldNormal; + + float3 albedo = material.albedoMap.Sample(material.sampler, vertex.uv); + + float3 color = albedo * max(0, dot(normal, environment.light.direction)); + return float4(color, 1); +} -- cgit v1.2.3