Exemplo n.º 1
0
void NoneLocalMeans::recompileOpenCLCode(Image::pointer input) {
    // Check if there is a need to recompile OpenCL code
    if(input->getDimensions() == mDimensionCLCodeCompiledFor &&
       input->getDataType() == mTypeCLCodeCompiledFor && !recompile)
        return;
    
    recompile = false;
    OpenCLDevice::pointer device = getMainDevice();
    std::string buildOptions = "";
    if(!device->isWritingTo3DTexturesSupported()) {
        buildOptions = "-DTYPE=" + getCTypeAsString(mOutputType);
    }
    buildOptions += " -D WINDOW=";
    buildOptions += std::to_string((windowSize-1)/2);
    buildOptions += " -D GROUP=";
    buildOptions += std::to_string((groupSize-1)/2);
    buildOptions += " -D KVERSION=";
    buildOptions += std::to_string(k);
    buildOptions += " -D EUCLID=";
    buildOptions += std::to_string(k);
    cl::Program program;
    if(input->getDimensions() == 2) {
        program = getOpenCLProgram(device, "2D", buildOptions);
    } else {
        //createOpenCLProgram(std::string(FAST_SOURCE_DIR) + "Algorithms/NoneLocalMeans/NoneLocalMeans3Dgs.cl", "3D");
        program = getOpenCLProgram(device, "3D", buildOptions);
    }
    mKernel = cl::Kernel(program, "noneLocalMeans");
    mDimensionCLCodeCompiledFor = input->getDimensions();
    mTypeCLCodeCompiledFor = input->getDataType();
}
Exemplo n.º 2
0
void Dilation::execute() {
    Image::pointer input = getInputData<Image>();
    if(input->getDataType() != TYPE_UINT8) {
        throw Exception("Data type of image given to Dilation must be UINT8");
    }

    Image::pointer output = getOutputData<Image>();
    output->createFromImage(input);
    SceneGraph::setParentNode(output, input);
    output->fill(0);

    OpenCLDevice::pointer device = std::dynamic_pointer_cast<OpenCLDevice>(getMainDevice());
    cl::CommandQueue queue = device->getCommandQueue();
    cl::Program program = getOpenCLProgram(device);
    cl::Kernel dilateKernel(program, "dilate");

    Vector3ui size = input->getSize();

    OpenCLImageAccess::pointer access = input->getOpenCLImageAccess(ACCESS_READ, device);
    dilateKernel.setArg(0, *access->get3DImage());
    dilateKernel.setArg(2, mSize/2);

    if(!device->isWritingTo3DTexturesSupported()) {
        OpenCLBufferAccess::pointer access2 = output->getOpenCLBufferAccess(ACCESS_READ_WRITE, device);
        dilateKernel.setArg(1, *access2->get());

        queue.enqueueNDRangeKernel(
            dilateKernel,
            cl::NullRange,
            cl::NDRange(size.x(), size.y(), size.z()),
            cl::NullRange
        );
    } else {
        OpenCLImageAccess::pointer access2 = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
        dilateKernel.setArg(1, *access2->get3DImage());

        queue.enqueueNDRangeKernel(
            dilateKernel,
            cl::NullRange,
            cl::NDRange(size.x(), size.y(), size.z()),
            cl::NullRange
        );
    }

}
Exemplo n.º 3
0
/*
void NoneLocalMeans::recompileOpenCLCode(Image::pointer input) {
	// Check if there is a need to recompile OpenCL code
	if (input->getDimensions() == mDimensionCLCodeCompiledFor &&
		input->getDataType() == mTypeCLCodeCompiledFor && !recompile)
		return;

	OpenCLDevice::pointer device = getMainDevice();
    recompile = false;
	std::string buildOptions = "";
	const bool writingTo3DTextures = device->getDevice().getInfo<CL_DEVICE_EXTENSIONS>().find("cl_khr_3d_image_writes") != std::string::npos;
	if (!writingTo3DTextures) {
		switch (mOutputType) {
		case TYPE_FLOAT:
			buildOptions += " -DTYPE=float";
			break;
		case TYPE_INT8:
			buildOptions += " -DTYPE=char";
			break;
		case TYPE_UINT8:
			buildOptions += " -DTYPE=uchar";
			break;
		case TYPE_INT16:
			buildOptions += " -DTYPE=short";
			break;
		case TYPE_UINT16:
			buildOptions += " -DTYPE=ushort";
			break;
		}
	}
    buildOptions += " -D WINDOW=";
    buildOptions += std::to_string((windowSize-1)/2);
	buildOptions += " -D GROUP=";
    buildOptions += std::to_string((groupSize-1)/2);
    
	std::string filename;
	//might have to seperate color vs gray here, for better runtime
	if (input->getDimensions() == 2) {
        if(k == 0){
            filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2Dconstant.cl";
        }else if(k == 1){
            filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2Dgaussian.cl";
        }else{
            filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2Dconstant.cl";
        }
		//filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2DgsPixelWise.cl";
		//filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2Dgs.cl";
        //filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2Dc.cl";
	}
	else {
		filename = "Algorithms/NoneLocalMeans/NoneLocalMeans3Dgs.cl";
	}
	int programNr = device->createProgramFromSource(std::string(FAST_SOURCE_DIR) + filename, buildOptions);
	mKernel = cl::Kernel(device->getProgram(programNr), "noneLocalMeans");
	mDimensionCLCodeCompiledFor = input->getDimensions();
	mTypeCLCodeCompiledFor = input->getDataType();
}*/
void NoneLocalMeans::execute() {
    Image::pointer input = getStaticInputData<Image>(0);
    Image::pointer output = getStaticOutputData<Image>(0);
    
    // Initialize output image
    ExecutionDevice::pointer device = getMainDevice();
    if(mOutputTypeSet) {
        output->create(input->getSize(), mOutputType, input->getNrOfComponents());
        output->setSpacing(input->getSpacing());
    } else {
        output->createFromImage(input);
    }
    mOutputType = output->getDataType();
    SceneGraph::setParentNode(output, input);
    
    
    if(device->isHost()) {
        switch(input->getDataType()) {
                fastSwitchTypeMacro(executeAlgorithmOnHost<FAST_TYPE>(input, output, groupSize, windowSize, denoiseStrength, sigma));
        }
    } else {
        OpenCLDevice::pointer clDevice = device;
        
        recompileOpenCLCode(input);
        
        cl::NDRange globalSize;
        
        OpenCLImageAccess::pointer inputAccess = input->getOpenCLImageAccess(ACCESS_READ, device);
        if(input->getDimensions() == 2) {
            OpenCLImageAccess::pointer outputAccess = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
            mKernel.setArg(2, (denoiseStrength*denoiseStrength));
            mKernel.setArg(3, (sigma*sigma));
            globalSize = cl::NDRange(input->getWidth(),input->getHeight());
            mKernel.setArg(0, *inputAccess->get2DImage());
            mKernel.setArg(1, *outputAccess->get2DImage());
            clDevice->getCommandQueue().enqueueNDRangeKernel(
                    mKernel,
                    cl::NullRange,
                    globalSize,
                    cl::NullRange
            );
        } else {
            // Create an auxilliary image
            //Image::pointer output2 = Image::New();
            //output2->createFromImage(output);
            
            globalSize = cl::NDRange(input->getWidth(),input->getHeight(),input->getDepth());
            
            if(clDevice->isWritingTo3DTexturesSupported()) {
                mKernel.setArg(2, (denoiseStrength*denoiseStrength));
                mKernel.setArg(3, (sigma*sigma));
                OpenCLImageAccess::pointer outputAccess = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
                //OpenCLImageAccess::pointer outputAccess2 = output2->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
                
                //cl::Image3D* image2;
                cl::Image3D* image;
                image = outputAccess->get3DImage();
                //image2 = outputAccess->get3DImage();
                mKernel.setArg(0, *inputAccess->get3DImage());
                mKernel.setArg(1, *image);
                clDevice->getCommandQueue().enqueueNDRangeKernel(
                        mKernel,
                        cl::NullRange,
                        globalSize,
                        cl::NullRange
                );
            }else{
                mKernel.setArg(2, (denoiseStrength*denoiseStrength));
                mKernel.setArg(3, (sigma*sigma));
                OpenCLBufferAccess::pointer outputAccess = output->getOpenCLBufferAccess(ACCESS_READ_WRITE, device);
                mKernel.setArg(0, *inputAccess->get3DImage());
                mKernel.setArg(1, *outputAccess->get());
                clDevice->getCommandQueue().enqueueNDRangeKernel(
                        mKernel,
                        cl::NullRange,
                        globalSize,
                        cl::NullRange
                );
            }
            
            
        }
    }
}