SDL-OpenCL code working 70% of the time

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

SDL-OpenCL code working 70% of the time

zweifel
Hi,

I programmed a code to do convolution in SDL- OpenCL, however it is working only 70% of the time.

Code :
#include"SDL2/SDL.h"
#include"SDL2/SDL_image.h"
#include"CL_Interface.h"
 
void printFilter(const float* filter, int filter_size)
{
	printf("filter:\n");
	int i, j;
	for(i=0;i<filter_size;++i)
	{
		for(j=0;j<filter_size;++j)
		{
			printf("%.2f ", filter[i*filter_size + j]);
		}
		printf("\n");
	}
 
}
 
 
int main(int argc, char** argv)
{
	bool quit= false;
	int filter_size=4;
 
	SDL_Event event;
 
	SDL_Init(SDL_INIT_VIDEO);
 
	//ATTENTION: 24bit RGB is not supported by OpenCL!	
	SDL_Surface* image= IMG_Load("out.png");
	int image_height=image->h;
	int image_width=image->w;
	int bpp = (int) image->format->BytesPerPixel;
	std::cout << "Bpp " << bpp << "Size "<< image->w << image->h << std::endl;
 
 
	SDL_Window* window= SDL_CreateWindow("Convolution", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 720, 0);
 
	SDL_Renderer* renderer= SDL_CreateRenderer(window, -1, 0);
 
	SDL_Texture* texture= SDL_CreateTextureFromSurface(renderer, image);
 
	printf("Initializing OpenCL...\n");
	//opencl
	CL_Interface* ocl= new CL_Interface();
	ocl->buildProgram("1_test.cl");
	Kernel* kernel= new Kernel("convolute", ocl->program);
 
        //input image (OpenCL have few supported formats!!! No 24bit RGB!)
	const cl::ImageFormat format(CL_RGBA, CL_UNSIGNED_INT8);
 
	cl::Image2D in(ocl->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, format, image_width, image_height, 0, image->pixels);
 
	//float* filter= (float*)malloc(sizeof(float)*filter_size*filter_size);
	float filter[]= 
	{ -1, 0, 1,0,
	  -2, 0, 2,0,
	  -1, 0, 1,0,
	  0, 0, 0,0};
	printFilter(filter, 4);
	cl::Buffer filter_buffer= cl::Buffer(ocl->context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, filter_size*filter_size*sizeof(float), filter);
 
	//cl::Image2D out(ocl->context, CL_MEM_WRITE_ONLY, format, image_width, image_height, 0, data2);
	cl::Image2D out(ocl->context, CL_MEM_WRITE_ONLY, format, image_width, image_height, 0, NULL);
	//cl::Image2D out(ocl->context, CL_MEM_WRITE_ONLY, format, image_width, image_height, 0, image2->pixels);
	kernel->kernel.setArg(0, in);
	kernel->kernel.setArg(1, out);
	kernel->kernel.setArg(2, filter_buffer);
	kernel->kernel.setArg(3, filter_size/2);
 
	ocl->queue.enqueueNDRangeKernel(kernel->kernel, cl::NullRange, cl::NDRange(image_width, image_height), cl::NullRange);
 
	ocl->queue.finish();
 
	//start and end coordinates for reading our image
	cl::size_t<3> origin;
	cl::size_t<3> size;
	origin[0] = 0;
	origin[1] = 0;
	origin[2] = 0;
	size[0] = image_width;
	size[1] = image_height;
 
	size[2] = 1;
 
 
	while(!quit)
	{
		ocl->queue.enqueueReadImage(out, CL_TRUE, origin, size, 0, 0, image->pixels);
 
		//SDL_Texture* texture2= SDL_CreateTextureFromSurface(renderer, image);
		SDL_UpdateTexture(texture, NULL, image->pixels, image->pitch);
 
		SDL_WaitEvent(&event);
 
		switch(event.type)
		{
			case SDL_QUIT:
			{
				quit=true;
			}
			break;
		}
 
		//SDL_RenderCopy(renderer, texture2, NULL, NULL);
		SDL_RenderCopy(renderer, texture, NULL, NULL);
		SDL_RenderPresent(renderer);
	}
 
	SDL_DestroyTexture(texture);
	SDL_FreeSurface(image);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
 
	SDL_Quit();
 
	return 0;
}

Both creating an texture and updating a texture give the same results. 
In other words, inside the code, the four lines below can be toggled without any improvement:
//SDL_Texture* texture2= SDL_CreateTextureFromSurface(renderer, image);
SDL_UpdateTexture(texture, NULL, image->pixels, image->pitch);
//SDL_RenderCopy(renderer, texture2, NULL, NULL); SDL_RenderCopy(renderer, texture, NULL, NULL);

