Ejemplo n.º 1
0
        public RayIntersections(string name, uint maxCount, int deviceIndex) : base(name)
        {
            MaxRayCount = maxCount;

            RayBuffer    = new StreamableBuffer(name + "_rays", maxCount * RaySize, BufferUsage.Storage);
            rayBufferPtr = RayBuffer.LocalBuffer.GetRayDevicePointer(0);

            HitBuffer       = new StreamableBuffer(name + "_hits", maxCount * HitSize, BufferUsage.Storage);
            resultBufferPtr = HitBuffer.LocalBuffer.GetRayDevicePointer(0);

            //Allocate the necessary memory
            unsafe
            {
                ulong scratchSz = 0;
                if (rrGetTraceMemoryRequirements(GraphicsDevice.DeviceInformation[deviceIndex].RaysContext, maxCount, &scratchSz) != RRError.RrSuccess)
                {
                    throw new Exception("Failed to determine trace memory requirements.");
                }

                ScratchBuffer = new GpuBuffer(name + "_scratch")
                {
                    MemoryUsage = MemoryUsage.GpuOnly,
                    Size        = scratchSz,
                    Usage       = BufferUsage.Storage | BufferUsage.TransferDst
                };
                ScratchBuffer.Build(deviceIndex);
                scratchBufferPtr = ScratchBuffer.GetRayDevicePointer(0);
            }
        }
Ejemplo n.º 2
0
        public void Set(uint binding, uint idx, GpuBuffer buf, ulong off, ulong len)
        {
            var buf_info = new VkDescriptorBufferInfo()
            {
                buffer = buf.hndl,
                offset = off,
                range  = len
            };
            var buf_info_ptr = buf_info.Pointer();

            var desc_write = new VkWriteDescriptorSet()
            {
                sType            = VkStructureType.StructureTypeWriteDescriptorSet,
                dstSet           = hndl,
                dstBinding       = binding,
                dstArrayElement  = idx,
                descriptorCount  = 1,
                pImageInfo       = IntPtr.Zero,
                pBufferInfo      = buf_info_ptr,
                pTexelBufferView = null,
                descriptorType   = (VkDescriptorType)Layout.Layouts[(int)binding].Type
            };

            vkUpdateDescriptorSets(GraphicsDevice.GetDeviceInfo(devID).Device, 1, desc_write.Pointer(), 0, null);
        }
Ejemplo n.º 3
0
 public void DispatchIndirect(GpuBuffer buffer, long offset)
 {
     Commands.Add(new CommandEntry()
     {
         Type           = CommandType.DispatchIndirect,
         Buffer         = buffer,
         IndirectOffset = offset
     });
 }
Ejemplo n.º 4
0
        public void SetupBuild(int deviceIndex, uint triangleCnt, GpuBuffer vertices, ulong vertices_off, GpuBuffer indices, ulong indices_off, bool shortIndices)
        {
            devID             = deviceIndex;
            TrianglePrimitive = new RRTriangleMeshPrimitive()
            {
                index_type       = shortIndices ? RRIndexType.RrIndexTypeUint16 : RRIndexType.RrIndexTypeUint32,
                triangle_count   = triangleCnt,
                triangle_indices = indices.GetRayDevicePointer(indices_off),
                vertex_count     = VertexCount,
                vertex_stride    = VertexSize,
                vertices         = vertices.GetRayDevicePointer(vertices_off)
            };
            TrianglePrimitive_ptr = TrianglePrimitive.Pointer();

            GeometryBuildInput = new RRGeometryBuildInput()
            {
                primitive_type  = RRPrimitiveType.RrPrimitiveTypeTriangleMesh,
                primitive_count = 1,
                primitives      = TrianglePrimitive_ptr,
            };
            GeometryBuildInput_ptr = GeometryBuildInput.Pointer();

            RRBuildOptions opts = new RRBuildOptions()
            {
                build_flags           = 0,
                backend_specific_info = IntPtr.Zero,
            };
            var opts_ptr = opts.Pointer();

            //Allocate the necessary memory
            ManagedPtr <RRMemoryRequirements> geomMemReqs = new ManagedPtr <RRMemoryRequirements>();

            if (rrGetGeometryBuildMemoryRequirements(GraphicsDevice.DeviceInformation[deviceIndex].RaysContext, GeometryBuildInput_ptr, opts_ptr, geomMemReqs) != RRError.RrSuccess)
            {
                throw new Exception("Failed to determine geometry memory requirements.");
            }

            ScratchBuffer = new GpuBuffer(Name + "_scratch")
            {
                MemoryUsage = MemoryUsage.GpuOnly,
                Size        = geomMemReqs.Value.temporary_build_buffer_size,
                Usage       = BufferUsage.Storage | BufferUsage.TransferDst
            };
            ScratchBuffer.Build(deviceIndex);
            scratchBufferPtr = ScratchBuffer.GetRayDevicePointer(0);

            BuiltGeometryBuffer = new GpuBuffer(Name + "_geom")
            {
                MemoryUsage = MemoryUsage.GpuOnly,
                Size        = geomMemReqs.Value.result_buffer_size,
                Usage       = BufferUsage.Storage | BufferUsage.TransferDst
            };
            BuiltGeometryBuffer.Build(deviceIndex);
            geomBufferPtr = BuiltGeometryBuffer.GetRayDevicePointer(0);
        }
