From c6f0335b624f80ce5fbf2444de494a079e058c48 Mon Sep 17 00:00:00 2001 From: "XTra.KrazzY" Date: Wed, 30 Sep 2009 21:01:34 +0000 Subject: [PATCH] OpenCL: More work on centralization (need SCons reflection) git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4351 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/Common/Common.vcproj | 8 + Source/Core/Common/Src/OpenCL.cpp | 95 +++++++++ Source/Core/Common/Src/OpenCL.h | 57 ++++++ .../Core/VideoCommon/Src/OpcodeDecoding.cpp | 9 + .../VideoCommon/Src/OpenCL/TextureDecoder.cpp | 180 ++++++------------ Source/Core/VideoCommon/VideoCommon.vcproj | 12 ++ 6 files changed, 240 insertions(+), 121 deletions(-) create mode 100644 Source/Core/Common/Src/OpenCL.cpp create mode 100644 Source/Core/Common/Src/OpenCL.h diff --git a/Source/Core/Common/Common.vcproj b/Source/Core/Common/Common.vcproj index 628ca6e250..02e184d576 100644 --- a/Source/Core/Common/Common.vcproj +++ b/Source/Core/Common/Common.vcproj @@ -724,6 +724,14 @@ RelativePath=".\Src\MsgHandler.h" > + + + + diff --git a/Source/Core/Common/Src/OpenCL.cpp b/Source/Core/Common/Src/OpenCL.cpp new file mode 100644 index 0000000000..06f39fb79a --- /dev/null +++ b/Source/Core/Common/Src/OpenCL.cpp @@ -0,0 +1,95 @@ +// 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 { + +cl_context g_context = NULL; +cl_command_queue g_cmdq = NULL; + +bool Initialize() { + if(g_context) + return false; + +#if defined(HAVE_OPENCL) && HAVE_OPENCL + + int err; // error code returned from api calls + cl_device_id device_id; + + // 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) + { + PanicAlert("Error: Failed to create a device group!\n"); + return false; + } + + // Create a compute context + // + g_context = clCreateContext(0, 1, &device_id, NULL, NULL, &err); + if (!g_context) + { + PanicAlert("Error: Failed to create a compute context!\n"); + return false; + } + + // Create a command commands + // + g_cmdq = clCreateCommandQueue(g_context, device_id, 0, &err); + if (!g_cmdq) + { + PanicAlert("Error: Failed to create a command commands!\n"); + return false; + } + + return true; +#else + return false; +#endif +} + +cl_context GetInstance() { + return g_context; +} + +cl_command_queue GetCommandQueue() { + return g_cmdq; +} + +cl_program CompileProgram(const char *program, unsigned int size) { + // TODO + return NULL; +} + +void Destroy() { + if(!g_context) + return; + +#if defined(HAVE_OPENCL) && HAVE_OPENCL + clReleaseCommandQueue(g_cmdq); + clReleaseContext(g_context); +#endif +} + + +}; \ No newline at end of file diff --git a/Source/Core/Common/Src/OpenCL.h b/Source/Core/Common/Src/OpenCL.h new file mode 100644 index 0000000000..8284158d53 --- /dev/null +++ b/Source/Core/Common/Src/OpenCL.h @@ -0,0 +1,57 @@ +// 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/ + +#ifndef __OPENCL_H__ +#define __OPENCL_H__ + +// Change to #if 1 if you want to test OpenCL (and you have it) on Windows +#if 0 +#pragma comment(lib, "OpenCL.lib") +#define HAVE_OPENCL 1 +#endif + +#if defined(HAVE_OPENCL) && HAVE_OPENCL + +#ifdef __APPLE__ +#include +#else +#include +#endif + +#else + +typedef void *cl_context; +typedef void *cl_command_queue; +typedef void *cl_program; + +#endif + +namespace OpenCL { + +bool Initialize(); + +cl_context GetContext(); + +cl_command_queue GetCommandQueue(); + +void Destroy(); + +cl_program CompileProgram(const char *program, unsigned int size); + +}; + +#endif diff --git a/Source/Core/VideoCommon/Src/OpcodeDecoding.cpp b/Source/Core/VideoCommon/Src/OpcodeDecoding.cpp index bacaaa2322..f5d0b41211 100644 --- a/Source/Core/VideoCommon/Src/OpcodeDecoding.cpp +++ b/Source/Core/VideoCommon/Src/OpcodeDecoding.cpp @@ -41,6 +41,8 @@ #include "Fifo.h" #include "DataReader.h" +#include "OpenCL.h" + u8* g_pVideoData = 0; extern u8* FAKE_GetFifoStartPtr(); @@ -377,11 +379,18 @@ static void DecodeSemiNop() void OpcodeDecoder_Init() { g_pVideoData = FAKE_GetFifoStartPtr(); + +#if defined(HAVE_OPENCL) && HAVE_OPENCL + OpenCL::Initialize(); +#endif } void OpcodeDecoder_Shutdown() { +#if defined(HAVE_OPENCL) && HAVE_OPENCL + OpenCL::Destroy(); +#endif } void OpcodeDecoder_Run(bool skipped_frame) diff --git a/Source/Core/VideoCommon/Src/OpenCL/TextureDecoder.cpp b/Source/Core/VideoCommon/Src/OpenCL/TextureDecoder.cpp index a062c10fc4..62d69afdfe 100644 --- a/Source/Core/VideoCommon/Src/OpenCL/TextureDecoder.cpp +++ b/Source/Core/VideoCommon/Src/OpenCL/TextureDecoder.cpp @@ -17,11 +17,7 @@ #include "TextureDecoder.h" -#ifdef __APPLE__ -#include -#else -#include -#endif +#include "OpenCL.h" #include #include @@ -32,12 +28,8 @@ #include #include - size_t global; // global domain size for our calculation - size_t local; // local domain size for our calculation - - cl_device_id device_id; // compute device id - cl_context context; // compute context - cl_command_queue commands; // compute command queue +size_t global; // global domain size for our calculation +size_t local; // local domain size for our calculation struct sDecoders @@ -46,105 +38,71 @@ struct sDecoders cl_kernel kernel; // compute kernel const char **cKernel; }; -const char *Kernel = " \ -__kernel void Decode(__local unsigned char *dst, \ - __local const unsigned char *src, \ - int width, int height) \ -{ \ - int id = get_global_id(0); \ - for (int xy = 0; xy < height*width; xy += 4) \ - for (int iy = 0; iy < 4; iy++, src += 8) { \ - u16 *ptr = (u16 *)dst + ((xy / width) + iy) * \ - width + (xy % width); \ - u16 *s = (u16 *)src; \ - for(int j = 0; j < 4; j++) \ - *ptr++ = Common::swap16(*s++); \ - } + +const char *Kernel = " \ +kernel void Decode(global uchar *dst, \ + const global uchar *src, \ + int width, int height) \ +{ \ + int x = get_global_id(0), y = get_global_id(1); \ + \ + for (int iy = 0; iy < 4; iy++, src += 8) { \ + u16 *ptr = (u16 *)dst + ((x + (y / width)) + iy) * \ + width + ((x * width + y) % width); \ + u16 *s = (u16 *)src; \ + for(int j = 0; j < 4; j++) \ + *ptr++ = Common::swap16(*s++); \ + } \ }"; sDecoders Decoders[] = { {NULL, NULL, &Kernel}, -bool Inited = false; - -// TODO: Deinit (clRelease...) - -bool Init_OpenCL() -{ - int err; // error code returned from api calls - - // Connect to a compute device - // - int gpu = 1; - err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 1, &device_id, NULL); - if (err != CL_SUCCESS) - { - printf("Error: Failed to create a device group!\n"); - return EXIT_FAILURE; - } - - // Create a compute context - // - context = clCreateContext(0, 1, &device_id, NULL, NULL, &err); - if (!context) - { - printf("Error: Failed to create a compute context!\n"); - return EXIT_FAILURE; - } - - // Create a command commands - // - commands = clCreateCommandQueue(context, device_id, 0, &err); - if (!commands) - { - printf("Error: Failed to create a command commands!\n"); - return EXIT_FAILURE; - } - - // Create the compute program from the source buffer - // - /* - program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err); - if (!program) - { - printf("Error: Failed to create compute program!\n"); - return EXIT_FAILURE; - } - - // Build the program executable - // - err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); - if (err != CL_SUCCESS) - { - size_t len; - char buffer[2048]; - - printf("Error: Failed to build program executable!\n"); - clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len); - printf("%s\n", buffer); - exit(1); - } - - // Create the compute kernel in the program we wish to run - // - kernel = clCreateKernel(program, "Decoder", &err); - if (!kernel || err != CL_SUCCESS) - { - printf("Error: Failed to create compute kernel!\n"); - exit(1); - } */ -} +bool g_Inited = false; PC_TexFormat TexDecoder_Decode_OpenCL(u8 *dst, const u8 *src, int width, int height, int texformat, int tlutaddr, int tlutfmt) { - if(!Inited) + if(!g_Inited) { - // Not yet inited, let's init now - // Need to make a init function later - if(!Init_OpenCL()) - PanicAlert("OpenCL could not initialize successfully"); - } + g_Inited = true; - // clEnqueueNDRangeKernel +#if defined(HAVE_OPENCL) && HAVE_OPENCL + // TODO: Compile the program + // Create the compute program from the source buffer + // + /* + program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err); + if (!program) + { + printf("Error: Failed to create compute program!\n"); + return EXIT_FAILURE; + } + + // Build the program executable + // + err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); + if (err != CL_SUCCESS) + { + size_t len; + char buffer[2048]; + + printf("Error: Failed to build program executable!\n"); + clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len); + printf("%s\n", buffer); + exit(1); + } + + // Create the compute kernel in the program we wish to run + // + kernel = clCreateKernel(program, "Decoder", &err); + if (!kernel || err != CL_SUCCESS) + { + printf("Error: Failed to create compute kernel!\n"); + exit(1); + } */ +#endif + } + + // TODO: clEnqueueNDRangeKernel /*switch (texformat) @@ -279,25 +237,6 @@ PC_TexFormat TexDecoder_Decode_OpenCL(u8 *dst, const u8 *src, int width, int hei case GX_TF_CMPR: // speed critical // The metroid games use this format almost exclusively. { -#if 0 // TODO - currently does not handle transparency correctly and causes problems when texture dimensions are not multiples of 8 - // 11111111 22222222 55555555 66666666 - // 33333333 44444444 77777777 88888888 - for (int y = 0; y < height; y += 8) - { - for (int x = 0; x < width; x += 8) - { - copyDXTBlock(dst+(y/2)*width+x*2, src); - src += 8; - copyDXTBlock(dst+(y/2)*width+x*2+8, src); - src += 8; - copyDXTBlock(dst+(y/2+2)*width+x*2, src); - src += 8; - copyDXTBlock(dst+(y/2+2)*width+x*2+8, src); - src += 8; - } - } - return PC_TEX_FMT_DXT1; -#else for (int y = 0; y < height; y += 8) { for (int x = 0; x < width; x += 8) @@ -312,7 +251,6 @@ PC_TexFormat TexDecoder_Decode_OpenCL(u8 *dst, const u8 *src, int width, int hei src += sizeof(DXTBlock); } } -#endif return PC_TEX_FMT_BGRA32; } } diff --git a/Source/Core/VideoCommon/VideoCommon.vcproj b/Source/Core/VideoCommon/VideoCommon.vcproj index 908b9f3ab2..5ea612c388 100644 --- a/Source/Core/VideoCommon/VideoCommon.vcproj +++ b/Source/Core/VideoCommon/VideoCommon.vcproj @@ -688,6 +688,18 @@ RelativePath=".\Src\TextureDecoder.h" > + + + + + +