forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTexture.cs
206 lines (173 loc) · 5.38 KB
/
Texture.cs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#region Copyright & License Information
/*
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.IO;
using OpenRA.Primitives;
namespace OpenRA.Platforms.Default
{
sealed class Texture : ThreadAffine, ITextureInternal
{
uint texture;
TextureScaleFilter scaleFilter;
public uint ID { get { return texture; } }
public Size Size { get; private set; }
bool disposed;
public TextureScaleFilter ScaleFilter
{
get
{
return scaleFilter;
}
set
{
VerifyThreadAffinity();
if (scaleFilter == value)
return;
scaleFilter = value;
PrepareTexture();
}
}
public Texture()
{
OpenGL.glGenTextures(1, out texture);
OpenGL.CheckGLError();
}
void PrepareTexture()
{
OpenGL.CheckGLError();
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, texture);
OpenGL.CheckGLError();
var filter = scaleFilter == TextureScaleFilter.Linear ? OpenGL.GL_LINEAR : OpenGL.GL_NEAREST;
OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, filter);
OpenGL.CheckGLError();
OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, filter);
OpenGL.CheckGLError();
OpenGL.glTexParameterf(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE);
OpenGL.CheckGLError();
OpenGL.glTexParameterf(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE);
OpenGL.CheckGLError();
OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_BASE_LEVEL, 0);
OpenGL.CheckGLError();
OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAX_LEVEL, 0);
OpenGL.CheckGLError();
}
void SetData(IntPtr data, int width, int height)
{
PrepareTexture();
var glInternalFormat = OpenGL.Profile == GLProfile.Embedded ? OpenGL.GL_BGRA : OpenGL.GL_RGBA8;
OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, glInternalFormat, width, height,
0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, data);
OpenGL.CheckGLError();
}
public void SetData(byte[] colors, int width, int height)
{
VerifyThreadAffinity();
if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
Size = new Size(width, height);
unsafe
{
fixed (byte* ptr = &colors[0])
SetData(new IntPtr(ptr), width, height);
}
}
// An array of RGBA
public void SetData(uint[,] colors)
{
VerifyThreadAffinity();
var width = colors.GetUpperBound(1) + 1;
var height = colors.GetUpperBound(0) + 1;
if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
Size = new Size(width, height);
unsafe
{
fixed (uint* ptr = &colors[0, 0])
SetData(new IntPtr(ptr), width, height);
}
}
public byte[] GetData()
{
VerifyThreadAffinity();
var data = new byte[4 * Size.Width * Size.Height];
// GLES doesn't support glGetTexImage so data must be read back via a frame buffer
if (OpenGL.Profile == GLProfile.Embedded)
{
// Query the active framebuffer so we can restore it afterwards
OpenGL.glGetIntegerv(OpenGL.GL_FRAMEBUFFER_BINDING, out var lastFramebuffer);
OpenGL.glGenFramebuffers(1, out var framebuffer);
OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, framebuffer);
OpenGL.CheckGLError();
OpenGL.glFramebufferTexture2D(OpenGL.GL_FRAMEBUFFER, OpenGL.GL_COLOR_ATTACHMENT0, OpenGL.GL_TEXTURE_2D, texture, 0);
OpenGL.CheckGLError();
var canReadBGRA = OpenGL.Features.HasFlag(OpenGL.GLFeatures.ESReadFormatBGRA);
unsafe
{
fixed (byte* ptr = &data[0])
{
var intPtr = new IntPtr(ptr);
var format = canReadBGRA ? OpenGL.GL_BGRA : OpenGL.GL_RGBA;
OpenGL.glReadPixels(0, 0, Size.Width, Size.Height, format, OpenGL.GL_UNSIGNED_BYTE, intPtr);
OpenGL.CheckGLError();
}
}
// Convert RGBA to BGRA
if (!canReadBGRA)
{
for (var i = 0; i < 4 * Size.Width * Size.Height; i += 4)
{
var temp = data[i];
data[i] = data[i + 2];
data[i + 2] = temp;
}
}
OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, (uint)lastFramebuffer);
OpenGL.glDeleteFramebuffers(1, ref framebuffer);
OpenGL.CheckGLError();
}
else
{
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, texture);
unsafe
{
fixed (byte* ptr = &data[0])
{
var intPtr = new IntPtr((void*)ptr);
OpenGL.glGetTexImage(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_BGRA,
OpenGL.GL_UNSIGNED_BYTE, intPtr);
}
}
OpenGL.CheckGLError();
}
return data;
}
public void SetEmpty(int width, int height)
{
VerifyThreadAffinity();
if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
Size = new Size(width, height);
SetData(IntPtr.Zero, width, height);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool disposing)
{
if (disposed)
return;
disposed = true;
OpenGL.glDeleteTextures(1, ref texture);
}
}
}