Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actuala2ps

Posted 06 September 2012 - 12:20 PM

hello,

so in the process of learning direct3d11 i decided to implement a small framework where is easy to change render type (ppl, deferred, forward, etc) and now im using that framework to implement a a pre-pass lightning.

im still in the process of rendering normals and depths into the gbuffer. problem is gbuffer clears fine (grey on the normal buffer, white on the depth buffer) BUT when i try to render 4 spheres (just for testing) onto the gbuffer, nothing changes it just keeps grey for normals and white on the depth. at least thats what it shows on PIX (im not rendering to the window just yet, just using PIX to see if it works).

problem is it could be wrong transformations, wrong shaders, wrong constant buffers.. i've already lost 3 days with this and its becoming really frustrating especially because i thought that i was starting to understand how direct3d works..

ill try to post every piece of relevant code

my clear gbuffer shader:

[source lang="cpp"]// simple vertex and pixel shader that clears the NormalDepth buffer to default values////////// Vertex Shader //////////struct VSInput{ float4 Position : SV_POSITION0;};struct VSOutput{ float4 Position : SV_POSITION0;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; output.Position = input.Position; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // set normals (for the normals buffer) to 0.5f (transforming 0.5f into [-1, 1] is 0.0f, a good default value output.Normal = float4(0.5f, 0.5f, 0.5f, 1.0f); // set depth (for the depth buffer) to white (maximum depth) output.Depth = 1.0f; return output;}[/source]

my render objects normals and depths into gbuffer shader:
[source lang="cpp"]// simple vertex and pixel shader that fills the NormalDepth buffer with the transformed geometry////////// Vertex Shader //////////cbuffer Parameters : register(b0){ matrix WorldMatrix; matrix ViewMatrix; matrix ProjectionMatrix;};struct VSInput{ float4 Position : SV_POSITION0; float4 Normal : NORMAL0;};struct VSOutput{ float4 Position : SV_POSITION0; float3 Normal : TEXCOORD0; float2 Depth : TEXCOORD1;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; // apply position transformations (into screen space) output.Position = mul(input.Position, WorldMatrix); output.Position = mul(output.Position, ViewMatrix); output.Position = mul(output.Position, ProjectionMatrix); // apply normal transformations (into world space) output.Normal = mul(WorldMatrix, input.Normal); // pass on the depth position output.Depth.x = input.Position.z; output.Depth.y = input.Position.w; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // remap the normal from [-1, 1] to [0, -1] output.Normal.rgb = 0.5f * (normalize(input.Normal) + 1.0f); // output.Normal = ((input.Normal + 1.0f) / 2.0f); // just pass the depth values output.Depth = input.Depth.x / input.Depth.y; return output;}[/source]

