Example #1
0
/**
 * @fn Initialize()
 * @brief Setup the simulation.
 */ 
void Initialize()
{
  // Initialize the UI
  InitParameters();
  
  // First initialize extensions.
  int err = glewInit();

  if (GLEW_OK != err)
  {
    /* problem: glewInit failed, something is seriously wrong */
    fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    exit(-1);
  }

  // Create the Cg context
  cgSetErrorCallback(cgErrorCallback);

  // Create cgContext.
  g_cgContext = cgCreateContext();
  
  // Create and initialize the Flo simulator object
  g_pFlo = new Flo(g_iFloWidth, g_iFloHeight);
  g_pFlo->Initialize(g_cgContext);

  glDisable(GL_DEPTH_TEST);
  glDisable(GL_LIGHTING);
}
Example #2
0
void
CGlutCgWindow::initializeCg() {

	// Basic Cg setup; register a callback function for any errors
    // and create an initial context
    cgSetErrorCallback(handleCgError);
    s_CgContext = cgCreateContext();

}
Example #3
0
GPUProgram::GPUProgram(void)
{
    cgSetErrorCallback(handleCgError);
    context = cgCreateContext();
    if (!cgIsContext(context)){
		fprintf(stderr, "Cg Error: cannot create context!\n");
		exit(1);
	}
	_chooseProfiles();
}
void init( unsigned int _width, unsigned int _height ) {
  window_width = _width;
  window_height = _height;
  glfwInit();
  glfwOpenWindowHint( GLFW_FSAA_SAMPLES, 8 );
  glfwOpenWindow( window_width, window_height, 8, 8, 8, 8, 16, 0, GLFW_WINDOW );
  glfwSetWindowSizeCallback( &resizeEvent );
  std::cout << glGetString( GL_VERSION ) << std::endl;
  glfwSwapInterval( 1 );
  
  GLenum err = glewInit();
  if (GLEW_OK != err)
  {
    std::cout << "Error: " << glewGetErrorString(err) << std::endl;
    throw -1;
  }
  
//  ilInit();
//  iluInit();
  
  chdir( BOOST_PP_STRINGIZE( RESOURCE_DIRECTORY ) );
  
  cgSetErrorCallback(&checkCgError);
  context = cgCreateContext();
  cgGLRegisterStates( context );
  quit_flag = false;
  cam_position[ 0 ] = 0.0f;
  cam_position[ 1 ] = 3.5f;
  cam_position[ 2 ] = 7.0f;
  cam_velocity[ 0 ] = 0.0f;
  cam_velocity[ 1 ] = 0.0f;
  cam_velocity[ 2 ] = 0.0f;
  cam_velocity[ 3 ] = 0.0f;
  cam_direction[ 0 ] = 0.0f;
  cam_direction[ 1 ] = 0.0f;
  cam_direction[ 2 ] = -1.0f;
  light_position = 0.0;
  light_velocity = 0.0;
  obj_position[ 0 ] = 0.0f;
  obj_position[ 1 ] = 0.0f;
  obj_velocity[ 0 ] = 0.0f;
  obj_velocity[ 1 ] = 0.0f;
  cam_moving_x = 0;
  cam_moving_y = 0;
  cam_moving_z = 0;
  cam_moving_dir = 0;
  lit_moving_x = 0;
  obj_moving_x = 0;
  obj_moving_y = 0;
  glfwSetKeyCallback( &keycb );
}
Example #5
0
int _tmain(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(500, 500);
    glutCreateWindow("render_texture_3D");
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutMotionFunc(myMotion);
    glutPassiveMotionFunc(myPMotion);
    glutIdleFunc(idle);
    context = cgCreateContext();
    cgSetErrorCallback(cgErrorCallback);
    fluid=new Fluid3D(context,64,64,64,0.0025f,1,0.5);
    glutMainLoop();
}
Example #6
0
int createShader ( int n, std::string vname, std::string vfunc, std::string fname, std::string ffunc)
{
#if 0
	char vnbuf[200];
	char vfbuf[200];
	char fnbuf[200];
	char ffbuf[200];
	strcpy ( vnbuf, vname.c_str() );
	strcpy ( vfbuf, vfunc.c_str() );
	strcpy ( fnbuf, fname.c_str() );
	strcpy ( ffbuf, ffunc.c_str() );

	if ( cgContext == 0 ) {
		cgSetErrorCallback( cgErrorCallback );
		cgContext = cgCreateContext();
	}

	// Select profiles
	vert_profile = cgGLGetLatestProfile ( CG_GL_VERTEX );
	cgGLSetOptimalOptions( vert_profile );
	
	frag_profile = cgGLGetLatestProfile ( CG_GL_FRAGMENT );
	cgGLSetOptimalOptions( frag_profile );

	printf ( "Vertex profile:   %s\n", cgGetProfileString(vert_profile) );
	printf ( "Fragment profile: %s\n", cgGetProfileString(frag_profile) );
	printf ( " (See http.developer.nvidia.com/Cg/index_profiles.html)\n");

	//debug.PrintF ( "rend", "Geometry profile: %s\n", cgGetProfileString(vert_profile) );	

	printf ( "Loading VP:       %s\n", vnbuf );
	cgVP = cgCreateProgramFromFile( cgContext, CG_SOURCE, vnbuf, vert_profile, vfbuf, NULL );
	cgGLLoadProgram( cgVP );
	
	printf ( "Loading FP:       %s\n", fnbuf );
	cgFP = cgCreateProgramFromFile( cgContext, CG_SOURCE, fnbuf, frag_profile, ffbuf, NULL );	
	cgGLLoadProgram( cgFP );
	
	cgGLSetManageTextureParameters ( cgContext, CG_FALSE ); 
	cgGLBindProgram ( cgVP );
	cgGLBindProgram ( cgFP );	
#endif
	return 0;
}
Example #7
0
void Sum::init_cg() {
  cgProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT) ;
  cgContext = cgCreateContext();
  errContext = cgContext;
  cgSetErrorCallback(cgErrorCallbackSum);
  
  
  fragmentProgram4tap = cgCreateProgramFromFile( cgContext, 
                                             CG_SOURCE, "FP-4tap.cg", 
                                             cgProfile, "FragmentProgram", 0);
 
  fragmentProgram8tap= cgCreateProgramFromFile( cgContext, 
                                             CG_SOURCE, "FP-8tap.cg", 
                                             cgProfile, "FragmentProgram", 0);
  fragmentProgram8tapY= cgCreateProgramFromFile( cgContext, 
                                             CG_SOURCE, "FP-8tapY.cg", 
                                             cgProfile, "FragmentProgram", 0);
  cgGLLoadProgram( fragmentProgram4tap );
  cgGLLoadProgram( fragmentProgram8tap );
  cgGLLoadProgram( fragmentProgram8tapY);
}
Example #8
0
    void GLRenderer::Initialise()
    {
        RenderState rs;
        rs.state = RenderState::None;
        rs.matrixMode = -1;
        rs.target = 0;
        this->_PushState(rs);

        GLenum error = glewInit();
        if (error != GLEW_OK)
            throw std::runtime_error(ToString("glewInit() failed: ") + ToString(glewGetErrorString(error)));
        if (!(GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader))
            throw std::runtime_error("ARB_vertex_shader and ARB_fragment_shader are required");

        Tools::log << "Glew version: " << (char const*)glewGetString(GLEW_VERSION) << "\n";
        Tools::log << "OpenGL version: " << (char const*)glGetString(GL_VERSION) << "\n";

        GLCHECK(glEnable(GL_BLEND));
        //GLCHECK(glEnable(GL_ALPHA_TEST));
        GLCHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
        //GLCHECK(glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA));

        if (_cgGlobalContext == 0)
            _cgGlobalContext = cgCreateContext();
        this->_cgContext = _cgGlobalContext;
        ++_cgGlobalNbReferences;
        cgSetErrorCallback(&ErrCallback);
        cgGLSetDebugMode(CG_FALSE);
        cgSetParameterSettingMode(this->_cgContext, CG_DEFERRED_PARAMETER_SETTING);
        cgGLRegisterStates(this->_cgContext);
        cgGLSetManageTextureParameters(this->_cgContext, CG_TRUE);

        InitDevIL();

        GLCHECK(glCullFace(GL_FRONT));
    }
