Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


xoofx

Member Since 25 Jul 2008
Offline Last Active Yesterday, 08:10 AM

#5114010 How to read depth buffer value at a pixel to CPU and get its world coordinate

Posted by xoofx on 03 December 2013 - 07:06 AM

 

A possible enhancement of the method detailed by MJP would be to create a staging texture of the size of one pixel (or the area you want to sample) and simply use DeviceContext.CopySubresourceRegion.

 

From MSDN:

Note  If you use CopySubresourceRegion with a depth-stencil buffer or a multisampled resource, you must copy the whole subresource. In this situation, you must pass 0 to the DstX, DstY, and DstZ parameters and NULL to the pSrcBox parameter.

 

 

Good catch! Though it should be still possible: ;)

  • Create a texture with same size as depth buffer, not bindable to any stage (but not staging) and format R24G8_Typeless (assuming the depth buffer is for example a D24_UNorm_S8_UInt)
  • Create a 1x1 staging texture with again this format R24G8_Typeless.
  • Copy the whole depth buffer to the first texture with DeviceContext.CopyResource()
  • Copy from first texture to staging texture on a 1x1 region with DeviceContext.CopySubresourceRegion()

I tested this scenario and it seems to work with a d3d11 device.




#5113940 How to read depth buffer value at a pixel to CPU and get its world coordinate

Posted by xoofx on 02 December 2013 - 10:26 PM

A possible enhancement of the method detailed by MJP would be to create a staging texture of the size of one pixel (or the area you want to sample) and simply use DeviceContext.CopySubresourceRegion. Also in order to avoid stalls when reading pixels back to CPU, you should consider using a pool of staging texture for the previous frames (at least 2 frames, but depending on your FPS, It might go higher).




#5113886 Dynamic Shader Linkage for Windows Store Apps (non-toolkit)

Posted by xoofx on 02 December 2013 - 06:15 PM

As MJP said, shipping D3DCompiler for Windows Store App is only possible with Windows 8.1. Also it is perfectly allowed to redistribute the D3DCompiler that is coming with W8.1 SDK, you just need to distribute it along your application as you did (this dll is not distributed by W8.x)




#5113199 SharpDX changing device size/parameters

Posted by xoofx on 30 November 2013 - 12:42 AM

Where is your code snippet that shows exactly what you are doing and where do you expect things working and where they are not working? What is the equivalent of the C++ code you are trying to replicate in SharpDX and you know for sure that it is working?

 

I don't know much about d3d9, but if you have a working C++ code/sample, the translation should not be much too difficult (apart for some change in naming, but you should be able to link to the generated documentation on sharpdx.org).

 

Whenever there is a Release() in C++, there should be a Dispose() for the same resource in C#. For  DeviceLost /ResetDevice, I don't know which  method you are referring in the original C++ API but when calling reset (or other methods), you can have a SharpDXException that will contain a ResultCode.DeviceLost or DeviceNotReset.




#5103534 [SharpDX] How to initialize ConstantBuffer?

Posted by xoofx on 22 October 2013 - 04:21 PM

Two remarks:

  • The BufferData class is initializing a Buffer object with a Stream, but the stream cannot be updated later. It must be used only to pass initial data to the buffer. The BufferData.Update() won't do anything.
  • _context.UpdateSubresource() is supposed to update the whole constant buffer, so you need to pass a region in memory that holds World, View, Proj in the same memory layout than the cbuffer declared in your shader.

 

Something like this:

[StructLayout(LayoutKind.Sequential)]
public struct ConstantBufferData
{
  public Matrix World;
  public Matrix View;
  public Matrix Projection;
}
// ....
ContantBufferData data;
[.... fill data]
var contantBuffer = Buffer Create(device, BindFlags.ConstantBuffer, ref data);
[...in rendering loop]
data.World = ...;
_context.UpdateSubresource(ref data, constantBuffer);


Note that using a struct can be problematic in case you have managed objects (array) inside. In this case, you will have to use a different approach (Use a DataBuffer or DataStream and manage the layout in unamanaged memory yourself).

 




#5051209 [SharpDX] Understanding DX11 blend states

Posted by xoofx on 08 April 2013 - 09:09 AM

I'm not sure how easy it would be to interface with that API from C# though, as the SharpDX implementation looks incomplete.

It is not incomplete, the class is marked as "partial" so the other part of the code is generated at built time by the DirectX C++ to C# code generator used in SharpDX.

 

Edit: Though there are here some NotImplementedException for GetStorageFilter/GetRetrievalFilter, will have to double check :D




#5050843 [SharpDX] Understanding DX11 blend states

Posted by xoofx on 07 April 2013 - 06:44 AM

You will be able to see D3D11 messages directly from the output window if you check "Enable native code debugging" in the Project Properties/Debug tab (you need to use the debugger to see the messages) otherwise DebugView is still an option.




#5022535 Performance comparison: HLSL Texture::GetDimension

Posted by xoofx on 17 January 2013 - 09:09 AM

My apologize to wake up this old thread, smile.png but I ran a quick test to verify Texture.GetDimensions costs. The code used for the micro-benchmark is like this: (as usual, it is a micro-benchmark with potentially some flaws...)

