LWJGL / lwjgl3

LWJGL is a Java library that enables cross-platform access to popular native APIs useful in the development of graphics (OpenGL, Vulkan, bgfx), audio (OpenAL, Opus), parallel computing (OpenCL, CUDA) and XR (OpenVR, LibOVR, OpenXR) applications.
https://www.lwjgl.org
BSD 3-Clause "New" or "Revised" License
4.67k stars 629 forks source link

How should i use KTX creation? #889

Closed danila-schelkov closed 1 year ago

danila-schelkov commented 1 year ago

Question

PointerBuffer causes the process crash and i can't do anything. My crashlog file: hs_err_pid11848.log

KTX binary file as TXT to avoid github restriction

That's how i'm actually creating the texture from stream:

        try {
            FileInputStream fileInputStream = new FileInputStream("test.txt");
            bytes = fileInputStream.readAllBytes();
            fileInputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        ktxStream ktxStream = new ktxStream(ByteBuffer.wrap(bytes));

        PointerBuffer newTex = PointerBuffer.allocateDirect(1);
        int status = KTX.ktxTexture_CreateFromStream(ktxStream, KTX.KTX_TEXTURE_CREATE_NO_FLAGS, newTex);

Some of console output when crashes:

[LWJGL] Version: 3.3.2+13
     OS: Windows 10 v10.0
    JRE: Windows amd64 16.0.2
    JVM: OpenJDK 64-Bit Server VM v16.0.2+7 by Eclipse Foundation
[LWJGL] Loading JNI library: lwjgl
    Module: org.lwjgl
    Loaded from org.lwjgl.librarypath: C:\Users\Admin\AppData\Local\Temp\lwjgl_Admin\3.3.2+13\x64\lwjgl.dll
[LWJGL] Loading library: ktx
    Module: org.lwjgl.ktx
    Loaded from org.lwjgl.librarypath: C:\Users\Admin\AppData\Local\Temp\lwjgl_Admin\3.3.2+13\x64\ktx.dll
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffb922f1c0e, pid=11848, tid=2368
#
# JRE version: OpenJDK Runtime Environment Temurin-16.0.2+7 (16.0.2+7) (build 16.0.2+7)
# Java VM: OpenJDK 64-Bit Server VM Temurin-16.0.2+7 (16.0.2+7, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64)
# Problematic frame:
# C  0x00007ffb922f1c0e
#
# No core dump will be written. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\Projects\Java\sc-editor\hs_err_pid11848.log
#
# If you would like to submit a bug report, please visit:
#   https://github.com/adoptium/adoptium-support/issues
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
danila-schelkov commented 1 year ago

Following command produces a normal texture PVRTexToolCLI -noout -i "%%x" -f r8g8b8a8,UBN,sRGB -d "%%x".png test ktx

NekoCaffeine commented 1 year ago

You are using the wrong method. Please try the following code, which I have tested.

ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length).put(bytes).rewind();
PointerBuffer newTex = PointerBuffer.allocateDirect(1);
int status = KTX.ktxTexture_CreateFromMemory(buffer, KTX.KTX_TEXTURE_CREATE_NO_FLAGS, newTex);
danila-schelkov commented 1 year ago

Thanks! That works for me. But what i need to do further to add this texture to the opengl or get texture bytes

NekoCaffeine commented 1 year ago

See also: ktxTexture_GetData

danila-schelkov commented 1 year ago

I've been trying for this, but i don't know how to convert PointerBuffer to actually ktxTexture object

pnkov commented 1 year ago

Pointer buffer contains the address of a struct. So you just need to call ktxTexture.create(newTex.get())

danila-schelkov commented 1 year ago

Thank you! But I still need to get image pixels as ByteBuffer or byte array. Is it possible to do it?

danila-schelkov commented 1 year ago

Oh, I found the best way to load KTX texture to the OpenGL.

Just parse ktx buffer as it described in specs and load each level manually.