<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://jogamp.org/bugzilla/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.2"
          urlbase="https://jogamp.org/bugzilla/"
          
          maintainer="sgothel@jausoft.com"
>

    <bug>
          <bug_id>1488</bug_id>
          
          <creation_ts>2024-01-22 02:36:55 +0100</creation_ts>
          <short_desc>Graph: Resolve Performance Regression in RegionRenderer&apos;s ShaderKey Utilization</short_desc>
          <delta_ts>2024-01-25 09:22:19 +0100</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>3</classification_id>
          <classification>JogAmp</classification>
          <product>Jogl</product>
          <component>graph</component>
          <version>2.6.0</version>
          <rep_platform>All</rep_platform>
          <op_sys>all</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P1</priority>
          <bug_severity>critical</bug_severity>
          <target_milestone>---</target_milestone>
          
          <blocked>805</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="Sven Gothel">sgothel</reporter>
          <assigned_to name="Sven Gothel">sgothel</assigned_to>
          <cc>sgothel</cc>
          
          <cf_type>DEFECT</cf_type>
          <cf_scm_refs>ffe4e670c9d35121934c6f3c95067d9c18aee386
6cf158cc59e901b49ab54681e363d23492421a9d
1dcfdf71c09c6d774ac47012c05e09da4a085d7b
95e2519a0fc37d19d05bc0f4cc14f593eaae7dd9
b711ae5239b8581a197d468b2804cfeb8c4d6c94</cf_scm_refs>
          <cf_workaround>---</cf_workaround>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>7049</commentid>
    <comment_count>0</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2024-01-22 02:36:55 +0100</bug_when>
    <thetext>RegionRenderer&apos;s ShaderKey

Commit 6363ae5fb6975a6f2e7c1093ce81f25b699e3e61 changed 
RegionRenderer.useShaderProgram()&apos;s shader mapping using a new ShaderKey instance.

Such ShaderKey instance is created every time @ RegionRenderer.useShaderProgram()
to retrieve the ShaderProgram from the HashMap&lt;ShaderKey, Shader Program&gt;.
While this is most correct, creating the ShaderKey instance causes 
a lot of temp objects. 

ShaderKey also uses the optional colorTexSeq shader code for equality test
in case of hash-collisions.

Previous code simply ignored hash-collisions and used a 1:1 hashCode -&gt; ShaderProgram mapping using our IntObjectHashMap. 
However, there was no test whether collision occur. 

+++

Solution would be either
1- Revert fully to the previous code just using an IntObjectHashMap,
   but throwing a RuntimeException in case of hashCode collisions.
   In case of a collisions, we would need to produce a better hashCode.
   This is possible, as the underlying data is fully internal .. etc.

2- Use the IntObjectHashMap as long there is no hashCode collision,
   then revert back to HashMap&lt;ShaderKey, Shader Program&gt;.

I favor variant (1), if it is possible to validate the hashCode.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>7054</commentid>
    <comment_count>1</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2024-01-22 05:57:05 +0100</bug_when>
    <thetext>commit ffe4e670c9d35121934c6f3c95067d9c18aee386
implements solution 1, i.e. throws exception on shader hash-code collision.

Code supplies both variants, however, 
after already having used IntObjectHashMap w/o actual 
ShaderKey artifacts we have to think about whether fallback 
is even possible.
Otherwise .. in case we have no internal collision, this should be good enough.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>7058</commentid>
    <comment_count>2</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2024-01-22 06:45:04 +0100</bug_when>
    <thetext>commit 6cf158cc59e901b49ab54681e363d23492421a9d (HEAD -&gt; master)
    Bug 1488 - Graph RegionRenderer: Ensure shaderPrograms1 path is disabled using &apos;static final boolean useShaderPrograms0 = true&apos;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>7060</commentid>
    <comment_count>3</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2024-01-22 08:39:05 +0100</bug_when>
    <thetext>Closing for now - may need to be reopened in case 
commit ffe4e670c9d35121934c6f3c95067d9c18aee386
is not sufficient, see comment 1</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>7061</commentid>
    <comment_count>4</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2024-01-22 22:03:21 +0100</bug_when>
    <thetext>commit 1dcfdf71c09c6d774ac47012c05e09da4a085d7b