float2 Texture1Size;
float2 Texture2Size;
float2 Texture3Size;
float2 Texture4Size;
float2 Texture5Size;
float2 Texture6Size;
float2 Texture7Size;
float2 Texture8Size;

Texture2D Texture1;
Texture2D Texture2;
Texture2D Texture3;
Texture2D Texture4;
Texture2D Texture5;
Texture2D Texture6;
Texture2D Texture7;
Texture2D Texture8;
SamplerState PointSampler;

float2 dim(Texture2D textureObj)
{
	uint width;
	uint height;
	textureObj.GetDimensions(width, height);
	return float2(width, height);
}

const float2 MaxSize = float2(1024, 1024);

float4 PSRaw(float2 tex : TEXCOORD) : SV_TARGET
{
	float3 color = float3(tex, 0);
	color += Texture1.Sample(PointSampler, float2(0,0));
	color += Texture2.Sample(PointSampler, float2(0,0));
	color += Texture3.Sample(PointSampler, float2(0,0));
	color += Texture4.Sample(PointSampler, float2(0,0));
	color += Texture5.Sample(PointSampler, float2(0,0));
	color += Texture6.Sample(PointSampler, float2(0,0));
	color += Texture7.Sample(PointSampler, float2(0,0));
	color += Texture8.Sample(PointSampler, float2(0,0));

	return float4(color, 1);
}

float4 PSUsingGetDimension(float2 tex : TEXCOORD) : SV_TARGET
{
	float2 size = dim(Texture1);
	size += dim(Texture2);
	size += dim(Texture3);
	size += dim(Texture4);
	size += dim(Texture5);
	size += dim(Texture6);
	size += dim(Texture7);
	size += dim(Texture8);
	size /= 8;
	size /= MaxSize;

	float3 color = float3(size * tex, 0);
	color += Texture1.Sample(PointSampler, float2(0,0));
	color += Texture2.Sample(PointSampler, float2(0,0));
	color += Texture3.Sample(PointSampler, float2(0,0));
	color += Texture4.Sample(PointSampler, float2(0,0));
	color += Texture5.Sample(PointSampler, float2(0,0));
	color += Texture6.Sample(PointSampler, float2(0,0));
	color += Texture7.Sample(PointSampler, float2(0,0));
	color += Texture8.Sample(PointSampler, float2(0,0));

	return float4(color, 1);
}

float4 PSUsingCBuffer(float2 tex : TEXCOORD) : SV_TARGET
{
	float2 size = Texture1Size;
	size += Texture2Size;
	size += Texture3Size;
	size += Texture4Size;
	size += Texture5Size;
	size += Texture6Size;
	size += Texture7Size;
	size += Texture8Size;
	size /= 8;
	size /= MaxSize;

	float3 color = float3(size * tex, 0);
	color += Texture1.Sample(PointSampler, float2(0,0));
	color += Texture2.Sample(PointSampler, float2(0,0));
	color += Texture3.Sample(PointSampler, float2(0,0));
	color += Texture4.Sample(PointSampler, float2(0,0));
	color += Texture5.Sample(PointSampler, float2(0,0));
	color += Texture6.Sample(PointSampler, float2(0,0));
	color += Texture7.Sample(PointSampler, float2(0,0));
	color += Texture8.Sample(PointSampler, float2(0,0));

	return float4(color, 1);
}

All Texture# are different instance textures bound and at each frame they are cycling by one in order to avoid any driver optim.

All TextureSize# are uploaded at each frame.

 

And the results are:

  • PSRaw (sampling directly textures) and PSUsingCBuffer (sampling textures + use of size of each texture) have similar results. Let's take the basis of 1ms (measured from CPU), both having on ShaderAnalyzer an AvgCycle of 3 (I took one card as reference).
  • PSUsingGetDimension (sampling textures + texture.GetDimension) is significantly adding an overhead, with 2ms (compare to the previous basis 1ms) confirmed by an AvgCycle of  6 (doubling the results).

It seems quite normal considering the number of instructions that were added when using Texture.GetDimensions (get the size from the texture and convert uint to float).

 

So when we are seeking to save some little GPU cycle, we should better not using Texture.GetDimension and prefer passing Texture dimensions in cbuffer directly.




#5011878 C#,C++ Some insight (Not about performance)

Posted by xoofx on 17 December 2012 - 07:44 PM

Definitely C# if you care about "easier" and "faster" development cycles and you are only targeting Windows platforms (Desktop, WinRT, WP8). You can develop in C# on other platforms with Mono but it requires a costly license for each platform.

No matter what you can trick with precompiled headers in C++, you won't have the same fast design-and-run round trip you can have while developing in C#. A typical large scale C# projects with thousands of classes compiles in few seconds, even if you change an API that requires to rebuild all assemblies. Faster compilation cycle implies more refactoring/design options and no fear about making large refactoring when they can help your application to be more future proof. Concerning access to existing underlying C++ API from C#, It really depends on your needs, most of .NET wrappers useful for game dev are often up-to-date (Though OpenTK is seriously lacking some updates from latest 4.3 specs, but I wouldn't use OpenTK for a Windows only project).

