Skip to content

Latest commit

 

History

History
61 lines (54 loc) · 1.71 KB

HelloTriangle.md

File metadata and controls

61 lines (54 loc) · 1.71 KB

HelloTriangle

The most basic usage, basic swap chain creation, pipeline creation.
No any resources, including vertex buffers.

1 InitGraphics

// Load an instance of the specified backend
Graphics = GraphicsInstance.LoadD3d12();
// Creating a Graphics Device
Device = Graphics.CreateDevice(Debug: true, Name: "Main Device");
// Creating a swap chain
Output = Device.MainQueue.CreateOutputForHwnd(new() { Width = Width, Height = Height }, Hwnd);

2 LoadResources

// See the source code for details
// This shader module is not a vk module. A module can only have one shader stage with one entrypoint.
// The main purpose is to use native memory to store data to avoid complex memory pinning.
var modules = await LoadShaderModules("HelloTriangle", [ShaderStage.Vertex, ShaderStage.Pixel]);
// To create a shader, you need to provide the shader stages (shader module)
// and the binding layout, input layout (if have vertex shader).
Shader = Device.CreateShader(modules, null, Device.CreateShaderInputLayout([]));
// Then create a pipeline using the graphics state
Pipeline = Device.CreateGraphicsShaderPipeline(
    Shader, new()
    {
        DsvFormat = TextureFormat.Unknown,
        BlendState =
        {
            Rt0 =
            {
                Src = BlendType.SrcAlpha,
                Dst = BlendType.InvSrcAlpha,
                Op = BlendOp.Add,
            }
        }
    }, Name: "HelloTriangle"
);

3 Render

var cmd = Device.MainCommandList;
while (!IsClosed)
{
   Render(cmd);
   Output.Present();
}

void Render(CommandList cmd)
{
    cmd.SetRenderTargets([Output]);
    cmd.ClearColor(Output, new float4(0.83f, 0.8f, 0.97f, 1f));
    cmd.Draw(Pipeline, 3);
}