Exemple #1
0
 internal AudioStream(ClydeHandle handle, TimeSpan length, int channelCount, string?name = null)
 {
     ClydeHandle  = handle;
     Length       = length;
     ChannelCount = channelCount;
     Name         = name;
 }
        private void SetTexture(TextureUnit unit, ClydeHandle textureId)
        {
            var glHandle = _loadedTextures[textureId].OpenGLObject;

            GL.ActiveTexture(unit);
            GL.BindTexture(TextureTarget.Texture2D, glHandle.Handle);
        }
Exemple #3
0
        public AudioStream LoadAudioWav(Stream stream, string name = null)
        {
            var wav = _readWav(stream);

            var buffer = AL.GenBuffer();

            ALFormat format;

            if (wav.BitsPerSample == 16)
            {
                if (wav.NumChannels == 1)
                {
                    format = ALFormat.Mono16;
                }
                else if (wav.NumChannels == 2)
                {
                    format = ALFormat.Stereo16;
                }
                else
                {
                    throw new InvalidOperationException("Unable to load audio with more than 2 channels.");
                }
            }
            else if (wav.BitsPerSample == 8)
            {
                if (wav.NumChannels == 1)
                {
                    format = ALFormat.Mono8;
                }
                else if (wav.NumChannels == 2)
                {
                    format = ALFormat.Stereo8;
                }
                else
                {
                    throw new InvalidOperationException("Unable to load audio with more than 2 channels.");
                }
            }
            else
            {
                throw new InvalidOperationException("Unable to load wav with bits per sample different from 8 or 16");
            }

            unsafe
            {
                fixed(byte *ptr = wav.Data.Span)
                {
                    AL.BufferData(buffer, format, (IntPtr)ptr, wav.Data.Length, wav.SampleRate);
                }
            }

            _checkAlError();

            var handle = new ClydeHandle(_audioSampleBuffers.Count);

            _audioSampleBuffers.Add(new LoadedAudioSample(buffer));
            var length = TimeSpan.FromSeconds(wav.Data.Length / (double)wav.BlockAlign / wav.SampleRate);

            return(new AudioStream(handle, length, wav.NumChannels, name));
        }
 public RenderTarget(Vector2i size, ClydeTexture texture, OGLHandle objectHandle, Clyde clyde, ClydeHandle handle)
 {
     Size         = size;
     Texture      = texture;
     ObjectHandle = objectHandle;
     _clyde       = clyde;
     Handle       = handle;
 }
Exemple #5
0
 public RenderTarget(Vector2i size, ClydeTexture texture, OGLHandle objectHandle, Clyde clyde,
                     ClydeHandle handle, OGLHandle stencilBuffer)
 {
     Size          = size;
     Texture       = texture;
     ObjectHandle  = objectHandle;
     _clyde        = clyde;
     Handle        = handle;
     StencilBuffer = stencilBuffer;
 }
        private void DeleteTexture(ClydeHandle textureHandle)
        {
            if (!_loadedTextures.TryGetValue(textureHandle, out var loadedTexture))
            {
                // Already deleted I guess.
                return;
            }

            GL.DeleteTexture(loadedTexture.OpenGLObject.Handle);
            _loadedTextures.Remove(textureHandle);
            //GC.RemoveMemoryPressure(loadedTexture.MemoryPressure);
        }
        public ShaderInstance InstanceShader(ClydeHandle handle)
        {
            var newHandle = AllocRid();
            var loaded    = new LoadedShaderInstance
            {
                ShaderHandle = handle
            };
            var instance = new ClydeShaderInstance(newHandle, this);

            _shaderInstances.Add(newHandle, loaded);
            return(instance);
        }
Exemple #8
0
        private void ReadCanvasKind(YamlMappingNode mapping)
        {
            var source = "";

            if (mapping.TryGetNode("light_mode", out var node))
            {
                switch (node.AsString())
                {
                case "normal":
                    break;

                case "unshaded":
                    source += "light_mode unshaded;\n";
                    break;

                default:
                    throw new InvalidOperationException($"Invalid light mode: '{node.AsString()}'");
                }
            }

            if (mapping.TryGetNode("blend_mode", out node))
            {
                switch (node.AsString())
                {
                case "mix":
                    source += "blend_mode mix;\n";
                    break;

                case "add":
                    source += "blend_mode add;\n";
                    break;

                case "subtract":
                    source += "blend_mode subtract;\n";
                    break;

                case "multiply":
                    source += "blend_mode multiply;\n";
                    break;

                default:
                    throw new InvalidOperationException($"Invalid blend mode: '{node.AsString()}'");
                }
            }

            source += "void fragment() {\n    COLOR = texture(TEXTURE, UV);\n}";

            var preset = ShaderParser.Parse(source, _resourceCache);

            CompiledCanvasShader = _clyde.LoadShader(preset, $"canvas_preset_{ID}");
        }
        private void _loadStockShaders()
        {
            _shaderWrapCodeSpriteFrag = _readFile("/Shaders/Internal/sprite.frag");
            _shaderWrapCodeSpriteVert = _readFile("/Shaders/Internal/sprite.vert");

            _defaultShader =
                _resourceCache.GetResource <ShaderSourceResource>("/Shaders/Internal/default-sprite.swsl").ClydeHandle;

            _currentShader = _defaultShader;

            var lightVert = _readFile("/Shaders/Internal/light.vert");
            var lightFrag = _readFile("/Shaders/Internal/light.frag");

            _lightShader = _compileProgram(lightVert, lightFrag, "_lightShader");
        }
        public void ReloadShader(ClydeHandle handle, ParsedShader newShader)
        {
            var loaded = _loadedShaders[handle];

            loaded.HasLighting = newShader.LightMode != ShaderLightMode.Unshaded;
            loaded.BlendMode   = newShader.BlendMode;

            var(vertBody, fragBody) = GetShaderCode(newShader);

            var program = _compileProgram(vertBody, fragBody, loaded.Name);

            loaded.Program.Delete();

            loaded.Program = program;

            program.BindBlock(UniProjViewMatrices, ProjViewBindingIndex);
            program.BindBlock(UniUniformConstants, UniformConstantsBindingIndex);
        }
Exemple #11
0
        public AudioStream LoadAudioOggVorbis(Stream stream, string name = null)
        {
            var vorbis = _readOggVorbis(stream);

            var buffer = AL.GenBuffer();

            ALFormat format;

            // NVorbis only supports loading into floats.
            // If this becomes a problem due to missing extension support (doubt it but ok),
            // check the git history, I originally used libvorbisfile which worked and loaded 16 bit LPCM.
            if (vorbis.Channels == 1)
            {
                format = ALFormat.MonoFloat32Ext;
            }
            else if (vorbis.Channels == 2)
            {
                format = ALFormat.StereoFloat32Ext;
            }
            else
            {
                throw new InvalidOperationException("Unable to load audio with more than 2 channels.");
            }

            unsafe
            {
                fixed(float *ptr = vorbis.Data.Span)
                {
                    AL.BufferData(buffer, format, (IntPtr)ptr, vorbis.Data.Length * sizeof(float),
                                  (int)vorbis.SampleRate);
                }
            }

            _checkAlError();

            var handle = new ClydeHandle(_audioSampleBuffers.Count);

            _audioSampleBuffers.Add(new LoadedAudioSample(buffer));
            var length = TimeSpan.FromSeconds(vorbis.TotalSamples / (double)vorbis.SampleRate);

            return(new AudioStream(handle, length, (int)vorbis.Channels, name));
        }
Exemple #12
0
        private void DeleteRenderTexture(ClydeHandle handle)
        {
            if (!_renderTargets.TryGetValue(handle, out var renderTarget))
            {
                return;
            }

            DebugTools.Assert(!renderTarget.IsWindow, "Cannot delete window-backed render targets directly.");

            GL.DeleteFramebuffer(renderTarget.FramebufferHandle.Handle);
            _renderTargets.Remove(handle);
            DeleteTexture(renderTarget.TextureHandle);

            if (renderTarget.DepthStencilHandle != default)
            {
                GL.DeleteRenderbuffer(renderTarget.DepthStencilHandle.Handle);
            }

            //GC.RemoveMemoryPressure(renderTarget.MemoryPressure);
        }
Exemple #13
0
        public void ReloadShader(ClydeHandle handle, ParsedShader newShader)
        {
            var loaded = _loadedShaders[handle];

            loaded.HasLighting = newShader.LightMode != ShaderLightMode.Unshaded;
            loaded.BlendMode   = newShader.BlendMode;

            var(vertBody, fragBody) = GetShaderCode(newShader);

            var program = _compileProgram(vertBody, fragBody, BaseShaderAttribLocations, loaded.Name);

            loaded.Program.Delete();

            loaded.Program = program;

            if (_hasGLUniformBuffers)
            {
                program.BindBlock(UniProjViewMatrices, BindingIndexProjView);
                program.BindBlock(UniUniformConstants, BindingIndexUniformConstants);
            }
        }
        private void LoadLightingShaders()
        {
            var depthVert = ReadEmbeddedShader("shadow-depth.vert");
            var depthFrag = ReadEmbeddedShader("shadow-depth.frag");

            _fovCalculationProgram = _compileProgram(depthVert, depthFrag, "Shadow Depth Program");

            var debugShader = _resourceCache.GetResource <ShaderSourceResource>("/Shaders/Internal/depth-debug.swsl");

            _fovDebugShaderInstance = (ClydeShaderInstance)InstanceShader(debugShader.ClydeHandle);

            ClydeHandle LoadShaderHandle(string path)
            {
                var shaderSource = _resourceCache.GetResource <ShaderSourceResource>(path);

                return(shaderSource.ClydeHandle);
            }

            _lightShaderHandle          = LoadShaderHandle("/Shaders/Internal/light.swsl");
            _fovShaderHandle            = LoadShaderHandle("/Shaders/Internal/fov.swsl");
            _fovLightShaderHandle       = LoadShaderHandle("/Shaders/Internal/fov-lighting.swsl");
            _wallBleedBlurShaderHandle  = LoadShaderHandle("/Shaders/Internal/wall-bleed-blur.swsl");
            _mergeWallLayerShaderHandle = LoadShaderHandle("/Shaders/Internal/wall-merge.swsl");
        }
Exemple #15
0
 public CursorImpl(GlfwWindowingImpl clyde, ClydeHandle id, bool standard)
 {
     _standard = standard;
     Owner     = clyde;
     Id        = id;
 }
Exemple #16
0
 private void DisposeImpl()
 {
     Owner.SendCmd(new CmdCursorDestroy(Id));
     Id = default;
 }
 public ShaderInstance InstanceShader(ClydeHandle handle)
 {
     return(new DummyShaderInstance());
 }
 public ClydeShaderInstance(ClydeHandle handle, Clyde parent)
 {
     Handle = handle;
     Parent = parent;
 }
 public RenderWindow(Clyde clyde, ClydeHandle handle) : base(clyde, handle)
 {
 }
 public ShaderInstance InstanceShader(ClydeHandle handle)
 {
     return(new ClydeShaderInstance(handle));
 }
 protected RenderTargetBase(Clyde clyde, ClydeHandle handle)
 {
     Clyde  = clyde;
     Handle = handle;
 }
 public RenderTexture(Vector2i size, ClydeTexture texture, Clyde clyde, ClydeHandle handle)
     : base(clyde, handle)
 {
     Size    = size;
     Texture = texture;
 }
 internal ClydeTexture(ClydeHandle id, Vector2i size, Clyde clyde) : base(size)
 {
     TextureId = id;
     _clyde    = clyde;
 }
Exemple #24
0
 public Viewport(ClydeHandle handle, string?name, Clyde clyde)
 {
     Name    = name;
     _handle = handle;
     _clyde  = clyde;
 }
        private RenderTarget CreateRenderTarget(Vector2i size, RenderTargetColorFormat colorFormat,
                                                TextureSampleParameters?sampleParameters = null, string name = null)
        {
            // Generate color attachment texture.
            var texture = new OGLHandle(GL.GenTexture());

            GL.BindTexture(TextureTarget.Texture2D, texture.Handle);

            _applySampleParameters(sampleParameters);

            PixelInternalFormat internalFormat;

            switch (colorFormat)
            {
            case RenderTargetColorFormat.Rgba8:
                internalFormat = PixelInternalFormat.Rgba8;
                break;

            case RenderTargetColorFormat.Rgba16F:
                internalFormat = PixelInternalFormat.Rgba16f;
                break;

            case RenderTargetColorFormat.Rgba8Srgb:
                internalFormat = PixelInternalFormat.Srgb8Alpha8;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(colorFormat), colorFormat, null);
            }

            var(width, height) = size;
            GL.TexImage2D(TextureTarget.Texture2D, 0, internalFormat, width, height, 0, PixelFormat.Red,
                          PixelType.Byte, IntPtr.Zero);

            // Generate FBO.
            var fbo = new OGLHandle(GL.GenFramebuffer());

            // Cache currently bound framebuffers
            // so if somebody creates a framebuffer while drawing it won't ruin everything.
            var boundDrawBuffer = GL.GetInteger(GetPName.DrawFramebufferBinding);
            var boundReadBuffer = GL.GetInteger(GetPName.ReadFramebufferBinding);

            // Bind color attachment to FBO.
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo.Handle);

            GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, texture.Handle,
                                  0);

            // This should always pass but OpenGL makes it easy to check for once so let's.
            var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);

            DebugTools.Assert(status == FramebufferErrorCode.FramebufferComplete,
                              $"new framebuffer has bad status {status}");

            // Re-bind previous framebuffers.
            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, boundDrawBuffer);
            GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, boundReadBuffer);

            var textureObject = _genTexture(texture, size, name);
            var handle        = new ClydeHandle(_nextRenderTarget++);
            var renderTarget  = new RenderTarget(size, textureObject, fbo, this, handle);

            _renderTargets.Add(handle, renderTarget);
            return(renderTarget);
        }
 internal ClydeTexture(ClydeHandle id, Vector2i size, bool srgb, Clyde clyde) : base(size)
 {
     TextureId = id;
     IsSrgb    = srgb;
     _clyde    = clyde;
 }
 public ClydeShaderInstance(ClydeHandle shaderHandle)
 {
     ShaderHandle = shaderHandle;
 }