diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-10-31 19:53:25 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-10-31 19:53:25 -0400 |
| commit | 111013b9ecb8b4208bfb1109e0f8ce8291fc90df (patch) | |
| tree | db67132d08569ade41c50b9e8948df3b094095d0 | |
| parent | 8a3994c5f6fd7023b5bdff9a02bc1f0ba110f12f (diff) | |
| download | balls-111013b9ecb8b4208bfb1109e0f8ce8291fc90df.zip | |
remove gcc vector with struct
| -rw-r--r-- | balls.c | 33 | ||||
| -rw-r--r-- | balls.h | 11 | ||||
| -rw-r--r-- | geo.c | 18 | ||||
| -rw-r--r-- | rand.c | 14 |
4 files changed, 40 insertions, 36 deletions
@@ -59,7 +59,7 @@ void freeGL(void); void initShaders(void); char *readFile(const char *filename, size_t *size); void compileShader(GLint shader); -float2 *noOverlapPositions(int n); +Vector *noOverlapPositions(int n); void frameCount(void); void drawString(const char *str); @@ -214,43 +214,48 @@ createKernel(cl_program prog, const char *kernelFunc) { void setPositions(void) { - float2 *hostPositions; + Vector *hostPositions; + int *hostPositionBuf; int err; /* Generate initial ball positions. */ hostPositions = noOverlapPositions(nBalls); + hostPositionBuf = flatten(hostPositions); + free(hostPositions); /* Create device-side buffer. */ - positions = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, nBalls*sizeof(float2), hostPositions, &err); + positions = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, nBalls*2*sizeof(float), hostPositionBuf, &err); if (err < 0) sysfatal("Failed to allocate position buffer.\n"); /* Copy positions to device. */ - err = clEnqueueWriteBuffer(queue, positions, CL_TRUE, 0, nBalls*sizeof(float2), hostPositions, 0, NULL, NULL); + err = clEnqueueWriteBuffer(queue, positions, CL_TRUE, 0, nBalls*2*sizeof(float), hostPositionBuf, 0, NULL, NULL); if (err < 0) sysfatal("Failed to copy ball positions to device.\n"); - free(hostPositions); + free(hostPositionBuf); } void setVelocities(void) { - float2 *hostVelocities; + float *hostVelocities; int i, err; /* Generate initial ball velocities. */ - if ((hostVelocities = malloc(nBalls*sizeof(float2))) == NULL) + if ((hostVelocities = malloc(nBalls*2*sizeof(float))) == NULL) sysfatal("Failed to allocate velocity array.\n"); - for (i = 0; i < nBalls; i++) - hostVelocities[i] = randVec(-VMAX_INIT, VMAX_INIT, -VMAX_INIT, VMAX_INIT); + for (i = 0; i < nBalls; i++) { + hostVelocities[2*i] = randFloat(-VMAX_INIT, VMAX_INIT); + hostVelocities[2*i+1] = randFloat(-VMAX_INIT, VMAX_INIT); + } /* Create device-side buffer. */ - velocities = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, nBalls*sizeof(float2), hostVelocities, &err); + velocities = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, nBalls*2*sizeof(float), hostVelocities, &err); if (err < 0) sysfatal("Failed to allocate velocity buffer.\n"); /* Copy velocities to device. */ - err = clEnqueueWriteBuffer(queue, velocities, CL_TRUE, 0, nBalls*sizeof(float2), hostVelocities, 0, NULL, NULL); + err = clEnqueueWriteBuffer(queue, velocities, CL_TRUE, 0, nBalls*2*sizeof(float), hostVelocities, 0, NULL, NULL); if (err < 0) sysfatal("Failed to copy ball velocities to device.\n"); @@ -589,13 +594,13 @@ compileShader(GLint shader) { * Generate n circle coordinates such that none overlap even if all circles * have the maximum radius. */ -float2 * +Vector * noOverlapPositions(int n) { - float2 *ps; + Vector *ps; Rectangle r; int i, j; - if ((ps = malloc(n*sizeof(float2))) == NULL) + if ((ps = malloc(n*sizeof(Vector))) == NULL) sysfatal("Failed to allocate position array.\n"); r = insetRect(bounds, RMAX); @@ -1,7 +1,9 @@ -typedef float float2 __attribute__ ((vector_size (2*sizeof(float)))); +typedef struct { + int x, y; +} Vector; typedef struct { - float2 min, max; + Vector min, max; } Rectangle; /* @@ -21,9 +23,8 @@ Partition partitionCollisions(size_t nBalls); void freePartition(Partition part); void printPartition(Partition part); -int isCollision(float2 p1, float r1, float2 p2, float r2); +int isCollision(Vector p1, float r1, Vector p2, float r2); Rectangle insetRect(Rectangle r, float n); float randFloat(float lo, float hi); -float2 randPtInRect(Rectangle r); -float2 randVec(float xmin, float xmax, float ymin, float ymax); +Vector randPtInRect(Rectangle r); @@ -3,18 +3,22 @@ #include "balls.h" int -isCollision(float2 p1, float r1, float2 p2, float r2) { - float2 dist; - float rhs; +isCollision(Vector p1, float r1, Vector p2, float r2) { + float dx, dy, rhs; - dist = p1 - p2; + dx = p1.x - p2.x; + dy = p1.y - p2.y; rhs = r1 + r2; - return (dist[0]*dist[0] + dist[1]*dist[1]) <= rhs*rhs; + return (dx*dx + dy*dy) <= rhs*rhs; } Rectangle insetRect(Rectangle r, float n) { - r.min += n; - r.max -= n; + r.min.x += n; + r.min.y += n; + + r.max.y -= n; + r.max.y -= n; + return r; } @@ -18,17 +18,11 @@ randFloat(float lo, float hi) { return lo + r*diff; } -float2 +Vector randPtInRect(Rectangle r) { - float2 pt = { - randFloat(r.min[0], r.max[0]), - randFloat(r.min[1], r.max[1]) + Vector pt = { + randFloat(r.min.x, r.max.x), + randFloat(r.min.y, r.max.y) }; return pt; } - -float2 -randVec(float xmin, float xmax, float ymin, float ymax) { - float2 v = {randFloat(xmin, xmax), randFloat(ymin, ymax)}; - return v; -} |