#include "system.h"

static Texture* clOutput = 0;
static Shader* shader = 0;
static Kernel* testFunction, *boundsCheck;
static Buffer* outputBuffer, *testData;

bool Game::Init()
{
	// load shader and texture
	clOutput = new Texture( SCRWIDTH, SCRHEIGHT, Texture::FLOAT );
	shader = new Shader( "shaders/vignette.vert", "shaders/vignette.frag" );
	// load OpenCL code
	testFunction = new Kernel( "programs/program.cl", "TestFunction" );
	boundsCheck = new Kernel( "programs/program.cl", "BoundsTest" );
	// link cl output texture as an OpenCL buffer
	outputBuffer = new Buffer( clOutput->GetID(), Buffer::TARGET );
	testFunction->SetArgument( 0, outputBuffer );
	testData = new Buffer( 10000 );
	testData->CopyToDevice();
	boundsCheck->SetArgument( 0, testData );
	// done
	return true;
}

void Game::Tick()
{
	testFunction->Run( outputBuffer );
	boundsCheck->Run( 9999 );
	testData->CopyFromDevice();
	if (testData->GetHostPtr()[9999] == 99999)
	{
		int w = 0;
	}
	shader->Bind();
	shader->SetInputTexture( GL_TEXTURE0, "color", clOutput );
	shader->SetInputMatrix( "view", mat4::Identity() );
	DrawQuad();
}

void Game::Shutdown()
{
}

// EOF