Author Topic: Usage of shared GLSLShader  (Read 2630 times)

Offline Wojtek

  • int
  • **
  • Posts: 62
    • View Profile
Usage of shared GLSLShader
« on: August 20, 2011, 10:39:26 pm »
Hello,

I am wondering how to use one instance of GLSLShader with multiple objects.

I am pointing to the situation when my shader is using additional uniform value, and each object where shader will be applied have different value for that uniform.

What I read in docs:
* I can call setUniform() to pass value during runtime
* I can call Object3D.setRenderHook() to assign shader to object.
* I can overwrite beforeRendering() method to pass my uniforms

My question is how to detect which obejct3d my shared is currently rendering?

Thanks for help,
Wojtek

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Usage of shared GLSLShader
« Reply #1 on: August 21, 2011, 10:31:15 am »
You can't...i should pass this information to the GLSLShader-class, i guess. For now, just use different shaders if the uniforms differs per object.

Offline Wojtek

  • int
  • **
  • Posts: 62
    • View Profile
Re: Usage of shared GLSLShader
« Reply #2 on: August 23, 2011, 09:12:10 pm »
I think I have managed that problem.
Basically what I am doing is to:
* create one GLSLShader instance so vertex/fragment shaders code is compiled once
* create one instance of proxy IRenderHook anonymous class per Object3D and delegates all calls to my one GLSLShader plus add additional setUniform() calls to beforeRendering() method.

The code looks more or less like that:
Code: [Select]
public void apply(final Object3D obj)
{
final GLSLShader su = getShader();

IRenderHook hook = new IRenderHook()
{
@Override
public boolean repeatRendering()
{
return su.repeatRendering();
}

@Override
public void onDispose()
{
su.onDispose();
}

@Override
public void clear()
{
su.clear();
}

@Override
public void beforeRendering(int arg)
{
su.setUniform(...);
su.beforeRendering(arg);
}

@Override
public void afterRendering(int arg)
{
su.afterRendering(arg);
}
};
obj.setRenderHook(hook);
}

The only thing I am wondering is when the clear() / onDispose() methods are called and if multiple calls of them will be not harming...

Egon,
If you think that this code is safe and usefull, perhaps it will be a good ideat to add it to JPCT?
As I understand the purpose of uniform values in GLSL shaders is to allow sharing the same shader between different objects.

Thanks,
Wojtek

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Usage of shared GLSLShader
« Reply #3 on: August 23, 2011, 09:24:11 pm »
Once you've called onDispose(), following calls to onDispose() or clear() won't do anything.

About the proxy solution...i would rather add this to the GLSLShader class itself. In your case, the proxy is a good solution until it has been added to GLSLShader itself, which will most likely happen in the next version.