EXECUTION ERROR #362: DEVICE_DRAW_POSITION_NOT_PRESENT - directx

I am trying to draw a cube in DirectX. However, I am getting the following error message;
D3D11 ERROR: ID3D11DeviceContext::DrawIndexed: Rasterization Unit is enabled (PixelShader is not NULL or Depth/Stencil test is enabled and RasterizedStream is not D3D11_SO_NO_RASTERIZED_STREAM) but position is not provided by the last shader before the Rasterization Unit. [ EXECUTION ERROR #362: DEVICE_DRAW_POSITION_NOT_PRESENT]
Here is the code for my shaders;
cbuffer WorldBuffer : register (b0)
{
matrix World;
matrix View;
matrix Projection;
}
struct VS_OUTPUT
{
float4 pos : SV_POSITION;
float4 color : COLOR0;
};
VS_OUTPUT VS(float4 pos : POSITION, float4 color : COLOR) : VS_OUTPUT_SEMANTICS
{
VS_OUTPUT output = (VS_OUTPUT)0;
output.pos = mul(pos, World);
output.pos = mul(output.pos, View);
output.pos = mul(output.pos, Projection);
output.color = color;
return output;
}
float4 PS(VS_OUTPUT input : VS_OUTPUT_SEMANTICS) : SV_TARGET
{
return input.color;
}
Since I have given SV_POSITION semantic to VS_OUTPUT.pos, I think shader should be passing position, but it doesn't seem to work. How can I fix my shaders?
This is how I compile my shaders:
UINT flags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_SKIP_OPTIMIZATION | D3DCOMPILE_DEBUG;
r = D3DCompileFromFile(L"shader.fx", NULL, NULL, "VS", "vs_5_0", flags, 0, &vertex_blob, &errMsg);

When using :
VS_OUTPUT VS(float4 pos : POSITION, float4 color : COLOR) : VS_OUTPUT_SEMANTICS
VS_OUTPUT_SEMANTICS will actually override the per element semantics, so instead of outputting a struct like :
struct VS_OUTPUT
{
float4 pos : SV_POSITION;
float4 color : COLOR0;
};
you actually output
struct VS_OUTPUT
{
float4 pos : VS_OUTPUT_SEMANTICS;
float4 color : VS_OUTPUT_SEMANTICS;
};
When you call :
UINT flags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_SKIP_OPTIMIZATION | D3DCOMPILE_DEBUG;
r = D3DCompileFromFile(L"shader.fx", NULL, NULL, "VS", "vs_5_0", flags, 0, &vertex_blob, &errMsg);
Even if the shader compiles, you should still look in errMsg, as the compiler might issue some warnings (which in this case will be) :
"semantics in type overriden by variable/function or enclosing type"
If can also be a good idea to call D3DDisassemble on the resulting blob,
this can give you the resulting reflection data in a text form.
In your vertex shader case you will have (only relevant part):
// Input signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION 0 xyzw 0 NONE float xyzw
// COLOR 0 xyzw 1 NONE float xyzw
//
//
// Output signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// VS_OUTPUT_SEMANTICS 0 xyzw 0 NONE float xyzw
// VS_OUTPUT_SEMANTICS 1 xyzw 1 NONE float xyzw
//

Related

How to use DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL correctly?

I am having issue while drawing my cube on the window. I don't see any graphics on the window. I see the following warning messages in visual studio:
D3D11 WARNING: ID3D11DeviceContext::DrawIndexed: The Pixel Shader expects a Render Target View bound to slot 0, but the Render Target View was unbound during a call to Present. A successful Present call for DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL SwapChains unbinds backbuffer 0 from all GPU writeable bind points. [ EXECUTION WARNING #3146082:
D3D11 WARNING: ID3D11DeviceContext::DrawIndexed: The Pixel Shader expects a Render Target View bound to slot 0, but none is bound. This is OK, as writes of an unbound Render Target View are discarded. It is also possible the developer knows the data will not be used anyway. This is only a problem if the developer actually intended to bind a Render Target View here. [ EXECUTION WARNING #3146081: DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET]
I created swapchain using the following API:
virtual IDXGISwapChain* SwapChain(HWND wnd)
{
HRESULT hr = S_OK;
IDXGISwapChain* swapchain = nullptr;
DXGI_SWAP_CHAIN_DESC desc;
ZeroMemory(&desc, sizeof(DXGI_SWAP_CHAIN_DESC));
desc.Windowed = TRUE; // Sets the initial state of full-screen mode.
desc.BufferCount = 2;
desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
desc.SampleDesc.Count = 1; //multisampling setting
desc.SampleDesc.Quality = 0; //vendor-specific flag
desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
desc.OutputWindow = wnd;
// Create the DXGI device object to use in other factories, such as Direct2D.
IDXGIDevice3* dxgiDevice;
hr = device_->QueryInterface(__uuidof(IDXGIDevice3), reinterpret_cast<void**>(&dxgiDevice));
if (FAILED(hr))
return nullptr;
// Create swap chain.
IDXGIAdapter* adapter;
IDXGIFactory* factory;
hr = dxgiDevice->GetAdapter(&adapter);
dxgiDevice->Release();
if (FAILED(hr))
return nullptr;
adapter->GetParent(IID_PPV_ARGS(&factory));
hr = factory->CreateSwapChain(device_, &desc, &swapchain);
adapter->Release();
factory->Release();
return swapchain;
}
Render Target is bound using the call:
m_d3dDevice.Context()->OMSetRenderTargets(1, &m_pRenderTarget, , _pDepthStencilView);
The Present is implemented as:
swap_chain->Present(0, 0);
The shader code is:
cbuffer ConstantBuffer : register(b0)
{
matrix World;
matrix View;
matrix Projection;
float4 vLightDir[2];
float4 vLightColor[2];
float4 vOutputColor;
}
struct VS_INPUT
{
float4 Pos : POSITION;
float3 Norm : NORMAL;
};
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float3 Norm : TEXCOORD0;
};
PS_INPUT VS(VS_INPUT input)
{
PS_INPUT output = (PS_INPUT)0;
output.Pos = mul(input.Pos, World);
output.Pos = mul(output.Pos, View);
output.Pos = mul(output.Pos, Projection);
output.Norm = mul(float4(input.Norm, 1), World).xyz;
return output;
}
float4 PS(PS_INPUT input) : SV_Target
{
float4 finalColor = 0;
//do NdotL lighting for 2 lights
for (int i = 0; i < 2; i++)
{
finalColor += saturate(dot((float3)vLightDir[i],input.Norm) * vLightColor[i]);
}
finalColor.a = 1;
return finalColor;
}
float4 PSSolid(PS_INPUT input) : SV_Target
{
return vOutputColor;
}
Invoke ID3D11DeviceContext::OMSetRenderTargets(...) before each rendering
// Add this before each rendering
spImCtx->OMSetRenderTargets(1, spRTV.GetAddressOf(), spZView.Get());
// clear
spImCtx->ClearRenderTargetView(spRTV.Get(), Colors::Black);
spImCtx->ClearDepthStencilView(spZView.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0);
// drawing...
// swap
spSwapChain->Present(1, 0);

DirectXTK and 3D Ojbect

I have an application that displays 3D object using D3D11 and DirectXMath. I also want to display a HUD in the Top Left Corner so i thought i would use DirectXTK sprintBatch/spriteFont to do this. My 3D is fine until i add the following code.
Is DirectXTK using it's own shaders and changing some States?
Question is how do i fix this?
m_spriteBatch->Begin();
const wchar_t* output = L"Hello World";
m_font->DrawString(m_spriteBatch.get(), output, DirectX::XMFLOAT2{ 10, 10 }, Colors::Yellow);
m_spriteBatch->End();
Without spritefont->DrawString
With spritefont->DrawString
Here is are my shaders.
Texture2D txDiffuse : register( t0 );
SamplerState samLinear : register(s0);
cbuffer WorldViewProjectionType : register(b0)
{
matrix World;
matrix View;
matrix Projection;
};
cbuffer TransparentBuffer
{
float4 blendAmount;
};
struct VS_INPUT
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
};
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};
PS_INPUT VS(VS_INPUT input)
{
PS_INPUT output = (PS_INPUT)0;
output.Pos.w = 1.0f;
output.Pos = mul(input.Pos, World);
output.Pos = mul(output.Pos, View);
output.Pos = mul(output.Pos, Projection);
output.Tex = input.Tex;
return output;
}
float4 PS(PS_INPUT input) : SV_Target
{
float4 color = txDiffuse.Sample(samLinear, input.Tex);
color.a = blendAmount.a;
return color;
}
float4 PSGray(PS_INPUT input) : SV_Target
{
float4 color = txDiffuse.Sample(samLinear, input.Tex);
float fLuminance = 0.299f * color.r + 0.587f * color.g + 0.114f * color.b;
return float4(fLuminance, fLuminance, fLuminance, blendAmount.a);
}
Sweet, i got it working. yippy. after the swapchain->present i did reset all these.
//*************************************************************************
m_pImmediateContext->IASetInputLayout(m_pVertexLayout);
m_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
TurnOffAlphaBlending(m_pImmediateContext);
// Set the depth stencil state.
m_pImmediateContext->OMSetDepthStencilState(m_pdepthStencilState, 1);
m_pImmediateContext->OMSetRenderTargets(1, &m_pdesignerRenderTargetView, m_pdesignerDepthStencilView);
m_pImmediateContext->RSSetState(m_prasterState);
//*************************************************************************

