Compare commits

...

23 Commits

Author SHA1 Message Date
hYdos d6795052cc r.i.p Litecraft 2020-03-07 07:55:20 +10:00
hYdos 9a047c9750 forgot to implement cleanup 2020-03-06 20:55:07 +10:00
hYdos 0627e671ef clean up memory after model not needed 2020-03-06 20:49:56 +10:00
hYdos 5324a6c1c9 need to make uniform buffer objects model specific(view and projection matricies same just need to change model matrix per model) 2020-03-06 20:40:03 +10:00
hYdos 1b3e6e3189 [Refactor] some gl naming and stuff 2020-03-06 18:53:31 +10:00
hYdos 2b30bbdbd1 cleanup stuff 2020-03-06 17:09:47 +10:00
hYdos 69d596e887 Gingerfy part 6 (cleanup next) 2020-03-06 16:10:27 +10:00
hYdos 1f4f89e556 Gingerfy part 5 2020-03-06 16:04:06 +10:00
hYdos 0a5a706839 Gingerfy part 4 2020-03-06 15:59:06 +10:00
hYdos e2cd717875 optimise imports 2020-03-06 15:13:09 +10:00
hYdos 7b600ad802 Merge branch 'liteCraft' into Ginger2 2020-03-06 15:10:38 +10:00
hayden v 2027507db2 Gingerfy part 3 2020-03-06 14:47:37 +10:00
hayden v c3bdddba55 Gingerfy part 2 2020-03-06 14:18:31 +10:00
hayden v 79f56cf9bf Gingerfy part 1 2020-03-06 13:50:36 +10:00
hayden v 6363515d7f added util and misc classes 2020-03-06 13:33:44 +10:00
hayden v 9ecb175643 clean up stuff 2020-03-06 13:02:32 +10:00
hayden v 6d203ac01b more than 1 model support 2020-03-06 12:26:27 +10:00
hayden v 037337fff8 redo the undo 2020-03-06 10:51:52 +10:00
hayden v 64c5c57422 less embedded classes 2020-03-06 10:25:23 +10:00
hayden v 1328dd9383 Implemented VKVariables 2020-03-06 10:21:59 +10:00
hayden v 4415310b9d prep before VKConstants usage 2020-03-06 09:47:16 +10:00
hayden v 70f33accc3 external renderers(renderer outside of main class) 2020-03-06 09:24:42 +10:00
hYdos 3968dafe36 render system? 2020-03-06 08:07:31 +10:00
61 changed files with 2669 additions and 2077 deletions

View File

