-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters