Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 29 Sep 2011
Offline Last Active Today, 02:28 PM

#5308310 Transitioning from game development to business development with C#

Posted by on 28 August 2016 - 12:37 AM

One thing I would like to add: Learn how to write unit tests and how to write software that it is properly unit testable.

This is valuable for two reasons: Firstly, "Test Driven Design" is one of those buzzwords that look good on a CV.

But more importantly, thinking about the testability of the code you write almost automatically nudges you in the direction of the SOLID principles and can be a good place to start thinking about software architecture.

From my experience in the "big business" world, having unit tests is the first step towards avoiding spaghetti code.

#5308274 DirectX11 and using more than 1 shader

Posted by on 27 August 2016 - 03:50 PM

Are you using some framework that we're not aware of? I suspect our definitions of the term "mesh" differ somewhat.


Each frame does basically the same thing:

- clear the entire screen

- for each "thing" you want to draw

   - set up the pipeline (probably at least a vertex buffer, a vertex shader and a pixel shader)

   - call draw

- repeat


If you want to use a different pixel shader for a specific object, you just use that pixel shader when setting up the pipeline for that object.

To start with, I suggest setting up the pipeline from scratch for each object. Optimizing for performance comes later.

#5308264 DirectX11 and using more than 1 shader

Posted by on 27 August 2016 - 02:23 PM

Each draw call uses the shaders (and other pipeline state) that were set on the device context previously.


Set Vertex Shader V1

Set Pixel Shader P1

Draw Mesh 1 (uses V1 and P1)


Set Pixel Shader P2

Draw Mesh 2 (uses V1 and P2)

#5290301 Is it C# Territory?

Posted by on 05 May 2016 - 01:39 PM

- Save time by going with C#

- Use saved time for pushing compute-heavy algorithms to GPU instead


This is exactly what is happening at my job currently (MRI scanner). Most of the user-facing code is moving from C++ to C#. More and more of the high-performance code is moving from C++ to CUDA and the likes.

#5276164 structs, semantics, vs_input and vs_output questions

Posted by on 17 February 2016 - 12:56 PM

From DirectX's point of view, the vertex and pixel shaders are completely independent. The only information DX uses to send data from one pipeline stage to the next are the semantics. Whatever your vertex shader writes to the variable with the TEXCOORD0 semantic will be linearly interpolated across the triangle by the rasterizer and sent to the pixel shader input variable with the same semantic.


Most people [citation needed] will simply reuse the code for the struct for both the VS output and PS input however.

#5276103 structs, semantics, vs_input and vs_output questions

Posted by on 17 February 2016 - 02:52 AM

The input and output structs don't have to be identical. The input semantics specify the fields you want to read from the vertex buffers per vertex. Most of the time, there is a field that represents the positions of a vertex, but this is not required. The input can also be a subset of the fields in your model's vertices or even empty if you only need system-generated values.


The output specifies the fields that are sent to the rasterizer. Here you are actually required to provide a field with the position semantic, so that the rasterizer knows what the triangles you want to draw look like. You can also output additional values from the vertex shader. For example, you might want to calculate the output-vertices' texture coordinates from the input-vertices' position.


Assume that you want to draw a terrain mesh, deform it using a height map and generate a color for each vertex based on its height:

struct VS_INPUT 
	float3 positionModelSpace : POSITION;
	float2 textureCoordinate : TEXCOORD0;

struct VS_OUTPUT 
	float4 positionProjSpace : POSITION;
	float4 color : COLOR0;

float4x4 gWorldMatrix;
float4x4 gViewMatrix;
float4x4 gProjectionMatrix;

VS_OUTPUT vs_main ( VS_INPUT Input )
	VS_OUTPUT Output;
	float4 positionWorldSpace = mul(Input.positionModelSpace, gWorldMatrix);
	float height = get_height_from_texture(Input.textureCoordinate);
	positionWorldSpace.y += height;
	Output.color = get_color_from_height(height);

	float4 positionViewSpace = mul(positionWorldSpace, gViewMatrix);
	Output.positionProjSpace = mul(positionViewSpace, gProjectionMatrix);

	return Output;

