<commit_before>package imgui.impl.gl

import glm_.L
import glm_.f
import glm_.glm
import glm_.i
import glm_.vec2.Vec2
import glm_.vec4.Vec4ub
import gln.checkError
import gln.glGetVec4i
import gln.glScissor
import gln.glViewport
import gln.glf.semantic
import gln.identifiers.GlBuffers
import gln.identifiers.GlProgram
import gln.identifiers.GlVertexArray
import gln.uniform.glUniform
import gln.vertexArray.glVertexAttribPointer
import imgui.BackendFlag
import imgui.DEBUG
import imgui.ImGui.io
import imgui.impl.mat
import imgui.internal.DrawData
import imgui.internal.DrawIdx
import imgui.internal.DrawVert
import imgui.or
import kool.*
import org.lwjgl.opengl.GL
import org.lwjgl.opengl.GL20C
import org.lwjgl.opengl.GL30C.*
import org.lwjgl.opengl.GL32C.glDrawElementsBaseVertex
import org.lwjgl.opengl.GL33C
import org.lwjgl.opengl.GL33C.glBindSampler
import org.lwjgl.opengl.GL45C.GL_CLIP_ORIGIN
import org.lwjgl.system.Platform
import org.lwjgl.system.MemoryStack

class ImplGL3 : GLInterface {

    var program = GlProgram(0)
    var matUL = -1

    val buffers = GlBuffers<Buffer>()
    var vao = GlVertexArray()

    init {

        // query for GL version
        glVersion = when {
            !OPENGL_ES2 -> glGetInteger(GL_MAJOR_VERSION) * 1000 + glGetInteger(GL_MINOR_VERSION)
            else -> 2000 // GLES 2
        }

        // Setup back-end capabilities flags
        io.backendRendererName = "imgui_impl_opengl3"

        if (MAY_HAVE_DRAW_WITH_BASE_VERTEX)
            if (glVersion >= 3200)
                io.backendFlags = io.backendFlags or BackendFlag.RendererHasVtxOffset  // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
    }

    override fun shutdown() = destroyDeviceObjects()

    override fun newFrame() {
        if (program.isInvalid)
            createDeviceObjects()
    }

