Sign in to follow this  

OpenGL DirectX11: Reading raw texture data on the CPU

This topic is 1535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I'm trying to read the raw texture values of textures in DirectX 11 and I've run into a bit of a weird problem. My code seems to work fine for 512x512 textures but as soon as I try reading data from a 1024x1024 texture, I get a crash at the memcpy() line.

 

Here's relevant code:

// set up the loaded textures as subresources
D3D11_SUBRESOURCE_DATA* subresources = new D3D11_SUBRESOURCE_DATA[texCount];
 
// copy the loaded texture data into the subresource structures
for(UINT i = 0; i < texCount; ++i) {
D3D11_MAPPED_SUBRESOURCE mappedSubResource;
 
HRESULT mapResult = p_deviceContext_->Map(textures[i], 0, D3D11_MAP_READ, 0, &mappedSubResource);
 
if(FAILED(mapResult)) {
MessageBox(NULL, L"Failed mapping a subresource.", L"Error", 0);
return;
}
 
// size of the texture's data defined by the length of a single row of data * the total number of rows (texture height)
subresources[i].pSysMem = new BYTE[mappedSubResource.RowPitch * texHeight];
memcpy(const_cast<void*>(subresources[i].pSysMem), mappedSubResource.pData, mappedSubResource.RowPitch * texHeight);
subresources[i].SysMemPitch = mappedSubResource.RowPitch;
subresources[i].SysMemSlicePitch = mappedSubResource.DepthPitch;
 
p_deviceContext_->Unmap(textures[i], 0);
 
}
 
I'm not quite sure as to why this crashes with 1024x1024 textures. What makes it even weirder is the fact that the textures have the same format, all I've done is scaled the higher resolution ones down.
 
Something similar was happening when I was using OpenGL as well: loading in textures 1024x1024 seemed to crash the application. However in this case, the textures get loaded in without a problem, it's just the reading that fails. (NOTE: I'm not writing to the textures using the GPU, so there are no CPU/GPU read/write access problems).
 
Thanks in advance!

Share this post


Link to post
Share on other sites

Maybe an issue with the last row. RowPitch tells you the offset between rows, it doesn't say anywhere in the spec that last row should be RowPitch in size.

I suggest you try to loop on each row, and copy exactly (PixelSizeInBytes*TexWidth), maybe it will help.

 

BTW, this is a good advice in any case - you then allocate less system memory, and everything in consecutive in memory, resulting in better cache behavior.

Edited by N.I.B.

Share this post


Link to post
Share on other sites

Maybe an issue with the last row. RowPitch tells you the offset between rows, it doesn't say anywhere in the spec that last row should be RowPitch in size.

I suggest you try to loop on each row, and copy exactly (PixelSizeInBytes*TexWidth), maybe it will help.

 

BTW, this is a good advice in any case - you then allocate less system memory, and everything in consecutive in memory, resulting in better cache behavior.

 

Thanks for your quick reply! I'm not quite sure how to loop row by row, the following code crashes around j = 344:

 

UINT pixelSize = mappedSubResource.RowPitch / texWidth;
BYTE** pixels = new BYTE*[texHeight];
void* dataPointer = mappedSubResource.pData;
 
for(int j = 0; j < texHeight; ++j) {
pixels[j] = new BYTE[pixelSize * texWidth];
memcpy(pixels[j], dataPointer, pixelSize * texWidth);
dataPointer = static_cast<char*>(dataPointer) + (pixelSize * texWidth);
}
 
I tried changing my original code to copy rowPitch * (texHeight - 1) to discard the final row but that didn't change anything.
 
Thanks in advance once more!
 
EDIT: could it be because of the format of the textures? Currently it's DXGI_FORMAT_BC2_UNORM.
Edited by dr4cula

Share this post


Link to post
Share on other sites

1314029819767.png
 
When I was loading the textures, I used ZeroMemory() to fill in the D3DX11_IMAGE_LOAD_INFO struct and only set the format, usage and CPU access flags myself. However, this caused the full mipmap chain to be created because
 
 

MipLevels
Type: UINT
The maximum number of mipmap levels in the texture. See the remarks in D3D11_TEX1D_SRV. Using 0 or D3DX11_DEFAULT will cause a full mipmap chain to be created.

 
 
Once I changed the MipLevels value to 1, my original copying code worked. Oops!
 
Thanks for your replies though!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Similar Content

    • By _OskaR
      Hi,
      I have an OpenGL application but without possibility to wite own shaders.
      I need to perform small VS modification - is possible to do it in an alternative way? Do we have apps or driver modifictions which will catch the shader sent to GPU and override it?
    • By xhcao
      Does sync be needed to read texture content after access texture image in compute shader?
      My simple code is as below,
      glUseProgram(program.get());
      glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
      glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
      glDispatchCompute(1, 1, 1);
      // Does sync be needed here?
      glUseProgram(0);
      glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
      glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
      glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
       
      Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?
    • By Jonathan2006
      My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:
      // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
    • By dpadam450
      I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.
       
      #define NUM_LIGHTS 2
      struct Light
      {
          vec3 position;
          vec3 diffuse;
          float attenuation;
      };
      uniform Light Lights[NUM_LIGHTS];
       
       
    • By pr033r
      Hello,
      I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things. 
      I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help. 
      My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
      Exe file (if you want to look) and models folder (for those who will download the sources):
      http://leteckaposta.cz/367190436
      Thanks for any help...

  • Popular Now