Skip to content

Commit

Permalink
Vulkan: Add guards to buffer and image lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoeris committed Nov 26, 2018
1 parent e49bbc4 commit d178f6a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
4 changes: 3 additions & 1 deletion gapis/api/vulkan/api/buffer.api
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ cmd void vkDestroyBuffer(
VkDevice device,
VkBuffer buffer,
AllocationCallbacks pAllocator) {
if !(device in Devices) { vkErrorInvalidDevice(device) } else {
if !(device in Devices) { vkErrorInvalidDevice(device) }
if !(buffer in Buffers) { vkErrorInvalidBuffer(buffer) } else {
if (buffer != as!VkBuffer(0)) {
bufferObject := Buffers[buffer]
if (bufferObject.Memory != null) {
Expand Down Expand Up @@ -180,6 +181,7 @@ cmd VkResult vkCreateBufferView(
next.Ptr = as!VulkanStructHeader*(next.Ptr)[0:1][0].PNext
}
}
if !(buffer_view_create_info.buffer in Buffers) { vkErrorInvalidBuffer(buffer_view_create_info.buffer) }

handle := ?
bufferViewObject := new!BufferViewObject(
Expand Down
12 changes: 7 additions & 5 deletions gapis/api/vulkan/api/coherent_memory.api
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,13 @@ sub void writeMemoryInImageBindings(map!(u32, ref!VkDescriptorImageInfo) imageBi
for _, _, v in imageBindings {
_ = Samplers[v.Sampler]
if v.ImageView != as!VkImageView(0) {
imageViewObj := ImageViews[v.ImageView]
imageObj := imageViewObj.Image
rng := imageViewObj.SubresourceRange
writeImageSubresource(imageObj, rng)
updateImageQueue(imageObj, rng)
if (v.ImageView in ImageViews) {
imageViewObj := ImageViews[v.ImageView]
imageObj := imageViewObj.Image
rng := imageViewObj.SubresourceRange
writeImageSubresource(imageObj, rng)
updateImageQueue(imageObj, rng)
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions gapis/api/vulkan/api/copy_clear_commands.api
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
}

sub void dovkCmdCopyBuffer(ref!vkCmdCopyBufferArgs buffer) {
if !(buffer.SrcBuffer in Buffers) { vkErrorInvalidBuffer(buffer.SrcBuffer) }
if !(buffer.DstBuffer in Buffers) { vkErrorInvalidBuffer(buffer.DstBuffer) }
sourceBuffer := Buffers[buffer.SrcBuffer]
destBuffer := Buffers[buffer.DstBuffer]
for _ , _ , region in buffer.CopyRegions {
Expand Down Expand Up @@ -212,6 +214,8 @@ class vkCmdCopyImageArgs {
}

sub void dovkCmdCopyImage(ref!vkCmdCopyImageArgs args) {
if !(args.SrcImage in Images) { vkErrorInvalidImage(args.SrcImage) }
if !(args.DstImage in Images) { vkErrorInvalidImage(args.DstImage) }
srcImageObject := Images[args.SrcImage]
dstImageObject := Images[args.DstImage]

Expand Down Expand Up @@ -339,6 +343,8 @@ class vkCmdBlitImageArgs {
}

sub void dovkCmdBlitImage(ref!vkCmdBlitImageArgs args) {
if !(args.SrcImage in Images) { vkErrorInvalidImage(args.SrcImage) }
if !(args.DstImage in Images) { vkErrorInvalidImage(args.DstImage) }
srcImageObject := Images[args.SrcImage]
dstImageObject := Images[args.DstImage]

Expand Down Expand Up @@ -510,6 +516,8 @@ class vkCmdCopyBufferToImageArgs {
}

sub void dovkCmdCopyBufferToImage(ref!vkCmdCopyBufferToImageArgs args) {
if !(args.SrcBuffer in Buffers) { vkErrorInvalidBuffer(args.SrcBuffer) }
if !(args.DstImage in Images) { vkErrorInvalidImage(args.DstImage) }
bufferObject := Buffers[args.SrcBuffer]
imageObject := Images[args.DstImage]
format := imageObject.Info.Format
Expand Down Expand Up @@ -669,6 +677,7 @@ cmd void vkCmdCopyImageToBuffer(
}

sub void dovkCmdUpdateBuffer(ref!vkCmdUpdateBufferArgs args) {
if !(args.DstBuffer in Buffers) { vkErrorInvalidBuffer(args.DstBuffer) }
Buffers[args.DstBuffer].LastBoundQueue = LastBoundQueue
buff := Buffers[args.DstBuffer]
bufPieces := getBufferBoundMemoryPiecesInRange(buff, args.DstOffset, args.DataSize)
Expand Down Expand Up @@ -713,6 +722,7 @@ class vkCmdFillBufferArgs {
}

sub void dovkCmdFillBuffer(ref!vkCmdFillBufferArgs args) {
if !(args.Buffer in Buffers) { vkErrorInvalidBuffer(args.Buffer) }
buf := Buffers[args.Buffer]
bufPieces := getBufferBoundMemoryPiecesInRange(buf, args.DstOffset, args.Size)
for _ , _ , p in bufPieces {
Expand Down Expand Up @@ -877,6 +887,8 @@ cmd void vkCmdClearAttachments(
}

sub void dovkCmdResolveImage(ref!vkCmdResolveImageArgs args) {
if !(args.SrcImage in Images) { vkErrorInvalidImage(args.SrcImage) }
if !(args.DstImage in Images) { vkErrorInvalidImage(args.DstImage) }
for _ , _ , r in args.ResolveRegions {
srcRange := VkImageSubresourceRange(
r.srcSubresource.aspectMask,
Expand Down
5 changes: 5 additions & 0 deletions gapis/api/vulkan/api/draw_commands.api
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ vkCmdBindIndexBufferArgs {


sub void dovkCmdBindIndexBuffer(ref!vkCmdBindIndexBufferArgs buffer) {
if !(buffer.Buffer in Buffers) { vkErrorInvalidBuffer(buffer.Buffer) }
lastDrawInfo().BoundIndexBuffer = new!BoundIndexBuffer(buffer.IndexType,
BoundBuffer(Buffers[buffer.Buffer], buffer.Offset, 0))
Buffers[buffer.Buffer].LastBoundQueue = LastBoundQueue
Expand Down Expand Up @@ -99,6 +100,7 @@ class CmdBindBuffer {
sub void dovkCmdBindVertexBuffers(ref!vkCmdBindVertexBuffersArgs bind) {
for i in (0 .. len(bind.Buffers)) {
v := bind.Buffers[as!u32(i)]
if !(v in Buffers) { vkErrorInvalidBuffer(v) }
lastDrawInfo().BoundVertexBuffers[as!u32(i) + bind.FirstBinding] = BoundBuffer(
Buffers[v], bind.Offsets[as!u32(i)],
Buffers[v].Info.Size - bind.Offsets[as!u32(i)])
Expand Down Expand Up @@ -260,6 +262,7 @@ sub void dovkCmdDrawIndirect(ref!vkCmdDrawIndirectArgs draw) {
readWriteMemoryInBoundGraphicsDescriptorSets()
command_size := as!VkDeviceSize(16)
indirect_buffer_read_size := as!VkDeviceSize((draw.DrawCount - 1) * draw.Stride) + command_size
if !(draw.Buffer in Buffers) { vkErrorInvalidBuffer(draw.Buffer) }
readMemoryInBuffer(Buffers[draw.Buffer], draw.Offset, indirect_buffer_read_size)
// Read through all the vertex buffers, as we cannot assume the buffer given to indirect draw is host
readMemoryInCurrentPipelineBoundVertexBuffers(0xFFFFFFFF, 0xFFFFFFFF, 0, 0)
Expand Down Expand Up @@ -307,6 +310,7 @@ sub void dovkCmdDrawIndexedIndirect(ref!vkCmdDrawIndexedIndirectArgs draw) {
readWriteMemoryInBoundGraphicsDescriptorSets()
command_size := as!VkDeviceSize(16)
indirect_buffer_read_size := as!VkDeviceSize((draw.DrawCount - 1) * draw.Stride) + command_size
if !(draw.Buffer in Buffers) { vkErrorInvalidBuffer(draw.Buffer) }
readMemoryInBuffer(Buffers[draw.Buffer], draw.Offset, indirect_buffer_read_size)
// Read through the whole index buffer.
indexBuffer := lastDrawInfo().BoundIndexBuffer.BoundBuffer.Buffer
Expand Down Expand Up @@ -384,6 +388,7 @@ class vkCmdDispatchIndirectArgs {

sub void dovkCmdDispatchIndirect(ref!vkCmdDispatchIndirectArgs dispatch) {
command_size := as!VkDeviceSize(12)
if !(dispatch.Buffer in Buffers) { vkErrorInvalidBuffer(dispatch.Buffer) }
readMemoryInBuffer(Buffers[dispatch.Buffer], dispatch.Offset, command_size)
readComputeState()
readWriteMemoryInBoundComputeDescriptorSets()
Expand Down
1 change: 1 addition & 0 deletions gapis/api/vulkan/api/renderpass_framebuffer.api
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ cmd VkResult vkCreateFramebuffer(
Layers: create_info.layers,
RenderPass: RenderPasses[create_info.renderPass])
for i in (0 .. create_info.attachmentCount) {
if !(attachments[i] in ImageViews) { vkErrorInvalidImageView(attachments[i]) }
framebufferObject.ImageAttachments[i] = ImageViews[attachments[i]]
}

Expand Down

0 comments on commit d178f6a

Please sign in to comment.