    fun setupRenderState(drawData: DrawData, fbWidth: Int, fbHeight: Int) {

        // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled
        glEnable(GL_BLEND)
        glBlendEquation(GL_FUNC_ADD)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glDisable(GL_CULL_FACE)
        glDisable(GL_DEPTH_TEST)
        glEnable(GL_SCISSOR_TEST)
        if (POLYGON_MODE)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

        // Setup viewport, orthographic projection matrix
        // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
        // DisplayPos is (0,0) for single viewport apps.
        glViewport(0, 0, fbWidth, fbHeight)
        // TODO re-sync -> mat from drawData
        val orthoProjection = glm.ortho(0f, io.displaySize.x.f, io.displaySize.y.f, 0f, mat)
        glUseProgram(program.name)
        glUniform(matUL, orthoProjection)
        if (SAMPLER_BINDING)
            glBindSampler(0, 0) // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise.

        vao.bind()

        // Bind vertex/index buffers and setup attributes for ImDrawVert
        glBindBuffer(GL_ARRAY_BUFFER, buffers[Buffer.Vertex].name)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Buffer.Element].name)
        glEnableVertexAttribArray(semantic.attr.POSITION)
        glEnableVertexAttribArray(semantic.attr.TEX_COORD)
        glEnableVertexAttribArray(semantic.attr.COLOR)
        glVertexAttribPointer(semantic.attr.POSITION, Vec2.length, GL_FLOAT, false, DrawVert.size, 0)
        glVertexAttribPointer(semantic.attr.TEX_COORD, Vec2.length, GL_FLOAT, false, DrawVert.size, Vec2.size)
        glVertexAttribPointer(semantic.attr.COLOR, Vec4ub.length, GL_UNSIGNED_BYTE, true, DrawVert.size, Vec2.size * 2)
    }

    /** OpenGL3 Render function.
     *  (this used to be set in io.renderDrawListsFn and called by ImGui::render(), but you can now call this directly
     *  from your main loop)
     *  Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL
     *  state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.   */
    override fun renderDrawData(drawData: DrawData) {

        // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
        val fbWidth = (drawData.displaySize.x * drawData.framebufferScale.x).i
        val fbHeight = (drawData.displaySize.y * drawData.framebufferScale.y).i
        if (fbWidth == 0 || fbHeight == 0) return

        // Backup GL state
        val lastActiveTexture = glGetInteger(GL_ACTIVE_TEXTURE)
        glActiveTexture(GL_TEXTURE0 + semantic.sampler.DIFFUSE)
        val lastProgram = glGetInteger(GL_CURRENT_PROGRAM)
        val lastTexture = glGetInteger(GL_TEXTURE_BINDING_2D)
        val lastSampler = when {
            SAMPLER_BINDING -> glGetInteger(GL33C.GL_SAMPLER_BINDING)
            else -> 0
        }
        val lastArrayBuffer = glGetInteger(GL_ARRAY_BUFFER_BINDING)
        val lastVertexArray = glGetInteger(GL_VERTEX_ARRAY_BINDING)
        val lastElementBuffer = glGetInteger(GL_ELEMENT_ARRAY_BUFFER_BINDING)
         val lastPolygonMode = when {
            POLYGON_MODE -> glGetInteger(GL_POLYGON_MODE)
            POLYGON_MODE -> {
                val stack = MemoryStack.stackGet()
                val sp = stack.getPointer()
                val params = stack.callocInt(2);
                glGetIntegerv(GL_POLYGON_MODE, params)
                val tmp = params.get(0)
                stack.setPointer(sp)
                tmp
            }
            else -> 0
        }<commit_msg>using put instead of = to avoid sharing of instances.<commit_after>package imgui.impl.gl

import glm_.L
import glm_.f
import glm_.glm
import glm_.i
import glm_.vec2.Vec2
import glm_.vec4.Vec4ub
import gln.checkError
import gln.glGetVec4i
import gln.glScissor
import gln.glViewport
import gln.glf.semantic
import gln.identifiers.GlBuffers
import gln.identifiers.GlProgram
import gln.identifiers.GlVertexArray
import gln.uniform.glUniform
import gln.vertexArray.glVertexAttribPointer
import imgui.BackendFlag
import imgui.DEBUG
import imgui.ImGui.io
import imgui.impl.mat
import imgui.internal.DrawData
import imgui.internal.DrawIdx
import imgui.internal.DrawVert
import imgui.or
import kool.*
import org.lwjgl.opengl.GL
import org.lwjgl.opengl.GL20C
import org.lwjgl.opengl.GL30C.*
import org.lwjgl.opengl.GL32C.glDrawElementsBaseVertex
import org.lwjgl.opengl.GL33C
import org.lwjgl.opengl.GL33C.glBindSampler
import org.lwjgl.opengl.GL45C.GL_CLIP_ORIGIN
import org.lwjgl.system.Platform
import org.lwjgl.system.MemoryStack

class ImplGL3 : GLInterface {

    var program = GlProgram(0)
    var matUL = -1

    val buffers = GlBuffers<Buffer>()
    var vao = GlVertexArray()

    init {

        // query for GL version
        glVersion = when {
            !OPENGL_ES2 -> glGetInteger(GL_MAJOR_VERSION) * 1000 + glGetInteger(GL_MINOR_VERSION)
            else -> 2000 // GLES 2
        }

        // Setup back-end capabilities flags
        io.backendRendererName = "imgui_impl_opengl3"

        if (MAY_HAVE_DRAW_WITH_BASE_VERTEX)
            if (glVersion >= 3200)
                io.backendFlags = io.backendFlags or BackendFlag.RendererHasVtxOffset  // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
    }

    override fun shutdown() = destroyDeviceObjects()

    override fun newFrame() {
        if (program.isInvalid)
            createDeviceObjects()
    }

    fun setupRenderState(drawData: DrawData, fbWidth: Int, fbHeight: Int) {

        // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled
        glEnable(GL_BLEND)
        glBlendEquation(GL_FUNC_ADD)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glDisable(GL_CULL_FACE)
        glDisable(GL_DEPTH_TEST)
        glEnable(GL_SCISSOR_TEST)
        if (POLYGON_MODE)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

        // Setup viewport, orthographic projection matrix
        // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
        // DisplayPos is (0,0) for single viewport apps.
        glViewport(0, 0, fbWidth, fbHeight)
        // TODO re-sync -> mat from drawData
        val orthoProjection = glm.ortho(0f, io.displaySize.x.f, io.displaySize.y.f, 0f, mat)
        glUseProgram(program.name)
        glUniform(matUL, orthoProjection)
        if (SAMPLER_BINDING)
            glBindSampler(0, 0) // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise.

        vao.bind()

        // Bind vertex/index buffers and setup attributes for ImDrawVert
        glBindBuffer(GL_ARRAY_BUFFER, buffers[Buffer.Vertex].name)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Buffer.Element].name)
        glEnableVertexAttribArray(semantic.attr.POSITION)
        glEnableVertexAttribArray(semantic.attr.TEX_COORD)
        glEnableVertexAttribArray(semantic.attr.COLOR)
        glVertexAttribPointer(semantic.attr.POSITION, Vec2.length, GL_FLOAT, false, DrawVert.size, 0)
        glVertexAttribPointer(semantic.attr.TEX_COORD, Vec2.length, GL_FLOAT, false, DrawVert.size, Vec2.size)
        glVertexAttribPointer(semantic.attr.COLOR, Vec4ub.length, GL_UNSIGNED_BYTE, true, DrawVert.size, Vec2.size * 2)
    }

    /** OpenGL3 Render function.
     *  (this used to be set in io.renderDrawListsFn and called by ImGui::render(), but you can now call this directly
     *  from your main loop)
     *  Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL
     *  state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.   */
    override fun renderDrawData(drawData: DrawData) {

        // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
        val fbWidth = (drawData.displaySize.x * drawData.framebufferScale.x).i
        val fbHeight = (drawData.displaySize.y * drawData.framebufferScale.y).i
        if (fbWidth == 0 || fbHeight == 0) return

        // Backup GL state
        val lastActiveTexture = glGetInteger(GL_ACTIVE_TEXTURE)
        glActiveTexture(GL_TEXTURE0 + semantic.sampler.DIFFUSE)
        val lastProgram = glGetInteger(GL_CURRENT_PROGRAM)
        val lastTexture = glGetInteger(GL_TEXTURE_BINDING_2D)
        val lastSampler = when {
            SAMPLER_BINDING -> glGetInteger(GL33C.GL_SAMPLER_BINDING)
            else -> 0
        }
        val lastArrayBuffer = glGetInteger(GL_ARRAY_BUFFER_BINDING)
        val lastVertexArray = glGetInteger(GL_VERTEX_ARRAY_BINDING)
        val lastElementBuffer = glGetInteger(GL_ELEMENT_ARRAY_BUFFER_BINDING)
         val lastPolygonMode = when {
            POLYGON_MODE -> {
                val stack = MemoryStack.stackGet()
                val sp = stack.getPointer()
                val params = stack.callocInt(2);
                glGetIntegerv(GL_POLYGON_MODE, params)
                val tmp = params.get(0)
                stack.setPointer(sp)
                tmp
            }
            else -> 0
        }
{'fraction_non_alphanumeric': 0.07873736478098954, 'fraction_numerical': 0.011172193651356624, 'mean_word_length': 2.9423278573925202, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3219635', 'n_tokens_mistral': 3681, 'n_tokens_neox': 3540, 'n_words': 812}