void SWRenderer::DrawTexture(u8 *texture, int width, int height) { // FIXME: This should add black bars when the game has set the VI to render less than the full xfb. // Save screenshot if (s_bScreenshot) { std::lock_guard<std::mutex> lk(s_criticalScreenshot); TextureToPng(texture, width*4, s_sScreenshotName, width, height, false); // Reset settings s_sScreenshotName.clear(); s_bScreenshot = false; } GLsizei glWidth = (GLsizei)GLInterface->GetBackBufferWidth(); GLsizei glHeight = (GLsizei)GLInterface->GetBackBufferHeight(); // Update GLViewPort glViewport(0, 0, glWidth, glHeight); glScissor(0, 0, glWidth, glHeight); glBindTexture(GL_TEXTURE_2D, s_RenderTarget); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glUseProgram(program); static const GLfloat verts[4][2] = { { -1, -1}, // Left top { -1, 1}, // left bottom { 1, 1}, // right bottom { 1, -1} // right top }; static const GLfloat texverts[4][2] = { {0, 1}, {0, 0}, {1, 0}, {1, 1} }; glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, verts); glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, texverts); glEnableVertexAttribArray(attr_pos); glEnableVertexAttribArray(attr_tex); glUniform1i(uni_tex, 0); glActiveTexture(GL_TEXTURE0); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glDisableVertexAttribArray(attr_pos); glDisableVertexAttribArray(attr_tex); glBindTexture(GL_TEXTURE_2D, 0); GL_REPORT_ERRORD(); }
bool TextureCache::TCacheEntry::Save(const std::string& filename, u32 level) { // TODO: Somehow implement this (D3DX11 doesn't support dumping individual LODs) static bool warn_once = true; if (level && warn_once) { WARN_LOG(VIDEO, "Dumping individual LOD not supported by D3D11 backend!"); warn_once = false; return false; } ID3D11Texture2D* pNewTexture = nullptr; ID3D11Texture2D* pSurface = texture->GetTex(); D3D11_TEXTURE2D_DESC desc; pSurface->GetDesc(&desc); if (desc.Format != DXGI_FORMAT_R8G8B8A8_UNORM) { // Do not support compressed texture dump right now return false; } desc.BindFlags = 0; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; desc.Usage = D3D11_USAGE_STAGING; HRESULT hr = D3D::device->CreateTexture2D(&desc, nullptr, &pNewTexture); bool saved_png = false; if (SUCCEEDED(hr) && pNewTexture) { D3D::context->CopyResource(pNewTexture, pSurface); D3D11_MAPPED_SUBRESOURCE map; hr = D3D::context->Map(pNewTexture, 0, D3D11_MAP_READ_WRITE, 0, &map); if (SUCCEEDED(hr)) { saved_png = TextureToPng((u8*)map.pData, map.RowPitch, filename, desc.Width, desc.Height); D3D::context->Unmap(pNewTexture, 0); } SAFE_RELEASE(pNewTexture); } return saved_png; }
bool DXTexture::Save(const std::string& filename, unsigned int level) { // We can't dump compressed textures currently (it would mean drawing them to a RGBA8 // framebuffer, and saving that). TextureCache does not call Save for custom textures // anyway, so this is fine for now. _assert_(m_config.format == AbstractTextureFormat::RGBA8); // Create a staging/readback texture with the dimensions of the specified mip level. u32 mip_width = std::max(m_config.width >> level, 1u); u32 mip_height = std::max(m_config.height >> level, 1u); CD3D11_TEXTURE2D_DESC staging_texture_desc(DXGI_FORMAT_R8G8B8A8_UNORM, mip_width, mip_height, 1, 1, 0, D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ); ID3D11Texture2D* staging_texture; HRESULT hr = D3D::device->CreateTexture2D(&staging_texture_desc, nullptr, &staging_texture); if (FAILED(hr)) { WARN_LOG(VIDEO, "Failed to create texture dumping readback texture: %X", static_cast<u32>(hr)); return false; } // Copy the selected mip level to the staging texture. CD3D11_BOX src_box(0, 0, 0, mip_width, mip_height, 1); D3D::context->CopySubresourceRegion(staging_texture, 0, 0, 0, 0, m_texture->GetTex(), D3D11CalcSubresource(level, 0, m_config.levels), &src_box); // Map the staging texture to client memory, and encode it as a .png image. D3D11_MAPPED_SUBRESOURCE map; hr = D3D::context->Map(staging_texture, 0, D3D11_MAP_READ, 0, &map); if (FAILED(hr)) { WARN_LOG(VIDEO, "Failed to map texture dumping readback texture: %X", static_cast<u32>(hr)); staging_texture->Release(); return false; } bool encode_result = TextureToPng(reinterpret_cast<u8*>(map.pData), map.RowPitch, filename, mip_width, mip_height); D3D::context->Unmap(staging_texture, 0); staging_texture->Release(); return encode_result; }
void Renderer::DumpFrameToImage(const FrameDumpConfig& config) { std::string filename = GetFrameDumpNextImageFileName(); TextureToPng(config.data, config.stride, filename, config.width, config.height, false); m_frame_dump_image_counter++; }
void Renderer::RunFrameDumps() { Common::SetCurrentThreadName("FrameDumping"); bool dump_to_avi = !g_ActiveConfig.bDumpFramesAsImages; bool frame_dump_started = false; // If Dolphin was compiled without libav, we only support dumping to images. #if !defined(HAVE_LIBAV) && !defined(_WIN32) if (dump_to_avi) { WARN_LOG(VIDEO, "AVI frame dump requested, but Dolphin was compiled without libav. " "Frame dump will be saved as images instead."); dump_to_avi = false; } #endif while (true) { m_frame_dump_start.Wait(); if (!m_frame_dump_thread_running.IsSet()) break; auto config = m_frame_dump_config; if (config.upside_down) { config.data = config.data + (config.height - 1) * config.stride; config.stride = -config.stride; } // Save screenshot if (m_screenshot_request.TestAndClear()) { std::lock_guard<std::mutex> lk(m_screenshot_lock); if (TextureToPng(config.data, config.stride, m_screenshot_name, config.width, config.height, false, (g_ActiveConfig.backend_info.APIType & API_D3D9) != 0)) OSD::AddMessage("Screenshot saved to " + m_screenshot_name); // Reset settings m_screenshot_name.clear(); m_screenshot_completed.Set(); } if (SConfig::GetInstance().m_DumpFrames) { if (!frame_dump_started) { if (dump_to_avi) frame_dump_started = StartFrameDumpToAVI(config); else frame_dump_started = StartFrameDumpToImage(config); // Stop frame dumping if we fail to start. if (!frame_dump_started) SConfig::GetInstance().m_DumpFrames = false; } // If we failed to start frame dumping, don't write a frame. if (frame_dump_started) { if (dump_to_avi) DumpFrameToAVI(config); else DumpFrameToImage(config); } } m_frame_dump_done.Set(); } if (frame_dump_started) { // No additional cleanup is needed when dumping to images. if (dump_to_avi) StopFrameDumpToAVI(); } }
bool TextureCache::TCacheEntry::Save(const std::string& filename, unsigned int level) { // EXISTINGD3D11TODO: Somehow implement this (D3DX11 doesn't support dumping individual LODs) static bool warn_once = true; if (level && warn_once) { WARN_LOG(VIDEO, "Dumping individual LOD not supported by D3D11 backend!"); warn_once = false; return false; } D3D12_RESOURCE_DESC textureDesc = m_texture->GetTex12()->GetDesc(); UINT requiredReadbackBufferSize = D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT + ((textureDesc.Width * 4 + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)) * textureDesc.Height; if (s_texture_cache_entry_readback_buffer_size < requiredReadbackBufferSize) { s_texture_cache_entry_readback_buffer_size = requiredReadbackBufferSize; // We know the readback buffer won't be in use right now, since we wait on this thread // for the GPU to finish execution right after copying to it. SAFE_RELEASE(s_texture_cache_entry_readback_buffer); } if (!s_texture_cache_entry_readback_buffer_size) { CheckHR( D3D::device12->CreateCommittedResource( &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK), D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer(s_texture_cache_entry_readback_buffer_size), D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&s_texture_cache_entry_readback_buffer) ) ); CheckHR(s_texture_cache_entry_readback_buffer->Map(0, nullptr, &s_texture_cache_entry_readback_buffer_data)); } bool saved_png = false; m_texture->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_COPY_SOURCE); D3D12_TEXTURE_COPY_LOCATION dst_location = {}; dst_location.pResource = s_texture_cache_entry_readback_buffer; dst_location.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; dst_location.PlacedFootprint.Offset = D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT; dst_location.PlacedFootprint.Footprint.Depth = 1; dst_location.PlacedFootprint.Footprint.Format = textureDesc.Format; dst_location.PlacedFootprint.Footprint.Width = static_cast<UINT>(textureDesc.Width); dst_location.PlacedFootprint.Footprint.Height = textureDesc.Height; dst_location.PlacedFootprint.Footprint.RowPitch = ((textureDesc.Width * 4 + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); D3D12_TEXTURE_COPY_LOCATION src_location = CD3DX12_TEXTURE_COPY_LOCATION(m_texture->GetTex12(), 0); D3D::current_command_list->CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, nullptr); D3D::command_list_mgr->ExecuteQueuedWork(true); saved_png = TextureToPng( static_cast<u8*>(s_texture_cache_entry_readback_buffer_data) + D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT, dst_location.PlacedFootprint.Footprint.RowPitch, filename, dst_location.PlacedFootprint.Footprint.Width, dst_location.PlacedFootprint.Footprint.Height ); return saved_png; }