Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problems with colors and samples mixing in GLSL shaders  (Read 885 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Posted 2014-02-05 23:29:42 »

Hello all!

I have a slight issue here. I have a shader in which I pass the color and sample into the fragment. The texture renders fine, but when I want to draw a plain colored cube, it will be tainted black. This is because I am using mix(), mixing the color and texture.

I can fix this by passing in a uniform u_sampled, which will act as a switch between mixing the texture and color, or just the color. This is pretty messy, however... So my question is: how can I figure out if a texture (sample) is bound from the shader?

Thanks,
-wes  Smiley

Offline Gef
« Reply #1 - Posted 2014-02-05 23:41:17 »

Few articles on internet said that starting GLSL 1.3, but I've never used it :
1  
2  
3  
4  
if( textureSize( SamplerDiffuse, 0).x > 0)
   // sample texture
else
   // use color

Say us if that works !

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #2 - Posted 2014-02-05 23:44:40 »

Nope. I already saw that Sad

I think it might not work because I have not unbinded the textures?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2014-02-05 23:48:23 »

Create a 4x4 texture filled with white pixels and bind that when you want to use "no" texture.

Myomyomyo.
Offline Gef
« Reply #4 - Posted 2014-02-05 23:50:35 »

I though of that solution too, but that will not break the mix between the color and the white pixel, no ?

Perhaps you can play and force the mix_factor to 0 or 1...

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #5 - Posted 2014-02-06 00:03:04 »

That seems a bit messy though... Is there no other way?

Offline theagentd
« Reply #6 - Posted 2014-02-06 00:09:03 »

This is what I'm seeing at the moment:

 - You're shader expects that you have a texture and mixes between an input color and a texture sample.
 - You currently don't have a texture bound.
 - You expect your shader to magically adapt and ignore the texture, but instead GLSL does the only logical thing and feeds you black.

Myomyomyo.
Offline quew8

JGO Coder


Medals: 29



« Reply #7 - Posted 2014-02-06 00:09:21 »

Conditional statements in shaders are worse than messy. They are slow. Very slow. Either do as @theagentd says, as @Gef says or just use a different shader. There is no other way I'm afraid.
Offline Gef
« Reply #8 - Posted 2014-02-06 00:14:56 »

You can also use the same shader with preprocessor like "#ifdef", to enable/disable parts in your shader at compilation.
At the end, you have one shader to maintain but two or more different versions declared in your Java code.
1  
2  
3  
4  
5  
#ifdef USE_TEXTURE
    // mix color with texture
#else
    // use color
#endif

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #9 - Posted 2014-02-06 00:29:43 »

I think that quew8 is right. Conditional statements are pretty messy.

I've managed to get it working; thank you all for your input!
-wes  Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #10 - Posted 2014-02-06 01:13:32 »

Conditional statements in shaders are worse than messy. They are slow. Very slow.
This is a common misconception. If-statements aren't expensive per se, but they have some quirks which can make them useless for performance gains.


First of all, conditional assignments are extremely cheap. Example:
x = myBoolean ? 10 : 5;


Secondly, on OpenGL cards with dynamic branching, if-statements can actually increase performance. Many people want to use if-statements to skip work in shaders, realize that they give no performance gain (but also don't performance cost!) and proclaim that if-statements are slow. GPUs runs shaders in groups, usually of 16x16 pixels, but the processors on your GPU aren't very flexible. If you have an if-else-statement in your shader and 255 pixels pass the if-statement and just a single pixel does (= enters the else-block), all 256 shader executions will run both the if and else blocks. For example, I had code that skipped work for pixels with depth=1.0 (meaning the sky was visible so I can skip some computations) and it worked very well, since the sky pixels were generally a continuous area on the screen, so a significant number of 16x16 blocks could skip the computation. If you on the other hand were to look at the sky through the branches of a tree, this would break down since most 16x16 blocks would have at least a few pixels that the branches or leaves covered, so the whole block would have to run the computations.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (14 views)
2014-08-01 22:53:16

CogWheelz (14 views)
2014-08-01 22:51:43

CopyableCougar4 (15 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!