Writing 2D Shaders in C#

Hello everyone.
As someone who understands the concept of shaders but has never written any, and has an intermediate knowledge of C#, but no knowledge of HLSL, would you recommend I learn how to write shaders in HLSL, or write them in C#? The only reason I’m asking is because I feel that iterating over a Texture2D 60 times a second in C# would be slow, but with regular shaders (HLSL), they seem to run smoothly. Thanks!

Um, boy. You cannot write shaders in C#. Shader runs directly on the GPU, that’s why it has so many restrictions and a separate language.

That’s not what I meant though. I’m talking about writing a method to do something specific to a texture by modifying it’s data (color array).

Also don’t call me “boy”.

Yes if you are using monogame with spritebatch or your own triangle drawing primitive routines i recommend you at least run a few of the basic examples to use shaders in monogame.

In mg the shader language used is basically HLSL or the direct x style of shader.


Boy. Again. You cannot write shaders in C#. Yes, you can modify texture data from C#, but it’s going to be stupidly slow.

OK, I think I know what you are asking, is it better to learn HLSL and do texture manipulaton on the GPU, or, use C# to do it on the CPU.

TLDR: Yes, but it will be very slow, and you won’t learn anything from it.

It will be much better for you to learn HLSL and do it in shaders. Firstly it will perform way better, and seondly, if you do texture manipulation on the CPU in C#, you will be working in pixels, in a HLSL you will be working in texels, while the difference is not massive, it is important I think and will help you understand how the GPU does what it does. Working in HLSL you will also have to learn about the many different “spaces” to work in, world space, object space, screen space, view space etc.

Thanks. I’ll try to learn hlsl then