question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pragma unroll_loop does not work with nested code blocks

See original GitHub issue

When unrolling a loop that has a block statement in it the shader breaks because it considers the first } encountered to be closing the for loop. For example this works:

#pragma unroll_loop
for ( int i = 0; i < 6; i ++ ) {

    if ( i < NUM_DIR_LIGHT_SHADOWS ) directLight.color *= getShadow( /* ... */ );

}

But this breaks:

#pragma unroll_loop
for ( int i = 0; i < 6; i ++ ) {

    if ( i < NUM_DIR_LIGHT_SHADOWS ) {

        directLight.color *= getShadow( /* ... */ );

    }

}

The problematic regex is here:

https://github.com/mrdoob/three.js/blob/dev/src/renderers/webgl/WebGLProgram.js#L224

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Mugen87commented, Feb 27, 2020

Also, as far as I remember, it was mainly for early iPads but I wonder if iOS handles this better these days…

According to #13090, certain Macbooks with radeon cards required the loop unrool, too.

I see so is the consensus that it’s not generally needed, then?

From my point of view, it is required until the opposite can be demonstrated with a few tests. I would not blindly remove it and risk shadow breakage on user devices.

1reaction
mrdoobcommented, Mar 4, 2020

which should be just as fast as the current approach:

#unroll_loop
for ( int i = 0; i < 5; i ++ ) {

 // nested blocks for days

}
#end_unroll_loop

That seems interesting!

I prefer this syntax:

#unroll_loop_start
for ( int i = 0; i < 5; i ++ ) {

  // nested blocks for days

}
#unroll_loop_end
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to unroll nested for loops in c++? - Stack Overflow
I found ways to unroll it without any code (and conditions) between nested 'for' loops using template metaprogramming, but that did not help...
Read more >
30201 – gcc doesn't unroll nested loops
While developing a Free C++ library, I am facing what I think is a bug in gcc: it doesn't unroll nested loops.
Read more >
HLS LOOP UNROLL in nested loops doesn't seem to help
I tried reading the generated Verilog code, but it doesn't make much sense with ... I have the same problem with unrolling the...
Read more >
Optimization Guide — SmartHLS 2022.2.1 documentation
This example shows a nested loop, which performs an element-wise multiplication of two 2-dimensional arrays and accumulates the sum. The inner loop is...
Read more >
lib/Transforms/Scalar/LoopUnrollPass.cpp Source File - LLVM
105 cl::desc("Don't allow loop unrolling to simulate more than this number of" ... 135 cl::desc("Unroll loops with run-time trip counts"));.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found