using System; using Xui.Core.Canvas; using Xui.Core.Canvas.Composition.GPU; using Xui.Core.Canvas.Composition.Graphics; using Xui.Core.UI; using Xui.GPU.Backends; using Xui.GPU.Hardware; using Xui.GPU.Shaders; using Xui.GPU.Shaders.Attributes; using Xui.GPU.Shaders.Types; namespace Xui.Apps.DemoApp.Docs.Gpu; [ShaderProgram] public partial struct TriangleShader { public struct Vertex { public Float3 Position; public Color4 Color; } public struct Varyings { [BuiltIn(BuiltIn.Position)] public Float4 Position; [Location(0)] public Color4 Color; } public struct Bindings { public Float4x4 Transform; } [VertexShader] public struct VertexStage : IVertexShader { public Varyings Execute(Vertex input, in Bindings bindings) => new() { Position = bindings.Transform * new Float4(input.Position, F32.One), Color = input.Color }; } [FragmentShader] public struct FragmentStage : IFragmentShader { public FragmentOutput Execute(Varyings input, in Bindings bindings) => new() { Color = input.Color }; } } public sealed class Triangle : View, IGpuSurfaceContent { private readonly TriangleShader.Vertex[] vertices = [new() { Position = new(-0.75f, -0.65f, 0), Color = new(0.52f, 0.22f, 0.81f, 1) }, new() { Position = new(0, 0.75f, 0), Color = new(0.85f, 0.34f, 0.66f, 1) }, new() { Position = new(0.75f, -0.65f, 0), Color = new(0.35f, 0.65f, 1, 1) } ]; private IGpuVertexShader? vertexShader; private IGpuFragmentShader? fragmentShader; private IGpuCommandList? commands; private GpuPipelineDesc? pipeline; private bool dirty = true; protected override void RenderCore(IContext context) { if (context.GpuSurface(new GroupOptions { Key = this, Bounds = Frame, HasUpdates = dirty }, out _)) return; // SVG and unsupported runtimes show a real fallback, not a simulated GPU image. context.SetFill(new Color(0xFAF7FFFF)); context.FillRect(Frame); context.SetFill(new Color(0x592C83FF)); context.SetFont(new Font(17, "Inter")); context.TextAlign = TextAlign.Center; context.TextBaseline = TextBaseline.Middle; context.FillText("GPU surface unavailable in this renderer", Frame.Center); } void IGpuSurfaceContent.Render(IGpuRenderTarget target) { if (pipeline is null) { var device = GetService(typeof(IGpuDevice)) as IGpuDevice ?? throw new NotSupportedException("This window has no GPU device."); var backend = GetService(typeof(IShaderBackend)) as IShaderBackend ?? throw new NotSupportedException("This window has no shader backend."); var source = TriangleShader.GenerateShaderSource(backend); vertexShader = device.CompileVertexShader(source, TriangleShader.GetVertexEntry(backend.Name)); fragmentShader = device.CompileFragmentShader(source, TriangleShader.GetFragmentEntry(backend.Name)); commands = device.CreateCommandList(); pipeline = new() { VertexShader = vertexShader, FragmentShader = fragmentShader, DepthTestEnabled = false, DepthWriteEnabled = false, CullMode = GpuCullMode.None }; } var transform = Float4x4.Identity; commands!.BeginRenderPass(target, new Color4(0.98f, 0.97f, 1, 1)); commands.SetPipeline(pipeline); commands.SetVertexBuffer(vertices); commands.SetConstantBuffer(in transform); commands.Draw(vertices.Length); commands.EndRenderPass(); commands.Commit(); dirty = false; } protected override void OnDeactivate() { commands?.Dispose(); commands = null; vertexShader?.Dispose(); vertexShader = null; fragmentShader?.Dispose(); fragmentShader = null; pipeline = null; dirty = true; base.OnDeactivate(); } }