示例#1
0
        public HardwareInputBuffer([NotNull] AccelerationDevice provider, [NotNull] Type itemType, int itemCount) : base(provider, Marshal.SizeOf(itemType) * itemCount)
        {
            if (itemType == null)
            {
                throw new ArgumentNullException(nameof(itemType));
            }

            if (!itemType.IsValueType)
            {
                throw new ArgumentException("Can't use a reference type as buffer structure.", nameof(itemType));
            }

            var nStructure = Marshal.SizeOf(itemType);
            var nBytes     = nStructure * itemCount;

            if (Context.IsDebugModeEnabled)
            {
                Context.DiagnosticsWriter.WriteDebug("Allocating {0:###,###,###,###,###,##0} bytes of UAV memory with a structure stride of {1:###,###,###,###,###,##0} bytes",
                                                     nBytes, nStructure);
            }

            var description = new BufferDescription
            {
                BindFlags           = BindFlags.ShaderResource,
                CpuAccessFlags      = CpuAccessFlags.Write,
                OptionFlags         = ResourceOptionFlags.StructuredBuffer,
                SizeInBytes         = nBytes,
                StructureByteStride = nStructure,
                Usage = ResourceUsage.Dynamic
            };

            mHardwareBuffer = new Buffer(Device, description);
            mResourceView   = new ShaderResourceView(Device, mHardwareBuffer);
        }
示例#2
0
        public HardwareTexture([NotNull] AccelerationDevice device, int width, int height) : base(device)
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(width));
            }

            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(height));
            }

            mArrayWidth  = width;
            mArrayHeight = height;

            var description = new Texture2DDescription
            {
                ArraySize         = 1,
                MipLevels         = 1,
                BindFlags         = BindFlags.ShaderResource,
                Format            = Format.R32G32B32A32_Float,
                CpuAccessFlags    = CpuAccessFlags.Write,
                Width             = width,
                Height            = height,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Dynamic
            };

            mTexture      = new Texture2D(Device, description);
            mResourceView = new ShaderResourceView(Device, mTexture);
        }
示例#3
0
        private static void Execute(AccelerationDevice device, CancellationToken cancellationToken)
        {
            using (var kernel = GetKernel(device))
            {
                using (var cb = new HardwareConstantBuffer <int>(device))
                    using (var bIn = new HardwareInputBuffer(device, typeof(int), 10))
                        using (var bOut = new HardwareOutputBuffer(device, typeof(int), 10))
                        {
                            kernel.ThreadGroupCount = new Vector3 <uint>(10, 1, 1);
                            kernel.Constants[0]     = cb;
                            kernel.Resources[0]     = bIn;
                            kernel.Outputs[0]       = bOut;
                            cb.Structure            = 2;

                            var inData  = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                            var outData = new int[inData.Length];

                            var bInArrayView  = new ArrayWriter <int>(bIn);
                            var bOutArrayView = new ArrayReader <int>(bOut);

                            bInArrayView.Write(inData);
                            kernel.Execute();
                            bOutArrayView.Read(outData);

                            using (WithColor(ConsoleColor.White, ConsoleColor.DarkBlue))
                            {
                                Console.WriteLine($"[ {string.Join(", ", inData)} ] => [ {string.Join(", ", outData)} ]");
                            }
                        }
            }
        }
        private AcceleratedImagingKernel([NotNull] AccelerationDevice device, int width, int height) : base(device)
        {
            mDevice = device;
            mWidth  = width;
            mHeight = height;

            mConstants = new DelegatedHardwareResourceList <HardwareConstantBuffer>(this, (dc, res, slot) => dc.PixelShader.SetConstantBuffer(res.GetBuffer(), slot));
            mResources = new DelegatedHardwareResourceList <HardwareInputBuffer>(this, (dc, res, slot) => dc.PixelShader.SetShaderResource(res.GetShaderResourceView(), slot));
        }
示例#5
0
        internal HardwareRenderTarget([NotNull] AccelerationDevice device, [NotNull] SwapChain swapChain) : base(device)
        {
            if (swapChain == null)
            {
                throw new ArgumentNullException(nameof(swapChain));
            }

            mTexture          = SlimDX.Direct3D11.Resource.FromSwapChain <Texture2D>(swapChain, 0);
            mRenderTargetView = new RenderTargetView(Device, mTexture);
        }
