2009-10-11 20:03:55 +00: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/
|
|
|
|
|
|
|
|
#include "OCLTextureDecoder.h"
|
|
|
|
|
|
|
|
#include "OpenCL.h"
|
2009-10-20 00:55:07 +00:00
|
|
|
#include "FileUtil.h"
|
2009-10-11 20:03:55 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2009-10-20 00:55:07 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
//#define DEBUG_OPENCL
|
|
|
|
|
2009-10-11 20:03:55 +00:00
|
|
|
struct sDecoders
|
|
|
|
{
|
2009-10-11 22:21:36 +00:00
|
|
|
const char name[256]; // kernel name
|
2009-10-11 20:03:55 +00:00
|
|
|
cl_kernel kernel; // compute kernel
|
|
|
|
};
|
|
|
|
|
2009-10-11 22:21:36 +00:00
|
|
|
cl_program g_program;
|
|
|
|
// NULL terminated set of kernels
|
2009-10-13 21:51:27 +00:00
|
|
|
sDecoders Decoders[] = {
|
|
|
|
{"DecodeI4", NULL},
|
|
|
|
{"DecodeI8", NULL},
|
|
|
|
{"DecodeIA4", NULL},
|
|
|
|
{"DecodeIA8", NULL},
|
2009-10-14 22:28:23 +00:00
|
|
|
{"DecodeRGBA8", NULL},
|
|
|
|
{"DecodeRGB565", NULL},
|
|
|
|
{"DecodeRGB5A3", NULL},
|
2009-10-20 00:55:07 +00:00
|
|
|
{"DecodeCMPR", NULL},
|
2009-10-13 21:51:27 +00:00
|
|
|
{"", NULL},
|
2009-10-11 20:03:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool g_Inited = false;
|
2009-10-14 22:28:23 +00:00
|
|
|
cl_mem g_clsrc, g_cldst; // texture buffer memory objects
|
2009-10-11 20:03:55 +00:00
|
|
|
|
2009-10-20 00:55:07 +00:00
|
|
|
void TexDecoder_OpenCL_Initialize() {
|
|
|
|
#if defined(HAVE_OPENCL) && HAVE_OPENCL
|
2009-10-14 22:28:23 +00:00
|
|
|
if(!g_Inited)
|
2009-10-11 20:03:55 +00:00
|
|
|
{
|
|
|
|
if(!OpenCL::Initialize())
|
2009-10-14 22:28:23 +00:00
|
|
|
return;
|
2009-10-11 20:03:55 +00:00
|
|
|
|
2009-10-20 00:55:07 +00:00
|
|
|
std::string code;
|
2010-02-02 21:56:29 +00:00
|
|
|
char filename[1024];
|
|
|
|
sprintf(filename, "%sOpenCL/TextureDecoder.cl", File::GetUserPath(D_USER_IDX));
|
2009-10-20 00:55:07 +00:00
|
|
|
if (!File::ReadFileToString(true, filename, code))
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Failed to load OpenCL code %s - file is missing?", filename);
|
|
|
|
return;
|
|
|
|
}
|
2009-10-11 20:03:55 +00:00
|
|
|
|
2009-10-20 00:55:07 +00:00
|
|
|
g_program = OpenCL::CompileProgram(code.c_str());
|
2009-10-11 21:14:02 +00:00
|
|
|
|
2009-10-11 22:21:36 +00:00
|
|
|
int i = 0;
|
|
|
|
while(strlen(Decoders[i].name) > 0) {
|
|
|
|
Decoders[i].kernel = OpenCL::CompileKernel(g_program, Decoders[i].name);
|
|
|
|
i++;
|
|
|
|
}
|
2009-10-11 20:03:55 +00:00
|
|
|
|
2009-10-14 22:28:23 +00:00
|
|
|
// Allocating maximal Wii texture size in advance, so that we don't have to allocate/deallocate per texture
|
2009-10-20 00:55:07 +00:00
|
|
|
#ifndef DEBUG_OPENCL
|
2009-10-14 22:28:23 +00:00
|
|
|
g_clsrc = clCreateBuffer(OpenCL::GetContext(), CL_MEM_READ_ONLY , 1024 * 1024 * sizeof(u32), NULL, NULL);
|
|
|
|
g_cldst = clCreateBuffer(OpenCL::GetContext(), CL_MEM_WRITE_ONLY, 1024 * 1024 * sizeof(u32), NULL, NULL);
|
2009-10-20 00:55:07 +00:00
|
|
|
#endif
|
2009-10-14 22:28:23 +00:00
|
|
|
|
2009-10-11 20:03:55 +00:00
|
|
|
g_Inited = true;
|
2009-10-20 00:55:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-10-14 22:28:23 +00:00
|
|
|
void TexDecoder_OpenCL_Shutdown() {
|
2009-10-20 00:55:07 +00:00
|
|
|
#if defined(HAVE_OPENCL) && HAVE_OPENCL && !defined(DEBUG_OPENCL)
|
2010-02-12 17:20:01 +00:00
|
|
|
|
|
|
|
clReleaseProgram(g_program);
|
|
|
|
int i = 0;
|
|
|
|
while(strlen(Decoders[i].name) > 0)
|
|
|
|
{
|
|
|
|
clReleaseKernel(Decoders[i].kernel);
|
|
|
|
i++;
|
|
|
|
}
|
2010-02-13 10:09:54 +00:00
|
|
|
|
2009-10-14 22:28:23 +00:00
|
|
|
if(g_clsrc)
|
|
|
|
clReleaseMemObject(g_clsrc);
|
|
|
|
|
|
|
|
if(g_cldst)
|
|
|
|
clReleaseMemObject(g_cldst);
|
2010-02-13 10:09:54 +00:00
|
|
|
|
|
|
|
g_Inited = false;
|
2009-10-14 22:28:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
PC_TexFormat TexDecoder_Decode_OpenCL(u8 *dst, const u8 *src, int width, int height, int texformat, int tlutaddr, int tlutfmt)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_OPENCL) && HAVE_OPENCL
|
|
|
|
cl_int err;
|
|
|
|
cl_kernel kernelToRun = Decoders[0].kernel;
|
|
|
|
float sizeOfDst = sizeof(u8), sizeOfSrc = sizeof(u8), xSkip, ySkip;
|
|
|
|
PC_TexFormat formatResult;
|
|
|
|
|
2009-10-11 20:03:55 +00:00
|
|
|
switch(texformat)
|
|
|
|
{
|
2009-10-13 21:51:27 +00:00
|
|
|
case GX_TF_I4:
|
|
|
|
kernelToRun = Decoders[0].kernel;
|
|
|
|
sizeOfSrc = sizeof(u8) / 2.0f;
|
|
|
|
sizeOfDst = sizeof(u8);
|
|
|
|
xSkip = 8;
|
|
|
|
ySkip = 8;
|
|
|
|
formatResult = PC_TEX_FMT_I4_AS_I8;
|
|
|
|
break;
|
|
|
|
case GX_TF_I8:
|
|
|
|
kernelToRun = Decoders[1].kernel;
|
|
|
|
sizeOfSrc = sizeOfDst = sizeof(u8);
|
|
|
|
xSkip = 8;
|
|
|
|
ySkip = 4;
|
|
|
|
formatResult = PC_TEX_FMT_I8;
|
|
|
|
break;
|
2009-10-11 20:03:55 +00:00
|
|
|
case GX_TF_IA4:
|
2009-10-13 21:51:27 +00:00
|
|
|
kernelToRun = Decoders[2].kernel;
|
2009-10-11 21:14:02 +00:00
|
|
|
sizeOfSrc = sizeof(u8);
|
2009-10-11 20:03:55 +00:00
|
|
|
sizeOfDst = sizeof(u16);
|
2009-10-11 21:14:02 +00:00
|
|
|
xSkip = 8;
|
|
|
|
ySkip = 4;
|
|
|
|
formatResult = PC_TEX_FMT_IA4_AS_IA8;
|
|
|
|
break;
|
|
|
|
case GX_TF_IA8:
|
2009-10-13 21:51:27 +00:00
|
|
|
kernelToRun = Decoders[3].kernel;
|
2009-10-11 21:14:02 +00:00
|
|
|
sizeOfSrc = sizeOfDst = sizeof(u16);
|
|
|
|
xSkip = 4;
|
|
|
|
ySkip = 4;
|
|
|
|
formatResult = PC_TEX_FMT_IA8;
|
|
|
|
break;
|
2009-10-14 22:28:23 +00:00
|
|
|
case GX_TF_RGBA8:
|
|
|
|
kernelToRun = Decoders[4].kernel;
|
|
|
|
sizeOfSrc = sizeOfDst = sizeof(u32);
|
|
|
|
xSkip = 4;
|
|
|
|
ySkip = 4;
|
|
|
|
formatResult = PC_TEX_FMT_BGRA32;
|
|
|
|
break;
|
|
|
|
case GX_TF_RGB565:
|
|
|
|
kernelToRun = Decoders[5].kernel;
|
|
|
|
sizeOfSrc = sizeOfDst = sizeof(u16);
|
|
|
|
xSkip = 4;
|
|
|
|
ySkip = 4;
|
|
|
|
formatResult = PC_TEX_FMT_RGB565;
|
|
|
|
break;
|
|
|
|
case GX_TF_RGB5A3:
|
|
|
|
kernelToRun = Decoders[6].kernel;
|
|
|
|
sizeOfSrc = sizeof(u16);
|
|
|
|
sizeOfDst = sizeof(u32);
|
|
|
|
xSkip = 4;
|
|
|
|
ySkip = 4;
|
|
|
|
formatResult = PC_TEX_FMT_BGRA32;
|
|
|
|
break;
|
2009-10-11 22:21:36 +00:00
|
|
|
case GX_TF_CMPR:
|
2010-02-12 16:40:13 +00:00
|
|
|
// Doesn't decode correctly
|
|
|
|
return PC_TEX_FMT_NONE;
|
2009-10-14 22:28:23 +00:00
|
|
|
kernelToRun = Decoders[7].kernel;
|
2009-10-20 00:55:07 +00:00
|
|
|
sizeOfSrc = sizeof(u8) / 2.0f;
|
|
|
|
sizeOfDst = sizeof(u32);
|
2009-10-11 22:21:36 +00:00
|
|
|
xSkip = 8;
|
|
|
|
ySkip = 8;
|
|
|
|
formatResult = PC_TEX_FMT_BGRA32;
|
2009-10-13 21:51:27 +00:00
|
|
|
break;
|
2009-10-11 21:14:02 +00:00
|
|
|
default:
|
|
|
|
return PC_TEX_FMT_NONE;
|
|
|
|
}
|
2009-10-11 20:03:55 +00:00
|
|
|
|
2009-10-20 00:55:07 +00:00
|
|
|
#ifdef DEBUG_OPENCL
|
|
|
|
g_clsrc = clCreateBuffer(OpenCL::GetContext(), CL_MEM_READ_ONLY , 1024 * 1024 * sizeof(u32), NULL, NULL);
|
|
|
|
g_cldst = clCreateBuffer(OpenCL::GetContext(), CL_MEM_WRITE_ONLY, 1024 * 1024 * sizeof(u32), NULL, NULL);
|
|
|
|
#endif
|
|
|
|
|
2009-10-14 22:28:23 +00:00
|
|
|
clEnqueueWriteBuffer(OpenCL::GetCommandQueue(), g_clsrc, CL_TRUE, 0, (size_t)(width * height * sizeOfSrc), src, 0, NULL, NULL);
|
2009-10-11 20:03:55 +00:00
|
|
|
|
2009-10-14 22:28:23 +00:00
|
|
|
clSetKernelArg(kernelToRun, 0, sizeof(cl_mem), &g_cldst);
|
|
|
|
clSetKernelArg(kernelToRun, 1, sizeof(cl_mem), &g_clsrc);
|
2009-10-11 21:14:02 +00:00
|
|
|
clSetKernelArg(kernelToRun, 2, sizeof(cl_int), &width);
|
2009-10-07 21:30:36 +00:00
|
|
|
|
2009-10-13 21:51:27 +00:00
|
|
|
size_t global[] = { (size_t)(width / xSkip), (size_t)(height / ySkip) };
|
2009-10-07 21:30:36 +00:00
|
|
|
|
2009-10-11 21:14:02 +00:00
|
|
|
// No work-groups for now
|
|
|
|
/*
|
|
|
|
size_t local;
|
|
|
|
err = clGetKernelWorkGroupInfo(kernelToRun, OpenCL::device_id, CL_KERNEL_WORK_GROUP_SIZE, sizeof(local), &local, NULL);
|
|
|
|
if(err)
|
|
|
|
PanicAlert("Error obtaining work-group information");
|
|
|
|
*/
|
2009-10-11 20:03:55 +00:00
|
|
|
|
2009-10-11 21:14:02 +00:00
|
|
|
err = clEnqueueNDRangeKernel(OpenCL::GetCommandQueue(), kernelToRun, 2, NULL, global, NULL, 0, NULL, NULL);
|
|
|
|
if(err)
|
2009-10-20 00:55:07 +00:00
|
|
|
OpenCL::HandleCLError(err, "Failed to enqueue kernel");
|
2009-10-11 20:03:55 +00:00
|
|
|
|
2009-10-11 21:14:02 +00:00
|
|
|
clFinish(OpenCL::GetCommandQueue());
|
|
|
|
|
2009-10-14 22:28:23 +00:00
|
|
|
clEnqueueReadBuffer(OpenCL::GetCommandQueue(), g_cldst, CL_TRUE, 0, (size_t)(width * height * sizeOfDst), dst, 0, NULL, NULL);
|
2009-10-20 00:55:07 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_OPENCL
|
|
|
|
clReleaseMemObject(g_clsrc);
|
|
|
|
clReleaseMemObject(g_cldst);
|
|
|
|
#endif
|
|
|
|
|
2009-10-11 21:14:02 +00:00
|
|
|
return formatResult;
|
2009-10-11 20:03:55 +00:00
|
|
|
#else
|
|
|
|
return PC_TEX_FMT_NONE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return PC_TEX_FMT_NONE;
|
|
|
|
}
|
|
|
|
|