[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
public void SetUnorderedAccessViews( UnorderedAccessView[] unorderedAccessViews, int startSlot, int count );
とあるとき、 RWStructuredBuffer<int> BufferSum;RWStructuredBuffer<int> Buffer0;RWStructuredBuffer<int> Buffer1;
: register( [shader_profile], Type#[subcomponent])shader_profileはつけるつけないは自由のオプションですが、"ps_5_0"等のシェーダープロファイルです。
Type | 対応するリソース |
b | 定数バッファ |
t | テクスチャとテクスチャバッファ |
c | Buffer offset |
s | Sampler |
u | Unordered Access View |
RWStructuredBuffer<int> BufferSum : register(u0);RWStructuredBuffer<int> Buffer0 : register(u1);RWStructuredBuffer<int> Buffer1 : register(u2);
RWStructuredBuffer<int> Buffer0 : register(u1);RWStructuredBuffer<int> Buffer1 : register(u2);
RWStructuredBuffer<int> BufferSum : register(u0);
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; static void Main(string[] args) { Device device = new Device(DriverType.Hardware); bufferSum = createStructuredBuffer(device, Enumerable.Range(0, 10).ToArray()); buffer0 = createStructuredBuffer(device, Enumerable.Range(0, 10).ToArray()); buffer1 = createStructuredBuffer(device, Enumerable.Range(0, 10).ToArray()); initComputeShader(device); device.ImmediateContext.Dispatch(10, 1, 1); writeBuffer(device, bufferSum); } static void initComputeShader(Device device) { device.ImmediateContext.ComputeShader.SetUnorderedAccessViews( new[] { new UnorderedAccessView(device, bufferSum), new UnorderedAccessView(device, buffer0), new UnorderedAccessView(device, buffer1), }, 0, 3 ); ShaderBytecode shaderBytecode = ShaderBytecode.CompileFromFile( "MyShader.fx", "MyComputeShader", "cs_5_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) { DataStream initialDataStream = new DataStream(initialData, true, true); return new Buffer( device, initialDataStream, new BufferDescription { SizeInBytes = (int)initialDataStream.Length, BindFlags = BindFlags.UnorderedAccess, 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);RWStructuredBuffer<int> Buffer0 : register(u1);RWStructuredBuffer<int> Buffer1 : register(u2);
[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今回やっていることはこうです: