Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 19 Mar 2014
Online Last Active Today, 10:30 PM

#5200705 C# book recommendations for beginners?

Posted by spazzarama on 29 December 2014 - 07:33 PM

Always good to have a few good books on the subject, you may also like to take a look at:

Once you know enough to ask the right questions you will find a lot of what you want to know using your favourite search engine.

#5194556 SlimDx - ResolveSubresource

Posted by spazzarama on 25 November 2014 - 12:51 AM

I have almost identical code using SharpDX (and previously worked with SlimDX), everything you are doing looks ok. The only differences I have here are that I am always passing 0 as the source and dest index (I'm not sure whether what you have would be coming through as 0 or not), and I have no need to bind to the pipeline so it has no BindFlags. Here is the example in case it helps:

// texture is multi-sampled, lets resolve it down to single sample
textureResolved = new Texture2D(texture.Device, new Texture2DDescription()
	CpuAccessFlags = CpuAccessFlags.None,
	Format = texture.Description.Format,
	Height = texture.Description.Height,
	Usage = ResourceUsage.Default,
	Width = texture.Description.Width,
	ArraySize = 1,
	SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), // Ensure single sample
	BindFlags = BindFlags.None,
	MipLevels = 1,
	OptionFlags = texture.Description.OptionFlags
// Resolve into textureResolved
texture.Device.ResolveSubresource(texture, 0, textureResolved, 0, texture.Description.Format);

#5190088 SlimDX to take Fullscreen Game-Screenshots

Posted by spazzarama on 30 October 2014 - 12:53 AM

To capture images from a fullscreen Direct3D application you require access to the underlying Direct3D device and depending upon your approach you may need to hook into the Direct3D methods. To do this you usually would need to perform some form of code injection into the target application.


I've played around with this for a few years in C# with both SlimDX and SharpDX. You can find a discussion on "Screen capture and overlays for Direct3D 9, 10 and 11 using API hooks" on my blog, and the Direct3DHook project on GitHub.  I now use SharpDX exclusively (its a bit faster, lighter weight and easier to distribute), but you can fairly easily convert to use SlimDX (or use the old link to code on the blog post - although I recommend only using the linked zip for reference as many issues have since been addressed). There is some other relevant information in an older post on my blog, this also describes an alternative approach to hooking using interface wrapping.


The Direct3DHook project uses EasyHook to inject managed C# assemblies into the target process and to hook the necessary Direct3D functions.


NOTE: SharpDX only works with C# not VB.NET - so if VB is essential you will need to stay with SlimDX.


EDIT: lol just reread that code and noticed it is a translation to VB from one of my earlier projects :)

#5189877 Questions about billboards

Posted by spazzarama on 29 October 2014 - 01:39 AM

What @unbird said, couldn't have said it any better.

#5189179 Questions about billboards

Posted by spazzarama on 26 October 2014 - 12:37 AM

You can expand and align the quads using a vertex shader and instancing without having to resort to using the geometry shader stage.


Here is the vertex shader part of a DX11 example of rendering particles using billboards and vertex shader instancing taken from Chapter 8 of my book Direct3D Rendering Cookbook. This doesn't need the geometry shader stage at all and the full example uses append/consume buffers within a compute shader to perform the particle simulation.


By using instancing to determine the index into the particle buffer there is no need to provide any input vertex buffer(s) into the vertex shader stage.

// Represents a particle
struct Particle {
    float3 Position;
    float Radius;
    float3 OldPosition;
    float Energy;

// Access to the particle buffer
StructuredBuffer<Particle> particles : register(t0);

// Some common structures and constant buffers (e.g. PS_Input, projections and so on)
#include "Common.hlsl"

// Represents the vertex positions for our triangle strips
static const float4 vertexUVPos[4] =
    { 0.0, 1.0, -1.0, -1.0 },
    { 0.0, 0.0, -1.0, +1.0 },
    { 1.0, 1.0, +1.0, -1.0 },
    { 1.0, 0.0, +1.0, +1.0 },

float4 ComputePosition(in float3 pos, in float size, in float2 vPos)
    // Create billboard (quad always facing the camera)
    float3 toEye = normalize(CameraPosition.xyz - pos);
    float3 up    = float3(0.0f, 1.0f, 0.0f);
    float3 right = cross(toEye, up);
    up           = cross(toEye, right);
    pos += (right * size * vPos.x) + (up * size * vPos.y);
    return mul(float4(pos, 1), WorldViewProjection);

PS_Input VSMainInstance(in uint vertexID : SV_VertexID, in uint instanceID : SV_InstanceID)
    PS_Input result = (PS_Input)0;