Writing on a UAV in pixel shader

I was doing some experiment with textures as UAV in pixel shader by writing some value on it, but I'm not seeing its effect in the next draw call when I bind the same texture again as an SRV.
Example shader:
RWTexture2D<unsigned int> uav;
Texture2D tex : register(t0);
// Vertex Shader
float4 VS( float4 Pos : POSITION ) : SV_POSITION
{
return Pos;
}
// Pixel Shader, draw1 warm up
float4 PS( float4 Pos : SV_POSITION ) : SV_Target
{
return float4( 1.0f, 1.0f, 0.0f, 1.0f ); // Yellow, with Alpha = 1
}
// Pixel Shader, we are writing onto the texture by binding it as an UAV, draw2
float4 PS1( float4 Pos : SV_POSITION ) : SV_Target
{
if((Pos.x %2) && (Pos.y %2))
{
uav[Pos.xy]=0xFF000000; //some color
}
else
{
uav[Pos.xy]=0x00FF0000; //some color
}
return float4( 1.0f, 0.0f, 0.0f, 1.0f );
}
// Pixel Shader, here we are accessing texture as an SRV, draw3
float4 PS2( float4 Pos : SV_POSITION ) : SV_Target
{
float4 x = tex[Pos.xy];
return x;
}
I can provide the app source code if required.
I enabled the debug layer. It was UAV format mismatch error. In the UAV description, I declared R8G8B8A8_UNORM as a format and I'm accessing the element as UINT in the shader.
description: D3D11 ERROR: ID3D11DeviceContext::Draw: The resource return type for component 0 declared in the shader code (UINT) is not compatible with the resource type bound to Unordered Access View slot 1 of the Pixel Shader unit (UNORM). This mismatch is invalid if the shader actually uses the view [ EXECUTION ERROR #2097372: DEVICE_UNORDEREDACCESSVIEW_RETURN_TYPE_MISMATCH]
Source code:
D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc;
ZeroMemory( &SRVdesc, sizeof(SRVdesc));
UAVdesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;
UAVdesc.ViewDimension=D3D11_UAV_DIMENSION_TEXTURE2D;
UAVdesc.Texture2D.MipSlice=0;
g_pd3dDevice->CreateUnorderedAccessView( g_pTexture, &UAVdesc, &g_pUAV);
Texture created :
D3D11_TEXTURE2D_DESC TextureData;
ZeroMemory( &TextureData, sizeof(TextureData) );
TextureData.ArraySize=1;
TextureData.Height=height;
TextureData.Width=width;
TextureData.Format=DXGI_FORMAT_R8G8B8A8_TYPELESS;
TextureData.CPUAccessFlags=0;
TextureData.BindFlags=D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET|D3D11_BIND_UNORDERED_ACCESS;
TextureData.MipLevels=1;
TextureData.MiscFlags=0;
TextureData.SampleDesc.Count=1;
TextureData.SampleDesc.Quality=0;
TextureData.Usage=D3D11_USAGE_DEFAULT;
D3D11_SUBRESOURCE_DATA InitialData;
ZeroMemory( &InitialData, sizeof(InitialData));
InitialData.pSysMem=pData;
InitialData.SysMemPitch=width * sizeof(UINT);
InitialData.SysMemSlicePitch=width * sizeof(UINT) * height;
g_pd3dDevice->CreateTexture2D( &TextureData, &InitialData, &g_pTexture);
Shader code is already given above.
Fix:
D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc;
ZeroMemory( &SRVdesc, sizeof(SRVdesc));
**UAVdesc.Format=DXGI_FORMAT_R32_UINT;**
UAVdesc.ViewDimension=D3D11_UAV_DIMENSION_TEXTURE2D;
UAVdesc.Texture2D.MipSlice=0;
g_pd3dDevice->CreateUnorderedAccessView( g_pTexture, &UAVdesc, &g_pUAV);
confirmed by dumping texture in staging resource. Thanks guys.

Vertex color is not interpolated in the context of ID3DXLine

I've created a standard Win32 DirectX9 window and I'm rendering to it using a custom effect, however I have a problem where the colours of vertices are not interpolated in the result.
void CRender::Begin()
{
perf.begin();
// Capture device state so it can be restored later.
// We use ID3DXLine::Begin() to fix some bugs that I don't know how to fix.
mpLine->Begin();
// Setup shader
shader.Begin( static_cast<float>(FloatTime()) );
}
void CRender::End()
{
// Reverse order of Begin()
shader.End();
mpLine->End();
}
The problem here lies with mpLine->Begin(), without calling this I get a perfectly nice interpolated triangle, with it the whole triangle has the same colour as the first vertex.
Image for clarification: http://i.imgur.com/vKN4SnE.png
I am using ID3DXLine::Begin() just to set up the device state for me. The reason I am using it is because I'm rendering in the context of another program (a game) by hooking its EndScene(). The game may leave the device in an unusable state causing rendering glitches in my overlay, all these problems go away when using ID3DXLine::Begin() except vertex colours aren't interpolated any more.
Vertex declaration:
// Create the vertex declaration for use with the shaders.
static const D3DVERTEXELEMENT9 vformat[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 8, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};
HRESULT hr = dev->CreateVertexDeclaration( vformat, &decl );
Effect source:
// Vertex shader input
struct VSIN
{
float2 coord : POSITION;
float4 color : COLOR0;
float2 tex : TEXCOORD0;
};
// Vertex shader output / Pixel shader input
struct VSOUT
{
float4 coord : POSITION;
float4 color : COLOR0;
float2 tex : TEXCOORD0;
float2 pos : TEXCOORD1;
};
uniform float2 screen;
uniform float2x4 project;
float4 vstransform( float2 coord, const float2 shift )
{
float2 final = ( mul( project, float4(coord.x,coord.y,1,1) ) + shift ) * 2 / screen;
return float4( final.x-1, 1-final.y, 0, 1 );
}
VSOUT vsfix( VSIN data )
{
VSOUT vert;
const float2 shift = { -0.5f, -0.5f };
vert.coord = vstransform( data.coord, shift );
vert.color = data.color;
vert.tex = data.tex;
vert.pos = vert.coord.xy;
return vert;
}
float4 diffuse( VSOUT vert ) : COLOR
{
float4 px = vert.color;
return px;
}
technique Diffuse
{
pass p0
{
PixelShader = compile ps_2_0 diffuse();
VertexShader = compile vs_2_0 vsfix();
}
}

HLSL invalid ps_2_0 input semantic POSITION0

I'm attempting to write a phong shader effect for Dx9 in RenderMonkey.
I'm getting a compile error in the pixel shader
*"invalid ps_2_0 input semantic 'POSITION0'"*
and I'm not sure how to fix it, although I know it's got to be something to do with the POSITION0 semantic in VS_OUTPUT.
I tried changing VS_OUTPUT's Pos semantic to TEXCOORD0, but then the system reports that
vertex shader must minimally write all four components of POSITION
Shaders are supplied below. Any suggestions?
Here's my vertex shader:
struct VS_INPUT
{
float4 Pos : POSITION0;
float3 Normal : NORMAL0;
};
struct VS_OUTPUT
{
float4 Pos : POSITION0;
float3 Normal : TEXCOORD0;
};
VS_OUTPUT vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;
Output.Pos = Input.Pos;
Output.Normal = Input.Normal;
return Output;
}
and my pixel shader:
float4x4 matViewProjection;
// light source
float4 lightPos;
float4 Ambient;
float4 Diffuse;
float4 Specular;
// material reflection properties
float4 Ke;
float4 Ka;
float4 Kd;
float4 Ks;
float nSpecular;
// eye
float4 eyePosition;
struct VS_OUTPUT
{
float4 Pos : POSITION0;
float3 Normal : TEXCOORD0;
};
float4 ps_main( VS_OUTPUT vsOutput ) : COLOR0
{
vsOutput.Pos = mul( vsOutput.Pos, matViewProjection );
float3 ViewDirection = normalize( eyePosition.xyz - vsOutput.Pos.xyz );
float3 LightDirection = normalize( lightPos.xyz - vsOutput.Pos.xyz );
float3 N = normalize( vsOutput.Normal );
float3 R = reflect( -LightDirection, N );
float LdotN = max( 0.0, dot( LightDirection, N ) );
float VdotR = max( 0.0, dot( ViewDirection, R ) );
// find colour components
float4 a = Ka * Ambient;
float4 d = Kd * Diffuse * LdotN;
float4 s = Ks * Specular * pow( VdotR, nSpecular );
float4 FragColour = Ke + a + d + s;
return FragColour;
}
Okay, I found a solution for those interested.
The Vertex Shader should have the following structs defined:
struct VS_INPUT
{
float4 Pos : POSITION0;
float3 Normal : NORMAL0;
};
struct VS_OUTPUT
{
float4 Pos : POSITION0;
float4 PosOut : TEXCOORD0;
float3 Normal : TEXCOORD1;
};
The VS_OUTPUT struct should be different in the pixel shader:
struct VS_OUTPUT
{
float4 PosOut : TEXCOORD0;
float3 Normal : TEXCOORD1;
};
My problem stemmed from the fact that you can't have a POSITION semantic as input to the pixel shader. At least for ps_2_0.
As user, one is not allowed to use the value of POSITION0 of the VertexShaderOutput in the PixelShaderFunction. This attribute seems to be cut off at some point between Vertex and Pixel Shader.
Instead you need to declare another attribute in the VertexshaderOutput, e.g.,
float4 newPosition : TEXCOORD1;
which you assign the same value as POSITION0. This new attribute, you may use ist in the PixelShaderfunction.

Resources