示例#6
0
        public HardwareOutputBuffer([NotNull] AccelerationDevice provider, [NotNull] Type itemType, int itemCount) : base(provider, Marshal.SizeOf(itemType) * itemCount)
        {
            if (itemType == null)
            {
                throw new ArgumentNullException(nameof(itemType));
            }

            if (!itemType.IsValueType)
            {
                throw new ArgumentException("Can't use a reference type as buffer structure.", nameof(itemType));
            }

            var nStructure = Marshal.SizeOf(itemType);
            var nBytes     = nStructure * itemCount;

            if (Context.IsDebugModeEnabled)
            {
                Context.DiagnosticsWriter.WriteDebug("Allocating {0:###,###,###,###,###,##0} bytes of UAV memory with a structure stride of {1:###,###,###,###,###,##0} bytes",
                                                     nBytes, nStructure);
            }

            var bufferDescription = new BufferDescription
            {
                BindFlags           = BindFlags.ShaderResource | BindFlags.UnorderedAccess,
                CpuAccessFlags      = CpuAccessFlags.None,
                OptionFlags         = ResourceOptionFlags.StructuredBuffer,
                SizeInBytes         = nBytes,
                StructureByteStride = nStructure,
                Usage = ResourceUsage.Default
            };

            var swapDescription = new BufferDescription
            {
                BindFlags           = BindFlags.None,
                CpuAccessFlags      = CpuAccessFlags.Read | CpuAccessFlags.Write,
                OptionFlags         = ResourceOptionFlags.StructuredBuffer,
                SizeInBytes         = bufferDescription.SizeInBytes,
                StructureByteStride = bufferDescription.StructureByteStride,
                Usage = ResourceUsage.Staging
            };

            var viewDescription = new UnorderedAccessViewDescription
            {
                ElementCount = itemCount,
                Format       = SlimDX.DXGI.Format.Unknown,
                Dimension    = UnorderedAccessViewDimension.Buffer,
                Flags        = UnorderedAccessViewBufferFlags.None
            };

            mSwapBuffer          = new Buffer(Device, swapDescription);
            mHardwareBuffer      = new Buffer(Device, bufferDescription);
            mResourceView        = new ShaderResourceView(Device, mHardwareBuffer);
            mUnorderedAccessView = new UnorderedAccessView(Device, mHardwareBuffer, viewDescription);
        }
示例#7
0
        internal HardwareResource([NotNull] AccelerationDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            mRegistry = device;
            mDevice   = mRegistry.GetDevice();

            device.AddResource(this);
        }
示例#8
0
        protected override IResult Execute(CancellationToken cancellationToken)
        {
            Log.Verbosity = LogVerbosity.Debug;

            using (var device = new AccelerationDevice(Log))
            {
                Execute(device, cancellationToken);
            }

            Console.ReadKey();

            return(Result.Success);
        }
示例#9
0
 protected sealed override void DisposeOverride()
 {
     try
     {
         DisposeResource();
     }
     finally
     {
         mRegistry?.RemoveResource(this);
         mRegistry = null;
         mDevice   = null;
     }
 }
示例#10
0
        public static IAcceleratedImagingKernelConfiguration FromBytecode([NotNull] AccelerationDevice device, [NotNull] IReadableMemory bytecode)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (bytecode == null)
            {
                throw new ArgumentNullException(nameof(bytecode));
            }

            return(new AcceleratedImagingKernelConfiguration(device, k => k.Load(bytecode)));
        }
示例#11
0
        public static IAcceleratedImagingKernelConfiguration FromSource([NotNull] AccelerationDevice device, [NotNull] KernelSourceWriter source, CompilerContext context = null)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            context = context ?? new CompilerContext();
            return(new AcceleratedImagingKernelConfiguration(device, k => k.Compile(source, context)));
        }
        public static AcceleratedComputationKernel FromBytecode([NotNull] AccelerationDevice device, [NotNull] IReadableMemory bytecode)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (bytecode == null)
            {
                throw new ArgumentNullException(nameof(bytecode));
            }

            var kernel = new AcceleratedComputationKernel(device);

            kernel.Load(bytecode);
            return(kernel);
        }
        public static AcceleratedComputationKernel FromSource([NotNull] AccelerationDevice device, [NotNull] KernelSourceWriter source, CompilerContext context = null)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var kernel = new AcceleratedComputationKernel(device);

            context = context ?? new CompilerContext();
            kernel.Compile(source, context);
            return(kernel);
        }
示例#14
0
        private static AcceleratedComputationKernel GetKernel(AccelerationDevice device)
        {
            var kernelWriter = new KernelSourceWriter();

            kernelWriter.Write(@"

				cbuffer cb : register(b0) {
					int x;
				}

				StructuredBuffer<int> b_in : register(t0);
				RWStructuredBuffer<int> b_out : register(u0);

				[numthreads(10, 1, 1)]
				void main(uint3 id : SV_DispatchThreadId) {
					b_out[id.x] = x * b_in[id.x];
				}

			"            );

            return(AcceleratedComputationKernel.FromSource(device, kernelWriter));
        }
 private AcceleratedComputationKernel([NotNull] AccelerationDevice device) : base(device)
 {
     mConstants = new DelegatedHardwareResourceList <HardwareConstantBuffer>(this, (dc, res, slot) => dc.ComputeShader.SetConstantBuffer(res?.GetBuffer(), slot));
     mResources = new DelegatedHardwareResourceList <HardwareInputBuffer>(this, (dc, res, slot) => dc.ComputeShader.SetShaderResource(res?.GetShaderResourceView(), slot));
     mOutputs   = new DelegatedHardwareResourceList <HardwareOutputBuffer>(this, (dc, res, slot) => dc.ComputeShader.SetUnorderedAccessView(res?.GetUnorderedAccessView(), slot));
 }
示例#16
0
 public AcceleratedImagingKernelConfiguration(AccelerationDevice device, Action <AcceleratedImagingKernel> constructor)
 {
     mDevice      = device;
     mConstructor = constructor;
 }