Trying, and failing, to create a simple texture creation teste. #339
-
| I have written a simple test that should, in theory, create an immutable texture (initialized with recognizable data), on the GPU, then copy that texture back to the CPU. The intent is simply to validate that the immutable texture was created with the expected data. The test always fails; whether it is because the texture was not created with the expected data, or because I failed to copy the texture back to the CPU is not clear. Can someone explain what I am doing wrong? static bool CreateAndVerifyTexture(IRenderDevice* pDevice, IDeviceContext* pContext) { // Input vector data // Let's create a simple 4x4 texture with R32_FLOAT format constexpr Uint32 Width = 4; constexpr Uint32 Height = 4; constexpr Uint32 DataSize = Width * Height; std::vector<float> sourceData(DataSize); // Fill the vector with recognizable data for (Uint32 i = 0; i < Width * Height; ++i) { sourceData[i] = static_cast<float>(PI_F); } // Create texture description TextureDesc TexDesc; TexDesc.Name = "Test Immutable Texture"; TexDesc.Type = RESOURCE_DIM_TEX_2D; TexDesc.Width = Width; TexDesc.Height = Height; TexDesc.Format = TEX_FORMAT_R32_FLOAT; TexDesc.Usage = USAGE_IMMUTABLE; TexDesc.BindFlags = BIND_SHADER_RESOURCE; TexDesc.MipLevels = 1; // Prepare texture data TextureData TexData; TextureSubResData SubResData; SubResData.pData = sourceData.data(); SubResData.Stride = Width * sizeof(float); // Bytes per row TexData.pSubResources = &SubResData; TexData.NumSubresources = 1; // Create the texture RefCntAutoPtr<ITexture> pTexture; pDevice->CreateTexture(TexDesc, &TexData, &pTexture); if (!pTexture) { std::cout << "Failed to create texture" << std::endl; return false; } std::cout << "Texture created successfully" << std::endl; // Create a staging texture for reading back the data TextureDesc StagingTexDesc = TexDesc; StagingTexDesc.Name = "Staging Texture"; StagingTexDesc.Usage = USAGE_STAGING; StagingTexDesc.CPUAccessFlags = CPU_ACCESS_READ; StagingTexDesc.BindFlags = BIND_NONE; RefCntAutoPtr<ITexture> pStagingTexture; pDevice->CreateTexture(StagingTexDesc, nullptr, &pStagingTexture); if (!pStagingTexture) { std::cout << "Failed to create staging texture" << std::endl; return false; } // Copy the data from the source texture to the staging texture pContext->CopyTexture( CopyTextureAttribs{ pTexture, // Source texture RESOURCE_STATE_TRANSITION_MODE_TRANSITION, // Transition source texture pStagingTexture, // Destination texture RESOURCE_STATE_TRANSITION_MODE_TRANSITION // Transition destination texture }); // Map the staging texture to read the data MappedTextureSubresource MappedData; pContext->MapTextureSubresource( pStagingTexture, // Texture to map 0, // Mip level 0, // Array slice MAP_READ, // Map type MAP_FLAG_DO_NOT_WAIT, // Map flags nullptr, // Box MappedData // Output mapped data ); // Read back the data and verify std::vector<float> readBackData; const float* data = static_cast<const float*>(MappedData.pData); readBackData.reserve(DataSize); std::copy_n(data, DataSize, std::back_inserter(readBackData)); bool dataMatches = sourceData == readBackData; return dataMatches; } |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
| You are missing synchronization between CPU and GPU: you map the texture before the data is actually copied to it. Add |
Beta Was this translation helpful? Give feedback.
You are missing synchronization between CPU and GPU: you map the texture before the data is actually copied to it.
Add
pContext->WaitForIdle()right beforepContext->MapTextureSubresource