• Advertisement

korvax

Member
  • Content count

    76
  • Joined

  • Last visited

Everything posted by korvax

  1. Hi, Im having some problem with moving out my Position Clip space calculation out of the shader and in to cpu space. That is creating a WorldViewProjection matrix and use that in the shaders.     The following shader code works fine  PS Shader. struct PS_INPUT { float4 PositionSS : SV_POSITION; float2 TexCoord : TEXCOORD0; }; //-------------------------------------------------------------------------------------- // Pixel Shader //-------------------------------------------------------------------------------------- PSOutput main(in PS_INPUT input) { PSOutput output; if (isTextured) output.Color = txDiffuse.Sample(samLinear, input.TexCoord); else output.Color = diffuse; return output; } cbuffer VSConstants : register(cb0 ) { matrix World; matrix View; matrix Projection; matrix WordViewProjection; } //-------------------------------------------------------------------------------------- struct VS_INPUT { float3 Position : POSITION; float2 TexCoord : TEXCOORD0; float3 Normal : NORMAL; }; struct VS_OUTPUT { float4 PositionCS : SV_POSITION; float2 TexCoord : TEXCOORD0; }; //-------------------------------------------------------------------------------------- // Vertex Shader //-------------------------------------------------------------------------------------- VS_OUTPUT main(in VS_INPUT input) { VS_OUTPUT output = (VS_OUTPUT)0; float4 position = float4(input.Position, 1.0f); output.PositionCS = mul(position, World); output.PositionCS = mul(output.PositionCS, View); output.PositionCS = mul(output.PositionCS, Projection); output.TexCoord = input.TexCoord; return output; } But I want to replace  output.PositionCS = mul(output.PositionCS, View); output.PositionCS = mul(output.PositionCS, Projection); with  output.PositionCS = mul(position, WordViewProjection); and my calculations for ConstantBuffer is VSC_WORLD PipelineManagerDX11::CalculateModelSpace(const IRenderable3D &renderble) { XMMATRIX model_matrix = renderble.World.GetTransformationMatrix(); VSC_WORLD vsS; vsS.mWorld = XMMatrixTranspose(model_matrix); vsS.mView = XMMatrixTranspose(this->m_View); vsS.mProjection = XMMatrixTranspose(this->m_Projection); XMMATRIX WorldView = model_matrix*this->m_View; XMMATRIX ViewProjection = this->m_View*this->m_Projection; vsS.mWordViewProjection = XMMatrixTranspose(WorldView*ViewProjection);; return vsS; } Im not sure what I'm doing wrong here. But its not working, my output is completely black so something is going badly wrong with the PositionCS Calc. Im pretty sure I'm missing something basic, not sure what though.   All the help is much appreciated. 
  2. Help with WorldViewProjection

    HI, thx for you help, problem solved your help was much appreciated.
  3. Help with WorldViewProjection

    Thx tonemgub, im now using ROW, so atleast I have no transpose..
  4. Help with WorldViewProjection

    Hi, thx, but even if I change to vsS.mWordViewProjection = XMMatrixTranspose(model_matrix*this->m_View*this->m_Projection); Im still getting same black output. I think it has to be with my WVP matrix, because it works when im doing the calculation on the shader.
  5. HI,  I have a question regarding Constant Buffers in Directx 11, I'm not sure if this should go to the beginners forum feel free to move it if so.  Anyhow. Im getting the following error:    D3D11 WARNING: ID3D11DeviceContext::DrawIndexed: The size of the Constant Buffer at slot 0 of the Pixel Shader unit is too small (48 bytes provided, 80 bytes, at least, expected). This is OK, as out-of-bounds reads are defined to return 0. It is also possible the developer knows the missing data will not be used anyway. This is only a problem if the developer actually intended to bind a sufficiently large Constant Buffer for what the shader expects.  [ EXECUTION WARNING #351: DEVICE_DRAW_CONSTANT_BUFFER_TOO_SMALL]   If I'm understanding this message correctly it says that my buffer is 48 bytes large but it should be 80 bytes.   This is how my buffer looks: struct cbPerLight { XMFLOAT4 lightDirection; //16 bytes XMFLOAT4 lightColor; //16 bytes XMFLOAT4 lightPosition; //16 bytes XMFLOAT4 lightAngle; //16 bytes XMFLOAT4 lightRange; //16 bytes };  16*5 = 80 bytes. And all of the is 16 bytes each so no problem that that one of the is over the 16 block.   This is how my PixelShader looks //--------------------------------------------------------------------------------------------------------------------- Texture2D txDiffuse : register(t0); Texture2D txNormal : register(t1); Texture2D txDepth : register(t2); Texture2D txSpecular : register(t3); //--------------------------------------------------------------------------------------------------------------------- SamplerState samLinear0 : register( s0 ); SamplerState samLinear1 : register( s1 ); SamplerState samLinear2 : register( s2 ); SamplerState samLinear3 : register( s3 ); //--------------------------------------------------------------------------------------------------------------------- cbuffer cbLight : register(b0) { float4 lightDirection; float4 lightColor; float4 lightPosition; float4 lightAngles; float4 lightRange; } //--------------------------------------------------------------------------------------------------------------------- struct PS_INPUT { float4 position : SV_POSITION; float2 texcoord : TEXCOORD0; }; //--------------------------------------------------------------------------------------------------------------------- float4 main(PS_INPUT input) : SV_Target0 { float4 diffuseAlbedo = txDiffuse.Sample(samLinear0, input.texcoord); float4 normal = txNormal.Sample(samLinear1, input.texcoord); float4 depth = txDepth.Sample(samLinear2, input.texcoord); float4 specular = txDepth.Sample(samLinear3, input.texcoord); float4 L = lightPosition - input.position; float distance = length(L); L /= distance; float attenuation = max(0, 1.0f - (distance / lightRange.x)); float4 L2 = lightDirection; float rho = dot(-L, L2); attenuation *= saturate((rho - lightAngles.y) / (lightAngles.x - lightAngles.y)); const float diffuseNormalizationFactor = 1.0f / 3.14159265f; float d1 = dot(normal, L); float nDotL = saturate(d1); float4 diffuse = nDotL * lightColor * diffuseAlbedo * diffuseNormalizationFactor; return diffuse * attenuation; } Its a light stage in my render, should render a spotlight when its finished (It not complete yet).   I have to say that I'm not really understanding how the constant buffer should be allocated in memory but I think they should be in blocks of 16bytes and no variable should cross a 16 byte block.  And one could use padding variable to achieve this..  My output is currently black because it seems that some of variables seems to be corrupt in the shader,, with makes me think that it has to do something with the allocation.    But in this case I'm not sure where it gets 48bytes from. Does anyone has any suggestions?    thx for you help.
  6. Hi, thx a lot for your help. No more warnings, now off to the second problem to try to make the shader to work..    cheers.
  7. Hi, I realized that I hade another constant buffer that was 48bytes long on b0 as well (thx megadan). I have now changed light structure to b1, no warnings, but all values in the structure (in shader space) are zero, so I guess I need to change something on the c++ side so the structure fills at register 1 and not 0? struct cbPerLight sCbLight; sCbLight.lightColor = light.Color; sCbLight.lightDirection = light.Direction; sCbLight.lightPosition = light.Position; sCbLight.lightAngle = light.Angles; sCbLight.lightRange = XMFLOAT4(2.0f, 1.0f, 1.0f, 1.0f); m_pContext->UpdateSubresource(pixelShader.GetConstantBuffer()->m_pBuffer, 0, nullptr, &sCbLight, 0, 0); your help is much appreciated.
  8. Hi, this question is more of a structural and code based. Lets say you have some sort of engine in lack of a better name, has as a rendersystem.  The Rendersystem could be OpenGL or DX based. The Engine have knowleage of some of the common render objects, like Shader, ShaderView, meshes and so on. It does not now what a OpenGL and DX Shader is. How does the more professional systems handle this?   I can think of two solutions, one is that the rendersystem has a list of all DX Shader and the Shaders in the engine has an ID to that list (alt 1), Or that OpenGL Shader would be a subclass of the Engine Shader and cast to a OpenGL Shader every time its beeing used in the Render system (alt 2).   (alt 1) void OpenGL::ShaderInput(EngineShader* pShader) { OpenGL* pOpenGLShader = GetOpenGLShader(pShader->ID); }   (alt2) void OpenGL::ShaderInput(EngineShader* pShader) { OpenGL* pOpenGLShader = static_cast<OpenGLShader>(pShader); DoSomething(pOpenGLShader) }   Just think both solutions seems a slow and maybe a bit wasteful.  any thoughts pls?
  9. Thank you Hodgman, excellent answer!
  10. Defered Render structure.

    Hi,   I render each object to  G-Buffer  after that I should render each light in the scene to the lightbuffer, and the result should be blended so the lightbuffer is a blended combination of all lights in the scene correct.   So how do I blend the lightbuffers renderpass? Is this something that i should program in the shaderworld or in the cpu world? And how would a typical very simple lightbuffer look like should that just be a pixel shader?   Also currently my setup works fine for 1 light (:-P) and it goes something like this.   1) Render all object to the G-Buffer one by one, each object has the G-Buffer as there primary shader 2) Render a Quad with the light buffer as a shader and a light as input. If i would start render 1000 lights I cant render the Quad 1000 times, so I should just run the "light shader" a 10000s without any geometry somehow and then as a third final step render the  a Quad and then apply the blend result from the light shader to that quad, how would I do that? // Lightbuffer //-------------------------------------------------------------------------------- Texture2D txDiffuse : register(t0); Texture2D txNormal : register(t1); Texture2D txDepth : register(t2); Texture2D txSpecular : register(t3); //-------------------------------------------------------------------------------- SamplerState samLinear0 : register( s0 ); SamplerState samLinear1 : register( s1 ); SamplerState samLinear2 : register( s2 ); SamplerState samLinear3 : register( s3 ); //-------------------------------------------------------------------------------- cbuffer cbLight : register(b0) { float4 lightDirection; float4 lightColor; float4 lightRange; } //-------------------------------------------------------------------------------- struct PS_INPUT { float4 position : SV_POSITION; float2 texcoord : TEXCOORD0; }; //-------------------------------------------------------------------------------- float4 main(PS_INPUT input) : SV_Target { float4 diffuse = txDiffuse.Sample(samLinear0, input.texcoord); float4 normal = txNormal.Sample(samLinear1, input.texcoord); float4 depth = txDepth.Sample(samLinear2, input.texcoord); float4 specular = txDepth.Sample(samLinear3, input.texcoord); float irradiance = saturate(dot(normal, -lightDirection)); return lightColor*irradiance*diffuse; } // G-BUFFER Texture2D txDiffuse : register( t0 ); SamplerState samLinear : register( s0 ); cbuffer cbPerObject : register(b0) { float4 diffuse; float4 specular; bool isTextured; } //-------------------------------------------------------------------------------------- struct PS_INPUT { float4 position : SV_POSITION; float2 texcoord : TEXCOORD0; float4 normal : NORMAL; }; //-------------------------------------------------------------------------------------- struct PSOutput { float4 Color : SV_Target0; float4 Normal : SV_Target1; float4 Depth : SV_Target2; float4 Specular : SV_Target3; }; //-------------------------------------------------------------------------------------- // Pixel Shader //-------------------------------------------------------------------------------------- PSOutput main(PS_INPUT input) { PSOutput output; output.Color = diffuse*txDiffuse.Sample(samLinear, input.texcoord); output.Normal = normalize(input.normal); output.Specular = specular; output.Depth = input.position.z / input.position.w; return output; } any help is much appreciated.
  11. Defered Render structure.

    Excellent, thx so much for you help guys!
  12. Defered Render structure.

    ou render the geometry first with out the lights. Only with color and texture. You THEN take that information you just gathered in the three buffers Color,Depth,and Normals and pass it to your differed lighting shader. Im already doing that.. My problem is:   Lets say i have 100 lights, as i has understood it i need to excute the lightshader 100 times for each light correct and how do I do this in practice, how do I execute a pixelshader (which the lightshader is) without having any geometry bound to it? I'm a bit lost here.  I understand how it should work in principle but not in practice.. 
  13. Defered Render structure.

    Hi, But how do I render x-number of light with a pixelshader without without any geomery and then merge the result from the lightshaders and then display it on a quad. I found al ot of tutorials and articles that discause the gbuffer but I havent found much on the final merging part?   Will have a look at your link btw thx.
  14. Hi, I have the basic for a simple defered render up and running. I render the end result on a "full screen quad" with the output as a ShaderView not sure if this is the best way to do it, if not what is? My problem thought as you can see in the picture, that the result it repeated and not "stretched out". I think this has to do something with the texture or ShaderView.. but I cant find the problem.   This is my Quad. UINT uFullWidth = 1024; UINT uFullHeight = 768; Vertex vertices_fullquad[] = { { XMFLOAT3(-1.0f*uFullWidth, 1.0f*uFullHeight, 0.0f), XMFLOAT2(0.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f) }, { XMFLOAT3(1.0f*uFullWidth, 1.0f*uFullHeight, 0.0f), XMFLOAT2(1.0f*uFullWidth, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f) }, { XMFLOAT3(-1.0f*uFullWidth, -1.0f*uFullHeight, 0.0f), XMFLOAT2(0.0f, 1.0f*uFullHeight), XMFLOAT3(0.0f, 1.0f, 0.0f) }, { XMFLOAT3(1.0f*uFullWidth, -1.0f*uFullHeight, 0.0f), XMFLOAT2(1.0f*uFullWidth, 1.0f*uFullHeight), XMFLOAT3(0.0f, 1.0f, 0.0f) }, }; TArray<WORD> waLightIndices; quad_indices.add(0); quad_indices.add(1); quad_indices.add(3); quad_indices.add(0); quad_indices.add(2); quad_indices.add(3); RenderTarget Texture ID3D11Texture2D* pTexture = nullptr; // Create a render target view D3D11_TEXTURE2D_DESC descTarget; ZeroMemory(&descTarget, sizeof(descTarget)); descTarget.Width = uWidth; descTarget.Height = uHeight; descTarget.MipLevels = 1; descTarget.ArraySize = 1; descTarget.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; descTarget.SampleDesc.Count = 1; descTarget.Usage = D3D11_USAGE_DEFAULT; descTarget.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; descTarget.CPUAccessFlags = 0; descTarget.MiscFlags = 0; HRESULT hr = m_pDevice->CreateTexture2D(&descTarget, nullptr, &pTexture); ShaderView pTexture->GetDesc(&descTarget); D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; srvDesc.Format = descTarget.Format; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MostDetailedMip = 0; srvDesc.Texture2D.MipLevels = 1; Light-Shader //---------------------------------------------------------------------------------------------- Texture2D txDiffuse : register(t0); Texture2D txNormal : register(t1); Texture2D txDepth : register(t2); Texture2D txSpecular : register(t3); //---------------------------------------------------------------------------------------------- SamplerState samLinear0 : register( s0 ); SamplerState samLinear1 : register( s1 ); SamplerState samLinear2 : register( s2 ); SamplerState samLinear3 : register( s3 ); //---------------------------------------------------------------------------------------------- cbuffer cbLight : register(b0) { float4 lightDirection; float4 lightColor; float4 lightRange; } //--------------------------------------------------------------------------------------------------------------------- struct PS_INPUT { float4 position : SV_POSITION; float2 texcoord : TEXCOORD0; }; //--------------------------------------------------------------------------------------------------------------------- float4 main(PS_INPUT input) : SV_Target { float4 diffuse = txDiffuse.Sample(samLinear0, input.texcoord); float4 normal = txNormal.Sample(samLinear1, input.texcoord); float4 depth = txDepth.Sample(samLinear2, input.texcoord); //Not used currently float4 specular = txDepth.Sample(samLinear3, input.texcoord); //Not used currently float irradiance = saturate(dot(normal, -lightDirection)); return lightColor*irradiance*diffuse; } G-Buffer Texture2D txDiffuse : register( t0 ); SamplerState samLinear : register( s0 ); cbuffer cbPerObject : register(b0) { float4 diffuse; float4 specular; bool isTextured; } //-------------------------------------------------------------------------------------- struct PS_INPUT { float4 position : SV_POSITION; float2 texcoord : TEXCOORD0; float4 normal : NORMAL; }; //-------------------------------------------------------------------------------------- struct PSOutput { float4 Color : SV_Target0; float4 Normal : SV_Target1; float4 Depth : SV_Target2; float4 Specular : SV_Target3; }; //-------------------------------------------------------------------------------------- // Pixel Shader //-------------------------------------------------------------------------------------- PSOutput main(PS_INPUT input) { PSOutput output; output.Color = diffuse*txDiffuse.Sample(samLinear, input.texcoord); output.Normal = normalize(input.normal); output.Specular = specular; output.Depth = input.position.z / input.position.w; return output; }
  15. Defered Render output, repeated.

    Hi, yes i think so.. long night yesterday. Thx for you help NMPTH
  16. Hi, this is more a question regarding architecture or how structure I should structure the my system. In a more real production render system, is it common to use multiple samplers states at the same time or is it just the same one thats being used on multiple times.   As its now, I have a Rendeble3D Class that has a Material. Material can have both a Texture Class (ShaderView) and a Shader Class. As of now i have some different shaders (all of this is test and for my learning) and they can have a multi sampler states bound to them. So should I structure it so that each Shader Class has one/multiple sampler state or should sampler state be a more global setting in the system.  With the second global option it will be hard to specify which sampler state the each shader should be using, not sure if thats a common problem?   All thoughts and feedback or real life examples is much appreciated.
  17.   Yes, a shader/effect should logically decide what type of sampler it expects. It might seem to be useful to delegate this to the texture instead, but when you think about it, it doesn't make sense. Aside from program-wise settings like the multisample-level, the effect will always somehow expect a certain sampler setting - for example, one effect might want to sample a texture with wrap mode set to clamp to avoid artifacts at the edges, therefore all textures passed to the effect must be sampled with clamp, and therefore the only logical place to put the sampler state is the effect.    Excellent, now I know how I should implement it.   thx all for you help.
  18. Hi,    So what sampler state that should be used is normally controlled by the shader,  and not the texture except of the what-kind-of-sampling-all-textures-should-have sample state?  
  19. Hi, im using C++, I have no problem implementing the different solutions, but this is more a question of what would be the more logical way doing it. I don't really have any experiencing from render system, so this is me learning :)
  20. Hello i have an array int * i = new int[1000];   but when Im trying to delete it, Im getting an assertion failed and that the heap is corrupted,    delete[] i;     I realize that this is probably not the best way to use an array, but i want to know how to delete this particulate problem, I read some where that you can just sett i=0; and then delete it, but that wouldn't free any memory, just deleting pointer 0.   any one pls`?  
  21. Strange heap problem when deleting an array

    Hi yes i know, I was just something i type to show case my problem.. thx for the feedback though.
  22. Strange heap problem when deleting an array

    Ok, thx all!  So its the i = l; creates the problem then. excellent!
  23. Strange heap problem when deleting an array

    Hi, thx for you help int * i = new int[100]; int l[100]; for(int i = 0; i < 20; i++) l[i] = INT_FUNCTION(); i = l; delete[] i; I have a feeling that the problem might be i is a pointer and l is not, but shouldn't the compiler complain?
  24. Hi. Question about pointer clean up. void funcA(int id) { Object* pkTemp = NULL; for (Object* pkObj : forwardList) { if (pkObj ->id = id) { pkTemp = pkObj; } } DoSomething(pkTemp); return; } So my question is, will it cause an memoryleak not deleting pkTemp? I see it is that pkTemp referees so pkObj so I cant delete it? Or should pkTemp be a copy of pkObj so i can delete it?   Is there any way I can track memory leaks in visual studio?
  25. C++ Pointer clean up question

    Sorry this is a bit puzzling for me :)   Pointer* matched = new Pointer[100];  is it the same or will it (the array above) create 100 *pointer, and the other one above will create 100 **pointer?   all help much appreciated.
  • Advertisement