[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
void GroupMemoryBarrierWithGroupSync();
groupshared int data[100];
using System.Collections.Generic; using System.Linq; using SlimDX; using SlimDX.Direct3D11; using SlimDX.D3DCompiler; class Program { static Buffer bufferSum; static void Main(string[] args) { Device device = new Device(DriverType.Hardware); bufferSum = createStructuredBuffer( device, 128 * sizeof(int), BindFlags.UnorderedAccess ); initComputeShader(device); device.ImmediateContext.Dispatch(1, 1, 1); writeBuffer(device, bufferSum); } static void initComputeShader(Device device) { device.ImmediateContext.ComputeShader.SetUnorderedAccessView( new UnorderedAccessView(device, bufferSum), 0 ); 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 sizeInBytes, BindFlags bindFlags) { return new Buffer( device, new BufferDescription { SizeInBytes = sizeInBytes, 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 dataBox = from.Device.ImmediateContext.MapSubresource( from, MapMode.Read, MapFlags.None ); int elementCount = (int)(dataBox.Data.Length / sizeof(int)); return dataBox.Data.ReadRange<int>(elementCount); } }
RWStructuredBuffer<int> MyBuffer : register(u0);
static const int ElementCount = 128;groupshared int data[ElementCount];
int getSum(){int result = 0;
for(int i = 0; i < ElementCount; i++){result += data[i];}
return result;}
[numthreads(ElementCount, 1, 1)]void MyComputeShader(uint groupIndex : SV_GroupIndex ){int time = 0;
//スレッドによって処理時間が変わるfor(int i = 0; i < groupIndex; i++){time++;}
data[groupIndex] = time;//GroupMemoryBarrierWithGroupSync();MyBuffer[groupIndex] = getSum();}
1951777886 1951777886 1951777886 1951777886 1951777886 1951777886 1951777886 1951777886 1951777886 ..(中略) 1951777886 8128 8128 8128 8128 8128 8128 8128 8128 ..(中略) 8128