2009-09-30 15:01:34 -06:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
// TODO: Make a more centralized version of this (for now every plugin that will use it will create its own context, which is weird). An object maybe?
|
|
|
|
|
|
|
|
#include "OpenCL.h"
|
|
|
|
#include "Common.h"
|
|
|
|
|
|
|
|
namespace OpenCL {
|
2009-10-06 20:48:21 -06:00
|
|
|
#if defined(HAVE_OPENCL) && HAVE_OPENCL
|
|
|
|
cl_device_id device_id = NULL;
|
|
|
|
cl_context g_context = NULL;
|
|
|
|
cl_command_queue g_cmdq = NULL;
|
|
|
|
#endif
|
2009-09-30 15:01:34 -06:00
|
|
|
|
2009-10-07 15:30:36 -06:00
|
|
|
bool g_bInitialized = false;
|
|
|
|
|
2009-09-30 15:01:34 -06:00
|
|
|
bool Initialize() {
|
2009-10-07 15:30:36 -06:00
|
|
|
if(g_bInitialized)
|
|
|
|
return true;
|
|
|
|
|
2009-09-30 15:01:34 -06:00
|
|
|
#if defined(HAVE_OPENCL) && HAVE_OPENCL
|
2009-10-06 20:48:21 -06:00
|
|
|
if(g_context)
|
|
|
|
return false;
|
2009-09-30 15:01:34 -06:00
|
|
|
int err; // error code returned from api calls
|
2009-10-06 20:48:21 -06:00
|
|
|
|
2009-09-30 15:01:34 -06:00
|
|
|
|
|
|
|
// Connect to a compute device
|
|
|
|
//
|
|
|
|
int gpu = 1; // I think we should use CL_DEVICE_TYPE_ALL
|
|
|
|
err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
|
|
|
|
if (err != CL_SUCCESS)
|
|
|
|
{
|
2009-10-07 15:30:36 -06:00
|
|
|
PanicAlert("Error: Failed to create a device group!");
|
2009-09-30 15:01:34 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a compute context
|
|
|
|
//
|
|
|
|
g_context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
|
|
|
|
if (!g_context)
|
|
|
|
{
|
2009-10-07 15:30:36 -06:00
|
|
|
PanicAlert("Error: Failed to create a compute context!");
|
2009-09-30 15:01:34 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a command commands
|
|
|
|
//
|
|
|
|
g_cmdq = clCreateCommandQueue(g_context, device_id, 0, &err);
|
|
|
|
if (!g_cmdq)
|
|
|
|
{
|
2009-10-07 15:30:36 -06:00
|
|
|
PanicAlert("Error: Failed to create a command commands!");
|
2009-09-30 15:01:34 -06:00
|
|
|
return false;
|
|
|
|
}
|
2009-10-07 15:30:36 -06:00
|
|
|
//PanicAlert("Initialized OpenCL fine!");
|
|
|
|
g_bInitialized = true;
|
2009-09-30 15:01:34 -06:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2009-10-06 20:48:21 -06:00
|
|
|
#if defined(HAVE_OPENCL) && HAVE_OPENCL
|
2009-10-07 15:30:36 -06:00
|
|
|
cl_context GetContext() {
|
2009-09-30 15:01:34 -06:00
|
|
|
return g_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
cl_command_queue GetCommandQueue() {
|
|
|
|
return g_cmdq;
|
|
|
|
}
|
|
|
|
|
2009-10-06 20:48:21 -06:00
|
|
|
cl_program CompileProgram(const char *Kernel) {
|
|
|
|
int err;
|
|
|
|
cl_program program;
|
|
|
|
program = clCreateProgramWithSource(OpenCL::g_context, 1, (const char **) & Kernel, NULL, &err);
|
|
|
|
if (!program)
|
|
|
|
{
|
2009-10-07 15:30:36 -06:00
|
|
|
printf("Error: Failed to create compute program!");
|
2009-10-06 20:48:21 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the program executable
|
|
|
|
//
|
|
|
|
err = clBuildProgram(program , 0, NULL, NULL, NULL, NULL);
|
2009-10-07 15:30:36 -06:00
|
|
|
if(err != CL_SUCCESS) {
|
|
|
|
char *errors[16384] = {0};
|
|
|
|
err = clGetProgramBuildInfo(program, OpenCL::device_id, CL_PROGRAM_BUILD_LOG, sizeof(errors),
|
|
|
|
errors, NULL);
|
|
|
|
PanicAlert("Error log:\n%s\n", errors);
|
2009-10-06 20:48:21 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return program;
|
2009-09-30 15:01:34 -06:00
|
|
|
}
|
2009-10-06 20:48:21 -06:00
|
|
|
cl_kernel CompileKernel(cl_program program, const char *Function)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
// Create the compute kernel in the program we wish to run
|
|
|
|
//
|
|
|
|
cl_kernel kernel = clCreateKernel(program, Function, &err);
|
|
|
|
if (!kernel || err != CL_SUCCESS)
|
|
|
|
{
|
2009-10-07 15:30:36 -06:00
|
|
|
PanicAlert("Error: Failed to create compute kernel!");
|
2009-10-06 20:48:21 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return kernel;
|
|
|
|
}
|
|
|
|
#endif
|
2009-09-30 15:01:34 -06:00
|
|
|
void Destroy() {
|
|
|
|
|
|
|
|
#if defined(HAVE_OPENCL) && HAVE_OPENCL
|
2009-10-06 20:48:21 -06:00
|
|
|
if(!g_context)
|
|
|
|
return;
|
2009-09-30 15:01:34 -06:00
|
|
|
clReleaseCommandQueue(g_cmdq);
|
|
|
|
clReleaseContext(g_context);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-06 20:48:21 -06:00
|
|
|
};
|