Skip to content

Commit

Permalink
aalaap
Browse files Browse the repository at this point in the history
  • Loading branch information
john-h-k committed May 23, 2024
1 parent 491fcc5 commit 1bd2004
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 3 deletions.
153 changes: 153 additions & 0 deletions interactive/Voltium.Interactive/Aalaap/AalaapApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Numerics;
using Microsoft.Extensions.DependencyModel.Resolution;
using Microsoft.Extensions.Logging;
using TerraFX.Interop;
using Voltium.Common;
using Voltium.Core;
using Voltium.Core.Configuration.Graphics;
using Voltium.Core.Contexts;
using Voltium.Core.Devices;
using Voltium.Core.Devices.Shaders;
using Voltium.Core.NativeApi;
using Voltium.Core.Pipeline;
using Buffer = Voltium.Core.Memory.Buffer;

namespace Voltium.Interactive.Aalaap
{
// This is our vertex type used in the shader
// [ShaderInput] triggers a source generator to create a shader input description which we need later
[ShaderInput]
internal partial struct AalaapVertex
{
public Vector3 Position;
public Vector3 Center;
public Vector4 Color;
}

public sealed class AalaapApp : Application
{
private GraphicsDevice _device = null!;
private Output _output = null!;
private PipelineStateObject _pso;
private Buffer _vertices;

public unsafe override void Initialize(Size outputSize, IOutputOwner output)
{
var debug = new DebugLayerConfiguration()
.WithDebugFlags(DebugFlags.DebugLayer)
.WithDredFlags(DredFlags.All)
.WithBreakpointLogLevel(LogLevel.None);

_device = GraphicsDevice.Create(new D3D12NativeDevice(D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_1));
_output = Output.Create(
new DXGINativeOutput(
_device.GraphicsQueue.Native,
new NativeOutputDesc
{
Format = BackBufferFormat.B8G8R8A8UnsignedNormalized,
BackBufferCount = 3,
PreserveBackBuffers = false,
VrStereo = false
},
output.GetOutput()
)
);

OnResize(outputSize);

static AalaapVertex[] Triangle(Vector3 center)
{
const float SIZE = 1f;
return new[]
{
new AalaapVertex { Position = center + new Vector3(+SIZE, -SIZE, +0.0f), Center = center, Color = (Vector4)Rgba128.Blue },
new AalaapVertex { Position = center + new Vector3(-SIZE, -SIZE, +0.0f), Center = center, Color = (Vector4)Rgba128.Green },
new AalaapVertex { Position = center + new Vector3(+0.0f, +SIZE, +0.0f), Center = center, Color = (Vector4)Rgba128.Red },
};
}

List<AalaapVertex> vertices = new();
vertices.AddRange(Triangle(default));
vertices.AddRange(Triangle(new Vector3(0.5f)));
vertices.AddRange(Triangle(new Vector3(0.5f, 0.1f, 0.0f)));
vertices.AddRange(Triangle(new Vector3(0.0f, -0.3f, 0.0f)));

// Allocate the vertices, using the overload which takes some initial data
_vertices = _device.Allocator.AllocateUploadBuffer(vertices.ToArray());

// The pipeline description. We compile shaders at runtime here, which is simpler but less efficient
var psoDesc = new GraphicsPipelineDesc
{
Topology = Topology.TriangleList,
VertexShader = ShaderManager.CompileShader("Aalaap/Shader.hlsl", ShaderType.Vertex, entrypoint: "VertexMain"),
PixelShader = ShaderManager.CompileShader("Aalaap/Shader.hlsl", ShaderType.Pixel, entrypoint: "PixelMain"),
RenderTargetFormats = _output.Format,
DepthStencil = DepthStencilDesc.DisableDepthStencil,
Inputs = InputLayout.FromType<AalaapVertex>(),
Blend = new BlendDesc
{
RenderTargets =
{
[0] = RenderTargetBlendDesc.CreateBlend(BlendFunc.Add, BlendFactor.SourceColor, BlendFactor.One, BlendFunc.Max, BlendFactor.SourceAlpha, BlendFactor.One)
}
},
Msaa = MsaaDesc.None,
};

_pso = _device.CreatePipelineStateObject(psoDesc);
}


public override void OnResize(Size newOutputSize) => _output.Resize(newOutputSize);
public override void Update(ApplicationTimer timer) { /* This app doesn't do any updating */ }

private GraphicsContext _context = new();
public unsafe override void Render()
{
var context = _context;

context.Reset();

context.SetPipelineState(_pso);

// We need to transition the back buffer to ResourceState.RenderTarget so we can draw to it
using (context.ScopedBarrier(ResourceTransition.Create(_output.OutputBuffer, ResourceState.Present, ResourceState.RenderTarget)))
using (context.ScopedRenderPass(new RenderTarget
{
Resource = _output.OutputBufferView,
Load = LoadOperation.Clear,
Store = StoreOperation.Preserve,
ColorClear = Rgba128.Black
}))
{
// Set that we render to the entire screen, clear the render target, set the vertex buffer, and set the topology we will use
context.SetVertexBuffers<AalaapVertex>(_vertices);

for (var i = 0; i < 12; i += 3)
{
context.Draw(3, i);
}
}

context.Close();

// Execute the context and wait for it to finish
_device.GraphicsQueue.Execute(context).Block();

// Present the rendered frame to the output
_output.Present();
}

public override void Dispose()
{
_pso.Dispose();
_vertices.Dispose();
_output.Dispose();
_device.Dispose();
}
}
}
28 changes: 28 additions & 0 deletions interactive/Voltium.Interactive/Aalaap/Shader.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
struct VertexOut
{
float4 SVPosition : SV_Position;
float4 Position : POSITION;
float4 Color : COLOR;
nointerpolation float4 Center : CENTER;
};

