Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 29 Sep 2011
Online Last Active Today, 10:09 AM

#5290301 Is it C# Territory?

Posted by lwm 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 lwm 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 lwm 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 lwm 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 lwm 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 lwm 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 lwm 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 lwm 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 lwm 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 lwm 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 lwm 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 lwm 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();

#5201877 Server socket with select, thread pool and queue based approach

Posted by lwm on 05 January 2015 - 02:08 AM

Now lets say as you said, fd1 is currently in process and meanwhile we get the data from fd1 again. Again that fd1 will be added in deque in main function and any thread whichever is free can consume that. If all threads are busy in processing, we will be keeping that fd1 in deque.  

What exactly is "fd" referring to? Did I accidentally skip over an explanation somewhere?

rip-off already mentioned that a single receive-call does not necessarily correspond with one single logical request. You may need multiple receive-calls until you have a full request. Or you may receive multiple requests (the first and last ones possibly being incomplete) with one receive call.


You mentioned 100 concurrent clients. Is that a realistic goal?

If so, using blocking IO calls is probably not a good idea. Use asynchronous IO wherever possible.

I just finished implementing a diagnostics server with soft real-time requirements at work and was pleasantly surprised how elegant the whole thing became once I left all the nasty waiting and blocking to the software that does it best: the operating system.

Is boost::asio an option for you?

#5186887 Example code wanted for Broad Phase Collision Detection.

Posted by lwm on 14 October 2014 - 05:28 AM

There are many ways of implementing a broad phase and there is no single correct way. It depends on many factors like whether you're in 2D or 3D, the number of entities, the average distance between entities, whether most of them are static or dynamic, etc. You could try:


  • Brute Force: This can actually be a good choice for a reasonable number of entities if you can make your data structures cache-friendly.
  • Spatial Partitioning: There are many examples on Quadtrees and Octrees out there.
  • Spatial Hashing: Good for many moving entities since reinsertion is fast, but has other drawbacks instead.

I've been using BepuPhysics for some time now. You can look at the source code at CodePlex.

#5175210 Destructor in vector called too often

Posted by lwm on 21 August 2014 - 03:21 AM

Would still be nice though if anybody with visual studio 2013 or the 2012 November compiler CTP could also test the code and confirm that this is happening for him too.


I'm sorry to say that I can't reproduce the problem with Visual Studio 2013 Update 2.