Ejemplo n.º 5
0
 public void DrawIndirect(GpuBuffer drawBuffer, ulong offset, GpuBuffer cntBuffer, ulong cntOffset, uint maxCnt, uint stride)
 {
     if (locked)
     {
         vkCmdDrawIndirectCount(hndl, drawBuffer.hndl, offset, cntBuffer.hndl, cntOffset, maxCnt, stride);
         IsEmpty = false;
     }
     else
     {
         throw new Exception("Command buffer not built.");
     }
 }
Ejemplo n.º 6
0
 public void DispatchIndirect(GpuBuffer indirectBuf, ulong offset)
 {
     if (locked)
     {
         vkCmdDispatchIndirect(hndl, indirectBuf.hndl, offset);
         IsEmpty = false;
     }
     else
     {
         throw new Exception("Command buffer not built.");
     }
 }
Ejemplo n.º 7
0
        public UniformBuffer(bool dynamic)
        {
            this.dynamic = dynamic;
            buf          = new GpuBuffer(BufferUsage.UniformBuffer, (ulong)UniformBufferSize, false);
            bindPoint    = GetFreeBindPoint();

            readyFence = new Fence[dynamic ? rungs : 1];
            for (int i = 0; i < readyFence.Length; i++)
            {
                readyFence[i] = new Fence();
                readyFence[i].PlaceFence();
            }
        }
Ejemplo n.º 8
0
 public void MultiDrawIndirect(PrimitiveType primType, GpuBuffer draws, long indirOffset, long drawCntOffset, int maxDrawCnt, int indirStride)
 {
     Commands.Add(new CommandEntry()
     {
         Type            = CommandType.MultiDrawIndirect,
         Buffer          = draws,
         PrimitiveType   = primType,
         IndirectOffset  = indirOffset,
         DrawCountOffset = drawCntOffset,
         MaxDrawCount    = maxDrawCnt,
         IndirectStride  = indirStride,
     });
 }
Ejemplo n.º 9
0
 public void DrawIndexed(GpuBuffer indexBuffer, ulong offset, IndexType indexType, uint indexCnt, uint instanceCnt, uint firstIndex, int firstVertex, uint baseInstance)
 {
     if (locked)
     {
         vkCmdBindIndexBuffer(hndl, indexBuffer.hndl, offset, (VkIndexType)indexType);
         vkCmdDrawIndexed(hndl, indexCnt, instanceCnt, firstIndex, firstVertex, baseInstance);
         IsEmpty = false;
     }
     else
     {
         throw new Exception("Command buffer not built.");
     }
 }
Ejemplo n.º 10
0
        StorageBuffer(GpuBuffer buf, bool stream)
        {
            size        = buf.Size / (ulong)(stream ? rungs : 1);
            this.buf    = buf;
            this.stream = stream;

            readyFence = new Fence[rungs];

            for (int i = 0; i < rungs; i++)
            {
                readyFence[i] = new Fence();
                readyFence[i].PlaceFence();
            }
        }
Ejemplo n.º 11
0
 //texture upload
 public void UploadTexture(GpuBuffer buffer, Texture tex, int write_lv, Vector3 offset, Vector3 size, PixelFormat fmt, PixelType type, Fence f)
 {
     Commands.Add(new CommandEntry()
     {
         Type       = CommandType.TexUpload,
         SyncFence  = f,
         Buffer     = buffer,
         Texture    = tex,
         WriteLevel = write_lv,
         Offset     = offset,
         Size       = size,
         Format     = fmt,
         PixType    = type
     });
 }
