summaryrefslogtreecommitdiffstats
path: root/examples/reflection-api/raster-simple.slang
diff options
context:
space:
mode:
authorTheresa Foley <10618364+tangent-vector@users.noreply.github.com>2024-12-12 13:30:43 -0800
committerGitHub <noreply@github.com>2024-12-12 13:30:43 -0800
commit1fb79ac4f922c3f9c3ecf8f4533c12ec7bdeb37d (patch)
treef44c62a7e88dd7b44fa5b36a02345305a169e6f5 /examples/reflection-api/raster-simple.slang
parent9c82ed36946941eb1df3186f00903593aab556c3 (diff)
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>
Diffstat (limited to 'examples/reflection-api/raster-simple.slang')
-rw-r--r--examples/reflection-api/raster-simple.slang88
1 files changed, 88 insertions, 0 deletions
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<float3> albedoMap;
+ Texture2D<float3> normalMap;
+ Texture2D<float> 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> material;
+uniform ConstantBuffer<Camera> camera;
+uniform ParameterBlock<Environment> 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);
+}