diff --git a/doc/Sample-Showcase-Plan.md b/doc/Sample-Showcase-Plan.md new file mode 100644 index 0000000..22f680a --- /dev/null +++ b/doc/Sample-Showcase-Plan.md @@ -0,0 +1,44 @@ +# Visual samples and optional UI hosts + +The sample lineup must demonstrate observable SDK behavior as coverage grows. Console readback fixtures remain correctness tests; they do not replace interactive graphics, media or streaming examples. This plan adds no completed-coverage claims for APIs that are still on the backlog. + +## Immediate visual polish + +- PlanarShadows: lower the directional light, lengthen the silhouettes, enlarge the receiving plane and adjust the camera. Increase receiver/shadow contrast and light rotating pyramid normals in world space with the same direction used for projection. Keep the simple opaque planar projection explicit; this is not a shadow-map or soft-shadow implementation. +- LitCube: move lighting into the pixel shader, normalize interpolated normals, add a warm key with specular response, a restrained cool fill and hemispherical ambient light. Convert linear output to sRGB explicitly for the existing UNORM target. This is an analytic lighting example, not a PBR material renderer. +- Keep the existing bounded WARP runs and debug-layer checks for both samples. Visual acceptance additionally needs an interactive hardware review; a successful smoke run alone does not establish appearance or frame pacing. + +## Optional host adapters + +Keep Core and Framework independent of WPF and Windows App SDK dependencies. Each reusable control belongs in an optional assembly in this repository and the normal Libraries solution group. Sample applications are separate executable projects. No copy of the graphics bindings or second rendering backend is needed. + +| Host | Proposed assembly / existing location | Presentation contract | +| --- | --- | --- | +| Native Win32 | Existing DXSharp.Framework | NativeGame and HWND FrameRenderer; no Windows Forms dependency | +| Windows Forms | Existing DXSharp.Framework.Desktop | Existing DesktopGame adapter | +| WPF | Proposed DXSharp.Framework.Wpf | HwndHost child HWND using the existing frame renderer; WPF dispatcher owns scheduling and teardown | +| WinUI 3 | Proposed DXSharp.Framework.WinUI | Composition swap chain attached through Microsoft.UI.Xaml ISwapChainPanelNative; Windows App SDK dependency stays in this adapter | + +WPF's child HWND has airspace restrictions: WPF elements cannot freely overlap or transform it as ordinary composed content. D3DImage is a different path requiring a Direct3D9 surface bridge and synchronization; it is not a direct D3D12 swap-chain target. Start with an honest HwndHost sample and assess a composed surface bridge separately if editor overlays require it. + +WinUI needs a composition presentation surface in Framework before the adapter can reuse frame-slot retirement. For D3D12, CreateSwapChainForComposition receives the direct command queue. Use the WinUI 3 interop interface from microsoft.ui.xaml.media.dxinterop.h, not the similarly named UWP interface. Attach/detach on the UI thread and handle composition scale, pixel size, unloading and outstanding GPU work explicitly. + +Acceptance: share the CharacterLab scene/rendering code; exercise repeated resizing, DPI/scale changes, minimize/restore or unload/reload, input focus, close during GPU work and device-loss reporting. Confirm consumers only acquire their chosen UI framework. Pin and qualify the Windows App SDK version when implementing WinUI; the native and WPF hosts must not require its runtime. A WinUI sample can be unpackaged; an MSIX packaging project is a deployment choice, not a requirement imposed by DirectX hosting. + +## Coverage-driven showcase work + +| Work item | Interactive demonstration | Required evidence | +| --- | --- | --- | +| Video and audio | Local licensed test clip decoded into a displayed texture with synchronized audio, play/pause/seek, volume/mute, format and hardware-decoding telemetry | Known color/time patterns, seek/end-of-stream, A/V clock drift, resize and teardown. Identify Media Foundation versus D3D video decode/processing paths explicitly; playing through a control is not proof of D3D12 video binding coverage. | +| Spatial audio | XAudio2 voices with X3DAudio emitter/listener motion, distance attenuation and channel routing | Deterministic DSP coefficient checks plus audible hardware verification; expose channel layout and device changes. X3DAudio computes spatial DSP parameters and does not itself play audio. | +| DirectStorage | Stream a prepared asset pack into a live scene; show queue state, read/decompression timings, bytes and active path | Compare decoded content hashes against the source, exercise missing/corrupt input and shutdown with pending requests. Distinguish file I/O, decompression and GPU visibility; report actual supported paths rather than silently presenting fallback as GPU decompression. Keep ordinary loading as a comparison. | +| DXR visual scene | Windowed camera-controlled scene with meshes and ground, visible ray-traced shadows and reflections, and a moving instance | Reuse the acceleration-structure/shader-table groundwork from DxrHelloWorld; add output presentation, TLAS updates, resize, fenced resource lifetime and deterministic image checks. Query DXR support and explain an unsupported result clearly; do not relabel rasterization as ray tracing. | + +Order: optional WPF HWND and WinUI composition host slice; video/audio and spatial audio alongside their native coverage; DirectStorage alongside its native coverage; then a focused DXR visual showcase. Retain the general texture/DDS, pipeline configuration and cross-queue synchronization backlog as supporting SDK work. Full applications must consume public SDK APIs and join the same source/package qualification mechanism as existing samples. + +## Platform references + +- [WPF and Win32 interoperation](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/wpf-and-win32-interoperation) +- [WPF and Direct3D9 interoperation](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/wpf-and-direct3d9-interoperation) +- [WinUI 3 ISwapChainPanelNative::SetSwapChain](https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/win32/microsoft.ui.xaml.media.dxinterop/nf-microsoft-ui-xaml-media-dxinterop-iswapchainpanelnative-setswapchain) +- [CreateSwapChainForComposition](https://learn.microsoft.com/en-us/windows/win32/api/dxgi1_2/nf-dxgi1_2-idxgifactory2-createswapchainforcomposition) diff --git a/samples/LitCube/Program.cs b/samples/LitCube/Program.cs index 70d4948..d51a665 100644 --- a/samples/LitCube/Program.cs +++ b/samples/LitCube/Program.cs @@ -12,17 +12,39 @@ internal static class Program { row_major float4x4 WorldViewProjection; row_major float4x4 World; }; - struct Vertex { float4 position : SV_Position; float3 color : COLOR; }; + struct Vertex { + float4 position : SV_Position; + float3 worldPosition : WORLDPOS; + float3 normal : NORMAL; + float3 color : COLOR; + }; struct VertexInput { float3 position : POSITION; float3 normal : NORMAL; float3 color : COLOR; }; Vertex VS(VertexInput input) { - float3 normal = mul(input.normal, (float3x3)World); - float light = 0.28 + 0.72 * saturate(dot(normal, normalize(float3(0.45, 0.8, 0.65)))); Vertex output; output.position = mul(float4(input.position, 1), WorldViewProjection); - output.color = input.color * light; + output.worldPosition = mul(float4(input.position, 1), World).xyz; + output.normal = mul(input.normal, (float3x3)World); + output.color = input.color; return output; } - float4 PS(Vertex input) : SV_Target { return float4(input.color, 1); } + float4 PS(Vertex input) : SV_Target { + float3 n = normalize(input.normal); + float3 v = normalize(float3(0, 1.4, 5.8) - input.worldPosition); + float3 l = normalize(float3(-3.5, 4.0, 4.0) - input.worldPosition); + float ndotl = saturate(dot(n, l)); + float3 h = normalize(l + v); + float specular = pow(saturate(dot(n, h)), 48) * ndotl; + float fill = saturate(dot(n, normalize(float3(1, 0.25, -0.6)))); + float hemisphere = n.y * 0.5 + 0.5; + float3 ambient = lerp(float3(0.025, 0.03, 0.05), float3(0.12, 0.16, 0.22), hemisphere); + float3 linearColor = input.color * (ambient + float3(1.05, 0.9, 0.7) * ndotl + + float3(0.1, 0.2, 0.34) * fill) + float3(1.0, 0.92, 0.8) * specular * 0.65; + // The target is UNORM, so perform the linear-to-sRGB transfer here. + linearColor = saturate(linearColor); + float3 srgb = lerp(12.92 * linearColor, 1.055 * pow(linearColor, 1.0 / 2.4) - 0.055, + step(0.0031308, linearColor)); + return float4(srgb, 1); + } """; [StructLayout(LayoutKind.Sequential)] diff --git a/samples/PlanarShadows/Program.cs b/samples/PlanarShadows/Program.cs index bb28ac4..87f6f3f 100644 --- a/samples/PlanarShadows/Program.cs +++ b/samples/PlanarShadows/Program.cs @@ -7,10 +7,12 @@ namespace PlanarShadows; internal static class Program { - private static readonly Vector3 Light = Vector3.Normalize(new Vector3(-0.6f, 1, 0.45f)); + // A low key light exposes the projected silhouette outside each pyramid's base. + private static readonly Vector3 Light = Vector3.Normalize(new Vector3(-1, 0.65f, 0.55f)); private const string Shader = """ cbuffer Scene : register(b0) { row_major float4x4 transform; + row_major float4x4 world; float4 tint; }; struct Vertex { float4 position : SV_Position; float3 local : LOCAL; float light : LIGHT; }; @@ -20,7 +22,7 @@ Vertex FloorVS(uint id : SV_VertexID) { float2(-1,-1), float2(1,1), float2(1,-1) }; Vertex v; - v.local = float3(corners[id].x, 0, corners[id].y) * 5; + v.local = float3(corners[id].x, 0, corners[id].y) * 6; v.position = mul(float4(v.local, 1), transform); v.light = 1; return v; @@ -31,11 +33,16 @@ Vertex PyramidVS(uint id : SV_VertexID) { float3(-1,0,1), float3(0,1.65,0) }; const uint indices[12] = { 0,1,4, 1,2,4, 2,3,4, 3,0,4 }; - const float light[4] = { 0.6, 0.8, 1.0, 0.72 }; + uint face = (id / 3) * 3; + float3 a = corners[indices[face]]; + float3 b = corners[indices[face + 1]]; + float3 c = corners[indices[face + 2]]; + float3 normal = normalize(cross(c - a, b - a)); Vertex v; v.local = corners[indices[id]]; v.position = mul(float4(v.local, 1), transform); - v.light = lerp(1, light[id / 3], tint.a); + normal = normalize(mul(normal, (float3x3)world)); + v.light = lerp(1, 0.3 + 0.7 * saturate(dot(normal, normalize(float3(-1, 0.65, 0.55)))), tint.a); return v; } float4 FloorPS(Vertex v) : SV_Target { @@ -52,7 +59,7 @@ private static readonly (Vector3 Position, Vector4 Color, float Scale)[] Objects ]; [StructLayout(LayoutKind.Sequential)] - private readonly record struct DrawConstants(Matrix4x4 Transform, Vector4 Tint); + private readonly record struct DrawConstants(Matrix4x4 Transform, Matrix4x4 World, Vector4 Tint); [STAThread] public static int Main(string[] args) { @@ -71,7 +78,7 @@ public static int Main(string[] args) { }); Console.WriteLine(device.AdapterName); using var compiler = new ShaderCompiler(); - using var root = device.CreateGraphicsConstantsRootSignature(20); + using var root = device.CreateGraphicsConstantsRootSignature(36); using var floor = CreatePipeline("FloorVS", "FloorPS"); using var pyramid = CreatePipeline("PyramidVS", "PyramidPS"); @@ -87,7 +94,7 @@ public static int Main(string[] args) { }, frame => { time += Math.Min((float)frame.Elapsed.TotalSeconds, 0.1f); float orbit = 0.38f + time * 0.28f; - var eye = new Vector3(8.5f * MathF.Sin(orbit), 5.8f, 8.5f * MathF.Cos(orbit)); + var eye = new Vector3(10.2f * MathF.Sin(orbit), 7.2f, 10.2f * MathF.Cos(orbit)); var view = Matrix4x4.CreateLookAt(eye, Vector3.Zero, Vector3.UnitY); var projection = Matrix4x4.CreatePerspectiveFieldOfView( MathF.PI / 3, (float)frame.Size.Width / frame.Size.Height, 0.1f, 50); @@ -100,11 +107,11 @@ public static int Main(string[] args) { } frame.Commands.SetGraphicsPipeline(floor); - Draw(frame.Commands, viewProjection, new(0.25f, 0.29f, 0.36f, 1), 6); + Draw(frame.Commands, viewProjection, Matrix4x4.Identity, new(0.36f, 0.42f, 0.5f, 1), 6); frame.Commands.SetGraphicsPipeline(pyramid); - foreach (var world in worlds) Draw(frame.Commands, world * shadow * viewProjection, new(0.09f, 0.11f, 0.16f, 0), 12); - for (int i = 0; i < worlds.Length; i++) Draw(frame.Commands, worlds[i] * viewProjection, Objects[i].Color, 12); + foreach (var world in worlds) Draw(frame.Commands, world * shadow * viewProjection, world, new(0.045f, 0.06f, 0.085f, 0), 12); + for (int i = 0; i < worlds.Length; i++) Draw(frame.Commands, worlds[i] * viewProjection, worlds[i], Objects[i].Color, 12); }); var diagnostics = device.GetDiagnostics(); foreach (string message in diagnostics.Messages) Console.WriteLine(message); @@ -118,8 +125,8 @@ PipelineState CreatePipeline(string vs, string ps) => compiler.Compile(Shader, ps, "ps_6_0"), PixelFormat.Rgba8Unorm, depthEnabled: true); } - private static void Draw(GraphicsCommandList commands, Matrix4x4 transform, Vector4 tint, uint vertices) { - var constants = new DrawConstants(transform, tint); + private static void Draw(GraphicsCommandList commands, Matrix4x4 transform, Matrix4x4 world, Vector4 tint, uint vertices) { + var constants = new DrawConstants(transform, world, tint); commands.SetGraphicsConstants(MemoryMarshal.Cast(MemoryMarshal.CreateReadOnlySpan(ref constants, 1))); commands.Draw(vertices); } diff --git a/samples/README.md b/samples/README.md index 93cd41a..523604f 100644 --- a/samples/README.md +++ b/samples/README.md @@ -9,10 +9,10 @@ Open `DXSharp.sln` or run a project with `dotnet run --project samples/ -c | [ModernTriangle](ModernTriangle) | Minimal graphics pipeline and resize qualification | | [HelloTexture](HelloTexture) | Texture upload, SRV and sampler | | [InstancedGrid](InstancedGrid) | 256 animated quads in one instanced draw | -| [LitCube](LitCube) | Indexed mesh upload, vertex input layouts, lighting and D32 depth | +| [LitCube](LitCube) | Indexed mesh upload, per-pixel diffuse/specular lighting, hemispherical ambient and D32 depth | | [StaticModelViewer](StaticModelViewer) | glTF and GLB import, shared geometry, node transforms, and directional lighting | | [FleetBattler](FleetBattler) | Fleet skirmishes, box selection, planetary development, and compute-driven civilian traffic | -| [PlanarShadows](PlanarShadows) | Orbiting camera, multiple pipelines and projected shadows | +| [PlanarShadows](PlanarShadows) | Low directional light, world-space face lighting and extended planar shadows | | [ModernCompute](ModernCompute) | DXC compute readback, DirectML Add, and matrix inference with fused ReLU | | [NBodyGravity](NBodyGravity) | Ping-pong GPU simulation, instanced drawing, and a seeded CPU reference | | [DxrHelloWorld](DxrHelloWorld) | DXR acceleration structures, shader table, and image readback | @@ -22,3 +22,5 @@ Every runnable sample project is included in DXSharp.sln and consumes the curren CharacterLab adds persistent GPU skinning, animation playback and material maps; see [CharacterLab](CharacterLab/README.md). `CharacterLab.Native` runs the same GPU-skinned scene as CharacterLab through the native Win32 host, with precompiled Framework shaders and no Windows Forms reference. Both samples participate in source and package qualification. + +See the [visual showcase and optional UI host plan](../doc/Sample-Showcase-Plan.md) for WPF/WinUI adapters and the upcoming media, spatial audio, DirectStorage and interactive DXR samples. These planned samples are not yet implemented.