[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
public class ShaderResourceView : ResourceView
public ShaderResourceView(Device device, Resource resource);
public void SetShaderResources( ShaderResourceView[] resourceViews, int startSlot, int count );
T Operator[](in uint indexPosition);
using System.Collections.Generic; using System.Linq; using SlimDX; using SlimDX.Direct3D11; using SlimDX.D3DCompiler; class Program { static Buffer bufferSum; static Buffer buffer0; static Buffer buffer1; const int ElementCount = 20; static void Main(string[] args) { Device device = new Device(DriverType.Hardware); bufferSum = createStructuredBuffer( device, Enumerable.Range(0, ElementCount).ToArray(), BindFlags.UnorderedAccess ); buffer0 = createStructuredBuffer( device, Enumerable.Range(0, ElementCount).ToArray(), BindFlags.ShaderResource ); buffer1 = createStructuredBuffer( device, Enumerable.Range(0, ElementCount).ToArray(), BindFlags.ShaderResource ); initComputeShader(device); device.ImmediateContext.Dispatch(ElementCount, 1, 1); writeBuffer(device, bufferSum); } static void initComputeShader(Device device) { device.ImmediateContext.ComputeShader.SetUnorderedAccessView( new UnorderedAccessView(device, bufferSum), 0 ); device.ImmediateContext.ComputeShader.SetShaderResources( new[] { new ShaderResourceView(device, buffer0), new ShaderResourceView(device, buffer1), }, 0, 2 ); ShaderBytecode shaderBytecode = ShaderBytecode.CompileFromFile( "MyShader.fx", "MyComputeShader", "cs_4_0", ShaderFlags.None, EffectFlags.None ); device.ImmediateContext.ComputeShader.Set( new ComputeShader(device, shaderBytecode) ); } static void writeBuffer(Device device, Buffer buffer) { Buffer cpuAccessibleBuffer = createCpuAccessibleBuffer(device, buffer.Description.SizeInBytes); device.ImmediateContext.CopyResource(buffer, cpuAccessibleBuffer); int[] readBack = readBackFromGpu(cpuAccessibleBuffer); foreach (var number in readBack) { System.Console.WriteLine(number); } } static Buffer createStructuredBuffer(Device device, int[] initialData, BindFlags bindFlags) { DataStream initialDataStream = new DataStream(initialData, true, true); return new Buffer( device, initialDataStream, new BufferDescription { SizeInBytes = (int)initialDataStream.Length, BindFlags = bindFlags, OptionFlags = ResourceOptionFlags.StructuredBuffer, StructureByteStride = sizeof(int) } ); } static Buffer createCpuAccessibleBuffer(Device device, int sizeInBytes) { return new Buffer( device, new BufferDescription { SizeInBytes = sizeInBytes, CpuAccessFlags = CpuAccessFlags.Read, Usage = ResourceUsage.Staging } ); } static int[] readBackFromGpu(Buffer from) { DataBox data = from.Device.ImmediateContext.MapSubresource( from, MapMode.Read, MapFlags.None ); return getArrayInt32(data.Data); } static int[] getArrayInt32(DataStream stream) { int[] buffer = new int[stream.Length / sizeof(int)]; stream.ReadRange(buffer, 0, buffer.Length); return buffer; } }
RWStructuredBuffer<int> BufferSum : register(u0);StructuredBuffer<int> Buffer0 : register(t0);StructuredBuffer<int> Buffer1 : register(t1);
[numthreads(1, 1, 1)]void MyComputeShader(uint3 threadID : SV_DispatchThreadID ){BufferSum[threadID.x] = Buffer0[threadID.x] + Buffer1[threadID.x];}
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38