Author Topic: GLSLShader pass matrix array  (Read 1307 times)

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
GLSLShader pass matrix array
« on: May 28, 2014, 06:05:43 pm »
Hi Egon,

Is it possible for you to add in passing matrix array to shader? I'm trying to implement hardware skinning and the input is matrix array. Some inputs are in 2 dimensional array too and I guess shader does not support it, right?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: GLSLShader pass matrix array
« Reply #1 on: May 28, 2014, 07:42:43 pm »
Should be possible. I guess it's not in there, because nobody ever needed it. I'll look into it.
Keep in mind that you will need vertex attributes as well. You'll find them in the Mesh class, not in GLSLShader...just in case that you haven't noticed them yet.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: GLSLShader pass matrix array
« Reply #2 on: May 28, 2014, 09:26:21 pm »

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: GLSLShader pass matrix array
« Reply #3 on: May 29, 2014, 12:02:47 pm »
Quote
Keep in mind that you will need vertex attributes as well. You'll find them in the Mesh class, not in GLSLShader...just in case that you haven't noticed them yet.
You're right. Initially I thought of using the 'uniform' to pass the values, but I guess it doesn't work that way. I need to use VertexAttributes instead, is that right? In this case, is it possible to pass matrix via VertexAttributes? All I can think of is passing 4 float[4] to reconstruct those values to a matrix in shader as a workaround.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: GLSLShader pass matrix array
« Reply #4 on: May 29, 2014, 12:40:21 pm »
No, vertex atttributes can only be vectors. You can pass three of them to form a matrix. Or pass all the matrices as uniforms and an index into that array as an attribute. Should work, but might be slower.

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: GLSLShader pass matrix array
« Reply #5 on: June 02, 2014, 03:31:41 pm »
I think I'll pass the matrices via uniform since they have constant 4 matrix palettes for skin animation, if I'm not wrong. BTW, can I have a jpct java version with GLSLShader matrix passing in it so I can debug the shader in pc? Im figuring out how to use glslDevil to debug. Is there any good recommendation for glsl debugger for either Android or pc?
« Last Edit: June 02, 2014, 04:03:54 pm by kkl »

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: GLSLShader pass matrix array
« Reply #6 on: June 08, 2014, 08:02:09 am »
Hi Egon,

Is it doable to pass dynamic matrix array to shader? It seems like in shader, we have to specify a fixed number of matrix uniform array size and pass values only within that limit. Is it true?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: GLSLShader pass matrix array
« Reply #7 on: June 09, 2014, 09:48:01 pm »
Yes, that's how it is. But you might use an array that "big enough" and don't fill it completely.

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: GLSLShader pass matrix array
« Reply #8 on: September 01, 2014, 08:01:33 am »
Hi Egon,

Sry to bring this up. I shared the the same shader with multiple objects and they pass different length of matrix array, but it causes an error

Code: [Select]
09-01 13:41:50.153: E/AndroidRuntime(27402): FATAL EXCEPTION: GLThread 35538
09-01 13:41:50.153: E/AndroidRuntime(27402): java.lang.ArrayIndexOutOfBoundsException: length=144; index=144
09-01 13:41:50.153: E/AndroidRuntime(27402): at com.threed.jpct.GLSLShader$Uniform.setValue(GLSLShader.java:1340)
09-01 13:41:50.153: E/AndroidRuntime(27402): at com.threed.jpct.GLSLShader.set(GLSLShader.java:1025)
09-01 13:41:50.153: E/AndroidRuntime(27402): at com.threed.jpct.GLSLShader.setUniform(GLSLShader.java:582)
09-01 13:41:50.153: E/AndroidRuntime(27402): at com.kisionlab.oceanblue3d.objects.TurtleScene$1.beforeRendering(TurtleScene.java:107)
09-01 13:41:50.153: E/AndroidRuntime(27402): at com.threed.jpct.CompiledInstance.render(CompiledInstance.java:467)
09-01 13:41:50.153: E/AndroidRuntime(27402): at com.threed.jpct.GLRenderer.drawVertexArray(GLRenderer.java:2314)
09-01 13:41:50.153: E/AndroidRuntime(27402): at com.threed.jpct.World.draw(World.java:1361)
09-01 13:41:50.153: E/AndroidRuntime(27402): at com.threed.jpct.World.draw(World.java:1099)

Is GLSLShader not meant to be shared among other objects?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: GLSLShader pass matrix array
« Reply #9 on: September 01, 2014, 08:35:29 am »
You should be able to share them. I'll look into this...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: GLSLShader pass matrix array
« Reply #10 on: September 01, 2014, 08:41:24 am »
It's a simple problem, i'll fix it this evening. However, by fixing this, your code will work but you'll create a new array instance each time you alter the length. It might be a better idea to create individual instances of GLSLShader for different array lengths to avoid this. Or isn't that an option?

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: GLSLShader pass matrix array
« Reply #11 on: September 01, 2014, 08:59:57 am »
Yea, I could do that with the individual instances GLSLShader. Does it cost more in switching the shader program? If the cost is high, is it possible to point the matrix object to shader instead of creating new array instance in GLSLShader (mayb I can convert matrix to float array manually and pass into shader)?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: GLSLShader pass matrix array
« Reply #12 on: September 01, 2014, 09:39:34 am »
The costs shouldn't be too high. Objects will be sorted by state anyway, so objects using the same combination of texture/shader will be batched together. I don't think that it will be noticable.

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: GLSLShader pass matrix array
« Reply #13 on: September 01, 2014, 09:58:02 am »
It should be ok then, thanks alot ; ).  Just that I worry that it will consume CPU at backend, expecially for live wallpaper. It just drain battery REAL fast if some CPU cycles are wasted.