| File: | /home/anal/dolphin-emu/Source/Core/VideoCommon/Src/OpenCL.cpp |
| Location: | line 167, column 6 |
| Description: | Value stored to 'compileStart' during its initialization is never read |
| 1 | // Copyright 2013 Dolphin Emulator Project |
| 2 | // Licensed under GPLv2 |
| 3 | // Refer to the license.txt file included. |
| 4 | |
| 5 | // TODO: Make a more centralized version of this (for now every backend that will use it will create its own context, which is weird). An object maybe? |
| 6 | |
| 7 | #include "OpenCL.h" |
| 8 | #include "Common.h" |
| 9 | #include "Timer.h" |
| 10 | |
| 11 | namespace OpenCL |
| 12 | { |
| 13 | |
| 14 | cl_device_id device_id = NULL__null; |
| 15 | cl_context g_context = NULL__null; |
| 16 | cl_command_queue g_cmdq = NULL__null; |
| 17 | |
| 18 | bool g_bInitialized = false; |
| 19 | |
| 20 | bool Initialize() |
| 21 | { |
| 22 | if(g_bInitialized) |
| 23 | return true; |
| 24 | |
| 25 | if(g_context) |
| 26 | return false; |
| 27 | int err; // error code returned from api calls |
| 28 | |
| 29 | #ifdef __APPLE__ |
| 30 | // If OpenCL is weakly linked and not found, its symbols will be NULL |
| 31 | if (clGetPlatformIDs == NULL__null) |
| 32 | return false; |
| 33 | #else |
| 34 | clrInit(); |
| 35 | if(!clrHasOpenCL()) |
| 36 | return false; |
| 37 | #endif |
| 38 | |
| 39 | // Connect to a compute device |
| 40 | cl_uint numPlatforms; |
| 41 | cl_platform_id platform = NULL__null; |
| 42 | err = clGetPlatformIDs(0, NULL__null, &numPlatforms); |
| 43 | |
| 44 | if (err != CL_SUCCESS0) |
| 45 | { |
| 46 | HandleCLError(err, "clGetPlatformIDs failed."); |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | if (0 < numPlatforms) |
| 51 | { |
| 52 | cl_platform_id* platforms = new cl_platform_id[numPlatforms]; |
| 53 | err = clGetPlatformIDs(numPlatforms, platforms, NULL__null); |
| 54 | |
| 55 | if (err != CL_SUCCESS0) |
| 56 | { |
| 57 | HandleCLError(err, "clGetPlatformIDs failed."); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | char pbuf[100]; |
| 62 | err = clGetPlatformInfo(platforms[0], CL_PLATFORM_VENDOR0x0903, sizeof(pbuf), |
| 63 | pbuf, NULL__null); |
| 64 | |
| 65 | if (err != CL_SUCCESS0) |
| 66 | { |
| 67 | HandleCLError(err, "clGetPlatformInfo failed."); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | platform = platforms[0]; |
| 72 | delete[] platforms; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | PanicAlert("No OpenCL platform found.")MsgAlert(false, WARNING, "No OpenCL platform found."); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | cl_context_properties cps[3] = {CL_CONTEXT_PLATFORM0x1084, |
| 81 | (cl_context_properties)platform, 0}; |
| 82 | |
| 83 | cl_context_properties* cprops = (NULL__null == platform) ? NULL__null : cps; |
| 84 | |
| 85 | err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_DEFAULT(1 << 0), 1, &device_id, NULL__null); |
| 86 | if (err != CL_SUCCESS0) |
| 87 | { |
| 88 | HandleCLError(err, "Failed to create a device group!"); |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | // Create a compute context |
| 93 | g_context = clCreateContext(cprops, 1, &device_id, NULL__null, NULL__null, &err); |
| 94 | if (!g_context) |
| 95 | { |
| 96 | HandleCLError(err, "Failed to create a compute context!"); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // Create a command commands |
| 101 | g_cmdq = clCreateCommandQueue(g_context, device_id, 0, &err); |
| 102 | if (!g_cmdq) |
| 103 | { |
| 104 | HandleCLError(err, "Failed to create a command commands!"); |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | g_bInitialized = true; |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | cl_context GetContext() |
| 113 | { |
| 114 | return g_context; |
| 115 | } |
| 116 | |
| 117 | cl_command_queue GetCommandQueue() |
| 118 | { |
| 119 | return g_cmdq; |
| 120 | } |
| 121 | |
| 122 | cl_program CompileProgram(const char *Kernel) |
| 123 | { |
| 124 | u32 compileStart = Common::Timer::GetTimeMs(); |
| 125 | cl_int err; |
| 126 | cl_program program; |
| 127 | program = clCreateProgramWithSource(OpenCL::g_context, 1, |
| 128 | (const char **) & Kernel, NULL__null, &err); |
| 129 | |
| 130 | if (!program) |
| 131 | { |
| 132 | HandleCLError(err, "Error: Failed to create compute program!"); |
| 133 | } |
| 134 | |
| 135 | // Build the program executable |
| 136 | err = clBuildProgram(program , 0, NULL__null, NULL__null, NULL__null, NULL__null); |
| 137 | if(err != CL_SUCCESS0) { |
| 138 | HandleCLError(err, "Error: failed to build program"); |
| 139 | |
| 140 | char *buildlog = NULL__null; |
| 141 | size_t buildlog_size = 0; |
| 142 | |
| 143 | clGetProgramBuildInfo(program, OpenCL::device_id, CL_PROGRAM_BUILD_LOG0x1183, 0, NULL__null, &buildlog_size); |
| 144 | buildlog = new char[buildlog_size + 1]; |
| 145 | err = clGetProgramBuildInfo(program, OpenCL::device_id, CL_PROGRAM_BUILD_LOG0x1183, buildlog_size, buildlog, NULL__null); |
| 146 | buildlog[buildlog_size] = 0; |
| 147 | |
| 148 | if(err != CL_SUCCESS0) |
| 149 | { |
| 150 | HandleCLError(err, "Error: can't get build log"); |
| 151 | } else |
| 152 | { |
| 153 | ERROR_LOG(COMMON, "Error log:\n%s\n", buildlog)do { { if (LogTypes::LERROR <= 3) GenericLog(LogTypes::LERROR , LogTypes::COMMON, "/home/anal/dolphin-emu/Source/Core/VideoCommon/Src/OpenCL.cpp" , 153, "Error log:\n%s\n", buildlog); } } while (0); |
| 154 | } |
| 155 | |
| 156 | delete[] buildlog; |
| 157 | return NULL__null; |
| 158 | } |
| 159 | |
| 160 | INFO_LOG(COMMON, "OpenCL CompileProgram took %.3f seconds",do { { if (LogTypes::LINFO <= 3) GenericLog(LogTypes::LINFO , LogTypes::COMMON, "/home/anal/dolphin-emu/Source/Core/VideoCommon/Src/OpenCL.cpp" , 161, "OpenCL CompileProgram took %.3f seconds", (float)(Common ::Timer::GetTimeMs() - compileStart) / 1000.0); } } while (0) |
| 161 | (float)(Common::Timer::GetTimeMs() - compileStart) / 1000.0)do { { if (LogTypes::LINFO <= 3) GenericLog(LogTypes::LINFO , LogTypes::COMMON, "/home/anal/dolphin-emu/Source/Core/VideoCommon/Src/OpenCL.cpp" , 161, "OpenCL CompileProgram took %.3f seconds", (float)(Common ::Timer::GetTimeMs() - compileStart) / 1000.0); } } while (0); |
| 162 | return program; |
| 163 | } |
| 164 | |
| 165 | cl_kernel CompileKernel(cl_program program, const char *Function) |
| 166 | { |
| 167 | u32 compileStart = Common::Timer::GetTimeMs(); |
Value stored to 'compileStart' during its initialization is never read | |
| 168 | int err; |
| 169 | |
| 170 | // Create the compute kernel in the program we wish to run |
| 171 | cl_kernel kernel = clCreateKernel(program, Function, &err); |
| 172 | if (!kernel || err != CL_SUCCESS0) |
| 173 | { |
| 174 | char buffer[1024]; |
| 175 | sprintf(buffer, "Failed to create compute kernel '%s' !", Function); |
| 176 | HandleCLError(err, buffer); |
| 177 | return NULL__null; |
| 178 | } |
| 179 | INFO_LOG(COMMON, "OpenCL CompileKernel took %.3f seconds",do { { if (LogTypes::LINFO <= 3) GenericLog(LogTypes::LINFO , LogTypes::COMMON, "/home/anal/dolphin-emu/Source/Core/VideoCommon/Src/OpenCL.cpp" , 180, "OpenCL CompileKernel took %.3f seconds", (float)(Common ::Timer::GetTimeMs() - compileStart) / 1000.0); } } while (0) |
| 180 | (float)(Common::Timer::GetTimeMs() - compileStart) / 1000.0)do { { if (LogTypes::LINFO <= 3) GenericLog(LogTypes::LINFO , LogTypes::COMMON, "/home/anal/dolphin-emu/Source/Core/VideoCommon/Src/OpenCL.cpp" , 180, "OpenCL CompileKernel took %.3f seconds", (float)(Common ::Timer::GetTimeMs() - compileStart) / 1000.0); } } while (0); |
| 181 | return kernel; |
| 182 | } |
| 183 | |
| 184 | void Destroy() |
| 185 | { |
| 186 | if (g_cmdq) |
| 187 | { |
| 188 | clReleaseCommandQueue(g_cmdq); |
| 189 | g_cmdq = NULL__null; |
| 190 | } |
| 191 | if (g_context) |
| 192 | { |
| 193 | clReleaseContext(g_context); |
| 194 | g_context = NULL__null; |
| 195 | } |
| 196 | g_bInitialized = false; |
| 197 | } |
| 198 | |
| 199 | void HandleCLError(cl_int error, const char* str) |
| 200 | { |
| 201 | const char* name; |
| 202 | switch(error) |
| 203 | { |
| 204 | #define CL_ERROR(x) case (x): name = #x; break |
| 205 | CL_ERROR(CL_SUCCESS0); |
| 206 | CL_ERROR(CL_DEVICE_NOT_FOUND-1); |
| 207 | CL_ERROR(CL_DEVICE_NOT_AVAILABLE-2); |
| 208 | CL_ERROR(CL_COMPILER_NOT_AVAILABLE-3); |
| 209 | CL_ERROR(CL_MEM_OBJECT_ALLOCATION_FAILURE-4); |
| 210 | CL_ERROR(CL_OUT_OF_RESOURCES-5); |
| 211 | CL_ERROR(CL_OUT_OF_HOST_MEMORY-6); |
| 212 | CL_ERROR(CL_PROFILING_INFO_NOT_AVAILABLE-7); |
| 213 | CL_ERROR(CL_MEM_COPY_OVERLAP-8); |
| 214 | CL_ERROR(CL_IMAGE_FORMAT_MISMATCH-9); |
| 215 | CL_ERROR(CL_IMAGE_FORMAT_NOT_SUPPORTED-10); |
| 216 | CL_ERROR(CL_BUILD_PROGRAM_FAILURE-11); |
| 217 | CL_ERROR(CL_MAP_FAILURE-12); |
| 218 | CL_ERROR(CL_INVALID_VALUE-30); |
| 219 | CL_ERROR(CL_INVALID_DEVICE_TYPE-31); |
| 220 | CL_ERROR(CL_INVALID_PLATFORM-32); |
| 221 | CL_ERROR(CL_INVALID_DEVICE-33); |
| 222 | CL_ERROR(CL_INVALID_CONTEXT-34); |
| 223 | CL_ERROR(CL_INVALID_QUEUE_PROPERTIES-35); |
| 224 | CL_ERROR(CL_INVALID_COMMAND_QUEUE-36); |
| 225 | CL_ERROR(CL_INVALID_HOST_PTR-37); |
| 226 | CL_ERROR(CL_INVALID_MEM_OBJECT-38); |
| 227 | CL_ERROR(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR-39); |
| 228 | CL_ERROR(CL_INVALID_IMAGE_SIZE-40); |
| 229 | CL_ERROR(CL_INVALID_SAMPLER-41); |
| 230 | CL_ERROR(CL_INVALID_BINARY-42); |
| 231 | CL_ERROR(CL_INVALID_BUILD_OPTIONS-43); |
| 232 | CL_ERROR(CL_INVALID_PROGRAM-44); |
| 233 | CL_ERROR(CL_INVALID_PROGRAM_EXECUTABLE-45); |
| 234 | CL_ERROR(CL_INVALID_KERNEL_NAME-46); |
| 235 | CL_ERROR(CL_INVALID_KERNEL_DEFINITION-47); |
| 236 | CL_ERROR(CL_INVALID_KERNEL-48); |
| 237 | CL_ERROR(CL_INVALID_ARG_INDEX-49); |
| 238 | CL_ERROR(CL_INVALID_ARG_VALUE-50); |
| 239 | CL_ERROR(CL_INVALID_ARG_SIZE-51); |
| 240 | CL_ERROR(CL_INVALID_KERNEL_ARGS-52); |
| 241 | CL_ERROR(CL_INVALID_WORK_DIMENSION-53); |
| 242 | CL_ERROR(CL_INVALID_WORK_GROUP_SIZE-54); |
| 243 | CL_ERROR(CL_INVALID_WORK_ITEM_SIZE-55); |
| 244 | CL_ERROR(CL_INVALID_GLOBAL_OFFSET-56); |
| 245 | CL_ERROR(CL_INVALID_EVENT_WAIT_LIST-57); |
| 246 | CL_ERROR(CL_INVALID_EVENT-58); |
| 247 | CL_ERROR(CL_INVALID_OPERATION-59); |
| 248 | CL_ERROR(CL_INVALID_GL_OBJECT-60); |
| 249 | CL_ERROR(CL_INVALID_BUFFER_SIZE-61); |
| 250 | CL_ERROR(CL_INVALID_MIP_LEVEL-62); |
| 251 | #undef CL_ERROR |
| 252 | default: |
| 253 | name = "Unknown error code"; |
| 254 | } |
| 255 | if(!str) |
| 256 | str = ""; |
| 257 | ERROR_LOG(COMMON, "OpenCL error: %s %s (%d)", str, name, error)do { { if (LogTypes::LERROR <= 3) GenericLog(LogTypes::LERROR , LogTypes::COMMON, "/home/anal/dolphin-emu/Source/Core/VideoCommon/Src/OpenCL.cpp" , 257, "OpenCL error: %s %s (%d)", str, name, error); } } while (0); |
| 258 | } |
| 259 | } |