2

I'm coding a pong game entirely in the fragment shader of OpenTK. C# does all the grunt work and then passes the info to the fragment shader so that it colors the correct pixels. Problem is, I have no idea how to pass information to the fragment shader itself.

Is there a way of passing information directly to some fragment shader variable, from C#?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • OpenTK appears to be a library written in c#, so the question "passing data from c# to OpenTK" doesn't make sense to me. it's all c#! I think you are asking how to use OpenTK in general, which is a very broad question. Maybe [this](https://stackoverflow.com/questions/34642804/simple-opentk-shader-not-working) will help? – John Wu Jul 09 '20 at 18:26
  • You might want to read the [OpenTK Learn section](https://opentk.net/learn/chapter1/2-hello-triangle.html) – Peri Jul 09 '20 at 22:12

1 Answers1

0

Yes, there are:

  1. Uniform variables

    • The simplest variant, corresponds to a global constant variable for the shader. The access should be the fastest because they can be cached in local memory.
  2. Uniform blocks

    • Grouping of uniform variables backed by a buffer. Can be used for storing medium-sized (~KBs) objects e.g. camera matrices, lighting setups, global settings. The advantage is that one buffer can be used for storing values for multiple shaders. They can still be cached in shared memory.
  3. Buffer storage blocks

    • Arbitrary data backed by a buffer, usually unlimited in size. The biggest advantage is that shaders can write to this block. But writing is little bit more complicated by incohorent memory access and thus need for memory barriers and such. Slowest to read.

That said, you seem to be misusing fragment shaders. Their primary use is to process fragments generated by rasterization of OpenGL primitives. So their inputs should come from e.g. vertex shader. The OpenGL way for drawing pong is to create buffers holding the paddles and a ball, their positions can be stored in uniform variables. The vertex shader then transforms the vertices of paddles, ball accordding to their locations stored in the uniforms. The fragment shader then just colors them accordingly.

Quimby
  • 17,735
  • 4
  • 35
  • 55