Sign in to follow this  

DX11 [D3D11/D2D] Mutex seems to fail...

This topic is 2645 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

Hi GameDev!

I'm currently on that D3D11 with D2D thingy and there seems to be a problem with the mutexes.

When I call this code:

//Sync the texture with D3D10
hr = SharedTexture->GetKeyedMutex10()->AcquireSync(0, INFINITE);

if(FAILED(hr))
{
LE(hr);
return hr;
}

D2D.GetRenderTarget()->BeginDraw();
RenderHUD();
D2D.GetRenderTarget()->EndDraw();

hr = SharedTexture->GetKeyedMutex10()->ReleaseSync(1);
if(FAILED(hr))
{
LE(hr);
return hr;
}

//Sync the texture with D3D11 and render to backbuffer
hr = SharedTexture->GetKeyedMutex11()->AcquireSync(1, INFINITE);
if(FAILED(hr))
{
LE(hr);
return hr;
}
ID3DX11EffectShaderResourceVariable* SRV=NULL;
ScreenQuad.GetQuadShader()->GetCustomVariable(SharedTextureIdx, &SRV);
if(SRV)
{
SRV->SetResource(SharedTexture->GetShaderResourceView());
}


ScreenQuad.Render();

hr = SharedTexture->GetKeyedMutex11()->ReleaseSync(0);
if(FAILED(hr))
{
LE(hr);
return hr;
}



I get this:

