boxy icon indicating copy to clipboard operation
boxy copied to clipboard

addImage waits full frame for first call to glBindTexture

Open chancyk opened this issue 1 year ago • 0 comments

I'm not sure if this is an actual issue or if it's just the nature of OpenGL, but I happened to notice it while tracing what was taking so long when I was measuring frame draw performance. Feel free to just close this if it's a known behavior, but I thought it might be useful informational content for anyone else that runs into it.

I traced it to addImage calling updateSubImage, ultimately calling glBindTexture. It seems like the first call to glBindTexture waits a full frame (something over 16ms in my case), but then subsequent calls are sub-millisecond. Here's what I'm measuring:

window.onButtonPress = proc(button: Button) =
  pressed.add button_to_char(button)
  var t0, t1: MonoTime
  let text = pressed.join("")
  let bounds = font1.layoutBounds(text)
  let caretBounds = font1.layoutBounds("^")
  if bounds.x > 0 and bounds.y > 0:
    let textImage = newImage(bounds.x.int, bounds.y.int)
    let textImage2 = newImage(bounds.x.int, bounds.y.int)
    let caretImage = newImage(caretBounds.x.int, caretBounds.y.int)
    center = vec2(bounds.x / 2, bounds.y) + 100
    # echo "Center: ", center
    textImage.fillText(font1, text)
    textImage2.fillText(font1, text)
    caretImage.fillText(font1, "^")
    t0 = getMonotime()
    bxy.addImage("text", textImage, genMipmaps = false)
    bxy.addImage("text2", textImage2, genMipmaps = false)
    bxy.addImage("caret", caretImage, genMipmaps = false)
    t1 = getMonotime()
    draw = true

  if draw:
    echo "Elapsed: ", (t1 - t0).inMicroseconds, "us"

And here's the output:

  BindTextureID: 1  
  Boxy: 16357us 
  BindTextureID: 1  
  Boxy: 272us   
  BindTextureID: 1  
  Boxy: 73us    
Elapsed: 25511us
  BindTextureID: 1  
  Boxy: 16314us 
  BindTextureID: 1  
  Boxy: 349us   
  BindTextureID: 1  
  Boxy: 116us   
Elapsed: 24795us
  BindTextureID: 1  
  Boxy: 16575us 
  BindTextureID: 1  
  Boxy: 253us   
  BindTextureID: 1  
  Boxy: 47us    
Elapsed: 25623us

And the Boxy and BindTextureID lines are from within boxy.addImage.

chancyk avatar Oct 26 '22 22:10 chancyk