Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:49:47+00:00 2026-06-05T18:49:47+00:00

I have read that GLSL (specifically v1.0.17: my application is running under WebGL) compilers

  • 0

I have read that GLSL (specifically v1.0.17: my application is running under WebGL) compilers will optimize away redundant assignments such as:

gl_FragCoord = ProjectionMatrix * ModelViewMatrix * VertexPosition;
. . .
gl_FragCoord = ProjectionMatrix * ModelViewMatrix * VertexPosition;

Is the compiler also smart enough to perform the same optimization across function calls? For example:

void doSomething1(void) {
  . . .
  gl_FragCoord = ProjectionMatrix * ModelViewMatrix * VertexPosition;
}

void doSomething2(void) {
  . . .
  gl_FragCoord = ProjectionMatrix * ModelViewMatrix * VertexPosition;
}

void main(void) {
  doSomething1();
  doSomething2();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:49:49+00:00Added an answer on June 5, 2026 at 6:49 pm

    I downloaded the GPU ShaderAnalyzer from AMD and fed the following GLSL program into it:

    uniform mat4 ModelViewMatrix;
    attribute vec4 VertexPosition;
    
    void doSomething1(void) {
      gl_Position = ModelViewMatrix * VertexPosition;
    }
    
    void doSomething2(void) {
      gl_Position = ModelViewMatrix * VertexPosition;
    }
    
    void main(void) {
      doSomething1();
      doSomething2();
    }
    

    This produced the following disassembly (or equivalent) on every card from Radeon HD 2400 to Radeon HD 6970:

    ; --------  Disassembly --------------------
    00 CALL_FS NO_BARRIER 
    01 ALU: ADDR(32) CNT(16) KCACHE0(CB0:0-15) 
          0  x: MUL         ____,  R1.w,  KC0[3].w      
             y: MUL         ____,  R1.w,  KC0[3].z      
             z: MUL         ____,  R1.w,  KC0[3].y      
             w: MUL         ____,  R1.w,  KC0[3].x      
          1  x: MULADD      R127.x,  R1.z,  KC0[2].w,  PV0.x      
             y: MULADD      R127.y,  R1.z,  KC0[2].z,  PV0.y      
             z: MULADD      R127.z,  R1.z,  KC0[2].y,  PV0.z      
             w: MULADD      R127.w,  R1.z,  KC0[2].x,  PV0.w      
          2  x: MULADD      R127.x,  R1.y,  KC0[1].w,  PV1.x      
             y: MULADD      R127.y,  R1.y,  KC0[1].z,  PV1.y      
             z: MULADD      R127.z,  R1.y,  KC0[1].y,  PV1.z      
             w: MULADD      R127.w,  R1.y,  KC0[1].x,  PV1.w      
          3  x: MULADD      R1.x,  R1.x,  KC0[0].x,  PV2.w      
             y: MULADD      R1.y,  R1.x,  KC0[0].y,  PV2.z      
             z: MULADD      R1.z,  R1.x,  KC0[0].z,  PV2.y      
             w: MULADD      R1.w,  R1.x,  KC0[0].w,  PV2.x      
    02 EXP_DONE: POS0, R1
    03 EXP_DONE: PARAM0, R0.____
    04 ALU: ADDR(48) CNT(1) 
          4  x: NOP         ____      
    05 NOP NO_BARRIER 
    END_OF_PROGRAM
    

    Then I commented out the doSomething2() function and its call in the main method. The result was exactly the same: every shader in AMD’s tool optimized out the redundant math. So the answer to this question is yes: in the general case, GLSL compilers will be smart enough to perform this optimization, with the caveat that @nicol-bolas pointed out in his comment: compiler optimizations are specific to each compiler, and there is no 100% guarantee that this will be true for all compilers. The safest bet is, of course, to perform such optimizations yourself whenever possible — but it’s nice to know that this is the case when for whatever reason you can’t.

    UPDATE: I compiled the same program, with and without commenting out the second function call, under Cg (one of NVIDIA’s compilers), and in both cases it produced the following:

    mul r0, v0.y, c1
    mad r0, v0.x, c0, r0
    mad r0, v0.z, c2, r0
    mad oPos, v0.w, c3, r0
    

    So yes, NVIDIA optimizes it too — or at least, the Cg compiler does. I found claims that Cg-compiled code runs on Intel GPUs, but this is outside the realm of my expertise, so take that for what it is.

    If anyone wants to add test cases to this, feel free, but at this point I feel the question has been answered suitably.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read that Thread.sleep() will pause the currently running thread for the time
I have read that passing -c when running rails generate will add the new
I have read that silverlight application will be downloaded as a XAP file on
I have read that dealloc for an object will be called, only if retain
I have read that calloc (malloc+init) will sometimes fail to init array with zero
I have read that the following code snippet will result in a compiler error
I have read that recursive implementation of QuickSort will require O(log n) of additional
I have read that Win32 will not allow remote invocation of a process that
I have read that copying the data directory will work. But, that is a
I have read that PHP is obsoleting the MySql functions. How will we connect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.