void GSDeviceOGL::CreateTextureFX() { m_vs_cb = new GSUniformBufferOGL(g_vs_cb_index, sizeof(VSConstantBuffer)); m_ps_cb = new GSUniformBufferOGL(g_ps_cb_index, sizeof(PSConstantBuffer)); // warning 1 sampler by image unit. So you cannot reuse m_ps_ss... m_palette_ss = CreateSampler(false, false, false); glBindSampler(1, m_palette_ss); // Pre compile all Geometry & Vertex Shader // It might cost a seconds at startup but it would reduce benchmark pollution GL_PUSH("Compile GS"); for (uint32 key = 0; key < countof(m_gs); key++) { GSSelector sel(key); if (sel.point == sel.sprite) m_gs[key] = 0; else m_gs[key] = CompileGS(GSSelector(key)); } GL_POP(); GL_PUSH("Compile VS"); for (uint32 key = 0; key < countof(m_vs); key++) { // wildhack is only useful if both TME and FST are enabled. VSSelector sel(key); if (sel.wildhack && (!sel.tme || !sel.fst)) m_vs[key] = 0; else m_vs[key] = CompileVS(sel, !GLLoader::found_GL_ARB_clip_control); } GL_POP(); // Enable all bits for stencil operations. Technically 1 bit is // enough but buffer is polluted with noise. Clear will be limited // to the mask. glStencilMask(0xFF); for (uint32 key = 0; key < countof(m_om_dss); key++) { m_om_dss[key] = CreateDepthStencil(OMDepthStencilSelector(key)); } // Help to debug FS in apitrace m_apitrace = CompilePS(PSSelector()); }
void GSDeviceOGL::CreateTextureFX() { GL_PUSH("CreateTextureFX"); m_vs_cb = new GSUniformBufferOGL(g_vs_cb_index, sizeof(VSConstantBuffer)); m_ps_cb = new GSUniformBufferOGL(g_ps_cb_index, sizeof(PSConstantBuffer)); // warning 1 sampler by image unit. So you cannot reuse m_ps_ss... m_palette_ss = CreateSampler(false, false, false); gl_BindSampler(1, m_palette_ss); // Pre compile all Geometry & Vertex Shader // It might cost a seconds at startup but it would reduce benchmark pollution m_gs = CompileGS(); int logz = theApp.GetConfig("logz", 1); // Don't do it in debug build, so it can still be tested #ifndef _DEBUG if (GLLoader::found_GL_ARB_clip_control && logz) { fprintf(stderr, "Your driver supports advance depth. Logz will be disabled\n"); logz = 0; } else if (!GLLoader::found_GL_ARB_clip_control && !logz) { fprintf(stderr, "Your driver DOESN'T support advance depth (GL_ARB_clip_control)\n It is higly recommmended to enable logz\n"); } #endif for (uint32 key = 0; key < VSSelector::size(); key++) { // wildhack is only useful if both TME and FST are enabled. VSSelector sel(key); if (sel.wildhack && (!sel.tme || !sel.fst)) m_vs[key] = 0; else m_vs[key] = CompileVS(sel, logz); } // Enable all bits for stencil operations. Technically 1 bit is // enough but buffer is polluted with noise. Clear will be limited // to the mask. glStencilMask(0xFF); for (uint32 key = 0; key < OMDepthStencilSelector::size(); key++) m_om_dss[key] = CreateDepthStencil(OMDepthStencilSelector(key)); // Help to debug FS in apitrace m_apitrace = CompilePS(PSSelector()); GL_POP(); }
void GSDeviceOGL::CreateTextureFX() { m_vs_cb = new GSUniformBufferOGL(g_vs_cb_index, sizeof(VSConstantBuffer)); m_ps_cb = new GSUniformBufferOGL(g_ps_cb_index, sizeof(PSConstantBuffer)); // warning 1 sampler by image unit. So you cannot reuse m_ps_ss... m_palette_ss = CreateSampler(false, false, false); gl_BindSampler(1, m_palette_ss); GSInputLayoutOGL vert_format[] = { {0 , 2 , GL_FLOAT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(0) } , {1 , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof(GSVertex) , (const GLvoid*)(8) } , {2 , 1 , GL_FLOAT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(12) } , {3 , 2 , GL_UNSIGNED_SHORT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(16) } , {4 , 1 , GL_UNSIGNED_INT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(20) } , {5 , 2 , GL_UNSIGNED_SHORT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(24) } , {6 , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof(GSVertex) , (const GLvoid*)(28) } , }; m_vb = new GSVertexBufferStateOGL(sizeof(GSVertex), vert_format, countof(vert_format)); // Pre compile all Geometry & Vertex Shader // It might cost a seconds at startup but it would reduce benchmark pollution m_gs = CompileGS(); for (uint32 key = 0; key < VSSelector::size(); key++) m_vs[key] = CompileVS(VSSelector(key)); for (uint32 key = 0; key < OMDepthStencilSelector::size(); key++) m_om_dss[key] = CreateDepthStencil(OMDepthStencilSelector(key)); // Help to debug FS in apitrace m_apitrace = CompilePS(PSSelector()); // VS gl_position.z => [-1,-1] // FS depth => [0, 1] // because of -1 we loose lot of precision for small GS value // This extension allow FS depth to range from -1 to 1. So // gl_position.z could range from [0, 1] if (GLLoader::found_GL_NV_depth_buffer_float) { gl_DepthRangedNV(-1.0f, 1.0f); } }
void GSDeviceOGL::CreateTextureFX() { m_vs_cb = new GSUniformBufferOGL(g_vs_cb_index, sizeof(VSConstantBuffer)); m_ps_cb = new GSUniformBufferOGL(g_ps_cb_index, sizeof(PSConstantBuffer)); // warning 1 sampler by image unit. So you cannot reuse m_ps_ss... m_palette_ss = CreateSampler(false, false, false); gl_BindSampler(1, m_palette_ss); GSInputLayoutOGL vert_format[] = { {0 , 2 , GL_FLOAT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(0) } , {1 , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof(GSVertex) , (const GLvoid*)(8) } , {2 , 1 , GL_FLOAT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(12) } , {3 , 2 , GL_UNSIGNED_SHORT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(16) } , {4 , 1 , GL_UNSIGNED_INT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(20) } , {5 , 2 , GL_UNSIGNED_SHORT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(24) } , {6 , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof(GSVertex) , (const GLvoid*)(28) } , }; m_vb = new GSVertexBufferStateOGL(sizeof(GSVertex), vert_format, countof(vert_format)); // Pre compile all Geometry & Vertex Shader // It might cost a seconds at startup but it would reduce benchmark pollution m_gs = CompileGS(); for (uint32 key = 0; key < VSSelector::size(); key++) m_vs[key] = CompileVS(VSSelector(key)); for (uint32 key = 0; key < OMDepthStencilSelector::size(); key++) m_om_dss[key] = CreateDepthStencil(OMDepthStencilSelector(key)); // Help to debug FS in apitrace m_apitrace = CompilePS(PSSelector()); if (!!theApp.GetConfig("GL_NV_Depth", 0)) { gl_DepthRangedNV(-1.0f, 1.0f); } }
void MagnifyTool::OnResizedSwapChain( ID3D10Device* pd3dDevice, IDXGISwapChain *pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext, int nPositionX, int nPositionY ) { CreateDepthStencil( pd3dDevice, pSwapChain, pBackBufferSurfaceDesc, pUserContext ); m_MagnifyUI.SetLocation( nPositionX, nPositionY ); m_MagnifyUI.SetSize( 170, 155 ); m_Magnify.OnResizedSwapChain( DXUTGetDXGIBackBufferSurfaceDesc() ); if( m_bReleaseRTOnResize ) { D3D10_RENDER_TARGET_VIEW_DESC RTDesc; DXUTGetD3D10RenderTargetView()->GetResource( &m_pSourceRTResource ); DXUTGetD3D10RenderTargetView()->GetDesc( &RTDesc ); m_RTFormat = RTDesc.Format; m_nWidth = DXUTGetDXGIBackBufferSurfaceDesc()->Width; m_nHeight = DXUTGetDXGIBackBufferSurfaceDesc()->Height; m_nSamples = DXUTGetDXGIBackBufferSurfaceDesc()->SampleDesc.Count; if( !m_MagnifyUI.GetCheckBox( IDC_MAGNIFY_CHECKBOX_DEPTH )->GetChecked() ) { m_Magnify.SetSourceResource( m_pSourceRTResource, m_RTFormat, m_nWidth, m_nHeight, m_nSamples ); } } if( m_bReleaseDepthOnResize ) { D3D10_DEPTH_STENCIL_VIEW_DESC DSDesc; m_pDSV->GetResource( &m_pSourceDepthResource ); m_pDSV->GetDesc( &DSDesc ); m_DepthFormat = DSDesc.Format; m_nWidth = DXUTGetDXGIBackBufferSurfaceDesc()->Width; m_nHeight = DXUTGetDXGIBackBufferSurfaceDesc()->Height; m_nSamples = DXUTGetDXGIBackBufferSurfaceDesc()->SampleDesc.Count; if( m_MagnifyUI.GetCheckBox( IDC_MAGNIFY_CHECKBOX_DEPTH )->GetChecked() ) { m_Magnify.SetSourceResource( m_pSourceDepthResource, m_DepthFormat, m_nWidth, m_nHeight, m_nSamples ); } } EnableTool(); if( m_nSamples > 1 ) { EnableSubSampleUI(); } else { DisableSubSampleUI(); } ID3D10Device1* pd3dDevice1 = DXUTGetD3D10Device1(); if( ( NULL == pd3dDevice1 ) && ( m_nSamples > 1 ) ) { DisableDepthUI(); if( NULL == m_pSourceRTResource ) { DisableUI(); DisableTool(); } else { m_Magnify.SetSourceResource( m_pSourceRTResource, m_RTFormat, m_nWidth, m_nHeight, m_nSamples ); } } else { if( NULL != m_pSourceDepthResource ) { EnableDepthUI(); } } }
HRESULT ForwardRenderer::init(HWND hwnd, ID3D11Device* pDevice, ID3D11DeviceContext* pContext, IDXGISwapChain* pSwapChain) { // Create a render target view HRESULT hr = S_OK; pRenderTarget = NULL; RECT rc; GetClientRect( hwnd, &rc ); mWidth = rc.right - rc.left; mHeight = rc.bottom - rc.top; ID3D11Texture2D* pBackBuffer = NULL; hr = pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer ); if( FAILED( hr ) ) return hr; hr = pDevice->CreateRenderTargetView( pBackBuffer, NULL, &pRenderTarget ); pBackBuffer->Release(); if( FAILED( hr ) ) return hr; pContext->OMSetRenderTargets( 1, &pRenderTarget, NULL ); CreateDepthStencil(pDevice, pContext); // Setup the viewport D3D11_VIEWPORT vp; vp.Width = (FLOAT)mWidth; vp.Height = (FLOAT)mHeight; vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; vp.TopLeftX = 0; vp.TopLeftY = 0; pContext->RSSetViewports( 1, &vp ); // Create the constant buffer D3D11_BUFFER_DESC bd; ZeroMemory( &bd, sizeof(bd) ); bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = sizeof(ConstantBuffer); bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; bd.StructureByteStride = 0; hr = pDevice->CreateBuffer( &bd, NULL, &g_pConstantBuffer ); if( FAILED( hr ) ) return hr; // Create test light shader // Compile the pixel shader ID3DBlob* pPSBlob = NULL; hr = CompileShaderFromFile2( "testShader.fx", "PSSolid", "ps_4_0", &pPSBlob ); if( FAILED( hr ) ) { MessageBox( NULL, "The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", "Error", MB_OK ); return hr; } // Create the pixel shader hr = pDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pPixelShaderSolid ); pPSBlob->Release(); if( FAILED( hr ) ) return hr; // Initialize the world matrix g_World = XMMatrixIdentity(); // Initialize the view matrix XMVECTOR Eye = XMVectorSet( 0.0f, 1.0f, -5.0f, 0.0f ); XMVECTOR At = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f ); XMVECTOR Up = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f ); g_View = XMMatrixLookAtLH( Eye, At, Up ); // Initialize the projection matrix g_Projection = XMMatrixPerspectiveFovLH( XM_PIDIV4, mWidth / (FLOAT)mHeight, 0.01f, 100.0f ); pTestCamera = new Camera(); return S_OK; }
//-------------------------------------------------------------------------------------- // Create Direct3D device and swap chain //-------------------------------------------------------------------------------------- HRESULT InitDevice() { HRESULT hr = S_OK; RECT rc; GetClientRect( g_hWnd, &rc ); UINT createDeviceFlags = 0; // Needs windows update or something #ifdef _DEBUG createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; #endif D3D_DRIVER_TYPE driverTypes[] = { D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP, D3D_DRIVER_TYPE_REFERENCE, }; UINT numDriverTypes = ARRAYSIZE( driverTypes ); D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_0 }; UINT numFeatureLevels = ARRAYSIZE( featureLevels ); DXGI_SWAP_CHAIN_DESC sd; ZeroMemory( &sd, sizeof( sd ) ); sd.BufferCount = 1; sd.BufferDesc.Width = g_options.dWidth; sd.BufferDesc.Height = g_options.dHeight; sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; sd.BufferDesc.RefreshRate.Numerator = 60; sd.BufferDesc.RefreshRate.Denominator = 1; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.OutputWindow = g_hWnd; sd.SampleDesc.Count = 1; sd.SampleDesc.Quality = 0; sd.Windowed = !g_options.fullscreen; for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ ) { g_driverType = driverTypes[driverTypeIndex]; hr = D3D11CreateDeviceAndSwapChain( NULL, g_driverType, NULL, createDeviceFlags, featureLevels, numFeatureLevels, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext ); if( SUCCEEDED( hr ) ) break; } if( FAILED( hr ) ) return hr; // Create a render target view ID3D11Texture2D* pBackBuffer = NULL; hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer ); if( FAILED( hr ) ) return hr; hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pBackBuffer ); pBackBuffer->Release(); if( FAILED( hr ) ) return hr; g_pImmediateContext->OMSetRenderTargets( 1, &g_pBackBuffer, NULL ); if( FAILED( CreateDepthStencil(g_options.iWidth, g_options.iHeight, &g_pDepthStencilView) ) ) return hr; if( FAILED( CreateDepthStencil(g_options.iWidth * 2, g_options.iHeight * 2, &g_particleDepthStencilView) ) ) return hr; // Setup the viewport D3D11_VIEWPORT vp; vp.Width = g_options.dWidth; vp.Height = (double)g_options.dWidth*g_options.aspectRatio; vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; vp.TopLeftX = 0; vp.TopLeftY = 0; g_pImmediateContext->RSSetViewports( 1, &vp ); return S_OK; }