Any ideas where the problem may be?







Best,

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: SDL-OpenCL code working 70% of the time

zweifel
The previous message was sent by email and the formatting makes it terrible to read.
So I am fixing it below.

I programmed a code to do convolution in OpenCL, however it is working only 70% of the time.








Code:

#include"SDL2/SDL.h"
#include"SDL2/SDL_image.h"
#include"CL_Interface.h"
 
void printFilter(const float* filter, int filter_size)
{
   printf("filter:\n");
   int i, j;
   for(i=0;i    {
      for(j=0;j       {
         printf("%.2f ", filter[i*filter_size + j]);
      }
      printf("\n");
   }
 
}
 
 
int main(int argc, char** argv)
{
   bool quit= false;
   int filter_size=4;
 
   SDL_Event event;
 
   SDL_Init(SDL_INIT_VIDEO);
 
   //ATTENTION: 24bit RGB is not supported by OpenCL!   
   SDL_Surface* image= IMG_Load("out.png");
   int image_height=image->h;
   int image_width=image->w;
   int bpp = (int) image->format->BytesPerPixel;
   std::cout << "Bpp " << bpp << "Size "<< image->w << image->h << std::endl;
 
 
   SDL_Window* window= SDL_CreateWindow("Convolution", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 720, 0);
 
   SDL_Renderer* renderer= SDL_CreateRenderer(window, -1, 0);
 
   SDL_Texture* texture= SDL_CreateTextureFromSurface(renderer, image);
 
   printf("Initializing OpenCL...\n");
   //opencl
   CL_Interface* ocl= new CL_Interface();
   ocl->buildProgram("1_test.cl");
   Kernel* kernel= new Kernel("convolute", ocl->program);
 
        //input image (OpenCL have few supported formats!!! No 24bit RGB!)
   const cl::ImageFormat format(CL_RGBA, CL_UNSIGNED_INT8);
 
   cl::Image2D in(ocl->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, format, image_width, image_height, 0, image->pixels);
 
   //float* filter= (float*)malloc(sizeof(float)*filter_size*filter_size);
   float filter[]=
   { -1, 0, 1,0,
     -2, 0, 2,0,
     -1, 0, 1,0,
     0, 0, 0,0};
   printFilter(filter, 4);
   cl::Buffer filter_buffer= cl::Buffer(ocl->context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, filter_size*filter_size*sizeof(float), filter);
 
   //cl::Image2D out(ocl->context, CL_MEM_WRITE_ONLY, format, image_width, image_height, 0, data2);
   cl::Image2D out(ocl->context, CL_MEM_WRITE_ONLY, format, image_width, image_height, 0, NULL);
   //cl::Image2D out(ocl->context, CL_MEM_WRITE_ONLY, format, image_width, image_height, 0, image2->pixels);
   kernel->kernel.setArg(0, in);
   kernel->kernel.setArg(1, out);
   kernel->kernel.setArg(2, filter_buffer);
   kernel->kernel.setArg(3, filter_size/2);
 
   ocl->queue.enqueueNDRangeKernel(kernel->kernel, cl::NullRange, cl::NDRange(image_width, image_height), cl::NullRange);
 
   ocl->queue.finish();
 
   //start and end coordinates for reading our image
   cl::size_t<3> origin;
   cl::size_t<3> size;
   origin[0] = 0;
   origin[1] = 0;
   origin[2] = 0;
   size[0] = image_width;
   size[1] = image_height;
 
   size[2] = 1;
 
 
   while(!quit)
   {
      ocl->queue.enqueueReadImage(out, CL_TRUE, origin, size, 0, 0, image->pixels);
 
      //SDL_Texture* texture2= SDL_CreateTextureFromSurface(renderer, image);
      SDL_UpdateTexture(texture, NULL, image->pixels, image->pitch);
 
      SDL_WaitEvent(&event);
 
      switch(event.type)
      {
         case SDL_QUIT:
         {
            quit=true;
         }
         break;
      }
 
      //SDL_RenderCopy(renderer, texture2, NULL, NULL);
      SDL_RenderCopy(renderer, texture, NULL, NULL);
      SDL_RenderPresent(renderer);
   }
 
   SDL_DestroyTexture(texture);
   SDL_FreeSurface(image);
   SDL_DestroyRenderer(renderer);
   SDL_DestroyWindow(window);
 
   SDL_Quit();
 
   return 0;
}


Any ideas why sometimes I get a nearly blank screen and sometimes I get the filtered image?

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org