1

EDIT:

My question was unclear at first, I'll try to rephrase it:

How do I use different shaders to do different rendering operations on the same mesh polygons? For example, I want to add lighting using one shader and add fog using another shader. I need to use the color interpolated from the first shader in the calculation of the second shader, but I don't know how to do it if I can't (or rather not supposed to) pass around the color buffer between shaders. Also (and that was where my question started), I need the same world-view-projection calculations for both shaders, so am I supposed to calculate it in every shader seperatly? Am I supposed to use one big shader for all my rendering operations?

Original question:

Say I have two different shader programs. The first one calculates the vertex positions in the vertex shader and does some operations in the fragment shader.

Let's say I want to use the fragment shader to do different calculations, but I still want to use the same vertex positions calculated by the first vertex shader. Do I have to calculate the vertex positions again or is there a way to share state between different shader programs?

asaf92
  • 1,557
  • 1
  • 19
  • 30
  • I want to use the same interpolated vertices positions in different fragment shaders in different shader programs without recalculating the vertex positions every time. – asaf92 Jan 27 '19 at 14:32
  • OK, and how about adding together color calculations from different fragment shaders? For example having one shader program to calculate some lighting and another shader program to calculate another type of lighting? – asaf92 Jan 27 '19 at 14:56
  • If I'm missing something fundamental about shaders let me know but I'm pretty sure I'm not supposed to just run one big shader one time right? – asaf92 Jan 27 '19 at 14:56
  • OK so I guess my question is how do I calculate color on the same pixels from different shader programs if I can't pass around the outputs of each shader? – asaf92 Jan 27 '19 at 15:35
  • 3
    @PanthersFan92: All of your questions seem to stem from a fundamental misunderstanding of what shaders do. Shaders are part of the [process of *rendering*](https://www.khronos.org/opengl/wiki/Rendering_Pipeline_Overview). VS's compute the vertex data needed for rasterization. FS's compute the products of that rasterization, to be fed into post-processing stages (blending, etc). These shaders are invoked when you *render something*. They're a part of the rendering pipeline, and the data they receive and generate is used for rendering that particular something that you asked to render. – Nicol Bolas Jan 27 '19 at 15:39
  • I understand everything you just said and still don't know how to do a simple calculation with two different shaders on the same buffer if I can't share state between them. – asaf92 Jan 27 '19 at 15:41
  • 2
    @PanthersFan92: And yet, what you just said shows that you didn't really understand what I just said. Shaders don't do "calculations" into "buffers"; they do *rendering*. If you want to use shaders to do "calculations", then you have to make calculations *look like rendering*. And therefore, you have to structure your operation to work within the confines of the rendering pipeline. – Nicol Bolas Jan 27 '19 at 15:44
  • How do I make one pass to add some color from one shader and another pass to add some lighting from another. Do I have access to the buffer that was calculated after the first shader pass? Do I not have access to this buffer? am I supposed to do every operation in one big shader? I understand the basics of what shader are etc... I just don't understand how to use them. – asaf92 Jan 27 '19 at 15:44
  • @PanthersFan92: "*How do I make one pass to add some color from one shader and another pass to add some lighting from another.*" Again, the question comes out wrong. What are you *rendering* that caused the "color from one shader" to exist, and what are you *rendering* which will "add some lighting from another"? Shaders do not exist in a vacuum; that's the point I'm trying to get across. You cannot talk about VSs and FSs without also implicitly talking about what you are *rendering*. – Nicol Bolas Jan 27 '19 at 15:49
  • I'm rendering polygon meshes. Let's say I want one shader to calculate the objects color, another shader to calculate the lighting, a third shader to calculate fog effects, another shader for some other post-effects... Can I use several different shaders for that? If so, than how? I can't share state between them so every time I calculate the color of the pixel it's independent from all the previous calculations. If I can't share state between shaders, am I supposed to do everything in one shader with several responsibilities? – asaf92 Jan 27 '19 at 15:52
  • @PanthersFan92: Maybe it would help if you could sketch out a dataflow diagram of what you're trying to achive. You know, a bunch of little boxes, with labels for input and output, in the box a description of what it calculates. That would help *us* to understand, what it is, you try to achieve. Because I have to agree with NicolBolas and Rabbis76 here: It is very difficult to grasp, what you're actually trying to do. *Just tell us, what your ultimate goal is, please.* – datenwolf Jan 27 '19 at 15:55
  • 1
    @PanthersFan92: OK, so what you're saying is that you want to render a thing, and you want to have different program objects be responsible for different *aspects* of that rendering (lighting, fog, etc). Is that what you're saying? If so, please put that in your question. – Nicol Bolas Jan 27 '19 at 16:04
  • First of all I really thank all of you for your patience and help. I do not take that for granted. Yes Nicol, that's exactly what I'm trying to do. I'll edit my question to make it more clear. – asaf92 Jan 27 '19 at 16:05
  • Unless you render by several passes, (e.g. with [Deferred Shading](https://learnopengl.com/Advanced-Lighting/Deferred-Shading)), only a VS and FS are required. The VS calculates vertices & texture positions and the FS get interpolated fragments of the primitive and calculates, e.g., the final color by doing all light/fog/etc operations. – Ripi2 Jan 27 '19 at 20:15

1 Answers1

1

you got more options:

  1. multi pass

    this one usually render the geometry into depth and "color" buffer first and then in next passes uses that as input textures for rendering single rectangle covering whole screen/view. Deferred shading is an example of this but there are many other implementations of effects that are not Deferred shading related. Here an example of multi pass:

    In first pass the planets and stars and stuff is rendered, in second the atmosphere is added.

    You can combine the passes either by blending or direct rendering. The direct rendering requires that you render to texture each pass and render in the last one. Blending is changing the color of the output in each pass.

  2. single pass

    what you describe is more like you should encode the different shaders as a functions for single fragment shader... Yes you can combine more shaders into single one if they are compatible and combine their results to final output color.

    Big shader is a performance hit but I think it would be still faster than having multiple passes doing the same.

    Take a look at this example:

    this one computes enviromental reflection, lighting, geometry color and combines them together to single output color.

  3. Exotic shaders

    There are also exotic shaders that go around the pipeline limitations like this one:

    Which are used for stuff that is believed to be not possible to implement in GL/GLSL pipeline. Anyway If the limitations are too binding you can still use compute shader...

Spektre
  • 49,595
  • 11
  • 110
  • 380