void CGLDevice::Initialize( const tInitStruct& initData ) { // Create modern GL context Create30Context(initData); // Set default states SetDefaultStates(); }
// Function for pixel shader that just needs material colour void PS_PlainColourFn( int method, CMatrix4x4* worldMatrix, CCamera* camera ) { // Ensure we have default render states SetDefaultStates(); LPD3DXCONSTANTTABLE shaderConsts = renderMethods[method].pixelConsts; shaderConsts->SetFloatArray( g_pd3dDevice, "MaterialColour", (FLOAT*)&m_DiffuseColour, 3 ); }
HRESULT InitApp() { /// 카메라 행렬설정 SetupCamera(); CreateFont(); InitFPS(); SetDefaultStates(); return S_OK; }
bool View3D::Create(HWND hWnd) { hDC = GetDC(hWnd); BYTE colorbits = 32, zbits = 32; static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, colorbits, 0,0,0,0,0,0, 0,0, 0,0,0,0,0, zbits, 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 }; int nPixelFormat = ChoosePixelFormat(hDC, &pfd); if (nPixelFormat == 0) { // ChoosePixelFormat() failed return false; } if (SetPixelFormat(hDC, nPixelFormat, &pfd) == FALSE) { // SetPixelFormat() failed return false; } hGLRC = wglCreateContext(hDC); if(hGLRC == NULL) { // wglCreateContext() failed return false; } if(wglMakeCurrent(hDC, hGLRC) == FALSE) { // wglMakeCurrent() failed return false; } SetDefaultStates(); trackball.SetWindow(hWnd); AddView(); return true; }
/** Called when we started to render the world */ XRESULT D3D11GraphicsEngineTest::OnStartWorldRendering() { if(Engine::GAPI->GetRendererState()->RendererSettings.DisableRendering) return XR_SUCCESS; SetDefaultStates(); Engine::GAPI->SetFarPlane(80000.0f); Clear(float4(Engine::GAPI->GetRendererState()->GraphicsState.FF_FogColor, 0.0f)); ID3D11RenderTargetView* rtvs[] = {GBuffer0_Diffuse->GetRenderTargetView(), GBuffer1_Normals_SpecIntens_SpecPower->GetRenderTargetView()}; Context->OMSetRenderTargets(2, rtvs, DepthStencilBuffer->GetDepthStencilView()); if(Engine::GAPI->GetRendererState()->RendererSettings.DrawWorldMesh) DrawWorldMeshTest(); if(Engine::GAPI->GetRendererState()->RendererSettings.DrawVOBs) DrawSceneLightPrePass(); SetDefaultStates(); PfxRenderer->CopyTextureToRTV(GBuffer0_Diffuse->GetShaderResView(), HDRBackBuffer->GetRenderTargetView()); PfxRenderer->CopyTextureToRTV(GBuffer1_Normals_SpecIntens_SpecPower->GetShaderResView(), HDRBackBuffer->GetRenderTargetView(), INT2(256,256)); // Set viewport for gothics rendering D3D11_VIEWPORT vp; vp.TopLeftX = 0.0f; vp.TopLeftY = 0.0f; vp.MinDepth = 0.0f; vp.MaxDepth = 0.0f; vp.Width = (float)GetBackbufferResolution().x; vp.Height = (float)GetBackbufferResolution().y; Context->RSSetViewports(1, &vp); PS_LPP = ShaderManager->GetPShader("PS_LPPNormalmappedAlphaTest"); return XR_SUCCESS; }
// Pass data to pixel shaders that perform pixel lighting (2 point lights) void PS_PixelLit2Fn( int method, CMatrix4x4* worldMatrix, CCamera* camera ) { // Ensure we have default render states SetDefaultStates(); LPD3DXCONSTANTTABLE shaderConsts = renderMethods[method].pixelConsts; D3DXVECTOR3 cameraPos = ToD3DXVECTOR( camera->Position() ); shaderConsts->SetFloatArray( g_pd3dDevice, "CameraPosition", (FLOAT*)&cameraPos, 3 ); shaderConsts->SetFloatArray( g_pd3dDevice, "AmbientLight", (FLOAT*)&m_AmbientLight, 3 ); shaderConsts->SetFloatArray( g_pd3dDevice, "Light1Position", (FLOAT*)&m_Lights[0]->GetPosition(), 3 ); shaderConsts->SetFloatArray( g_pd3dDevice, "Light1Colour", (FLOAT*)&m_Lights[0]->GetColour(), 3 ); shaderConsts->SetFloat( g_pd3dDevice, "Light1Brightness", m_Lights[0]->GetBrightness() ); shaderConsts->SetFloatArray( g_pd3dDevice, "Light2Position", (FLOAT*)&m_Lights[1]->GetPosition(), 3 ); shaderConsts->SetFloatArray( g_pd3dDevice, "Light2Colour", (FLOAT*)&m_Lights[1]->GetColour(), 3 ); shaderConsts->SetFloat( g_pd3dDevice, "Light2Brightness", m_Lights[1]->GetBrightness() ); shaderConsts->SetFloatArray( g_pd3dDevice, "MaterialColour", (FLOAT*)&m_DiffuseColour, 3 ); // If needed shaderConsts->SetFloatArray( g_pd3dDevice, "SpecularStrength", (FLOAT*)&m_SpecularColour, 3 ); // If needed shaderConsts->SetFloat( g_pd3dDevice, "SpecularPower", m_SpecularPower ); }
// Function for pixel shaders that don't require any setup void PS_NullFn( int method, CMatrix4x4* worldMatrix, CCamera* camera ) { // Nothing to do, just ensure we have default render states SetDefaultStates(); }
/** Draws the scene using the light-pre-pass technique */ void D3D11GraphicsEngineTest::DrawSceneLightPrePass() { // Make sure nothing from the previous stage remained SetDefaultStates(); // Collect visible vobs and lights static bool s_done = false; //if(!s_done) { if(Engine::GAPI->GetRendererState()->RendererSettings.DrawVOBs || Engine::GAPI->GetRendererState()->RendererSettings.EnableDynamicLighting) { if(!Engine::GAPI->GetRendererState()->RendererSettings.FixViewFrustum || (Engine::GAPI->GetRendererState()->RendererSettings.FixViewFrustum && FrameVisibleVobs.empty())) Engine::GAPI->CollectVisibleVobs(FrameVisibleVobs, FrameVisibleLights); // Push the data to the GPU FillInstancingBuffer(FrameVisibleVobs); // Reset the collected vobs for next frame MarkVobNonVisibleInFrame(FrameVisibleVobs); s_done = true; } } // Collect mesh list for the world std::list<std::pair<MeshKey, MeshInfo *>> worldMeshList; GetWorldMeshRenderList(worldMeshList); /** Init graphics */ // Vobs need this Engine::GAPI->GetRendererState()->RasterizerState.FrontCounterClockwise = true; Engine::GAPI->GetRendererState()->RasterizerState.SetDirty(); // Set current view-matrix D3DXMATRIX view; Engine::GAPI->GetViewMatrix(&view); Engine::GAPI->SetViewTransform(view); // Wireframe? if(Engine::GAPI->GetRendererState()->RendererSettings.WireframeVobs) { Engine::GAPI->GetRendererState()->RasterizerState.Wireframe = true; } // Set shaders SetActivePixelShader("PS_LPPNormalmappedAlphaTest"); SetActiveVertexShader("VS_ExInstancedObj"); // Init drawcalls SetupVS_ExMeshDrawCall(); SetupVS_ExConstantBuffer(); // Get reference to visual-map std::hash_map<zCProgMeshProto*, MeshVisualInfo*> vis = Engine::GAPI->GetStaticMeshVisuals(); /** z-pre-pass */ // Unbind Pixelshader PS_LPP->Apply(); // Bind GBuffer 2 Context->OMSetRenderTargets(1, GBuffer1_Normals_SpecIntens_SpecPower->GetRenderTargetViewPtr(), DepthStencilBuffer->GetDepthStencilView()); // Set stage SetRenderingStage(DES_Z_PRE_PASS); // Draw the instances of every visual, if available for(std::hash_map<zCProgMeshProto*, MeshVisualInfo*>::const_iterator it = vis.begin(); it != vis.end(); it++) { // Draw all submeshes of this in the world in one batch each if(!(*it).second->Instances.empty()) DrawVisualInstances((*it).second); } SetActiveVertexShader("VS_Ex"); Engine::GAPI->ResetWorldTransform(); // WorldMesh is always at 0,0,0 // Init drawcalls SetupVS_ExMeshDrawCall(); SetupVS_ExConstantBuffer(); // Draw world mesh DrawWorldMeshRenderList(0, worldMeshList); /** Diffuse pass */ // Bind Rendertaget Context->OMSetRenderTargets(1, GBuffer0_Diffuse->GetRenderTargetViewPtr(), DepthStencilBuffer->GetDepthStencilView()); // Set stage SetRenderingStage(DES_MAIN); SetActivePixelShader("PS_Simple"); SetActiveVertexShader("VS_ExInstancedObj"); // Init drawcalls SetupVS_ExMeshDrawCall(); SetupVS_ExConstantBuffer(); // Set depth-func to equal only to make use of the z-pre pass Engine::GAPI->GetRendererState()->DepthState.DepthBufferCompareFunc = GothicDepthBufferStateInfo::CF_COMPARISON_EQUAL; Engine::GAPI->GetRendererState()->DepthState.SetDirty(); UpdateRenderStates(); // Draw the instances of every visual, if available for(std::hash_map<zCProgMeshProto*, MeshVisualInfo*>::const_iterator it = vis.begin(); it != vis.end(); it++) { if(!(*it).second->Instances.empty()) { // Draw all submeshes of this in the world in one batch each DrawVisualInstances((*it).second); // Start new frame on the visual, since we don't need the instancing data anymore //(*it).second->StartNewFrame(); } } Context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); Context->DSSetShader(NULL, NULL, NULL); Context->HSSetShader(NULL, NULL, NULL); SetActiveVertexShader("VS_Ex"); Engine::GAPI->ResetWorldTransform(); // WorldMesh is always at 0,0,0 // Init drawcalls SetupVS_ExMeshDrawCall(); SetupVS_ExConstantBuffer(); // Draw world mesh DrawWorldMeshRenderList(0, worldMeshList); if(Engine::GAPI->GetRendererState()->RendererSettings.WireframeVobs) { Engine::GAPI->GetRendererState()->RasterizerState.Wireframe = false; } FrameVisibleVobs.clear(); FrameVisibleLights.clear(); }
/** Flushes the renderqueue */ void D3D11GraphicsEngineQueued::FlushRenderQueue(bool sortQueue) { BoundPipelineState = DefaultPipelineState; // Execute the commandlists from our worker-threads ExecuteDeferredCommandLists(); // Initial clear Clear(Engine::GAPI->GetRendererState()->GraphicsState.FF_FogColor); // Force farplane Engine::GAPI->SetFarPlane(Engine::GAPI->GetRendererState()->RendererSettings.SectionDrawRadius * WORLD_SECTION_SIZE); // Primitive topology Context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); if(!Engine::GAPI->GetRendererState()->RendererSettings.DisableRendering) { Engine::GAPI->GetRendererState()->RendererInfo.FramePipelineStates = RenderQueue.size(); if(Engine::GAPI->GetRendererState()->RendererSettings.SortRenderQueue) { // Sort the queue, if wanted if(sortQueue) std::sort(RenderQueue.begin(), RenderQueue.end(), PipelineState::PipelineSortItem::cmp); if(Engine::GAPI->GetRendererState()->RendererSettings.DrawThreaded) { // Figure out how much work there is to do for each thread int n = RenderQueue.size() / Engine::RenderingThreadPool->getNumThreads(); int rest = RenderQueue.size() - n * Engine::RenderingThreadPool->getNumThreads(); // If there are less states than threads, just draw them if(n == 0) { // Draw all states for(auto it = RenderQueue.begin(); it != RenderQueue.end(); it++) { BindPipelineState((*it)->AssociatedState); DrawPipelineState((*it)->AssociatedState); (*it)->AssociatedState->StateWasDrawn(); //testset.insert((*it)); } } else { std::vector<std::future<void>> futures; for(int i = 0; i < Engine::RenderingThreadPool->getNumThreads(); i++) { std::future<void> future; // Enqueue threads if(i == Engine::RenderingThreadPool->getNumThreads() - 1) futures.push_back(Engine::RenderingThreadPool->enqueue(D3D11GraphicsEngineQueued::RenderTask, i*n, n + rest)); // last thread simply gets the remainder else futures.push_back(Engine::RenderingThreadPool->enqueue(D3D11GraphicsEngineQueued::RenderTask, i*n, n)); } // Now wait for all of them for(int i = 0; i < futures.size(); i++) { futures[i].wait(); // TODO: Could already execute that contexts commandlist here! } ExecuteDeferredCommandLists(); } } else { // Draw all states for(auto it = RenderQueue.begin(); it != RenderQueue.end(); it++) { BindPipelineState((*it)->AssociatedState); DrawPipelineState((*it)->AssociatedState); (*it)->AssociatedState->StateWasDrawn(); //testset.insert((*it)); } } //std::set<PipelineState::PipelineSortItem*> testset; // Draw all states /*for(auto it = RenderQueue.begin(); it != RenderQueue.end(); it++) { BindPipelineState((*it)->AssociatedState); DrawPipelineState((*it)->AssociatedState); (*it)->AssociatedState->StateWasDrawn(); //testset.insert((*it)); }*/ //if(testset.size() != RenderQueue.size()) // LogWarn() << "Renderer: Submitted one pipelinestate more than once!"; }else { // Draw all states for(auto it = RenderQueue.begin(); it != RenderQueue.end(); it++) { BindPipelineStateForced((*it)->AssociatedState); DrawPipelineState((*it)->AssociatedState); (*it)->AssociatedState->StateWasDrawn(); } } } // Clear the renderqueue for next stage ClearRenderingQueue(); SetDefaultStates(); }