# Problem with shader

**URL:** <https://forum.defold.com/t/problem-with-shader/3808>\
**Category:** Questions\
**Created:** [December 30, 2016, 2:44pm UTC](https://forum.defold.com/t/problem-with-shader/3808 "2016-12-30T14:44:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [December 30, 2016, 2:44pm UTC](https://forum.defold.com/t/problem-with-shader/3808/1 "2016-12-30T14:44:09Z")

</div>

I am trying to implement 2d light. I port 2d light from libgdx [See here: https://github.com/mattdesl/lwjgl-basics/wiki/2D-Pixel-Perfect-Shadows · GitHub](https://gist.github.com/mattdesl/5286905) . When i try it in libgdx i can have about 70 lights, before fps go down. But when i try defold i can have 5 light before fps go down. The problem is with shadow fragment shader. If i comment line in shader(distance = min(distance, dst)) fps is ok. If watch in profiler, the problem is with vsync. What i am doing wrong, how can i fixed it.

```lua
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP 
#endif
#define PI 3.14

varying vec2 vTexCoord0;
varying LOWP vec4 vColor;

uniform sampler2D u_texture;
uniform vec2 resolution;

//for debugging; use a constant value in final release
uniform float upScale;

//alpha threshold for our occlusion map
const float THRESHOLD = 0.75;

void main(void) {
  float distance = 1.0;
  
  for (float y=0.0; y<resolution.y; y+=1.0) {
    	//rectangular to polar filter
		vec2 norm = vec2(vTexCoord0.s, y/resolution.y) * 2.0 - 1.0;
		float theta = PI*1.5 + norm.x * PI; 
		float r = (1.0 + norm.y) * 0.5;
		
		//coord which we will sample from occlude map
		vec2 coord = vec2(-r * sin(theta), -r * cos(theta))/2.0 + 0.5;
		
		//sample the occlusion map
		vec4 data = texture2D(u_texture, coord);
		
		//the current distance is how far from the top we've come
		float dst = y/resolution.y / upScale;
		
		//if we've hit an opaque fragment (occluder), then get new distance
		//if the new distance is below the current, then we'll use that for our ray
		float caster = data.a;
		if (caster > THRESHOLD) {
                       //if comment this line all work correct
			distance = min(distance, dst);
  		}
  } 
  gl_FragColor = vec4(vec3(distance), 1.0);
}

```

module that render light

```lua
local Target_helper = require "helpers.render.render_target.render_target_helper"
local Screen_helper = require "helpers.render.screen.screen_helper"
local M = {}
M.__index = M

local add_light_hash=hash("add_light")

local function init_render_targets(self,size)
	self.light_size=size
	self.pred = render.predicate({"screen"})
	self.block_light_pred = render.predicate({"block_light"})
	self.occlusion_map_target=Target_helper.create_default("2dlight_occlusion_map",size,size,false)
	
	local shadow_color_params = {
    	format = render.FORMAT_RGBA,
    	width = size,
    	height = size,
    	min_filter = render.FILTER_LINEAR,
    	mag_filter = render.FILTER_LINEAR,
    	u_wrap = render.WRAP_REPEAT,
    	v_wrap = render.WRAP_REPEAT }
	self.shadow_map_target=Target_helper.new_with_params("2dlight_shadow_map",size,1,shadow_color_params,nil)
	self.light_map_target=Target_helper.create_default("2dlight_light_map",size,size,false)
	self.clear_color = vmath.vector4(0, 0, 0, 1)
	
	self.shadow_map_constants = render.constant_buffer()
    self.shadow_map_constants.resolution = vmath.vector4(size, size, 0, 0)

end

local function enable_blend(self)
	render.enable_state(render.STATE_BLEND)
	if(self.additive) then
    	render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE)
    else
    	render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)
	end
end

local function draw_occlusion(self,light)
	local width=render.get_width()
	local height=render.get_height()
	render.set_viewport(0, 0, self.light_size, self.light_size)
	render.set_projection(vmath.matrix4_orthographic(-self.light_size/2+light.position.x, self.light_size/2+light.position.x, -self.light_size/2+light.position.y, self.light_size/2+light.position.y, -1, 1))
	render.enable_render_target(self.occlusion_map_target.target)
	enable_blend(self)
	render.clear({[render.BUFFER_COLOR_BIT] = vmath.vector4(0,0,0,0), [render.BUFFER_DEPTH_BIT] = 1, [render.BUFFER_STENCIL_BIT] = 0})
	render.draw(self.block_light_pred)
	enable_blend(self)
	render.disable_render_target(self.occlusion_map_target.target)
	
	--self.screen_helper:draw_in(self.occlusion_map_target.target,0,0,256,256)
end

local function draw_shadow_map(self)

	render.enable_render_target(self.shadow_map_target.target)
	render.enable_material("2d_light_shadow")
	self.screen_helper:draw(self.occlusion_map_target.target,self.shadow_map_constants,vmath.vector4(0,0,0,0))
	render.disable_material("2d_light_shadow")
	render.disable_render_target(self.shadow_map_target.target)
	--self.screen_helper:draw_in(self.shadow_map_target.target,0,0,256,256)
end

local function draw_light_map(self,light)
	local width=render.get_width()
	local height=render.get_height()
	local width_scale = width/self.light_size
	local height_scale = height/self.light_size
	render.set_viewport(0, 0, render.get_window_width(), render.get_height())
	local x=0.01 * width_scale-light.position.x/width*0.02*width_scale + 0.01
	local y=0.01 * height_scale-light.position.y/height*0.02*height_scale + 0.01
	
	local projection=vmath.matrix4_orthographic(x-0.01*width_scale,x+0.01*width_scale,y-0.01*height_scale,y+0.01*height_scale,-1,1)
	render.set_projection(projection)
	
	--print(light.color)
	render.enable_material("light_map")
	render.enable_texture(0, self.shadow_map_target.target, render.BUFFER_COLOR_BIT)
	
	render.draw(self.pred,light.const)
	render.disable_texture(0,self.shadow_map_target.target)
	render.disable_material("light_map")
end

function M.new(light_size, up_scale, additive, soft_shadows)
	local self = setmetatable({},M)
	self.screen_helper=Screen_helper.new(render)
	self.lights={}
	init_render_targets(self,light_size)
    return self
end

local function draw_light(self,light)
	draw_occlusion(self,light)
	draw_shadow_map(self)
	--draw_light_map(self,light)
	render.set_viewport(0, 0, render.get_window_width(), render.get_height())
end

function M:draw()
	for key,light in pairs(self.lights) do
		draw_light(self,light)
	end
end

function M:handle_message(message_id,message)
	if(message_id==add_light_hash)then
		local light_constants = render.constant_buffer()
    	light_constants.resolution = vmath.vector4(self.light_size, self.light_size, 0, 0)
   		light_constants.vColor = message.color
		local light={position=message.position,size=message.size,color=message.color,const=light_constants}
		self.lights[message.light_id]=light
	end
end

return M

```

 ![](https://forum-defold-uploads.b-cdn.net/original/2X/a/abfc3bf108fc708cb95c746e4e438b75188b4593.png)

---

<div class="post-metadata">

**Author:** ![alex.tyk97](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@alex.tyk97](https://forum.defold.com/u/alex.tyk97)\
**Post date:** [December 31, 2016, 11:10am UTC](https://forum.defold.com/t/problem-with-shader/3808/2 "2016-12-31T11:10:40Z")

</div>

I do not know why this is happening.  
But maybe it’s because of OpenGL ES.  
At least under your link and it is written:  
“However, as these features are not supported in OpenGL ES, I chose not to use them.”

I think this is because the shader operator “if” is always expensive, and apparently GLSL ES did not optimize it as GLSL.

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [December 31, 2016, 11:57am UTC](https://forum.defold.com/t/problem-with-shader/3808/3 "2016-12-31T11:57:06Z")

</div>

This is not gl\_es issue, because i test on pc. The problem is with defold, or with my code. I use same shader in libgdx and in defold.Libgdx 70 light sources, defold 6 light sources.

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [January 1, 2017, 1:59pm UTC](https://forum.defold.com/t/problem-with-shader/3808/4 "2017-01-01T13:59:45Z")

</div>

The problem with shadow shader if i use y+=10 instead of y+=1 i can have a lot of light sources. Also if i change projection to bigger, light not work correct but i can have a lot of light sources too. Why shader can’be so slow? As i said early **libgdx is about x10 faster.**  **Where is defold perfomance?** I tryed web profiler but it is broken.It show me 41ms per frame when game profiler show me 16ms. In game profiler Engine use 84ms while graphics 0.7ms. What mean engine?Is shaders time in engine or in graphics?

```lua
local function draw_shadow_map(self)
	self.projection=vmath.matrix4_orthographic(0,0.02,0,0.02,-1,1)
	render.set_projection(self.projection)
	render.enable_render_target(self.shadow_map_target.target)
	render.clear({[render.BUFFER_COLOR_BIT] = vmath.vector4(0,0,0,0), [render.BUFFER_DEPTH_BIT] = 1, [render.BUFFER_STENCIL_BIT] = 0})
	render.enable_material("2d_light_shadow")
	render.enable_texture(0,self.occlusion_map_target.target, render.BUFFER_COLOR_BIT)
	--self.screen_helper:draw(self.occlusion_map_target.target,self.shadow_map_constants,vmath.vector4(0,0,0,0))
	render.draw(self.pred,self.shadow_map_constants)
	render.disable_texture(0,self.occlusion_map_target.target)
	render.disable_material("2d_light_shadow")
	render.disable_render_target(self.shadow_map_target.target)
	self.screen_helper:draw_in(self.shadow_map_target.target,0,0,256,256)
end

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 1, 2017, 2:56pm UTC](https://forum.defold.com/t/problem-with-shader/3808/5 "2017-01-01T14:56:08Z")

</div>

> [@d954mas](#):
>
> libgdx is about x10 faster. Where is defold perfomance?

I don’t have enough experience with shader coding to answer this. @Mathias_Westerdahl, @sven, @Andreas_Tadic, do you have any ideas?

> [@d954mas](#):
>
> I tryed web profiler but it is broken.

I’m sure it’s not broken. You are likely reading the values wrong. Post a screenshot and someone like @jakob.pogulis, @sven or @Mathias_Westerdahl can help you interpret the readout.

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [January 1, 2017, 3:18pm UTC](https://forum.defold.com/t/problem-with-shader/3808/6 "2017-01-01T15:18:25Z")

</div>

Web profiler with no light sources(pic1) game profiler show 16ms  
Web profiler with 10 light sources(pic2)

 ![](https://forum-defold-uploads.b-cdn.net/original/2X/b/b7ec597389dee16ffe58c719bda5975afb1e2c14.png)  
 ![](https://forum-defold-uploads.b-cdn.net/original/2X/c/cccbd4641a08aefb6de62c908c7c9f3697b63eb2.png)

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [January 1, 2017, 8:55pm UTC](https://forum.defold.com/t/problem-with-shader/3808/7 "2017-01-01T20:55:05Z")

</div>

Well, it’s hard to say without investigating properly.

You say you get performance back if you comment out the line below?

```lua
distance = min(distance, dst);

```

Seems strange that that line alone would affect performance, unless the output of the shader is affecting some next step in you render chain.  
Btw, I cannot see that you upload the constant `upScale`? Perhaps you do that elsewhere?

As for the performance, it’s always good to keep an eye on the number of draw calls. In your scenario with 0 lights, you have 6 draw calls, and when you have 10 lights, you have 42 draw calls. Although not ideal, it shouldn’t drop a modern PC to 100ms. A more interesting thing is that you only get 41 ms when using _no_ lights? Perhaps that’s a clue? Btw, do you know the number of draw calls in your libgdx example?

Can I ask what your settings are for your game.project: `update_frequency` and `variable_dt` ?

Since your libgdx example ran on the same machine without problems, I really think it should have run fine in Defold too. 😕

Tomorrow, more people will be back to work, and hopefully we can figure out something new about this.

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [January 1, 2017, 9:48pm UTC](https://forum.defold.com/t/problem-with-shader/3808/8 "2017-01-01T21:48:34Z")

</div>

Thx, for your reply.

> [@Mathias\_Westerdahl](#):
>
> Btw, I cannot see that you upload the constant upScale? Perhaps you do that elsewhere?

Now I changed uniform to const value 1.It not used for now, but i want use it in future.

> [@Mathias\_Westerdahl](#):
>
> A more interesting thing is that you only get 41 ms when using no lights? Perhaps that’s a clue?  
> [/quote] I think web profiler is wrong. Profiler in game said 16ms per frame.
> 
> > [@Mathias\_Westerdahl](#):
> >
> > Btw, do you know the number of draw calls in your libgdx example?
> 
> 8 draw calls with one light.[quote=“Mathias\_Westerdahl, post:7, topic:3808”]  
> Can I ask what your settings are for your game.project: update\_frequency and variable\_dt ?

update\_frequency 60  
variable\_dt not checked

> [@Mathias\_Westerdahl](#):
>
> Since your libgdx example ran on the same machine without problems, I really think it should have run fine in Defold too.

I do not understand it too.Because all work happened in shader.  
I hope for your help, because i have no idea why it happened.🙂  
I upload defold/libgdx sources, and libgdx jar [here](https://yadi.sk/d/OBtMqo3q36PWAa).

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [January 4, 2017, 7:57am UTC](https://forum.defold.com/t/problem-with-shader/3808/9 "2017-01-04T07:57:30Z")

</div>

@Mathias_Westerdahl any idea? 🤔

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [January 4, 2017, 12:50pm UTC](https://forum.defold.com/t/problem-with-shader/3808/10 "2017-01-04T12:50:37Z")

</div>

No, nothing new I’m sorry to say 😕

Have you tested running one of the tutorials? Do they run at full framerate? (\<16ms/frame)

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [January 4, 2017, 1:15pm UTC](https://forum.defold.com/t/problem-with-shader/3808/11 "2017-01-04T13:15:02Z")

</div>

the web profiler said 42ms=( . But in game profiler said 16ms, and it feels like 60 fps.

 ![](https://forum-defold-uploads.b-cdn.net/original/2X/5/541addfa68ca4820afe08c9183b82c965415b55c.png)

---

<div class="post-metadata">

**Author:** ![sven](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sven/32/1086_2.png) [@sven](https://forum.defold.com/u/sven)\
**Post date:** [January 4, 2017, 1:56pm UTC](https://forum.defold.com/t/problem-with-shader/3808/12 "2017-01-04T13:56:10Z")

</div>

What happens if you set `variable_dt` to true in your `game.project` file? What will each profiler report?

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [January 4, 2017, 3:57pm UTC](https://forum.defold.com/t/problem-with-shader/3808/13 "2017-01-04T15:57:42Z")

</div>

![](https://forum-defold-uploads.b-cdn.net/original/2X/3/3a2c207dc2e5cd4710150fe53f61733305a1f41d.png)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 4, 2017, 6:05pm UTC](https://forum.defold.com/t/problem-with-shader/3808/14 "2017-01-04T18:05:31Z")

</div>

I tried the code you shared on my Macbook Pro. I added 9 lightsources and fired up the web profiler:

 ![](https://forum-defold-uploads.b-cdn.net/original/2X/e/e2c02b5b4d049f354c6cc4d688da4eb172ccb8c4.png)

I’m not really sure what this is an indication of though… 🙂 Some issue with the shader on certain GPUs? In that case why is it working well with libgdx? @Mathias_Westerdahl, @sven, @Ragnar_Svensson, any idea?

---

<div class="post-metadata">

**Author:** ![sven](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sven/32/1086_2.png) [@sven](https://forum.defold.com/u/sven)\
**Post date:** [January 4, 2017, 6:43pm UTC](https://forum.defold.com/t/problem-with-shader/3808/15 "2017-01-04T18:43:14Z")

</div>

Would you mind giving this a try?

- Create a new script file; `glversion.script`
- Replace its content with:

```Lua
if not ffi then
    ffi = package.preload.ffi()
end
ffi.cdef[[
const char* glGetString(unsigned int);
]]
print("OpenGL Version: " .. ffi.string(ffi.C.glGetString(7938)))

```

- Attach the script to one of your gameobjects and run the game.
- Hopefully the “Console” panel in the editor should print something along the lines of: `DEBUG:SCRIPT: OpenGL Version: 2.1 INTEL-10.22.25`

This should determine what version of the OpenGL context the engine has created. I’m not sure if it will be relevant information, but couldn’t hurt to try.

(I haven’t tried this on Windows, but I think it should work.)

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [January 4, 2017, 7:11pm UTC](https://forum.defold.com/t/problem-with-shader/3808/16 "2017-01-04T19:11:28Z")

</div>

ERROR:SCRIPT: glversion.script:7: cannot resolve symbol ‘glGetString’

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [January 4, 2017, 7:20pm UTC](https://forum.defold.com/t/problem-with-shader/3808/17 "2017-01-04T19:20:22Z")

</div>

![](https://forum-defold-uploads.b-cdn.net/original/2X/5/50171d9bc569b3bcbf717f05d1617837f9d0319c.png)

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [January 4, 2017, 10:08pm UTC](https://forum.defold.com/t/problem-with-shader/3808/18 "2017-01-04T22:08:27Z")

</div>

Does it behave better/worse/same in fullscreen mode? (Idea from [Win 10 vsync issue](https://support.microsoft.com/en-us/kb/3158621))

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [January 4, 2017, 10:24pm UTC](https://forum.defold.com/t/problem-with-shader/3808/19 "2017-01-04T22:24:57Z")

</div>

same. But in fullscreen mode alt+tab not work.(the screen became black, and nothing happened)

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [January 4, 2017, 10:58pm UTC](https://forum.defold.com/t/problem-with-shader/3808/20 "2017-01-04T22:58:42Z")

</div>

Are there any vsync options available in the os or per app? (See [this thread](https://forum.defold.com/t/why-is-the-update-speed-too-fast-def-1785/1635/11) for some vsync issues)

Next idea I have is to try the app at another Windows machine, and also on another Windows version… 😕

[Next page](https://forum.defold.com/t/problem-with-shader/3808.md?page=2)
