Skip to content

Instantly share code, notes, and snippets.

@Alan-FGR
Created September 19, 2018 21:38
Show Gist options
  • Save Alan-FGR/6be8b1fdf60bde885500b6f245e03f48 to your computer and use it in GitHub Desktop.
Save Alan-FGR/6be8b1fdf60bde885500b6f245e03f48 to your computer and use it in GitHub Desktop.
Lines 112:115 won't change texture *for reasons*... shader code on L64:65 and MiniTri.hlsl is from SharpDX samples (simple VS/PS)
using System;
using SharpDX;
using SharpDX.D3DCompiler;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Windows;
using Buffer = SharpDX.Direct3D11.Buffer;
using Device = SharpDX.Direct3D11.Device;
using Color = SharpDX.Color;
struct PosColTex
{
public Vector4 pos, col;
public Vector2 tex;
public PosColTex(Vector4 pos, Vector4 col, Vector2 tex) { this.pos = pos; this.col = col; this.tex = tex; }
public static InputElement[] VertsLayout => new[] {
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0),
new InputElement("TEXCOORD", 0, Format.R32G32_Float, 32, 0)};
}
internal static class Program
{
[STAThread]
private static void Main()
{
RenderForm form = new RenderForm("Test");
SwapChainDescription desc = new SwapChainDescription
{
BufferCount = 1,
ModeDescription =
new ModeDescription(form.ClientSize.Width, form.ClientSize.Height,
new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
// Create device, sc and setup window
Device device;
SwapChain swapChain;
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, new[] { FeatureLevel.Level_11_0 }, desc, out device, out swapChain);
DeviceContext context = device.ImmediateContext;
swapChain.GetParent<Factory>().MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
// Create backbuffer
Texture2D backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
RenderTargetView renderView = new RenderTargetView(device, backBuffer);
// Compile shaders
CompilationResult vertexShaderByteCode = ShaderBytecode.CompileFromFile("MiniTri.hlsl", "VS", "vs_5_0");
VertexShader vertexShader = new VertexShader(device, vertexShaderByteCode);
CompilationResult pixelShaderByteCode = ShaderBytecode.CompileFromFile("MiniTri.hlsl", "PS", "ps_5_0");
PixelShader pixelShader = new PixelShader(device, pixelShaderByteCode);
CompilationResult computeShaderBin = ShaderBytecode.Compile(@"RWTexture2D<uint> map : register(u0);
[numthreads(1, 1, 1)] void Main(uint3 id : SV_DispatchThreadID) { map[id.xy] = id.x + id.y; }",
"Main", "cs_5_0");
ComputeShader computeShader = new ComputeShader(device, computeShaderBin);
// Create random texture
const int width = 64;
const int height = 64;
Random random = new Random(42);
DataStream randbomBuffer = new DataStream(sizeof(float) * width * height, true, true);
for (int i = 0; i < width * height; i++) {
randbomBuffer.Write(random.Next());
}
Texture2D texture = new Texture2D(device,
new Texture2DDescription {
Format = Format.R32_UInt,
Width = width, Height = height,
BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess,
CpuAccessFlags = CpuAccessFlags.None,
ArraySize = 1, MipLevels = 1,
SampleDescription = new SampleDescription(1, 0)},
new DataRectangle(randbomBuffer.DataPointer, sizeof(float) * width));
// Create UAV and SRV
UnorderedAccessView uav = new UnorderedAccessView(device, texture);
ShaderResourceView srv = new ShaderResourceView(device, texture);
// Setup geometry
InputLayout layout = new InputLayout(device,ShaderSignature.GetInputSignature(vertexShaderByteCode),PosColTex.VertsLayout);
Buffer vertices = Buffer.Create(device, BindFlags.VertexBuffer, new[] {
new PosColTex(new Vector4(-1,-1,0,1.1f), new Vector4(0,0,0,1), new Vector2(0,0)),
new PosColTex(new Vector4(-1, 1,0,1.1f), new Vector4(0,1,0,1), new Vector2(1,0)),
new PosColTex(new Vector4( 1,-1,0,1.1f), new Vector4(1,0,0,1), new Vector2(0,1)),
new PosColTex(new Vector4( 1, 1,0,1.1f), new Vector4(1,1,0,1), new Vector2(1,1)),
});
// Set some pipeline states
context.InputAssembler.InputLayout = layout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 40, 0));
context.Rasterizer.SetViewport(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));
context.OutputMerger.SetTargets(renderView);
// Set and run compute
context.ComputeShader.Set(computeShader);
context.ComputeShader.SetUnorderedAccessView(0, uav);
context.Dispatch(1, 1, 1);
context.ComputeShader.SetUnorderedAccessView(0, null); //unset
// Set other shaders
context.PixelShader.Set(pixelShader);
context.PixelShader.SetShaderResource(0, srv);
context.VertexShader.Set(vertexShader);
// Main loop
RenderLoop.Run(form, () =>
{
context.ClearRenderTargetView(renderView, Color.CornflowerBlue);
context.Draw(4, 0);
swapChain.Present(2, PresentFlags.None);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment