cl::Program *oclKernelCache::ForcedCompile(cl::Context &context, cl::Device &device, const std::string &kernelsParameters, const std::string &kernelSource, cl::STRING_CLASS *error) { cl::Program *program = NULL; try { cl::Program::Sources source(1, std::make_pair(kernelSource.c_str(), kernelSource.length())); program = new cl::Program(context, source); VECTOR_CLASS<cl::Device> buildDevice; buildDevice.push_back(device); program->build(buildDevice, kernelsParameters.c_str()); } catch (cl::Error err) { const std::string clerr = program->getBuildInfo<CL_PROGRAM_BUILD_LOG>(device); std::stringstream ss; ss << "ERROR " << err.what() << "[" << luxrays::oclErrorString(err.err()) << "]:" << std::endl << clerr << std::endl; *error = ss.str(); if (program) delete program; program = NULL; } return program; }
OpenCLBVHKernel(OpenCLIntersectionDevice *dev) : OpenCLKernel(dev), vertsBuff(NULL), trisBuff(NULL), bvhBuff(NULL) { const Context *deviceContext = device->GetContext(); cl::Context &oclContext = device->GetOpenCLContext(); cl::Device &oclDevice = device->GetOpenCLDevice(); const std::string &deviceName(device->GetName()); // Compile sources std::string code( _LUXRAYS_POINT_OCLDEFINE _LUXRAYS_VECTOR_OCLDEFINE _LUXRAYS_RAY_OCLDEFINE _LUXRAYS_RAYHIT_OCLDEFINE _LUXRAYS_TRIANGLE_OCLDEFINE _LUXRAYS_BBOX_OCLDEFINE); code += KernelSource_BVH; cl::Program::Sources source(1, std::make_pair(code.c_str(), code.length())); cl::Program program = cl::Program(oclContext, source); try { VECTOR_CLASS<cl::Device> buildDevice; buildDevice.push_back(oclDevice); program.build(buildDevice); } catch (cl::Error err) { cl::STRING_CLASS strError = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(oclDevice); LR_LOG(deviceContext, "[OpenCL device::" << deviceName << "] BVH compilation error:\n" << strError.c_str()); throw err; } delete kernel; kernel = new cl::Kernel(program, "Intersect"); kernel->getWorkGroupInfo<size_t>(oclDevice, CL_KERNEL_WORK_GROUP_SIZE, &workGroupSize); LR_LOG(deviceContext, "[OpenCL device::" << deviceName << "] BVH kernel work group size: " << workGroupSize); kernel->getWorkGroupInfo<size_t>(oclDevice, CL_KERNEL_WORK_GROUP_SIZE, &workGroupSize); LR_LOG(deviceContext, "[OpenCL device::" << deviceName << "] Suggested work group size: " << workGroupSize); if (device->GetForceWorkGroupSize() > 0) { workGroupSize = device->GetForceWorkGroupSize(); LR_LOG(deviceContext, "[OpenCL device::" << deviceName << "] Forced work group size: " << workGroupSize); } }
cl::Program *oclKernelVolatileCache::Compile(cl::Context &context, cl::Device& device, const std::string &kernelsParameters, const std::string &kernelSource, bool *cached, cl::STRING_CLASS *error) { // Check if the kernel is available in the cache std::map<std::string, cl::Program::Binaries>::iterator it = kernelCache.find(kernelsParameters); if (it == kernelCache.end()) { // It isn't available, compile the source cl::Program *program = ForcedCompile( context, device, kernelsParameters, kernelSource, error); if (!program) return NULL; // Obtain the binaries of the sources VECTOR_CLASS<char *> bins = program->getInfo<CL_PROGRAM_BINARIES>(); assert (bins.size() == 1); VECTOR_CLASS<size_t> sizes = program->getInfo<CL_PROGRAM_BINARY_SIZES>(); assert (sizes.size() == 1); if (sizes[0] > 0) { // Add the kernel to the cache char *bin = new char[sizes[0]]; memcpy(bin, bins[0], sizes[0]); kernels.push_back(bin); kernelCache[kernelsParameters] = cl::Program::Binaries(1, std::make_pair(bin, sizes[0])); } if (cached) *cached = false; return program; } else { // Compile from the binaries VECTOR_CLASS<cl::Device> buildDevice; buildDevice.push_back(device); cl::Program *program = new cl::Program(context, buildDevice, it->second); program->build(buildDevice); if (cached) *cached = true; return program; } }
void SetUpOpenCL() { //---------------------------------------------------------------------- // Compile kernel //---------------------------------------------------------------------- const std::string &kernelFileName = commandLineOpts["kernel"].as<std::string>(); OCLTOY_LOG("Compile OpenCL kernel: " << kernelFileName); // Read the kernel const std::string kernelSource = ReadSources(kernelFileName, "jugCLer"); // Create the kernel program cl::Device &oclDevice = selectedDevices[0]; cl::Context &oclContext = deviceContexts[0]; cl::Program program = cl::Program(oclContext, kernelSource); try { VECTOR_CLASS<cl::Device> buildDevice; buildDevice.push_back(oclDevice); program.build(buildDevice); } catch (cl::Error err) { cl::STRING_CLASS strError = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(oclDevice); OCLTOY_LOG("Kernel compilation error:\n" << strError.c_str()); throw err; } kernelsJugCLer = cl::Kernel(program, "render_gpu"); kernelsJugCLer.getWorkGroupInfo<size_t>(oclDevice, CL_KERNEL_WORK_GROUP_SIZE, &kernelsWorkGroupSize); if (commandLineOpts.count("workgroupsize")) kernelsWorkGroupSize = commandLineOpts["workgroupsize"].as<size_t>(); OCLTOY_LOG("Using workgroup size: " << kernelsWorkGroupSize); //---------------------------------------------------------------------- // Allocate buffer //---------------------------------------------------------------------- AllocateBuffers(); //---------------------------------------------------------------------- // Set kernel arguments //---------------------------------------------------------------------- kernelsJugCLer.setArg(0, *sceneBuff); kernelsJugCLer.setArg(1, *pixelsBuff); }
void OpenCLPixelDevice::CompileKernel(cl::Context &ctx, cl::Device &device, const std::string &src, const char *kernelName, cl::Kernel **kernel) { // Compile sources cl::Program::Sources source(1, std::make_pair(src.c_str(), src.length())); cl::Program program = cl::Program(ctx, source); try { VECTOR_CLASS<cl::Device> buildDevice; buildDevice.push_back(device); program.build(buildDevice, "-I."); } catch (cl::Error err) { cl::STRING_CLASS strError = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device); LR_LOG(deviceContext, "[OpenCL device::" << deviceName << "] " << kernelName << " compilation error:\n" << strError.c_str()); throw err; } *kernel = new cl::Kernel(program, kernelName); }
cl::Program *oclKernelPersistentCache::Compile(cl::Context &context, cl::Device& device, const std::string &kernelsParameters, const std::string &kernelSource, bool *cached, cl::STRING_CLASS *error) { // Check if the kernel is available in the cache cl::Platform platform = device.getInfo<CL_DEVICE_PLATFORM>(); std::string platformName = platform.getInfo<CL_PLATFORM_VENDOR>(); std::string deviceName = device.getInfo<CL_DEVICE_NAME>(); std::string deviceUnits = ToString(device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>()); std::string kernelName = HashString(kernelsParameters) + "-" + HashString(kernelSource) + ".ocl"; std::string dirName = "kernel_cache/" + appName + "/" + platformName + "/" + deviceName + "/" + deviceUnits; std::string fileName = dirName +"/" +kernelName; if (!boost::filesystem::exists(fileName)) { // It isn't available, compile the source cl::Program *program = ForcedCompile( context, device, kernelsParameters, kernelSource, error); if (!program) return NULL; // Obtain the binaries of the sources VECTOR_CLASS<char *> bins = program->getInfo<CL_PROGRAM_BINARIES>(); assert (bins.size() == 1); VECTOR_CLASS<size_t> sizes = program->getInfo<CL_PROGRAM_BINARY_SIZES >(); assert (sizes.size() == 1); // Create the file only if the binaries include something if (sizes[0] > 0) { // Add the kernel to the cache boost::filesystem::create_directories(dirName); BOOST_OFSTREAM file(fileName.c_str(), std::ios_base::out | std::ios_base::binary); file.write(bins[0], sizes[0]); // Check for errors char buf[512]; if (file.fail()) { sprintf(buf, "Unable to write kernel file cache %s", fileName.c_str()); throw std::runtime_error(buf); } file.close(); } if (cached) *cached = false; return program; } else { const size_t kernelSize = boost::filesystem::file_size(fileName); if (kernelSize > 0) { char *kernelBin = new char[kernelSize]; BOOST_IFSTREAM file(fileName.c_str(), std::ios_base::in | std::ios_base::binary); file.read(kernelBin, kernelSize); // Check for errors char buf[512]; if (file.fail()) { sprintf(buf, "Unable to read kernel file cache %s", fileName.c_str()); throw std::runtime_error(buf); } file.close(); // Compile from the binaries VECTOR_CLASS<cl::Device> buildDevice; buildDevice.push_back(device); cl::Program *program = new cl::Program(context, buildDevice, cl::Program::Binaries(1, std::make_pair(kernelBin, kernelSize))); program->build(buildDevice); if (cached) *cached = true; delete[] kernelBin; return program; } else { // Something wrong in the file, remove the file and retry boost::filesystem::remove(fileName); return Compile(context, device, kernelsParameters, kernelSource, cached, error); } } }
bool VNNclAlgorithm::reconstruct(ProcessedUSInputDataPtr input, vtkImageDataPtr outputData, float radius, int nClosePlanes) { mMeasurementNames.clear(); int numBlocks = 10; // FIXME? needs to be the same as the number of input bscans to the voxel_method kernel // Split input US into blocks // Splits and copies data from the processed input in the way the kernel will processes it, which is per frameBlock frameBlock_t* inputBlocks = new frameBlock_t[numBlocks]; size_t nPlanes_numberOfInputImages = input->getDimensions()[2]; this->initializeFrameBlocks(inputBlocks, numBlocks, input); // Allocate CL memory for each frame block VECTOR_CLASS<cl::Buffer> clBlocks; report("Allocating OpenCL input block buffers"); for (int i = 0; i < numBlocks; i++) { //TODO why does the context suddenly contain a "dummy" device? cl::Buffer buffer = mOulContex->createBuffer(mOulContex->getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, inputBlocks[i].length, inputBlocks[i].data, "block buffer "+QString::number(i).toStdString()); clBlocks.push_back(buffer); } // Allocate output memory int *outputDims = outputData->GetDimensions(); size_t outputVolumeSize = outputDims[0] * outputDims[1] * outputDims[2] * sizeof(unsigned char); report(QString("Allocating CL output buffer, size %1").arg(outputVolumeSize)); cl_ulong globalMemUse = 10 * inputBlocks[0].length + outputVolumeSize + sizeof(float) * 16 * nPlanes_numberOfInputImages + sizeof(cl_uchar) * input->getDimensions()[0] * input->getDimensions()[1]; if(isUsingTooMuchMemory(outputVolumeSize, inputBlocks[0].length, globalMemUse)) return false; cl::Buffer outputBuffer = mOulContex->createBuffer(mOulContex->getContext(), CL_MEM_WRITE_ONLY, outputVolumeSize, NULL, "output volume buffer"); // Fill the plane matrices float *planeMatrices = new float[16 * nPlanes_numberOfInputImages]; //4x4 (matrix) = 16 this->fillPlaneMatrices(planeMatrices, input); cl::Buffer clPlaneMatrices = mOulContex->createBuffer(mOulContex->getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, nPlanes_numberOfInputImages * sizeof(float) * 16, planeMatrices, "plane matrices buffer"); // US Probe mask cl::Buffer clMask = mOulContex->createBuffer(mOulContex->getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(cl_uchar) * input->getMask()->GetDimensions()[0] * input->getMask()->GetDimensions()[1], input->getMask()->GetScalarPointer(), "mask buffer"); double *out_spacing = outputData->GetSpacing(); float spacings[2]; float f_out_spacings[3]; f_out_spacings[0] = out_spacing[0]; f_out_spacings[1] = out_spacing[1]; f_out_spacings[2] = out_spacing[2]; spacings[0] = input->getSpacing()[0]; spacings[1] = input->getSpacing()[1]; //TODO why 4? because float4 is used?? size_t planes_eqs_size = sizeof(cl_float)*4*nPlanes_numberOfInputImages; // Find the optimal local work size size_t local_work_size; unsigned int deviceNumber = 0; cl::Device device = mOulContex->getDevice(deviceNumber); mKernel.getWorkGroupInfo(device, CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE, &local_work_size); size_t close_planes_size = this->calculateSpaceNeededForClosePlanes(mKernel, device, local_work_size, nPlanes_numberOfInputImages, nClosePlanes); this->setKernelArguments( mKernel, outputDims[0], outputDims[1], outputDims[2], f_out_spacings[0], f_out_spacings[1], f_out_spacings[2], input->getDimensions()[0], input->getDimensions()[1], spacings[0], spacings[1], clBlocks, outputBuffer, clPlaneMatrices, clMask, planes_eqs_size, close_planes_size, radius); report(QString("Using %1 as local workgroup size").arg(local_work_size)); // We will divide the work into cubes of CUBE_DIM^3 voxels. The global work size is the total number of voxels divided by that. int cube_dim = 4; int cube_dim_pow3 = cube_dim * cube_dim * cube_dim; // Global work items: size_t global_work_size = (((outputDims[0] + cube_dim) * (outputDims[1] + cube_dim) * (outputDims[2] + cube_dim)) / cube_dim_pow3); // = number of cubes = number of kernels to run // Round global_work_size up to nearest multiple of local_work_size if (global_work_size % local_work_size) global_work_size = ((global_work_size / local_work_size) + 1) * local_work_size; // ceil(...) unsigned int queueNumber = 0; cl::CommandQueue queue = mOulContex->getQueue(queueNumber); this->measureAndExecuteKernel(queue, mKernel, global_work_size, local_work_size, mKernelMeasurementName); this->measureAndReadBuffer(queue, outputBuffer, outputVolumeSize, outputData->GetScalarPointer(), "vnncl_read_buffer"); setDeepModified(outputData); // Cleaning up report(QString("Done, freeing GPU memory")); this->freeFrameBlocks(inputBlocks, numBlocks); delete[] inputBlocks; inputBlocks = NULL; return true; }
RenderDevice::RenderDevice(const cl::Device &device, const string &kernelFileName, const unsigned int forceGPUWorkSize, Camera *camera, Sphere *spheres, const unsigned int sceneSphereCount/*, boost::barrier *startBarrier, boost::barrier *endBarrier*/) : /*renderThread(NULL), threadStartBarrier(startBarrier), threadEndBarrier(endBarrier),*/ sphereCount(sceneSphereCount), colorBuffer(NULL), pixelBuffer(NULL), seedBuffer(NULL), pixels(NULL), colors(NULL), seeds(NULL), exeUnitCount(0.0), exeTime(0.0) { deviceName = "anonymouse";//device.getInfo<CL_DEVICE_NAME > ().c_str(); // Allocate a context with the selected device cl::Platform platform = device.getInfo<CL_DEVICE_PLATFORM>(); VECTOR_CLASS<cl::Device> devices; devices.push_back(device); cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform(), 0 }; context = new cl::Context(devices, cps); // Allocate the queue for this device cl_command_queue_properties prop = CL_QUEUE_PROFILING_ENABLE; queue = new cl::CommandQueue(*context, device, prop); // Create the kernel string src = ReadSources(kernelFileName); // Compile sources cl::Program::Sources source(1, make_pair(src.c_str(), src.length())); cl::Program program = cl::Program(*context, source); try { VECTOR_CLASS<cl::Device> buildDevice; buildDevice.push_back(device); #if defined(__EMSCRIPTEN__) program.build(buildDevice, ""); #elif defined(__APPLE__) program.build(buildDevice, "-D__APPLE__"); #else program.build(buildDevice, ""); #endif cl::string result = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device); cerr << "[Device::" << deviceName << "]" << " Compilation result: " << result.c_str() << endl; } catch (cl::Error err) { cl::string strError = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device); cerr << "[Device::" << deviceName << "]" << " Compilation error:" << endl << strError.c_str() << endl; throw err; } kernel = new cl::Kernel(program, "RadianceGPU"); kernel->getWorkGroupInfo<size_t>(device, CL_KERNEL_WORK_GROUP_SIZE, &workGroupSize); cerr << "[Device::" << deviceName << "]" << " Suggested work group size: " << workGroupSize << endl; // Force workgroup size if applicable and required if ((forceGPUWorkSize > 0) && (device.getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_GPU)) { workGroupSize = forceGPUWorkSize; cerr << "[Device::" << deviceName << "]" << " Forced work group size: " << workGroupSize << endl; } // Create the thread for the rendering //renderThread = new boost::thread(boost::bind(RenderDevice::RenderThread, this)); // Create camera buffer cameraBuffer = new cl::Buffer(*context, #if defined (__APPLE__) CL_MEM_READ_ONLY, // CL_MEM_USE_HOST_PTR is very slow with Apple's OpenCL #else CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, #endif sizeof(Camera), camera); cerr << "[Device::" << deviceName << "] Camera buffer size: " << (sizeof(Camera) / 1024) << "Kb" << endl; sphereBuffer = new cl::Buffer(*context, #if defined (__APPLE__) CL_MEM_READ_ONLY, // CL_MEM_USE_HOST_PTR is very slow with Apple's OpenCL #else CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, #endif sizeof(Sphere) * sphereCount, spheres); cerr << "[Device::" << deviceName << "] Scene buffer size: " << (sizeof(Sphere) * sphereCount / 1024) << "Kb" << endl; }
void OCLRendererThread::DrawFrame() { const GameLevel &gameLevel(*(renderer->gameLevel)); const GameConfig &gameConfig(*(gameLevel.gameConfig)); const unsigned int width = gameConfig.GetScreenWidth(); const unsigned int height = gameConfig.GetScreenHeight(); //-------------------------------------------------------------------------- // Merge all the framebuffers if required //-------------------------------------------------------------------------- const size_t threadCount = renderer->renderThread.size(); if (threadCount > 1) { vector<Pixel *> cpuFrameBuffers(threadCount); for (size_t i = 0; i < threadCount; ++i) cpuFrameBuffers[i] = renderer->renderThread[i]->cpuFrameBuffer->GetPixels(); Pixel *dst = cpuFrameBuffers[0]; for (size_t i = 0; i < width * height; ++i) { float r = dst->r; float g = dst->g; float b = dst->b; for (size_t j = 1; j < threadCount; ++j) { r += cpuFrameBuffers[j]->r; g += cpuFrameBuffers[j]->g; b += cpuFrameBuffers[j]->b; cpuFrameBuffers[j] += 1; } dst->r = r; dst->g = g; dst->b = b; ++dst; } cmdQueue->enqueueWriteBuffer(*passFrameBuffer, CL_FALSE, 0, sizeof(Pixel) * width * height, cpuFrameBuffers[0]); } //-------------------------------------------------------------------------- // Blend the new frame with the old one //-------------------------------------------------------------------------- kernelBlendFrame->setArg(2, renderer->blendFactor); cmdQueue->enqueueNDRangeKernel(*kernelBlendFrame, cl::NullRange, cl::NDRange(RoundUp<unsigned int>(width * height, WORKGROUP_SIZE)), cl::NDRange(WORKGROUP_SIZE)); //-------------------------------------------------------------------------- // Tone mapping //-------------------------------------------------------------------------- switch (gameLevel.toneMap->GetType()) { case TONEMAP_REINHARD02: case TONEMAP_LINEAR: cmdQueue->enqueueNDRangeKernel(*kernelToneMapLinear, cl::NullRange, cl::NDRange(RoundUp<unsigned int>(width * height, WORKGROUP_SIZE)), cl::NDRange(WORKGROUP_SIZE)); break; default: assert (false); } //-------------------------------------------------------------------------- // Copy the OpenCL frame buffer to OpenGL one //-------------------------------------------------------------------------- VECTOR_CLASS<cl::Memory> buffs; buffs.push_back(*pboBuff); cmdQueue->enqueueAcquireGLObjects(&buffs); cmdQueue->enqueueNDRangeKernel(*kernelUpdatePixelBuffer, cl::NullRange, cl::NDRange(RoundUp<unsigned int>(width * height, WORKGROUP_SIZE)), cl::NDRange(WORKGROUP_SIZE)); cmdQueue->enqueueReleaseGLObjects(&buffs); cmdQueue->finish(); // Draw the image on the screen glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo); glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); }
OCLRendererThread::OCLRendererThread(const size_t threadIndex, OCLRenderer *renderer, cl::Device device) : index(threadIndex), renderer(renderer), dev(device), usedDeviceMemory(0) { const GameLevel &gameLevel(*(renderer->gameLevel)); const unsigned int width = gameLevel.gameConfig->GetScreenWidth(); const unsigned int height = gameLevel.gameConfig->GetScreenHeight(); const CompiledScene &compiledScene(*(renderer->compiledScene)); if (renderer->renderThread.size() > 1) cpuFrameBuffer = new FrameBuffer(width, height); else cpuFrameBuffer = NULL; //-------------------------------------------------------------------------- // OpenCL setup //-------------------------------------------------------------------------- // Allocate a context with the selected device VECTOR_CLASS<cl::Device> devices; devices.push_back(dev); cl::Platform platform = dev.getInfo<CL_DEVICE_PLATFORM>(); // The first thread uses OpenCL/OpenGL interoperability if (index == 0) { #if defined (__APPLE__) CGLContextObj kCGLContext = CGLGetCurrentContext(); CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext); cl_context_properties cps[] = { CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup, 0 }; #else #ifdef WIN32 cl_context_properties cps[] = { CL_GL_CONTEXT_KHR, (intptr_t)wglGetCurrentContext(), CL_WGL_HDC_KHR, (intptr_t)wglGetCurrentDC(), CL_CONTEXT_PLATFORM, (cl_context_properties)platform(), 0 }; #else cl_context_properties cps[] = { CL_GL_CONTEXT_KHR, (intptr_t)glXGetCurrentContext(), CL_GLX_DISPLAY_KHR, (intptr_t)glXGetCurrentDisplay(), CL_CONTEXT_PLATFORM, (cl_context_properties)platform(), 0 }; #endif #endif ctx = new cl::Context(devices, cps); } else ctx = new cl::Context(devices); // Allocate the queue for this device cmdQueue = new cl::CommandQueue(*ctx, dev); //-------------------------------------------------------------------------- // Allocate the buffers //-------------------------------------------------------------------------- passFrameBuffer = NULL; tmpFrameBuffer = NULL; frameBuffer = NULL; toneMapFrameBuffer = NULL; bvhBuffer = NULL; gpuTaskBuffer = NULL; cameraBuffer = NULL; infiniteLightBuffer = NULL; matBuffer = NULL; matIndexBuffer = NULL; texMapBuffer = NULL; texMapRGBBuffer = NULL; texMapInstanceBuffer = NULL; bumpMapInstanceBuffer = NULL; AllocOCLBufferRW(&passFrameBuffer, sizeof(Pixel) * width * height, "Pass FrameBuffer"); AllocOCLBufferRW(&tmpFrameBuffer, sizeof(Pixel) * width * height, "Temporary FrameBuffer"); if (index == 0) { AllocOCLBufferRW(&frameBuffer, sizeof(Pixel) * width * height, "FrameBuffer"); AllocOCLBufferRW(&toneMapFrameBuffer, sizeof(Pixel) * width * height, "ToneMap FrameBuffer"); } AllocOCLBufferRW(&gpuTaskBuffer, sizeof(ocl_kernels::GPUTask) * width * height, "GPUTask"); AllocOCLBufferRO(&cameraBuffer, sizeof(compiledscene::Camera), "Camera"); AllocOCLBufferRO(&infiniteLightBuffer, (void *)(gameLevel.scene->infiniteLight->GetTexture()->GetTexMap()->GetPixels()), sizeof(Spectrum) * gameLevel.scene->infiniteLight->GetTexture()->GetTexMap()->GetWidth() * gameLevel.scene->infiniteLight->GetTexture()->GetTexMap()->GetHeight(), "Inifinite Light"); AllocOCLBufferRO(&matBuffer, (void *)(&compiledScene.mats[0]), sizeof(compiledscene::Material) * compiledScene.mats.size(), "Materials"); AllocOCLBufferRO(&matIndexBuffer, (void *)(&compiledScene.sphereMats[0]), sizeof(unsigned int) * compiledScene.sphereMats.size(), "Material Indices"); if (compiledScene.texMaps.size() > 0) { AllocOCLBufferRO(&texMapBuffer, (void *)(&compiledScene.texMaps[0]), sizeof(compiledscene::TexMap) * compiledScene.texMaps.size(), "Texture Maps"); AllocOCLBufferRO(&texMapRGBBuffer, (void *)(compiledScene.rgbTexMem), sizeof(Spectrum) * compiledScene.totRGBTexMem, "Texture Map Images"); AllocOCLBufferRO(&texMapInstanceBuffer, (void *)(&compiledScene.sphereTexs[0]), sizeof(compiledscene::TexMapInstance) * compiledScene.sphereTexs.size(), "Texture Map Instances"); if (compiledScene.sphereBumps.size() > 0) AllocOCLBufferRO(&bumpMapInstanceBuffer, (void *)(&compiledScene.sphereBumps[0]), sizeof(compiledscene::BumpMapInstance) * compiledScene.sphereBumps.size(), "Bump Map Instances"); } SFERA_LOG("[OCLRenderer] Total OpenCL device memory used: " << fixed << setprecision(2) << usedDeviceMemory / (1024 * 1024) << "Mbytes"); if (index == 0) { //-------------------------------------------------------------------------- // Create pixel buffer object for display //-------------------------------------------------------------------------- glGenBuffersARB(1, &pbo); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo); glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * sizeof(GLubyte) * 4, 0, GL_STREAM_DRAW_ARB); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); pboBuff = new cl::BufferGL(*ctx, CL_MEM_READ_WRITE, pbo); } //-------------------------------------------------------------------------- // Compile the kernel source //-------------------------------------------------------------------------- // Set #define symbols stringstream ss; ss.precision(6); ss << scientific << " -D PARAM_SCREEN_WIDTH=" << width << " -D PARAM_SCREEN_HEIGHT=" << height << " -D PARAM_SCREEN_SAMPLEPERPASS="******" -D PARAM_RAY_EPSILON=" << EPSILON << "f" << " -D PARAM_MAX_DIFFUSE_BOUNCE=" << gameLevel.maxPathDiffuseBounces << " -D PARAM_MAX_SPECULARGLOSSY_BOUNCE=" << gameLevel.maxPathSpecularGlossyBounces << " -D PARAM_IL_SHIFT_U=" << gameLevel.scene->infiniteLight->GetShiftU() << "f" << " -D PARAM_IL_SHIFT_V=" << gameLevel.scene->infiniteLight->GetShiftV() << "f" << " -D PARAM_IL_GAIN_R=" << gameLevel.scene->infiniteLight->GetGain().r << "f" << " -D PARAM_IL_GAIN_G=" << gameLevel.scene->infiniteLight->GetGain().g << "f" << " -D PARAM_IL_GAIN_B=" << gameLevel.scene->infiniteLight->GetGain().b << "f" << " -D PARAM_IL_MAP_WIDTH=" << gameLevel.scene->infiniteLight->GetTexture()->GetTexMap()->GetWidth() << " -D PARAM_IL_MAP_HEIGHT=" << gameLevel.scene->infiniteLight->GetTexture()->GetTexMap()->GetHeight() << " -D PARAM_GAMMA=" << gameLevel.toneMap->GetGamma() << "f" << " -D PARAM_MEM_TYPE=" << gameLevel.gameConfig->GetOpenCLMemType(); if (compiledScene.enable_MAT_MATTE) ss << " -D PARAM_ENABLE_MAT_MATTE"; if (compiledScene.enable_MAT_MIRROR) ss << " -D PARAM_ENABLE_MAT_MIRROR"; if (compiledScene.enable_MAT_GLASS) ss << " -D PARAM_ENABLE_MAT_GLASS"; if (compiledScene.enable_MAT_METAL) ss << " -D PARAM_ENABLE_MAT_METAL"; if (compiledScene.enable_MAT_ALLOY) ss << " -D PARAM_ENABLE_MAT_ALLOY"; if (texMapBuffer) { ss << " -D PARAM_HAS_TEXTUREMAPS"; if (compiledScene.sphereBumps.size() > 0) ss << " -D PARAM_HAS_BUMPMAPS"; } switch (gameLevel.toneMap->GetType()) { case TONEMAP_REINHARD02: ss << " -D PARAM_TM_LINEAR_SCALE=1.0f"; break; case TONEMAP_LINEAR: { LinearToneMap *tm = (LinearToneMap *)gameLevel.toneMap; ss << " -D PARAM_TM_LINEAR_SCALE=" << tm->scale << "f"; break; } default: assert (false); } #if defined(__APPLE__) ss << " -D __APPLE__"; #endif SFERA_LOG("[OCLRenderer] Defined symbols: " << ss.str()); SFERA_LOG("[OCLRenderer] Compiling kernels"); cl::Program::Sources source(1, std::make_pair(KernelSource_kernel_core.c_str(), KernelSource_kernel_core.length())); cl::Program program = cl::Program(*ctx, source); try { VECTOR_CLASS<cl::Device> buildDevice; buildDevice.push_back(dev); program.build(buildDevice, ss.str().c_str()); } catch (cl::Error err) { cl::STRING_CLASS strError = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(dev); SFERA_LOG("[OCLRenderer] Kernel compilation error:\n" << strError.c_str()); throw err; } kernelInit = new cl::Kernel(program, "Init"); kernelInit->setArg(0, *gpuTaskBuffer); cmdQueue->enqueueNDRangeKernel(*kernelInit, cl::NullRange, cl::NDRange(RoundUp<unsigned int>(width * height, WORKGROUP_SIZE)), cl::NDRange(WORKGROUP_SIZE)); kernelInitFrameBuffer = new cl::Kernel(program, "InitFB"); if (index == 0) { kernelInitFrameBuffer->setArg(0, *frameBuffer); cmdQueue->enqueueNDRangeKernel(*kernelInitFrameBuffer, cl::NullRange, cl::NDRange(RoundUp<unsigned int>(width * height, WORKGROUP_SIZE)), cl::NDRange(WORKGROUP_SIZE)); } kernelInitFrameBuffer->setArg(0, *passFrameBuffer); kernelPathTracing = new cl::Kernel(program, "PathTracing"); unsigned int argIndex = 0; kernelPathTracing->setArg(argIndex++, *gpuTaskBuffer); argIndex++; kernelPathTracing->setArg(argIndex++, *cameraBuffer); kernelPathTracing->setArg(argIndex++, *infiniteLightBuffer); kernelPathTracing->setArg(argIndex++, *passFrameBuffer); kernelPathTracing->setArg(argIndex++, *matBuffer); kernelPathTracing->setArg(argIndex++, *matIndexBuffer); if (texMapBuffer) { kernelPathTracing->setArg(argIndex++, *texMapBuffer); kernelPathTracing->setArg(argIndex++, *texMapRGBBuffer); kernelPathTracing->setArg(argIndex++, *texMapInstanceBuffer); if (compiledScene.sphereBumps.size() > 0) kernelPathTracing->setArg(argIndex++, *bumpMapInstanceBuffer); } kernelApplyBlurLightFilterXR1 = new cl::Kernel(program, "ApplyBlurLightFilterXR1"); kernelApplyBlurLightFilterXR1->setArg(0, *passFrameBuffer); kernelApplyBlurLightFilterXR1->setArg(1, *tmpFrameBuffer); kernelApplyBlurLightFilterYR1 = new cl::Kernel(program, "ApplyBlurLightFilterYR1"); kernelApplyBlurLightFilterYR1->setArg(0, *tmpFrameBuffer); kernelApplyBlurLightFilterYR1->setArg(1, *passFrameBuffer); kernelApplyBlurHeavyFilterXR1 = new cl::Kernel(program, "ApplyBlurHeavyFilterXR1"); kernelApplyBlurHeavyFilterXR1->setArg(0, *passFrameBuffer); kernelApplyBlurHeavyFilterXR1->setArg(1, *tmpFrameBuffer); kernelApplyBlurHeavyFilterYR1 = new cl::Kernel(program, "ApplyBlurHeavyFilterYR1"); kernelApplyBlurHeavyFilterYR1->setArg(0, *tmpFrameBuffer); kernelApplyBlurHeavyFilterYR1->setArg(1, *passFrameBuffer); kernelApplyBoxFilterXR1 = new cl::Kernel(program, "ApplyBoxFilterXR1"); kernelApplyBoxFilterXR1->setArg(0, *passFrameBuffer); kernelApplyBoxFilterXR1->setArg(1, *tmpFrameBuffer); kernelApplyBoxFilterYR1 = new cl::Kernel(program, "ApplyBoxFilterYR1"); kernelApplyBoxFilterYR1->setArg(0, *tmpFrameBuffer); kernelApplyBoxFilterYR1->setArg(1, *passFrameBuffer); if (index == 0) { kernelBlendFrame = new cl::Kernel(program, "BlendFrame"); kernelBlendFrame->setArg(0, *passFrameBuffer); kernelBlendFrame->setArg(1, *frameBuffer); kernelToneMapLinear = new cl::Kernel(program, "ToneMapLinear"); kernelToneMapLinear->setArg(0, *frameBuffer); kernelToneMapLinear->setArg(1, *toneMapFrameBuffer); kernelUpdatePixelBuffer = new cl::Kernel(program, "UpdatePixelBuffer"); kernelUpdatePixelBuffer->setArg(0, *toneMapFrameBuffer); kernelUpdatePixelBuffer->setArg(1, *pboBuff); } else { kernelBlendFrame = NULL; kernelToneMapLinear = NULL; kernelUpdatePixelBuffer = NULL; } }