CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/GPU/D3D11/GPU_D3D11.cpp
Views: 1401
// Copyright (c) 2017- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <string>1819#include "Common/Log.h"20#include "Common/Serialize/Serializer.h"21#include "Common/GraphicsContext.h"22#include "Common/System/System.h"23#include "Common/Profiler/Profiler.h"24#include "Common/Data/Text/I18n.h"25#include "Core/Debugger/Breakpoints.h"26#include "Core/MemMapHelpers.h"27#include "Core/MIPS/MIPS.h"28#include "Core/Config.h"29#include "Core/System.h"3031#include "GPU/GPUState.h"32#include "GPU/ge_constants.h"33#include "GPU/GeDisasm.h"3435#include "GPU/Common/FramebufferManagerCommon.h"36#include "GPU/D3D11/ShaderManagerD3D11.h"37#include "GPU/D3D11/GPU_D3D11.h"38#include "GPU/D3D11/FramebufferManagerD3D11.h"39#include "GPU/D3D11/DrawEngineD3D11.h"40#include "GPU/D3D11/TextureCacheD3D11.h"41#include "GPU/D3D11/D3D11Util.h"4243GPU_D3D11::GPU_D3D11(GraphicsContext *gfxCtx, Draw::DrawContext *draw)44: GPUCommonHW(gfxCtx, draw), drawEngine_(draw,45(ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE),46(ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT)) {47device_ = (ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE);48context_ = (ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT);49D3D_FEATURE_LEVEL featureLevel = (D3D_FEATURE_LEVEL)draw->GetNativeObject(Draw::NativeObject::FEATURE_LEVEL);5051stockD3D11.Create(device_);5253shaderManagerD3D11_ = new ShaderManagerD3D11(draw, device_, context_, featureLevel);54framebufferManagerD3D11_ = new FramebufferManagerD3D11(draw);55framebufferManager_ = framebufferManagerD3D11_;56textureCacheD3D11_ = new TextureCacheD3D11(draw, framebufferManager_->GetDraw2D());57textureCache_ = textureCacheD3D11_;58drawEngineCommon_ = &drawEngine_;59shaderManager_ = shaderManagerD3D11_;60drawEngine_.SetShaderManager(shaderManagerD3D11_);61drawEngine_.SetTextureCache(textureCacheD3D11_);62drawEngine_.SetFramebufferManager(framebufferManagerD3D11_);63drawEngine_.Init();64framebufferManagerD3D11_->SetTextureCache(textureCacheD3D11_);65framebufferManagerD3D11_->SetShaderManager(shaderManagerD3D11_);66framebufferManagerD3D11_->SetDrawEngine(&drawEngine_);67framebufferManagerD3D11_->Init(msaaLevel_);68textureCacheD3D11_->SetFramebufferManager(framebufferManagerD3D11_);69textureCacheD3D11_->SetShaderManager(shaderManagerD3D11_);7071// Sanity check gstate72if ((int *)&gstate.transferstart - (int *)&gstate != 0xEA) {73ERROR_LOG(Log::G3D, "gstate has drifted out of sync!");74}7576// No need to flush before the tex scale/offset commands if we are baking77// the tex scale/offset into the vertices anyway.78UpdateCmdInfo();79gstate_c.SetUseFlags(CheckGPUFeatures());8081BuildReportingInfo();8283// Some of our defaults are different from hw defaults, let's assert them.84// We restore each frame anyway, but here is convenient for tests.85textureCache_->NotifyConfigChanged();86}8788GPU_D3D11::~GPU_D3D11() {89stockD3D11.Destroy();90}9192u32 GPU_D3D11::CheckGPUFeatures() const {93u32 features = GPUCommonHW::CheckGPUFeatures();9495// Accurate depth is required because the Direct3D API does not support inverse Z.96// So we cannot incorrectly use the viewport transform as the depth range on Direct3D.97features |= GPU_USE_ACCURATE_DEPTH;9899features |= GPU_USE_TEXTURE_FLOAT;100features |= GPU_USE_INSTANCE_RENDERING;101features |= GPU_USE_TEXTURE_LOD_CONTROL;102103uint32_t fmt4444 = draw_->GetDataFormatSupport(Draw::DataFormat::A4R4G4B4_UNORM_PACK16);104uint32_t fmt1555 = draw_->GetDataFormatSupport(Draw::DataFormat::A1R5G5B5_UNORM_PACK16);105uint32_t fmt565 = draw_->GetDataFormatSupport(Draw::DataFormat::R5G6B5_UNORM_PACK16);106if ((fmt4444 & Draw::FMT_TEXTURE) && (fmt565 & Draw::FMT_TEXTURE) && (fmt1555 & Draw::FMT_TEXTURE)) {107features |= GPU_USE_16BIT_FORMATS;108}109110return CheckGPUFeaturesLate(features);111}112113void GPU_D3D11::DeviceLost() {114draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);115// Simply drop all caches and textures.116// FBOs appear to survive? Or no?117shaderManager_->ClearShaders();118drawEngine_.ClearInputLayoutMap();119textureCache_->Clear(false);120121GPUCommonHW::DeviceLost();122}123124void GPU_D3D11::DeviceRestore(Draw::DrawContext *draw) {125GPUCommonHW::DeviceRestore(draw);126// Nothing needed.127}128129void GPU_D3D11::BeginHostFrame() {130GPUCommonHW::BeginHostFrame();131132textureCache_->StartFrame();133drawEngine_.BeginFrame();134135shaderManager_->DirtyLastShader();136137framebufferManager_->BeginFrame();138gstate_c.Dirty(DIRTY_PROJTHROUGHMATRIX);139140if (gstate_c.useFlagsChanged) {141// TODO: It'd be better to recompile them in the background, probably?142// This most likely means that saw equal depth changed.143WARN_LOG(Log::G3D, "Shader use flags changed, clearing all shaders and depth buffers");144shaderManager_->ClearShaders();145framebufferManager_->ClearAllDepthBuffers();146drawEngine_.ClearInputLayoutMap();147gstate_c.useFlagsChanged = false;148}149}150151void GPU_D3D11::FinishDeferred() {152// This finishes reading any vertex data that is pending.153drawEngine_.FinishDeferred();154}155156void GPU_D3D11::GetStats(char *buffer, size_t bufsize) {157size_t offset = FormatGPUStatsCommon(buffer, bufsize);158buffer += offset;159bufsize -= offset;160if ((int)bufsize < 0)161return;162snprintf(buffer, bufsize,163"Vertex, Fragment shaders loaded: %d, %d\n",164shaderManagerD3D11_->GetNumVertexShaders(),165shaderManagerD3D11_->GetNumFragmentShaders()166);167}168169170