summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-11-12 20:15:24 -0500
committerSam Anthony <sam@samanthony.xyz>2024-11-12 20:15:24 -0500
commit09fec8a0e1df59f191413c69d8ac431e7373814e (patch)
treea47bc6778d183e8e10b48d36e5262ab4f847dbc0
parent45058d8f5d510841aa759679af5aaac0e3a03edb (diff)
downloadballs-09fec8a0e1df59f191413c69d8ac431e7373814e.zip
copy positions from cpu to gpu
-rw-r--r--balls.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/balls.c b/balls.c
index ba6a5f4..587b989 100644
--- a/balls.c
+++ b/balls.c
@@ -452,6 +452,8 @@ setKernelArgs(void) {
void
animate(int v) {
+ cl_event readEvent;
+ int err;
clock_t tstart, elapsed;
unsigned int nextFrame;
@@ -461,8 +463,20 @@ animate(int v) {
collideBalls();
collideWalls();
+ /* Copy new positions from CPU to host asynchronously. */
+ err = clEnqueueReadBuffer(cpuQueue, positionsCpuBuf, CL_FALSE, 0, nBalls*2*sizeof(float), positionsHostBuf, 0, NULL, &readEvent);
+ if (err < 0)
+ sysfatal("Failed to copy positions from CPU to host.\n");
+
display();
+ /* Copy new positions from host to GPU. */
+ err = clEnqueueWriteBuffer(gpuQueue, positionsGpuBuf, CL_TRUE, 0, nBalls*2*sizeof(float), positionsHostBuf, 1, &readEvent, NULL);
+ if (err < 0)
+ sysfatal("Failed to copy positions from host to GPU.\n");
+
+ clReleaseEvent(readEvent);
+
elapsed = (clock() - tstart) / (CLOCKS_PER_SEC / MS_PER_S);
nextFrame = (elapsed > FRAME_TIME_MS) ? 0 : FRAME_TIME_MS-elapsed;
glutTimerFunc(nextFrame, animate, 0);