SyntaxBomb - Indie Coders

Languages & Coding => BlitzMax / BlitzMax NG => Topic started by: col on June 02, 2018, 19:16:38

Title: Blitzmax shader framework
Post by: col on June 02, 2018, 19:16:38
https://github.com/davecamp/BMax-Shader-Framework (https://github.com/davecamp/BMax-Shader-Framework)
Once downloaded see that the folder structure is
'BLITZMAX_INSTALL/mod/srs.mod/shaderframework.mod/[*files from the repo*]'

Hiya,

I've pushed a GL version of a shader framework that will support all 3 standard ( GL, D3D9 and D3D11 ) graphics contexts for BlitzMax.
The Dx version will come shortly as soon as I clean up some code.

I'm only mentioning it so soon so as to get feedback on the design of the command set for potential improvements/adjustments.

@Derron
After playing with this this afternoon, and seeing that it can be very useful for 2D with BMax I decided to open it up on my github. You kinda influenced/convinced me to look into this again :)


I'm not sure where it will end up but let's see eh :)

Title: Re: Blitzmax shader framework
Post by: Derron on June 02, 2018, 19:57:54
Cool beans. Will have a look when feeling healthy again (some influenza thing from the hospital ... the disadvantage of becoming father again ;-)).


Thanks.

bye
Ron
Title: Re: Blitzmax shader framework
Post by: col on June 03, 2018, 22:07:43
Ahhh hospitals eh... to think that you normally go there to get well again ???

Congratulations to you and your family with your new baby :) and I wish that you get well soon.
Title: Re: Blitzmax shader framework
Post by: col on June 09, 2018, 13:21:20
There's still plenty more of 'infrastructure' to resolve, however there are currently 2 examples. Dx11 can wait as most of the issues are getting the d3d9 and gl code to work in the same way that d3d11 will - in other words - I'm doing the hard bit first ;)

Example 1: Use the left and right keys to 'spin' the image to create a swirly pattern effect.
Example 2: Uses muli-texturing, a diffuse texture and a normal texture, to create a lighting effect. Move the light using the mouse.
Title: Re: Blitzmax shader framework
Post by: Henri on June 10, 2018, 00:23:54
Interesting work, shaders sound really cool  8) . Shader code  itself is unfamiliar to me, but might be fun to look that up someday.

-Henri
Title: Re: Blitzmax shader framework
Post by: Derron on June 10, 2018, 06:43:37
After some adjustments (size_T for the memalloc-lines when compiling with BlitzMaxNG - simply wrapped in "?bmxng" conditionals) both examples just worked fine. Good stuff.


bye
Ron
Title: Re: Blitzmax shader framework
Post by: NoNameHere on August 03, 2018, 12:42:56
I was wondering if it is possible to feed the previously rendered frame into the shader. I've tried to use https://github.com/davecamp/Render2Texture, the shader does work inside of the FBO and successfully renders to texture, but passing that texture to the shader for the next loop doesn't seem to work (It appears to be just black).
Here is a snippet of my code (I'm using a different shader code than https://github.com/davecamp/BMax-Shader-Framework though, but it does all the same stuff)

Local ShaderImage:TRenderImage = CreateRenderImage(gc, 800, 600)

SetRenderImage(ShaderImage)
Local Shader:TShader = TShader.Create(VertexSource, PixelSource, "")
Shader.Start()

Shader.SetUniform1i(Shader.GetUniform("tex0"), 0)
Shader.SetUniform1i(Shader.GetUniform("tex1"), 1)

Shader.Stop()
SetRenderImage(Null)

While Not (KeyDown(KEY_ESCAPE) Or AppTerminate())
SetRenderImage(Null)
Cls
SetColor(255, 255, 255)

SetRenderImage(ShaderImage)
Shader.Start()

If KeyDown(KEY_Q)
DrawText("______________________", 0, 0)
Else
DrawText("Internal text and a random number "+Int(Rnd(2000)), 0, 0)
End If

glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, TGLImageFrame(ShaderImage.Frame(0)).name)
Shader.SetUniform1i(Shader.GetUniform("tex1"), 1)
glActiveTexture(GL_TEXTURE0)

Shader.Stop()
SetRenderImage(Null)

DrawText("External text", 0, 20)

If Not KeyDown(KEY_Z) Then DrawImage(ShaderImage, 0, 0)

Flip
Wend

Print "GL error code (0 = no errors): "+glGetError()
End


Pixel shader source:

uniform sampler2D tex0;
uniform sampler2D tex1;

void main(void) {
    vec4 coord = gl_TexCoord[0];

    gl_FragColor = texture2D(tex1, coord.xy) - vec4( vec3(0.5), 0.0);
}


Vertex shader source:

void main(void) {
    gl_Position = ftransform();
    gl_TexCoord[0] = gl_MultiTexCoord0;
}


Changing texture2D(tex1, coord.xy) to texture2D(tex0, coord.xy) in the pixel shader makes it display the text in gray color, indicating that the shader itself does work, but with tex1 it only displays a black rectangle where the text should be.
I don't really know OpenGL, so apologies if my code is nonsense.
Title: Re: Blitzmax shader framework
Post by: col on August 03, 2018, 19:12:28
Quotebut passing that texture to the shader for the next loop doesn't seem to work (It appears to be just black)

There could be a couple of things that can trip you up.

1. If you set a texture to be rendered into and then want to use that 'rendered-to-texture' as a shader input you must set the current render target to a different texture or the back buffer. This is because you shouldn't have the same texture resource bound as an input and output in the gpu pipeline - sometimes the driver will fix this for you and make it look as though it is acceptable but then it could break on another system.

2. The shader compilers are incredibly aggressive at optimisation. I notice that you're not using the 'tex0' uniform in the pixel/fragment shader so that uniform will be optimised out. I doubt that your texture binding is working as you're expecting it to due to this.
Title: Re: Blitzmax shader framework
Post by: NoNameHere on August 04, 2018, 18:26:45
Quote from: col on August 03, 2018, 19:12:28
you shouldn't have the same texture resource bound as an input and output in the gpu pipeline

Thanks for pointing that out, I had no idea there are limitations like that. Making two render targets and switching between them when rendering made the thing work. Here are the sources if anyone is interested: https://drive.google.com/file/d/1R_JHKVuFnP3eZ10Nbi3sbEHAFzcO6NGV.
Title: Re: Blitzmax shader framework
Post by: col on August 05, 2018, 15:51:45
Quoteyou shouldn't have the same texture resource bound as an input and output in the gpu pipeline

I'd just like to clarify that this should read as:
'you shouldn't have the same texture resource bound as an input and output at the same time when drawing'