VertexOut VertexMain(float3 position : POSITION, float3 center : CENTER, float4 color : COLOR)
{
VertexOut v;
v.SVPosition = float4(position, 1);
v.Position = float4(position, 1);
v.Center = float4(center, 1);
v.Color = color;
return v;
}


float4 PixelMain(VertexOut v) : SV_Target
{
float MAX_LEN = 0.45;

float len = length(v.Center - v.Position.xy) / MAX_LEN;
len = clamp(len, 0, 1);
//return lerp(v.Color, float4(0, 0, 0, 0), len / 100);
return lerp(float4(0, 1, 1, 1), float4(0, 0, 0, 1), sqrt(len));
}
3 changes: 2 additions & 1 deletion interactive/Voltium.Interactive/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
using Microsoft.Extensions.Logging;
using System.Numerics;
using Voltium.Interactive.HelloTriangleRaytracing;
using Voltium.Interactive.Aalaap;

namespace Voltium.Interactive
{
internal static unsafe class Program
{
private static int Main(string[] args)
{
ApplicationRunner.RunWin32(new HelloTriangleRaytracingApp());
ApplicationRunner.RunWin32(new AalaapApp());
return 0;
}
}
Expand Down
3 changes: 3 additions & 0 deletions interactive/Voltium.Interactive/Voltium.Interactive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
</ItemGroup>

<ItemGroup>
<None Include="Aalaap\Shader.hlsl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Assets\cube.obj" />
<None Include="Assets\logo.mtl" />
<None Include="Assets\logo.obj" />
Expand Down
1 change: 1 addition & 0 deletions sources/Voltium.Core/Contexts/GpuContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public GpuContext()
{
_attachedResources = new(1, ArrayPool<ResourceHandle>.Shared);
_encoder = ContextEncoder.Create(new ValueResettableArrayBufferWriter<byte>(64));
_closedForReset = ContextEncoder.Create(new ValueResettableArrayBufferWriter<byte>(64));
_closedMemory = ReadOnlyMemory<byte>.Empty;
_firstPipeline = default;
}
Expand Down
4 changes: 2 additions & 2 deletions sources/Voltium.Core/NativeApi/D3D12/D3D12NativeDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ public PipelineHandle CreatePipeline(in NativeGraphicsPipelineDesc desc)

var rootSig = _mapper.GetInfo(desc.RootSignature);

builder.Add(new D3D12_NODE_MASK { NodeMask = desc.NodeMask });
//builder.Add(new D3D12_NODE_MASK { NodeMask = desc.NodeMask });

builder.Add(new D3D12_GLOBAL_ROOT_SIGNATURE { pGlobalRootSignature = rootSig.RootSignature });

Expand Down Expand Up @@ -1298,7 +1298,7 @@ public PipelineHandle CreatePipeline(in NativeGraphicsPipelineDesc desc)
BlendEnable = Helpers.BoolToInt32(renderTargetBlendDesc.EnableBlendOp),
LogicOpEnable = Helpers.BoolToInt32(renderTargetBlendDesc.EnableLogicOp),
SrcBlend = (D3D12_BLEND)renderTargetBlendDesc.SrcBlend,
DestBlend = (D3D12_BLEND)renderTargetBlendDesc.SrcBlend,
DestBlend = (D3D12_BLEND)renderTargetBlendDesc.DestBlend,
BlendOp = (D3D12_BLEND_OP)renderTargetBlendDesc.BlendOp,
SrcBlendAlpha = (D3D12_BLEND)renderTargetBlendDesc.SrcBlendAlpha,
DestBlendAlpha = (D3D12_BLEND)renderTargetBlendDesc.DestBlendAlpha,
Expand Down

0 comments on commit 1bd2004

Please sign in to comment.