Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to map a Texture2D resource in DirectX11 via SharpDX.

The resource is declared as a ShaderResource, with Dynamic usage and the 'Write' CPU flag specified.

My call however fails with a generic exception from SharpDX:

_Parent.Context.MapSubresource(
  _Resource,
  0,
  SharpDX.Direct3D11.MapMode.Write,
  SharpDX.Direct3D11.MapFlags.None,
  out stream
);

I see from this question that it is supported. The MSDN docs and this other question hint that instead of using Context.MapSubresource() I should be using Texture2D.Map(), however, the DirectX11 Texture2D class does not define Map() (though it does for the D3D 10 equivalent).

If I call the above with MapMode.WriteDiscard, the call succeeds but in this case the previous content of the texture is lost, which is no good when I only want to update a section of it.

Has the Map() method been removed in Direct3D 11 or am I looking in the wrong place? Is the MapSubresource() method unsuitable or am I using it wrong?

EDIT: I declared my resource as Dynamic with CPU Write Flags - not Default as I originaly wrote - sorry for the fairly huge 'typo' that changes the entire question!

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

The main reason Direct3D10 Map methods were moved to Direct3D11 DeviceContext is to support multithreading. They were previously attached to each resource (thus implicitly, a single device), but with Direct3D11, It is now possible to update the same resource from different deffered context.

Concerning your issue with MapSubresource, you need to check this documentation on Resource Usage.

You will see that it is not possible to use Map method with Usage.Default, as it is only working with dynamic texture. Usage.Default is suitable if you are only using UpdateSubresource. The correct way to use Map is to declare the texture with Usage.Dynamic and Map with WriteDiscard. It is not possible to keep the content of the dynamic texture (update partially) as a race condition between the GPU and CPU would arise.

share|improve this answer
    
Thank you for your answer, I understand now. I made a mistake in my original question, but it applies entirely still. I will use an intermediate buffer and CopySubresourceRegion() to update my texture, as I assume that since the GPU will process these calls 'serially' the race condition no longer applies? –  sebf Nov 29 '11 at 9:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.