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.simplefont; 7 import polyplex.core.content; 8 import polyplex.core.color; 9 import polyplex.core.content; 10 static import win = polyplex.core.window; 11 import polyplex.math; 12 import bindbc.sdl; 13 import polyplex.math; 14 import polyplex; 15 import std.stdio; 16 17 public enum VSyncState { 18 LateTearing = -1, 19 Immidiate = 0, 20 VSync = 1 21 } 22 23 public class Renderer { 24 private: 25 static BackendRenderer BGRend; 26 27 public: 28 static win.Window Window; 29 30 /** 31 Backend function, run automatically, no need to invoke it manually. c: 32 */ 33 static void AssignRenderer(win.Window parent) { 34 BGRend = CreateBackendRenderer(parent); 35 BGRend.Init(); 36 Window = parent; 37 } 38 39 static void ClearColor(Color color) { 40 BGRend.ClearColor(color); 41 } 42 43 static void ClearDepth() { 44 BGRend.ClearDepth(); 45 } 46 47 static void SwapBuffers() { 48 BGRend.SwapBuffers(); 49 } 50 51 static Shader CreateShader(ShaderCode code) { 52 return BGRend.CreateShader(code); 53 } 54 55 static void AdjustViewport() { 56 BGRend.AdjustViewport(); 57 } 58 59 static @property VSyncState VSync() { 60 return BGRend.VSync; 61 } 62 63 static @property void VSync(VSyncState state) { 64 BGRend.VSync = state; 65 } 66 67 static @property Rectangle ScissorRectangle() { 68 return BGRend.ScissorRectangle; 69 } 70 71 static @property void ScissorRectangle(Rectangle rect) { 72 BGRend.ScissorRectangle = rect; 73 } 74 75 static SpriteBatch NewBatcher() { 76 return new GlSpriteBatch(); 77 } 78 79 } 80 81 public class BackendRenderer { 82 public: 83 SpriteBatch Batch; 84 win.Window Window; 85 86 this(win.Window parent) { 87 this.Window = parent; 88 } 89 90 abstract void Init(); 91 abstract void ClearColor(Color color); 92 abstract void ClearDepth(); 93 abstract void SwapBuffers(); 94 abstract Shader CreateShader(ShaderCode code); 95 96 abstract void AdjustViewport(); 97 98 abstract @property VSyncState VSync(); 99 abstract @property void VSync(VSyncState state); 100 101 abstract @property Rectangle ScissorRectangle(); 102 abstract @property void ScissorRectangle(Rectangle rect); 103 } 104 105 public enum Blending { 106 Opqaue, 107 AlphaBlend, 108 NonPremultiplied, 109 Additive 110 } 111 112 public enum ProjectionState { 113 Orthographic, 114 Perspective 115 } 116 117 public enum Sampling { 118 AnisotropicClamp, 119 AnisotropicWrap, 120 AnisotropicMirror, 121 LinearClamp, 122 LinearWrap, 123 LinearMirror, 124 PointClamp, 125 PointMirror, 126 PointWrap 127 } 128 129 public enum SpriteSorting { 130 BackToFront, 131 Deferred, 132 FrontToBack, 133 Immediate, 134 Texture 135 } 136 137 public struct RasterizerState { 138 public: 139 static RasterizerState Default() { 140 return RasterizerState(false, false, 0f); 141 } 142 143 bool ScissorTest; 144 bool MSAA; 145 float SlopeScaleBias; 146 } 147 148 /* 149 public enum Stencil { 150 Default, 151 DepthRead, 152 None 153 }*/ 154 155 public enum SpriteFlip { 156 None = 0x0, 157 FlipVertical = 0x1, 158 FlipHorizontal = 0x2 159 } 160 161 public abstract class SpriteBatch { 162 public: 163 bool OffsetOrigin = true; 164 abstract Matrix4x4 MultMatrices(); 165 abstract void Begin(); 166 abstract void Begin(SpriteSorting sort_mode, Blending blend_state, Sampling sample_state, RasterizerState raster_state, Shader s, Matrix4x4 matrix); 167 abstract void Begin(SpriteSorting sort_mode, Blending blend_state, Sampling sample_State, RasterizerState raster_state, Shader s, Camera camera); 168 abstract void Begin(SpriteSorting sort_mode, Blending blend_state, Sampling sample_State, RasterizerState raster_state, ProjectionState pstate, Shader s, Camera camera); 169 abstract void Draw(Texture2D texture, Rectangle pos, Rectangle cutout, Color color, SpriteFlip flip = SpriteFlip.None, float zlayer = 0f); 170 abstract void Draw(Framebuffer texture, Rectangle pos, Rectangle cutout, Color color, SpriteFlip flip = SpriteFlip.None, float zlayer = 0f); 171 abstract void Draw(Framebuffer texture, Rectangle pos, Rectangle cutout, float rotation, Vector2 origin, Color color, SpriteFlip flip = SpriteFlip.None, float zlayer = 0f); 172 abstract void Draw(Texture2D texture, Rectangle pos, Rectangle cutout, float rotation, Vector2 origin, Color color, SpriteFlip flip = SpriteFlip.None, float zlayer = 0f); 173 abstract void DrawAABB(Texture2D texture, Rectangle pos_top, Rectangle pos_bottom, Rectangle cutout, Vector2 Origin, Color color, SpriteFlip flip = SpriteFlip.None, float zlayer = 0f); 174 abstract void Flush(); 175 abstract void SwapChain(); 176 abstract void End(); 177 } 178 179 public class Framebuffer { 180 private: 181 FramebufferImpl implementation; 182 183 public: 184 @property int Width() { 185 return implementation.Width; 186 } 187 188 @property int Height() { 189 return implementation.Height; 190 } 191 192 this(int width, int height, int colorAttachments = 1) { 193 implementation = new GlFramebufferImpl(width, height, colorAttachments); 194 } 195 196 void Begin() { 197 implementation.Begin(); 198 } 199 200 void Resize(int width, int height, int colorAttachments = 1) { 201 implementation.Resize(width, height, colorAttachments); 202 } 203 204 void End() { 205 GlFramebufferImpl.End(); 206 Renderer.AdjustViewport(); 207 } 208 209 FramebufferImpl Implementation() { 210 return implementation; 211 } 212 } 213 214 public abstract class FramebufferImpl { 215 protected: 216 int width; 217 int height; 218 219 public: 220 221 this(int width, int height, int colorAttachments) { 222 this.width = width; 223 this.height = height; 224 } 225 226 abstract @property int Width(); 227 abstract @property int Height(); 228 abstract void Resize(int width, int height, int colorAttachments = 1); 229 abstract void Begin(); 230 } 231 232 public abstract class Shader { 233 public: 234 abstract @property bool Attached(); 235 abstract void Attach(); 236 abstract void Detach(); 237 abstract void SetUniform(int location, float value); 238 abstract void SetUniform(int location, Vector2 value); 239 abstract void SetUniform(int location, Vector3 value); 240 abstract void SetUniform(int location, Vector4 value); 241 abstract void SetUniform(int location, int value); 242 abstract void SetUniform(int location, Vector2i value); 243 abstract void SetUniform(int location, Vector3i value); 244 abstract void SetUniform(int location, Vector4i value); 245 abstract void SetUniform(int location, Matrix2x2 value); 246 abstract void SetUniform(int location, Matrix3x3 value); 247 abstract void SetUniform(int location, Matrix4x4 value); 248 abstract int GetUniform(string name); 249 abstract bool HasUniform(string name); 250 251 } 252 253 enum ShaderType { 254 Vertex, 255 Geometry, 256 Fragment, 257 258 Stencil, 259 Compute 260 } 261 262 enum ShaderLang { 263 PPSL, 264 GLSL, 265 266 //For eventual Direct X version? 267 //Would be added in PPSL language as support aswell. 268 CG 269 } 270 271 class ShaderCode { 272 private: 273 ShaderLang language; 274 275 public: 276 @property ShaderLang Language() { return language; } 277 string Vertex; 278 string Fragment; 279 string Geometry; 280 281 this() {} 282 283 this(string vertex, string fragment) { 284 this.Vertex = vertex; 285 language = ShaderLang.GLSL; 286 if (this.Vertex[0..5] == "shader") { 287 language = ShaderLang.PPSL; 288 } 289 this.Fragment = fragment; 290 } 291 292 this(string vertex, string geometry, string fragment) { 293 this.Vertex = vertex; 294 this.Geometry = geometry; 295 language = ShaderLang.GLSL; 296 if (this.Vertex[0..5] == "shader") { 297 language = ShaderLang.PPSL; 298 } 299 this.Fragment = fragment; 300 } 301 } 302 303 public BackendRenderer CreateBackendRenderer(win.Window parent) { 304 return new GlRenderer(parent); 305 }