void ResourceManager::ValidateDevice(IInspectable* wrapper, ICanvasDevice* device) { auto wrapperWithDevice = MaybeAs<ICanvasResourceWrapperWithDevice>(wrapper); if (wrapperWithDevice) { ValidateDevice(wrapperWithDevice.Get(), device); } }
//----------------------------------------------------------------------------- // Name : EnumerateDevices () (Private) // Desc : Enumerates the valid device types for the specified adapter. //----------------------------------------------------------------------------- HRESULT CD3DInitialize::EnumerateDevices( CD3DEnumAdapter * pAdapter ) { ULONG i; HRESULT hRet; D3DCAPS9 Caps; // Loop through each device type (HAL, SW, REF) for ( i = 0; i < DeviceTypeCount; i++ ) { // Retrieve device caps (on failure, device not generally available) if ( FAILED( m_pD3D->GetDeviceCaps( pAdapter->Ordinal, DeviceTypes[i], &Caps ) ) ) continue; // Supported by user ? if ( !ValidateDevice( DeviceTypes[ i ], Caps ) ) continue; // Allocate a new device CD3DEnumDevice * pDevice = new CD3DEnumDevice; if ( !pDevice ) return E_OUTOFMEMORY; // Store device information pDevice->DeviceType = DeviceTypes[i]; pDevice->Caps = Caps; // Retrieve various init options for this device if ( FAILED( hRet = EnumerateDeviceOptions( pDevice, pAdapter ) ) ) { delete pDevice; if ( hRet == E_ABORT ) continue; else return hRet; } // End if failed to enumerate // Add it to our adapter list try { pAdapter->Devices.push_back( pDevice ); } catch ( ... ) { delete pDevice; return E_OUTOFMEMORY; } // End Try / Catch Block } // Next Device Type // Success? return (pAdapter->Devices.size() == 0) ? E_ABORT : S_OK; }
ComPtr<IInspectable> ResourceManager::GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi) { ComPtr<IUnknown> resourceIdentity = AsUnknown(resource); ComPtr<IInspectable> wrapper; std::lock_guard<std::recursive_mutex> lock(m_mutex); // Do we already have a wrapper around this resource? auto it = m_resources.find(resourceIdentity.Get()); if (it != m_resources.end()) { wrapper = LockWeakRef<IInspectable>(it->second); } // Create a new wrapper instance? if (!wrapper) { for (auto& tryCreateFunction : tryCreateFunctions) { if (tryCreateFunction(device, resource, dpi, &wrapper)) { break; } } // Fail if we did not find a way to wrap this type. if (!wrapper) { ThrowHR(E_NOINTERFACE, Strings::ResourceManagerUnknownType); } } // Validate that the object we got back reports the expected device and DPI. ValidateDevice(wrapper.Get(), device); ValidateDpi(wrapper.Get(), dpi); return wrapper; }
bool nGraphics::InitGraphics() { // Create our base direct3d object m_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION); if(!m_pDirect3D) { nMainFrame::LastError("Failed to create direct3d 9, directx 9 not installed?"); return TraceRet(__FUNCTION__" Failed to create direct3d 9, directx 9 not installed?\n",false); } // Check if the device support's the caps we will use if(!ValidateDevice()) return false; // Get the desktop's display mode D3DDISPLAYMODE displayMode; if(FAILED(m_pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode))) { nMainFrame::LastError("Failed to get current display adapter mode."); return TraceRet(__FUNCTION__" Failed to get current display adapter mode.\n",false); } // The struct that describes our direct3d device ZeroMemory(&m_PresentParameters,sizeof(m_PresentParameters)); m_Fullscreen = !nGetInstance()->GetCommandLineVar("-windowed",SETTINGS_WINDOWED).asBool(); m_PresentParameters.Windowed = !m_Fullscreen; m_PresentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; m_PresentParameters.BackBufferFormat = D3DFMT_X8R8G8B8; m_PresentParameters.EnableAutoDepthStencil = TRUE; m_PresentParameters.AutoDepthStencilFormat = D3DFMT_D24S8; // Check if we should use fsaa if(nGetInstance()->GetCommandLineVar("-fsaa",SETTINGS_FSAA).asBool()) { unsigned long samples = 0; // Check if multisampling is supported and enable it if it is if(SUCCEEDED(m_pDirect3D->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,m_PresentParameters.BackBufferFormat,!m_Fullscreen,D3DMULTISAMPLE_NONMASKABLE,&samples))) { m_PresentParameters.MultiSampleQuality = samples-1; m_PresentParameters.MultiSampleType = D3DMULTISAMPLE_NONMASKABLE; Trace(__FUNCTION__" Using %dx multisampling.\n",samples-1); } } // Check if we should display in fullscreen if(m_Fullscreen) { m_PresentParameters.BackBufferWidth = displayMode.Width; m_PresentParameters.BackBufferHeight = displayMode.Height; } else { nRect rect; GetClientRect(nGetInstance()->GetWindowHandle(),(LPRECT)&rect); m_PresentParameters.BackBufferWidth = rect.GetWidth(); m_PresentParameters.BackBufferHeight = rect.GetHeight(); } // Check if we shouldn't use vsync if(!nGetInstance()->GetCommandLineVar("-vsync",SETTINGS_VSYNC).asBool()) m_PresentParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; Trace(__FUNCTION__" Display mode %dx%d.\n",m_PresentParameters.BackBufferWidth,m_PresentParameters.BackBufferHeight); // Now create our direct3d device from the base direct3d object HRESULT hr = m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,nGetInstance()->GetWindowHandle(),D3DCREATE_SOFTWARE_VERTEXPROCESSING,&m_PresentParameters,&m_pDevice); if(FAILED(hr)) return TraceRet(__FUNCTION__" Failed to create the device.\n",false); // Turn on color dithering hr = m_pDevice->SetRenderState(D3DRS_DITHERENABLE,TRUE); if(FAILED(hr)) Trace(__FUNCTION__" Display adapter doesn't support color dithering, using default.\n"); // Turn off culling m_pDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); // Enable anisotropic filtering if fsaa is enabled //if(flags & RECFLAG_FSAA) // EnableAnisotropic(m_p3DDevice); // Create our line drawing object hr = D3DXCreateLine(m_pDevice, &m_pLine); if(FAILED(hr)) { nMainFrame::LastError("Failed to create line drawing object."); return TraceRet(__FUNCTION__" Failed to create line drawing object.\n",false); } // Don't use gl lines m_pLine->SetGLLines(false); // Init our font hr = D3DXCreateFont(m_pDevice,SETTINGS_FONTHEIGHT,0,FW_BOLD,0,FALSE,DEFAULT_CHARSET,OUT_TT_PRECIS,6/*CLEARTYPE_QUALITY*/,DEFAULT_PITCH,SETTINGS_FONTNAME,&m_pFontBold); if(FAILED(hr)) { nMainFrame::LastError("Failed to create font. Font not found?"); return TraceRet(__FUNCTION__" Failed to create font. Font not found?",false); } // Init our thin font hr = D3DXCreateFont(m_pDevice,SETTINGS_FONTHEIGHT,0,FW_NORMAL,0,FALSE,DEFAULT_CHARSET,OUT_TT_PRECIS,6/*CLEARTYPE_QUALITY*/,DEFAULT_PITCH,SETTINGS_FONTNAME,&m_pFontNormal); if(FAILED(hr)) { nMainFrame::LastError("Failed to create bold font. Font not found?"); return TraceRet(__FUNCTION__" Failed to create bold font. Font not found?",false); } // Get the font sizes nRect fontSize; GetTextRect(GetBoldFont(),"str",&fontSize,NULL); m_BoldFontHeight = fontSize.GetHeight(); GetTextRect(GetNormalFont(),"str",&fontSize,NULL); m_NormalFontHeight = fontSize.GetHeight(); // Create our sprite drawing object hr = D3DXCreateSprite(m_pDevice, &m_pSprite); if(FAILED(hr)) { nMainFrame::LastError("Failed to create sprite."); return TraceRet(__FUNCTION__" Failed to create sprite.\n",false); } // Turn on texture filtering m_pDevice->SetSamplerState(0,D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); m_pDevice->SetSamplerState(0,D3DSAMP_MINFILTER, D3DTEXF_LINEAR); m_pDevice->SetSamplerState(1,D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); m_pDevice->SetSamplerState(1,D3DSAMP_MINFILTER, D3DTEXF_LINEAR); m_pDevice->SetSamplerState(2,D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); m_pDevice->SetSamplerState(2,D3DSAMP_MINFILTER, D3DTEXF_LINEAR); return true; }
int main(int argc, char** argv) { int dw = GetScreenWidth(); int dh = GetScreenHeight(); vec3 icp = {0.0F, 4.0F, 8.0F}; vec3 icl = {0.0F, 0.0F, 0.0F}; vec3 icu = {0.0F, 1.0F, 0.0F}; g_globalCam.SetCamera(icp, icl, icu); g_globalCam.SetPerspective(65.0F, dw / (float)dh, 0.1F, 1000.0F); // Query extension base // CreateAppWindow("Mooball", dw, dh, 32, false); initExtensions(); InitKeys(); srand(time(NULL)); // Query Device capabilities // if(!ValidateDevice()) { DestroyAppWindow(); return 0; } g_pTexInterface = new CTextureInterface; g_pShaderInterface = new CShaderInterface; g_lightManager = new CLightManager; g_bMSAA = (quickINI::Instance()->getValueAsInt("msaa") > 0) && (g_pTexInterface->GetMaxMSAA() > 0); g_bVSYNC = quickINI::Instance()->getValueAsInt("vsync") > 0; // g_model.Load( "Media/Models/Pokeball.3ds" ); CModelObject* pMdl = new COBJModel; std::string err = pMdl->LoadModel("sponza.obj", "Media/Models/"); // Initialize CG Runtime and shaders // init_cg(); // Turn vsync on // if( EXT_VSYNC && g_bVSYNC ) { #ifdef WIN32 wglSwapIntervalEXT(1); #else glXSwapIntervalSGI(1); #endif } // Create offscreen targets and depth configurations // init_render_targets(); // Added 8/4/10 - Keeps Mooball from hogging // the input focus while it's minimized. // HWND windowHandle = GetFocus(); while(running) { if(QueryQuitMsg()) running = false; else { if(!g_bDebugMode) { UpdateScene(); PollKeys(); PollMouse(); } RenderScene(); FlipBuffers(); } } // Fall through to destruction // DestroyAppWindow(); delete g_lightManager; delete g_pTexInterface; delete g_pShaderInterface; return 0; }