@ -6,20 +6,20 @@ import org.lwjgl.opengl.*;
import com.github.halotroop.litecraft.types.block.BlockInstance;
import com.github.halotroop.litecraft.world.gen.WorldGenConstants;
import com.github.hydos.ginger.engine.common.api.GingerRegister;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.elements.objects.GLRenderObject;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.common.math.Maths;
import com.github.hydos.ginger.engine.opengl.render.Renderer;
import com.github.hydos.ginger.engine.common.render.Renderer;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
import com.github.hydos.ginger.engine.opengl.render.shaders.StaticShader;
import com.github.hydos.ginger.engine.opengl.render.shaders.GLStaticShader;
import com.github.hydos.ginger.engine.opengl.utils.GLLoader;
public class BlockRenderer extends Renderer implements WorldGenConstants
public class GLBlockRenderer extends Renderer implements WorldGenConstants
{
public StaticShader shader;
public GLStaticShader shader;
public int atlasID;
public BlockRenderer(StaticShader shader, Matrix4f projectionMatrix)
public GLBlockRenderer(GLStaticShader shader, Matrix4f projectionMatrix)
{
this.shader = shader;
shader.start();
@ -28,7 +28,7 @@ public class BlockRenderer extends Renderer implements WorldGenConstants
this.atlasID = GLLoader.createBlockAtlas();
}
private void prepBlockInstance(RenderObject entity)
private void prepBlockInstance(GLRenderObject entity)
{
Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotX(),
entity.getRotY(), entity.getRotZ(), entity.getScale());
@ -79,7 +79,7 @@ public class BlockRenderer extends Renderer implements WorldGenConstants
for (BlockInstance entity : renderList) {
if (entity != null && entity.getModel() != null)
{
GLTexturedModel blockModel = entity.getModel();
GLTexturedModel blockModel = (GLTexturedModel) entity.getModel();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, blockModel.getTexture().getTextureID());
prepBlockInstance(entity);
GL11.glDrawElements(GL11.GL_TRIANGLES, blockModel.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);

View File

@ -8,7 +8,7 @@ import com.github.halotroop.litecraft.Litecraft;
import com.github.halotroop.litecraft.save.LitecraftSave;
import com.github.halotroop.litecraft.world.dimension.Dimensions;
import com.github.hydos.ginger.engine.common.api.GingerEngine;
import com.github.hydos.ginger.engine.common.elements.GuiTexture;
import com.github.hydos.ginger.engine.common.elements.GLGuiTexture;
import com.github.hydos.ginger.engine.common.elements.buttons.TextureButton;
import com.github.hydos.ginger.engine.common.font.GUIText;
import com.github.hydos.ginger.engine.common.io.Window;
@ -28,7 +28,7 @@ public class TitleScreen extends Screen
public TitleScreen()
{
elements = new ArrayList<GuiTexture>();
elements = new ArrayList<GLGuiTexture>();
playButton = ((GingerGL)engine).registerButton("/textures/guis/playbutton.png", new Vector2f(0, 0), new Vector2f(0.25f, 0.1f));
playButton.show(Litecraft.getInstance().data.guis);
debugText = ((GingerGL)engine).registerText("Loading...", 2, new Vector2f(0, 0), 1f, true, "debugInfo");

View File

@ -3,9 +3,9 @@ package com.github.halotroop.litecraft.types.block;
import org.joml.Vector3f;
import com.github.halotroop.litecraft.world.Chunk;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.elements.objects.GLRenderObject;
public class BlockInstance extends RenderObject
public class BlockInstance extends GLRenderObject
{
public BlockInstance(Block block, Vector3f position)
{ super(block.model, position, 0, 0, 0, new Vector3f(1f, 1f, 1f)); }

View File

@ -2,10 +2,10 @@ package com.github.halotroop.litecraft.types.entity;
import org.joml.Vector3f;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.elements.objects.GLRenderObject;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
public abstract class Entity extends RenderObject
public abstract class Entity extends GLRenderObject
{
public Entity(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
{ super(model, position, rotX, rotY, rotZ, scale); }

View File

@ -6,7 +6,7 @@ import java.util.function.ToIntFunction;
import org.joml.Vector3f;
import com.github.halotroop.litecraft.logic.SODSerializable;
import com.github.halotroop.litecraft.render.BlockRenderer;
import com.github.halotroop.litecraft.render.GLBlockRenderer;
import com.github.halotroop.litecraft.types.block.*;
import com.github.halotroop.litecraft.world.gen.WorldGenConstants;
@ -71,7 +71,7 @@ public class Chunk implements BlockAccess, WorldGenConstants, SODSerializable
return this.blockInstances[index(x, y, z)];
}
public void render(BlockRenderer blockRenderer)
public void render(GLBlockRenderer blockRenderer)
{
if (shouldRender)
{

View File

@ -8,13 +8,14 @@ import java.util.function.LongConsumer;
import org.joml.Vector3f;
import com.github.halotroop.litecraft.Litecraft;
import com.github.halotroop.litecraft.render.BlockRenderer;
import com.github.halotroop.litecraft.render.GLBlockRenderer;
import com.github.halotroop.litecraft.save.LitecraftSave;
import com.github.halotroop.litecraft.types.block.*;
import com.github.halotroop.litecraft.types.entity.PlayerEntity;
import com.github.halotroop.litecraft.world.dimension.Dimension;
import com.github.halotroop.litecraft.world.gen.*;
import com.github.halotroop.litecraft.world.gen.modifier.WorldModifier;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
import it.unimi.dsi.fastutil.longs.*;
@ -175,9 +176,9 @@ public class World implements BlockAccess, WorldGenConstants
public Chunk optimiseChunk(Chunk chunk)
{ return chunk; }
public void render(BlockRenderer blockRenderer)
public void render(GLBlockRenderer blockRenderer)
{
blockRenderer.prepareModel(this.dummy.getModel());
blockRenderer.prepareModel((GLTexturedModel) this.dummy.getModel());
this.chunks.forEach((pos, c) ->
{
if (c != null && c.isFullyGenerated())

View File

@ -0,0 +1,37 @@
package com.github.hydos.ginger;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
import com.github.hydos.ginger.engine.vulkan.elements.VKRenderObject;
import com.github.hydos.ginger.engine.vulkan.render.*;
import com.github.hydos.ginger.engine.vulkan.render.renderers.EntityRenderer;
public class GingerVK
{
private static GingerVK INSTANCE;
public EntityRenderer entityRenderer;
public static void init() {
INSTANCE = new GingerVK();
VKVariables.renderManager = new VKRenderManager();
}
public static GingerVK getInstance()
{
return INSTANCE;
}
public void createRenderers() {
entityRenderer = new EntityRenderer();
VKVariables.renderManager.addRenderer(entityRenderer);
}
public void cleanup()
{
for(VKRenderObject entity: entityRenderer.entities) {
VKBufferMesh bufferMesh = entity.getModel();
bufferMesh.cleanup();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,12 @@
package com.github.hydos.ginger.engine.common.api.game;
import com.github.hydos.ginger.engine.common.cameras.Camera;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.elements.objects.GLRenderObject;
public abstract class Game
{
public GameData data;
public RenderObject player; // FIXME: @hYdos you know these probably don't need to be here, but the game stops working when they're not.
public GLRenderObject player; // FIXME: @hYdos you know these probably don't need to be here, but the game stops working when they're not.
public Camera camera;
public Game()

View File

@ -5,7 +5,7 @@ import java.util.*;
import org.joml.Vector4f;
import com.github.hydos.ginger.engine.common.cameras.Camera;
import com.github.hydos.ginger.engine.common.elements.GuiTexture;
import com.github.hydos.ginger.engine.common.elements.GLGuiTexture;
import com.github.hydos.ginger.engine.common.elements.objects.*;
/**
@ -14,23 +14,23 @@ import com.github.hydos.ginger.engine.common.elements.objects.*;
*/
public class GameData
{
public List<GuiTexture> guis;
public List<RenderObject> entities;
public List<GLGuiTexture> guis;
public List<GLRenderObject> entities;
public List<Light> lights;
public List<RenderObject> normalMapEntities;
public RenderObject playerObject;
public List<GLRenderObject> normalMapEntities;
public GLRenderObject playerObject;
public Camera camera;
public Vector4f clippingPlane;
public boolean handleGuis = true;
public int tickSpeed = 20;
public GameData(RenderObject playerEntity, Camera camera, int tickSpeed)
public GameData(GLRenderObject playerEntity, Camera camera, int tickSpeed)
{
clippingPlane = new Vector4f(0, -1, 0, 100000);
guis = new ArrayList<GuiTexture>();
entities = new ArrayList<RenderObject>();
guis = new ArrayList<GLGuiTexture>();
entities = new ArrayList<GLRenderObject>();
lights = new ArrayList<Light>();
normalMapEntities = new ArrayList<RenderObject>();
normalMapEntities = new ArrayList<GLRenderObject>();
this.playerObject = playerEntity;
this.camera = camera;
this.tickSpeed = tickSpeed;

View File

@ -2,11 +2,11 @@ package com.github.hydos.ginger.engine.common.cameras;
import org.joml.Vector3f;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.elements.objects.GLRenderObject;
public abstract class Camera
{
public RenderObject player;
public GLRenderObject player;
private float pitch, yaw, roll;
private Vector3f position = new Vector3f(0, 0, 0);

View File

@ -2,7 +2,7 @@ package com.github.hydos.ginger.engine.common.cameras;
import org.joml.Vector3f;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.elements.objects.GLRenderObject;
import com.github.hydos.ginger.engine.common.io.Window;
public class FirstPersonCamera extends Camera
@ -11,7 +11,7 @@ public class FirstPersonCamera extends Camera
private float pitch, yaw;
private float roll;
public FirstPersonCamera(RenderObject playerEntity)
public FirstPersonCamera(GLRenderObject playerEntity)
{
this.player = playerEntity;
playerEntity.setVisible(false);

View File

@ -2,12 +2,12 @@ package com.github.hydos.ginger.engine.common.cameras;
import org.lwjgl.glfw.*;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.elements.objects.GLRenderObject;
import com.github.hydos.ginger.engine.common.io.Window;
public class ThirdPersonCamera extends Camera
{
public ThirdPersonCamera(RenderObject playerEntity)
public ThirdPersonCamera(GLRenderObject playerEntity)
{
this.player = playerEntity;
}

View File

@ -2,12 +2,12 @@ package com.github.hydos.ginger.engine.common.elements;
import org.joml.Vector2f;
public class GuiTexture
public class GLGuiTexture
{
private int texture;
private Vector2f position, scale;
public GuiTexture(int texture, Vector2f position, Vector2f scale)
public GLGuiTexture(int texture, Vector2f position, Vector2f scale)
{
this.texture = texture;
this.position = position;

View File

@ -1,27 +1,15 @@
package com.github.hydos.ginger.engine.common.elements.objects;
package com.github.hydos.ginger.engine.common.elements;
import org.joml.Vector3f;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
public class RenderObject
public abstract class RenderObject
{
private GLTexturedModel model;
public Vector3f position;
private float rotX = 0, rotY = 0, rotZ = 0;
private Vector3f scale;
private boolean visible = true;
public RenderObject(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
{
this.model = model;
this.position = position;
this.rotX = rotX;
this.rotY = rotY;
this.rotZ = rotZ;
this.scale = scale;
}
public float rotX = 0, rotY = 0, rotZ = 0;
public Vector3f scale;
public boolean visible = true;
public void x(float x)
{ this.position.x = x; }
@ -30,9 +18,7 @@ public class RenderObject
public void z(float z)
{ this.position.z = z; }
public GLTexturedModel getModel()
{ return model; }
public Vector3f getPosition()
{ return position; }
@ -63,9 +49,6 @@ public class RenderObject
this.rotZ += dz;
}
public void setModel(GLTexturedModel model)
{ this.model = model; }
public void setPosition(Vector3f position)
{ this.position = position; }
@ -90,4 +73,5 @@ public class RenderObject
{
this.visible = visible;
}
}

View File

@ -5,13 +5,13 @@ import java.util.List;
import org.joml.Vector2f;
import org.lwjgl.glfw.GLFW;
import com.github.hydos.ginger.engine.common.elements.GuiTexture;
import com.github.hydos.ginger.engine.common.elements.GLGuiTexture;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.opengl.utils.GLLoader;
public class TextureButton
{
private GuiTexture guiTexture;
private GLGuiTexture guiTexture;
private boolean shown = false;
private boolean clicked = false;
private boolean isHovering = false;
@ -20,10 +20,10 @@ public class TextureButton
public TextureButton(String texture, Vector2f position, Vector2f scale)
{
resourceLocation = texture;
guiTexture = new GuiTexture(GLLoader.loadTextureDirectly(texture), position, scale);
guiTexture = new GLGuiTexture(GLLoader.loadTextureDirectly(texture), position, scale);
}
public void hide(List<GuiTexture> guiTextureList)
public void hide(List<GLGuiTexture> guiTextureList)
{
if (!shown)
{
@ -44,7 +44,7 @@ public class TextureButton
public boolean isShown()
{ return shown; }
public void show(List<GuiTexture> guiTextureList)
public void show(List<GLGuiTexture> guiTextureList)
{
if (shown)
{

View File

@ -0,0 +1,28 @@
package com.github.hydos.ginger.engine.common.elements.objects;
import org.joml.Vector3f;
import com.github.hydos.ginger.engine.common.elements.RenderObject;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
public class GLRenderObject extends RenderObject
{
private TexturedModel model;
public GLRenderObject(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
{
this.model = model;
this.position = position;
this.rotX = rotX;
this.rotY = rotY;
this.rotZ = rotZ;
this.scale = scale;
}
public TexturedModel getModel()
{ return model; }
public void setModel(TexturedModel model)
{ this.model = model; }
}

View File

@ -10,7 +10,7 @@ import com.github.hydos.ginger.engine.common.api.GingerRegister;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
public class Player extends RenderObject implements WorldGenConstants
public class Player extends GLRenderObject implements WorldGenConstants
{
private boolean isInAir = false;
private double upwardsSpeed;

View File

@ -0,0 +1,5 @@
package com.github.hydos.ginger.engine.common.elements.objects;
public abstract class TexturedModel
{
}

View File

@ -0,0 +1,13 @@
package com.github.hydos.ginger.engine.common.exceptions;
public class GingerException extends RuntimeException
{
public GingerException(String string)
{super(string);}
/**
* default engine exception (ginger2)
*/
private static final long serialVersionUID = 6473251828709805188L;
}

View File

@ -0,0 +1,10 @@
package com.github.hydos.ginger.engine.common.exceptions;
public class OpenGLException extends RuntimeException
{
/**
* default openGL exception from ginger
*/
private static final long serialVersionUID = 4522795553161016196L;
}

View File

@ -0,0 +1,10 @@
package com.github.hydos.ginger.engine.common.exceptions;
public class VulkanExcpetion extends RuntimeException
{
/**
* default vulkan exception
*/
private static final long serialVersionUID = -2001638471757509524L;
}

View File

@ -240,4 +240,11 @@ public class Window
{
return window;
}
public static void destroy()
{
GLFW.glfwDestroyWindow(Window.getWindow());
GLFW.glfwTerminate();
}
}

View File

@ -0,0 +1,18 @@
package com.github.hydos.ginger.engine.common.render;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkCommandBuffer;
public abstract class Renderer
{
public int priority = 2;//default is 2. 1 is the highest and 10 is the lowest
public void VKRender(MemoryStack stack, VkCommandBuffer commandBuffer, int index)
{}
public void VKBindShaders(MemoryStack stack)
{}
public void VKUnbindShaders(MemoryStack stack)
{}
}

View File

@ -2,11 +2,11 @@ package com.github.hydos.ginger.engine.common.screen;
import java.util.List;
import com.github.hydos.ginger.engine.common.elements.GuiTexture;
import com.github.hydos.ginger.engine.common.elements.GLGuiTexture;
public abstract class Screen
{
public List<GuiTexture> elements;
public List<GLGuiTexture> elements;
public abstract void render(); // FIXME: This never gets called!!!

View File

@ -5,7 +5,7 @@ import org.joml.Vector2f;
import com.github.hydos.ginger.engine.common.api.*;
import com.github.hydos.ginger.engine.common.api.game.Game;
import com.github.hydos.ginger.engine.common.elements.buttons.TextureButton;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.elements.objects.GLRenderObject;
import com.github.hydos.ginger.engine.common.font.*;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.common.screen.Screen;
@ -36,7 +36,7 @@ public class GingerGL extends GingerEngine
getRegistry().currentScreen = screen;
}
public void setGingerPlayer(RenderObject player)
public void setGingerPlayer(GLRenderObject player)
{
registry.game.data.entities.remove(registry.game.player); // remove the old player
registry.game.data.playerObject = player; // set all the player variables

View File

@ -6,10 +6,10 @@ import java.util.*;
import org.joml.*;
import org.lwjgl.opengl.*;
import com.github.halotroop.litecraft.render.BlockRenderer;
import com.github.halotroop.litecraft.render.GLBlockRenderer;
import com.github.hydos.ginger.engine.common.api.GingerRegister;
import com.github.hydos.ginger.engine.common.cameras.Camera;
import com.github.hydos.ginger.engine.common.elements.GuiTexture;
import com.github.hydos.ginger.engine.common.elements.GLGuiTexture;
import com.github.hydos.ginger.engine.common.elements.objects.*;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
@ -32,8 +32,8 @@ public class GLRenderManager
// GL11.glCullFace(GL11.GL_BACK);
}
public BlockRenderer blockRenderer;
private StaticShader entityShader;
public GLBlockRenderer blockRenderer;
private GLStaticShader entityShader;
public GLObjectRenderer entityRenderer;
private GuiShader guiShader;
private GLGuiRenderer guiRenderer;
@ -41,14 +41,14 @@ public class GLRenderManager
private GLNormalMappingRenderer normalRenderer;
private Matrix4f projectionMatrix;
private ShadowMapMasterRenderer shadowMapRenderer;
private Map<GLTexturedModel, List<RenderObject>> entities = new HashMap<GLTexturedModel, List<RenderObject>>();
private Map<GLTexturedModel, List<RenderObject>> normalMapEntities = new HashMap<GLTexturedModel, List<RenderObject>>();
private Map<GLTexturedModel, List<GLRenderObject>> entities = new HashMap<GLTexturedModel, List<GLRenderObject>>();
private Map<GLTexturedModel, List<GLRenderObject>> normalMapEntities = new HashMap<GLTexturedModel, List<GLRenderObject>>();
public GLRenderManager(Camera camera)
{
createProjectionMatrix();
entityShader = new StaticShader();
blockRenderer = new BlockRenderer(entityShader, projectionMatrix);
entityShader = new GLStaticShader();
blockRenderer = new GLBlockRenderer(entityShader, projectionMatrix);
entityRenderer = new GLObjectRenderer(entityShader, projectionMatrix);
guiShader = new GuiShader();
guiRenderer = new GLGuiRenderer(guiShader);
@ -93,41 +93,41 @@ public class GLRenderManager
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
}
private void processEntity(RenderObject entity)
private void processEntity(GLRenderObject entity)
{
GLTexturedModel entityModel = entity.getModel();
List<RenderObject> batch = entities.get(entityModel);
GLTexturedModel entityModel = (GLTexturedModel) entity.getModel();
List<GLRenderObject> batch = entities.get(entityModel);
if (batch != null)
{
batch.add(entity);
}
else
{
List<RenderObject> newBatch = new ArrayList<RenderObject>();
List<GLRenderObject> newBatch = new ArrayList<GLRenderObject>();
newBatch.add(entity);
entities.put(entityModel, newBatch);
}
}
private void processEntityWithNormal(RenderObject entity)
private void processEntityWithNormal(GLRenderObject entity)
{
GLTexturedModel entityModel = entity.getModel();
List<RenderObject> batch = normalMapEntities.get(entityModel);
GLTexturedModel entityModel = (GLTexturedModel) entity.getModel();
List<GLRenderObject> batch = normalMapEntities.get(entityModel);
if (batch != null)
{
batch.add(entity);
}
else
{
List<RenderObject> newBatch = new ArrayList<RenderObject>();
List<GLRenderObject> newBatch = new ArrayList<GLRenderObject>();
newBatch.add(entity);
normalMapEntities.put(entityModel, newBatch);
}
}
private void renderEntities(List<RenderObject> entities, Camera camera, List<Light> lights)
private void renderEntities(List<GLRenderObject> entities, Camera camera, List<Light> lights)
{
for (RenderObject entity : entities)
for (GLRenderObject entity : entities)
{ processEntity(entity); }
entityRenderer.prepare();
entityShader.start();
@ -139,24 +139,24 @@ public class GLRenderManager
this.entities.clear();
}
public void renderGui(GuiTexture guiTexture)
public void renderGui(GLGuiTexture guiTexture)
{
List<GuiTexture> texture = new ArrayList<GuiTexture>();
List<GLGuiTexture> texture = new ArrayList<GLGuiTexture>();
texture.add(guiTexture);
guiRenderer.render(texture);
}
public void renderGuis(List<GuiTexture> guis)
public void renderGuis(List<GLGuiTexture> guis)
{ guiRenderer.render(guis); }
private void renderNormalEntities(List<RenderObject> normalEntities, List<Light> lights, Camera camera, Vector4f clipPlane)
private void renderNormalEntities(List<GLRenderObject> normalEntities, List<Light> lights, Camera camera, Vector4f clipPlane)
{
for (RenderObject entity : normalEntities)
for (GLRenderObject entity : normalEntities)
{ processEntityWithNormal(entity); }
normalRenderer.render(normalMapEntities, clipPlane, lights, camera);
}
public void renderScene(List<RenderObject> entities, List<RenderObject> normalEntities, List<Light> lights, Camera camera, Vector4f clipPlane)
public void renderScene(List<GLRenderObject> entities, List<GLRenderObject> normalEntities, List<Light> lights, Camera camera, Vector4f clipPlane)
{
prepare();
renderEntities(entities, camera, lights);
@ -165,9 +165,9 @@ public class GLRenderManager
// skyboxRenderer.render(camera);
}
public void renderShadowMap(List<RenderObject> entityList, Light sun)
public void renderShadowMap(List<GLRenderObject> entityList, Light sun)
{
for (RenderObject entity : entityList)
for (GLRenderObject entity : entityList)
{ processEntity(entity); }
shadowMapRenderer.render(entities, sun);
entities.clear();

View File

@ -1,5 +0,0 @@
package com.github.hydos.ginger.engine.opengl.render;
public abstract class Renderer
{
}

View File

@ -1,8 +1,9 @@
package com.github.hydos.ginger.engine.opengl.render.models;
import com.github.hydos.ginger.engine.common.elements.objects.TexturedModel;
import com.github.hydos.ginger.engine.opengl.render.texture.ModelTexture;
public class GLTexturedModel
public class GLTexturedModel extends TexturedModel
{
private RawModel rawModel;
private ModelTexture texture;

View File

@ -5,7 +5,7 @@ import java.util.*;
import org.lwjgl.opengl.*;
import com.github.hydos.ginger.engine.common.font.*;
import com.github.hydos.ginger.engine.opengl.render.Renderer;
import com.github.hydos.ginger.engine.common.render.Renderer;
import com.github.hydos.ginger.engine.opengl.render.shaders.FontShader;
public class GLFontRenderer extends Renderer

View File

@ -5,9 +5,9 @@ import java.util.List;
import org.joml.Matrix4f;
import org.lwjgl.opengl.*;
import com.github.hydos.ginger.engine.common.elements.GuiTexture;
import com.github.hydos.ginger.engine.common.elements.GLGuiTexture;
import com.github.hydos.ginger.engine.common.math.Maths;
import com.github.hydos.ginger.engine.opengl.render.Renderer;
import com.github.hydos.ginger.engine.common.render.Renderer;
import com.github.hydos.ginger.engine.opengl.render.models.RawModel;
import com.github.hydos.ginger.engine.opengl.render.shaders.GuiShader;
import com.github.hydos.ginger.engine.opengl.utils.GLLoader;
@ -30,7 +30,7 @@ public class GLGuiRenderer extends Renderer
public void cleanUp()
{ shader.cleanUp(); }
public void render(List<GuiTexture> guis)
public void render(List<GLGuiTexture> guis)
{
if (guis != null)
{
@ -41,7 +41,7 @@ public class GLGuiRenderer extends Renderer
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
for (GuiTexture gui : guis)
for (GLGuiTexture gui : guis)
{
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, gui.getTexture());

View File

@ -9,6 +9,7 @@ import com.github.hydos.ginger.engine.common.cameras.Camera;
import com.github.hydos.ginger.engine.common.elements.objects.*;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.common.math.Maths;
import com.github.hydos.ginger.engine.common.render.Renderer;
import com.github.hydos.ginger.engine.opengl.render.*;
import com.github.hydos.ginger.engine.opengl.render.models.*;
import com.github.hydos.ginger.engine.opengl.render.shaders.NormalMappingShader;
@ -40,7 +41,7 @@ public class GLNormalMappingRenderer extends Renderer
shader.loadViewMatrix(viewMatrix);
}
private void prepareInstance(RenderObject entity)
private void prepareInstance(GLRenderObject entity)
{
Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotX(),
entity.getRotY(), entity.getRotZ(), entity.getScale());
@ -66,15 +67,15 @@ public class GLNormalMappingRenderer extends Renderer
GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getNormalMap());
}
public void render(Map<GLTexturedModel, List<RenderObject>> entities, Vector4f clipPlane, List<Light> lights, Camera camera)
public void render(Map<GLTexturedModel, List<GLRenderObject>> entities, Vector4f clipPlane, List<Light> lights, Camera camera)
{
shader.start();
prepare(clipPlane, lights, camera);
for (GLTexturedModel model : entities.keySet())
{
prepareTexturedModel(model);
List<RenderObject> batch = entities.get(model);
for (RenderObject entity : batch)
List<GLRenderObject> batch = entities.get(model);
for (GLRenderObject entity : batch)
{
prepareInstance(entity);
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);

View File

@ -7,19 +7,20 @@ import org.lwjgl.opengl.*;
import com.github.halotroop.litecraft.types.block.BlockInstance;
import com.github.hydos.ginger.engine.common.api.GingerRegister;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.elements.objects.GLRenderObject;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.common.math.Maths;
import com.github.hydos.ginger.engine.common.render.Renderer;
import com.github.hydos.ginger.engine.opengl.render.*;
import com.github.hydos.ginger.engine.opengl.render.models.*;
import com.github.hydos.ginger.engine.opengl.render.shaders.StaticShader;
import com.github.hydos.ginger.engine.opengl.render.shaders.GLStaticShader;
import com.github.hydos.ginger.engine.opengl.render.texture.ModelTexture;
public class GLObjectRenderer extends Renderer
{
private StaticShader shader;
private GLStaticShader shader;
public GLObjectRenderer(StaticShader shader, Matrix4f projectionMatrix)
public GLObjectRenderer(GLStaticShader shader, Matrix4f projectionMatrix)
{
this.shader = shader;
shader.start();
@ -30,7 +31,7 @@ public class GLObjectRenderer extends Renderer
public void prepare()
{ GL11.glEnable(GL11.GL_DEPTH_TEST); }
private void prepareInstance(RenderObject entity)
private void prepareInstance(GLRenderObject entity)
{
Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
shader.loadTransformationMatrix(transformationMatrix);
@ -58,13 +59,13 @@ public class GLObjectRenderer extends Renderer
GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getTextureID());
}
public void render(Map<GLTexturedModel, List<RenderObject>> entities)
public void render(Map<GLTexturedModel, List<GLRenderObject>> entities)
{
for (GLTexturedModel model : entities.keySet())
{
prepareTexturedModel(model);
List<RenderObject> batch = entities.get(model);
for (RenderObject entity : batch)
List<GLRenderObject> batch = entities.get(model);
for (GLRenderObject entity : batch)
{
if (entity.isVisible())
{
@ -90,11 +91,11 @@ public class GLObjectRenderer extends Renderer
shader.start();
shader.loadSkyColour(Window.getColour());
shader.loadViewMatrix(GingerRegister.getInstance().game.data.camera);
for (RenderObject entity : renderList)
for (GLRenderObject entity : renderList)
{
if (entity != null && entity.getModel() != null)
{
GLTexturedModel model = entity.getModel();
GLTexturedModel model = (GLTexturedModel) entity.getModel();
prepareTexturedModel(model);
prepareInstance(entity);
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);

View File

@ -4,7 +4,7 @@ import org.joml.Matrix4f;
import org.lwjgl.opengl.*;
import com.github.hydos.ginger.engine.common.cameras.Camera;
import com.github.hydos.ginger.engine.opengl.render.Renderer;
import com.github.hydos.ginger.engine.common.render.Renderer;
import com.github.hydos.ginger.engine.opengl.render.models.RawModel;
import com.github.hydos.ginger.engine.opengl.render.shaders.SkyboxShader;
import com.github.hydos.ginger.engine.opengl.utils.GLLoader;

View File

@ -8,7 +8,7 @@ import com.github.hydos.ginger.engine.common.cameras.Camera;
import com.github.hydos.ginger.engine.common.elements.objects.Light;
import com.github.hydos.ginger.engine.common.math.Maths;
public class StaticShader extends ShaderProgram
public class GLStaticShader extends ShaderProgram
{
private static final int MAX_LIGHTS = 5;
private int location_transformationMatrix;
@ -22,7 +22,7 @@ public class StaticShader extends ShaderProgram
private int location_useFakeLighting;
private int location_skyColour;
public StaticShader()
public GLStaticShader()
{ super("entityVertexShader.glsl", "entityFragmentShader.glsl"); }
@Override

View File

@ -5,7 +5,7 @@ import java.util.*;
import org.joml.Matrix4f;
import org.lwjgl.opengl.*;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.elements.objects.GLRenderObject;
import com.github.hydos.ginger.engine.common.math.Maths;
import com.github.hydos.ginger.engine.opengl.render.GLRenderManager;
import com.github.hydos.ginger.engine.opengl.render.models.*;
@ -47,7 +47,7 @@ public class ShadowMapEntityRenderer
*
* @param entity
* - the entity to be prepared for rendering. */
private void prepareInstance(RenderObject entity)
private void prepareInstance(GLRenderObject entity)
{
Matrix4f modelMatrix = Maths.createTransformationMatrix(entity.getPosition(),
entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
@ -60,7 +60,7 @@ public class ShadowMapEntityRenderer
*
* @param entities
* - the entities to be rendered to the shadow map. */
protected void render(Map<GLTexturedModel, List<RenderObject>> entities)
protected void render(Map<GLTexturedModel, List<GLRenderObject>> entities)
{
for (GLTexturedModel model : entities.keySet())
{
@ -70,7 +70,7 @@ public class ShadowMapEntityRenderer
GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getTextureID());
if (model.getTexture().isTransparent())
{ GLRenderManager.disableCulling(); }
for (RenderObject entity : entities.get(model))
for (GLRenderObject entity : entities.get(model))
{
prepareInstance(entity);
GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(),

View File

@ -134,7 +134,7 @@ public class ShadowMapMasterRenderer
* entities in that list use.
* @param sun
* - the light acting as the sun in the scene. */
public void render(Map<GLTexturedModel, List<RenderObject>> entities, Light sun)
public void render(Map<GLTexturedModel, List<GLRenderObject>> entities, Light sun)
{
shadowBox.update();
Vector3f sunPosition = sun.getPosition();

View File

@ -1,8 +1,48 @@
package com.github.hydos.ginger.engine.vulkan;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.vulkan.VK10.*;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
public class VKRegister
{
public static void createInstance() {
try(MemoryStack stack = stackPush()) {
// Use calloc to initialize the structs with 0s. Otherwise, the program can crash due to random values
VkApplicationInfo appInfo = VkApplicationInfo.callocStack(stack);
appInfo.sType(VK_STRUCTURE_TYPE_APPLICATION_INFO);
appInfo.pApplicationName(stack.UTF8Safe("Hello Triangle"));
appInfo.applicationVersion(VK_MAKE_VERSION(1, 0, 0));
appInfo.pEngineName(stack.UTF8Safe("No Engine"));
appInfo.engineVersion(VK_MAKE_VERSION(1, 0, 0));
appInfo.apiVersion(VK_API_VERSION_1_0);
VkInstanceCreateInfo createInfo = VkInstanceCreateInfo.callocStack(stack);
createInfo.sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
createInfo.pApplicationInfo(appInfo);
// enabledExtensionCount is implicitly set when you call ppEnabledExtensionNames
createInfo.ppEnabledExtensionNames(VKUtils.getRequiredExtensions());
// We need to retrieve the pointer of the created instance
PointerBuffer instancePtr = stack.mallocPointer(1);
if(vkCreateInstance(createInfo, null, instancePtr) != VK_SUCCESS) {
throw new RuntimeException("Failed to create instance");
}
VKVariables.instance = new VkInstance(instancePtr.get(0), createInfo);
}
}
}

View File

@ -0,0 +1,87 @@
package com.github.hydos.ginger.engine.vulkan;
import static org.lwjgl.vulkan.VK10.VK_SAMPLE_COUNT_1_BIT;
import java.util.List;
import java.util.Map;
import org.lwjgl.vulkan.VkCommandBuffer;
import org.lwjgl.vulkan.VkDevice;
import org.lwjgl.vulkan.VkExtent2D;
import org.lwjgl.vulkan.VkInstance;
import org.lwjgl.vulkan.VkPhysicalDevice;
import org.lwjgl.vulkan.VkQueue;
import com.github.hydos.ginger.engine.vulkan.model.VKVertex;
import com.github.hydos.ginger.engine.vulkan.render.*;
/**
*
* the place where Vulkan variables are stored
* @author hydos
*
*/
public class VKVariables
{
public static VkInstance instance;
public static long surface;
public static VkPhysicalDevice physicalDevice;
public static VkDevice device;
public static int msaaSamples = VK_SAMPLE_COUNT_1_BIT;
public static VkQueue graphicsQueue;
public static VkQueue presentQueue;
public static long swapChain;
public static List<Long> swapChainImages;
public static int swapChainImageFormat;
public static VkExtent2D swapChainExtent;
public static List<Long> swapChainImageViews;
public static List<Long> swapChainFramebuffers;
public static long renderPass;
public static long descriptorPool;
public static long descriptorSetLayout;
public static List<Long> descriptorSets;
public static long pipelineLayout;
public static long graphicsPipeline;
public static long commandPool;
public static long colorImage;
public static long colorImageMemory;
public static long colorImageView;
public static long depthImage;
public static long depthImageMemory;
public static long depthImageView;
public static int mipLevels;
public static long textureImage;
public static long textureImageMemory;
public static long textureImageView;
public static long textureSampler;
public static VKVertex[] vertices; //TODO: remove and properly add model loading
public static int[] indices;
public static List<Long> uniformBuffers;
public static List<Long> uniformBuffersMemory;
public static List<VkCommandBuffer> commandBuffers;
public static List<Frame> inFlightFrames;
public static Map<Integer, Frame> imagesInFlight;
public static int currentFrame;
public static boolean framebufferResize;
public static VKRenderManager renderManager;
public static int currentImageIndex;
}

View File

@ -0,0 +1,36 @@
package com.github.hydos.ginger.engine.vulkan.elements;
import org.joml.Vector3f;
import com.github.hydos.ginger.engine.common.elements.RenderObject;
import com.github.hydos.ginger.engine.vulkan.model.VKModelLoader.VKMesh;
import com.github.hydos.ginger.engine.vulkan.render.VKBufferMesh;
public class VKRenderObject extends RenderObject
{
private VKBufferMesh model = null;//until i add VKTextured models
private VKMesh rawModel;//until i add VKTextured models
public VKRenderObject(VKMesh rawModel, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
{
this.rawModel = rawModel;
this.position = position;
this.rotX = rotX;
this.rotY = rotY;
this.rotZ = rotZ;
this.scale = scale;
}
public VKBufferMesh getModel()
{ return model; }
public void setModel(VKBufferMesh model)
{ this.model = model; }
public VKMesh getRawModel()
{
return rawModel;
}
}

View File

@ -0,0 +1,30 @@
package com.github.hydos.ginger.engine.vulkan.io;
import static org.lwjgl.vulkan.VK10.VK_NULL_HANDLE;
import static org.lwjgl.vulkan.VK10.VK_SUCCESS;
import java.nio.LongBuffer;
import org.lwjgl.glfw.GLFWVulkan;
import org.lwjgl.system.MemoryStack;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
public class VKWindow
{
public static void createSurface() {
try(MemoryStack stack = MemoryStack.stackPush()) {
LongBuffer pSurface = stack.longs(VK_NULL_HANDLE);
if(GLFWVulkan.glfwCreateWindowSurface(VKVariables.instance, Window.getWindow(), null, pSurface) != VK_SUCCESS) {
throw new RuntimeException("Failed to create window surface");
}
VKVariables.surface = pSurface.get(0);
}
}
}

View File

@ -0,0 +1,155 @@
package com.github.hydos.ginger.engine.vulkan.managers;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.vulkan.VK10.VK_COMMAND_BUFFER_LEVEL_PRIMARY;
import static org.lwjgl.vulkan.VK10.VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
import static org.lwjgl.vulkan.VK10.VK_NULL_HANDLE;
import static org.lwjgl.vulkan.VK10.VK_PIPELINE_BIND_POINT_GRAPHICS;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_SUBMIT_INFO;
import static org.lwjgl.vulkan.VK10.VK_SUBPASS_CONTENTS_INLINE;
import static org.lwjgl.vulkan.VK10.VK_SUCCESS;
import static org.lwjgl.vulkan.VK10.vkAllocateCommandBuffers;
import static org.lwjgl.vulkan.VK10.vkBeginCommandBuffer;
import static org.lwjgl.vulkan.VK10.vkCmdBeginRenderPass;
import static org.lwjgl.vulkan.VK10.vkCmdBindPipeline;
import static org.lwjgl.vulkan.VK10.vkCmdEndRenderPass;
import static org.lwjgl.vulkan.VK10.vkEndCommandBuffer;
import static org.lwjgl.vulkan.VK10.vkFreeCommandBuffers;
import static org.lwjgl.vulkan.VK10.vkQueueSubmit;
import static org.lwjgl.vulkan.VK10.vkQueueWaitIdle;
import java.util.ArrayList;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkClearValue;
import org.lwjgl.vulkan.VkCommandBuffer;
import org.lwjgl.vulkan.VkCommandBufferAllocateInfo;
import org.lwjgl.vulkan.VkCommandBufferBeginInfo;
import org.lwjgl.vulkan.VkOffset2D;
import org.lwjgl.vulkan.VkRect2D;
import org.lwjgl.vulkan.VkRenderPassBeginInfo;
import org.lwjgl.vulkan.VkSubmitInfo;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
public class CommandBufferManager
{
public static void createCommandBuffers() {
final int commandBuffersCount = VKVariables.swapChainFramebuffers.size();
VKVariables.commandBuffers = new ArrayList<>(commandBuffersCount);
try(MemoryStack stack = stackPush()) {
VkCommandBufferAllocateInfo allocInfo = VkCommandBufferAllocateInfo.callocStack(stack);
allocInfo.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO);
allocInfo.commandPool(VKVariables.commandPool);
allocInfo.level(VK_COMMAND_BUFFER_LEVEL_PRIMARY);
allocInfo.commandBufferCount(commandBuffersCount);
PointerBuffer pCommandBuffers = stack.mallocPointer(commandBuffersCount);
if(vkAllocateCommandBuffers(VKVariables.device, allocInfo, pCommandBuffers) != VK_SUCCESS) {
throw new RuntimeException("Failed to allocate command buffers");
}
for(int i = 0;i < commandBuffersCount;i++) {
VKVariables.commandBuffers.add(new VkCommandBuffer(pCommandBuffers.get(i), VKVariables.device));
}
VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.callocStack(stack);
beginInfo.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO);
VkRenderPassBeginInfo renderPassInfo = VkRenderPassBeginInfo.callocStack(stack);
renderPassInfo.sType(VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO);
renderPassInfo.renderPass(VKVariables.renderPass);
VkRect2D renderArea = VkRect2D.callocStack(stack);
renderArea.offset(VkOffset2D.callocStack(stack).set(0, 0));
renderArea.extent(VKVariables.swapChainExtent);
renderPassInfo.renderArea(renderArea);
VkClearValue.Buffer clearValues = VkClearValue.callocStack(2, stack);
clearValues.get(0).color().float32(stack.floats(Window.getColour().x / 255, Window.getColour().y / 255, Window.getColour().z / 255, 1.0f)); //The screens clear colour
clearValues.get(1).depthStencil().set(1.0f, 0);
renderPassInfo.pClearValues(clearValues);
for(int i = 0;i < commandBuffersCount;i++) {
VkCommandBuffer commandBuffer = VKVariables.commandBuffers.get(i);
if(vkBeginCommandBuffer(commandBuffer, beginInfo) != VK_SUCCESS) {
throw new RuntimeException("Failed to begin recording command buffer");
}
renderPassInfo.framebuffer(VKVariables.swapChainFramebuffers.get(i));
vkCmdBeginRenderPass(commandBuffer, renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
{
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, VKVariables.graphicsPipeline);
VKVariables.renderManager.render(stack, commandBuffer, i);
}
vkCmdEndRenderPass(commandBuffer);
if(vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
throw new RuntimeException("Failed to record command buffer");
}
}
}
}
public static VkCommandBuffer beginSingleTimeCommands() {
try(MemoryStack stack = stackPush()) {
VkCommandBufferAllocateInfo allocInfo = VkCommandBufferAllocateInfo.callocStack(stack);
allocInfo.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO);
allocInfo.level(VK_COMMAND_BUFFER_LEVEL_PRIMARY);
allocInfo.commandPool(VKVariables.commandPool);
allocInfo.commandBufferCount(1);
PointerBuffer pCommandBuffer = stack.mallocPointer(1);
vkAllocateCommandBuffers(VKVariables.device, allocInfo, pCommandBuffer);
VkCommandBuffer commandBuffer = new VkCommandBuffer(pCommandBuffer.get(0), VKVariables.device);
VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.callocStack(stack);
beginInfo.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO);
beginInfo.flags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
vkBeginCommandBuffer(commandBuffer, beginInfo);
return commandBuffer;
}
}
public static void endSingleTimeCommands(VkCommandBuffer commandBuffer) {
try(MemoryStack stack = stackPush()) {
vkEndCommandBuffer(commandBuffer);
VkSubmitInfo.Buffer submitInfo = VkSubmitInfo.callocStack(1, stack);
submitInfo.sType(VK_STRUCTURE_TYPE_SUBMIT_INFO);
submitInfo.pCommandBuffers(stack.pointers(commandBuffer));
vkQueueSubmit(VKVariables.graphicsQueue, submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(VKVariables.graphicsQueue);
vkFreeCommandBuffers(VKVariables.device, VKVariables.commandPool, commandBuffer);
}
}
}

View File

@ -0,0 +1,126 @@
package com.github.hydos.ginger.engine.vulkan.managers;
import static org.lwjgl.stb.STBImage.*;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.vulkan.VK10.*;
import java.net.*;
import java.nio.*;
import java.nio.file.Paths;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkSamplerCreateInfo;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
import com.github.hydos.ginger.engine.vulkan.utils.*;
public class VKTextureManager
{
public static void createTextureImage()
{
try(MemoryStack stack = stackPush()) {
String filename = Paths.get(new URI(ClassLoader.getSystemClassLoader().getResource("textures/chalet.jpg").toExternalForm())).toString();
IntBuffer pWidth = stack.mallocInt(1);
IntBuffer pHeight = stack.mallocInt(1);
IntBuffer pChannels = stack.mallocInt(1);
ByteBuffer pixels = stbi_load(filename, pWidth, pHeight, pChannels, STBI_rgb_alpha);
long imageSize = pWidth.get(0) * pHeight.get(0) * 4; // pChannels.get(0);
VKVariables.mipLevels = (int) Math.floor(VKUtils.log2(Math.max(pWidth.get(0), pHeight.get(0)))) + 1;
if(pixels == null) {
throw new RuntimeException("Failed to load texture image " + filename);
}
LongBuffer pStagingBuffer = stack.mallocLong(1);
LongBuffer pStagingBufferMemory = stack.mallocLong(1);
VKBufferUtils.createBuffer(imageSize,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
pStagingBuffer,
pStagingBufferMemory);
PointerBuffer data = stack.mallocPointer(1);
vkMapMemory(VKVariables.device, pStagingBufferMemory.get(0), 0, imageSize, 0, data);
{
VKUtils.memcpy(data.getByteBuffer(0, (int)imageSize), pixels, imageSize);
}
vkUnmapMemory(VKVariables.device, pStagingBufferMemory.get(0));
stbi_image_free(pixels);
LongBuffer pTextureImage = stack.mallocLong(1);
LongBuffer pTextureImageMemory = stack.mallocLong(1);
VKUtils.createImage(pWidth.get(0), pHeight.get(0),
VKVariables.mipLevels,
VK_SAMPLE_COUNT_1_BIT, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
pTextureImage,
pTextureImageMemory);
VKVariables.textureImage = pTextureImage.get(0);
VKVariables.textureImageMemory = pTextureImageMemory.get(0);
VKUtils.transitionImageLayout(VKVariables.textureImage,
VK_FORMAT_R8G8B8A8_SRGB,
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VKVariables.mipLevels);
VKUtils.copyBufferToImage(pStagingBuffer.get(0), VKVariables.textureImage, pWidth.get(0), pHeight.get(0));
// Transitioned to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL while generating mipmaps
VKUtils.generateMipmaps(VKVariables.textureImage, VK_FORMAT_R8G8B8A8_SRGB, pWidth.get(0), pHeight.get(0), VKVariables.mipLevels);
vkDestroyBuffer(VKVariables.device, pStagingBuffer.get(0), null);
vkFreeMemory(VKVariables.device, pStagingBufferMemory.get(0), null);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
public static void createTextureImageView() {
VKVariables.textureImageView = VKUtils.createImageView(VKVariables.textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT, VKVariables.mipLevels);
}
public static void createTextureSampler() {
try(MemoryStack stack = stackPush()) {
VkSamplerCreateInfo samplerInfo = VkSamplerCreateInfo.callocStack(stack);
samplerInfo.sType(VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
samplerInfo.magFilter(VK_FILTER_LINEAR);
samplerInfo.minFilter(VK_FILTER_LINEAR);
samplerInfo.addressModeU(VK_SAMPLER_ADDRESS_MODE_REPEAT);
samplerInfo.addressModeV(VK_SAMPLER_ADDRESS_MODE_REPEAT);
samplerInfo.addressModeW(VK_SAMPLER_ADDRESS_MODE_REPEAT);
samplerInfo.anisotropyEnable(true);
samplerInfo.maxAnisotropy(16.0f);
samplerInfo.borderColor(VK_BORDER_COLOR_INT_OPAQUE_BLACK);
samplerInfo.unnormalizedCoordinates(false);
samplerInfo.compareEnable(false);
samplerInfo.compareOp(VK_COMPARE_OP_ALWAYS);
samplerInfo.mipmapMode(VK_SAMPLER_MIPMAP_MODE_LINEAR);
samplerInfo.minLod(0); // Optional
samplerInfo.maxLod((float) VKVariables.mipLevels);
samplerInfo.mipLodBias(0); // Optional
LongBuffer pTextureSampler = stack.mallocLong(1);
if(vkCreateSampler(VKVariables.device, samplerInfo, null, pTextureSampler) != VK_SUCCESS) {
throw new RuntimeException("Failed to create texture sampler");
}
VKVariables.textureSampler = pTextureSampler.get(0);
}
}
}

View File

@ -1,47 +0,0 @@
package com.github.hydos.ginger.engine.vulkan.misc;
import java.nio.LongBuffer;
import static org.lwjgl.system.MemoryStack.stackGet;
/**
* Wraps the needed sync objects for an in flight frame
*
* This frame's sync objects must be deleted manually
* */
public class Frame {
private final long imageAvailableSemaphore;
private final long renderFinishedSemaphore;
private final long fence;
public Frame(long imageAvailableSemaphore, long renderFinishedSemaphore, long fence) {
this.imageAvailableSemaphore = imageAvailableSemaphore;
this.renderFinishedSemaphore = renderFinishedSemaphore;
this.fence = fence;
}
public long imageAvailableSemaphore() {
return imageAvailableSemaphore;
}
public LongBuffer pImageAvailableSemaphore() {
return stackGet().longs(imageAvailableSemaphore);
}
public long renderFinishedSemaphore() {
return renderFinishedSemaphore;
}
public LongBuffer pRenderFinishedSemaphore() {
return stackGet().longs(renderFinishedSemaphore);
}
public long fence() {
return fence;
}
public LongBuffer pFence() {
return stackGet().longs(fence);
}
}

View File

@ -1,4 +1,4 @@
package com.github.hydos.ginger.engine.vulkan.misc;
package com.github.hydos.ginger.engine.vulkan.model;
import static java.util.Objects.requireNonNull;
import static org.lwjgl.assimp.Assimp.*;

View File

@ -0,0 +1,70 @@
package com.github.hydos.ginger.engine.vulkan.model;
import static org.lwjgl.vulkan.VK10.VK_FORMAT_R32G32B32_SFLOAT;
import static org.lwjgl.vulkan.VK10.VK_FORMAT_R32G32_SFLOAT;
import static org.lwjgl.vulkan.VK10.VK_VERTEX_INPUT_RATE_VERTEX;
import org.joml.Vector2fc;
import org.joml.Vector3fc;
import org.lwjgl.vulkan.VkVertexInputAttributeDescription;
import org.lwjgl.vulkan.VkVertexInputBindingDescription;
public class VKVertex {
public static final int SIZEOF = (3 + 3 + 2) * Float.BYTES;
public static final int OFFSETOF_POS = 0;
public static final int OFFSETOF_COLOR = 3 * Float.BYTES;
public static final int OFFSETOF_TEXTCOORDS = (3 + 3) * Float.BYTES;
public Vector3fc pos;
public Vector3fc color;
public Vector2fc texCoords;
public VKVertex(Vector3fc pos, Vector3fc color, Vector2fc texCoords) {
this.pos = pos;
this.color = color;
this.texCoords = texCoords;
}
public static VkVertexInputBindingDescription.Buffer getBindingDescription() {
VkVertexInputBindingDescription.Buffer bindingDescription =
VkVertexInputBindingDescription.callocStack(1);
bindingDescription.binding(0);
bindingDescription.stride(VKVertex.SIZEOF);
bindingDescription.inputRate(VK_VERTEX_INPUT_RATE_VERTEX);
return bindingDescription;
}
public static VkVertexInputAttributeDescription.Buffer getAttributeDescriptions() {
VkVertexInputAttributeDescription.Buffer attributeDescriptions =
VkVertexInputAttributeDescription.callocStack(3);
// Position
VkVertexInputAttributeDescription posDescription = attributeDescriptions.get(0);
posDescription.binding(0);
posDescription.location(0);
posDescription.format(VK_FORMAT_R32G32B32_SFLOAT);
posDescription.offset(OFFSETOF_POS);
// Color
VkVertexInputAttributeDescription colorDescription = attributeDescriptions.get(1);
colorDescription.binding(0);
colorDescription.location(1);
colorDescription.format(VK_FORMAT_R32G32B32_SFLOAT);
colorDescription.offset(OFFSETOF_COLOR);
// Texture coordinates
VkVertexInputAttributeDescription texCoordsDescription = attributeDescriptions.get(2);
texCoordsDescription.binding(0);
texCoordsDescription.location(2);
texCoordsDescription.format(VK_FORMAT_R32G32_SFLOAT);
texCoordsDescription.offset(OFFSETOF_TEXTCOORDS);
return attributeDescriptions.rewind();
}
}

View File

@ -0,0 +1,126 @@
package com.github.hydos.ginger.engine.vulkan.render;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
import java.nio.*;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.VulkanExample;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
import com.github.hydos.ginger.engine.vulkan.swapchain.VKSwapchainManager;
/**
* Wraps the needed sync objects for an in flight frame
*
* This frame's sync objects must be deleted manually
* */
public class Frame {
private final long imageAvailableSemaphore;
private final long renderFinishedSemaphore;
private final long fence;
public Frame(long imageAvailableSemaphore, long renderFinishedSemaphore, long fence) {
this.imageAvailableSemaphore = imageAvailableSemaphore;
this.renderFinishedSemaphore = renderFinishedSemaphore;
this.fence = fence;
}
public long imageAvailableSemaphore() {
return imageAvailableSemaphore;
}
public LongBuffer pImageAvailableSemaphore() {
return stackGet().longs(imageAvailableSemaphore);
}
public long renderFinishedSemaphore() {
return renderFinishedSemaphore;
}
public LongBuffer pRenderFinishedSemaphore() {
return stackGet().longs(renderFinishedSemaphore);
}
public long fence() {
return fence;
}
public LongBuffer pFence() {
return stackGet().longs(fence);
}
public static void drawFrame() {
try(MemoryStack stack = stackPush()) {
Frame thisFrame = VKVariables.inFlightFrames.get(VKVariables.currentFrame);
vkWaitForFences(VKVariables.device, thisFrame.pFence(), true, VulkanExample.UINT64_MAX);
IntBuffer pImageIndex = stack.mallocInt(1);
int vkResult = vkAcquireNextImageKHR(VKVariables.device, VKVariables.swapChain, VulkanExample.UINT64_MAX,
thisFrame.imageAvailableSemaphore(), VK_NULL_HANDLE, pImageIndex);
if(vkResult == VK_ERROR_OUT_OF_DATE_KHR) {
VKSwapchainManager.recreateSwapChain();
return;
}
final int imageIndex = pImageIndex.get(0);
VKVariables.currentImageIndex = imageIndex;
if(VKVariables.imagesInFlight.containsKey(imageIndex)) {
vkWaitForFences(VKVariables.device, VKVariables.imagesInFlight.get(imageIndex).fence(), true, VulkanExample.UINT64_MAX);
}
VKVariables.imagesInFlight.put(imageIndex, thisFrame);
VkSubmitInfo submitInfo = VkSubmitInfo.callocStack(stack);
submitInfo.sType(VK_STRUCTURE_TYPE_SUBMIT_INFO);
submitInfo.waitSemaphoreCount(1);
submitInfo.pWaitSemaphores(thisFrame.pImageAvailableSemaphore());
submitInfo.pWaitDstStageMask(stack.ints(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT));
submitInfo.pSignalSemaphores(thisFrame.pRenderFinishedSemaphore());
submitInfo.pCommandBuffers(stack.pointers(VKVariables.commandBuffers.get(imageIndex)));
vkResetFences(VKVariables.device, thisFrame.pFence());
if((vkResult = vkQueueSubmit(VKVariables.graphicsQueue, submitInfo, thisFrame.fence())) != VK_SUCCESS) {
vkResetFences(VKVariables.device, thisFrame.pFence());
throw new RuntimeException("Failed to submit draw command buffer: " + vkResult);
}
VkPresentInfoKHR presentInfo = VkPresentInfoKHR.callocStack(stack);
presentInfo.sType(VK_STRUCTURE_TYPE_PRESENT_INFO_KHR);
presentInfo.pWaitSemaphores(thisFrame.pRenderFinishedSemaphore());
presentInfo.swapchainCount(1);
presentInfo.pSwapchains(stack.longs(VKVariables.swapChain));
presentInfo.pImageIndices(pImageIndex);
vkResult = vkQueuePresentKHR(VKVariables.presentQueue, presentInfo);
if(vkResult == VK_ERROR_OUT_OF_DATE_KHR || vkResult == VK_SUBOPTIMAL_KHR || VKVariables.framebufferResize) {
VKVariables.framebufferResize = false;
VKSwapchainManager.recreateSwapChain();
} else if(vkResult != VK_SUCCESS) {
throw new RuntimeException("Failed to present swap chain image");
}
VKVariables.currentFrame = (VKVariables.currentFrame + 1) % VulkanExample.MAX_FRAMES_IN_FLIGHT;
}
}
}

View File

@ -0,0 +1,28 @@
package com.github.hydos.ginger.engine.vulkan.render;
import static org.lwjgl.vulkan.VK10.*;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
import com.github.hydos.ginger.engine.vulkan.model.VKVertex;
import com.github.hydos.ginger.engine.vulkan.model.VKModelLoader.VKMesh;
public class VKBufferMesh
{
public long vertexBuffer;
public long indexBuffer;
public VKMesh vkMesh;
public int[] indices;
public VKVertex[] vertices;
public long vertexBufferMemory;
public long indexBufferMemory;
public void cleanup() {
vkDestroyBuffer(VKVariables.device, indexBuffer, null);
vkFreeMemory(VKVariables.device, indexBufferMemory, null);
vkDestroyBuffer(VKVariables.device, vertexBuffer, null);
vkFreeMemory(VKVariables.device, vertexBufferMemory, null);
}
}

View File

@ -0,0 +1,151 @@
package com.github.hydos.ginger.engine.vulkan.render;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.vulkan.KHRSwapchain.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
import static org.lwjgl.vulkan.VK10.*;
import java.nio.LongBuffer;
import java.util.*;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.common.exceptions.GingerException;
import com.github.hydos.ginger.engine.common.render.Renderer;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
/** used to manage all the renderers and shaders to go with them
*
* @author hydos */
public class VKRenderManager
{
private static VKRenderManager instance;
public List<Renderer> renderers;
public VKRenderManager()
{
instance = this;
renderers = new ArrayList<Renderer>();
}
public void addRenderer(Renderer renderer)
{
if(renderers == null || renderers.size() == 0)
{
renderers = new ArrayList<Renderer>();
renderers.add(renderer);
}else {
for(int i = 0; i < renderers.size(); i++)
{
Renderer r = renderers.get(i);
if(r.priority < renderer.priority) {
renderers.add(i, renderer);
return;
}
}
}
}
public static VKRenderManager getInstance()
{
if (instance == null)
{ throw new GingerException("The Vulkan render manager is not setup"); }
return instance;
}
public void render(MemoryStack stack, VkCommandBuffer commandBuffer, int index)
{
for(Renderer renderer: renderers)
{
renderer.VKRender(stack, commandBuffer, index);
}
}
public static void createRenderPass() {
try(MemoryStack stack = stackPush()) {
VkAttachmentDescription.Buffer attachments = VkAttachmentDescription.callocStack(3, stack);
VkAttachmentReference.Buffer attachmentRefs = VkAttachmentReference.callocStack(3, stack);
// Color attachments
// MSAA Image
VkAttachmentDescription colorAttachment = attachments.get(0);
colorAttachment.format(VKVariables.swapChainImageFormat);
colorAttachment.samples(VKVariables.msaaSamples);
colorAttachment.loadOp(VK_ATTACHMENT_LOAD_OP_CLEAR);
colorAttachment.storeOp(VK_ATTACHMENT_STORE_OP_STORE);
colorAttachment.stencilLoadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE);
colorAttachment.stencilStoreOp(VK_ATTACHMENT_STORE_OP_DONT_CARE);
colorAttachment.initialLayout(VK_IMAGE_LAYOUT_UNDEFINED);
colorAttachment.finalLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
VkAttachmentReference colorAttachmentRef = attachmentRefs.get(0);
colorAttachmentRef.attachment(0);
colorAttachmentRef.layout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
// Present Image
VkAttachmentDescription colorAttachmentResolve = attachments.get(2);
colorAttachmentResolve.format(VKVariables.swapChainImageFormat);
colorAttachmentResolve.samples(VK_SAMPLE_COUNT_1_BIT);
colorAttachmentResolve.loadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE);
colorAttachmentResolve.storeOp(VK_ATTACHMENT_STORE_OP_STORE);
colorAttachmentResolve.stencilLoadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE);
colorAttachmentResolve.stencilStoreOp(VK_ATTACHMENT_STORE_OP_DONT_CARE);
colorAttachmentResolve.initialLayout(VK_IMAGE_LAYOUT_UNDEFINED);
colorAttachmentResolve.finalLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
VkAttachmentReference colorAttachmentResolveRef = attachmentRefs.get(2);
colorAttachmentResolveRef.attachment(2);
colorAttachmentResolveRef.layout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
// Depth-Stencil attachments
VkAttachmentDescription depthAttachment = attachments.get(1);
depthAttachment.format(VKUtils.findDepthFormat());
depthAttachment.samples(VKVariables.msaaSamples);
depthAttachment.loadOp(VK_ATTACHMENT_LOAD_OP_CLEAR);
depthAttachment.storeOp(VK_ATTACHMENT_STORE_OP_DONT_CARE);
depthAttachment.stencilLoadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE);
depthAttachment.stencilStoreOp(VK_ATTACHMENT_STORE_OP_DONT_CARE);
depthAttachment.initialLayout(VK_IMAGE_LAYOUT_UNDEFINED);
depthAttachment.finalLayout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
VkAttachmentReference depthAttachmentRef = attachmentRefs.get(1);
depthAttachmentRef.attachment(1);
depthAttachmentRef.layout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
VkSubpassDescription.Buffer subpass = VkSubpassDescription.callocStack(1, stack);
subpass.pipelineBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
subpass.colorAttachmentCount(1);
subpass.pColorAttachments(VkAttachmentReference.callocStack(1, stack).put(0, colorAttachmentRef));
subpass.pDepthStencilAttachment(depthAttachmentRef);
subpass.pResolveAttachments(VkAttachmentReference.callocStack(1, stack).put(0, colorAttachmentResolveRef));
VkSubpassDependency.Buffer dependency = VkSubpassDependency.callocStack(1, stack);
dependency.srcSubpass(VK_SUBPASS_EXTERNAL);
dependency.dstSubpass(0);
dependency.srcStageMask(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
dependency.srcAccessMask(0);
dependency.dstStageMask(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
dependency.dstAccessMask(VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
VkRenderPassCreateInfo renderPassInfo = VkRenderPassCreateInfo.callocStack(stack);
renderPassInfo.sType(VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
renderPassInfo.pAttachments(attachments);
renderPassInfo.pSubpasses(subpass);
renderPassInfo.pDependencies(dependency);
LongBuffer pRenderPass = stack.mallocLong(1);
if(vkCreateRenderPass(VKVariables.device, renderPassInfo, null, pRenderPass) != VK_SUCCESS) {
throw new RuntimeException("Failed to create render pass");
}
VKVariables.renderPass = pRenderPass.get(0);
}
}
}

View File

@ -1,16 +1,57 @@
package com.github.hydos.ginger.engine.vulkan.render.pipelines;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.vulkan.VK10.*;
import static org.lwjgl.vulkan.VK10.VK_COLOR_COMPONENT_A_BIT;
import static org.lwjgl.vulkan.VK10.VK_COLOR_COMPONENT_B_BIT;
import static org.lwjgl.vulkan.VK10.VK_COLOR_COMPONENT_G_BIT;
import static org.lwjgl.vulkan.VK10.VK_COLOR_COMPONENT_R_BIT;
import static org.lwjgl.vulkan.VK10.VK_COMPARE_OP_LESS;
import static org.lwjgl.vulkan.VK10.VK_CULL_MODE_BACK_BIT;
import static org.lwjgl.vulkan.VK10.VK_FRONT_FACE_COUNTER_CLOCKWISE;
import static org.lwjgl.vulkan.VK10.VK_LOGIC_OP_COPY;
import static org.lwjgl.vulkan.VK10.VK_NULL_HANDLE;
import static org.lwjgl.vulkan.VK10.VK_POLYGON_MODE_FILL;
import static org.lwjgl.vulkan.VK10.VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
import static org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_FRAGMENT_BIT;
import static org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_VERTEX_BIT;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_SUCCESS;
import static org.lwjgl.vulkan.VK10.vkCreateGraphicsPipelines;
import static org.lwjgl.vulkan.VK10.vkCreatePipelineLayout;
import static org.lwjgl.vulkan.VK10.vkDestroyShaderModule;
import java.nio.*;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import org.lwjgl.vulkan.VkGraphicsPipelineCreateInfo;
import org.lwjgl.vulkan.VkOffset2D;
import org.lwjgl.vulkan.VkPipelineColorBlendAttachmentState;
import org.lwjgl.vulkan.VkPipelineColorBlendStateCreateInfo;
import org.lwjgl.vulkan.VkPipelineDepthStencilStateCreateInfo;
import org.lwjgl.vulkan.VkPipelineInputAssemblyStateCreateInfo;
import org.lwjgl.vulkan.VkPipelineLayoutCreateInfo;
import org.lwjgl.vulkan.VkPipelineMultisampleStateCreateInfo;
import org.lwjgl.vulkan.VkPipelineRasterizationStateCreateInfo;
import org.lwjgl.vulkan.VkPipelineShaderStageCreateInfo;
import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo;
import org.lwjgl.vulkan.VkPipelineViewportStateCreateInfo;
import org.lwjgl.vulkan.VkRect2D;
import org.lwjgl.vulkan.VkViewport;
import com.github.hydos.ginger.VulkanExample;
import com.github.hydos.ginger.VulkanExample.VulkanDemoGinger2.Vertex;
import com.github.hydos.ginger.engine.vulkan.shaders.*;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
import com.github.hydos.ginger.engine.vulkan.model.VKVertex;
import com.github.hydos.ginger.engine.vulkan.shaders.VKShaderManager;
import com.github.hydos.ginger.engine.vulkan.shaders.VKShaderUtils;
import com.github.hydos.ginger.engine.vulkan.shaders.VKShaderUtils.SPIRV;
public class VKPipelineManager
@ -47,8 +88,8 @@ public class VKPipelineManager
VkPipelineVertexInputStateCreateInfo vertexInputInfo = VkPipelineVertexInputStateCreateInfo.callocStack(stack);
vertexInputInfo.sType(VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO);
vertexInputInfo.pVertexBindingDescriptions(Vertex.getBindingDescription());
vertexInputInfo.pVertexAttributeDescriptions(Vertex.getAttributeDescriptions());
vertexInputInfo.pVertexBindingDescriptions(VKVertex.getBindingDescription());
vertexInputInfo.pVertexAttributeDescriptions(VKVertex.getAttributeDescriptions());
// ASSEMBLY STAGE
@ -62,14 +103,14 @@ public class VKPipelineManager
VkViewport.Buffer viewport = VkViewport.callocStack(1, stack);
viewport.x(0.0f);
viewport.y(0.0f);
viewport.width(VulkanExample.VulkanDemoGinger2.swapChainExtent.width());
viewport.height(VulkanExample.VulkanDemoGinger2.swapChainExtent.height());
viewport.width(VKVariables .swapChainExtent.width());
viewport.height(VKVariables.swapChainExtent.height());
viewport.minDepth(0.0f);
viewport.maxDepth(1.0f);
VkRect2D.Buffer scissor = VkRect2D.callocStack(1, stack);
scissor.offset(VkOffset2D.callocStack(stack).set(0, 0));
scissor.extent(VulkanExample.VulkanDemoGinger2.swapChainExtent);
scissor.extent(VKVariables.swapChainExtent);
VkPipelineViewportStateCreateInfo viewportState = VkPipelineViewportStateCreateInfo.callocStack(stack);
viewportState.sType(VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO);
@ -94,7 +135,7 @@ public class VKPipelineManager
multisampling.sType(VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO);
multisampling.sampleShadingEnable(true);
multisampling.minSampleShading(0.2f); // Enable sample shading in the pipeline
multisampling.rasterizationSamples(VulkanExample.VulkanDemoGinger2.msaaSamples); // Min fraction for sample shading; closer to one is smoother
multisampling.rasterizationSamples(VKVariables.msaaSamples); // Min fraction for sample shading; closer to one is smoother
VkPipelineDepthStencilStateCreateInfo depthStencil = VkPipelineDepthStencilStateCreateInfo.callocStack(stack);
depthStencil.sType(VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO);
@ -123,15 +164,15 @@ public class VKPipelineManager
VkPipelineLayoutCreateInfo pipelineLayoutInfo = VkPipelineLayoutCreateInfo.callocStack(stack);
pipelineLayoutInfo.sType(VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
pipelineLayoutInfo.pSetLayouts(stack.longs(VulkanExample.VulkanDemoGinger2.descriptorSetLayout));
pipelineLayoutInfo.pSetLayouts(stack.longs(VKVariables.descriptorSetLayout));
LongBuffer pPipelineLayout = stack.longs(VK_NULL_HANDLE);
if(vkCreatePipelineLayout(VulkanExample.VulkanDemoGinger2.device, pipelineLayoutInfo, null, pPipelineLayout) != VK_SUCCESS) {
if(vkCreatePipelineLayout(VKVariables.device, pipelineLayoutInfo, null, pPipelineLayout) != VK_SUCCESS) {
throw new RuntimeException("Failed to create pipeline layout");
}
VulkanExample.VulkanDemoGinger2.pipelineLayout = pPipelineLayout.get(0);
VKVariables.pipelineLayout = pPipelineLayout.get(0);
VkGraphicsPipelineCreateInfo.Buffer pipelineInfo = VkGraphicsPipelineCreateInfo.callocStack(1, stack);
pipelineInfo.sType(VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
@ -143,24 +184,24 @@ public class VKPipelineManager
pipelineInfo.pMultisampleState(multisampling);
pipelineInfo.pDepthStencilState(depthStencil);
pipelineInfo.pColorBlendState(colorBlending);
pipelineInfo.layout(VulkanExample.VulkanDemoGinger2.pipelineLayout);
pipelineInfo.renderPass(VulkanExample.VulkanDemoGinger2.renderPass);
pipelineInfo.layout(VKVariables.pipelineLayout);
pipelineInfo.renderPass(VKVariables.renderPass);
pipelineInfo.subpass(0);
pipelineInfo.basePipelineHandle(VK_NULL_HANDLE);
pipelineInfo.basePipelineIndex(-1);
LongBuffer pGraphicsPipeline = stack.mallocLong(1);
if(vkCreateGraphicsPipelines(VulkanExample.VulkanDemoGinger2.device, VK_NULL_HANDLE, pipelineInfo, null, pGraphicsPipeline) != VK_SUCCESS) {
if(vkCreateGraphicsPipelines(VKVariables.device, VK_NULL_HANDLE, pipelineInfo, null, pGraphicsPipeline) != VK_SUCCESS) {
throw new RuntimeException("Failed to create graphics pipeline");
}
VulkanExample.VulkanDemoGinger2.graphicsPipeline = pGraphicsPipeline.get(0);
VKVariables.graphicsPipeline = pGraphicsPipeline.get(0);
// Cleanup
vkDestroyShaderModule(VulkanExample.VulkanDemoGinger2.device, vertShaderModule, null);
vkDestroyShaderModule(VulkanExample.VulkanDemoGinger2.device, fragShaderModule, null);
vkDestroyShaderModule(VKVariables.device, vertShaderModule, null);
vkDestroyShaderModule(VKVariables.device, fragShaderModule, null);
vertShaderSPIRV.free();
fragShaderSPIRV.free();

View File

@ -0,0 +1,87 @@
package com.github.hydos.ginger.engine.vulkan.render.renderers;
import static org.lwjgl.vulkan.VK10.*;
import java.nio.LongBuffer;
import java.util.*;
import org.joml.Vector3f;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkCommandBuffer;
import com.github.hydos.ginger.engine.common.render.Renderer;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
import com.github.hydos.ginger.engine.vulkan.elements.VKRenderObject;
import com.github.hydos.ginger.engine.vulkan.model.VKVertex;
import com.github.hydos.ginger.engine.vulkan.model.VKModelLoader.VKMesh;
import com.github.hydos.ginger.engine.vulkan.render.VKBufferMesh;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
public class EntityRenderer extends Renderer
{
public List<VKRenderObject> entities;//TODO: batch rendering
public EntityRenderer() {
priority = 1;
entities = new ArrayList<VKRenderObject>();
}
public void processEntity(VKRenderObject entity) {
VKMesh mesh = entity.getRawModel();
VKBufferMesh processedMesh = new VKBufferMesh();
processedMesh.vkMesh = mesh;
int vertexCount = mesh.positions.size();
processedMesh.vertices = new VKVertex[vertexCount];
Vector3f color = new Vector3f(1.0f, 1.0f, 1.0f);
for(int i = 0;i < vertexCount;i++) {
processedMesh.vertices[i] = new VKVertex(
mesh.positions.get(i),
color,
mesh.texCoords.get(i));
}
processedMesh.indices = new int[mesh.indices.size()];
for(int i = 0;i < processedMesh.indices.length;i++) {
processedMesh.indices[i] = mesh.indices.get(i);
}
processedMesh = VKUtils.createVertexBuffer(processedMesh);
processedMesh = VKUtils.createIndexBuffer(processedMesh);
entity.setModel(processedMesh);
entities.add(entity);
}
@Override
public void VKRender(MemoryStack stack, VkCommandBuffer commandBuffer, int index)
{
for(VKRenderObject entity : entities)
{
VKBufferMesh mesh = entity.getModel();
VKUtils.updateUniformBuffer(VKVariables.currentImageIndex, entity);//TODO: move this to entitiy renderer and update before every entity is drawn
LongBuffer vertexBuffers = stack.longs(mesh.vertexBuffer);
LongBuffer offsets = stack.longs(0);
vkCmdBindVertexBuffers(commandBuffer, 0, vertexBuffers, offsets);
vkCmdBindIndexBuffer(commandBuffer, mesh.indexBuffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
VKVariables.pipelineLayout,
0, stack.longs(
VKVariables.descriptorSets.get(index)
),
null);
vkCmdDrawIndexed(commandBuffer, mesh.vkMesh.indices.size(), 1, 0, 0, 0);
}
}
}

View File

@ -1,9 +0,0 @@
package com.github.hydos.ginger.engine.vulkan.render.renderers;
/**
* used to manage all the renderers and shaders to go with them
* @author hydos
*
*/
public class VKRenderManager
{
}

View File

@ -1,14 +1,17 @@
package com.github.hydos.ginger.engine.vulkan.shaders;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.vulkan.VK10.*;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_SUCCESS;
import static org.lwjgl.vulkan.VK10.vkCreateShaderModule;
import java.nio.*;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkShaderModuleCreateInfo;
import com.github.hydos.ginger.VulkanExample;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
/**
* will be used in the future to manage multiple shaders
@ -28,7 +31,7 @@ public class VKShaderManager
LongBuffer pShaderModule = stack.mallocLong(1);
if(vkCreateShaderModule(VulkanExample.VulkanDemoGinger2.device, createInfo, null, pShaderModule) != VK_SUCCESS) {
if(vkCreateShaderModule(VKVariables.device, createInfo, null, pShaderModule) != VK_SUCCESS) {
throw new RuntimeException("Failed to create shader module");
}

View File

@ -12,42 +12,45 @@ import java.util.ArrayList;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.VulkanExample.VulkanDemoGinger2;
import com.github.hydos.ginger.VulkanExample.VulkanDemoGinger2.*;
import com.github.hydos.ginger.VulkanExample.*;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
import com.github.hydos.ginger.engine.vulkan.managers.CommandBufferManager;
import com.github.hydos.ginger.engine.vulkan.render.VKRenderManager;
import com.github.hydos.ginger.engine.vulkan.render.pipelines.VKPipelineManager;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
public class VKSwapchainManager
{
public static void cleanupSwapChain() {
vkDestroyImageView(VulkanDemoGinger2.device, VulkanDemoGinger2.colorImageView, null);
vkDestroyImage(VulkanDemoGinger2.device, VulkanDemoGinger2.colorImage, null);
vkFreeMemory(VulkanDemoGinger2.device, VulkanDemoGinger2.colorImageMemory, null);
vkDestroyImageView(VKVariables.device, VKVariables.colorImageView, null);
vkDestroyImage(VKVariables.device, VKVariables.colorImage, null);
vkFreeMemory(VKVariables.device, VKVariables.colorImageMemory, null);
vkDestroyImageView(VulkanDemoGinger2.device, VulkanDemoGinger2.depthImageView, null);
vkDestroyImage(VulkanDemoGinger2.device, VulkanDemoGinger2.depthImage, null);
vkFreeMemory(VulkanDemoGinger2.device, VulkanDemoGinger2.depthImageMemory, null);
vkDestroyImageView(VKVariables.device, VKVariables.depthImageView, null);
vkDestroyImage(VKVariables.device, VKVariables.depthImage, null);
vkFreeMemory(VKVariables.device, VKVariables.depthImageMemory, null);
VulkanDemoGinger2.uniformBuffers.forEach(ubo -> vkDestroyBuffer(VulkanDemoGinger2.device, ubo, null));
VulkanDemoGinger2.uniformBuffersMemory.forEach(uboMemory -> vkFreeMemory(VulkanDemoGinger2.device, uboMemory, null));
VKVariables.uniformBuffers.forEach(ubo -> vkDestroyBuffer(VKVariables.device, ubo, null));
VKVariables.uniformBuffersMemory.forEach(uboMemory -> vkFreeMemory(VKVariables.device, uboMemory, null));
vkDestroyDescriptorPool(VulkanDemoGinger2.device, VulkanDemoGinger2.descriptorPool, null);
vkDestroyDescriptorPool(VKVariables.device, VKVariables.descriptorPool, null);
VulkanDemoGinger2.swapChainFramebuffers.forEach(framebuffer -> vkDestroyFramebuffer(VulkanDemoGinger2.device, framebuffer, null));
VKVariables.swapChainFramebuffers.forEach(framebuffer -> vkDestroyFramebuffer(VKVariables.device, framebuffer, null));
vkFreeCommandBuffers(VulkanDemoGinger2.device, VulkanDemoGinger2.commandPool, VulkanDemoGinger2.asPointerBuffer(VulkanDemoGinger2.commandBuffers));
vkFreeCommandBuffers(VKVariables.device, VKVariables.commandPool, VKUtils.asPointerBuffer(VKVariables.commandBuffers));
vkDestroyPipeline(VulkanDemoGinger2.device, VulkanDemoGinger2.graphicsPipeline, null);
vkDestroyPipeline(VKVariables.device, VKVariables.graphicsPipeline, null);
vkDestroyPipelineLayout(VulkanDemoGinger2.device, VulkanDemoGinger2.pipelineLayout, null);
vkDestroyPipelineLayout(VKVariables.device, VKVariables.pipelineLayout, null);
vkDestroyRenderPass(VulkanDemoGinger2.device, VulkanDemoGinger2.renderPass, null);
vkDestroyRenderPass(VKVariables.device, VKVariables.renderPass, null);
VulkanDemoGinger2.swapChainImageViews.forEach(imageView -> vkDestroyImageView(VulkanDemoGinger2.device, imageView, null));
VKVariables.swapChainImageViews.forEach(imageView -> vkDestroyImageView(VKVariables.device, imageView, null));
vkDestroySwapchainKHR(VulkanDemoGinger2.device, VulkanDemoGinger2.swapChain, null);
vkDestroySwapchainKHR(VKVariables.device, VKVariables.swapChain, null);
}
public static void recreateSwapChain() {
@ -63,7 +66,7 @@ public class VKSwapchainManager
}
}
vkDeviceWaitIdle(VulkanDemoGinger2.device);
vkDeviceWaitIdle(VKVariables.device);
VKSwapchainManager.cleanupSwapChain();
@ -74,11 +77,11 @@ public class VKSwapchainManager
try(MemoryStack stack = stackPush()) {
SwapChainSupportDetails swapChainSupport = VulkanDemoGinger2.querySwapChainSupport(VulkanDemoGinger2.physicalDevice, stack);
SwapChainSupportDetails swapChainSupport = VKUtils.querySwapChainSupport(VKVariables.physicalDevice, stack);
VkSurfaceFormatKHR surfaceFormat = VulkanDemoGinger2.chooseSwapSurfaceFormat(swapChainSupport.formats);
int presentMode = VulkanDemoGinger2.chooseSwapPresentMode(swapChainSupport.presentModes);
VkExtent2D extent = VulkanDemoGinger2.chooseSwapExtent(swapChainSupport.capabilities);
VkSurfaceFormatKHR surfaceFormat = VKUtils.chooseSwapSurfaceFormat(swapChainSupport.formats);
int presentMode = VKUtils.chooseSwapPresentMode(swapChainSupport.presentModes);
VkExtent2D extent = VKUtils.chooseSwapExtent(swapChainSupport.capabilities);
IntBuffer imageCount = stack.ints(swapChainSupport.capabilities.minImageCount() + 1);
@ -89,7 +92,7 @@ public class VKSwapchainManager
VkSwapchainCreateInfoKHR createInfo = VkSwapchainCreateInfoKHR.callocStack(stack);
createInfo.sType(VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
createInfo.surface(VulkanDemoGinger2.surface);
createInfo.surface(VKVariables.surface);
// Image settings
createInfo.minImageCount(imageCount.get(0));
@ -99,7 +102,7 @@ public class VKSwapchainManager
createInfo.imageArrayLayers(1);
createInfo.imageUsage(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
QueueFamilyIndices indices = VulkanDemoGinger2.findQueueFamilies(VulkanDemoGinger2.physicalDevice);
QueueFamilyIndices indices = VKUtils.findQueueFamilies(VKVariables.physicalDevice);
if(!indices.graphicsFamily.equals(indices.presentFamily)) {
createInfo.imageSharingMode(VK_SHARING_MODE_CONCURRENT);
@ -117,26 +120,26 @@ public class VKSwapchainManager
LongBuffer pSwapChain = stack.longs(VK_NULL_HANDLE);
if(vkCreateSwapchainKHR(VulkanDemoGinger2.device, createInfo, null, pSwapChain) != VK_SUCCESS) {
if(vkCreateSwapchainKHR(VKVariables.device, createInfo, null, pSwapChain) != VK_SUCCESS) {
throw new RuntimeException("Failed to create swap chain");
}
VulkanDemoGinger2.swapChain = pSwapChain.get(0);
VKVariables.swapChain = pSwapChain.get(0);
vkGetSwapchainImagesKHR(VulkanDemoGinger2.device, VulkanDemoGinger2.swapChain, imageCount, null);
vkGetSwapchainImagesKHR(VKVariables.device, VKVariables.swapChain, imageCount, null);
LongBuffer pSwapchainImages = stack.mallocLong(imageCount.get(0));
vkGetSwapchainImagesKHR(VulkanDemoGinger2.device, VulkanDemoGinger2.swapChain, imageCount, pSwapchainImages);
vkGetSwapchainImagesKHR(VKVariables.device, VKVariables.swapChain, imageCount, pSwapchainImages);
VulkanDemoGinger2.swapChainImages = new ArrayList<>(imageCount.get(0));
VKVariables.swapChainImages = new ArrayList<>(imageCount.get(0));
for(int i = 0;i < pSwapchainImages.capacity();i++) {
VulkanDemoGinger2.swapChainImages.add(pSwapchainImages.get(i));
VKVariables.swapChainImages.add(pSwapchainImages.get(i));
}
VulkanDemoGinger2.swapChainImageFormat = surfaceFormat.format();
VulkanDemoGinger2.swapChainExtent = VkExtent2D.create().set(extent);
VKVariables.swapChainImageFormat = surfaceFormat.format();
VKVariables.swapChainExtent = VkExtent2D.create().set(extent);
}
}
@ -146,16 +149,16 @@ public class VKSwapchainManager
*/
public static void createSwapChainObjects() {
createSwapChain();
VulkanDemoGinger2.createImageViews();
VulkanDemoGinger2.createRenderPass();
VKUtils.createImageViews();
VKRenderManager.createRenderPass();
VKPipelineManager.createGraphicsPipeline();
VulkanDemoGinger2.createColorResources();
VulkanDemoGinger2.createDepthResources();
VulkanDemoGinger2.createFramebuffers();
VulkanDemoGinger2.createUniformBuffers();
VulkanDemoGinger2.createDescriptorPool();
VulkanDemoGinger2.createDescriptorSets();
VulkanDemoGinger2.createCommandBuffers();
VKUtils.createColorResources();
VKUtils.createDepthResources();
VKUtils.createFramebuffers();
VKUtils.createUniformBuffers();
VKUtils.createDescriptorPool();
VKUtils.createDescriptorSets();
CommandBufferManager.createCommandBuffers();
}
}

View File

@ -1,4 +1,4 @@
package com.github.hydos.ginger.engine.vulkan.misc;
package com.github.hydos.ginger.engine.vulkan.utils;
import java.util.*;

View File

@ -0,0 +1,62 @@
package com.github.hydos.ginger.engine.vulkan.utils;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.vulkan.VK10.*;
import java.nio.LongBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
import com.github.hydos.ginger.engine.vulkan.managers.CommandBufferManager;
public class VKBufferUtils
{
public static void createBuffer(long size, int usage, int properties, LongBuffer pBuffer, LongBuffer pBufferMemory) {
try(MemoryStack stack = stackPush()) {
VkBufferCreateInfo bufferInfo = VkBufferCreateInfo.callocStack(stack);
bufferInfo.sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
bufferInfo.size(size);
bufferInfo.usage(usage);
bufferInfo.sharingMode(VK_SHARING_MODE_EXCLUSIVE);
if(vkCreateBuffer(VKVariables.device, bufferInfo, null, pBuffer) != VK_SUCCESS) {
throw new RuntimeException("Failed to create vertex buffer");
}
VkMemoryRequirements memRequirements = VkMemoryRequirements.mallocStack(stack);
vkGetBufferMemoryRequirements(VKVariables.device, pBuffer.get(0), memRequirements);
VkMemoryAllocateInfo allocInfo = VkMemoryAllocateInfo.callocStack(stack);
allocInfo.sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
allocInfo.allocationSize(memRequirements.size());
allocInfo.memoryTypeIndex(VKUtils.findMemoryType(memRequirements.memoryTypeBits(), properties));
if(vkAllocateMemory(VKVariables.device, allocInfo, null, pBufferMemory) != VK_SUCCESS) {
throw new RuntimeException("Failed to allocate vertex buffer memory");
}
vkBindBufferMemory(VKVariables.device, pBuffer.get(0), pBufferMemory.get(0), 0);
}
}
public static void copyBuffer(long srcBuffer, long dstBuffer, long size) {
try(MemoryStack stack = stackPush()) {
VkCommandBuffer commandBuffer = CommandBufferManager.beginSingleTimeCommands();
VkBufferCopy.Buffer copyRegion = VkBufferCopy.callocStack(1, stack);
copyRegion.size(size);
vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, copyRegion);
CommandBufferManager.endSingleTimeCommands(commandBuffer);
}
}
}

View File

@ -0,0 +1,139 @@
package com.github.hydos.ginger.engine.vulkan.utils;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.vulkan.VK10.VK_NULL_HANDLE;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_SUCCESS;
import static org.lwjgl.vulkan.VK10.vkCreateDevice;
import static org.lwjgl.vulkan.VK10.vkEnumeratePhysicalDevices;
import static org.lwjgl.vulkan.VK10.vkGetDeviceQueue;
import static org.lwjgl.vulkan.VK10.vkGetPhysicalDeviceFeatures;
import java.nio.IntBuffer;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkDevice;
import org.lwjgl.vulkan.VkDeviceCreateInfo;
import org.lwjgl.vulkan.VkDeviceQueueCreateInfo;
import org.lwjgl.vulkan.VkPhysicalDevice;
import org.lwjgl.vulkan.VkPhysicalDeviceFeatures;
import org.lwjgl.vulkan.VkQueue;
import com.github.hydos.ginger.VulkanExample;
import com.github.hydos.ginger.VulkanExample.QueueFamilyIndices;
import com.github.hydos.ginger.VulkanExample.SwapChainSupportDetails;
import com.github.hydos.ginger.engine.vulkan.VKVariables;
public class VKDeviceManager
{
public static void createLogicalDevice() {
try(MemoryStack stack = stackPush()) {
QueueFamilyIndices indices = VKUtils.findQueueFamilies(VKVariables.physicalDevice);
int[] uniqueQueueFamilies = indices.unique();
VkDeviceQueueCreateInfo.Buffer queueCreateInfos = VkDeviceQueueCreateInfo.callocStack(uniqueQueueFamilies.length, stack);
for(int i = 0;i < uniqueQueueFamilies.length;i++) {
VkDeviceQueueCreateInfo queueCreateInfo = queueCreateInfos.get(i);
queueCreateInfo.sType(VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO);
queueCreateInfo.queueFamilyIndex(uniqueQueueFamilies[i]);
queueCreateInfo.pQueuePriorities(stack.floats(1.0f));
}
VkPhysicalDeviceFeatures deviceFeatures = VkPhysicalDeviceFeatures.callocStack(stack);
deviceFeatures.samplerAnisotropy(true);
deviceFeatures.sampleRateShading(true); // Enable sample shading feature for the device
VkDeviceCreateInfo createInfo = VkDeviceCreateInfo.callocStack(stack);
createInfo.sType(VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
createInfo.pQueueCreateInfos(queueCreateInfos);
// queueCreateInfoCount is automatically set
createInfo.pEnabledFeatures(deviceFeatures);
createInfo.ppEnabledExtensionNames(VKUtils.asPointerBuffer(VulkanExample.DEVICE_EXTENSIONS));
PointerBuffer pDevice = stack.pointers(VK_NULL_HANDLE);
if(vkCreateDevice(VKVariables.physicalDevice, createInfo, null, pDevice) != VK_SUCCESS) {
throw new RuntimeException("Failed to create logical device");
}
VKVariables.device = new VkDevice(pDevice.get(0), VKVariables.physicalDevice, createInfo);
PointerBuffer pQueue = stack.pointers(VK_NULL_HANDLE);
vkGetDeviceQueue(VKVariables.device, indices.graphicsFamily, 0, pQueue);
VKVariables.graphicsQueue = new VkQueue(pQueue.get(0), VKVariables.device);
vkGetDeviceQueue(VKVariables.device, indices.presentFamily, 0, pQueue);
VKVariables.presentQueue = new VkQueue(pQueue.get(0), VKVariables.device);
}
}
public static void pickPhysicalDevice()
{
try(MemoryStack stack = stackPush()) {
IntBuffer deviceCount = stack.ints(0);
vkEnumeratePhysicalDevices(VKVariables.instance, deviceCount, null);
if(deviceCount.get(0) == 0) {
throw new RuntimeException("Failed to find GPUs with Vulkan support");
}
PointerBuffer ppPhysicalDevices = stack.mallocPointer(deviceCount.get(0));
vkEnumeratePhysicalDevices(VKVariables.instance, deviceCount, ppPhysicalDevices);
VkPhysicalDevice device = null;
for(int i = 0;i < ppPhysicalDevices.capacity();i++) {
device = new VkPhysicalDevice(ppPhysicalDevices.get(i), VKVariables.instance);
if(isDeviceSuitable(device)) {
break;
}
}
if(device == null) {
throw new RuntimeException("Failed to find a suitable GPU");
}
VKVariables.physicalDevice = device;
VKVariables.msaaSamples = VKUtils.getMaxUsableSampleCount();
}
}
private static boolean isDeviceSuitable(VkPhysicalDevice device) {
QueueFamilyIndices indices = VKUtils.findQueueFamilies(device);
boolean extensionsSupported = VKUtils.checkDeviceExtensionSupport(device);
boolean swapChainAdequate = false;
boolean anisotropySupported = false;
if(extensionsSupported) {
try(MemoryStack stack = stackPush()) {
SwapChainSupportDetails swapChainSupport = VKUtils.querySwapChainSupport(device, stack);
swapChainAdequate = swapChainSupport.formats.hasRemaining() && swapChainSupport.presentModes.hasRemaining();
VkPhysicalDeviceFeatures supportedFeatures = VkPhysicalDeviceFeatures.mallocStack(stack);
vkGetPhysicalDeviceFeatures(device, supportedFeatures);
anisotropySupported = supportedFeatures.samplerAnisotropy();
}
}
return indices.isComplete() && extensionsSupported && swapChainAdequate && anisotropySupported;
}
}

View File

@ -0,0 +1,5 @@
package com.github.hydos.ginger.engine.vulkan.utils;
public class VKMath
{
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Material
Ns 323.999994
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2

View File

@ -0,0 +1,46 @@
# Blender v2.82 (sub 7) OBJ File: ''
# www.blender.org
mtllib block.mtl
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
vt 0.875000 0.500000
vt 0.625000 0.750000
vt 0.625000 0.500000
vt 0.375000 1.000000
vt 0.375000 0.750000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.375000 0.000000
vt 0.375000 0.500000
vt 0.125000 0.750000
vt 0.125000 0.500000
vt 0.625000 0.250000
vt 0.875000 0.750000
vt 0.625000 1.000000
vn 0.0000 1.0000 0.0000
vn 0.0000 0.0000 1.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 -1.0000 0.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
usemtl Material
s off
f 5/1/1 3/2/1 1/3/1
f 3/2/2 8/4/2 4/5/2
f 7/6/3 6/7/3 8/8/3
f 2/9/4 8/10/4 6/11/4
f 1/3/5 4/5/5 2/9/5
f 5/12/6 2/9/6 6/7/6
f 5/1/1 7/13/1 3/2/1
f 3/2/2 7/14/2 8/4/2
f 7/6/3 5/12/3 6/7/3
f 2/9/4 4/5/4 8/10/4
f 1/3/5 3/2/5 4/5/5
f 5/12/6 1/3/6 2/9/6