Page 4 of 4

Re: BallZ

Posted: Wed Jul 29, 2015 2:08 pm
by Rado1
Kjell wrote:do keep in mind that you can't use the same texture as input & output at the same time
Hm, in this case what's the usual technique, copying content of bitmaps or swapping two sets of bitmaps (one for input and one for output) in material textures and FBO attachments in each render pass?

Re: BallZ

Posted: Wed Jul 29, 2015 2:17 pm
by Kjell
Hi Rado1,
Rado1 wrote:Hm, in this case what's the usual technique, copying content of bitmaps or swapping two sets of bitmaps (one for input and one for output) in material textures and FBO attachments in each render pass?
As you probably noticed in the 2012 example i linked to earlier in this thread, i usually go with the swap approach :wink:

By the way, just to be clear .. even though i answered with the MRT solution on your "is there a way how to use FBO with more than 4-float depth?" question, that doesn't mean you necessarily need MRT in every situation where you're working with more than 4 floating point values per pixel. If you don't have all values affecting each other you can usually split the values over multiple buffers & shaders.

K

Re: BallZ

Posted: Thu Jul 30, 2015 7:23 am
by Rado1
Thanks Kjell for answers; now I have all information to implement BallZ with MRT.
Kjell wrote:If you don't have all values affecting each other you can usually split the values over multiple buffers & shaders.
I'm using it in the current intermediate version (triple RenderTargets, Shaders, Materials, ...), but my computations of position/size/color use common pre-computed values, so I would like to reduce them to one shader which is much better to maintain/extend.

Re: BallZ

Posted: Fri Jul 31, 2015 12:23 pm
by Kjell
:idea:

Just in case someone else ( other than Rado1 ) is having trouble with this .. below is a simple MRT example.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32" Source="//" DefinitionsFile="opengl.txt"/>
    <ZExpression>
      <Expression>
<![CDATA[// Make sure the textures are allocated

@UseMaterial(Material: RedMaterial);
@UseMaterial(Material: GreenMaterial);

//

Buffers[0] = GL_COLOR_ATTACHMENT0;
Buffers[1] = GL_COLOR_ATTACHMENT1;

//

glGenFramebuffers(1, Framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, Framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, RedBitmap.Handle, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, GreenBitmap.Handle, 0);
glDrawBuffers(2, Buffers);]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnRender>
    <ZExpression>
      <Expression>
<![CDATA[//

glBindFramebuffer(GL_FRAMEBUFFER, Framebuffer);]]>
      </Expression>
    </ZExpression>
    <UseMaterial Material="FooMaterial"/>
    <RenderMesh Mesh="FooMesh"/>
    <ZExpression>
      <Expression>
<![CDATA[//

glBindFramebuffer(GL_FRAMEBUFFER, 0);]]>
      </Expression>
    </ZExpression>
  </OnRender>
  <OnClose>
    <ZExpression>
      <Expression>
<![CDATA[//

glDeleteFramebuffers(1, Framebuffer);]]>
      </Expression>
    </ZExpression>
  </OnClose>
  <Content>
    <Array Name="Buffers" Type="1" SizeDim1="2"/>
    <Variable Name="Framebuffer" Type="1"/>
    <Bitmap Name="RedBitmap" Filter="1"/>
    <Material Name="RedMaterial" Shading="1" Light="0" ZBuffer="0">
      <Textures>
        <MaterialTexture Texture="RedBitmap" TexCoords="1"/>
      </Textures>
    </Material>
    <Bitmap Name="GreenBitmap" Filter="1"/>
    <Material Name="GreenMaterial" Shading="1" Light="0" ZBuffer="0">
      <Textures>
        <MaterialTexture Texture="GreenBitmap" TexCoords="1"/>
      </Textures>
    </Material>
    <Mesh Name="FooMesh">
      <Producers>
        <MeshBox Grid2DOnly="255"/>
      </Producers>
    </Mesh>
    <Shader Name="FooShader">
      <VertexShaderSource>
<![CDATA[//

void main()
{
  gl_Position = gl_Vertex;
}]]>
      </VertexShaderSource>
      <FragmentShaderSource>
<![CDATA[//

void main()
{
  gl_FragData[0] = vec4(1.0, 0.0, 0.0, 1.0);
  gl_FragData[1] = vec4(0.0, 1.0, 0.0, 1.0);
}]]>
      </FragmentShaderSource>
    </Shader>
    <Material Name="FooMaterial" Shading="1" Light="0" ZBuffer="0" Shader="FooShader"/>
  </Content>
</ZApplication>
The only thing it does is fill the RedBitmap & GreenBitmap components with their respective color.

K

Re: BallZ

Posted: Tue Aug 11, 2015 8:51 am
by Rado1
A new version of BallZ comes with the following improvements:
- improved performance (all computations are done by GPU)
- new effects; see screenshots
- new scene transition

Thanks to sharing know-how with Kjell, I was able to accomplish my first real usage of FBOs and shaders used for computations. I'm attaching also sources, so you can take some inspiration for your projects.

Please test the current version because I'm not sure how/whether it runs on various GPUs. Thanks.

Re: BallZ

Posted: Tue Aug 11, 2015 11:23 am
by VilleK
Cool :). It runs real smooth here. Interesting that it is possible to compute all the scenes inside a shader.

I notice that the "scene" variable is a float in the computeshader. Can you change this to an "int"? Or just do "int intScene=(int)scene" and then make the switch use that. I can imagine some GPUs complaining otherwise (because a "switch" statement requires a integer).

Re: BallZ

Posted: Tue Aug 11, 2015 1:08 pm
by Rado1
VilleK wrote:I notice that the "scene" variable is a float in the computeshader. Can you change this to an "int"? Or just do "int intScene=(int)scene" and then make the switch use that. I can imagine some GPUs complaining otherwise (because a "switch" statement requires a integer).
I see. I thought that all uniforms coming from ZGE ShaderVariable are only floats. If not, then of course "uniform int scene;" is better. BTW can I also pass vec* values as uniforms by ShaderVariable?

Another issue I had is precision of RenderTarget. I originally use it for computation, but observed (Kjell told me) that it supports only 256 values per channel (RGBA), which is not sufficient for computations. Therefore, usage of own GL_RGBA16 textures was necessary, and of course it requires a lot of cumbersome OpenGL programming as you can see. The situation would be better if RenderTarget allowed to specify also "precision" mapped to internalFormat parameter.

Answer to your older question:
VilleK wrote:How do you come up with ideas for all these cool formations? :)
For BallZ I take inspiration in water, sea and sea creatures...

Re: BallZ

Posted: Wed Aug 12, 2015 1:45 pm
by Rado1
Ville, I just observed that "uniform int scene;" is not working; GL ERROR: 1282 is reported. It seems ZGE supports just float uniforms now.