aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics/Gal/OpenGL/OGLStreamBuffer.cs
blob: 329c5b5df7344ef96afa21388a0c045d379cc27d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using OpenTK.Graphics.OpenGL;

namespace Ryujinx.Graphics.Gal.OpenGL
{
    abstract class OGLStreamBuffer : IDisposable
    {
        public int Handle { get; protected set; }

        public int Size { get; protected set; }

        protected BufferTarget Target { get; private set; }

        private bool Mapped = false;

        public OGLStreamBuffer(BufferTarget Target, int MaxSize)
        {
            Handle = 0;
            Mapped = false;

            this.Target = Target;
            this.Size = MaxSize;
        }

        public static OGLStreamBuffer Create(BufferTarget Target, int MaxSize)
        {
            //TODO: Query here for ARB_buffer_storage and use when available
            return new SubDataBuffer(Target, MaxSize);
        }

        public byte[] Map(int Size)
        {
            if (Handle == 0 || Mapped || Size > this.Size)
            {
                throw new InvalidOperationException();
            }

            byte[] Memory = InternMap(Size);

            Mapped = true;

            return Memory;
        }

        public void Unmap(int UsedSize)
        {
            if (Handle == 0 || !Mapped)
            {
                throw new InvalidOperationException();
            }

            InternUnmap(UsedSize);

            Mapped = false;
        }

        protected abstract byte[] InternMap(int Size);

        protected abstract void InternUnmap(int UsedSize);

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool Disposing)
        {
            if (Disposing && Handle != 0)
            {
                GL.DeleteBuffer(Handle);

                Handle = 0;
            }
        }
    }

    class SubDataBuffer : OGLStreamBuffer
    {
        private byte[] Memory;

        public SubDataBuffer(BufferTarget Target, int MaxSize)
            : base(Target, MaxSize)
        {
            Memory = new byte[MaxSize];

            GL.GenBuffers(1, out int Handle);

            GL.BindBuffer(Target, Handle);

            GL.BufferData(Target, Size, IntPtr.Zero, BufferUsageHint.StreamDraw);

            this.Handle = Handle;
        }

        protected override byte[] InternMap(int Size)
        {
            return Memory;
        }

        protected override void InternUnmap(int UsedSize)
        {
            GL.BindBuffer(Target, Handle);
            
            unsafe
            {
                fixed (byte* MemoryPtr = Memory)
                {
                    GL.BufferSubData(Target, IntPtr.Zero, UsedSize, (IntPtr)MemoryPtr);
                }
            }
        }
    }
}