@ -242,7 +242,7 @@ int main(int argc, char* argv[])
// Setup the rng
std : : mt19937 rng ( time ( NULL ) ) ;
std : : uniform_int_distribution < int > rgen ( 0 , 2 ) ; // 25% chance
std : : uniform_int_distribution < int > rgen ( 0 , 4 ) ; // 25% chance
// Init the grids
unsigned char * node_grid = new unsigned char [ GRID_WIDTH * GRID_HEIGHT ] ;
@ -262,20 +262,10 @@ int main(int argc, char* argv[])
for ( int i = 0 ; i < GRID_WIDTH * GRID_HEIGHT * 4 ; i + = 4 ) {
if ( node_grid [ i / 4 ] = = 1 ) {
pixel_array [ i ] = 0 ; // R?
pixel_array [ i + 1 ] = 0 ; // G?
pixel_array [ i + 2 ] = 0 ; // B?
pixel_array [ i + 3 ] = 0 ; // A?
}
else {
pixel_array [ i ] = 0 ; // R?
pixel_array [ i + 1 ] = 0 ; // G?
pixel_array [ i + 2 ] = 0 ; // B?
pixel_array [ i + 3 ] = 0 ; // A?
}
pixel_array [ i ] = i % 255 ; // R?
pixel_array [ i + 1 ] = 70 ; // G?
pixel_array [ i + 2 ] = 100 ; // B?
pixel_array [ i + 3 ] = 100 ; // A?
}
GLuint texture ;
@ -312,9 +302,9 @@ int main(int argc, char* argv[])
int err = 0 ;
cl_mem frontWriteBuffer = clCreateFromGLTexture ( context , CL_MEM_ READ_ WRITE, GL_TEXTURE_2D , 0 , texture , & err ) ;
//cl_mem frontReadBuffer = clCreateFromGLTexture(context, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, texture, &err);
cl_mem backBuffer = clCreateBuffer ( context , CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR , GRID_WIDTH * GRID_HEIGHT * sizeof ( char ) , ( void * ) node_grid , & err ) ;
cl_mem frontWriteBuffer = clCreateFromGLTexture ( context , CL_MEM_ WRITE_ONLY , GL_TEXTURE_2D , 0 , texture , & err ) ;
cl_mem frontReadBuffer = clCreateFromGLTexture ( context , CL_MEM_READ_ONLY , GL_TEXTURE_2D , 0 , texture , & err ) ;
cl_mem backBuffer = clCreateBuffer ( context , CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR , sizeof ( int ) , ( void * ) node_grid , & err ) ;
cl_mem workerCountBuffer = clCreateBuffer ( context , CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR , sizeof ( int ) , & WORKER_SIZE , & err ) ;
cl_mem gridWidthBuffer = clCreateBuffer ( context , CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR , sizeof ( int ) , & GRID_WIDTH , & err ) ;
@ -327,7 +317,7 @@ int main(int argc, char* argv[])
status = clSetKernelArg ( compute_kernel , 3 , sizeof ( cl_mem ) , ( void * ) & gridWidthBuffer ) ;
status = clSetKernelArg ( compute_kernel , 4 , sizeof ( cl_mem ) , ( void * ) & gridHeightBuffer ) ;
status = clSetKernelArg ( align_kernel , 0 , sizeof ( cl_mem ) , ( void * ) & front Write Buffer) ;
status = clSetKernelArg ( align_kernel , 0 , sizeof ( cl_mem ) , ( void * ) & front Read Buffer) ;
status = clSetKernelArg ( align_kernel , 1 , sizeof ( cl_mem ) , ( void * ) & backBuffer ) ;
status = clSetKernelArg ( align_kernel , 2 , sizeof ( cl_mem ) , ( void * ) & workerCountBuffer ) ;
status = clSetKernelArg ( align_kernel , 3 , sizeof ( cl_mem ) , ( void * ) & gridWidthBuffer ) ;
@ -335,31 +325,27 @@ int main(int argc, char* argv[])
// ===================================== Loop ==================================================================
int i = 0 ;
while ( ! glfwWindowShouldClose ( gl_window ) ) {
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
//glClear(GL_COLOR_BUFFER_BIT);
glClearColor ( 0.2f , 0.3f , 0.3f , 1.0f ) ;
glClear ( GL_COLOR_BUFFER_BIT ) ;
// ======================================= OpenCL Shtuff ===================================================
// Work size, for each y line
size_t global_work_size [ 1 ] = { WORKER_SIZE } ;
glFinish( ) ;
status = clEnqueueAcquireGLObjects ( commandQueue , 1 , & frontReadBuffer , 0 , 0 , 0 ) ;
status = clEnqueueAcquireGLObjects ( commandQueue , 1 , & frontWriteBuffer , 0 , 0 , 0 ) ;
status = clEnqueueNDRangeKernel ( commandQueue , compute_kernel , 1 , NULL , global_work_size , NULL , 0 , NULL , NULL ) ;
status = clEnqueueNDRangeKernel ( commandQueue , align_kernel , 1 , NULL , global_work_size , NULL , 0 , NULL , NULL ) ;
//clEnqueueBarrier(commandQueue);
//status = clEnqueueNDRangeKernel(commandQueue, align_kernel, 1, NULL, global_work_size, NULL, 0, NULL, NULL);
status = clEnqueueReleaseGLObjects ( commandQueue , 1 , & frontReadBuffer , 0 , NULL , NULL ) ;
status = clEnqueueReleaseGLObjects ( commandQueue , 1 , & frontWriteBuffer , 0 , NULL , NULL ) ;
//clEnqueueBarrier(commandQueue);
clFinish ( commandQueue ) ;
// ======================================= Rendering Shtuff =================================================
@ -375,17 +361,19 @@ int main(int argc, char* argv[])
glDrawElements ( GL_TRIANGLES , 6 , GL_UNSIGNED_INT , 0 ) ;
glBindVertexArray ( 0 ) ;
glFinish ( ) ;
// Render
glfwSwapBuffers ( gl_window ) ;
}
glfwTerminate ( ) ;
// Release the buffers
// status = clReleaseMemObject(frontReadBuffer);
status = clReleaseMemObject ( frontReadBuffer ) ;
status = clReleaseMemObject ( workerCountBuffer ) ;
status = clReleaseMemObject ( gridWidthBuffer ) ;
status = clReleaseMemObject ( gridHeightBuffer ) ;