October 17, 2018, 10:36:44 PM

Author Topic: Blitzmax shader framework  (Read 702 times)

Offline col

  • Sr. Member
  • ****
  • Posts: 397
Blitzmax shader framework
« on: June 02, 2018, 07:16:38 PM »
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 :)

Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline Derron

  • Hero Member
  • *****
  • Posts: 1354
Re: Blitzmax shader framework
« Reply #1 on: June 02, 2018, 07:57:54 PM »
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

Offline col

  • Sr. Member
  • ****
  • Posts: 397
Re: Blitzmax shader framework
« Reply #2 on: June 03, 2018, 10:07:43 PM »
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.
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline col

  • Sr. Member
  • ****
  • Posts: 397
Re: Blitzmax shader framework
« Reply #3 on: June 09, 2018, 01:21:20 PM »
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.
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline Henri

  • Full Member
  • ***
  • Posts: 154
Re: Blitzmax shader framework
« Reply #4 on: June 10, 2018, 12:23:54 AM »
Interesting work, shaders sound really cool  8) . Shader code  itself is unfamiliar to me, but might be fun to look that up someday.

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Offline Derron

  • Hero Member
  • *****
  • Posts: 1354
Re: Blitzmax shader framework
« Reply #5 on: June 10, 2018, 06:43:37 AM »
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

Offline NoNameHere

  • Jr. Member
  • **
  • Posts: 2
Re: Blitzmax shader framework
« Reply #6 on: August 03, 2018, 12:42:56 PM »
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)
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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.

Offline col

  • Sr. Member
  • ****
  • Posts: 397
Re: Blitzmax shader framework
« Reply #7 on: August 03, 2018, 07:12:28 PM »
Quote
but 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.
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline NoNameHere

  • Jr. Member
  • **
  • Posts: 2
Re: Blitzmax shader framework
« Reply #8 on: August 04, 2018, 06:26:45 PM »
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.

Offline col

  • Sr. Member
  • ****
  • Posts: 397
Re: Blitzmax shader framework
« Reply #9 on: August 05, 2018, 03:51:45 PM »
Quote
you 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'
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp