Skip to content

Commit f30bea9

Browse files
committed
# Conflicts: # CLW/CLWParallelPrimitives.cpp # RadeonRays/RadeonRays.lua
2 parents 63c0c8c + 5eb8f11 commit f30bea9

20 files changed

+264
-47
lines changed

CLW/CLW.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ project "CLW"
1414

1515
configuration {}
1616

17+
if _OPTIONS["allow_cpu_devices"] then
18+
defines {"RR_ALLOW_CPU_DEVICES=1"}
19+
end
20+
1721
-- we rely on RadeonRays to do the actual embedding for us
1822
defines {"RR_EMBED_KERNELS=1"}
1923
os.execute( "python ../Tools/scripts/stringify.py " ..

CLW/CLWDevice.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ CLWDevice::CLWDevice(cl_device_id id) : ReferenceCounter<cl_device_id, clRetainD
3232
GetDeviceInfoParameter(*this, CL_DEVICE_NAME, name_);
3333
GetDeviceInfoParameter(*this, CL_DEVICE_EXTENSIONS, extensions_);
3434
GetDeviceInfoParameter(*this, CL_DEVICE_VENDOR, vendor_);
35-
GetDeviceInfoParameter(*this, CL_DEVICE_VERSION, version_);
35+
GetDeviceInfoParameter(*this, CL_DRIVER_VERSION, version_);
3636
GetDeviceInfoParameter(*this, CL_DEVICE_PROFILE, profile_);
3737
GetDeviceInfoParameter(*this, CL_DEVICE_TYPE, type_);
3838

CLW/CLWParallelPrimitives.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ THE SOFTWARE.
3030

3131
#ifdef RR_EMBED_KERNELS
3232
#if USE_OPENCL
33-
#include <CLW/kernelcache/clwkernels_cl.h>
33+
#include "CLW/kernelcache/clwkernels_cl.h"
3434
#endif
3535
#endif // RR_EMBED_KERNELS
3636

@@ -952,4 +952,4 @@ CLWEvent CLWParallelPrimitives::Copy(unsigned int deviceIdx, CLWBuffer<cl_int> i
952952
copyKernel.SetArg(2, output);
953953

954954
return context_.Launch1D(0, NUM_BLOCKS * WG_SIZE, WG_SIZE, copyKernel);
955-
}
955+
}

CLW/CLWPlatform.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ void CLWPlatform::CreateAllPlatforms(std::vector<CLWPlatform>& platforms)
4848
status = clGetPlatformIDs(numPlatforms, &platformIds[0], nullptr);
4949
ThrowIf(status != CL_SUCCESS, status, "clGetPlatformIDs failed");
5050

51-
cl_device_type type = CL_DEVICE_TYPE_ALL;
51+
52+
#ifdef RR_ALLOW_CPU_DEVICES
53+
cl_device_type type = CL_DEVICE_TYPE_ALL;
54+
#else
55+
cl_device_type type = CL_DEVICE_TYPE_GPU;
56+
#endif
5257

5358
// TODO: this is a workaround for nasty Apple's OpenCL runtime
5459
// which doesn't allow to have work group sizes > 1 on CPU devices
@@ -70,8 +75,8 @@ void CLWPlatform::CreateAllPlatforms(std::vector<CLWPlatform>& platforms)
7075

7176
std::string versionstr(version.begin(), version.end());
7277