#5272859 3D Grass and Depth Problem

Posted by on 27 January 2016 - 12:05 PM

You can do this by setting the Depth-Stencil-State to

DepthEnable = true


#5272854 3D Grass and Depth Problem

Posted by on 27 January 2016 - 11:33 AM

Instead of alpha blending, you probably want alpha testing for the grass billboards so that no depth value is written for the transparent pixels.


You can do something like this in your pixel shader:

float4 texture = ...

if(texture.a < 0.5)

#5258098 C# Float text format in other countries

Posted by on 20 October 2015 - 08:07 AM

You can set the default culture for a thread:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#5256487 Basic constant buffer question

Posted by on 10 October 2015 - 12:15 AM

Yes, the last SetShader call will determine what shader will run. Methods like SetConstantBuffer bind things to the pipeline (device context), not a specific shader.


You can do this for example and both draw calls will use the same constant buffer:

SetConstantBuffer(0, B1)

#5255834 Possible to read back vertex from GPU?

Posted by on 06 October 2015 - 10:29 AM

Tessellation happens before the Geometry Shader, so I'm pretty sure you can use the Stream-Out stage to write your tessellated mesh to a buffer and read that back to the CPU.

#5254470 Need help for game. C#

Posted by on 28 September 2015 - 02:13 PM

I don't really understand what you mean by the "C# framework" and you can't really put XNA and Unity in the same category. XNA is/was a framework while Unity is an entire ecosystem.

If you want a useful answer you will have to tell us what you're aiming for.

#5249825 How to Set Pixel Shader Constants

Posted by on 31 August 2015 - 12:10 AM

The idea is to create one buffer per update frequency. So for example, one buffer for all the parameters that change only once per frame, one buffer for parameters that change occasionally and a buffer for parameters that change with every draw call.


Each cbuffer in HLSL essentially declares a struct. You then duplicate that struct in C++ and fill a buffer with that data. The memory layout of the HLSL and the C++ struct have to be identical, so watch out for the packing rules.

#5223502 Why high level languages are slow

Posted by on 15 April 2015 - 01:23 PM

This discussion is soooo old smile.png


The project I work on at my day job is *very* large scale. Some parts of it have hard real-time requirements and are basically written in C with classes. Some parts are C++ because they are compute-bound. But the majority of the components is written in C#, because performance simply isn't the top priority, productivity is.


Use the right tool for the job and be done with it.


Most developers I meet every day are entrenched in "their" world, but I love switching between the worlds and the languages. Spend the morning writing "fast" code, go to lunch and then switch over to writing "nice" code. What really annoys me though, is the fact that everyone and their dog calls themselves a "C# developer" nowadays after reading a few tutorials. It's sad to see so many programmers being completely oblivious of all the nice aspects of the "high level" languages that we willingly paid for by sacrificing performance.

#5218347 String: How to find the most common character occurrence.

Posted by on 22 March 2015 - 04:56 PM

Unless there are predetermined performance constraints, I would always go for a simple solution first:

private static string GetAnyMostFrequentCharacter(string input)
        throw new ArgumentOutOfRangeException("input");
    var group = input.GroupBy(c => c).OrderByDescending(g => g.Count()).First();
    return string.Format("<{0}, {1}>", group.Key, group.Count());

Or to get all most frequent characters:

private static string GetAllMostFrequentCharacters(string input)
    if (!input.Any())
        throw new ArgumentOutOfRangeException("input");
    var groups = input.GroupBy(c => c).OrderByDescending(g => g.Count());
    var firstLargestGroup = groups.First();
    var allLargestGroups = groups.Where(g => g.Count() == firstLargestGroup.Count());
    var stringBuilder = new StringBuilder();
    foreach (var group in allLargestGroups)
        stringBuilder.AppendLine(string.Format("<{0}, {1}>", group.Key, group.Count()));
    return stringBuilder.ToString();