Jump to content
  • Advertisement

KieronH

Member
  • Content Count

    12
  • Joined

  • Last visited

Community Reputation

146 Neutral

About KieronH

  • Rank
    Member
  1. KieronH

    Copy 2D array into Texture2D?

      I've fixed the crashes now and am on the way to solving this problem. However, using the code below the image appears sheared so there is still a problem with the numbers somewhere in there and I can't seem to work out what it is. const uint32_t texelSize = 16; // size of DXGI_FORMAT_R32G32B32A32_FLOAT D3D11_MAPPED_SUBRESOURCE mappedResource; dc->Map(writeToTex, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); uint8_t* dstData = reinterpret_cast<uint8_t*>(mappedResource.pData); const uint8_t* srcData = reinterpret_cast<const uint8_t*>(grid[0]); for (uint32_t i = 0; i < height; ++i) { memcpy(dstData, srcData, width * texelSize); dstData += mappedResource.RowPitch; srcData += width * texelSize; } dc->Unmap(writeToTex, 0);
  2. KieronH

    Geometry Shader Blank Screen

      Thanks for the advice, I did try using the graphics debugger to debug the shader however it failed to build the shader trace everytime. To be honest I'm just happy I have it working for now!
  3. KieronH

    Geometry Shader Blank Screen

      Yeah so was I as well, but at this point I've got other things to work out, I'll come back to this later if I need to!    And that was the next problem, scaling takes the aspect ratio of the rendertarget but that's an easy enough fix! Thanks though!
  4. KieronH

    Copy 2D array into Texture2D?

      I am indeed looking to create a dynamic texture and update it frequently.   First thing's first, yeah sorry I realised after that the memcpy was completely wrong when I pasted it. That's something I fixed before anyone replied, but thanks for pointing it out in any case.   Secondly, I've tried the solution you're suggesting here with similar results. It actually crashes with your solution as well, also I have been able to get something of the image output to the screen but it is messed up and you can make out only part of the image. It also crashes every now and then as well. The only other thing I can think of being the problem is the actual array that's being passed in. Looking further into it (I didn't write the array part) it seems it's a struct made up of 4 floats. The floats make up an RGBA value for each pixel of the texture.
  5. So here's my problem, I have a 2D array filled with values that I want to be accessed on the GPU. Firstly I thought it was best to use a texture2d to achieve this. This needs to be updated when I need to pass new values over to the GPU.   Having looked around for a solution I came across this: http://www.gamedev.net/topic/606100-solved-dx11-updating-texture-data/   This gave me a further idea of how to achieve something like this but I am still having trouble actually doing it.    At the moment there's not much code to show other than creation of a texture and the array that I need to pass over. I've tried many different options with them all resulting in either completely wrong textures or crashes. I'll show what I have so far though: D3D11_TEXTURE2D_DESC desc2; ZeroMemory(&desc2, sizeof(desc2)); desc2.Width = width; desc2.Height = height; desc2.ArraySize = 1; desc2.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; desc2.Usage = D3D11_USAGE_DYNAMIC; desc2.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; desc2.BindFlags = D3D11_BIND_SHADER_RESOURCE; desc2.MipLevels = 1; desc2.SampleDesc.Count = 1; desc2.SampleDesc.Quality = 0; D3D11_SUBRESOURCE_DATA data; data.pSysMem = grid; data.SysMemPitch = desc2.Width * 16; data.SysMemSlicePitch = 0; device->CreateTexture2D(&desc2, NULL, &textureBuffer); D3D11_MAPPED_SUBRESOURCE mappedResource; deviceContext->Map(textureBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); memcpy(&mappedResource.pData, &data, width); deviceContext->Unmap(textureBuffer, 0); I know most of this is completely wrong. But hopefully it helps with visualising what I want. 'grid' is the 2D array data. Each value in the array is a float4 value.
  6. KieronH

    Geometry Shader Blank Screen

    I simply unbound the geometry shader after the draw call like so:   deviceContext->GSSetShader(NULL, 0, 0);   And this is the revised shader code that now works as intended. Not sure if it's a perfect fix but it works for me: //#pragma pack_matrix( row_major ) cbuffer ConstantBuffer : register(b0) { matrix World; matrix View; matrix Projection; } //-------------------------------------------------------------------------------------- // Vertex Shader //-------------------------------------------------------------------------------------- struct particles { float3 position : POSITION; float3 velocity; float life; float age; bool alive; float startSize; float endSize; float4 startColour; float4 endColour; }; struct VS_INPUT { uint id : SV_VERTEXID; }; struct GS_INPUT { float3 Pos : POSITION; float4 Col : COLOUR; }; struct PS_INPUT { float4 Pos : SV_POSITION; float4 Col : COLOUR; float2 TexCoord : TEXCOORD; }; struct GeoOut { float4 PosH : SV_POSITION; float2 Tex : TEXCOORD; uint PrimID : SV_PrimitiveID; }; static const float scale = 0.001f; static const float4 g_positions[4] = { float4(-scale, scale, 0, 0), float4(scale, scale, 0, 0), float4(-scale, -scale, 0, 0), float4(scale, -scale, 0, 0), }; static const float2 g_texcoords[4] = { float2(0, 1), float2(1, 1), float2(0, 0), float2(1, 0), }; StructuredBuffer<particles> particlesIn : register(t0); GS_INPUT VS(VS_INPUT input) { GS_INPUT output; uint index = input.id; particles p = particlesIn[index]; output.Pos = mul(p.position, World); output.Pos = mul(output.Pos, View); output.Pos = mul(output.Pos, Projection); output.Col = p.startColour; return output; } [maxvertexcount(4)] void GS(point GS_INPUT input[1], inout TriangleStream<PS_INPUT> SpriteStream) { PS_INPUT output; // Transform to view space float4 viewposition = mul(float4(input[0].Pos, 1.0f), World); // Emit two new triangles for (int i = 0; i < 4; i++) { output.Pos = viewposition + g_positions[i]; output.TexCoord = g_texcoords[i]; output.Col = input[0].Col; SpriteStream.Append(output); } SpriteStream.RestartStrip(); } //-------------------------------------------------------------------------------------- // Pixel Shader //-------------------------------------------------------------------------------------- float4 PS(GS_INPUT input) : SV_Target { float4 pos; pos.xyz = input.Pos.xyz; return input.Col; } Any other thoughts on how this could be improved are always welcome! But this is now working as intended for my purposes. :)
  7. KieronH

    Geometry Shader Blank Screen

    I've fixed it now, there were errors in how I was calculating positions in the shader. Thanks for your help!   Also, the primitive type is always point list as I'm only making a particle system, there isn't anything else to draw. I hope I've understood what you're trying to say? Thanks though.
  8. KieronH

    Geometry Shader Blank Screen

      Ah, forgot to unbind it! Thank you! That solves my problem of nothing showing up on screen, so the particle counter and fps counter are working now as intended. I just can't see any particles on the screen. This leads me to believe it's something to do with the positions and matrices. I've changed the line you said and there are still no particles on the screen.
  9. Okay so I'm trying to do point to quad expansion on particle data that is being fed through the graphics pipeline. If I have a simple vertex and pixel shader everything works perfectly (rendered as points). However, when I add in the geometry shader stage everything disappears from the screen. I don't just mean the particles, even the text that is being rendered using spritebatch from the DirectXTK. Any ideas as to what might be causing this? I'll post my shader code. It appears to happen more specifically when I use this line: deviceContext->GSSetShader(geometryShader, 0, 0); I've checked everything compiles correctly and that seems to be fine.   Here's the shader code I'm referring too: //#pragma pack_matrix( row_major ) cbuffer ConstantBuffer : register(b0) { matrix World; matrix View; matrix Projection; } cbuffer cbFixed { // // Compute texture coordinates to stretch texture over quad. // float2 gTexC[4] = { float2(0.0f, 1.0f), float2(0.0f, 0.0f), float2(1.0f, 1.0f), float2(1.0f, 0.0f) }; }; struct particles { float3 position : POSITION; float3 velocity; float life; float age; bool alive; float startSize; float endSize; float4 startColour; float4 endColour; }; struct VS_INPUT { uint id : SV_VERTEXID; }; struct GS_INPUT { float3 Pos : POSITION; }; struct PS_INPUT { float4 Pos : SV_POSITION; float4 Col : COLOUR; float2 TexCoord : TEXCOORD; }; struct GeoOut { float4 PosH : SV_POSITION; float2 Tex : TEXCOORD; uint PrimID : SV_PrimitiveID; }; static const float scale = 0.5f; static const float4 g_positions[4] = { float4(-scale, scale, 0, 0), float4(scale, scale, 0, 0), float4(-scale, -scale, 0, 0), float4(scale, -scale, 0, 0), }; static const float2 g_texcoords[4] = { float2(0, 1), float2(1, 1), float2(0, 0), float2(1, 0), }; StructuredBuffer<particles> particlesIn : register(t0); GS_INPUT VS(VS_INPUT input) { GS_INPUT output; output.Pos.xyz = particlesIn[input.id].position; return output; } [maxvertexcount(4)] void GS(point GS_INPUT input[1], inout TriangleStream<PS_INPUT> SpriteStream) { PS_INPUT output; float3 CameraLocation = float3(0.0f, 1.0f, -5.0f); float dist = saturate(length(input[0].Pos - CameraLocation.xyz) / 100.0f); //float4 color = float4( 0.2f, 1.0f, 0.2f, 0.0f ) * dist + float4( 1.0f, 0.1f, 0.1f, 0.0f ) * ( 1.0f - dist ); //float4 color = float4( 0.2f, 1.0f, 1.0f, 0.0f ) * dist + float4( 1.0f, 0.1f, 0.1f, 0.0f ) * ( 1.0f - dist ); float4 color = float4(0.2f, 0.2f, 1.0f, 0.0f) * dist + float4(1.0f, 0.1f, 0.1f, 0.0f) * (1.0f - dist); // Transform to view space float4 viewposition = mul(float4(input[0].Pos, 1.0f), World); // Emit two new triangles for (int i = 0; i < 4; i++) { float4 p = mul(input[0].Pos, World); p = mul(p, View); p = mul(p, Projection); output.Pos = p; output.TexCoord = g_texcoords[i]; output.Col = color; SpriteStream.Append(output); } SpriteStream.RestartStrip(); } //-------------------------------------------------------------------------------------- // Pixel Shader //-------------------------------------------------------------------------------------- float4 PS(PS_INPUT input) : SV_Target { return float4(1.0f, 1.0f, 0.0f, 1.0f); }
  10. Okay, so I've fixed it! Guess what? It was to do with the projection and view matrices. Basically from what I understand I wasn't setting them correctly. And from debugging the vertex shader I realised that it was losing some values for the matrices as it was going through. So I tore allof it out and wrote the camera part again. Now it works perfectly, that took much longer than it should have!
  11.   You don't mention what view and projection you're using, but, if that is a position, don't you need to set w = 1?     Sorry for not using the code tags, I'll start using them now.    I don't understand how it can anything to do with the view or projection I'm using though as I've tested this out (using input layout, vertex buffers and with the same view and projection) and it draws completely fine. I honestly think it's something to do with how I'm using the SRV in the vertex shader or how I'm using the SRV full stop. To follow up on what you said about shouldn't w = 1. I've tried that and again a value of (0.0f,0.0f,0.0f,1.0f) gives a point drawn in the centre of the screen. However if I was to change it to something like (0.0f, 0.1f, 0.0f, 1.0f) just as an example I get absolutely nothing on the screen.   Having looked through multiple examples that are attempting to do the same thing I am really struggling to find differences in how they are setting this up and using it. 
  12. First thing's first, this is my very first post here so I'm not really sure of how things work here so bear with me a little! I don't normally resort to forums but I've been stuck with this for a few days now with no success and it's driving me up the wall!   Okay so basically what I'm attempting to do is read some values from an SRV in a vertex shader. I am doing this without setting a vertex buffer or input layout. So they are both null. The data is just an array of float4s that hold a position of a particle.    Here is my vertex and pixel shader:   cbuffer ConstantBuffer : register(b0) { matrix World; matrix View; matrix Projection; }   //-------------------------------------------------------------------------------------- // Vertex Shader //--------------------------------------------------------------------------------------   struct VS_INPUT { uint id : SV_VERTEXID; };   struct PS_INPUT { float4 Pos : SV_POSITION; float4 Col : COLOUR; };   struct particles { float4 position : POSITION; };   StructuredBuffer<particles>   particlesIn : register(t0);   PS_INPUT VS(VS_INPUT input) { PS_INPUT output = (PS_INPUT)0;   uint index = input.id; particles p = particlesIn[index];     output.Pos = mul(p.position, World); output.Pos = mul(output.Pos, View); output.Pos = mul(output.Pos, Projection);   output.Col = float4(1.0f, 1.0f, 1.0f, 1.0f);   return output; }     //-------------------------------------------------------------------------------------- // Pixel Shader //-------------------------------------------------------------------------------------- float4 PS(PS_INPUT input) : SV_Target { return input.Col;   //return float4(1.0f, 1.0f, 0.0f, 1.0f); }   Now here's the thing - I pass a value of (0.0f,0.0f,0.0f,0.0f) into this and I get a white point in the centre of the screen. This is the correct behaviour. However, if I change any one of the x,y or z values (by any value it can be as small as 0.000001f or even smaller) I get a blank screen with no point whatsoever. Now I thought it might be to do with some of the matrices that I'm using, but I have tested it out with another program and they are fine. At this point I'm out of ideas, I've tried all sorts to get it working, including using the graphics debugger to look through values that are being passed through the vertex shader. To my surprise they were actually correct! (I think?) What I have posted is just the shaders. If I need to post any other code for you to look at please let me know. I'm desperate for new ideas to take this forward!    Thanks in advance!
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!