Thursday, March 6, 2025
spot_img

directx11 – Again buffer texture seems distorted earlier than presenting


I’m attempting to ship video information from Dolphin Emulator to a different course of. To take action, I first get a reference to the again buffer from the swap chain.

// in file: D3DGfx.cpp 
// in operate: void Gfx::PresentBackBuffer()
  HRESULT hr = m_swap_chain->GetDXGISwapChain()->GetBuffer(
      0, __uuidof(ID3D11Texture2D), reinterpret_cast(back_buffer.GetAddressOf()));

I then create a shared texture and do a GPU -> GPU copy so I can modify the feel later.

  D3D11_TEXTURE2D_DESC desc;
  ComPtr shared_texture;

  back_buffer->GetDesc(&desc);

  desc.BindFlags = 0;
  desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;

  hr = D3D::device->CreateTexture2D(&desc, nullptr, shared_texture.GetAddressOf());

  D3D::context->CopyResource(shared_texture.Get(), back_buffer.Get());

Then, I create a shared deal with, and map it to shared reminiscence with my different course of for studying.

  hr = shared_texture.As(&dxgi_resource);
  hr = dxgi_resource->GetSharedHandle(&shared_handle);
  void* pMappedMem = MapViewOfFile(hMapFile, FILE_MAP_WRITE, 0, 0,
                                   32);  // map 32 bytes into the digital handle house
  struct SharedTextureData sd;
  sd.deal with = shared_handle;
  sd.width = width;
  sd.top = top;
  if (pMappedMem)
  {
    std::memcpy(pMappedMem, &sd, sizeof(SharedTextureData));
    UnmapViewOfFile(pMappedMem);
  }

Nevertheless, earlier than sharing the feel, I wished to debug the feel to verify I used to be sending the right information. So I created a staging texture, mapped the useful resource, and saved to png to view the feel.

  desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;  // Enable CPU learn entry
  desc.Utilization = D3D11_USAGE_STAGING;
  ComPtr staging_texture;
  hr = D3D::device->CreateTexture2D(&desc, nullptr, staging_texture.GetAddressOf());
  D3D::context->CopyResource(staging_texture.Get(), shared_texture.Get());

  D3D11_MAPPED_SUBRESOURCE mappedResource;
  hr = D3D::context->Map(stagingTexture.Get(), 0, D3D11_MAP_READ, 0, &mappedResource);

  BYTE* information = reinterpret_cast(mappedResource.pData);
  UINT rowPitch = mappedResource.RowPitch;

  //save to png
  stbi_write_png("output.png", width, top, 4, information, rowPitch);

That is the feel that was saved.
Distorted Texture

For reference, that is what the feel seems to be like after being drawn to the display screen by Dolphin Emulator.
Actual Texture

Issues I’ve tried to resolve this subject however didn’t change something or made it worse:

  • Be certain that the format is right (DXGI_FORMAT_R10G10B10A2_UNORM)

  • Regulate the format to be PNG-compatible:

      for (UINT i = 0; i > 2) & 0x3F;
          information[offset + 1] = (information[offset] >> 2) & 0x3F;
          information[offset + 2] = (information[offset] >> 2) & 0x3F;
          information[offset + 3] = (information[offset] >> 2) & 0x3F;
    
        }
      }
    

    This leads to the next texture:
    Even more distorted texture

  • Resolve the feel to a single pattern (if it was multi-sampled) earlier than copying to the
    staging texture

      desc.Utilization = D3D11_USAGE_DEFAULT;
      desc.SampleDesc.Rely = 1;  // Resolve to a single pattern
      desc.SampleDesc.High quality = 0;
    
      ComPtr resolvedTexture;
      hr = D3D::device->CreateTexture2D(&desc, nullptr, resolvedTexture.GetAddressOf());
    
      D3D::context->CopyResource(resolvedTexture.Get(), backBuffer.Get());
    
      D3D::context->ResolveSubresource(resolvedTexture.Get(), 0, backBuffer.Get(), 0,
                                       desc.Format);
    
    

If anybody might determine what the reason for the distortion could be, or recommend a greater approach or a unique method totally, I’d significantly admire it.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles