1 module polyplex.core.render; 2 public import polyplex.core.render.camera; 3 public import polyplex.core.render.shapes; 4 5 import polyplex.core.render.gl; 6 import polyplex.core.render.vk; 7 import polyplex.core.render.simplefont; 8 import polyplex.core.content; 9 import polyplex.core.color; 10 import polyplex.core.content; 11 import polyplex.core.window; 12 import polyplex.math; 13 import derelict.sdl2.sdl; 14 import polyplex.math; 15 import polyplex; 16 import std.stdio; 17 18 public enum VSyncState { 19 LateTearing = -1, 20 Immidiate = 0, 21 VSync = 1 22 } 23 24 public class Renderer { 25 public SpriteBatch Batch; 26 public GameWindow Window; 27 28 this(GameWindow parent) { 29 this.Window = parent; 30 } 31 32 public abstract void Init(SDL_Window* win); 33 public abstract void ClearColor(Color color); 34 public abstract void ClearDepth(); 35 public abstract void SwapBuffers(); 36 public abstract Shader CreateShader(ShaderCode code); 37 38 public abstract void AdjustViewport(); 39 40 public abstract @property VSyncState VSync(); 41 public abstract @property void VSync(VSyncState state); 42 } 43 44 public enum Blending { 45 Opqaue, 46 AlphaBlend, 47 NonPremultiplied, 48 Additive 49 } 50 51 public enum ProjectionState { 52 Orthographic, 53 Perspective 54 } 55 56 public enum Sampling { 57 AnisotropicClamp, 58 AnisotropicWrap, 59 LinearClamp, 60 LinearWrap, 61 PointClamp, 62 PointWrap 63 } 64 65 public enum SpriteSorting { 66 BackToFront, 67 Deferred, 68 FrontToBack, 69 Immediate, 70 Texture 71 } 72 /* 73 public enum Stencil { 74 Default, 75 DepthRead, 76 None 77 }*/ 78 79 public enum SpriteFlip { 80 None = 0x0, 81 FlipVertical = 0x1, 82 FlipHorizontal = 0x2 83 } 84 85 public abstract class SpriteBatch { 86 public abstract void Begin(); 87 public abstract void Begin(SpriteSorting sort_mode, Blending blend_state, Sampling sample_State, Shader s, Matrix4x4 matrix); 88 public abstract void Begin(SpriteSorting sort_mode, Blending blend_state, Sampling sample_State, Shader s, Camera camera); 89 public abstract void Begin(SpriteSorting sort_mode, Blending blend_state, Sampling sample_State, ProjectionState pstate, Shader s, Camera camera); 90 public abstract void Draw(Texture2D texture, Rectangle pos, Rectangle cutout, Color color, SpriteFlip flip = SpriteFlip.None, float zlayer = 0f); 91 public abstract void Draw(Texture2D texture, Rectangle pos, Rectangle cutout, float rotation, Vector2 Origin, Color color, SpriteFlip flip = SpriteFlip.None, float zlayer = 0f); 92 public abstract void DrawAABB(Texture2D texture, Rectangle pos_top, Rectangle pos_bottom, Rectangle cutout, Vector2 Origin, Color color, SpriteFlip flip = SpriteFlip.None, float zlayer = 0f); 93 94 public abstract void Flush(); 95 public abstract void SwapChain(); 96 public abstract void End(); 97 } 98 99 public abstract class Shader { 100 public abstract @property bool Attached(); 101 102 public abstract void Attach(); 103 public abstract void Detach(); 104 public abstract void SetUniform(int location, float value); 105 public abstract void SetUniform(int location, Vector2 value); 106 public abstract void SetUniform(int location, Vector3 value); 107 public abstract void SetUniform(int location, Vector4 value); 108 public abstract void SetUniform(int location, int value); 109 public abstract void SetUniform(int location, Vector2i value); 110 public abstract void SetUniform(int location, Vector3i value); 111 public abstract void SetUniform(int location, Vector4i value); 112 public abstract void SetUniform(int location, Matrix2x2 value); 113 public abstract void SetUniform(int location, Matrix3x3 value); 114 public abstract void SetUniform(int location, Matrix4x4 value); 115 public abstract uint GetUniform(string name); 116 public abstract bool HasUniform(string name); 117 118 } 119 120 enum ShaderType { 121 Vertex, 122 Geometry, 123 Fragment, 124 125 Stencil, 126 Compute 127 } 128 129 enum ShaderLang { 130 PPSL, 131 GLSL, 132 133 //For eventual Direct X version? 134 //Would be added in PPSL language as support aswell. 135 CG 136 } 137 138 class ShaderCode { 139 private ShaderLang language; 140 141 public @property ShaderLang Language() { return language; } 142 public string Vertex; 143 public string Fragment; 144 public string Geometry; 145 public string[] Attributes; 146 147 this() {} 148 149 this(string vertex, string fragment, string[] attribs) { 150 this.Vertex = vertex; 151 language = ShaderLang.GLSL; 152 if (this.Vertex[0..5] == "shader") { 153 language = ShaderLang.PPSL; 154 } 155 this.Fragment = fragment; 156 this.Attributes = attribs; 157 } 158 159 this(string vertex, string geometry, string fragment, string[] attribs) { 160 this.Vertex = vertex; 161 this.Geometry = geometry; 162 language = ShaderLang.GLSL; 163 if (this.Vertex[0..5] == "shader") { 164 language = ShaderLang.PPSL; 165 } 166 this.Fragment = fragment; 167 this.Attributes = attribs; 168 } 169 } 170 171 public Renderer CreateBackendRenderer(GameWindow parent) { 172 if (ChosenBackend == GraphicsBackend.Vulkan) return new VkRenderer(parent); 173 return new GlRenderer(parent); 174 }