    // Load particle using vertex instance Id
    Particle p = particles[instanceID];
    // Vertices in triangle strip
    // 0-1
    //  /
    // 2-3
    // Load vertex pos using the vertexID for the vertex in the strip (i.e. 0, 1, 2 or 3)
    result.UV = vertexUVPos[vertexID].xy;
    result.Position = ComputePosition(p.Position, p.Radius, vertexUVPos[vertexID].zw);
    result.Energy = p.Energy;
    return result;

To use this shader you set the input assembler primitive topology to a "triangle strip" and then use DrawInstancedIndirect - assuming that a compute shader determines the number of particles, otherwise DrawInstanced.

#5179780 1D and 3D textures useless?

Posted by spazzarama on 12 September 2014 - 12:17 AM

I want to create a (100,200,300) 3D texture, how will this look in a 2D texture (?,?)


To represent the same number of texels as that 3D texture you would need to create one very larger 2D texture, e.g. 30,000 x 200 - of course it won't look exactly the same in memory and I would think that mipmapping will be very different and probably give undesirable results. Otherwise 300 separate 100x200 2D textures? 


Promit's link on texture tiling is probably what you are after here.

#5179754 1D and 3D textures useless?

Posted by spazzarama on 11 September 2014 - 09:58 PM

It's important to note that textures are not only used to store image data. You can store tables of lookup values, pseudo-random numbers, height maps, all sorts of information, for some of these the use of a 1D or 3D texture makes sense.


A 1D texture might be used in cell-shading to map the diffuse reflection component to a colour, or a range of other lookup tables.


A 3D texture might be used for some volumetric effects (e.g. smoke and the like). Pretty good overview of 3D textures here.

#5177825 Compile shaders in build time with common functions

Posted by spazzarama on 03 September 2014 - 04:42 AM

Hodgman's answer is definitely what you are after.


Another option, that although is not what you after is interesting non-the-less, is that DirectX 11.2 supports HLSL shader linking. Adding support for precompiled HLSL functions that can be packaged into libraries and linked into shaders at runtime. This would allow you to build up your shader libraries to support more variations without the cost of runtime HLSL compiler times.

#5177809 Domain vs Geomtry Shader

Posted by spazzarama on 03 September 2014 - 02:51 AM

You should definitely try to calculate your normals etc in the domain shader. This is the 3rd and final stage of the optional tessellation stages and is specifically used to calculate the final vertex position and data of the subdivided point. Because you are using the tessellation pipline the domain shader is going to be called no matter what, whereas the geometry shader stage is still optional and will incur additional cost (even for an empty shader).


Depending on the domain (tri or quad ) you may need to use barycentric, bilinear or bicubic interpolation to determine the correct values.


You will want to implement backface culling and/or dynamic LoD within the hull shader.


Below is an example domain shader taken from Chapter 5: Applying Hardware Tessellation of my book Direct3D Rendering Cookbook. It uses bilinear interpolation and a combination of patch and constant data for the inputs:

// This domain shader applies control point weighting with bilinear interpolation using the SV_DomainLocation
PixelShaderInput DS_Quads( HS_QuadPatchConstant constantData, const OutputPatch<DS_ControlPointInput, 4> patch, float2 uv : SV_DomainLocation )
    PixelShaderInput result = (PixelShaderInput)0;