D3D10: INFO: ID3D10Device::IASetPrimitiveTopology: Topology value D3D10_PRIMITIVE_TOPOLOGY_UNDEFINED is OK to set during IASetPrimitiveTopology; but will not be valid for any Draw routine. [ STATE_SETTING INFO #237: DEVICE_IASETPRIMITIVETOPOLOGY_TOPOLOGY_UNDEFINED ]
D3D10: INFO: As many as 1 previous debug layer message(s) may be a result of restoring device state saved by D2D. [ APPLICATION_DEFINED INFO #11: STRING_FROM_APPLICATION ]
D3D10: INFO: ID3D10Device::IASetPrimitiveTopology: Topology value D3D10_PRIMITIVE_TOPOLOGY_UNDEFINED is OK to set during IASetPrimitiveTopology; but will not be valid for any Draw routine. [ STATE_SETTING INFO #237: DEVICE_IASETPRIMITIVETOPOLOGY_TOPOLOGY_UNDEFINED ]
D3D10: INFO: ID3D10Device::IASetPrimitiveTopology: Topology value D3D10_PRIMITIVE_TOPOLOGY_UNDEFINED is OK to set during IASetPrimitiveTopology; but will not be valid for any Draw routine. [ STATE_SETTING INFO #237: DEVICE_IASETPRIMITIVETOPOLOGY_TOPOLOGY_UNDEFINED ]
D3D10: INFO: As many as 1 previous debug layer message(s) may be a result of restoring device state saved by D2D. [ APPLICATION_DEFINED INFO #11: STRING_FROM_APPLICATION ]
D3D10: INFO: ID3D10Device::IASetPrimitiveTopology: Topology value D3D10_PRIMITIVE_TOPOLOGY_UNDEFINED is OK to set during IASetPrimitiveTopology; but will not be valid for any Draw routine. [ STATE_SETTING INFO #237: DEVICE_IASETPRIMITIVETOPOLOGY_TOPOLOGY_UNDEFINED ]
D3D10: INFO: As many as 1 previous debug layer message(s) may be a result of restoring device state saved by D2D. [ APPLICATION_DEFINED INFO #11: STRING_FROM_APPLICATION ]



from the Begin/End calls of Direct2D. Yes, that are just infos and I can disable them. But maybe they are important to fix my real problem:

The first time I call AcquireSync() on the D3D11 mutex it works well, but when it comes to the second time I get a E_INVALIDARG error. Even if the inputs didn't change since the last frame (I think so..).

Why could that be? Any ideas?

Share this post


Link to post
Share on other sites
I found something out!

The problem is gone when I comment out my Present() call of the SwapChain of the D3D11 device like this:

/** Presents the swapchain to the window */
HRESULT SceneRenderer_ToSwapchain::PresentSwapChain()
{
return S_OK;
//return SwapChain->Present(1, NULL);
}



Then everything works well, but unfortunatley I need this Present()call to see what my engine renders. [wink]

Maybe someone can help me now...

Share this post


Link to post
Share on other sites
I don't think that return value should actually be possible. Double check that it is acquire that is returning E_INVALIDARG. The only think I can think of is that the resource was not unbound correctly from the dx10 pipeline. From the dx10 surface you can QI for the dx10 device and then you can clear its render targets yourself - just to see if that is the issue. You can do the same and unbind the shared texture SRV from the D3D11 pipeline before releasing it too.

What hardware are you using? Maybe it's a hardware bug. You'd have to try your code on different hardware since REF and WARP don't support shared resources.

Share this post


Link to post
Share on other sites
Thanks for your response!

Quote:

I don't think that return value should actually be possible. Double check that it is acquire that is returning E_INVALIDARG.


I was confused about that E_INVALIDARG too, since the documentation only says that these three are possible:
- WAIT_ABANDONED
- WAIT_TIMEOUT
- E_FAIL

There is no E_INVALIDARG in that list. But for some reason I keep getting it..

Here is a screenshot to prove that:


See, "hr" just turned red after pressing F10.

As I said above, this only happens when I hit the code the second time. I can even see the rendering of the first frame on screen.
And after that Hud-Rendering function is only the Present()-Call.

Oh, and you might noticed that exception in the debug-output. It seems to come from the AcquireSync() call. The whole text is this:

First-chance exception at 0x7704b727 in WTech_2010.exe: Microsoft C++ exception: _com_error at memory location 0x0022de38..


Quote:

The only think I can think of is that the resource was not unbound correctly from the dx10 pipeline. From the dx10 surface you can QI for the dx10 device and then you can clear its render targets yourself - just to see if that is the issue.


How do I do that? I never used QueryInterface very often and I'm not familiar with it. However, I tried to set the rendertargets to NULL with my dummy D3D10.1 device.

Here is the updated code:

/** Renders the HUD to the backbuffer */
HRESULT Hud::RenderToBackbuffer()
{
HRESULT hr=S_OK;

//Sync the texture with D3D10
hr = SharedTexture->GetKeyedMutex10()->AcquireSync(0, INFINITE);
if(FAILED(hr))
{
LE(hr);
return hr;
}

// Tell the debug output to be quiet..
D3D10Device.GetDevice()->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_POINTLIST);

// Draw hud
D2D.GetRenderTarget()->BeginDraw();
RenderHUD();
D2D.GetRenderTarget()->EndDraw();

//Unbind rendertargets? -------------------------------------------
D3D10Device.GetDevice()->OMSetRenderTargets(0, NULL, NULL);

//Release sync of D3D10 device
hr = SharedTexture->GetKeyedMutex10()->ReleaseSync(1);
if(FAILED(hr))
{
LE(hr);
return hr;
}

//Sync the texture with D3D11 and render to backbuffer
hr = SharedTexture->GetKeyedMutex11()->AcquireSync(1, INFINITE); //<------ Fails when we get here the second time!
if(FAILED(hr))
{
LE(hr);
return hr;
}

//Set custom shader variable
ID3DX11EffectShaderResourceVariable* SRV=NULL;
ScreenQuad.GetQuadShader()->GetCustomVariable(SharedTextureIdx, &SRV);
if(SRV)
{
SRV->SetResource(SharedTexture->GetShaderResourceView());
}

//Render a screenquad
ScreenQuad.Render();

//Unbind shader resources? -------------------------------------------
ID3D11ShaderResourceView *const NoSRV[2] = {NULL,NULL};
DXUTGetD3D11DeviceContext()->PSSetShaderResources(0, 2, NoSRV);

//Release sync of D3D11
hr = SharedTexture->GetKeyedMutex11()->ReleaseSync(0);
if(FAILED(hr))
{
LE(hr);
return hr;
}


return S_OK;
}



Quote:

What hardware are you using? Maybe it's a hardware bug. You'd have to try your code on different hardware since REF and WARP don't support shared resources.


I can run the samples posted in this thread without problems...

However, here are my specs:
Win7 64-bit
3GB RAM
Nvidia GeForce 8400GS Go (Yes, it's a laptop)
AMD Turion64x2 2x2GHz

Share this post


Link to post
Share on other sites
Right, you don't actually have to QI since you already have the DX10 device (I forgot that would be the case) The code you added should indeed have cleared all render targets on the dx10 device.

The code from the other post work for me. I'm assuming that you did everything the same as in that example. Which SDK version are you using? Try creating your devices without the debug flag to see if perhaps SDKLayers is adding the unexpected return value, or perhaps causing some other error.

Right now nothing else comes to mind. If that other example ran find on your laptop hardware then I'm not sure what the difference would be in this case.

Share this post


Link to post
Share on other sites
Creating them without the debug flag didn't change it. I'm using the February 2010 SDK, but I downloaded the June 2010 SDK as well. A friend of mine got a few problems when compiling the engine with the new SDK, thats why I didn't install it yet.
I could if that would fix the issue...

Maybe I messed something up in my objects creation?

Here's code for them:

D3D10 device:

D3D10Device::D3D10Device(void)
{
HRESULT hr=S_OK;
Device=NULL;

IDXGIFactory1* DXGIFactory;
hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)(&DXGIFactory) );

IDXGIAdapter* Adapter;
DXGIFactory->EnumAdapters(0, &Adapter);

LE(D3D10CreateDevice1(
Adapter,
D3D10_DRIVER_TYPE_HARDWARE,
NULL,
D3D10_CREATE_DEVICE_DEBUG |
D3D10_CREATE_DEVICE_BGRA_SUPPORT |
D3D10_CREATE_DEVICE_SINGLETHREADED,
D3D10_FEATURE_LEVEL_9_3,
D3D10_1_SDK_VERSION,
&Device
));

Adapter->Release();

DXGIFactory->Release();
}




D3D11 device flags (Gets created via DXUT):

/** In this function you can modify the settings of the device */
bool Engine::ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings)
{
pDeviceSettings->d3d11.CreateFlags |= D3D11_CREATE_DEVICE_BGRA_SUPPORT;
pDeviceSettings->d3d11.CreateFlags |= D3D11_CREATE_DEVICE_SINGLETHREADED;
pDeviceSettings->d3d11.AdapterOrdinal=0;
return true;
}




SharedTexture creation:


D3D10_SharedTexture::D3D10_SharedTexture(ID3D11Device* D3D11Device, ID3D10Device1* D3D10Device,UINT SizeX, UINT SizeY, DXGI_FORMAT Format, HRESULT* hr)
{
HRESULT hResult;

D3D11_TEXTURE2D_DESC sharedTextureDesc;
ID3D11Texture2D *pSharedTexture11;

ZeroMemory(&sharedTextureDesc, sizeof(sharedTextureDesc));
sharedTextureDesc.Width = SizeX;
sharedTextureDesc.Height = SizeY;
sharedTextureDesc.MipLevels = 1;
sharedTextureDesc.ArraySize = 1;
sharedTextureDesc.Format = Format;
sharedTextureDesc.SampleDesc.Count = 1;
sharedTextureDesc.Usage = D3D11_USAGE_DEFAULT;
sharedTextureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
sharedTextureDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;

hResult = D3D11Device->CreateTexture2D(&sharedTextureDesc, NULL, &pSharedTexture11);
if(FAILED(hResult)) {
MessageBox(NULL, DXGetErrorDescription(hResult), TEXT("pDevice11->CreateTexture2D for shared texture"), MB_OK);
if(hr)*hr=hResult;
return;
}

// Get the keyed mutex for the shared texture (for D3D11)
IDXGIKeyedMutex *pKeyedMutex11;

hResult = pSharedTexture11->QueryInterface(__uuidof(IDXGIKeyedMutex), (void**)&pKeyedMutex11);
if(FAILED(hResult)) {
MessageBox(NULL, DXGetErrorDescription(hResult), TEXT("pSharedTexture11->QueryInterface for IDXGIKeyedMutex"), MB_OK);
if(hr)*hr=hResult;
return;
}

// Get the shared handle needed to open the shared texture in D3D10.1
IDXGIResource *pSharedResource11;
HANDLE hSharedHandle11;



hResult = pSharedTexture11->QueryInterface(__uuidof(IDXGIResource), (void**)&pSharedResource11);
if(FAILED(hResult)) {
MessageBox(NULL, DXGetErrorDescription(hResult), TEXT("pSharedTexture11->QueryInterface for IDXGIResource"), MB_OK);
if(hr)*hr=hResult;
return;
}

pSharedResource11->GetSharedHandle(&hSharedHandle11);
if(FAILED(hResult)) {
MessageBox(NULL, DXGetErrorDescription(hResult), TEXT("pSharedResource11->GetSharedHandle"), MB_OK);
if(hr)*hr=hResult;
return;
}

pSharedResource11->Release();

// Open the surface for the shared texture in D3D10.1
IDXGISurface1 *pSharedSurface10;

hResult = D3D10Device->OpenSharedResource(hSharedHandle11, __uuidof(IDXGISurface1), (void**)(&pSharedSurface10));
if(FAILED(hResult)) {
MessageBox(NULL, DXGetErrorDescription(hResult), TEXT("pDevice101->OpenSharedResource"), MB_OK);
if(hr)*hr=hResult;
return;
}

// Get the keyed mutex for the shared texture (for D3D10.1)
IDXGIKeyedMutex *pKeyedMutex10;

hResult = pSharedSurface10->QueryInterface(__uuidof(IDXGIKeyedMutex), (void**)&pKeyedMutex10);
if(FAILED(hResult)) {
MessageBox(NULL, DXGetErrorDescription(hResult), TEXT("pSharedSurface10->QueryInterface for IDXGIKeyedMutex"), MB_OK);
if(hr)*hr=hResult;
return;
}

Texture2D=pSharedTexture11;
KeyedMutex10=pKeyedMutex10;
KeyedMutex11=pKeyedMutex11;
Surface10=pSharedSurface10;

pSharedResource11->Release();


// Create the resource view
D3D11_SHADER_RESOURCE_VIEW_DESC DescRV;
DescRV.Format = Format;
DescRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
DescRV.Texture2D.MipLevels = 1;
DescRV.Texture2D.MostDetailedMip = 0;
HRESULT hr2 = DXUTGetD3D11Device()->CreateShaderResourceView( (ID3D11Resource *)Texture2D, &DescRV, &ShaderResourceView );

if(FAILED(hr2))
{
LogError() << L"ID3D11ShaderResourceView for HUD texture";

if(hr)*hr=hr2;
return;
}


if(hr)*hr=S_OK;
}




Hopefully this clears up the situation.

Edit: When I specify some other flag than 0 (DXGI_PRESENT_DO_NOT_SEQUENCE or DXGI_PRESENT_TEST) on the Present()-call it works, too, but I get no output then.

Strange that it is able to present it right for one time...

Share this post


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

  • Similar Content

    • By mister345
      Hi, can somebody please tell me in clear simple steps how to debug and step through an hlsl shader file?
      I already did Debug > Start Graphics Debugging > then captured some frames from Visual Studio and
      double clicked on the frame to open it, but no idea where to go from there.
       
      I've been searching for hours and there's no information on this, not even on the Microsoft Website!
      They say "open the  Graphics Pixel History window" but there is no such window!
      Then they say, in the "Pipeline Stages choose Start Debugging"  but the Start Debugging option is nowhere to be found in the whole interface.
      Also, how do I even open the hlsl file that I want to set a break point in from inside the Graphics Debugger?
       
      All I want to do is set a break point in a specific hlsl file, step thru it, and see the data, but this is so unbelievably complicated
      and Microsoft's instructions are horrible! Somebody please, please help.
       
       
       

    • By mister345
      I finally ported Rastertek's tutorial # 42 on soft shadows and blur shading. This tutorial has a ton of really useful effects and there's no working version anywhere online.
      Unfortunately it just draws a black screen. Not sure what's causing it. I'm guessing the camera or ortho matrix transforms are wrong, light directions, or maybe texture resources not being properly initialized.  I didnt change any of the variables though, only upgraded all types and functions DirectX3DVector3 to XMFLOAT3, and used DirectXTK for texture loading. If anyone is willing to take a look at what might be causing the black screen, maybe something pops out to you, let me know, thanks.
      https://github.com/mister51213/DX11Port_SoftShadows
       
      Also, for reference, here's tutorial #40 which has normal shadows but no blur, which I also ported, and it works perfectly.
      https://github.com/mister51213/DX11Port_ShadowMapping
       
    • By xhcao
      Is Direct3D 11 an api function like glMemoryBarrier in OpenGL? For example, if binds a texture to compute shader, compute shader writes some values to texture, then dispatchCompute, after that, read texture content to CPU side. I know, In OpenGL, we could call glMemoryBarrier before reading to assure that texture all content has been updated by compute shader.
      How to handle incoherent memory access in Direct3D 11? Thank you.
    • By _Engine_
      Atum engine is a newcomer in a row of game engines. Most game engines focus on render
      techniques in features list. The main task of Atum is to deliver the best toolset; that’s why,
      as I hope, Atum will be a good light weighted alternative to Unity for indie games. Atum already
      has fully workable editor that has an ability to play test edited scene. All system code has
      simple ideas behind them and focuses on easy to use functionality. That’s why code is minimized
      as much as possible.
      Currently the engine consists from:
      - Scene Editor with ability to play test edited scene;
      - Powerful system for binding properties into the editor;
      - Render system based on DX11 but created as multi API; so, adding support of another GAPI
        is planned;
      - Controls system based on aliases;
      - Font system based on stb_truetype.h;
      - Support of PhysX 3.0, there are samples in repo that use physics;
      - Network code which allows to create server/clinet; there is some code in repo which allows
        to create a simple network game
      I plan to use this engine in multiplayer game - so, I definitely will evolve the engine. Also
      I plan to add support for mobile devices. And of course, the main focus is to create a toolset
      that will ease games creation.
      Link to repo on source code is - https://github.com/ENgineE777/Atum
      Video of work process in track based editor can be at follow link: 
       
       

    • By mister345
      I made a spotlight that
      1. Projects 3d models onto a render target from each light POV to simulate shadows
      2. Cuts a circle out of the square of light that has been projected onto the render target
      as a result of the light frustum, then only lights up the pixels inside that circle 
      (except the shadowed parts of course), so you dont see the square edges of the projected frustum.
       
      After doing an if check to see if the dot product of light direction and light to vertex vector is greater than .95
      to get my initial cutoff, I then multiply the light intensity value inside the resulting circle by the same dot product value,
      which should range between .95 and 1.0.
       
      This should give the light inside that circle a falloff from 100% lit to 0% lit toward the edge of the circle. However,
      there is no falloff. It's just all equally lit inside the circle. Why on earth, I have no idea. If someone could take a gander
      and let me know, please help, thank you so much.
      float CalculateSpotLightIntensity(     float3 LightPos_VertexSpace,      float3 LightDirection_WS,      float3 SurfaceNormal_WS) {     //float3 lightToVertex = normalize(SurfacePosition - LightPos_VertexSpace);     float3 lightToVertex_WS = -LightPos_VertexSpace;          float dotProduct = saturate(dot(normalize(lightToVertex_WS), normalize(LightDirection_WS)));     // METALLIC EFFECT (deactivate for now)     float metalEffect = saturate(dot(SurfaceNormal_WS, normalize(LightPos_VertexSpace)));     if(dotProduct > .95 /*&& metalEffect > .55*/)     {         return saturate(dot(SurfaceNormal_WS, normalize(LightPos_VertexSpace)));         //return saturate(dot(SurfaceNormal_WS, normalize(LightPos_VertexSpace))) * dotProduct;         //return dotProduct;     }     else     {         return 0;     } } float4 LightPixelShader(PixelInputType input) : SV_TARGET {     float2 projectTexCoord;     float depthValue;     float lightDepthValue;     float4 textureColor;     // Set the bias value for fixing the floating point precision issues.     float bias = 0.001f;     // Set the default output color to the ambient light value for all pixels.     float4 lightColor = cb_ambientColor;     /////////////////// NORMAL MAPPING //////////////////     float4 bumpMap = shaderTextures[4].Sample(SampleType, input.tex);     // Expand the range of the normal value from (0, +1) to (-1, +1).     bumpMap = (bumpMap * 2.0f) - 1.0f;     // Change the COORDINATE BASIS of the normal into the space represented by basis vectors tangent, binormal, and normal!     float3 bumpNormal = normalize((bumpMap.x * input.tangent) + (bumpMap.y * input.binormal) + (bumpMap.z * input.normal));     //////////////// LIGHT LOOP ////////////////     for(int i = 0; i < NUM_LIGHTS; ++i)     {     // Calculate the projected texture coordinates.     projectTexCoord.x =  input.vertex_ProjLightSpace[i].x / input.vertex_ProjLightSpace[i].w / 2.0f + 0.5f;     projectTexCoord.y = -input.vertex_ProjLightSpace[i].y / input.vertex_ProjLightSpace[i].w / 2.0f + 0.5f;     if((saturate(projectTexCoord.x) == projectTexCoord.x) && (saturate(projectTexCoord.y) == projectTexCoord.y))     {         // Sample the shadow map depth value from the depth texture using the sampler at the projected texture coordinate location.         depthValue = shaderTextures[6 + i].Sample(SampleTypeClamp, projectTexCoord).r;         // Calculate the depth of the light.         lightDepthValue = input.vertex_ProjLightSpace[i].z / input.vertex_ProjLightSpace[i].w;         // Subtract the bias from the lightDepthValue.         lightDepthValue = lightDepthValue - bias;         float lightVisibility = shaderTextures[6 + i].SampleCmp(SampleTypeComp, projectTexCoord, lightDepthValue );         // Compare the depth of the shadow map value and the depth of the light to determine whether to shadow or to light this pixel.         // If the light is in front of the object then light the pixel, if not then shadow this pixel since an object (occluder) is casting a shadow on it.             if(lightDepthValue < depthValue)             {                 // Calculate the amount of light on this pixel.                 float lightIntensity = saturate(dot(bumpNormal, normalize(input.lightPos_LS[i])));                 if(lightIntensity > 0.0f)                 {                     // Determine the final diffuse color based on the diffuse color and the amount of light intensity.                     float spotLightIntensity = CalculateSpotLightIntensity(                         input.lightPos_LS[i], // NOTE - this is NOT NORMALIZED!!!                         cb_lights[i].lightDirection,                          bumpNormal/*input.normal*/);                     lightColor += cb_lights[i].diffuseColor*spotLightIntensity* .18f; // spotlight                     //lightColor += cb_lights[i].diffuseColor*lightIntensity* .2f; // square light                 }             }         }     }     // Saturate the final light color.     lightColor = saturate(lightColor);    // lightColor = saturate( CalculateNormalMapIntensity(input, lightColor, cb_lights[0].lightDirection));     // TEXTURE ANIMATION -  Sample pixel color from texture at this texture coordinate location.     input.tex.x += textureTranslation;     // BLENDING     float4 color1 = shaderTextures[0].Sample(SampleTypeWrap, input.tex);     float4 color2 = shaderTextures[1].Sample(SampleTypeWrap, input.tex);     float4 alphaValue = shaderTextures[3].Sample(SampleTypeWrap, input.tex);     textureColor = saturate((alphaValue * color1) + ((1.0f - alphaValue) * color2));     // Combine the light and texture color.     float4 finalColor = lightColor * textureColor;     /////// TRANSPARENCY /////////     //finalColor.a = 0.2f;     return finalColor; }  
      Light_vs.hlsl
      Light_ps.hlsl
  • Popular Now