Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better design for vkMapMemory function #19

Closed
mackst opened this issue Jul 28, 2017 · 6 comments
Closed

A better design for vkMapMemory function #19

mackst opened this issue Jul 28, 2017 · 6 comments

Comments

@mackst
Copy link
Contributor

mackst commented Jul 28, 2017

I am looking at vulkan compute example. I think the vkMapMemory function can have better design for better performance.

Usually fill mapped memory data

data = vkMapMemory(device, memory, 0, memorySize, 0)
# fill data
mydata = ffi.new('int[]', [1, 2, 3, 4, 5])
ffi.memmove(data, mydata, memorySize)
vkUnmapMemory(device, memory)

Above is OK. But if you want to fetch the data from GPU buffer

data = vkMapMemory(device, memory, 0, memorySize, 0)

# amusing int array data
# you need to loop through the array to get the data
mydata = [ffi.addressof(data, i)[0] for i in range(int(memorySize/4))]

vkUnmapMemory(device, memory)

array.array work with cffi example

import array
import cffi

ffi = cffi.FFI()

a = array.array('I', [1, 2, 3, 4, 5])
pa = ffi.cast('uint32_t *', a.buffer_info()[0])
print(pa, pa[0])
pa[0] = 7
print(a)
print(pa, pa[0])

a[0] = 10
print(a)
print(pa, pa[0])
print(ffi.addressof(pa, 1), ffi.addressof(pa, 1)[0])

<cdata 'uint32_t *' 0x000002A454682420> 1
array('I', [7, 2, 3, 4, 5])
<cdata 'uint32_t *' 0x000002A454682420> 7
array('I', [10, 2, 3, 4, 5])
<cdata 'uint32_t *' 0x000002A454682420> 10
<cdata 'uint32_t *' 0x000002A454682424> 2

So how about we can do something like this with vkMapMemory

bufferLength = 16384
bufferSize = 4 * bufferLength
memorySize = bufferSize * 2
msize = int(memorySize / 4)

# data array
da = array.array('I', [0] * msize)
# pointer of the array
pda = ffi.cast('uint32_t *', da.buffer_info()[0])
# pointer of the array pointer
ppda = ffi.cast('void**', pda)
vkMapMemory(device, memory, 0, memorySize, 0, ppda)
# fill data
for i in range(len(da)):
    da[i] = random.randint(1, 100000)

vkUnmapMemory(device, memory)

##################################################
# do compute stuff
##################################################

# fetch data

vkMapMemory(device, memory, 0, memorySize, 0, ppda)
# no for loop
print(da)

vkUnmapMemory(device, memory)
@realitix
Copy link
Owner

Hello @mackst, thanks for the example.

I don't exactly understand your question, is it working with your example ? What do you want to change with VkMapMemory ?

@mackst
Copy link
Contributor Author

mackst commented Jul 29, 2017

Yes it works. What I want is VkMapMemory either can be a more lower or higher level function.

lower level just like above user can define their data pointer to pass to VkMapMemory function.
higher level would be VkMapMemory function can take array instance cast to data pointer and return the pointer.
For higher level may also need to support numpy array in the future.

@realitix
Copy link
Owner

If I understand correctly your issue, you want to pass a custom ppda whereas currently you can't because the wrapper do it for you and return the ppda pointer.

Moreover, managing yourself the ppda would allow you to reuse the pointer.

Is is what your are saying ?

@mackst
Copy link
Contributor Author

mackst commented Jul 29, 2017

Yes

@realitix
Copy link
Owner

OK @mackst.
I noticed that in your example, you allocate the ppda pointer before calling VkMapMemory. By doing that, VkMapMemory will replace the address in the pointer by the mapped address. So I don't understand why you allocate memory for the pointer.
VkMapMemory allows you to update the memory directly in the GPU. I don't think it's a good idea to reuse a reference across several VkMapMemory because you can't be sure that the mapping will be done at the same place in the memory.

Maybe you are trying to resolve a no-issue.
If I don't fully understand your need, don't hesitate to correct me but I think VkMapMemory works as expected.

@mackst
Copy link
Contributor Author

mackst commented Jul 31, 2017

I try again. I think your are right

@mackst mackst closed this as completed Jul 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants