Esempio n. 1
0
        public void ResetViewport()
        {
            EnsureNotDisposed();

            SDL_gpu.GPU_UnsetViewport(TargetHandle);
            CurrentViewport = null;
        }
Esempio n. 2
0
        public void SetCamera(Camera camera)
        {
            EnsureNotDisposed();

            SDL_gpu.GPU_SetCamera(TargetHandle, ref camera.GpuCamera);
            CurrentCamera = camera;
        }
Esempio n. 3
0
        public void SetUniform(string name, Texture value, int textureUnit)
        {
            EnsureNotDisposed();

            if (value.Disposed)
            {
                throw new ArgumentException("Texture provided was already diposed.");
            }

            if (textureUnit == 0)
            {
                _log.Error("Cannot set texture unit 0: reserved for use by the rendering system blitting functions.");
                return;
            }

            var loc = SDL_gpu.GPU_GetUniformLocation(ProgramHandle, name);

            if (loc == -1)
            {
                _log.Warning($"Texture sampler '{name}' does not exist.");
                return;
            }

            SDL_gpu.GPU_SetShaderImage(value.ImageHandle, loc, textureUnit);
        }
Esempio n. 4
0
        public void ResetCamera()
        {
            EnsureNotDisposed();

            SDL_gpu.GPU_SetCamera(TargetHandle, IntPtr.Zero);
            CurrentCamera = null;
        }
Esempio n. 5
0
        public VertexShader(string filePath)
        {
            FilePath   = filePath;
            SourceCode = File.ReadAllText(FilePath);

            VertexShaderObjectHandle = SDL_gpu.GPU_CompileShader(SDL_gpu.GPU_ShaderEnum.GPU_VERTEX_SHADER, SourceCode);

            if (VertexShaderObjectHandle == 0)
            {
                throw new ShaderException("Compilation failed.", SDL_gpu.GPU_GetShaderMessage());
            }

            CompileAndSetDefaultPixelShader();
            if (PixelShaderObjectHandle == 0)
            {
                throw new ShaderException(
                          "Default Chroma pixel shader compilation failed. " +
                          "Report an issue - be sure to include GLSL errors or I'll f**k you up just like I did with this framework.", SDL_gpu.GPU_GetShaderMessage());
            }

            ProgramHandle = SDL_gpu.GPU_LinkShaders(PixelShaderObjectHandle, VertexShaderObjectHandle);

            if (ProgramHandle == 0)
            {
                throw new ShaderException("Linkage failed.", SDL_gpu.GPU_GetShaderMessage());
            }

            CreateShaderBlock();
        }
Esempio n. 6
0
        private void LinkShaders()
        {
            ProgramHandle = SDL_gpu.GPU_LinkShaders(PixelShaderObjectHandle, VertexShaderObjectHandle);

            if (ProgramHandle == 0)
            {
                throw new ShaderException("Compound shader linkage failed.", SDL_gpu.GPU_GetShaderMessage());
            }
        }
Esempio n. 7
0
        private void TryCompileVertexShader()
        {
            VertexShaderSourceCode   = File.ReadAllText(VertexShaderFilePath);
            VertexShaderObjectHandle = SDL_gpu.GPU_CompileShader(SDL_gpu.GPU_ShaderEnum.GPU_VERTEX_SHADER, VertexShaderSourceCode);

            if (VertexShaderObjectHandle == 0)
            {
                throw new ShaderException("Vertex shader compilation failed.", SDL_gpu.GPU_GetShaderMessage());
            }
        }
Esempio n. 8
0
        private void TryCompilePixelShader()
        {
            PixelShaderSourceCode   = File.ReadAllText(PixelShaderFilePath);
            PixelShaderObjectHandle = SDL_gpu.GPU_CompileShader(SDL_gpu.GPU_ShaderEnum.GPU_PIXEL_SHADER, PixelShaderSourceCode);

            if (PixelShaderObjectHandle == 0)
            {
                throw new ShaderException("Pixel shader compilation failed.", SDL_gpu.GPU_GetShaderMessage());
            }
        }
Esempio n. 9
0
        protected virtual void CompileAndSetDefaultPixelShader()
        {
            EnsureNotDisposed();

            var shaderSourceStream = Assembly.GetExecutingAssembly()
                                     .GetManifestResourceStream("Chroma.Resources.shader.default.frag");

            using var sr            = new StreamReader(shaderSourceStream !);
            PixelShaderObjectHandle =
                SDL_gpu.GPU_CompileShader(SDL_gpu.GPU_ShaderEnum.GPU_PIXEL_SHADER, sr.ReadToEnd());
        }
Esempio n. 10
0
        protected void CompileAndSetDefaultVertexShader()
        {
            EnsureNotDisposed();

            var shaderSourceStream = Assembly.GetExecutingAssembly()
                                     .GetManifestResourceStream("Chroma.Resources.shader.default.vert");

            using var sr             = new StreamReader(shaderSourceStream);
            VertexShaderObjectHandle =
                SDL_gpu.GPU_CompileShader(SDL_gpu.GPU_ShaderEnum.GPU_VERTEX_SHADER, sr.ReadToEnd());
        }
Esempio n. 11
0
        protected void CleanupAfterLinking()
        {
            SDL_gpu.GPU_DetachShader(ProgramHandle, VertexShaderObjectHandle);
            SDL_gpu.GPU_DetachShader(ProgramHandle, PixelShaderObjectHandle);

            SDL_gpu.GPU_FreeShader(VertexShaderObjectHandle);
            SDL_gpu.GPU_FreeShader(PixelShaderObjectHandle);

            VertexShaderObjectHandle = 0;
            PixelShaderObjectHandle  = 0;
        }
Esempio n. 12
0
        public void Quit()
        {
            Audio.Dispose();
            Content.Dispose();

            SDL_mixer.Mix_Quit();
            SDL_gpu.GPU_Quit();
            SDL2.SDL_Quit();

            Environment.Exit(0);
        }
Esempio n. 13
0
        public virtual void Activate()
        {
            EnsureNotDisposed();

            if (ProgramHandle == 0)
            {
                _log.Warning($"Refusing to activate invalid shader.");
                return;
            }

            SDL_gpu.GPU_ActivateShaderProgram(ProgramHandle, ref Block);
        }
Esempio n. 14
0
        protected void CreateShaderBlock()
        {
            EnsureNotDisposed();

            Block = SDL_gpu.GPU_LoadShaderBlock(
                ProgramHandle,
                "gpu_VertexPosition",
                "gpu_TexCoord",
                "gpu_VertexColor",
                "gpu_ModelViewProjection"
                );
        }
Esempio n. 15
0
        private static void InitializeSdlSystems()
        {
            Console.WriteLine("---");

            Console.WriteLine("Initializing SDL2 core...");
            SDL2.SDL_Init(BootConfig.SdlModules.SdlInitFlags);

            if (BootConfig.EnableSdlGpuDebugging)
            {
                Console.WriteLine("Enabling SDL_gpu debugging...");
                SDL_gpu.GPU_SetDebugLevel(SDL_gpu.GPU_DebugLevelEnum.GPU_DEBUG_LEVEL_MAX);
            }
        }
Esempio n. 16
0
        public void SetViewport(Rectangle viewportRectangle)
        {
            EnsureNotDisposed();

            CurrentViewport = viewportRectangle;
            SDL_gpu.GPU_SetViewport(TargetHandle, new SDL_gpu.GPU_Rect
            {
                x = viewportRectangle.X,
                y = viewportRectangle.Y,
                w = viewportRectangle.Width,
                h = viewportRectangle.Height
            });
        }
Esempio n. 17
0
        public void SetUniform(string name, int value)
        {
            EnsureNotDisposed();

            var loc = SDL_gpu.GPU_GetUniformLocation(ProgramHandle, name);

            if (loc == -1)
            {
                _log.Warning($"Int uniform '{name}' does not exist.");
                return;
            }

            SDL_gpu.GPU_SetUniformi(loc, value);
        }
Esempio n. 18
0
        public void SetUniform(string name, Color value)
        {
            EnsureNotDisposed();

            var loc = SDL_gpu.GPU_GetUniformLocation(ProgramHandle, name);

            if (loc == -1)
            {
                Log.Warning($"Vec4 uniform '{name}' does not exist.");
                return;
            }

            SDL_gpu.GPU_SetUniformfv(loc, 4, 1, value.AsOrderedArray());
        }
Esempio n. 19
0
        public void SetUniform(string name, Vector3 value)
        {
            EnsureNotDisposed();

            var loc = SDL_gpu.GPU_GetUniformLocation(ProgramHandle, name);

            if (loc == -1)
            {
                _log.Warning($"Vec3 uniform '{name}' does not exist.");
                return;
            }

            SDL_gpu.GPU_SetUniformfv(loc, 3, 1, new[] { value.X, value.Y, value.Z });
        }
Esempio n. 20
0
        public void SetAttribute(string name, Color value)
        {
            EnsureNotDisposed();

            var loc = SDL_gpu.GPU_GetAttributeLocation(ProgramHandle, name);

            if (loc == -1)
            {
                _log.Warning($"Vec4 attribute '{name}' does not exist.");
                return;
            }

            SDL_gpu.GPU_SetAttributefv(loc, 4, value.AsNormalizedFloatArray());
        }
Esempio n. 21
0
        public void SetAttribute(string name, Vector4 value)
        {
            EnsureNotDisposed();

            var loc = SDL_gpu.GPU_GetAttributeLocation(ProgramHandle, name);

            if (loc == -1)
            {
                _log.Warning($"Vec4 attribute '{name}' does not exist.");
                return;
            }

            SDL_gpu.GPU_SetAttributefv(loc, 4, new[] { value.X, value.Y, value.Z, value.W });
        }
Esempio n. 22
0
        public void SetAttribute(string name, uint value)
        {
            EnsureNotDisposed();

            var loc = SDL_gpu.GPU_GetAttributeLocation(ProgramHandle, name);

            if (loc == -1)
            {
                _log.Warning($"Uint attribute '{name}' does not exist.");
                return;
            }

            SDL_gpu.GPU_SetAttributeui(loc, value);
        }
Esempio n. 23
0
        protected virtual void TryLinkShaders()
        {
            ProgramHandle = SDL_gpu.GPU_CreateShaderProgram();

            SDL_gpu.GPU_AttachShader(ProgramHandle, VertexShaderObjectHandle);
            SDL_gpu.GPU_AttachShader(ProgramHandle, PixelShaderObjectHandle);
            SDL_gpu.GPU_LinkShaderProgram(ProgramHandle);

            var obj = SDL_gpu.GPU_PopErrorCode();

            if (obj.error == SDL_gpu.GPU_ErrorEnum.GPU_ERROR_BACKEND_ERROR)
            {
                throw new ShaderException(
                          "Failed to link vertex and pixel shader.\nMake sure your 'in' parameters have correct f*****g types.",
                          string.Empty
                          );
            }
        }
Esempio n. 24
0
        protected override void FreeNativeResources()
        {
            EnsureNotDisposed();

            if (PixelShaderObjectHandle != 0)
            {
                SDL_gpu.GPU_FreeShader(PixelShaderObjectHandle);
            }

            if (VertexShaderObjectHandle != 0)
            {
                SDL_gpu.GPU_FreeShader(VertexShaderObjectHandle);
            }

            if (ProgramHandle != 0)
            {
                SDL_gpu.GPU_FreeShaderProgram(ProgramHandle);
            }
        }
Esempio n. 25
0
        public void SetUniform(string name, Texture value, int textureUnit)
        {
            EnsureNotDisposed();

            if (value.Disposed)
            {
                throw new ArgumentException("Texture provided was already diposed.");
            }

            var loc = SDL_gpu.GPU_GetUniformLocation(ProgramHandle, name);

            if (loc == -1)
            {
                Log.Warning($"Float uniform '{name}' does not exist.");
                return;
            }

            SDL_gpu.GPU_SetShaderImage(value.ImageHandle, loc, textureUnit);
        }
Esempio n. 26
0
        public RenderTarget(int width, int height)
            : base(width, height)
        {
            if (ImageHandle == IntPtr.Zero)
            {
                var msg = $"Failed to create texture handle: {SDL2.SDL_GetError()}";
                Log.Error(msg);
                throw new Exception(msg);
            }

            TargetHandle = SDL_gpu.GPU_LoadTarget(ImageHandle);

            if (TargetHandle == IntPtr.Zero)
            {
                var msg = $"Failed to create render target handle: {SDL2.SDL_GetError()}";

                Log.Error(msg);
                throw new Exception(msg);
            }
        }
Esempio n. 27
0
        public void SetUniform(string name, Matrix4x4 value)
        {
            EnsureNotDisposed();

            var loc = SDL_gpu.GPU_GetUniformLocation(ProgramHandle, name);

            if (loc == -1)
            {
                _log.Warning($"Mat4 uniform '{name}' does not exist.");
                return;
            }

            SDL_gpu.GPU_SetUniformMatrixfv(loc, 1, 4, 4, false, new[]
            {
                value.M11, value.M12, value.M13, value.M14,
                value.M21, value.M22, value.M23, value.M24,
                value.M31, value.M32, value.M33, value.M34,
                value.M41, value.M42, value.M43, value.M44
            });
        }
Esempio n. 28
0
 public static void Deactivate()
 => SDL_gpu.GPU_ActivateShaderProgram(0, IntPtr.Zero);
Esempio n. 29
0
 protected override void FreeNativeResources()
 {
     SDL_gpu.GPU_FreeTarget(TargetHandle);
     base.FreeNativeResources();
 }
Esempio n. 30
0
 public RenderTarget(int width, int height)
     : base(width, height)
 {
     TargetHandle = SDL_gpu.GPU_LoadTarget(ImageHandle);
 }