Example #9
0
void InitCg()
{
	cgSetErrorCallback(CgErrorCallback);
	Context = cgCreateContext();
	VertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
	printf("VertexProfile %s\n", cgGetProfileString(VertexProfile));
	cgGLSetOptimalOptions(VertexProfile);
	FragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
	printf("FragmentProfile %s\n", cgGetProfileString(FragmentProfile));
	cgGLSetOptimalOptions(FragmentProfile);

	VP_DrawSites = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_DrawSites.cg",
							VertexProfile,
							NULL, NULL);
	FP_DrawSites = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_DrawSites.cg",
							FragmentProfile,
							NULL, NULL);
	VP_Flood = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_Flood.cg",
							VertexProfile,
							NULL, NULL);
	FP_Flood = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_Flood.cg",
							FragmentProfile,
							NULL, NULL);
	VP_Scatter = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_Scatter.cg",
							VertexProfile,
							NULL, NULL);
	FP_Scatter = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_Scatter.cg",
							FragmentProfile,
							NULL, NULL);
	VP_DrawNewSites = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_DrawNewSites.cg",
							VertexProfile,
							NULL, NULL);
	FP_DrawNewSites = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_DrawNewSites.cg",
							FragmentProfile,
							NULL, NULL);
	VP_DrawSitesOQ = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_DrawSitesOQ.cg",
							VertexProfile,
							NULL, NULL);
	FP_DrawSitesOQ = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_DrawSitesOQ.cg",
							FragmentProfile,
							NULL, NULL);
	VP_FinalRender = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_FinalRender.cg",
							VertexProfile,
							NULL, NULL);
	FP_FinalRender = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_FinalRender.cg",
							FragmentProfile,
							NULL, NULL);
	VP_ComputeEnergy = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_ComputeEnergy.cg",
							VertexProfile,
							NULL, NULL);
	FP_ComputeEnergy = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_ComputeEnergy.cg",
							FragmentProfile,
							NULL, NULL);
	VP_Deduction = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_Deduction.cg",
							VertexProfile,
							NULL, NULL);
	FP_Deduction = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_Deduction.cg",
							FragmentProfile,
							NULL, NULL);
	VP_ComputeEnergyCentroid = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_ComputeEnergyCentroid.cg",
							VertexProfile,
							NULL, NULL);
	FP_ComputeEnergyCentroid = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_ComputeEnergyCentroid.cg",
							FragmentProfile,
							NULL, NULL);
	VP_ScatterCentroid = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_ScatterCentroid.cg",
							VertexProfile,
							NULL, NULL);
	FP_ScatterCentroid = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_ScatterCentroid.cg",
							FragmentProfile,
							NULL, NULL);

	if(VP_DrawSites != NULL)
	{
		cgGLLoadProgram(VP_DrawSites);
	}
	if(FP_DrawSites != NULL)
	{
		cgGLLoadProgram(FP_DrawSites);
	}

	if(VP_Flood != NULL)
	{
		cgGLLoadProgram(VP_Flood);

		// Bind parameters to give access to variables in the shader
		VP_Flood_Steplength = cgGetNamedParameter(VP_Flood, "steplength");
		VP_Flood_Size = cgGetNamedParameter(VP_Flood, "size");
	}
	if(FP_Flood != NULL)
	{
		cgGLLoadProgram(FP_Flood);
	}

	if(VP_Scatter != NULL)
	{
		cgGLLoadProgram(VP_Scatter);

		// Bind parameters to give access to variables in the shader
		VP_Scatter_Size = cgGetNamedParameter(VP_Scatter, "size");
	}
	if(FP_Scatter != NULL)
	{
		cgGLLoadProgram(FP_Scatter);
	}

	if(VP_DrawNewSites != NULL)
	{
		cgGLLoadProgram(VP_DrawNewSites);

		// Bind parameters to give access to variables in the shader
		VP_DrawNewSites_Size = cgGetNamedParameter(VP_DrawNewSites, "size");
	}
	if(FP_DrawNewSites != NULL)
	{
		cgGLLoadProgram(FP_DrawNewSites);
	}

	if(VP_DrawSitesOQ != NULL)
	{
		cgGLLoadProgram(VP_DrawSitesOQ);

		// Bind parameters to give access to variables in the shader
		VP_DrawSitesOQ_Size = cgGetNamedParameter(VP_DrawSitesOQ, "size");
	}
	if(FP_DrawSitesOQ != NULL)
	{
		cgGLLoadProgram(FP_DrawSitesOQ);
	}

	if(VP_FinalRender != NULL)
	{
		cgGLLoadProgram(VP_FinalRender);
	}
	if(FP_FinalRender != NULL)
	{
		cgGLLoadProgram(FP_FinalRender);

		// Bind parameters to give access to variables in the shader
		FP_FinalRender_Size = cgGetNamedParameter(FP_FinalRender, "size");
	}

	if(VP_ComputeEnergy != NULL)
	{
		cgGLLoadProgram(VP_ComputeEnergy);
	}
	if(FP_ComputeEnergy != NULL)
	{
		cgGLLoadProgram(FP_ComputeEnergy);

		// Bind parameters to give access to variables in the shader
		FP_ComputeEnergy_Size = cgGetNamedParameter(FP_ComputeEnergy, "size");
	}

	if(VP_Deduction != NULL)
	{
		cgGLLoadProgram(VP_Deduction);
	}
	if(FP_Deduction != NULL)
	{
		cgGLLoadProgram(FP_Deduction);
	}

	if(VP_ComputeEnergyCentroid != NULL)
	{
		cgGLLoadProgram(VP_ComputeEnergyCentroid);
	}
	if(FP_ComputeEnergyCentroid != NULL)
	{
		cgGLLoadProgram(FP_ComputeEnergyCentroid);

		// Bind parameters to give access to variables in the shader
		FP_ComputeEnergyCentroid_Size = cgGetNamedParameter(FP_ComputeEnergyCentroid, "size");
	}

	if(VP_ScatterCentroid != NULL)
	{
		cgGLLoadProgram(VP_ScatterCentroid);

		// Bind parameters to give access to variables in the shader
		VP_ScatterCentroid_Size = cgGetNamedParameter(VP_ScatterCentroid, "size");
	}
	if(FP_ScatterCentroid != NULL)
	{
		cgGLLoadProgram(FP_ScatterCentroid);
	}
}
Example #10
0
void GLWindow::initializeGL()
{
	mInitialized = true;
	
	if( checkGLError() ) LOG_TRACE
	// Set up the rendering context, define display lists etc.:
	glClearColor( 8/256.0, 5/256.0, 76/256.0, 0.0 );
	glDisable(GL_DEPTH_TEST);

	if( checkGLError() ) LOG_TRACE

#ifdef COMPILE_CG
	if( mUseCG && cgGLIsProfileSupported( CG_PROFILE_FP20 ) ){
		//LOG_3("Using Cg");
		// This is the most basic fragment profile, should run on quite a few cards
		cgFragmentProfile = CG_PROFILE_FP20;
		cgSetErrorCallback(cgErrorCallback);
		cgContext = cgCreateContext();
		// TODO: Include the shader source in the executable
		cgProgram = cgCreateProgram( cgContext, CG_SOURCE, FRAGMENT_SHADER, cgFragmentProfile, 0, 0);
		cgGLLoadProgram( cgProgram );
		cgImageParam = cgGetNamedParameter( cgProgram, "image" );
		cgColorClampParam = cgGetNamedParameter( cgProgram, "colorClamp" );
		cgGLBindProgram( cgProgram );
	}else
#endif
	{
		//LOG_3("Using stock OpenGL");
		mUseCG = false;
	}

// GL_TEXTURE_ENV_MODE defaults to GL_MODULATE
	// GL_TEXTURE_ENV_COLOR defaults to (0,0,0,0)

#ifdef GL_NV_texture_rectangle
	mUseGL_NV_texture_rectangle = isExtensionSupported( "GL_NV_texture_rectangle" );
	if( checkGLError() ) LOG_TRACE
#endif // GL_NV_texture_rectangle

#ifdef GL_NV_texture_rectangle
	if( mUseGL_NV_texture_rectangle ){
		//LOG_5( "Using GL_TEXTURE_RECTANGLE_NV" );
		mTextureMode = GL_TEXTURE_RECTANGLE_NV;
		// Only try to use cg, if texture_rectangle is supported
		// 4/23/04 WHY?
		mUseCG = true;
	} else
#endif // GL_NV_texture_rectangle
	{
		//qWarning("NOT USING GL_TEXTURE_RECTANGLE_NV");
		mTextureMode = GL_TEXTURE_2D;
	
		// Set up the texturing params
		GLint wrapMode = GL_CLAMP;
		int glMajor, glMinor;
		glVersion(glMajor,glMinor);
		if( glMajor >= 1 && glMinor >=2 )
			wrapMode = GL_CLAMP_TO_EDGE;
		
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, wrapMode);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, wrapMode);
	
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	}
}
Example #11
0
	void D3D9Device::init(
		const ion_uint32 width,const ion_uint32 height,
		const base::String& title,const bool fullscreen,void* displayhandle,
		const ion_uint32 adapter,
		const video::Pixelformat colorbufferformat,
		const video::Pixelformat depthstencilformat,const ion_uint32 Hz,const bool vsync)
	{
		if (!m_IsValid) {
			base::log("D3D9Device::init()",base::Warning) <<
				"Initializing an invalid device\n";
		}

		openWindow(width,height,title,fullscreen,displayhandle);

		bool windowed=!m_Fullscreen;

		D3DFORMAT adapterfmt;
		if (windowed) {
			 D3DDISPLAYMODE d3ddm;
			 m_pD3D9->GetAdapterDisplayMode(adapter,&d3ddm);
			 adapterfmt=d3ddm.Format;
		} else adapterfmt=d3dpixelformat(colorbufferformat);

		if (adapterfmt==D3DFMT_UNKNOWN) {
			base::log("D3D9Device::init()",base::Error) << "Unknown/unsupported color buffer format\n";
			m_IsValid=false; return;
		}

		RECT r;
		GetClientRect(m_hWindow,&r);

		m_D3DPP.Windowed=windowed;
		m_D3DPP.BackBufferWidth=r.right-r.left;
		m_D3DPP.BackBufferHeight=r.bottom-r.top;
		m_D3DPP.BackBufferCount=1;
		m_D3DPP.BackBufferFormat=adapterfmt;
		m_D3DPP.AutoDepthStencilFormat=d3dpixelformat(depthstencilformat);
		m_D3DPP.EnableAutoDepthStencil=TRUE;
		m_D3DPP.Flags=0;
		m_D3DPP.PresentationInterval=vsync ? D3DPRESENT_INTERVAL_DEFAULT : D3DPRESENT_INTERVAL_IMMEDIATE;
		m_D3DPP.FullScreen_RefreshRateInHz=windowed ? 0 : Hz;
		m_D3DPP.hDeviceWindow=m_hWindow;
		m_D3DPP.MultiSampleQuality=0;
		m_D3DPP.MultiSampleType=D3DMULTISAMPLE_NONE;
		m_D3DPP.SwapEffect=D3DSWAPEFFECT_DISCARD;
		m_D3DPP.Windowed=windowed;

		// Getting caps
		base::log("D3D9Device::init()",base::Message) << "Getting caps\n";

		HRESULT hr=m_pD3D9->GetDeviceCaps(adapter,D3DDEVTYPE_HAL,&m_D3DCaps);
		if (FAILED(hr)) {
			base::log("D3D9Device::init()",base::Error) << "GetDeviceCaps() failed\n";
			m_IsValid=false;
			return;
		}

		// Caps structure is initialized here
		{
			base::log("D3D9Device::init()",base::Message) << "Analyzing caps\n";

			m_Caps.m_NPOTTextures=!((m_D3DCaps.TextureCaps&D3DPTEXTURECAPS_POW2) ||
				(m_D3DCaps.TextureCaps&D3DPTEXTURECAPS_NONPOW2CONDITIONAL));
			m_Caps.m_RectangularTextures=(m_D3DCaps.TextureCaps&D3DPTEXTURECAPS_SQUAREONLY)==0;
			m_Caps.m_Cubemaps=(m_D3DCaps.TextureCaps&D3DPTEXTURECAPS_CUBEMAP)!=0;
			m_Caps.m_NPOTCubemaps=(m_D3DCaps.TextureCaps&D3DPTEXTURECAPS_CUBEMAP_POW2)==0;
			m_Caps.m_ProjectedTextures=(m_D3DCaps.TextureCaps&D3DPTEXTURECAPS_PROJECTED)!=0;
			m_Caps.m_DisplacementMaps=false;
			m_Caps.m_CompressedTextures=false;

			m_Caps.m_MaxTextureWidth=m_D3DCaps.MaxTextureWidth;
			m_Caps.m_MaxTextureHeight=m_D3DCaps.MaxTextureHeight;
			m_Caps.m_MaxTextureVolumeDepth=m_D3DCaps.MaxVolumeExtent;
			m_Caps.m_MaxAnisotropy=m_D3DCaps.MaxAnisotropy;
			m_Caps.m_MaxTextureBlendStages=m_D3DCaps.MaxTextureBlendStages;
			m_Caps.m_MaxActiveFFLights=m_D3DCaps.MaxActiveLights;
			m_Caps.m_MaxPointSize=m_D3DCaps.MaxPointSize;
			m_Caps.m_MaxPrimitiveCount=m_D3DCaps.MaxPrimitiveCount;
			m_Caps.m_MaxVertexIndex=m_D3DCaps.MaxVertexIndex;

		}

		// Probing formats
		{

			video::Caps::Texflags **pTexflags,
				*pTexflagarrays[]={m_Caps.m_Valid2DTextureFormats,m_Caps.m_Valid3DTextureFormats,
				m_Caps.m_ValidCubemapFormats,0};

			const D3DRESOURCETYPE rtype[]={D3DRTYPE_TEXTURE,D3DRTYPE_VOLUMETEXTURE,D3DRTYPE_CUBETEXTURE};

			pTexflags=pTexflagarrays;
			unsigned int ii=0;

			base::log("D3D9Device::init()",base::Message) << "Probing texture formats\n";

			while (*pTexflags) {

				video::Caps::Texflags *pTexflagarray=*pTexflags;

				for (unsigned int i=0;i<video::Pixelformat_NumFormats;++i) {
					D3DFORMAT fmt=d3dpixelformat((video::Pixelformat)i);
					hr=0;

					hr=m_pD3D9->CheckDeviceFormat(adapter,D3DDEVTYPE_HAL,adapterfmt,0,rtype[ii],fmt);
					if (FAILED(hr)) { pTexflagarray[i].m_Supported=false; continue; }
					else pTexflagarray[i].m_Supported=true;

					if ((i==video::Pixelformat_RGB_DXT1)
						|| (i==video::Pixelformat_RGBA_DXT1)
						|| (i==video::Pixelformat_RGBA_DXT3)
						|| (i==video::Pixelformat_RGBA_DXT5))
						m_Caps.m_CompressedTextures=true;

					// Volume textures are not supported as render targets
					if (rtype[ii]!=D3DRTYPE_VOLUMETEXTURE) {
						hr=m_pD3D9->CheckDeviceFormat(adapter,D3DDEVTYPE_HAL,adapterfmt,D3DUSAGE_RENDERTARGET,rtype[ii],fmt);
						pTexflagarray[i].m_Rendertarget=(hr==D3D_OK);
						m_Caps.m_Rendertargets=true;
					} else pTexflagarray[i].m_Rendertarget=false;

					hr=m_pD3D9->CheckDeviceFormat(adapter,D3DDEVTYPE_HAL,adapterfmt,D3DUSAGE_DYNAMIC,rtype[ii],fmt);
					pTexflagarray[i].m_Dynamic=(hr==D3D_OK);

					// For depth buffers, only depth formats are supported
					// Volume textures are not supported
					if (isDepthformat((video::Pixelformat)i) && (rtype[ii]!=D3DRTYPE_VOLUMETEXTURE)) {
						hr=m_pD3D9->CheckDeviceFormat(adapter,D3DDEVTYPE_HAL,adapterfmt,D3DUSAGE_DEPTHSTENCIL,rtype[ii],fmt);
						pTexflagarray[i].m_Depth=(hr==D3D_OK);
					} else pTexflagarray[i].m_Depth=false;

					if ((m_D3DCaps.DevCaps2&D3DDEVCAPS2_DMAPNPATCH)!=0) {
						hr=m_pD3D9->CheckDeviceFormat(adapter,D3DDEVTYPE_HAL,adapterfmt,D3DUSAGE_DMAP,rtype[ii],fmt);
						pTexflagarray[i].m_DisplacementMap=(hr==D3D_OK);
					} else pTexflagarray[i].m_DisplacementMap=false;
				}

				++pTexflags;
				++ii;
			}
		}

		base::log("D3D9Device::init()",base::Message) << "Creating D3D device\n";

		hr=m_pD3D9->CreateDevice(adapter,D3DDEVTYPE_HAL,m_hWindow,
			D3DCREATE_HARDWARE_VERTEXPROCESSING,&m_D3DPP,&m_pD3DDev9);

		if (FAILED(hr)) {
			base::log("D3D9Device::init()",base::Error) << "CreateDevice() failed\n";
			m_IsValid=false;
			return;
		}

		D3DVIEWPORT9 viewport;
		viewport.X=0;
		viewport.Y=0;
		viewport.Width=r.right-r.left;
		viewport.Height=r.bottom-r.top;
		viewport.MinZ=0;
		viewport.MaxZ=1;
		m_pD3DDev9->SetViewport(&viewport);

#ifdef D3D9DRV_USE_CG
		base::log("D3D9Device::init()",base::Message) << "Initializing Cg\n";
		{
			m_CgContext=cgCreateContext();
			cgD3D9SetDevice(m_pD3DDev9);

#ifdef D3D9DRV_CG_DEBUG_ERRORCALLBACK
			errctx=m_CgContext;
			cgSetErrorCallback(cgErrorCallback);
#endif

			m_CgVertexProfile=cgD3D9GetLatestVertexProfile();
			m_CgFragmentProfile=cgD3D9GetLatestPixelProfile();
			m_CgOptimalVertexProfileOptions=cgD3D9GetOptimalOptions(m_CgVertexProfile);
			m_CgOptimalFragmentProfileOptions=cgD3D9GetOptimalOptions(m_CgFragmentProfile);
		}
#endif
			
		base::log("D3D9Device::init()",base::Message) << "Probing queries\n";
		{
			IDirect3DQuery9* pQuery=0;
			hr=m_pD3DDev9->CreateQuery(D3DQUERYTYPE_OCCLUSION,&pQuery);
			m_Caps.m_HWOcclusionQueries=(hr==D3D_OK);
			if (pQuery) pQuery->Release();
		}
			
		{
			base::log("D3D9Device::init()",base::Message) << "Probing vertex program support\n";

			// Supported vertex programs
			{
				unsigned int version=HIBYTE(LOWORD(m_D3DCaps.VertexShaderVersion));
				unsigned int subversion=LOBYTE(LOWORD(m_D3DCaps.VertexShaderVersion));

				if (version>=3) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_vs11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_vs20");
					m_Caps.m_SupportedProgramFormats.addString("d3d_vs30");
				} else if (version>=2) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_vs11");
					if (subversion>=0) m_Caps.m_SupportedProgramFormats.addString("d3d_vs20");
				} else if (version>=1) {
					if (subversion>=1) m_Caps.m_SupportedProgramFormats.addString("d3d_vs11");
				}

				// HLSL support

				LPCSTR vsprofile=D3DXGetVertexShaderProfile(m_pD3DDev9);
				base::String hlslvsprofile;
				if (vsprofile) hlslvsprofile=vsprofile;

				if (hlslvsprofile.compare("vs_1_1",true)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_vs11");
				} else if (hlslvsprofile.compare("vs_2_0",true)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_vs11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_vs20");
				} else if (hlslvsprofile.compare("vs_2_a",true)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_vs11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_vs20");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_vs2a");
				} else if (hlslvsprofile.compare("vs_3_0",true) || (version>=3)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_vs11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_vs20");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_vs2a");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_vs30");
				}

