/*
 * Copyright LWJGL. All rights reserved.
 * License terms: https://www.lwjgl.org/license
 */
package org.lwjgl.util.shaderc;

import org.testng.annotations.*;

import java.nio.*;
import java.util.*;

import static org.lwjgl.util.shaderc.Shaderc.*;
import static org.lwjgl.util.shaderc.ShadercSpvc.*;
import static org.testng.Assert.*;

@Test
public class ShadercSpvcTest {

    private static final String PROGRAM =
        "#version 450 core\n" +
        "layout(location = 0) in highp vec4 vtxColor;" +
        "layout(location = 0) out highp vec4 outColor;" +
        "void main() {" +
        "  outColor = vtxColor;" +
        "}";

    public void testBasicCompile() {
        // shaderc

        long compiler = shaderc_compiler_initialize();
        long options  = shaderc_compile_options_initialize();

        long result = shaderc_compile_into_spv(
            compiler,
            PROGRAM,
            shaderc_glsl_fragment_shader,
            "a.glsl",
            "main",
            options
        );

        shaderc_compile_options_release(options);
        shaderc_compiler_release(compiler);

        assertEquals(shaderc_result_get_compilation_status(result), shaderc_compilation_status_success);

        ByteBuffer bb = Objects.requireNonNull(shaderc_result_get_bytes(result));

        // shaderc_spvc

        long spvc_result = shaderc_spvc_result_create();

        long spvc_context = shaderc_spvc_context_create();
        long spvc_options = shaderc_spvc_compile_options_create();

        assertEquals(shaderc_spvc_initialize_for_glsl(spvc_context, bb.asIntBuffer(), spvc_options), shaderc_spvc_status_success);
        assertEquals(shaderc_spvc_compile_shader(spvc_context, spvc_result), shaderc_spvc_status_success);

        shaderc_spvc_compile_options_destroy(spvc_options);
        shaderc_spvc_context_destroy(spvc_context);

        shaderc_result_release(result);

        String glsl = Objects.requireNonNull(shaderc_spvc_result_get_string_output(spvc_result));

        assertTrue(glsl.contains("#version 450"));
        assertTrue(glsl.contains("layout(location = 0) in vec4 vtxColor"));
        assertTrue(glsl.contains("layout(location = 0) out vec4 outColor"));
        assertTrue(glsl.contains("outColor = vtxColor"));

        shaderc_spvc_result_destroy(spvc_result);
    }

}

@@ -122,16 +122,63 @@
 aderc;%0A%0A
+import org.lwjgl.*;%0Aimport org.lwjgl.system.*;%0A
 import o
@@ -239,24 +239,70 @@
 va.util.*;%0A%0A
+import static org.lwjgl.system.MemoryStack.*;%0A
 import stati
@@ -1591,32 +1591,96 @@
 _options_create(
+shaderc_spvc_spv_env_opengl_4_5, shaderc_spvc_spv_env_opengl_4_5
 );%0A%0A        asse
@@ -2083,86 +2083,273 @@
 glsl
- = Objects.requireNonNull(shaderc_spvc_result_get_string_output(spvc_result));
+;%0A        try (MemoryStack stack = stackPush()) %7B%0A            PointerBuffer pp = stack.callocPointer(1);%0A            assertEquals(shaderc_spvc_result_get_string_output(spvc_result, pp), shaderc_spvc_status_success);%0A            glsl = pp.getStringASCII();%0A        %7D
 %0A%0A  

migrate shaderc spvc test