Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.39 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2018, Adam <[email protected]>
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright notice, this
  9.  *    list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  11.  *    this list of conditions and the following disclaimer in the documentation
  12.  *    and/or other materials provided with the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  */
  25. package org.empyrean.client.graphics.opengl;
  26.  
  27. import com.jogamp.nativewindow.AbstractGraphicsConfiguration;
  28. import com.jogamp.nativewindow.NativeWindowFactory;
  29. import com.jogamp.nativewindow.awt.AWTGraphicsConfiguration;
  30. import com.jogamp.nativewindow.awt.JAWTWindow;
  31. import com.jogamp.opengl.GL4;
  32. import com.jogamp.opengl.GLCapabilities;
  33. import com.jogamp.opengl.GLContext;
  34. import com.jogamp.opengl.GLDrawable;
  35. import com.jogamp.opengl.GLDrawableFactory;
  36. import com.jogamp.opengl.GLProfile;
  37. import java.awt.Canvas;
  38. import java.util.function.Function;
  39. import javax.swing.JFrame;
  40.  
  41. import static org.empyrean.client.graphics.opengl.GLUtil.inputStreamToString;
  42. import static org.junit.Assert.fail;
  43.  
  44. import org.empyrean.client.graphics.opengl.template.Template;
  45. import org.junit.Before;
  46. import org.junit.Ignore;
  47. import org.junit.Test;
  48.  
  49. public class ShaderTest
  50. {
  51.     private static final String VERTEX_SHADER = "" +
  52.         "void main() {" +
  53.         "       gl_Position = vec4(1.0, 1.0, 1.0, 1.0);" +
  54.         "}";
  55.     private GL4 gl;
  56.  
  57.     @Before
  58.     public void before()
  59.     {
  60.         Canvas canvas = new Canvas();
  61.         JFrame frame = new JFrame();
  62.         frame.setSize(100, 100);
  63.         frame.add(canvas);
  64.         frame.setVisible(true);
  65.  
  66.         GLProfile glProfile = GLProfile.getMaxFixedFunc(true);
  67.  
  68.         GLCapabilities glCaps = new GLCapabilities(glProfile);
  69.         AbstractGraphicsConfiguration config = AWTGraphicsConfiguration.create(canvas.getGraphicsConfiguration(),
  70.             glCaps, glCaps);
  71.  
  72.         JAWTWindow jawtWindow = (JAWTWindow) NativeWindowFactory.getNativeWindow(canvas, config);
  73.  
  74.         GLDrawableFactory glDrawableFactory = GLDrawableFactory.getFactory(glProfile);
  75.  
  76.         GLDrawable glDrawable = glDrawableFactory.createGLDrawable(jawtWindow);
  77.         glDrawable.setRealized(true);
  78.  
  79.  
  80.         GLContext glContext = glDrawable.createContext(null);
  81.         int res = glContext.makeCurrent();
  82.         if (res == GLContext.CONTEXT_NOT_CURRENT)
  83.         {
  84.             fail("error making context current");
  85.         }
  86.  
  87.         gl = glContext.getGL().getGL4();
  88.     }
  89.  
  90.     @Test
  91.     @Ignore
  92.     public void testUnordered() throws ShaderException
  93.     {
  94.         int glComputeProgram = gl.glCreateProgram();
  95.         int glComputeShader = gl.glCreateShader(gl.GL_COMPUTE_SHADER);
  96.         try
  97.         {
  98.             Function<String, String> func = (s) -> inputStreamToString(getClass().getResourceAsStream(s));
  99.             Template template = new Template(func);
  100.             String source = template.process(func.apply("comp_unordered.glsl"));
  101.  
  102.             int line = 0;
  103.             for (String str : source.split("\\n"))
  104.             {
  105.                 System.out.println(++line + " " + str);
  106.             }
  107.  
  108.             GLUtil.loadComputeShader(gl, glComputeProgram, glComputeShader, source);
  109.         }
  110.         finally
  111.         {
  112.             gl.glDeleteShader(glComputeShader);
  113.             gl.glDeleteProgram(glComputeProgram);
  114.         }
  115.     }
  116.  
  117.     @Test
  118.     @Ignore
  119.     public void testSmall() throws ShaderException
  120.     {
  121.         int glComputeProgram = gl.glCreateProgram();
  122.         int glComputeShader = gl.glCreateShader(gl.GL_COMPUTE_SHADER);
  123.         try
  124.         {
  125.             Function<String, String> func = (s) -> inputStreamToString(getClass().getResourceAsStream(s));
  126.             Template template = new Template(func);
  127.             String source = template.process(func.apply("comp_small.glsl"));
  128.  
  129.             int line = 0;
  130.             for (String str : source.split("\\n"))
  131.             {
  132.                 System.out.println(++line + " " + str);
  133.             }
  134.  
  135.             GLUtil.loadComputeShader(gl, glComputeProgram, glComputeShader, source);
  136.         }
  137.         finally
  138.         {
  139.             gl.glDeleteShader(glComputeShader);
  140.             gl.glDeleteProgram(glComputeProgram);
  141.         }
  142.     }
  143.  
  144.     @Test
  145.     @Ignore
  146.     public void testComp() throws ShaderException
  147.     {
  148.         int glComputeProgram = gl.glCreateProgram();
  149.         int glComputeShader = gl.glCreateShader(gl.GL_COMPUTE_SHADER);
  150.         try
  151.         {
  152.             Function<String, String> func = (s) -> inputStreamToString(getClass().getResourceAsStream(s));
  153.             Template template = new Template(func);
  154.             String source = template.process(func.apply("comp.glsl"));
  155.  
  156.             int line = 0;
  157.             for (String str : source.split("\\n"))
  158.             {
  159.                 System.out.println(++line + " " + str);
  160.             }
  161.  
  162.             GLUtil.loadComputeShader(gl, glComputeProgram, glComputeShader, source);
  163.         }
  164.         finally
  165.         {
  166.             gl.glDeleteShader(glComputeShader);
  167.             gl.glDeleteProgram(glComputeProgram);
  168.         }
  169.     }
  170.  
  171.     @Test
  172.     @Ignore
  173.     public void testGeom() throws ShaderException
  174.     {
  175.         int glComputeProgram = gl.glCreateProgram();
  176.         int glVertexShader = gl.glCreateShader(gl.GL_VERTEX_SHADER);
  177.         int glGeometryShader = gl.glCreateShader(gl.GL_GEOMETRY_SHADER);
  178.         int glFragmentShader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);
  179.         try
  180.         {
  181.             Function<String, String> func = (s) -> inputStreamToString(getClass().getResourceAsStream(s));
  182.             Template template = new Template(func);
  183.             String source = template.process(func.apply("geom.glsl"));
  184.  
  185.             int line = 0;
  186.             for (String str : source.split("\\n"))
  187.             {
  188.                 System.out.println(++line + " " + str);
  189.             }
  190.  
  191.             GLUtil.loadShaders(gl, glComputeProgram, glVertexShader, glGeometryShader, glFragmentShader, VERTEX_SHADER, source, "");
  192.         }
  193.         finally
  194.         {
  195.             gl.glDeleteShader(glVertexShader);
  196.             gl.glDeleteShader(glGeometryShader);
  197.             gl.glDeleteShader(glFragmentShader);
  198.             gl.glDeleteProgram(glComputeProgram);
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement