org.lwjgl.system.MemoryUtil Java Examples

The following examples show how to use org.lwjgl.system.MemoryUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example #2
Source File: Hud.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public void init(Window window) throws Exception {
    this.vg = window.getOptions().antialiasing ? nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES) : nvgCreate(NVG_STENCIL_STROKES);
    if (this.vg == NULL) {
        throw new Exception("Could not init nanovg");
    }

    fontBuffer = Utils.ioResourceToByteBuffer("/fonts/OpenSans-Bold.ttf", 150 * 1024);
    int font = nvgCreateFontMem(vg, FONT_NAME, fontBuffer, 0);
    if (font == -1) {
        throw new Exception("Could not add font");
    }
    colour = NVGColor.create();

    posx = MemoryUtil.memAllocDouble(1);
    posy = MemoryUtil.memAllocDouble(1);

    counter = 0;
}
 
Example #3
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example #4
Source File: Hud.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public void init(Window window) throws Exception {
    this.vg = window.getOptions().antialiasing ? nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES) : nvgCreate(NVG_STENCIL_STROKES);
    if (this.vg == NULL) {
        throw new Exception("Could not init nanovg");
    }

    fontBuffer = Utils.ioResourceToByteBuffer("/fonts/OpenSans-Bold.ttf", 150 * 1024);
    int font = nvgCreateFontMem(vg, FONT_NAME, fontBuffer, 0);
    if (font == -1) {
        throw new Exception("Could not add font");
    }
    colour = NVGColor.create();

    posx = MemoryUtil.memAllocDouble(1);
    posy = MemoryUtil.memAllocDouble(1);

    counter = 0;
}
 
Example #5
Source File: Hud.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public void init(Window window) throws Exception {
    this.vg = window.getOptions().antialiasing ? nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES) : nvgCreate(NVG_STENCIL_STROKES);
    if (this.vg == NULL) {
        throw new Exception("Could not init nanovg");
    }

    fontBuffer = Utils.ioResourceToByteBuffer("/fonts/OpenSans-Bold.ttf", 150 * 1024);
    int font = nvgCreateFontMem(vg, FONT_NAME, fontBuffer, 0);
    if (font == -1) {
        throw new Exception("Could not add font");
    }
    colour = NVGColor.create();

    posx = MemoryUtil.memAllocDouble(1);
    posy = MemoryUtil.memAllocDouble(1);

    counter = 0;
}
 
Example #6
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example #7
Source File: LWJGLBufferAllocator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void destroyDirectBuffer(final Buffer buffer) {

    final long address = getAddress(buffer);

    if (address == -1) {
        LOGGER.warning("Not found address of the " + buffer);
        return;
    }

    // disable deallocator
    final Deallocator deallocator = DEALLOCATORS.remove(address);

    if (deallocator == null) {
        LOGGER.warning("Not found a deallocator for address " + address);
        return;
    }

    deallocator.setAddress(null);

    MemoryUtil.memFree(buffer);
}
 
Example #8
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example #9
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example #10
Source File: Context.java    From LWJGUI with MIT License 6 votes vote down vote up
public void dispose() {
	for (ByteBuffer buf : fontBuffers) {
		MemoryUtil.memFree(buf);
	}
	for (Image image : loadedImages) {
		image.dispose();
	}
	loadedFonts.clear();
	fontBuffers.clear();
	loadedImages.clear();
	currentSheets.clear();
	if (this.isModernOpenGL()) {
		NanoVGGL3.nvgDelete(nvgContext);
	} else {
		NanoVGGL2.nvgDelete(nvgContext);
	}
}
 
Example #11
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example #12
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example #13
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example #14
Source File: InstancedMesh.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
@Override
public void cleanUp() {
    super.cleanUp();
    if (this.instanceDataBuffer != null) {
        MemoryUtil.memFree(this.instanceDataBuffer);
        this.instanceDataBuffer = null;
    }
}
 
Example #15
Source File: OpenGLStars.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void init() {
	// Set the error handling code: all GLFW errors will be printed to the system error stream (just like println)
	GLFWErrorCallback.createPrint(System.err).set();
       glfwSetErrorCallback(errorCallback);

       // Initialize GLFW:
       if (!glfwInit())
           throw new IllegalStateException("GLFW initialization failed");

       // Configure the GLFW window
       windowID = glfwCreateWindow(
               640, 480,   // Width and height of the drawing canvas in pixels
               "Test",     // Title of the window
               MemoryUtil.NULL, // Monitor ID to use for fullscreen mode, or NULL to use windowed mode (LWJGL JavaDoc)
               MemoryUtil.NULL); // Window to share resources with, or NULL to not share resources (LWJGL JavaDoc)

       if (windowID == MemoryUtil.NULL)
           throw new IllegalStateException("GLFW window creation failed");

       glfwMakeContextCurrent(windowID); // Links the OpenGL context of the window to the current thread (GLFW_NO_CURRENT_CONTEXT error)
       glfwSwapInterval(1); // Enable VSync, which effective caps the frame-rate of the application to 60 frames-per-second
       glfwShowWindow(windowID);
	// If you don't add this line, you'll get the following exception:
       //  java.lang.IllegalStateException: There is no OpenGL context current in the current thread.
    	glfwMakeContextCurrent(windowID);
    // This line is critical for LWJGL's interoperation with GLFW's
       // OpenGL context, or any context that is managed externally.
       // LWJGL detects the context that is current in the current thread,
       // creates the GLCapabilities instance and makes the OpenGL
       // bindings available for use.
    	GL.createCapabilities();
    	
    // Set the clear color
    	glClearColor(0.5f, 0.5f, 0.5f, 1f);
    	setUpMatrices();
       
}
 
Example #16
Source File: APIBuffer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Ensures space for the specified string encoded in UTF-16, encodes the
 * string at the allocated offset and returns that offset.
 */
public int stringParamUTF16(CharSequence value, boolean nullTerminated) {
    if (value == null) {
        return -1;
    }

    int offset = bufferParam((value.length() + (nullTerminated ? 1 : 0)) << 1);
    MemoryUtil.memUTF16(value, nullTerminated, buffer, offset);
    return offset;
}
 
Example #17
Source File: Hud.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void cleanup() {
    nvgDelete(vg);
    if (posx != null) {
        MemoryUtil.memFree(posx);
    }
    if (posy != null) {
        MemoryUtil.memFree(posy);
    }
}
 
Example #18
Source File: LWJGLBufferAllocator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ByteBuffer allocate(final int size) {
    final Long address = MemoryUtil.nmemAlloc(size);
    final ByteBuffer byteBuffer = MemoryUtil.memByteBuffer(address, size);
    DEALLOCATORS.put(address, createDeallocator(address, byteBuffer));
    return byteBuffer;
}
 
Example #19
Source File: APIBuffer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * UTF8 encodes the specified strings and ensures space for two additional
 * buffers filled with the lengths and memory addresses of the encoded
 * strings, respectively. The lengths are 4-bytes integers and the memory
 * address buffer starts immediately after the lengths buffer.
 *
 * <p>
 * The encoded buffers must be later freed with
 * {@link #pointerArrayFree(int, int)}.</p>
 *
 * @return the offset to the lengths buffer
 */
public int pointerArrayParamUTF8i(CharSequence... strings) {
    int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
    int lengthsAddress = bufferParam(strings.length << 2);

    for (int i = 0; i < strings.length; i++) {
        ByteBuffer buffer = MemoryUtil.memUTF8(strings[i]);

        pointerParam(buffersAddress, i, memAddress(buffer));
        intParam(lengthsAddress, i, buffer.remaining());
    }

    return buffersAddress;
}
 
Example #20
Source File: PlatformX11VKCanvas.java    From lwjgl3-awt with MIT License 5 votes vote down vote up
@Override
public long create(Canvas canvas, VKData data) throws AWTException {
    MemoryStack stack = MemoryStack.stackGet();
    int ptr = stack.getPointer();
    JAWTDrawingSurface ds = JAWT_GetDrawingSurface(canvas, awt.GetDrawingSurface());
    try {
        int lock = JAWT_DrawingSurface_Lock(ds, ds.Lock());
        if ((lock & JAWT_LOCK_ERROR) != 0)
            throw new AWTException("JAWT_DrawingSurface_Lock() failed");
        try {
            JAWTDrawingSurfaceInfo dsi = JAWT_DrawingSurface_GetDrawingSurfaceInfo(ds, ds.GetDrawingSurfaceInfo());
            try {
                JAWTX11DrawingSurfaceInfo dsiX11 = JAWTX11DrawingSurfaceInfo.create(dsi.platformInfo());
                long display = dsiX11.display();
                long window = dsiX11.drawable();

                VkXlibSurfaceCreateInfoKHR sci = VkXlibSurfaceCreateInfoKHR.callocStack(stack)
                        .sType(VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR)
                        .dpy(display)
                        .window(window);

                long surfaceAddr = stack.nmalloc(8, 8);
                int err = nvkCreateXlibSurfaceKHR(data.instance, sci.address(), 0L, surfaceAddr);
                long surface = MemoryUtil.memGetLong(surfaceAddr);
                stack.setPointer(ptr);
                if (err != VK_SUCCESS) {
                    throw new AWTException("Calling vkCreateXlibSurfaceKHR failed with error: " + err);
                }

                return surface;
            } finally {
                JAWT_DrawingSurface_FreeDrawingSurfaceInfo(dsi, ds.FreeDrawingSurfaceInfo());
            }
        } finally {
            JAWT_DrawingSurface_Unlock(ds, ds.Unlock());
        }
    } finally {
        JAWT_FreeDrawingSurface(ds, awt.FreeDrawingSurface());
    }
}
 
Example #21
Source File: Hud.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void cleanup() {
    nvgDelete(vg);
    if (posx != null) {
        MemoryUtil.memFree(posx);
    }
    if (posy != null) {
        MemoryUtil.memFree(posy);
    }
}
 
Example #22
Source File: Hud.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void cleanup() {
    nvgDelete(vg);
    if (posx != null) {
        MemoryUtil.memFree(posx);
    }
    if (posy != null) {
        MemoryUtil.memFree(posy);
    }
}
 
Example #23
Source File: GlfwMouseInputVR.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void setNativeCursor(JmeCursor jmeCursor) {
    if (jmeCursor != null) {
        Long glfwCursor = jmeToGlfwCursorMap.get(jmeCursor);

        if (glfwCursor == null) {
            glfwCursor = createGlfwCursor(jmeCursor);
            jmeToGlfwCursorMap.put(jmeCursor, glfwCursor);
        }

        glfwSetCursor(context.getWindowHandle(), glfwCursor);
    } else {
        glfwSetCursor(context.getWindowHandle(), MemoryUtil.NULL);
    }
}
 
Example #24
Source File: SimpleDemo.java    From lwjgl3-swt with MIT License 5 votes vote down vote up
/**
 * Create a Vulkan instance using LWJGL 3.
 * 
 * @return the VkInstance handle
 */
private static VkInstance createInstance() {
    VkApplicationInfo appInfo = VkApplicationInfo.calloc()
            .sType(VK_STRUCTURE_TYPE_APPLICATION_INFO)
            .pApplicationName(memUTF8("SWT Vulkan Demo"))
            .pEngineName(memUTF8(""))
            .apiVersion(VK_MAKE_VERSION(1, 0, 2));
    ByteBuffer VK_KHR_SURFACE_EXTENSION = memUTF8(VK_KHR_SURFACE_EXTENSION_NAME);
    ByteBuffer VK_KHR_OS_SURFACE_EXTENSION;
    if (Platform.get() == Platform.WINDOWS)
        VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
    else
        VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
    PointerBuffer ppEnabledExtensionNames = memAllocPointer(2);
    ppEnabledExtensionNames.put(VK_KHR_SURFACE_EXTENSION);
    ppEnabledExtensionNames.put(VK_KHR_OS_SURFACE_EXTENSION);
    ppEnabledExtensionNames.flip();
    VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo.calloc()
            .sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO)
            .pNext(0L)
            .pApplicationInfo(appInfo);
    if (ppEnabledExtensionNames.remaining() > 0) {
        pCreateInfo.ppEnabledExtensionNames(ppEnabledExtensionNames);
    }
    PointerBuffer pInstance = MemoryUtil.memAllocPointer(1);
    int err = vkCreateInstance(pCreateInfo, null, pInstance);
    if (err != VK_SUCCESS) {
        throw new RuntimeException("Failed to create VkInstance: " + translateVulkanResult(err));
    }
    long instance = pInstance.get(0);
    memFree(pInstance);
    VkInstance ret = new VkInstance(instance, pCreateInfo);
    memFree(ppEnabledExtensionNames);
    memFree(VK_KHR_OS_SURFACE_EXTENSION);
    memFree(VK_KHR_SURFACE_EXTENSION);
    appInfo.free();
    return ret;
}
 
Example #25
Source File: InstancedMesh.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
@Override
public void cleanUp() {
    super.cleanUp();
    if (this.modelViewBuffer != null) {
        MemoryUtil.memFree(this.modelViewBuffer);
        this.modelViewBuffer = null;
    }
    if (this.modelLightViewBuffer != null) {
        MemoryUtil.memFree(this.modelLightViewBuffer);
        this.modelLightViewBuffer = null;
    }
}
 
Example #26
Source File: InstancedMesh.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) {
    super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0));

    this.numInstances = numInstances;

    glBindVertexArray(vaoId);

    // Model View Matrix
    modelViewVBO = glGenBuffers();
    vboIdList.add(modelViewVBO);
    this.modelViewBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.MATRIX_SIZE_FLOATS);
    glBindBuffer(GL_ARRAY_BUFFER, modelViewVBO);
    int start = 5;
    for (int i = 0; i < 4; i++) {
        glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.MATRIX_SIZE_BYTES, i * InstancedMesh.VECTOR4F_SIZE_BYTES);
        glVertexAttribDivisor(start, 1);
        glEnableVertexAttribArray(start);
        start++;
    }

    // Light view matrix
    modelLightViewVBO = glGenBuffers();
    vboIdList.add(modelLightViewVBO);
    this.modelLightViewBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.MATRIX_SIZE_FLOATS);
    glBindBuffer(GL_ARRAY_BUFFER, modelLightViewVBO);
    for (int i = 0; i < 4; i++) {
        glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.MATRIX_SIZE_BYTES, i * InstancedMesh.VECTOR4F_SIZE_BYTES);
        glVertexAttribDivisor(start, 1);
        glEnableVertexAttribArray(start);
        start++;
    }

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}
 
Example #27
Source File: SimpleDemo.java    From lwjgl3-awt with MIT License 5 votes vote down vote up
/**
 * Create a Vulkan instance using LWJGL 3.
 * 
 * @return the VkInstance handle
 */
private static VkInstance createInstance() {
    VkApplicationInfo appInfo = VkApplicationInfo.calloc()
            .sType(VK_STRUCTURE_TYPE_APPLICATION_INFO)
            .pApplicationName(memUTF8("AWT Vulkan Demo"))
            .pEngineName(memUTF8(""))
            .apiVersion(VK_MAKE_VERSION(1, 0, 2));
    ByteBuffer VK_KHR_SURFACE_EXTENSION = memUTF8(VK_KHR_SURFACE_EXTENSION_NAME);
    ByteBuffer VK_KHR_OS_SURFACE_EXTENSION;
    if (Platform.get() == Platform.WINDOWS)
        VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
    else
        VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
    PointerBuffer ppEnabledExtensionNames = memAllocPointer(2);
    ppEnabledExtensionNames.put(VK_KHR_SURFACE_EXTENSION);
    ppEnabledExtensionNames.put(VK_KHR_OS_SURFACE_EXTENSION);
    ppEnabledExtensionNames.flip();
    VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo.calloc()
            .sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO)
            .pNext(0L)
            .pApplicationInfo(appInfo);
    if (ppEnabledExtensionNames.remaining() > 0) {
        pCreateInfo.ppEnabledExtensionNames(ppEnabledExtensionNames);
    }
    PointerBuffer pInstance = MemoryUtil.memAllocPointer(1);
    int err = vkCreateInstance(pCreateInfo, null, pInstance);
    if (err != VK_SUCCESS) {
        throw new RuntimeException("Failed to create VkInstance: " + translateVulkanResult(err));
    }
    long instance = pInstance.get(0);
    memFree(pInstance);
    VkInstance ret = new VkInstance(instance, pCreateInfo);
    memFree(ppEnabledExtensionNames);
    memFree(VK_KHR_OS_SURFACE_EXTENSION);
    memFree(VK_KHR_SURFACE_EXTENSION);
    appInfo.free();
    return ret;
}
 
Example #28
Source File: APIBuffer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * UTF16 encodes the specified strings and ensures space for two additional
 * buffers filled with the lengths and memory addresses of the encoded
 * strings, respectively. The lengths are 4-bytes integers and the memory
 * address buffer starts immediately after the lengths buffer.
 *
 * <p>
 * The encoded buffers must be later freed with
 * {@link #pointerArrayFree(int, int)}.</p>
 *
 * @return the offset to the lengths buffer
 */
public int pointerArrayParamUTF16i(CharSequence... strings) {
    int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
    int lengthsAddress = bufferParam(strings.length << 2);

    for (int i = 0; i < strings.length; i++) {
        ByteBuffer buffer = MemoryUtil.memUTF16(strings[i]);

        pointerParam(buffersAddress, i, memAddress(buffer));
        intParam(lengthsAddress, i, buffer.remaining());
    }

    return buffersAddress;
}
 
Example #29
Source File: APIBuffer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Ensures space for the specified string encoded in UTF-8, encodes the
 * string at the allocated offset and returns that offset.
 */
public int stringParamUTF8(CharSequence value, boolean nullTerminated) {
    if (value == null) {
        return -1;
    }

    int encodedLen = MemoryUtil.memLengthUTF8(value, nullTerminated);
    int offset = bufferParam(encodedLen);
    MemoryUtil.memUTF8(value, nullTerminated, buffer, offset);
    return offset;
}
 
Example #30
Source File: PlatformWin32GLCanvas.java    From lwjgl3-swt with MIT License 5 votes vote down vote up
private void wglNvSwapGroupAndBarrier(GLData attribs, long bufferAddr, long hDC) throws SWTException {
    int success;
    long wglQueryMaxSwapGroupsNVAddr = WGL.wglGetProcAddress("wglQueryMaxSwapGroupsNV");
    success = JNI.callPPPI(hDC, bufferAddr, bufferAddr + 4, wglQueryMaxSwapGroupsNVAddr);
    int maxGroups = MemoryUtil.memGetInt(bufferAddr);
    if (maxGroups < attribs.swapGroupNV) {
        throw new SWTException("Swap group exceeds maximum group index");
    }
    int maxBarriers = MemoryUtil.memGetInt(bufferAddr + 4);
    if (maxBarriers < attribs.swapBarrierNV) {
        throw new SWTException("Swap barrier exceeds maximum barrier index");
    }
    if (attribs.swapGroupNV > 0) {
        long wglJoinSwapGroupNVAddr = WGL.wglGetProcAddress("wglJoinSwapGroupNV");
        if (wglJoinSwapGroupNVAddr == 0L) {
            throw new SWTException("WGL_NV_swap_group available but wglJoinSwapGroupNV is NULL");
        }
        success = JNI.callPI(hDC, attribs.swapGroupNV, wglJoinSwapGroupNVAddr);
        if (success == 0) {
            throw new SWTException("Failed to join swap group");
        }
        if (attribs.swapBarrierNV > 0) {
            long wglBindSwapBarrierNVAddr = WGL.wglGetProcAddress("wglBindSwapBarrierNV");
            if (wglBindSwapBarrierNVAddr == 0L) {
                throw new SWTException("WGL_NV_swap_group available but wglBindSwapBarrierNV is NULL");
            }
            success = JNI.callI(attribs.swapGroupNV, attribs.swapBarrierNV, wglBindSwapBarrierNVAddr);
            if (success == 0) {
                throw new SWTException("Failed to bind swap barrier. Probably no G-Sync card installed.");
            }
        }
    }
}