@@ -7,7 +7,6 @@ import com.jogamp.opengl.util.FPSAnimator
7
7
import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil
8
8
import graphics.scenery.*
9
9
import graphics.scenery.backends.*
10
- import graphics.scenery.backends.ShaderCompilationException
11
10
import graphics.scenery.controls.TrackerInput
12
11
import graphics.scenery.fonts.SDFFontAtlas
13
12
import graphics.scenery.spirvcrossj.Loader
@@ -122,9 +121,11 @@ class OpenGLRenderer(hub: Hub,
122
121
var initialized = false
123
122
private set
124
123
124
+ private var pboBuffers: Array <ByteBuffer ?> = arrayOf(null , null )
125
+ private var pboBufferAvailable = arrayOf(true , true )
125
126
@Volatile private var pbos: IntArray = intArrayOf(0 , 0 )
126
127
private var readIndex = 0
127
- private var updateIndex = 0
128
+ private var updateIndex = 1
128
129
129
130
private var renderConfig: RenderConfigReader .RenderConfig
130
131
override var renderConfigFile = " "
@@ -1265,7 +1266,7 @@ class OpenGLRenderer(hub: Hub,
1265
1266
}
1266
1267
}
1267
1268
1268
- if (! n.metadata.containsKey(" OpenGLRenderer" )) {
1269
+ if (! n.metadata.containsKey(" OpenGLRenderer" ) || ! n.initialized ) {
1269
1270
n.metadata.put(" OpenGLRenderer" , OpenGLObjectState ())
1270
1271
initializeNode(n)
1271
1272
return @nonInstancedDrawing
@@ -1540,7 +1541,7 @@ class OpenGLRenderer(hub: Hub,
1540
1541
}
1541
1542
1542
1543
readIndex = (readIndex + 1 ) % 2
1543
- updateIndex = (readIndex + 1 ) % 2
1544
+ updateIndex = (updateIndex + 1 ) % 2
1544
1545
1545
1546
if (pbos[0 ] == 0 || pbos[1 ] == 0 || mustRecreateFramebuffers) {
1546
1547
gl.glGenBuffers(2 , pbos, 0 )
@@ -1552,21 +1553,42 @@ class OpenGLRenderer(hub: Hub,
1552
1553
gl.glBufferData(GL4 .GL_PIXEL_PACK_BUFFER , window.width * window.height * 4L , null , GL4 .GL_STREAM_READ )
1553
1554
1554
1555
gl.glBindBuffer(GL4 .GL_PIXEL_PACK_BUFFER , 0 )
1556
+
1557
+ if (pboBuffers[0 ] != null ) {
1558
+ MemoryUtil .memFree(pboBuffers[0 ])
1559
+ }
1560
+
1561
+ if (pboBuffers[1 ] != null ) {
1562
+ MemoryUtil .memFree(pboBuffers[1 ])
1563
+ }
1564
+
1565
+ pboBuffers[0 ] = null
1566
+ pboBuffers[1 ] = null
1555
1567
}
1556
1568
1557
- gl.glBindBuffer(GL4 .GL_PIXEL_PACK_BUFFER , pbos[readIndex])
1569
+ if (pboBuffers[0 ] == null ) {
1570
+ pboBuffers[0 ] = MemoryUtil .memAlloc(4 * window.width* window.height)
1571
+ }
1558
1572
1559
- gl.glReadBuffer(GL4 .GL_FRONT )
1560
- gl.glReadPixels(0 , 0 , window.width, window.height, GL4 .GL_BGRA , GL4 .GL_UNSIGNED_BYTE , 0 )
1573
+ if (pboBuffers[1 ] == null ) {
1574
+ pboBuffers[1 ] = MemoryUtil .memAlloc(4 * window.width* window.height)
1575
+ }
1561
1576
1562
1577
gl.glBindBuffer(GL4 .GL_PIXEL_PACK_BUFFER , pbos[updateIndex])
1563
1578
1564
- val buffer = gl.glMapBuffer(GL4 .GL_PIXEL_PACK_BUFFER , GL4 .GL_READ_ONLY )
1565
- if (buffer != null ) {
1579
+ gl.glReadBuffer(GL4 .GL_BACK )
1580
+ gl.glReadPixels(0 , 0 , window.width, window.height, GL4 .GL_BGRA , GL4 .GL_UNSIGNED_BYTE , 0 )
1581
+
1582
+ gl.glGetBufferSubData(GL4 .GL_PIXEL_PACK_BUFFER , 0 ,
1583
+ 4L * window.width * window.height, pboBuffers[updateIndex])
1584
+
1585
+ if (! mustRecreateFramebuffers) {
1566
1586
Platform .runLater {
1567
- if (! mustRecreateFramebuffers) embedPanel.update(buffer)
1587
+ pboBuffers[readIndex]?.let {
1588
+ val id = viewportPass.output.values.first().getTextureId(" Viewport" )
1589
+ embedPanel.update(it, id = id)
1590
+ }
1568
1591
}
1569
- gl.glUnmapBuffer(GL4 .GL_PIXEL_PACK_BUFFER )
1570
1592
}
1571
1593
1572
1594
gl.glBindBuffer(GL4 .GL_PIXEL_PACK_BUFFER , 0 )
@@ -1633,6 +1655,10 @@ class OpenGLRenderer(hub: Hub,
1633
1655
* @return True if the initialisation went alright, False if it failed.
1634
1656
*/
1635
1657
fun initializeNode (node : Node ): Boolean {
1658
+ if (! node.lock.tryLock(2 , TimeUnit .MILLISECONDS )) {
1659
+ return false
1660
+ }
1661
+
1636
1662
val s: OpenGLObjectState
1637
1663
1638
1664
if (node.instanceOf == null ) {
@@ -1696,21 +1722,17 @@ class OpenGLRenderer(hub: Hub,
1696
1722
}
1697
1723
1698
1724
if (node is HasGeometry ) {
1699
- node.lock.tryLock(100 , TimeUnit .MILLISECONDS )
1700
- if (node.lock.tryLock()) {
1701
- setVerticesAndCreateBufferForNode(node)
1702
- setNormalsAndCreateBufferForNode(node)
1725
+ setVerticesAndCreateBufferForNode(node)
1726
+ setNormalsAndCreateBufferForNode(node)
1703
1727
1704
- if (node.texcoords.limit() > 0 ) {
1705
- setTextureCoordsAndCreateBufferForNode(node)
1706
- }
1707
-
1708
- if (node.indices.limit() > 0 ) {
1709
- setIndicesAndCreateBufferForNode(node)
1710
- }
1728
+ if (node.texcoords.limit() > 0 ) {
1729
+ setTextureCoordsAndCreateBufferForNode(node)
1730
+ }
1711
1731
1712
- node.lock.unlock()
1732
+ if (node.indices.limit() > 0 ) {
1733
+ setIndicesAndCreateBufferForNode(node)
1713
1734
}
1735
+
1714
1736
}
1715
1737
1716
1738
val matricesUbo = OpenGLUBO (backingBuffer = buffers[" UBOBuffer" ])
@@ -1726,35 +1748,13 @@ class OpenGLRenderer(hub: Hub,
1726
1748
s.UBOs .put(" Matrices" , this )
1727
1749
}
1728
1750
1729
-
1730
1751
loadTexturesForNode(node, s)
1731
1752
1732
1753
val materialUbo = OpenGLUBO (backingBuffer = buffers[" UBOBuffer" ])
1733
- var materialType = 0
1734
-
1735
- if (node.material.textures.containsKey(" ambient" ) && ! s.defaultTexturesFor.contains(" ambient" )) {
1736
- materialType = materialType or MATERIAL_HAS_AMBIENT
1737
- }
1738
-
1739
- if (node.material.textures.containsKey(" diffuse" ) && ! s.defaultTexturesFor.contains(" diffuse" )) {
1740
- materialType = materialType or MATERIAL_HAS_DIFFUSE
1741
- }
1742
-
1743
- if (node.material.textures.containsKey(" specular" ) && ! s.defaultTexturesFor.contains(" specular" )) {
1744
- materialType = materialType or MATERIAL_HAS_SPECULAR
1745
- }
1746
-
1747
- if (node.material.textures.containsKey(" normal" ) && ! s.defaultTexturesFor.contains(" normal" )) {
1748
- materialType = materialType or MATERIAL_HAS_NORMAL
1749
- }
1750
-
1751
- if (node.material.textures.containsKey(" alphamask" ) && ! s.defaultTexturesFor.contains(" alphamask" )) {
1752
- materialType = materialType or MATERIAL_HAS_ALPHAMASK
1753
- }
1754
1754
1755
1755
with (materialUbo) {
1756
1756
name = " MaterialProperties"
1757
- add(" materialType" , { materialType })
1757
+ add(" materialType" , { node.materialToMaterialType() })
1758
1758
add(" Ka" , { node.material.ambient })
1759
1759
add(" Kd" , { node.material.diffuse })
1760
1760
add(" Ks" , { node.material.specular })
@@ -1785,9 +1785,44 @@ class OpenGLRenderer(hub: Hub,
1785
1785
node.metadata[this .javaClass.simpleName] = s
1786
1786
1787
1787
s.initialized = true
1788
+ node.lock.unlock()
1788
1789
return true
1789
1790
}
1790
1791
1792
+ private fun Node.materialToMaterialType (): Int {
1793
+ var materialType = 0
1794
+ val s = this .metadata[" OpenGLRenderer" ] as ? OpenGLObjectState ? : return 0
1795
+
1796
+ s.defaultTexturesFor.clear()
1797
+ arrayOf(" ambient" , " diffuse" , " specular" , " normal" , " alphamask" , " displacement" ).forEach {
1798
+ if (! s.textures.containsKey(it)) {
1799
+ s.defaultTexturesFor.add(it)
1800
+ }
1801
+ }
1802
+
1803
+ if (this .material.textures.containsKey(" ambient" ) && ! s.defaultTexturesFor.contains(" ambient" )) {
1804
+ materialType = materialType or MATERIAL_HAS_AMBIENT
1805
+ }
1806
+
1807
+ if (this .material.textures.containsKey(" diffuse" ) && ! s.defaultTexturesFor.contains(" diffuse" )) {
1808
+ materialType = materialType or MATERIAL_HAS_DIFFUSE
1809
+ }
1810
+
1811
+ if (this .material.textures.containsKey(" specular" ) && ! s.defaultTexturesFor.contains(" specular" )) {
1812
+ materialType = materialType or MATERIAL_HAS_SPECULAR
1813
+ }
1814
+
1815
+ if (this .material.textures.containsKey(" normal" ) && ! s.defaultTexturesFor.contains(" normal" )) {
1816
+ materialType = materialType or MATERIAL_HAS_NORMAL
1817
+ }
1818
+
1819
+ if (this .material.textures.containsKey(" alphamask" ) && ! s.defaultTexturesFor.contains(" alphamask" )) {
1820
+ materialType = materialType or MATERIAL_HAS_ALPHAMASK
1821
+ }
1822
+
1823
+ return materialType
1824
+ }
1825
+
1791
1826
/* *
1792
1827
* Parallel forEach implementation for HashMaps.
1793
1828
*
@@ -1908,13 +1943,6 @@ class OpenGLRenderer(hub: Hub,
1908
1943
}
1909
1944
}
1910
1945
1911
- arrayOf(" ambient" , " diffuse" , " specular" , " normal" , " alphamask" , " displacement" ).forEach {
1912
- if (! s.textures.containsKey(it)) {
1913
- // s.textures.putIfAbsent(it, textureCache["DefaultTexture"]!!)
1914
- s.defaultTexturesFor.add(it)
1915
- }
1916
- }
1917
-
1918
1946
node.material.needsTextureReload = false
1919
1947
s.initialized = true
1920
1948
node.lock.unlock()
0 commit comments