COCOS2DX writing shader encountering overflow problem

Source: Internet
Author: User

In programming languages, no matter what type of data you have, there are a variety of limitations that cannot be expressed in real-world situations. For example, int, char will overflow, float will overflow and accuracy is inaccurate.

So we need to pay special attention to these things in development.


In the near future Cocos2dx (2.1.4) engine with shader to do some effects. There are no problems with Windows, but porting to Android can be problematic.

record them. One is to deepen the impression. Second, in order to provide a similar problem to the Bo friends to participate in the test.


Mainly focused on two aspects:

(1) Shader does not support the operation of different types of numbers

Like what

int A;

float B;

Float C = a + B;

This problem is OK, because in Eclipse COCOS2DX will print the compile error log


(2) floating-point overflow problem, this problem will be more difficult to solve, I also slowly try out

First look at some of the minimum range of shader accuracy



Here are some of my shader source code:

<span style= "FONT-SIZE:18PX;" >//varying vec4        v_fragmentcolor;varying vec2        v_texcoord;uniformfloatu_radius;uniform vec2   u_ Touchpos;uniform vec2   u_bgsize;uniform sampler2d cc_texture0;float isincircle () {  vec2 pos = u_bgsize * v_ Texcoord;  float dis = distance (pos,u_touchpos);  if (dis >= U_radius | | u_radius = = 0.0) return 1.0;  Elsereturn 0.0;} void Main () {    vec4 texcolor   = texture2d (cc_texture0, V_texcoord); float isIn = Isincircle (); Gl_fragcolor    =  Texcolor * ISIN;} </span>

Error concentration in float dis = distance (pos,u_touchpos);

Distance is the distance between two points in the screen. I expect it to be in the form of roughly this

Float distance (vec2 pos1, vec2 pos2) {

VEC3 sub = Pos1-pos2;

return sqrt (sub.x * sub.x + sub.y * sub.y);

}

Because COCOS2DX defaults to the vertex shader using high-precision float, the fragment shader uses a medium-precision float, which is the code of the fragment shader.

So the float range is between 16384 and 16384, and when two hundred numbers multiply it is very likely to cause overflow.


Here's the code for the last change:

<span style= "FONT-SIZE:18PX;" >//varying vec4        v_fragmentcolor;varying vec2        v_texcoord;uniform floatu_radius;uniform highp vec2 u_ Touchpos;uniform vec2u_bgsize;uniform sampler2d cc_texture0;float isincircle () {  highp vec2 pos = u_bgSize * v_ Texcoord;  float dis = distance (pos,u_touchpos);  if (dis >= u_radius) return 1.0;  Elsereturn 0.0;} void Main () {    vec4 texcolor   = texture2d (cc_texture0, V_texcoord); float isIn = Isincircle (); Gl_fragcolor    =  Texcolor * ISIN;} </span>





COCOS2DX writing shader encountering overflow problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.