Also It will depends a lot on your requirements, and there are several questions that are independent of the language of your choice: What are you willing to develop for your game? Only 2D or 3D? Full game engine from a ground up? Only game logic? What about a scene editor? What about mesh loader/converter? Are you proficient with shaders, GLSL/HLSL languages?...etc.

if you are going to build everything using raw level API like DirectX, prepare to have several months of work to build something useful from this API. If you have strictly no experience with DirectX, this could be even more than a few months. It will be a bit worse with raw C++ and DirectX, as you have to handle yourself the raw nature of the API (sometimes only #defines instead of enums, lots of method check for HRESULT, lots of APIs that are more laborious to work with - DirectInput...etc.). For C#, using MonoGame, or probably SharpDX.Toolkit would help a lot. For C++, DirectXTk would be your best option if you want to have a very minimal bootstrap above the raw API. But they are not game engines, just higher level API around DirectX.

But then, you could also consider using a game engine like Unity3d and concentrate on game logic (The scripting is in C#, under the hood, the engine is in C++ using Mono for the scripting) and you could build a prototype game in just a few hours.


#5007949 [SharpDX] Questions about DirectX11

Posted by xoofx on 06 December 2012 - 06:55 PM

Also, in case you want to access by the name of the variable in your shader and get the slot associated with this name, you can query these slots using D3DReflect (via the ID3D11ShaderReflection or ShaderReflection in SharpDX), and shaderReflection.GetResourceBindingDesc() methods. This is used by legacy D3DX Effect systems. Note that the reflection API is not certified API on the Windows Store App so if you really need this access at runtime, you will have to store them along the bytecode of your shader.

While it can be practical to hardcode these specifics register slots in the original shader, you have less opportunity after this to combine includes/shaders (as you could have some conflicts: for example if you declare in a include TextureA.h a TextureA mapped to register t0, and in TextureB.h a TextureB mapped to register t0, the HLSL compiler will failed at compiling a shader that is using these two variables for the same stage). Though assigning a specific register can be handy in some cases where you want a specific resources to be accessible at a specific slot (for example a particular constant buffer used across all your shaders... etc.), while the others variable could be assigned automatically to the available registers.


#5005919 DeviceContext->map() using system and GPU memory?

Posted by xoofx on 30 November 2012 - 08:23 PM

@MJP, indeed, unlike D3D9, D3D11 doesn't make any strict assumption about the location of the memory, but a MAP_WRITE_DISCARD needs anyway to allocate a new buffer, even if it is in a shared memory, and the previous memory of the buffer will be released at a later "flush" when It is no longer being used by the GPU. So in practice, when using MAP_WRITE_DISCARD, you are more likely to at *least* double the memory usage (exactly like a double buffering).


#5005903 DeviceContext->map() using system and GPU memory?

Posted by xoofx on 30 November 2012 - 06:54 PM

After the end of these lines, the amount of memory the program uses is greater than before. If it does in fact keep the vertices stored in main RAM and video RAM, what is the reason for this?

This is expected, as MAP_WRITE_DISCARD requires behind the scene to invalidate the previous buffer, and It has no other ways than to allocate a new buffer on the CPU side before sending it later to a new buffer on the GPU (and releasing the previous GPU buffer). So basically, at the time you use MAP_WRITE_DISCARD, It is not writing directly to the GPU memory, but delaying the writing (and storing this in a temp buffer).


#5000732 Windows Phone (7 or 8) platform that supports custom shaders?

Posted by xoofx on 13 November 2012 - 07:05 PM

You will only get custom shaders from WP8 using Direct3D11. SharpDX is providing DirectX API for WP8 so custom shaders are working well. But if you want to leverage on XNA, MonoGame is working on a port to WP8 platform - which is using SharpDX (I saw a screenshot on twitter that the port was already working, but you should better check their latest github sourcecode branches/forks)


#4999053 Using Direct X 11 from the old DX-SDK under Windows 8

Posted by xoofx on 08 November 2012 - 04:36 PM

To create DirectX 11.1 device you need to use different functions. DirectX 11 functions haven't changed so SlimDX should continue working on Windows 8.

Not exactly, with D3D11.1, you create a normal D3D11 device and you query a D3D11.1 interface on it if you want to access the new features.

I'm using SlimDX for Direct X 11 (DX-SDK) development currently under Windows 7 with VS2012. I want to install Windows 8 but my concern is that as SlimDX does not support Direct X 11.1 (Windows SDK) that if I move to Windows 8 I won't be able to work. So I'd like to know if I can have Direct X 11 (DX-SDK) installed on Windows 8 side by side with Windows SDK so that I can keep working normally.

There is no problem installing DirectX June 2010 End-User runtime or SDK on Windows 8, I doesn't interfere with the Windows SDK. From a runtime point of view, your D3D11 application will work without any issues.


#4996064 Sorry if this is a stupid question. What would it take to get DirectX on Linux?

Posted by xoofx on 31 October 2012 - 09:46 PM

There was an attempt two years ago with Gallium "Direct3D 10/11 Is Now Natively Implemented On Linux!", but not sure they have been working on it since then...




PARTNERS