RegionRenderer: Use a more deterministic 64-bit shaderKey: 
[ 0-31] bit values and state, 
[32-63] colorTexSeqHash

This leaves only room for a key collision on the 32-bit colorTexSeqHash value
and hence should be save within our shader-code environment.

+        //  # |  s |
+        //  0 |  1 | isTwoPass
+        //  1 |  1 | pass1
+        //  2 |  5 | ShaderModeSelector1
+        //  7 |  1 | hasFrustumClipping
+        //  8 |  1 | hasColorChannel
+        //  9 |  1 | hasColorTexture
+        // 32 | 32 | colorTexSeqHash
+        long hash =             ( isTwoPass ? 1 : 0 );
+        hash = ( hash &lt;&lt; 1 ) | ( pass1 ? 1 : 0 ) ;
+        hash = ( hash &lt;&lt; 1 ) | sms.ordinal(); // incl. pass2Quality + sampleCount
+        hash = ( hash &lt;&lt; 5 ) | ( hasFrustumClipping ? 1 : 0 );
+        hash = ( hash &lt;&lt; 1 ) | ( hasColorChannel ? 1 : 0 );
+        hash = ( hash &lt;&lt; 1 ) | ( hasColorTexture ? 1 : 0 );
+        hash = ( hash &lt;&lt; 1 ) | ( ( colorTexSeqHash &amp; 0xFFFFFFL ) &lt;&lt; 32 );</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>7062</commentid>
    <comment_count>5</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2024-01-22 22:05:22 +0100</bug_when>
    <thetext>commit 95e2519a0fc37d19d05bc0f4cc14f593eaae7dd9
FFMPEGMediaPlayer: Fix getTextureFragmentShaderHashID(), i.e. use actual tc_w_1 = (float)getWidth() / (float)texWidth value as hardcoded within the shader</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>7065</commentid>
    <comment_count>6</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2024-01-25 09:22:19 +0100</bug_when>
    <thetext>(In reply to Sven Gothel from comment #4)
commit b711ae5239b8581a197d468b2804cfeb8c4d6c94
Bug 1488: Complete/Fix producing the 64-bit shaderKey: Use long values in bit-shift expressions and simplify it

commit 1dcfdf71c09c6d774ac47012c05e09da4a085d7b 
- still used the &apos;hash code&apos; bit shift pattern, not necessary -&gt; simplified
- the value as not ensured to be long, hence conversion occured

This caused Graph&apos;s MSAA not being picked up properly using the shaderKey.

-        long hash =             ( isTwoPass ? 1 : 0 );
-        hash = ( hash &lt;&lt; 1 ) | ( pass1 ? 1 : 0 ) ;
-        hash = ( hash &lt;&lt; 1 ) | sms.ordinal(); // incl. pass2Quality + sampleCount
-        hash = ( hash &lt;&lt; 5 ) | ( hasFrustumClipping ? 1 : 0 );
-        hash = ( hash &lt;&lt; 1 ) | ( hasColorChannel ? 1 : 0 );
-        hash = ( hash &lt;&lt; 1 ) | ( hasColorTexture ? 1 : 0 );
-        hash = ( hash &lt;&lt; 1 ) | ( ( colorTexSeqHash &amp; 0xFFFFFFL ) &lt;&lt; 32 );
+        long hash =  isTwoPass ? 1L : 0L;
+        hash |= ( pass1 ? 1L : 0L )               &lt;&lt; 1;
+        hash |= (long)( sms.ordinal() &amp; 0b11111 ) &lt;&lt; 2; // incl. pass2Quality + sampleCount
+        hash |= ( hasFrustumClipping ? 1L : 0L )  &lt;&lt; 7;
+        hash |= ( hasColorChannel ? 1L : 0L )     &lt;&lt; 8;
+        hash |= ( hasColorTexture ? 1L : 0L )     &lt;&lt; 9;
+        hash |= ( colorTexSeqHash &amp; 0xFFFFFFL )   &lt;&lt; 32;</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>