    // Interpolate using bilerp
    float4 c[4];
    float3 p[4];
    for(uint i=0;i<4;i++) {
        p[i] = patch[i].Position;
        c[i] = patch[i].Diffuse;
    float3 position = Bilerp(p, uv);
    float2 UV = Bilerp(constantData.TextureUV, uv);
    float4 diffuse = Bilerp(c, uv);
    float3 normal = Bilerp(constantData.NormalW, uv);

    // Prepare pixel shader input:
    // Transform world position to view-projection
    result.PositionV = mul( float4(position,1), ViewProjection );
    result.Diffuse = diffuse;
    result.UV = UV;
    result.NormalW = normal;
    result.PositionW = position;
    return result;

And bilinear interpolation on float2, float3 and float4 properties for the simple quad domain:

// QUAD bilinear interpolation
float2 Bilerp(float2 v[4], float2 uv)
    // bilerp the float2 values
    float2 side1 = lerp( v[0], v[1], uv.x );
    float2 side2 = lerp( v[3], v[2], uv.x );
    float2 result = lerp( side1, side2, uv.y );
    return result;    

float3 Bilerp(float3 v[4], float2 uv)
    // bilerp the float3 values
    float3 side1 = lerp( v[0], v[1], uv.x );
    float3 side2 = lerp( v[3], v[2], uv.x );
    float3 result = lerp( side1, side2, uv.y );
    return result;    

float4 Bilerp(float4 v[4], float2 uv)
    // bilerp the float4 values
    float4 side1 = lerp( v[0], v[1], uv.x );
    float4 side2 = lerp( v[3], v[2], uv.x );
    float4 result = lerp( side1, side2, uv.y );
    return result;    


For tri domains you would use barycentric interpolation - something like the following:

// TRIANGLE interpolation (using Barycentric coordinates)
    barycentric.xyz == uvw
  C ______________ B
    \.    w    . /
     \  .    .  / 
      \    P   /
       \u  . v/
        \  . /
         \ ./
float2 BarycentricInterpolate(float2 v0, float2 v1, float2 v2, float3 barycentric)
    return barycentric.z * v0 + barycentric.x * v1 + barycentric.y * v2;

float3 BarycentricInterpolate(float3 v0, float3 v1, float3 v2, float3 barycentric)
    return barycentric.z * v0 + barycentric.x * v1 + barycentric.y * v2;

float4 BarycentricInterpolate(float4 v0, float4 v1, float4 v2, float3 barycentric)
    return barycentric.z * v0 + barycentric.x * v1 + barycentric.y * v2;

Good luck.

#5161251 How to install latest SharpDX from GitRepository.

Posted by spazzarama on 18 June 2014 - 01:30 AM


I have SharpDX 2.6.3 synced in GitHub. But I'm not sure on how to actually install it, other than how I've done it in the past and just copied the bin folder and the documentaions folder over to my main drive. But should I also copy packages and external or not?

Thanks for any help.




If you are not trying to build it, you only need the bin directory.

#5158816 (c#) list of lists of lists or outside helpers?

Posted by spazzarama on 06 June 2014 - 05:58 PM

Keep in mind that your objects within those lists are only references to the object, so aside from the small overhead of the List<T> or Dictionary objects you are not necessarily wasting much in the way of resources.


I would also think about the life-cycle of some of these objects and how they interact, and ensure that you have an efficient system for maintaining these relationships. E.g. if the bottle is lifted from the table, do you also have a reference on the bottle object to the table or do you have to look through all your objects looking for this elusive bottle reference? After considering some of these events this might help you determine the approach that works best for you. I think that maintaining a two-way parent/child relationship is a common approach.


I wouldn't use Object as the type however, I would create a base MyBaseObject class that implements support for these relationships and then inherit from this.


As to the choice between a List or a Dictionary, it really depends on how you are iterating/finding items within those lists. If you find yourself iterating Lists looking for an object that has a property X of value Y then you might want to consider using a dictionary instead. If however you find yourself iterating over the list in order to perform an action on each item within the list then the List would suffice.

#5158321 What's a quick way to get a database going for a .net text-based game?

Posted by spazzarama on 05 June 2014 - 01:51 AM

I completely agree with nfactorial,


Write a set of simple services that interact with the database as needed on behalf of your client app.


For some quick service development perhaps take a look at ASP.NET WebAPI for the server side... Of course, then you have to secure those services, you may need to have a set of services to allow each player register a username and password and require this authentication before allowing any interaction with the services that talk to the database.

#5158290 VS2013 Graphics Diagnostics problem

Posted by spazzarama on 05 June 2014 - 12:17 AM

Windows 8.1 with VS2013 (no update).


I can confirm it is working for me for the scenario you mentioned, both when using the CSSetConstantBuffers call as you have posted and with something more complex with multiple CS dispatch calls.


Doesn't work if I am using multiple render targets (stalls on capturing the frame).

#5157507 Beginners problem

Posted by spazzarama on 02 June 2014 - 04:42 AM

Sounds like something is zero smile.png


Try outputting the light.ambient, input.normal and possibly light.dir or light.diffuse:

float4 PS( VS_OUTPUT input ) : SV_Target
    return light.ambient;
    //return input.normal;
    //return light.dir;
    //return light.diffuse;

My guess is that at least light.ambient is zero. You're correct, even if the normal vector or light direction is zero (dot product with zero vector = 0) you are adding this to the finalColor so += 0 should still return you the diffuse * light.ambient.


What edition of Visual Studio are you using? If VS 2012 or VS 2013 there is a built-in graphics debugger that would allow you to step through and evaluate some of the values within your shader. When compiling the shader you must supply D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION and the shader file name - see this topic for an example. Alternatively there are a number of other debuggers available see this GameDev topic about free HLSL debugging tools.

#5157259 Visual 2013 problem with debugging shader

Posted by spazzarama on 31 May 2014 - 11:45 PM

VS can't work out which file your shader belongs to.


In your ShaderEngine.cpp


Change line 13 from:

hr = D3DCompile(buffer.data(), buffer.size(), NULL, NULL, NULL, mainFucName, "vs_5_0", D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION, NULL, &VS_Buffer, &errorVS);


hr = D3DCompile(buffer.data(), buffer.size(), name, NULL, NULL, mainFucName, "vs_5_0", D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION, NULL, &VS_Buffer, &errorVS);

then do the same for your pixel shader compiler function.


That worked for me with your sample (passing name into the pSourceName parameter). Not very intuitive smile.png