GasPatch(const RefCountedPtr<GasPatchContext> &_ctx, GasGiant *gs, vector3d v0, vector3d v1, vector3d v2, vector3d v3) : ctx(_ctx), gasSphere(gs), clipCentroid(((v0+v1+v2+v3) * 0.25).Normalized()), clipRadius(0.0) { v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3; for (int i=0; i<4; i++) { clipRadius = std::max(clipRadius, (v[i]-clipCentroid).Length()); } UpdateVBOs(); }
static void Display_(void) { FrameCount++; ExecutionCount++; uint64_t uiStartTime = GetCurrentTime(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); if(Animated) { UpdateData(); UpdateVBOs(); } int err = Recompute(); if (err != 0) { printf("Error %d from Recompute!\n", err); exit(1); } if (EnableStideExec && ((ExecutionCount / ExecuteStride) % 2) == 0) { printf("CL only for frame %d\n", ExecutionCount); return; } glDrawArrays(GL_POINTS, 0, DataElemCount / 4); ReportInfo(); glFinish(); // for timing uint64_t uiEndTime = GetCurrentTime(); ReportStats(uiStartTime, uiEndTime); DrawText(TextOffset[0], TextOffset[1], 1, (Animated == 0) ? "Press space to animate" : " "); glutSwapBuffers(); }
static int Recompute(void) { if(!ComputeKernel) return CL_SUCCESS; if (NDRangeCount > MaxNDRange) { printf("Reach Max NDRange, Quitting\n"); Cleanup(); exit(0); } if (EnableStideExec && (ExecutionCount / ExecuteStride) % 2 == 1) { printf("GL only for frame %d\n", ExecutionCount); return CL_SUCCESS; } void *values[2]; size_t sizes[2]; int err = 0; unsigned int v = 0, s = 0, a = 0; values[v++] = &ComputeInputOutputReal; values[v++] = &ComputeInputOutputImaginary; sizes[s++] = sizeof(cl_mem); sizes[s++] = sizeof(cl_mem); if(Animated || Update) { glFinish(); // If use shared context, then data for ComputeInputOutput* is already in Vbo* #if (USE_GL_ATTACHMENTS) err = clEnqueueAcquireGLObjects(ComputeCommands, 1, &ComputeInputOutputReal, 0, 0, 0); if (err != CL_SUCCESS) { printf("Failed to acquire GL object! %d\n", err); return EXIT_FAILURE; } err = clEnqueueAcquireGLObjects(ComputeCommands, 1, &ComputeInputOutputImaginary, 0, 0, 0); if (err != CL_SUCCESS) { printf("Failed to acquire GL object! %d\n", err); return EXIT_FAILURE; } #if (DEBUG_INFO) float *DataRealReadBack = (float *)calloc(1, sizeof(float) * DataElemCount); float *DataImaginaryReadBack = (float *)calloc(1, sizeof(float) * DataElemCount); err = clEnqueueReadBuffer( ComputeCommands, ComputeInputOutputReal, CL_TRUE, 0, DataElemCount * sizeof(float), DataRealReadBack, 0, NULL, NULL ); if (err != CL_SUCCESS) { printf("Failed to read buffer! %d\n", err); return EXIT_FAILURE; } err = clEnqueueReadBuffer( ComputeCommands, ComputeInputOutputImaginary, CL_TRUE, 0, DataElemCount * sizeof(float), DataImaginaryReadBack, 0, NULL, NULL ); if (err != CL_SUCCESS) { printf("Failed to read buffer! %d\n", err); return EXIT_FAILURE; } for (int i = 0; i < DataElemCount; ++i) printf("Before NDRange %d - %d: [%f %f] - [%f %f]\n", NDRangeCount, i, DataReal[i], DataImaginary[i], DataRealReadBack[i], DataImaginaryReadBack[i]); free(DataRealReadBack); free(DataImaginaryReadBack); #endif #else // Not sharing context with OpenGL, needs to explicitly copy/write to exchange data err = clEnqueueWriteBuffer(ComputeCommands, ComputeInputOutputReal, 1, 0, DataElemCount * sizeof(float), DataReal, 0, 0, NULL); if (err != CL_SUCCESS) { printf("Failed to write buffer! %d\n", err); return EXIT_FAILURE; } err = clEnqueueWriteBuffer(ComputeCommands, ComputeInputOutputImaginary, 1, 0, DataElemCount * sizeof(float), DataImaginary, 0, 0, NULL); if (err != CL_SUCCESS) { printf("Failed to write buffer! %d\n", err); return EXIT_FAILURE; } #endif Update = 0; err = CL_SUCCESS; for (a = 0; a < s; a++) err |= clSetKernelArg(ComputeKernel, a, sizes[a], values[a]); if (err) return -10; size_t global[1]; size_t local[1]; global[0] = DataElemCount; local[0] = 64; #if (DEBUG_INFO) if(FrameCount <= 1) printf("Global[%4d] Local[%4d]\n", (int)global[0], (int)local[0]); #endif err = clEnqueueNDRangeKernel(ComputeCommands, ComputeKernel, 1, NULL, global, local, 0, NULL, NULL); if (err) { printf("Failed to enqueue kernel! %d\n", err); return err; } NDRangeCount++; #if DEBUG_INFO DataRealReadBack = (float *)calloc(1, sizeof(float) * DataElemCount); DataImaginaryReadBack = (float *)calloc(1, sizeof(float) * DataElemCount); err = clEnqueueReadBuffer( ComputeCommands, ComputeInputOutputReal, CL_TRUE, 0, DataElemCount * sizeof(float), DataRealReadBack, 0, NULL, NULL ); if (err != CL_SUCCESS) { printf("Failed to read buffer! %d\n", err); return EXIT_FAILURE; } err = clEnqueueReadBuffer( ComputeCommands, ComputeInputOutputImaginary, CL_TRUE, 0, DataElemCount * sizeof(float), DataImaginaryReadBack, 0, NULL, NULL ); if (err != CL_SUCCESS) { printf("Failed to read buffer! %d\n", err); return EXIT_FAILURE; } for (int i = 0; i < DataElemCount; ++i) printf("After NDRange %d - %d: [%f %f] - [%f %f]\n", NDRangeCount, i, DataReal[i], DataImaginary[i], DataRealReadBack[i], DataImaginaryReadBack[i]); free(DataRealReadBack); free(DataImaginaryReadBack); #endif #if (USE_GL_ATTACHMENTS) // Release control and the data is already in VBOs err = clEnqueueReleaseGLObjects(ComputeCommands, 1, &ComputeInputOutputReal, 0, 0, 0); if (err != CL_SUCCESS) { printf("Failed to release GL object! %d\n", err); return EXIT_FAILURE; } err = clEnqueueReleaseGLObjects(ComputeCommands, 1, &ComputeInputOutputImaginary, 0, 0, 0); if (err != CL_SUCCESS) { printf("Failed to release GL object! %d\n", err); return EXIT_FAILURE; } #else // Explicitly copy data back to host and update VBOs err = clEnqueueReadBuffer( ComputeCommands, ComputeInputOutputReal, CL_TRUE, 0, DataElemCount * sizeof(float), DataReal, 0, NULL, NULL ); if (err != CL_SUCCESS) { printf("Failed to read buffer! %d\n", err); return EXIT_FAILURE; } err = clEnqueueReadBuffer( ComputeCommands, ComputeInputOutputImaginary, CL_TRUE, 0, DataElemCount * sizeof(float), DataImaginary, 0, NULL, NULL ); if (err != CL_SUCCESS) { printf("Failed to read buffer! %d\n", err); return EXIT_FAILURE; } UpdateVBOs(); #endif clFinish(ComputeCommands); } return CL_SUCCESS; }