73-
if (versionstr.find("1.0") != std::string::npos ||
74-
versionstr.find("1.1") != std::string::npos)
78+
if (versionstr.find("OpenCL 1.0 ") != std::string::npos ||
79+
versionstr.find("OpenCL 1.1") != std::string::npos)
7580
{
7681
continue;
7782
}
@@ -163,6 +168,12 @@ void CLWPlatform::InitDeviceList(cl_device_type type) const
163168
{
164169
cl_uint numDevices = 0;
165170
cl_int status = clGetDeviceIDs(*this, type, 0, nullptr, &numDevices);
171+
172+
if (status == CL_DEVICE_NOT_FOUND)
173+
{
174+
return;
175+
}
176+
166177
ThrowIf(status != CL_SUCCESS, status, "clGetDeviceIDs failed");
167178

168179
std::vector<cl_device_id> deviceIds(numDevices);

CLW/CLWProgram.cpp

Lines changed: 114 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ CLWProgram CLWProgram::CreateFromSource(char const* sourcecode,
107107
CLWContext context)
108108
{
109109
cl_int status = CL_SUCCESS;
110-
110+
111111
std::vector<cl_device_id> deviceIds(context.GetDeviceCount());
112112
for(unsigned int i = 0; i < context.GetDeviceCount(); ++i)
113113
{
@@ -170,6 +170,61 @@ CLWProgram CLWProgram::CreateFromSource(char const* sourcecode,
170170
return prg;
171171
}
172172

173+
CLWProgram CLWProgram::CreateFromBinary(std::uint8_t** binaries, std::size_t* binary_sizes, CLWContext context)
174+
{
175+
cl_int status = CL_SUCCESS;
176+
177+
std::vector<cl_device_id> deviceIds(context.GetDeviceCount());
178+
for (unsigned int i = 0; i < context.GetDeviceCount(); ++i)
179+
{
180+
deviceIds[i] = context.GetDevice(i);
181+
}
182+
183+
cl_int binary_status = 0;
184+
cl_program program = clCreateProgramWithBinary(context, context.GetDeviceCount(), &deviceIds[0], binary_sizes, (const unsigned char**)binaries, &binary_status, &status);
185+
186+
if (status != CL_SUCCESS)
187+
{
188+
std::vector<char> buildLog;
189+
size_t logSize;
190+
clGetProgramBuildInfo(program, deviceIds[0], CL_PROGRAM_BUILD_LOG, 0, nullptr, &logSize);
191+
192+
buildLog.resize(logSize);
193+
clGetProgramBuildInfo(program, deviceIds[0], CL_PROGRAM_BUILD_LOG, logSize, &buildLog[0], nullptr);
194+
195+
#ifdef _DEBUG
196+
std::cout << &buildLog[0] << "\n";
197+
#endif
198+
199+
throw CLWException(status, std::string(&buildLog[0]));
200+
}
201+
202+
status = clBuildProgram(program, context.GetDeviceCount(), &deviceIds[0], nullptr, nullptr, nullptr);
203+
204+
if (status != CL_SUCCESS)
205+
{
206+
std::vector<char> buildLog;
207+
size_t logSize;
208+
clGetProgramBuildInfo(program, deviceIds[0], CL_PROGRAM_BUILD_LOG, 0, nullptr, &logSize);
209+
210+
buildLog.resize(logSize);
211+
clGetProgramBuildInfo(program, deviceIds[0], CL_PROGRAM_BUILD_LOG, logSize, &buildLog[0], nullptr);
212+
213+
#ifdef _DEBUG
214+
std::cout << &buildLog[0] << "\n";
215+
#endif
216+
217+
throw CLWException(status, std::string(&buildLog[0]));
218+
}
219+
220+
CLWProgram prg(program);
221+
222+
clReleaseProgram(program);
223+
224+
return prg;
225+
226+
}
227+
173228
CLWProgram CLWProgram::CreateFromFile(char const* filename, char const* buildopts, CLWContext context)
174229
{
175230
std::vector<char> sourcecode;
@@ -218,24 +273,24 @@ CLWProgram::CLWProgram(cl_program program)
218273
ThrowIf(numKernels == 0, CL_BUILD_ERROR, "clCreateKernelsInProgram return 0 kernels");
219274

220275
ThrowIf(status != CL_SUCCESS, status, "clCreateKernelsInProgram failed");
221-
276+
222277
std::vector<cl_kernel> kernels(numKernels);
223278
status = clCreateKernelsInProgram(*this, numKernels, &kernels[0], nullptr);
224-
279+
225280
ThrowIf(status != CL_SUCCESS, status, "clCreateKernelsInProgram failed");
226-
281+
227282
std::for_each(kernels.begin(), kernels.end(), [this](cl_kernel k)
228283
{
229284
size_t size = 0;
230285
cl_int res;
231-
286+
232287
res = clGetKernelInfo(k, CL_KERNEL_FUNCTION_NAME, 0, nullptr, &size);
233288
ThrowIf(res != CL_SUCCESS, res, "clGetKernelInfo failed");
234-
289+
235290
std::vector<char> temp(size);
236291
res = clGetKernelInfo(k, CL_KERNEL_FUNCTION_NAME, size, &temp[0], nullptr);
237292
ThrowIf(res != CL_SUCCESS, res, "clGetKernelInfo failed");
238-
293+
239294
std::string funcName(temp.begin(), temp.end()-1);
240295
kernels_[funcName] = CLWKernel::Create(k);
241296
});
@@ -258,3 +313,55 @@ CLWKernel CLWProgram::GetKernel(std::string const& funcName) const
258313

259314
return iter->second;
260315
}
316+
317+
void CLWProgram::GetBinaries(int device, std::vector<std::uint8_t>& data) const
318+
{
319+
std::uint32_t num_devices;
320+
auto status = clGetProgramInfo(*this, CL_PROGRAM_NUM_DEVICES,
321+
sizeof(uint32_t),
322+
&num_devices,
323+
nullptr);
324+
325+
326+
std::vector<std::size_t> binary_sizes(num_devices);
327+
status = clGetProgramInfo(*this, CL_PROGRAM_BINARY_SIZES,
328+
sizeof(size_t) * num_devices,
329+
&binary_sizes[0],
330+
nullptr);
331+
332+
ThrowIf(status != CL_SUCCESS, status, "clGetProgramInfo failed");
333+
334+
data.resize(binary_sizes[device]);
335+
336+
char** temp = new char*[num_devices];
337+
338+
for (auto i = 0u; i < num_devices; ++i)
339+
{
340+
if (i == device)
341+
{
342+
temp[i] = (char*)&data[0];
343+
}
344+
else
345+
{
346+
temp[i] = new char[binary_sizes[i]];
347+
}
348+
}
349+
350+
status = clGetProgramInfo(*this, CL_PROGRAM_BINARIES,
351+
binary_sizes[0],
352+
temp,
353+
nullptr);
354+
355+
// TODO: fix potential leak here
356+
ThrowIf(status != CL_SUCCESS, status, "clGetProgramInfo failed");
357+
358+
for (auto i = 0u; i < num_devices; ++i)
359+
{
360+
if (i != device)
361+
{
362+
delete temp[i];
363+
}
364+
}
365+
366+
delete temp;
367+
}

CLW/CLWProgram.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ THE SOFTWARE.
2727
#include <vector>
2828
#include <map>
2929
#include <string>
30+
#include <cstdint>
3031

3132
#ifdef __APPLE__
3233
#include <OpenCL/OpenCL.h>
@@ -62,12 +63,16 @@ class CLWProgram : public ReferenceCounter<cl_program, clRetainProgram, clReleas
6263
char const* buildopts,
6364
CLWContext context);
6465

66+
static CLWProgram CreateFromBinary(std::uint8_t** binaries, std::size_t* binary_sizes, CLWContext context);
67+
6568
CLWProgram() {}
6669
virtual ~CLWProgram();
6770

6871
unsigned int GetKernelCount() const;
6972
CLWKernel GetKernel(std::string const& funcName) const;
70-
73+
74+
void GetBinaries(int device, std::vector<std::uint8_t>& data) const;
75+
7176
private:
7277
CLWProgram(cl_program program);
7378
std::map<std::string, CLWKernel> kernels_;

Calc/src/device_clw.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,6 @@ namespace Calc
459459
""
460460
#endif
461461
);
462-
#ifdef USE_SAFE_MATH
463-
buildopts.append("-D USE_SAFE_MATH");
464-
#endif
465462

466463
return new ExecutableClw(
467464
CLWProgram::CreateFromFile(filename, headernames, numheaders, buildopts.c_str(), m_context)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ The app only supports loading of pure triangle .obj meshes. The list of supporte
144144
- dds (limited support)
145145
- tga
146146

147+
## Run unit tests
148+
They need to be run from the <Radeon Rays_SDK path>/UnitTest path.
149+
Premake should be runned with the `--safe_math` option.
147150

148151
# Hardware support
149152

RadeonRays/RadeonRays.lua

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ project "RadeonRays"
77
end
88

99
location "../RadeonRays"
10-
includedirs { "./include", "../Calc/inc", ".." }
10+
includedirs { "./include", "../Calc/inc" }
1111

1212
if _OPTIONS["shared_calc"] then
1313
defines {"CALC_IMPORT_API"};
14-
links {"dl"}
14+
if os.is("windows") then
15+
characterset ("MBCS")
16+
else
17+
links {"dl"}
18+
end
1519
else
16-
defines {"CALC_STATIC_LIBRARY"}
20+
defines {"CALC_STATIC_LIBRARY"}
1721
links {"Calc"}
18-
if _OPTIONS["use_opencl"] then
22+
if _OPTIONS["use_opencl"] then
1923
links {"CLW"}
20-
end
24+
end
2125
end
2226

2327
if _OPTIONS["enable_raymask"] then
@@ -62,15 +66,29 @@ project "RadeonRays"
6266

6367
configuration {}
6468

65-
defines {"RR_EMBED_KERNELS=1"}
69+
if _OPTIONS["embed_kernels"] then
70+
defines {"RR_EMBED_KERNELS=1"}
71+
72+
if _OPTIONS["use_vulkan"] then
73+
os.execute( "python ../Tools/scripts/stringify.py " ..
74+
os.getcwd() .. "../RadeonRays/src/kernels/GLSL/ " ..
75+
".comp " ..
76+
"vulkan " ..
77+
"> ./src/kernelcache/kernels_vk.h"
78+
)
79+
print ">> RadeonRays: VK kernels embedded"
80+
end
6681

67-
os.execute( "python ../Tools/scripts/stringify.py " ..
82+
if _OPTIONS["use_opencl"] then
83+
os.execute( "python ../Tools/scripts/stringify.py " ..
6884
os.getcwd() .. "/../RadeonRays/src/kernels/CL/ " ..
6985
".cl " ..
7086
"opencl " ..
7187
"> ./src/kernelcache/kernels_cl.h"
7288
)
73-
print ">> RadeonRays: CL kernels embedded"
89+
print ">> RadeonRays: CL kernels embedded"
90+
end
91+
end
7492

7593
if _OPTIONS["use_embree"] then
7694
files {"../RadeonRays/src/device/embree*"}
@@ -94,16 +112,46 @@ project "RadeonRays"
94112
end
95113

96114
if _OPTIONS["enable_raymask"] then
97-
configuration {}
98-
defines {"RR_RAY_MASK"}
115+
configuration {}
116+
defines {"RR_RAY_MASK"}
117+
end
118+
119+
if _OPTIONS["safe_math"] then
120+
configuration {}
121+
defines { "USE_SAFE_MATH" }
122+
end
123+
124+
if _OPTIONS["use_vulkan"] then
125+
local vulkanSDKPath = os.getenv( "VK_SDK_PATH" );
126+
if vulkanSDKPath == nil then
127+
vulkanSDKPath = os.getenv( "VULKAN_SDK" );
128+
end
129+
if vulkanSDKPath ~= nil then
130+
configuration {"x32"}
131+
libdirs { vulkanSDKPath .. "/Bin32" }
132+
configuration {"x64"}
133+
libdirs { vulkanSDKPath .. "/Bin" }
134+
configuration {}
135+
end
136+
if os.is("macosx") then
137+
--no Vulkan on macOs need to error out TODO
138+
elseif os.is("linux") then
139+
libdirs { vulkanSDKPath .. "/lib" }
140+
links { "Anvil",
141+
"vulkan",
142+
"pthread"}
143+
elseif os.is("windows") then
144+
links {"Anvil"}
145+
links {"vulkan-1"}
146+
end
99147
end
100148

101149
configuration {"x32", "Debug"}
102-
targetdir "../../Bin/Debug/x86"
150+
targetdir "../Bin/Debug/x86"
103151
configuration {"x64", "Debug"}
104-
targetdir "../../Bin/Debug/x64"
152+
targetdir "../Bin/Debug/x64"
105153
configuration {"x32", "Release"}
106-
targetdir "../../Bin/Release/x86"
154+
targetdir "../Bin/Release/x86"
107155
configuration {"x64", "Release"}
108-
targetdir "../../Bin/Release/x64"
156+
targetdir "../Bin/Release/x64"
109157
configuration {}

0 commit comments

Comments
 (0)