calculation of the view matrix:
[source lang="cpp"]DirectX::XMMATRIX D3DCamera::GetViewMatrix() { // create position vector auto positionVector = DirectX::XMVectorSet(positionX, positionY, positionZ, 0.0f); // default lookAt is through the Z coord auto lookAtVector = DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); // default up is through the Y coord auto upVector = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // create the rotation matrix, roll = Z, pitch = X, yaw = Y auto rotationMatrix = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); // transform the lookAt and up vector using the rotation matrix lookAtVector = DirectX::XMVector3TransformCoord(lookAtVector, rotationMatrix); upVector = DirectX::XMVector3TransformCoord(upVector, rotationMatrix); // update lookAt vector from the new position lookAtVector = DirectX::XMVectorAdd(positionVector, lookAtVector); // finally create the view matrix using the updated vectors return DirectX::XMMatrixLookAtLH(positionVector, lookAtVector, upVector); }[/source]

calculation of the world matrix for each object:
[source lang="cpp"]virtual DirectX::XMMATRIX GetWorldMatrix() { // TODO: rotate around a point, now it only rotates around itself auto scaling = DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ); auto rotation = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); auto translation = DirectX::XMMatrixTranslation(positionX, positionY, positionZ); auto scale_rotation = DirectX::XMMatrixMultiply(DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ), DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ)); return DirectX::XMMatrixMultiply(scale_rotation, translation); }[/source]

now setting the shader parameters and calling draws:
[source lang="cpp"]void D3DMaterialFillNormalDepth::SetParameters(const DirectX::XMMATRIX& worldMatrix, const DirectX::XMMATRIX& viewMatrix, const DirectX::XMMATRIX& projectionMatrix) { cbParameters.WorldMatrix = worldMatrix; cbParameters.ViewMatrix = viewMatrix; cbParameters.ProjectionMatrix = projectionMatrix; d3d->DeviceContext()->UpdateSubresource(cbBuffer, 0, nullptr, &cbParameters, 0, 0); }// then for each object:d3d->DeviceContext()->OMSetRenderTargets(renderTargetViews.size(), &renderTargetViews[0], d3d->DepthStencilView());d3d->DeviceContext()->IASetInputLayout(pInputLayout);d3d->DeviceContext()->VSSetShader(pVertexShader, nullptr, 0);d3d->DeviceContext()->PSSetShader(pPixelShader, nullptr, 0);d3d->DeviceContext()->VSSetConstantBuffers(0, 1, &cbBuffer);d3d->DeviceContext()->PSSetConstantBuffers(0, 1, &cbBuffer);// activate this geometry on the device contextunsigned int stride = sizeof(VertexType);unsigned int offset = 0;d3d->DeviceContext()->IASetVertexBuffers(0, 1, &pVertexBuffer, &stride, &offset);d3d->DeviceContext()->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);d3d->DeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);d3d->DeviceContext()->DrawIndexed(indices.size(), 0, 0);[/source]
PIX does say it draws around 1200 indices for each sphere, so i assume things are kind of working, but the result buffer is just grey for normals and white for depth like it is after clearing it.

sorry the wall of code, trying to post every piece that i think is relevant, feel free to ask for more if needed.
and thanks in advance.

#5a2ps

Posted 06 September 2012 - 12:11 PM

hello,

so in the process of learning direct3d11 i decided to implement a small framework where is easy to change render type (ppl, deferred, forward, etc) and now im using that framework to implement a a pre-pass lightning.

im still in the process of rendering normals and depths into the gbuffer. problem is gbuffer clears fine (grey on the normal buffer, white on the depth buffer) BUT when i try to render 4 spheres (just for testing) onto the gbuffer, nothing changes it just keeps grey for normals and white on the depth. at least thats what it shows on PIX (im not rendering to the window just yet, just using PIX to see if it works).

problem is it could be wrong transformations, wrong shaders, wrong constant buffers.. i've already lost 3 days with this and its becoming really frustrating especially because i thought that i was starting to understand how direct3d works..

ill try to post every piece of relevant code

my clear gbuffer shader:

[source lang="cpp"]// simple vertex and pixel shader that clears the NormalDepth buffer to default values////////// Vertex Shader //////////struct VSInput{ float4 Position : SV_POSITION0;};struct VSOutput{ float4 Position : SV_POSITION0;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; output.Position = input.Position; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // set normals (for the normals buffer) to 0.5f (transforming 0.5f into [-1, 1] is 0.0f, a good default value output.Normal = float4(0.5f, 0.5f, 0.5f, 1.0f); // set depth (for the depth buffer) to white (maximum depth) output.Depth = 1.0f; return output;}[/source]

my render objects normals and depths into gbuffer shader:
[source lang="cpp"]// simple vertex and pixel shader that fills the NormalDepth buffer with the transformed geometry////////// Vertex Shader //////////cbuffer Parameters : register(b0){ matrix WorldMatrix; matrix ViewMatrix; matrix ProjectionMatrix;};struct VSInput{ float4 Position : SV_POSITION0; float4 Normal : NORMAL0;};struct VSOutput{ float4 Position : SV_POSITION0; float3 Normal : TEXCOORD0; float2 Depth : TEXCOORD1;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; // apply position transformations (into screen space) output.Position = mul(input.Position, WorldMatrix); output.Position = mul(output.Position, ViewMatrix); output.Position = mul(output.Position, ProjectionMatrix); // apply normal transformations (into world space) output.Normal = mul(WorldMatrix, input.Normal); // pass on the depth position output.Depth.x = input.Position.z; output.Depth.y = input.Position.w; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // remap the normal from [-1, 1] to [0, -1] output.Normal.rgb = 0.5f * (normalize(input.Normal) + 1.0f); // output.Normal = ((input.Normal + 1.0f) / 2.0f); // just pass the depth values output.Depth = input.Depth.x / input.Depth.y; return output;}[/source]

calculation of the view matrix:
[source lang="cpp"]DirectX::XMMATRIX D3DCamera::GetViewMatrix() { // create position vector auto positionVector = DirectX::XMVectorSet(positionX, positionY, positionZ, 0.0f); // default lookAt is through the Z coord auto lookAtVector = DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); // default up is through the Y coord auto upVector = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // create the rotation matrix, roll = Z, pitch = X, yaw = Y auto rotationMatrix = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); // transform the lookAt and up vector using the rotation matrix lookAtVector = DirectX::XMVector3TransformCoord(lookAtVector, rotationMatrix); upVector = DirectX::XMVector3TransformCoord(upVector, rotationMatrix); // update lookAt vector from the new position lookAtVector = DirectX::XMVectorAdd(positionVector, lookAtVector); // finally create the view matrix using the updated vectors return DirectX::XMMatrixLookAtLH(positionVector, lookAtVector, upVector); }[/source]

calculation of the world matrix for each object:
[source lang="cpp"]virtual DirectX::XMMATRIX GetWorldMatrix() { // TODO: rotate around a point, now it only rotates around itself auto scaling = DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ); auto rotation = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); auto translation = DirectX::XMMatrixTranslation(positionX, positionY, positionZ); auto scale_rotation = DirectX::XMMatrixMultiply(DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ), DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ)); return DirectX::XMMatrixMultiply(scale_rotation, translation); }[/source]

now setting the shader parameters and calling draws:
[source lang="cpp"]void D3DMaterialFillNormalDepth::SetParameters(const DirectX::XMMATRIX& worldMatrix, const DirectX::XMMATRIX& viewMatrix, const DirectX::XMMATRIX& projectionMatrix) { cbParameters.WorldMatrix = worldMatrix; cbParameters.ViewMatrix = viewMatrix; cbParameters.ProjectionMatrix = projectionMatrix; d3d->DeviceContext()->UpdateSubresource(cbBuffer, 0, nullptr, &cbParameters, 0, 0); }// then for each object:d3d->DeviceContext()->OMSetRenderTargets(renderTargetViews.size(), &renderTargetViews[0], d3d->DepthStencilView());d3d->DeviceContext()->IASetInputLayout(pInputLayout);d3d->DeviceContext()->VSSetShader(pVertexShader, nullptr, 0);d3d->DeviceContext()->PSSetShader(pPixelShader, nullptr, 0);d3d->DeviceContext()->VSSetConstantBuffers(0, 1, &cbBuffer);// activate this geometry on the device contextunsigned int stride = sizeof(VertexType);unsigned int offset = 0;d3d->DeviceContext()->IASetVertexBuffers(0, 1, &pVertexBuffer, &stride, &offset);d3d->DeviceContext()->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);d3d->DeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);d3d->DeviceContext()->DrawIndexed(indices.size(), 0, 0);[/source]
PIX does say it draws around 1200 indices for each sphere, so i assume things are kind of working, but the result buffer is just grey for normals and white for depth like it is after clearing it.

sorry the wall of code, trying to post every piece that i think is relevant, feel free to ask for more if needed.
and thanks in advance.

#4a2ps

Posted 06 September 2012 - 12:07 PM

hello,

so in the process of learning direct3d11 i decided to implement a small framework where is easy to change render type (ppl, deferred, forward, etc) and now im using that framework to implement a a pre-pass lightning.

im still in the process of rendering normals and depths into the gbuffer. problem is gbuffer clears fine (grey on the normal buffer, white on the depth buffer) BUT when i try to render 4 spheres (just for testing) onto the gbuffer, nothing changes it just keeps grey for normals and white on the depth. at least thats what it shows on PIX (im not rendering to the window just yet, just using PIX to see if it works).

problem is it could be wrong transformations, wrong shaders, wrong constant buffers.. i've already lost 3 days with this and its becoming really frustrating especially because i thought that i was starting to understand how direct3d works..

ill try to post every piece of relevant code

my clear gbuffer shader:

[source lang="cpp"]// simple vertex and pixel shader that clears the NormalDepth buffer to default values////////// Vertex Shader //////////struct VSInput{ float4 Position : SV_POSITION0;};struct VSOutput{ float4 Position : SV_POSITION0;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; output.Position = input.Position; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // set normals (for the normals buffer) to 0.5f (transforming 0.5f into [-1, 1] is 0.0f, a good default value output.Normal = float4(0.5f, 0.5f, 0.5f, 1.0f); // set depth (for the depth buffer) to white (maximum depth) output.Depth = 1.0f; return output;}[/source]

my render objects normals and depths into gbuffer shader:
[source lang="cpp"]// simple vertex and pixel shader that fills the NormalDepth buffer with the transformed geometry////////// Vertex Shader //////////cbuffer Parameters : register(b0){ matrix WorldMatrix; matrix ViewMatrix; matrix ProjectionMatrix;};struct VSInput{ float4 Position : SV_POSITION0; float4 Normal : NORMAL0;};struct VSOutput{ float4 Position : SV_POSITION0; float3 Normal : TEXCOORD0; float2 Depth : TEXCOORD1;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; // apply position transformations (into screen space) output.Position = mul(input.Position, WorldMatrix); output.Position = mul(output.Position, ViewMatrix); output.Position = mul(output.Position, ProjectionMatrix); // apply normal transformations (into world space) output.Normal = mul(WorldMatrix, input.Normal); // pass on the depth position output.Depth.x = input.Position.z; output.Depth.y = input.Position.w; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // remap the normal from [-1, 1] to [0, -1] output.Normal.rgb = 0.5f * (normalize(input.Normal) + 1.0f); // output.Normal = ((input.Normal + 1.0f) / 2.0f); // just pass the depth values output.Depth = input.Depth.x / input.Depth.y; return output;}[/source]

calculation of the view matrix:
[source lang="cpp"]DirectX::XMMATRIX D3DCamera::GetViewMatrix() { // create position vector auto positionVector = DirectX::XMVectorSet(positionX, positionY, positionZ, 0.0f); // default lookAt is through the Z coord auto lookAtVector = DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); // default up is through the Y coord auto upVector = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // create the rotation matrix, roll = Z, pitch = X, yaw = Y auto rotationMatrix = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); // transform the lookAt and up vector using the rotation matrix lookAtVector = DirectX::XMVector3TransformCoord(lookAtVector, rotationMatrix); upVector = DirectX::XMVector3TransformCoord(upVector, rotationMatrix); // update lookAt vector from the new position lookAtVector = DirectX::XMVectorAdd(positionVector, lookAtVector); // finally create the view matrix using the updated vectors return DirectX::XMMatrixLookAtLH(positionVector, lookAtVector, upVector); }[/source]

calculation of the world matrix for each object:
[source lang="cpp"]virtual DirectX::XMMATRIX GetWorldMatrix() { // TODO: rotate around a point, now it only rotates around itself auto scaling = DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ); auto rotation = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); auto translation = DirectX::XMMatrixTranslation(positionX, positionY, positionZ); auto scale_rotation = DirectX::XMMatrixMultiply(DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ), DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ)); return DirectX::XMMatrixMultiply(scale_rotation, translation); }[/source]

now setting the shader parameters and calling draws:
[source lang="cpp"]void D3DMaterialFillNormalDepth::SetParameters(const DirectX::XMMATRIX& worldMatrix, const DirectX::XMMATRIX& viewMatrix, const DirectX::XMMATRIX& projectionMatrix) { cbParameters.WorldMatrix = worldMatrix; cbParameters.ViewMatrix = viewMatrix; cbParameters.ProjectionMatrix = projectionMatrix; d3d->DeviceContext()->UpdateSubresource(cbBuffer, 0, nullptr, &cbParameters, 0, 0); }// then for each object:d3d->DeviceContext()->OMSetRenderTargets(renderTargetViews.size(), &renderTargetViews[0], d3d->DepthStencilView());d3d->DeviceContext()->IASetInputLayout(pInputLayout);d3d->DeviceContext()->VSSetShader(pVertexShader, nullptr, 0);d3d->DeviceContext()->PSSetShader(pPixelShader, nullptr, 0);d3d->DeviceContext()->VSSetConstantBuffers(0, 1, &cbBuffer);// activate this geometry on the device contextunsigned int stride = sizeof(VertexType);unsigned int offset = 0;d3d->DeviceContext()->IASetVertexBuffers(0, 1, &pVertexBuffer, &stride, &offset);d3d->DeviceContext()->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);d3d->DeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);[/source]

sorry the wall of code, trying to post every piece that i think is relevant, feel free to ask for more if needed.
and thanks in advance.

#3a2ps

Posted 06 September 2012 - 12:06 PM

hello,

so in the process of learning direct3d11 i decided to implement a small framework where is easy to change render type (ppl, deferred, forward, etc) and now im using that framework to implement a a pre-pass lightning.

im still in the process of rendering normals and depths into the gbuffer. problem is gbuffer clears fine (grey on the normal buffer, white on the depth buffer) BUT when i try to render 4 spheres (just for testing) onto the gbuffer, nothing changes it just keeps grey for normals and white on the depth. at least thats what it shows on PIX (im not rendering to the window just yet, just using PIX to see if it works).

problem is it could be wrong transformations, wrong shaders, wrong constant buffers.. i've already lost 3 days with this and its becoming really frustrating especially because i thought that i was starting to understand how direct3d works..

ill try to post every piece of relevant code

my clear gbuffer shader:

[source lang="cpp"]// simple vertex and pixel shader that clears the NormalDepth buffer to default values////////// Vertex Shader //////////struct VSInput{ float4 Position : SV_POSITION0;};struct VSOutput{ float4 Position : SV_POSITION0;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; output.Position = input.Position; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // set normals (for the normals buffer) to 0.5f (transforming 0.5f into [-1, 1] is 0.0f, a good default value output.Normal = float4(0.5f, 0.5f, 0.5f, 1.0f); // set depth (for the depth buffer) to white (maximum depth) output.Depth = 1.0f; return output;}[/source]

my render objects normals and depths into gbuffer shader:
[source lang="cpp"]// simple vertex and pixel shader that fills the NormalDepth buffer with the transformed geometry////////// Vertex Shader //////////cbuffer Parameters : register(b0){ matrix WorldMatrix; matrix ViewMatrix; matrix ProjectionMatrix;};struct VSInput{ float4 Position : SV_POSITION0; float4 Normal : NORMAL0;};struct VSOutput{ float4 Position : SV_POSITION0; float3 Normal : TEXCOORD0; float2 Depth : TEXCOORD1;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; // apply position transformations (into screen space) output.Position = mul(input.Position, WorldMatrix); output.Position = mul(output.Position, ViewMatrix); output.Position = mul(output.Position, ProjectionMatrix); // apply normal transformations (into world space) output.Normal = mul(WorldMatrix, input.Normal); // pass on the depth position output.Depth.x = input.Position.z; output.Depth.y = input.Position.w; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // remap the normal from [-1, 1] to [0, -1] output.Normal.rgb = 0.5f * (normalize(input.Normal) + 1.0f); // output.Normal = ((input.Normal + 1.0f) / 2.0f); // just pass the depth values output.Depth = input.Depth.x / input.Depth.y; return output;}[/source]

calculation of the view matrix:
[source lang="cpp"]DirectX::XMMATRIX D3DCamera::GetViewMatrix() { // create position vector auto positionVector = DirectX::XMVectorSet(positionX, positionY, positionZ, 0.0f); // default lookAt is through the Z coord auto lookAtVector = DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); // default up is through the Y coord auto upVector = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // create the rotation matrix, roll = Z, pitch = X, yaw = Y auto rotationMatrix = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); // transform the lookAt and up vector using the rotation matrix lookAtVector = DirectX::XMVector3TransformCoord(lookAtVector, rotationMatrix); upVector = DirectX::XMVector3TransformCoord(upVector, rotationMatrix); // update lookAt vector from the new position lookAtVector = DirectX::XMVectorAdd(positionVector, lookAtVector); // finally create the view matrix using the updated vectors return DirectX::XMMatrixLookAtLH(positionVector, lookAtVector, upVector); }[/source]

calculation of the world matrix for each object:
[source lang="cpp"]virtual DirectX::XMMATRIX GetWorldMatrix() { // TODO: rotate around a point, now it only rotates around itself auto scaling = DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ); auto rotation = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); auto translation = DirectX::XMMatrixTranslation(positionX, positionY, positionZ); auto scale_rotation = DirectX::XMMatrixMultiply(DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ), DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ)); return DirectX::XMMatrixMultiply(scale_rotation, translation); }[/source]

now setting the shader parameters and calling draws:
[source lang="cpp"]void D3DMaterialFillNormalDepth::SetParameters(const DirectX::XMMATRIX& worldMatrix, const DirectX::XMMATRIX& viewMatrix, const DirectX::XMMATRIX& projectionMatrix) { cbParameters.WorldMatrix = worldMatrix; cbParameters.ViewMatrix = viewMatrix; cbParameters.ProjectionMatrix = projectionMatrix; d3d->DeviceContext()->UpdateSubresource(cbBuffer, 0, nullptr, &cbParameters, 0, 0); }// then for each object:d3d->DeviceContext()->OMSetRenderTargets(renderTargetViews.size(), &renderTargetViews[0], d3d->DepthStencilView());d3d->DeviceContext()->IASetInputLayout(pInputLayout);d3d->DeviceContext()->VSSetShader(pVertexShader, nullptr, 0);d3d->DeviceContext()->PSSetShader(pPixelShader, nullptr, 0);d3d->DeviceContext()->VSSetConstantBuffers(0, 1, &cbBuffer);// activate this geometry on the device contextunsigned int stride = sizeof(VertexType);unsigned int offset = 0;d3d->DeviceContext()->IASetVertexBuffers(0, 1, &pVertexBuffer, &stride, &offset);d3d->DeviceContext()->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);d3d->DeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);[/source]

sorry the wall of code, trying to post every piece that i think is relevant, feel free to ask for more if needed.
and thanks in advance.

#2a2ps

Posted 06 September 2012 - 12:05 PM

hello,

so in the process of learning direct3d11 i decided to implement a small framework where is easy to change render type (ppl, deferred, forward, etc) and now im using that framework to implement a a pre-pass lightning.

im still in the process of rendering normals and depths into the gbuffer. problem is gbuffer clears fine (grey on the normal buffer, white on the depth buffer) BUT when i try to render 4 spheres (just for testing) onto the gbuffer, nothing changes it just keeps grey for normals and white on the depth. at least thats what it shows on PIX (im not rendering to the window just yet, just using PIX to see if it works).

problem is it could be wrong transformations, wrong shaders, wrong constant buffers.. i've already lost 3 days with this and its becoming really frustrating especially because i thought that i was starting to understand how direct3d works..

ill try to post every piece of relevant code

my clear gbuffer shader:

[source lang="cpp"]// simple vertex and pixel shader that clears the NormalDepth buffer to default values////////// Vertex Shader //////////struct VSInput{ float4 Position : SV_POSITION0;};struct VSOutput{ float4 Position : SV_POSITION0;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; output.Position = input.Position; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // set normals (for the normals buffer) to 0.5f (transforming 0.5f into [-1, 1] is 0.0f, a good default value output.Normal = float4(0.5f, 0.5f, 0.5f, 1.0f); // set depth (for the depth buffer) to white (maximum depth) output.Depth = 1.0f; return output;}[/source]

my render objects normals and depths into gbuffer shader:
[source lang="cpp"]// simple vertex and pixel shader that fills the NormalDepth buffer with the transformed geometry////////// Vertex Shader //////////cbuffer Parameters : register(b0){ matrix WorldMatrix; matrix ViewMatrix; matrix ProjectionMatrix;};struct VSInput{ float4 Position : SV_POSITION0; float4 Normal : NORMAL0;};struct VSOutput{ float4 Position : SV_POSITION0; float3 Normal : TEXCOORD0; float2 Depth : TEXCOORD1;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; // apply position transformations (into screen space) output.Position = mul(input.Position, WorldMatrix); output.Position = mul(output.Position, ViewMatrix); output.Position = mul(output.Position, ProjectionMatrix); // apply normal transformations (into world space) output.Normal = mul(WorldMatrix, input.Normal); // pass on the depth position output.Depth.x = input.Position.z; output.Depth.y = input.Position.w; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // remap the normal from [-1, 1] to [0, -1] output.Normal.rgb = 0.5f * (normalize(input.Normal) + 1.0f); // output.Normal = ((input.Normal + 1.0f) / 2.0f); // just pass the depth values output.Depth = input.Depth.x / input.Depth.y; return output;}[/source]

calculation of the view matrix:
[source lang="cpp"]DirectX::XMMATRIX D3DCamera::GetViewMatrix() { // create position vector auto positionVector = DirectX::XMVectorSet(positionX, positionY, positionZ, 0.0f); // default lookAt is through the Z coord auto lookAtVector = DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); // default up is through the Y coord auto upVector = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // create the rotation matrix, roll = Z, pitch = X, yaw = Y auto rotationMatrix = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); // transform the lookAt and up vector using the rotation matrix lookAtVector = DirectX::XMVector3TransformCoord(lookAtVector, rotationMatrix); upVector = DirectX::XMVector3TransformCoord(upVector, rotationMatrix); // update lookAt vector from the new position lookAtVector = DirectX::XMVectorAdd(positionVector, lookAtVector); // finally create the view matrix using the updated vectors return DirectX::XMMatrixLookAtLH(positionVector, lookAtVector, upVector); }[/source]

calculation of the world matrix for each object:
[source lang="cpp"]virtual DirectX::XMMATRIX GetWorldMatrix() { // TODO: rotate around a point, now it only rotates around itself auto scaling = DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ); auto rotation = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); auto translation = DirectX::XMMatrixTranslation(positionX, positionY, positionZ); auto scale_rotation = DirectX::XMMatrixMultiply(DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ), DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ)); return DirectX::XMMatrixMultiply(scale_rotation, translation); }[/source]

now setting the shader parameters and calling draws:
[source lang="cpp"]void D3DMaterialFillNormalDepth::SetParameters(const DirectX::XMMATRIX& worldMatrix, const DirectX::XMMATRIX& viewMatrix, const DirectX::XMMATRIX& projectionMatrix) { cbParameters.WorldMatrix = worldMatrix; cbParameters.ViewMatrix = viewMatrix; cbParameters.ProjectionMatrix = projectionMatrix; d3d->DeviceContext()->UpdateSubresource(cbBuffer, 0, nullptr, &cbParameters, 0, 0); }// then for each object:d3d->DeviceContext()->OMSetRenderTargets(renderTargetViews.size(), &renderTargetViews[0], d3d->DepthStencilView());d3d->DeviceContext()->IASetInputLayout(pInputLayout);d3d->DeviceContext()->VSSetShader(pVertexShader, nullptr, 0);d3d->DeviceContext()->PSSetShader(pPixelShader, nullptr, 0);d3d->DeviceContext()->VSSetConstantBuffers(0, 1, &cbBuffer);// activate this geometry on the device contextunsigned int stride = sizeof(VertexType);unsigned int offset = 0;d3d->DeviceContext()->IASetVertexBuffers(0, 1, &pVertexBuffer, &stride, &offset);d3d->DeviceContext()->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0); d3d->DeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);[/source]

sorry the wall of code, trying to post every piece that i think is relevant, feel free to ask for more if needed.
and thanks in advance.

#1a2ps

Posted 06 September 2012 - 12:04 PM

hello,

so in the process of learning direct3d11 i decided to implement a small framework where is easy to change render type (ppl, deferred, forward, etc) and now im using that framework to implement a a pre-pass lightning.

im still in the process of rendering normals and depths into the gbuffer. problem is gbuffer clears fine (grey on the normal buffer, white on the depth buffer) BUT when i try to render 4 spheres (just for testing) onto the gbuffer, nothing changes it just keeps grey for normals and white on the depth. at least thats what it shows on PIX (im not rendering to the window just yet, just using PIX to see if it works).

problem is it could be wrong transformations, wrong shaders, wrong constant buffers.. i've already lost 3 days with this and its becoming really frustrating especially because i thought that i was starting to understand how direct3d works..

ill try to post every piece of relevant code

my clear gbuffer shader:

[source lang="cpp"]// simple vertex and pixel shader that clears the NormalDepth buffer to default values////////// Vertex Shader //////////struct VSInput{ float4 Position : SV_POSITION0;};struct VSOutput{ float4 Position : SV_POSITION0;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; output.Position = input.Position; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // set normals (for the normals buffer) to 0.5f (transforming 0.5f into [-1, 1] is 0.0f, a good default value output.Normal = float4(0.5f, 0.5f, 0.5f, 1.0f); // set depth (for the depth buffer) to white (maximum depth) output.Depth = 1.0f; return output;}[/source]

my render objects normals and depths into gbuffer shader:
[source lang="cpp"]// simple vertex and pixel shader that fills the NormalDepth buffer with the transformed geometry////////// Vertex Shader //////////cbuffer Parameters : register(b0){ matrix WorldMatrix; matrix ViewMatrix; matrix ProjectionMatrix;};struct VSInput{ float4 Position : SV_POSITION0; float4 Normal : NORMAL0;};struct VSOutput{ float4 Position : SV_POSITION0; float3 Normal : TEXCOORD0; float2 Depth : TEXCOORD1;};VSOutput VS(VSInput input){ VSOutput output = (VSOutput)0; // apply position transformations (into screen space) output.Position = mul(input.Position, WorldMatrix); output.Position = mul(output.Position, ViewMatrix); output.Position = mul(output.Position, ProjectionMatrix); // apply normal transformations (into world space) output.Normal = mul(WorldMatrix, input.Normal); // pass on the depth position output.Depth.x = input.Position.z; output.Depth.y = input.Position.w; return output;}////////// Pixel Shader //////////struct PSOutput{ float4 Normal : SV_TARGET0; float4 Depth : SV_TARGET1;};PSOutput PS(VSOutput input){ PSOutput output = (PSOutput)0; // remap the normal from [-1, 1] to [0, -1] output.Normal.rgb = 0.5f * (normalize(input.Normal) + 1.0f); // output.Normal = ((input.Normal + 1.0f) / 2.0f); // just pass the depth values output.Depth = input.Depth.x / input.Depth.y; return output;}[/source]

calculation of the view matrix:
[source lang="cpp"]DirectX::XMMATRIX D3DCamera::GetViewMatrix() { // create position vector auto positionVector = DirectX::XMVectorSet(positionX, positionY, positionZ, 0.0f); // default lookAt is through the Z coord auto lookAtVector = DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); // default up is through the Y coord auto upVector = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // create the rotation matrix, roll = Z, pitch = X, yaw = Y auto rotationMatrix = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); // transform the lookAt and up vector using the rotation matrix lookAtVector = DirectX::XMVector3TransformCoord(lookAtVector, rotationMatrix); upVector = DirectX::XMVector3TransformCoord(upVector, rotationMatrix); // update lookAt vector from the new position lookAtVector = DirectX::XMVectorAdd(positionVector, lookAtVector); // finally create the view matrix using the updated vectors return DirectX::XMMatrixLookAtLH(positionVector, lookAtVector, upVector); }[/source]

calculation of the world matrix for each object:
[source lang="cpp"]virtual DirectX::XMMATRIX GetWorldMatrix() { // TODO: rotate around a point, now it only rotates around itself auto scaling = DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ); auto rotation = DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ); auto translation = DirectX::XMMatrixTranslation(positionX, positionY, positionZ); auto scale_rotation = DirectX::XMMatrixMultiply(DirectX::XMMatrixScaling(scaleX, scaleY, scaleZ), DirectX::XMMatrixRotationRollPitchYaw(rotationX, rotationY, rotationZ)); return DirectX::XMMatrixMultiply(scale_rotation, translation); }[/source]

now setting the shader parameters and calling draws:
[source lang="cpp"]void D3DMaterialFillNormalDepth::SetParameters(const DirectX::XMMATRIX& worldMatrix, const DirectX::XMMATRIX& viewMatrix, const DirectX::XMMATRIX& projectionMatrix) { cbParameters.WorldMatrix = worldMatrix; cbParameters.ViewMatrix = viewMatrix; cbParameters.ProjectionMatrix = projectionMatrix; d3d->DeviceContext()->UpdateSubresource(cbBuffer, 0, nullptr, &cbParameters, 0, 0); }// then for each object:d3d->DeviceContext()->OMSetRenderTargets(renderTargetViews.size(), &renderTargetViews[0], d3d->DepthStencilView());d3d->DeviceContext()->IASetInputLayout(pInputLayout);d3d->DeviceContext()->VSSetShader(pVertexShader, nullptr, 0);d3d->DeviceContext()->PSSetShader(pPixelShader, nullptr, 0);d3d->DeviceContext()->VSSetConstantBuffers(0, 1, &cbBuffer);// activate this geometry on the device contextunsigned int stride = sizeof(VertexType);unsigned int offset = 0;d3d->DeviceContext()->IASetVertexBuffers(0, 1, &pVertexBuffer, &stride, &offset);d3d->DeviceContext()->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0); d3d->DeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);[/source]

sorry the wall of code, trying to post every piece that i think is relevant, feel free to ask for more if needed.
and thanks in advance.

PARTNERS