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"
|
2009-10-21 21:53:47 -06:00
|
|
|
#include "Timer.h"
|
2009-09-30 15:01:34 -06:00
|
|
|
|
2009-10-21 21:53:47 -06:00
|
|
|
namespace OpenCL
|
|
|
|
{
|
|
|
|
|
|
|
|
#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-10-21 21:53:47 -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-12-22 18:31:42 -07:00
|
|
|
// Connect to a compute device
|
|
|
|
cl_uint numPlatforms;
|
|
|
|
cl_platform_id platform = NULL;
|
|
|
|
err = clGetPlatformIDs(0, NULL, &numPlatforms);
|
|
|
|
|
|
|
|
if (err != CL_SUCCESS)
|
|
|
|
{
|
|
|
|
HandleCLError(err, "clGetPlatformIDs failed.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 < numPlatforms)
|
|
|
|
{
|
|
|
|
cl_platform_id* platforms = new cl_platform_id[numPlatforms];
|
|
|
|
err = clGetPlatformIDs(numPlatforms, platforms, NULL);
|
|
|
|
|
|
|
|
if (err != CL_SUCCESS)
|
|
|
|
{
|
|
|
|
HandleCLError(err, "clGetPlatformIDs failed.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char pbuf[100];
|
|
|
|
err = clGetPlatformInfo(platforms[0], CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL);
|
|
|
|
|
|
|
|
if (err != CL_SUCCESS)
|
|
|
|
{
|
|
|
|
HandleCLError(err, "clGetPlatformInfo failed.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
platform = platforms[0];
|
|
|
|
delete[] platforms;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PanicAlert("No OpenCL platform found.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cl_context_properties cps[3] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0};
|
|
|
|
|
|
|
|
cl_context_properties* cprops = (NULL == platform) ? NULL : cps;
|
|
|
|
|
|
|
|
int gpu = 1; // I think we should use CL_DEVICE_TYPE_ALL
|
|
|
|
|
|
|
|
err = clGetDeviceIDs(platform, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
|
|
|
|
if (err != CL_SUCCESS)
|
|
|
|
{
|
|
|
|
HandleCLError(err, "Failed to create a device group!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a compute context
|
|
|
|
g_context = clCreateContext(cprops, 1, &device_id, NULL, NULL, &err);
|
|
|
|
if (!g_context)
|
|
|
|
{
|
|
|
|
HandleCLError(err, "Failed to create a compute context!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a command commands
|
|
|
|
g_cmdq = clCreateCommandQueue(g_context, device_id, 0, &err);
|
|
|
|
if (!g_cmdq)
|
|
|
|
{
|
|
|
|
HandleCLError(err, "Failed to create a command commands!");
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-21 21:53:47 -06:00
|
|
|
|
|
|
|
NOTICE_LOG(COMMON, "Initialized OpenCL!");
|
2009-10-07 15:30:36 -06:00
|
|
|
g_bInitialized = true;
|
2009-09-30 15:01:34 -06:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2009-10-21 21:53:47 -06:00
|
|
|
|
2009-10-06 20:48:21 -06:00
|
|
|
#if defined(HAVE_OPENCL) && HAVE_OPENCL
|
2009-10-21 21:53:47 -06:00
|
|
|
cl_context GetContext()
|
|
|
|
{
|
2009-09-30 15:01:34 -06:00
|
|
|
return g_context;
|
|
|
|
}
|
|
|
|
|
2009-10-21 21:53:47 -06:00
|
|
|
cl_command_queue GetCommandQueue()
|
|
|
|
{
|
2009-09-30 15:01:34 -06:00
|
|
|
return g_cmdq;
|
|
|
|
}
|
|
|
|
|
2009-10-21 21:53:47 -06:00
|
|
|
cl_program CompileProgram(const char *Kernel)
|
|
|
|
{
|
|
|
|
u32 compileStart = timeGetTime();
|
|
|
|
int err;
|
|
|
|
cl_program program;
|
|
|
|
program = clCreateProgramWithSource(OpenCL::g_context, 1, (const char **) & Kernel, NULL, &err);
|
|
|
|
if (!program)
|
|
|
|
{
|
|
|
|
HandleCLError(err, "Error: Failed to create compute program!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the program executable
|
|
|
|
err = clBuildProgram(program , 0, NULL, NULL, NULL, NULL);
|
|
|
|
if(err != CL_SUCCESS) {
|
|
|
|
char *errors[16384] = {0};
|
|
|
|
err = clGetProgramBuildInfo(program, OpenCL::device_id, CL_PROGRAM_BUILD_LOG, sizeof(errors),
|
|
|
|
errors, NULL);
|
2009-12-22 18:31:42 -07:00
|
|
|
ERROR_LOG(COMMON, "Error log:\n%s\n", errors);
|
2009-10-21 21:53:47 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
NOTICE_LOG(COMMON, "OpenCL CompileProgram took %.3f seconds", (float)(timeGetTime() - compileStart) / 1000.0);
|
|
|
|
return program;
|
2009-09-30 15:01:34 -06:00
|
|
|
}
|
2009-10-21 21:53:47 -06:00
|
|
|
|
2009-10-06 20:48:21 -06:00
|
|
|
cl_kernel CompileKernel(cl_program program, const char *Function)
|
|
|
|
{
|
2009-10-21 21:53:47 -06:00
|
|
|
u32 compileStart = timeGetTime();
|
|
|
|
int err;
|
2009-12-22 18:31:42 -07:00
|
|
|
|
2009-10-21 21:53:47 -06:00
|
|
|
// Create the compute kernel in the program we wish to run
|
|
|
|
cl_kernel kernel = clCreateKernel(program, Function, &err);
|
|
|
|
if (!kernel || err != CL_SUCCESS)
|
|
|
|
{
|
|
|
|
HandleCLError(err, "Failed to create compute kernel!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
NOTICE_LOG(COMMON, "OpenCL CompileKernel took %.3f seconds", (float)(timeGetTime() - compileStart) / 1000.0);
|
|
|
|
return kernel;
|
2009-10-06 20:48:21 -06:00
|
|
|
}
|
|
|
|
#endif
|
2009-09-30 15:01:34 -06:00
|
|
|
|
2009-10-21 21:53:47 -06:00
|
|
|
void Destroy()
|
|
|
|
{
|
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;
|
2009-09-30 15:01:34 -06:00
|
|
|
clReleaseCommandQueue(g_cmdq);
|
|
|
|
clReleaseContext(g_context);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-10-19 18:55:07 -06:00
|
|
|
void HandleCLError(cl_int error, char* str)
|
|
|
|
{
|
2009-10-21 21:53:47 -06:00
|
|
|
#if defined(HAVE_OPENCL) && HAVE_OPENCL
|
|
|
|
|
2009-12-22 18:31:42 -07:00
|
|
|
char* name;
|
|
|
|
switch(error)
|
|
|
|
{
|
2009-10-19 18:55:07 -06:00
|
|
|
#define CL_ERROR(x) case (x): name = #x; break
|
2009-12-22 18:31:42 -07:00
|
|
|
CL_ERROR(CL_SUCCESS);
|
|
|
|
CL_ERROR(CL_DEVICE_NOT_FOUND);
|
|
|
|
CL_ERROR(CL_DEVICE_NOT_AVAILABLE);
|
|
|
|
CL_ERROR(CL_COMPILER_NOT_AVAILABLE);
|
|
|
|
CL_ERROR(CL_MEM_OBJECT_ALLOCATION_FAILURE);
|
|
|
|
CL_ERROR(CL_OUT_OF_RESOURCES);
|
|
|
|
CL_ERROR(CL_OUT_OF_HOST_MEMORY);
|
|
|
|
CL_ERROR(CL_PROFILING_INFO_NOT_AVAILABLE);
|
|
|
|
CL_ERROR(CL_MEM_COPY_OVERLAP);
|
|
|
|
CL_ERROR(CL_IMAGE_FORMAT_MISMATCH);
|
|
|
|
CL_ERROR(CL_IMAGE_FORMAT_NOT_SUPPORTED);
|
|
|
|
CL_ERROR(CL_BUILD_PROGRAM_FAILURE);
|
|
|
|
CL_ERROR(CL_MAP_FAILURE);
|
|
|
|
CL_ERROR(CL_INVALID_VALUE);
|
|
|
|
CL_ERROR(CL_INVALID_DEVICE_TYPE);
|
|
|
|
CL_ERROR(CL_INVALID_PLATFORM);
|
|
|
|
CL_ERROR(CL_INVALID_DEVICE);
|
|
|
|
CL_ERROR(CL_INVALID_CONTEXT);
|
|
|
|
CL_ERROR(CL_INVALID_QUEUE_PROPERTIES);
|
|
|
|
CL_ERROR(CL_INVALID_COMMAND_QUEUE);
|
|
|
|
CL_ERROR(CL_INVALID_HOST_PTR);
|
|
|
|
CL_ERROR(CL_INVALID_MEM_OBJECT);
|
|
|
|
CL_ERROR(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR);
|
|
|
|
CL_ERROR(CL_INVALID_IMAGE_SIZE);
|
|
|
|
CL_ERROR(CL_INVALID_SAMPLER);
|
|
|
|
CL_ERROR(CL_INVALID_BINARY);
|
|
|
|
CL_ERROR(CL_INVALID_BUILD_OPTIONS);
|
|
|
|
CL_ERROR(CL_INVALID_PROGRAM);
|
|
|
|
CL_ERROR(CL_INVALID_PROGRAM_EXECUTABLE);
|
|
|
|
CL_ERROR(CL_INVALID_KERNEL_NAME);
|
|
|
|
CL_ERROR(CL_INVALID_KERNEL_DEFINITION);
|
|
|
|
CL_ERROR(CL_INVALID_KERNEL);
|
|
|
|
CL_ERROR(CL_INVALID_ARG_INDEX);
|
|
|
|
CL_ERROR(CL_INVALID_ARG_VALUE);
|
|
|
|
CL_ERROR(CL_INVALID_ARG_SIZE);
|
|
|
|
CL_ERROR(CL_INVALID_KERNEL_ARGS);
|
|
|
|
CL_ERROR(CL_INVALID_WORK_DIMENSION);
|
|
|
|
CL_ERROR(CL_INVALID_WORK_GROUP_SIZE);
|
|
|
|
CL_ERROR(CL_INVALID_WORK_ITEM_SIZE);
|
|
|
|
CL_ERROR(CL_INVALID_GLOBAL_OFFSET);
|
|
|
|
CL_ERROR(CL_INVALID_EVENT_WAIT_LIST);
|
|
|
|
CL_ERROR(CL_INVALID_EVENT);
|
|
|
|
CL_ERROR(CL_INVALID_OPERATION);
|
|
|
|
CL_ERROR(CL_INVALID_GL_OBJECT);
|
|
|
|
CL_ERROR(CL_INVALID_BUFFER_SIZE);
|
|
|
|
CL_ERROR(CL_INVALID_MIP_LEVEL);
|
2009-10-19 18:55:07 -06:00
|
|
|
#undef CL_ERROR
|
2009-12-22 18:31:42 -07:00
|
|
|
default:
|
|
|
|
name = "Unknown error code";
|
|
|
|
}
|
|
|
|
if(!str)
|
|
|
|
str = "";
|
|
|
|
ERROR_LOG(COMMON, "OpenCL error: %s %s (%d)", str, name, error);
|
2009-10-19 20:11:50 -06:00
|
|
|
#endif
|
2009-12-22 18:31:42 -07:00
|
|
|
}
|
2009-10-21 21:53:47 -06:00
|
|
|
}
|