mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 06:09:50 -06:00
More OpenCL work, got XFB converting which needs more optimization. I haven't checked for FPS changes. My desktop isn't the best to test on anyway (Phenom 1, 32 stream processors). The package check doesn't work for me, so I just checked true if you compile with opencl=true. Requires a bit of cleanup still
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4369 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
@ -19,6 +19,8 @@
|
||||
#include <intrin.h>
|
||||
#endif
|
||||
|
||||
#include "OpenCL.h"
|
||||
|
||||
#include <xmmintrin.h>
|
||||
|
||||
#include "XFBConvert.h"
|
||||
@ -62,13 +64,119 @@ void InitXFBConvTables()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const char *__ConvertFromXFB = "int bound(int i) \n \
|
||||
{ \n \
|
||||
return (i>255)?255:((i<0)?0:i); \n \
|
||||
} \n \
|
||||
\n \
|
||||
void yuv2rgb(int y, int u, int v, int &r, int &g, int &b) \n \
|
||||
{ \n \
|
||||
b = bound((76283*(y - 16) + 132252*(u - 128))>>16); \n \
|
||||
g = bound((76283*(y - 16) - 53281 *(v - 128) - 25624*(u - 128))>>16); //last one u? \n \
|
||||
r = bound((76283*(y - 16) + 104595*(v - 128))>>16); \n \
|
||||
} \n \
|
||||
\n \
|
||||
void ConvertFromXFB(u32 *dst, const u8* _pXFB) \n \
|
||||
{ \n \
|
||||
const unsigned char *src = _pXFB; \n \
|
||||
int id = get_global_id(0); \n \
|
||||
int srcOffset = id * 4; \n \
|
||||
int dstOffset = id; \n \
|
||||
u32 numBlocks = (width * height) / 2; \n \
|
||||
\n \
|
||||
int Y1 = src[srcOffset]; \n \
|
||||
int U = src[srcOffset + 1]; \n \
|
||||
int Y2 = src[srcOffset + 2]; \n \
|
||||
int V = src[srcOffset + 3]; \n \
|
||||
\n \
|
||||
int r, g, b; \n \
|
||||
yuv2rgb(Y1,U,V, r,g,b); \n \
|
||||
dst[dstOffset] = 0xFF000000 | (r<<16) | (g<<8) | (b); \n \
|
||||
yuv2rgb(Y2,U,V, r,g,b); \n \
|
||||
dst[dstOffset + 1] = 0xFF000000 | (r<<16) | (g<<8) | (b); \n \
|
||||
} \n";
|
||||
|
||||
void ConvertFromXFB(u32 *dst, const u8* _pXFB, int width, int height)
|
||||
{
|
||||
if (((size_t)dst & 0xF) != 0) {
|
||||
PanicAlert("ConvertFromXFB - unaligned destination");
|
||||
}
|
||||
const unsigned char *src = _pXFB;
|
||||
}
|
||||
const unsigned char *src = _pXFB;
|
||||
u32 numBlocks = ((width * height) / 2) / 2;
|
||||
#if defined(HAVE_OPENCL) && HAVE_OPENCL
|
||||
cl_kernel kernel;
|
||||
cl_program program;
|
||||
program = OpenCL::CompileProgram(__ConvertFromXFB);
|
||||
kernel = OpenCL::CompileKernel(program, "ConvertFromXFB");
|
||||
int err;
|
||||
|
||||
size_t global = 0; // global domain size for our calculation
|
||||
size_t local = 0; // local domain size for our calculation
|
||||
printf("width %d, height %d\n", width, height);
|
||||
// Create the input and output arrays in device memory for our calculation
|
||||
//
|
||||
cl_mem _dst = clCreateBuffer(OpenCL::g_context, CL_MEM_WRITE_ONLY, sizeof(unsigned int) * numBlocks, NULL, NULL);
|
||||
if (!dst)
|
||||
{
|
||||
printf("Error: Failed to allocate device memory!\n");
|
||||
exit(1);
|
||||
}
|
||||
cl_mem _src = clCreateBuffer(OpenCL::g_context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(unsigned char) * width * height, (void*)_pXFB, NULL);
|
||||
if (!src)
|
||||
{
|
||||
printf("Error: Failed to allocate device memory!\n");
|
||||
exit(1);
|
||||
}
|
||||
// Set the arguments to our compute kernel
|
||||
//
|
||||
err = 0;
|
||||
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &_dst);
|
||||
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &_src);
|
||||
if (err != CL_SUCCESS)
|
||||
{
|
||||
printf("Error: Failed to set kernel arguments! %d\n", err);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Get the maximum work group size for executing the kernel on the device
|
||||
//
|
||||
err = clGetKernelWorkGroupInfo(Decoders[0].kernel, OpenCL::device_id, CL_KERNEL_WORK_GROUP_SIZE, sizeof(int), &local, NULL);
|
||||
if (err != CL_SUCCESS)
|
||||
{
|
||||
printf("Error: Failed to retrieve kernel work group info! %d\n", err);
|
||||
exit(1);
|
||||
}
|
||||
// Execute the kernel over the entire range of our 1d input data set
|
||||
// using the maximum number of work group items for this device
|
||||
//
|
||||
global = numBlocks;
|
||||
if(global < local)
|
||||
{
|
||||
// Global can't be less than local
|
||||
}
|
||||
err = clEnqueueNDRangeKernel(OpenCL::g_cmdq, kernel, 1, NULL, &global, &local, 0, NULL, NULL);
|
||||
if (err != CL_SUCCESS)
|
||||
{
|
||||
printf("Error: Failed to execute kernel! %d\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait for the command commands to get serviced before reading back results
|
||||
//
|
||||
clFinish(OpenCL::g_cmdq);
|
||||
|
||||
// Read back the results from the device to verify the output
|
||||
//
|
||||
err = clEnqueueReadBuffer( OpenCL::g_cmdq, _dst, CL_TRUE, 0, sizeof(unsigned int) * numBlocks, dst, 0, NULL, NULL );
|
||||
if (err != CL_SUCCESS)
|
||||
{
|
||||
printf("Error: Failed to read output array! %d\n", err);
|
||||
exit(1);
|
||||
}
|
||||
clReleaseMemObject(_dst);
|
||||
clReleaseMemObject(_src);
|
||||
#else
|
||||
for (u32 i = 0; i < numBlocks; i++)
|
||||
{
|
||||
__m128i y1 = _y[src[0]];
|
||||
@ -90,8 +198,27 @@ void ConvertFromXFB(u32 *dst, const u8* _pXFB, int width, int height)
|
||||
dst += 4;
|
||||
src += 8;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
const char *__ConvertToXFB = "__kernel void ConvertToXFB(__global unsigned int *dst, __global const unsigned char* _pEFB) \n \
|
||||
{ \n \
|
||||
const unsigned char *src = _pEFB;\n \
|
||||
int id = get_global_id(0);\n \
|
||||
src += id * 8; \n \
|
||||
\n \
|
||||
int y1 = (((16843 * src[0]) + (33030 * src[1]) + (6423 * src[2])) >> 16) + 16; \n \
|
||||
int u1 = ((-(9699 * src[0]) - (19071 * src[1]) + (28770 * src[2])) >> 16) + 128;\n \
|
||||
src += 4;\n \
|
||||
\n \
|
||||
int y2 = (((16843 * src[0]) + (33030 * src[1]) + (6423 * src[2])) >> 16) + 16;\n \
|
||||
int v2 = (((28770 * src[0]) - (24117 * src[1]) - (4653 * src[2])) >> 16) + 128;\n \
|
||||
src += 4;\n \
|
||||
\n \
|
||||
dst[id] = (v2 << 24) | (y2 << 16) | (u1 << 8) | (y1); \n \
|
||||
} \n ";
|
||||
|
||||
|
||||
void ConvertToXFB(u32 *dst, const u8* _pEFB, int width, int height)
|
||||
{
|
||||
const unsigned char *src = _pEFB;
|
||||
@ -100,7 +227,79 @@ void ConvertToXFB(u32 *dst, const u8* _pEFB, int width, int height)
|
||||
if (((size_t)dst & 0xF) != 0) {
|
||||
PanicAlert("ConvertToXFB - unaligned XFB");
|
||||
}
|
||||
#if defined(HAVE_OPENCL) && HAVE_OPENCL
|
||||
cl_kernel kernel;
|
||||
cl_program program;
|
||||
program = OpenCL::CompileProgram(__ConvertToXFB);
|
||||
kernel = OpenCL::CompileKernel(program, "ConvertToXFB");
|
||||
int err;
|
||||
|
||||
size_t global = 0; // global domain size for our calculation
|
||||
size_t local = 0; // local domain size for our calculation
|
||||
printf("width %d, height %d\n", width, height);
|
||||
// Create the input and output arrays in device memory for our calculation
|
||||
//
|
||||
cl_mem _dst = clCreateBuffer(OpenCL::g_context, CL_MEM_WRITE_ONLY, sizeof(unsigned int) * numBlocks, NULL, NULL);
|
||||
if (!dst)
|
||||
{
|
||||
printf("Error: Failed to allocate device memory!\n");
|
||||
exit(1);
|
||||
}
|
||||
cl_mem _src = clCreateBuffer(OpenCL::g_context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(unsigned char) * width * height, (void*)_pEFB, NULL);
|
||||
if (!src)
|
||||
{
|
||||
printf("Error: Failed to allocate device memory!\n");
|
||||
exit(1);
|
||||
}
|
||||
// Set the arguments to our compute kernel
|
||||
//
|
||||
err = 0;
|
||||
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &_dst);
|
||||
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &_src);
|
||||
if (err != CL_SUCCESS)
|
||||
{
|
||||
printf("Error: Failed to set kernel arguments! %d\n", err);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Get the maximum work group size for executing the kernel on the device
|
||||
//
|
||||
err = clGetKernelWorkGroupInfo(Decoders[0].kernel, OpenCL::device_id, CL_KERNEL_WORK_GROUP_SIZE, sizeof(int), &local, NULL);
|
||||
if (err != CL_SUCCESS)
|
||||
{
|
||||
printf("Error: Failed to retrieve kernel work group info! %d\n", err);
|
||||
exit(1);
|
||||
}
|
||||
// Execute the kernel over the entire range of our 1d input data set
|
||||
// using the maximum number of work group items for this device
|
||||
//
|
||||
global = numBlocks;
|
||||
if(global < local)
|
||||
{
|
||||
// Global can't be less than local
|
||||
}
|
||||
err = clEnqueueNDRangeKernel(OpenCL::g_cmdq, kernel, 1, NULL, &global, &local, 0, NULL, NULL);
|
||||
if (err != CL_SUCCESS)
|
||||
{
|
||||
printf("Error: Failed to execute kernel! %d\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait for the command commands to get serviced before reading back results
|
||||
//
|
||||
clFinish(OpenCL::g_cmdq);
|
||||
|
||||
// Read back the results from the device to verify the output
|
||||
//
|
||||
err = clEnqueueReadBuffer( OpenCL::g_cmdq, _dst, CL_TRUE, 0, sizeof(unsigned int) * numBlocks, dst, 0, NULL, NULL );
|
||||
if (err != CL_SUCCESS)
|
||||
{
|
||||
printf("Error: Failed to read output array! %d\n", err);
|
||||
exit(1);
|
||||
}
|
||||
clReleaseMemObject(_dst);
|
||||
clReleaseMemObject(_src);
|
||||
#else
|
||||
for (u32 i = 0; i < numBlocks; i++)
|
||||
{
|
||||
__m128i yuyv0 = _mm_srai_epi32(
|
||||
@ -127,5 +326,6 @@ void ConvertToXFB(u32 *dst, const u8* _pEFB, int width, int height)
|
||||
_mm_store_si128((__m128i *)dst, four_dest);
|
||||
dst += 4;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user