Ejemplo n.º 12
0
 public void Stage(GpuBuffer src, ulong src_off, GpuBuffer dst, ulong dst_off, ulong len)
 {
     if (locked)
     {
         var bufCopy = new VkBufferCopy()
         {
             dstOffset = dst_off,
             srcOffset = src_off,
             size      = len
         };
         vkCmdCopyBuffer(hndl, src.hndl, dst.hndl, 1, bufCopy.Pointer());
         IsEmpty = false;
     }
     else
     {
         throw new Exception("Command buffer not built.");
     }
 }
Ejemplo n.º 13
0
        public StreamableBuffer(string name, ulong sz, BufferUsage usage) : base(name)
        {
            this.Size       = sz;
            this.Streamable = true;
            LocalBuffer     = new GpuBuffer(name)
            {
                MemoryUsage = MemoryUsage.GpuOnly,
                Size        = sz,
                Usage       = usage | BufferUsage.TransferDst,
            };
            LocalBuffer.Build(0);

            HostBuffer = new GpuBuffer(name + "_host")
            {
                Mapped      = true,
                Size        = sz,
                MemoryUsage = MemoryUsage.CpuToGpu,
                Usage       = BufferUsage.TransferSrc
            };
            HostBuffer.Build(0);
        }
Ejemplo n.º 14
0
        public StreamableImage(string name, ulong buf_sz, uint dims, uint w, uint h, uint d, uint layers, uint levels, ImageViewType viewType, ImageFormat format, ImageUsage usage) : base(name)
        {
            this.Size       = buf_sz;
            this.Streamable = true;
            LocalImage      = new Image(Name + "_upload_img")
            {
                Cubemappable  = false,
                Width         = w,
                Height        = h,
                Depth         = d,
                Dimensions    = dims,
                InitialLayout = ImageLayout.Undefined,
                Layers        = layers,
                Levels        = levels,
                MemoryUsage   = MemoryUsage.GpuOnly,
                Usage         = usage | ImageUsage.TransferDst,
                Format        = format,
            };
            LocalImage.Build(0);

            LocalImageView = new ImageView(Name)
            {
                BaseLayer  = 0,
                BaseLevel  = 0,
                Format     = format,
                LayerCount = layers,
                LevelCount = levels,
                ViewType   = viewType,
            };
            LocalImageView.Build(LocalImage);

            HostBuffer = new GpuBuffer(name + "_host")
            {
                Mapped      = true,
                Size        = buf_sz,
                MemoryUsage = MemoryUsage.CpuToGpu,
                Usage       = BufferUsage.TransferSrc
            };
            HostBuffer.Build(0);
        }
Ejemplo n.º 15
0
 public void Stage(GpuBuffer src, ulong src_off, Image dst, uint mipLevel, uint baseArrayLayer, uint layerCount, int x, int y, int z, uint w, uint h, uint d)
 {
     if (locked)
     {
         unsafe
         {
             var bufCopy = new VkBufferImageCopy()
             {
                 bufferOffset      = src_off,
                 bufferRowLength   = 0,
                 bufferImageHeight = 0,
                 imageSubresource  = new VkImageSubresourceLayers()
                 {
                     aspectMask     = VkImageAspectFlags.ImageAspectColorBit,
                     mipLevel       = mipLevel,
                     baseArrayLayer = baseArrayLayer,
                     layerCount     = layerCount
                 },
                 imageOffset = new VkOffset3D()
                 {
                     x = x,
                     y = y,
                     z = z
                 },
                 imageExtent = new VkExtent3D()
                 {
                     width  = w,
                     height = h,
                     depth  = d
                 }
             };
             vkCmdCopyBufferToImage(hndl, src.hndl, dst.hndl, (VkImageLayout)dst.CurrentLayout, 1, bufCopy.Pointer());
             IsEmpty = false;
         }
     }
     else
     {
         throw new Exception("Command buffer not built.");
     }
 }
Ejemplo n.º 16
0
 public static void UploadBuffer(GpuBuffer src, GpuBuffer dst, ulong src_off, ulong dst_off, ulong sz)
 {
     GL.CopyNamedBufferSubData((int)src, (int)dst, (IntPtr)src_off, (IntPtr)dst_off, (IntPtr)sz);
 }
Ejemplo n.º 17
0
 public void SetElementBufferObject(GpuBuffer buffer)
 {
     GL.VertexArrayElementBuffer(id, buffer);
 }