#ifdef D3D9DRV_USE_CG
				m_Caps.m_SupportedProgramFormats.addString("cg_vprogram");
#endif
			}

			base::log("D3D9Device::init()",base::Message) << "Probing fragment program support\n";

			// Supported fragment programs
			{
				unsigned int version=HIBYTE(LOWORD(m_D3DCaps.PixelShaderVersion));
				unsigned int subversion=LOBYTE(LOWORD(m_D3DCaps.PixelShaderVersion));

				if (version>=3) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_ps11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_ps12");
					m_Caps.m_SupportedProgramFormats.addString("d3d_ps13");
					m_Caps.m_SupportedProgramFormats.addString("d3d_ps14");
					m_Caps.m_SupportedProgramFormats.addString("d3d_ps20");
					if (subversion>=0) m_Caps.m_SupportedProgramFormats.addString("d3d_ps30");
				} else if (version>=2) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_ps11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_ps12");
					m_Caps.m_SupportedProgramFormats.addString("d3d_ps13");
					m_Caps.m_SupportedProgramFormats.addString("d3d_ps14");
					if (subversion>=0) m_Caps.m_SupportedProgramFormats.addString("d3d_ps20");
				} else if (version>=1) {
					if (subversion>=1) m_Caps.m_SupportedProgramFormats.addString("d3d_ps11");
					if (subversion>=2) m_Caps.m_SupportedProgramFormats.addString("d3d_ps12");
					if (subversion>=3) m_Caps.m_SupportedProgramFormats.addString("d3d_ps13");
					if (subversion>=4) m_Caps.m_SupportedProgramFormats.addString("d3d_ps14");
				}
				

				

				// HLSL support

				LPCSTR vsprofile=D3DXGetPixelShaderProfile(m_pD3DDev9);
				base::String hlslpsprofile;
				if (vsprofile) hlslpsprofile=vsprofile;

				if (hlslpsprofile.compare("ps_1_1",true)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps11");
				} else if (hlslpsprofile.compare("ps_1_2",true)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps12");
				} else if (hlslpsprofile.compare("ps_1_3",true)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps12");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps13");
				} else if (hlslpsprofile.compare("ps_1_4",true)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps12");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps13");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps14");
				} else if (hlslpsprofile.compare("ps_2_0",true)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps12");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps13");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps14");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps20");
				} else if (hlslpsprofile.compare("ps_2_a",true)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps12");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps13");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps14");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps20");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps2a");
				} else if (hlslpsprofile.compare("ps_3_0",true) || (version>=3)) {
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps11");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps12");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps13");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps14");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps20");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps2a");
					m_Caps.m_SupportedProgramFormats.addString("d3d_hlsl_ps30");
				}

#ifdef D3D9DRV_USE_CG
				m_Caps.m_SupportedProgramFormats.addString("cg_fprogram");
#endif
			}

		} // Caps end

		m_Displaywidth=viewport.Width;
		m_Displayheight=viewport.Height;
		m_ColorbufferFormat=colorbufferformat;
		m_DepthstencilbufferFormat=depthstencilformat;

		m_pD3DDev9->SetRenderState(D3DRS_FOGVERTEXMODE,D3DFOG_LINEAR);

		fixedLighting(false);

		base::log("D3D9Device::init()",base::Message) << "Init complete\n";
	}