Board index FlightGear Development Effects and shaders

Help Needed: Writing first shader

An exciting "new" option in FlightGear, that includes reflections, lightmaps, the particle system etc.. A lot is yet to be discovered/implemented!

Help Needed: Writing first shader

Postby merspieler » Sat Oct 03, 2020 12:56 am

So I've written (and partly copy-pasted) a shader which works fine in shadertoy.

I'm trying to port that to a compositor pipeline.

I've taken the ALS pipeline and changed the buffers to cubemaps
Code: Select all
  <buffer>
    <name>color</name>
    <type>cubemap</type>
    <width>2048</width>
    <height>2048</height>
    <format>rgba8</format>
  </buffer>
  <buffer>
    <name>depth</name>
    <type>cubemap</type>
    <width>2048</width>
    <height>2048</height>
    <format>depth32f</format>
  </buffer>

Then I've added another pass at the bottom with an effect:
Code: Select all
  <pass>
    <name>display</name>
    <type>quad</type>
    <binding>
      <buffer>color</buffer>
      <unit>0</unit>
    </binding>
    <effect>Compositor/Effects/projection</effect>
  </pass>

The effect currently looks like this(there are still some parameters missing... and maybe stuff, I've missed???):
Code: Select all
<?xml version='1.0' encoding='UTF-8'?>
<PropertyList>
        <name>Effects/Compositor/projection</name>
        <parameters>
                <display_xsize><use>/sim/startup/xsize</use></display_xsize>
                <display_ysize><use>/sim/startup/ysize</use></display_ysize>
                <hfov><use>/sim/current-view/field-of-view</use></hfov>
                <vfov>180</vfov><!-- TODO add parameter -->
                <auto_vfov>1</auto_vfov><!-- TODO add parameter -->
                <enable_overlay>1</enable_overlay><!-- TODO add parameter -->
        </parameters>
        <technique n="1">
                <pass>
                        <program>
                                <fragment-shader>Compositor/Shaders/ALS/projection.frag</fragment-shader>
                        </program>
                        <uniform>
                                <name>display_xsize</name>
                                <type>int</type>
                                <value><use>display_xsize</use></value>
                        </uniform>
                        <uniform>
                                <name>display_ysize</name>
                                <type>int</type>
                                <value><use>display_ysize</use></value>
                        </uniform>
                        <uniform>
                                <name>hfov</name>
                                <type>float</type>
                                <value><use>hfov</use></value>
                        </uniform>
                        <uniform>
                                <name>vfov</name>
                                <type>float</type>
                                <value><use>vfov</use></value>
                        </uniform>
                        <uniform>
                                <name>auto_vfov</name>
                                <type>int</type>
                                <value><use>auto_vfov</use></value>
                        </uniform>
                        <uniform>
                                <name>enable_overlay</name>
                                <type>int</type>
                                <value><use>enable_overlay</use></value>
                        </uniform>
                </pass>
        </technique>
</PropertyList>

In shadertoy to I've got this line:
Code: Select all
vec4 ret = texture( iChannel0, ray );

Now obviously in flightgear I need to use `texture3D()` instead.
`iChannel0` is a cubemap input.
So... how do I access the `color` buffer I've defined in the pipline from within my shader?
Nia (you&, she/her)

Please use gender neutral terms when referring to a group of people!

Be the change you wish to see in the world, be an ally to all!

Join the official matrix space
merspieler
 
Posts: 2295
Joined: Thu Oct 26, 2017 11:43 am
Location: Wish to be in YBCS
Pronouns: you&, she/her
Callsign: you&, she/her
IRC name: merspieler
Version: next
OS: NixOS

Re: Help Needed: Writing first shader

Postby Necolatis » Sun Oct 04, 2020 2:56 pm

In the effect you need to define iChannel0 in parameters and then again inside the technique as an uniform.
See http://wiki.flightgear.org/Effect_Framework
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2238
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: Help Needed: Writing first shader

Postby merspieler » Sun Oct 04, 2020 3:15 pm

Okay... but it doesn't say anything what I've got to put into the parameter block, to access a buffer...
Nia (you&, she/her)

Please use gender neutral terms when referring to a group of people!

Be the change you wish to see in the world, be an ally to all!

Join the official matrix space
merspieler
 
Posts: 2295
Joined: Thu Oct 26, 2017 11:43 am
Location: Wish to be in YBCS
Pronouns: you&, she/her
Callsign: you&, she/her
IRC name: merspieler
Version: next
OS: NixOS

Re: Help Needed: Writing first shader

Postby icecode » Sun Oct 04, 2020 10:53 pm

So... how do I access the `color` buffer I've defined in the pipline from within my shader?


You can expose buffers to passes via the binding tag. See http://wiki.flightgear.org/Compositor#Passes. The unit parameter specifies the texture unit where all shaders being used on that pass will be able to access the buffer.
icecode
 
Posts: 710
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Help Needed: Writing first shader

Postby merspieler » Mon Oct 05, 2020 12:16 am

I've done that already... see my first post.

Then how do I access it in the shader?
Nia (you&, she/her)

Please use gender neutral terms when referring to a group of people!

Be the change you wish to see in the world, be an ally to all!

Join the official matrix space
merspieler
 
Posts: 2295
Joined: Thu Oct 26, 2017 11:43 am
Location: Wish to be in YBCS
Pronouns: you&, she/her
Callsign: you&, she/her
IRC name: merspieler
Version: next
OS: NixOS

Re: Help Needed: Writing first shader

Postby icecode » Thu Oct 08, 2020 12:20 pm

The buffer is just a normal texture at this point... Use a sampler2D uniform with the texture unit you have chosen as its value. If you don't know how to do the Effect part, Necolatis did a nice write-up on the Effects framework. The $FG_ROOT/Effects directory is also filled with practical examples.
icecode
 
Posts: 710
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Help Needed: Writing first shader

Postby merspieler » Thu Oct 08, 2020 12:31 pm

it's sampler3D in my case ;-)

... !thanks for RTFM... I read it already... and I didn't find, what I was looking for.

So just tell me, how to get that buffer object as a uniform in the effect.
Nia (you&, she/her)

Please use gender neutral terms when referring to a group of people!

Be the change you wish to see in the world, be an ally to all!

Join the official matrix space
merspieler
 
Posts: 2295
Joined: Thu Oct 26, 2017 11:43 am
Location: Wish to be in YBCS
Pronouns: you&, she/her
Callsign: you&, she/her
IRC name: merspieler
Version: next
OS: NixOS

Re: Help Needed: Writing first shader

Postby icecode » Thu Oct 08, 2020 7:03 pm

I don't understand what you are having trouble with...

Just write something like
Code: Select all
<uniform>
  <name>youruniformname</name>
  <type>sampler-3d</type>
  <value>yourtextureunit</value>
</uniform>
icecode
 
Posts: 710
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Help Needed: Writing first shader

Postby merspieler » Thu Oct 08, 2020 9:26 pm

Okay, I somehow expected to have another block in the parameters which I then reference in the <value>, like with the other uniforms.

So next issue I'm facing.

According to the wiki, fg_BufferSize are the window dimensions.
And according to GL docs, gl_FragCoord contains the coords of the fragment.

So the following should at least give me a transition of colors:
Code: Select all
vec2 uv = gl_FragCoord.xy / fg_BufferSize.xy;
vec4 ret = vec4(uv.x, uv.y, 0.0, 1.0);
gl_FragColor = ret;


But instead, I get a plain yellow window.

I've put the code into shadertoy and I get transitions with the corners being green, yellow, red and black (clockwise, starting top left).

What am I doing wrong?
Nia (you&, she/her)

Please use gender neutral terms when referring to a group of people!

Be the change you wish to see in the world, be an ally to all!

Join the official matrix space
merspieler
 
Posts: 2295
Joined: Thu Oct 26, 2017 11:43 am
Location: Wish to be in YBCS
Pronouns: you&, she/her
Callsign: you&, she/her
IRC name: merspieler
Version: next
OS: NixOS

Re: Help Needed: Writing first shader

Postby icecode » Sat Oct 10, 2020 11:52 am

fg_BufferSize comes from Rembrandt and is outdated. Use fg_ViewportSize instead.
icecode
 
Posts: 710
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Help Needed: Writing first shader

Postby merspieler » Sat Oct 10, 2020 2:04 pm

Okay, thanks. This got me a hugh step further.

My uniform sampler3D is still empty.

Is it possible, that I need to copy and modify all effects to use a cubemap instead?

That would be a nightmare to maintain...
Nia (you&, she/her)

Please use gender neutral terms when referring to a group of people!

Be the change you wish to see in the world, be an ally to all!

Join the official matrix space
merspieler
 
Posts: 2295
Joined: Thu Oct 26, 2017 11:43 am
Location: Wish to be in YBCS
Pronouns: you&, she/her
Callsign: you&, she/her
IRC name: merspieler
Version: next
OS: NixOS

Re: Help Needed: Writing first shader

Postby icecode » Sun Oct 11, 2020 8:43 pm

That tediousness is solved by Effect schemes. They allow using different effects for the same model depending on the rendering pass. They also allow setting a default technique to use when an effect doesn't implement a particular scheme. This is a bit advanced and can be tricky to understand if you are just starting out with Effects. There aren't many practical examples either. Perhaps you can get something out of this.
icecode
 
Posts: 710
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Help Needed: Writing first shader

Postby merspieler » Mon Oct 12, 2020 12:28 pm

This doesn't answer the question, if I need to rewrite some shaders cause I'm rendering to a cubemap now.
Nia (you&, she/her)

Please use gender neutral terms when referring to a group of people!

Be the change you wish to see in the world, be an ally to all!

Join the official matrix space
merspieler
 
Posts: 2295
Joined: Thu Oct 26, 2017 11:43 am
Location: Wish to be in YBCS
Pronouns: you&, she/her
Callsign: you&, she/her
IRC name: merspieler
Version: next
OS: NixOS

Re: Help Needed: Writing first shader

Postby icecode » Mon Oct 12, 2020 6:22 pm

It does answer your question, you just need to have more experience with Effects and shaders before delving further in this topic. You can propose another project for the virtual FSWeekend event and I can try to help you a bit more directly there.
icecode
 
Posts: 710
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Help Needed: Writing first shader

Postby merspieler » Wed Oct 14, 2020 10:16 am

Well... if the answer is "no", I'll continue down this path and try to get it working.
If the answer is "yes", then I know enough, to fix my initiall issue, without adding any other new features.

As for the FSWeekend... if you say, I shouldn't do it then I'll probably just hop onto another idea... cause right now, I don't really have any other... except messing with the A320 or A350.
Nia (you&, she/her)

Please use gender neutral terms when referring to a group of people!

Be the change you wish to see in the world, be an ally to all!

Join the official matrix space
merspieler
 
Posts: 2295
Joined: Thu Oct 26, 2017 11:43 am
Location: Wish to be in YBCS
Pronouns: you&, she/her
Callsign: you&, she/her
IRC name: merspieler
Version: next
OS: NixOS

Next

Return to Effects and shaders

Who is online

Users browsing this forum: No registered users and 6 guests