You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
279 lines
8.2 KiB
279 lines
8.2 KiB
9 years ago
|
#include <CL/cl.h>
|
||
|
#include <string.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <iostream>
|
||
|
#include <string>
|
||
|
#include <fstream>
|
||
|
#include <random>
|
||
|
#include <ctime>
|
||
|
#include <SFML/Graphics.hpp>
|
||
9 years ago
|
#include <windows.h>
|
||
9 years ago
|
|
||
|
#define SUCCESS 0
|
||
|
#define FAILURE 1
|
||
|
|
||
9 years ago
|
float elap_time() {
|
||
|
static __int64 start = 0;
|
||
|
static __int64 frequency = 0;
|
||
|
|
||
|
if (start == 0) {
|
||
|
QueryPerformanceCounter((LARGE_INTEGER*)&start);
|
||
|
QueryPerformanceFrequency((LARGE_INTEGER*)&frequency);
|
||
|
return 0.0f;
|
||
|
}
|
||
|
|
||
|
__int64 counter = 0;
|
||
|
QueryPerformanceCounter((LARGE_INTEGER*)&counter);
|
||
|
return (float)((counter - start) / double(frequency));
|
||
|
}
|
||
9 years ago
|
|
||
|
/* convert the kernel file into a string */
|
||
|
int convertToString(const char *filename, std::string& s)
|
||
|
{
|
||
|
size_t size;
|
||
|
char* str;
|
||
|
std::fstream f(filename, (std::fstream::in | std::fstream::binary));
|
||
|
|
||
|
if(f.is_open())
|
||
|
{
|
||
|
size_t fileSize;
|
||
|
f.seekg(0, std::fstream::end);
|
||
|
size = fileSize = (size_t)f.tellg();
|
||
|
f.seekg(0, std::fstream::beg);
|
||
|
str = new char[size+1];
|
||
|
if(!str)
|
||
|
{
|
||
|
f.close();
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
f.read(str, fileSize);
|
||
|
f.close();
|
||
|
str[size] = '\0';
|
||
|
s = str;
|
||
|
delete[] str;
|
||
|
return 0;
|
||
|
}
|
||
9 years ago
|
std::cout << "Error: failed to open file\n:" << filename << std::endl;
|
||
9 years ago
|
return FAILURE;
|
||
|
}
|
||
|
|
||
|
int main(int argc, char* argv[])
|
||
|
{
|
||
9 years ago
|
int WINDOW_X = 1000;
|
||
|
int WINDOW_Y = 1000;
|
||
|
int GRID_WIDTH = 1000;
|
||
|
int GRID_HEIGHT = 1000;
|
||
9 years ago
|
int WORKER_SIZE = 2000;
|
||
9 years ago
|
|
||
9 years ago
|
// ============================== OpenCL Setup ==================================================================
|
||
9 years ago
|
|
||
9 years ago
|
// Get the platforms
|
||
|
cl_uint numPlatforms;
|
||
|
cl_platform_id platform = NULL;
|
||
|
cl_int status = clGetPlatformIDs(0, NULL, &numPlatforms); // Retrieve the number of platforms
|
||
9 years ago
|
if (status != CL_SUCCESS) {
|
||
|
std::cout << "Error: Getting platforms!" << std::endl;
|
||
9 years ago
|
return FAILURE;
|
||
|
}
|
||
|
|
||
9 years ago
|
// Choose the first available platform
|
||
9 years ago
|
if(numPlatforms > 0) {
|
||
|
cl_platform_id* platforms = new cl_platform_id[numPlatforms];
|
||
|
status = clGetPlatformIDs(numPlatforms, platforms, NULL); // Now populate the array with the platforms
|
||
9 years ago
|
platform = platforms[0];
|
||
9 years ago
|
delete platforms;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
|
||
|
cl_uint numDevices = 0;
|
||
|
cl_device_id *devices;
|
||
9 years ago
|
status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
|
||
9 years ago
|
if (numDevices == 0) { //no GPU available.
|
||
|
std::cout << "No GPU device available." << std::endl;
|
||
|
std::cout << "Choose CPU as default device." << std::endl;
|
||
9 years ago
|
status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 0, NULL, &numDevices);
|
||
9 years ago
|
devices = new cl_device_id[numDevices];
|
||
9 years ago
|
status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, numDevices, devices, NULL);
|
||
|
}
|
||
9 years ago
|
else {
|
||
9 years ago
|
devices = new cl_device_id[numDevices];
|
||
9 years ago
|
status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
|
||
|
}
|
||
|
|
||
|
cl_context context = clCreateContext(NULL,1, devices,NULL,NULL,NULL);
|
||
|
cl_command_queue commandQueue = clCreateCommandQueue(context, devices[0], 0, NULL);
|
||
|
|
||
9 years ago
|
|
||
|
// ============================== Kernel Compilation, Setup ====================================================
|
||
9 years ago
|
|
||
|
// Read the kernel from the file to a string
|
||
|
const char *filename = "conway_kernel.cl";
|
||
9 years ago
|
std::string sourceStr;
|
||
9 years ago
|
status = convertToString(filename, sourceStr);
|
||
9 years ago
|
|
||
|
// Create a program with the source
|
||
9 years ago
|
const char *source = sourceStr.c_str();
|
||
|
size_t sourceSize[] = {strlen(source)};
|
||
|
cl_program program = clCreateProgramWithSource(context, 1, &source, sourceSize, NULL);
|
||
|
|
||
9 years ago
|
// Build the program
|
||
|
status = clBuildProgram(program, 1,devices,NULL,NULL,NULL);
|
||
9 years ago
|
|
||
9 years ago
|
// If the build failed
|
||
9 years ago
|
if (status == CL_BUILD_PROGRAM_FAILURE) {
|
||
9 years ago
|
|
||
9 years ago
|
// Determine the size of the log
|
||
|
size_t log_size;
|
||
|
clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
|
||
9 years ago
|
|
||
9 years ago
|
// Allocate memory for the log
|
||
9 years ago
|
char *log = new char[log_size];
|
||
9 years ago
|
|
||
9 years ago
|
// Get the log
|
||
|
clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
|
||
9 years ago
|
|
||
9 years ago
|
// Print the log
|
||
9 years ago
|
std::cout << log << std::endl;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Now create the kernel
|
||
|
cl_kernel kernel = clCreateKernel(program, "conway", NULL);
|
||
9 years ago
|
|
||
|
// ======================================= Setup grid =========================================================
|
||
|
|
||
|
// Setup the rng
|
||
|
std::mt19937 rng(time(NULL));
|
||
9 years ago
|
std::uniform_int_distribution<int> rgen(0, 12); // 25% chance
|
||
9 years ago
|
|
||
9 years ago
|
// Init the grids
|
||
|
unsigned char* front_grid = new unsigned char[GRID_WIDTH * GRID_HEIGHT* 2];
|
||
9 years ago
|
|
||
9 years ago
|
for (int i = 0; i < 1000 * 1000; i += 2) {
|
||
9 years ago
|
if (rgen(rng) == 1) {
|
||
9 years ago
|
front_grid[i] = 1;
|
||
9 years ago
|
}
|
||
|
else {
|
||
9 years ago
|
front_grid[i] = 0;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
unsigned char* rear_grid = new unsigned char[GRID_WIDTH * GRID_HEIGHT * 2];
|
||
|
|
||
|
for (int i = 0; i < GRID_WIDTH * GRID_HEIGHT; i++) {
|
||
|
rear_grid[i] = front_grid[i];
|
||
|
}
|
||
|
|
||
9 years ago
|
// ====================================== Setup SFML ==========================================================
|
||
9 years ago
|
|
||
9 years ago
|
sf::Uint8* asdf = rear_grid;
|
||
|
|
||
|
sf::Uint8* pixel_array = new sf::Uint8[WINDOW_X * WINDOW_Y * 4];
|
||
|
|
||
|
for (int i = 0; i < GRID_WIDTH * GRID_HEIGHT * 2; i += 2) {
|
||
|
|
||
|
int p = i / 2;
|
||
|
|
||
|
pixel_array[p * 4] = 49; // R?
|
||
|
pixel_array[p * 4 + 1] = 68; // G?
|
||
|
pixel_array[p * 4 + 2] = 72; // B?
|
||
|
pixel_array[p * 4 + 3] = 255; // A?
|
||
|
}
|
||
|
|
||
|
char* arr = new char[1000 * 1000];
|
||
|
|
||
9 years ago
|
|
||
|
// Init window, and loop data
|
||
9 years ago
|
sf::RenderWindow window(sf::VideoMode(GRID_WIDTH, GRID_HEIGHT), "Classic Games");
|
||
9 years ago
|
|
||
|
float step_size = 0.0005f;
|
||
|
double frame_time = 0.0, elapsed_time = 0.0, delta_time = 0.0, accumulator_time = 0.0, current_time = 0.0;
|
||
|
int frame_count = 0;
|
||
|
|
||
|
|
||
9 years ago
|
int err = 0;
|
||
9 years ago
|
cl_mem frontBuffer = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, GRID_WIDTH * GRID_HEIGHT * sizeof(char), (void*)front_grid, &err);
|
||
|
cl_mem rearBuffer = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, GRID_WIDTH * GRID_HEIGHT * sizeof(char), (void*)rear_grid, &err);
|
||
|
|
||
9 years ago
|
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);
|
||
|
cl_mem gridHeightBuffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int), &GRID_HEIGHT, &err);
|
||
9 years ago
|
|
||
9 years ago
|
status = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&frontBuffer);
|
||
9 years ago
|
status = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&workerCountBuffer);
|
||
|
status = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&gridWidthBuffer);
|
||
|
status = clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *)&gridHeightBuffer);
|
||
9 years ago
|
|
||
9 years ago
|
|
||
9 years ago
|
sf::Texture texture;
|
||
|
texture.create(WINDOW_X, WINDOW_Y);
|
||
9 years ago
|
sf::Sprite sprite(texture);
|
||
|
|
||
9 years ago
|
|
||
9 years ago
|
// ===================================== Loop ==================================================================
|
||
9 years ago
|
while (window.isOpen()) {
|
||
|
|
||
|
sf::Event event;
|
||
|
while (window.pollEvent(event)) {
|
||
|
if (event.type == sf::Event::Closed)
|
||
|
window.close();
|
||
|
}
|
||
|
|
||
|
// Time keeping
|
||
9 years ago
|
//elapsed_time = elap_time();
|
||
9 years ago
|
delta_time = elapsed_time - current_time;
|
||
|
current_time = elapsed_time;
|
||
|
if (delta_time > 0.02f)
|
||
|
delta_time = 0.02f;
|
||
|
accumulator_time += delta_time;
|
||
|
|
||
|
while ((accumulator_time - step_size) >= step_size) {
|
||
|
accumulator_time -= step_size;
|
||
|
// Do nothing, FPS tied update()
|
||
|
}
|
||
|
|
||
9 years ago
|
// ======================================= OpenCL Shtuff =============================================
|
||
9 years ago
|
|
||
9 years ago
|
// Update the data in GPU memory
|
||
9 years ago
|
//status = clEnqueueWriteBuffer(commandQueue, frontBuffer, CL_TRUE, 0, GRID_WIDTH * GRID_HEIGHT * 2 * sizeof(char), (void*)grid, NULL, 0, NULL);
|
||
9 years ago
|
|
||
9 years ago
|
// Work size, for each y line
|
||
9 years ago
|
size_t global_work_size[1] = { WORKER_SIZE };
|
||
9 years ago
|
|
||
9 years ago
|
// Run the kernel
|
||
|
status = clEnqueueNDRangeKernel(commandQueue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, NULL);
|
||
9 years ago
|
|
||
9 years ago
|
// Get output, put back into grid
|
||
9 years ago
|
status = clEnqueueReadBuffer(commandQueue, frontBuffer, CL_TRUE, 0, GRID_WIDTH * GRID_HEIGHT * sizeof(char), (void*)rear_grid, 0, NULL, NULL);
|
||
9 years ago
|
|
||
9 years ago
|
|
||
9 years ago
|
texture.update(pixel_array);
|
||
|
window.draw(sprite);
|
||
|
|
||
9 years ago
|
frame_count++;
|
||
|
window.display();
|
||
|
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
|
||
9 years ago
|
// Temporary
|
||
9 years ago
|
status = clReleaseMemObject(frontBuffer);
|
||
9 years ago
|
status = clReleaseMemObject(workerCountBuffer);
|
||
|
status = clReleaseMemObject(gridWidthBuffer);
|
||
|
status = clReleaseMemObject(gridHeightBuffer);
|
||
9 years ago
|
|
||
|
/*Step 12: Clean the resources.*/
|
||
|
status = clReleaseKernel(kernel); //Release kernel.
|
||
|
status = clReleaseProgram(program); //Release the program object.
|
||
|
status = clReleaseCommandQueue(commandQueue); //Release Command queue.
|
||
|
status = clReleaseContext(context); //Release context.
|
||
|
|
||
|
if (devices != NULL)
|
||
|
{
|
||
|
free(devices);
|
||
|
devices = NULL;
|
||
|
}
|
||
|
|
||
|
return SUCCESS;
|
||
|
}
|