/
r_swtexture.h
259 lines (212 loc) · 7.06 KB
/
r_swtexture.h
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#pragma once
#include "textures.h"
#include "v_video.h"
#include "g_levellocals.h"
#include "d_main.h"
struct FSoftwareTextureSpan
{
uint16_t TopOffset;
uint16_t Length; // A length of 0 terminates this column
};
// For now this is just a minimal wrapper around FTexture. Once the software renderer no longer accesses FTexture directly, it is time for cleaning up.
class FSoftwareTexture : public ISoftwareTexture
{
protected:
FGameTexture *mTexture;
FTexture *mSource;
TArray<uint8_t> Pixels;
TArray<uint32_t> PixelsBgra;
struct
{
const void* Pixels = nullptr;
int LastUpdate = -1;
} Unlockeddata[3];
FSoftwareTextureSpan **Spandata[3] = { };
DVector2 Scale;
uint8_t WidthBits = 0, HeightBits = 0;
uint16_t WidthMask = 0;
int mPhysicalWidth, mPhysicalHeight;
int mPhysicalScale;
int mBufferFlags;
void FreeAllSpans();
template<class T> FSoftwareTextureSpan **CreateSpans(const T *pixels);
void FreeSpans(FSoftwareTextureSpan **spans);
void CalcBitSize();
public:
FSoftwareTexture(FGameTexture *tex);
virtual ~FSoftwareTexture()
{
FreeAllSpans();
}
FGameTexture *GetTexture() const
{
return mTexture;
}
// The feature from hell... :(
bool useWorldPanning(FLevelLocals *Level) const
{
return mTexture->useWorldPanning() || (Level->flags3 & LEVEL3_FORCEWORLDPANNING);
}
bool isMasked()
{
return mTexture->isMasked();
}
uint16_t GetRotations() const
{
return mTexture->GetRotations();
}
int GetSkyOffset() const { return mTexture->GetSkyOffset(); }
int GetWidth () { return mTexture->GetTexelWidth(); }
int GetHeight () { return mTexture->GetTexelHeight(); }
int GetWidthBits() { return WidthBits; }
int GetHeightBits() { return HeightBits; }
double GetScaledWidth () { return mTexture->GetDisplayWidth(); }
double GetScaledHeight () { return mTexture->GetDisplayHeight(); }
// Now with improved offset adjustment.
int GetLeftOffset(int adjusted) { return mTexture->GetTexelLeftOffset(adjusted); }
int GetTopOffset(int adjusted) { return mTexture->GetTexelTopOffset(adjusted); }
// Interfaces for the different renderers. Everything that needs to check renderer-dependent offsets
// should use these, so that if changes are needed, this is the only place to edit.
// For the original software renderer
int GetLeftOffsetSW() { return GetLeftOffset(r_spriteadjustSW); }
int GetTopOffsetSW() { return GetTopOffset(r_spriteadjustSW); }
double GetScaledLeftOffsetSW() { return mTexture->GetDisplayLeftOffset(r_spriteadjustSW); }
double GetScaledTopOffsetSW() { return mTexture->GetDisplayTopOffset(r_spriteadjustSW); }
DVector2 GetScale() const { return Scale; }
int GetPhysicalWidth() { return mPhysicalWidth; }
int GetPhysicalHeight() { return mPhysicalHeight; }
int GetPhysicalScale() const { return mPhysicalScale; }
virtual void Unload()
{
Pixels.Reset();
PixelsBgra.Reset();
for (auto& d : Unlockeddata) d = {};
}
// Returns true if the next call to GetPixels() will return an image different from the
// last call to GetPixels(). This should be considered valid only if a call to CheckModified()
// is immediately followed by a call to GetPixels().
virtual bool CheckModified (int which) { return false; }
void GenerateBgraFromBitmap(const FBitmap &bitmap);
void CreatePixelsBgraWithMipmaps();
void GenerateBgraMipmaps();
int MipmapLevels();
// Returns true if GetPixelsBgra includes mipmaps
virtual bool Mipmapped() { return true; }
// Returns a single column of the texture
const uint8_t* GetColumn(int style, unsigned int column, const FSoftwareTextureSpan** spans_out)
{
column = WrapColumn(column);
const uint8_t* pixels = GetPixels(style);
if (spans_out)
*spans_out = Spandata[style][column];
return pixels + column * GetPhysicalHeight();
}
// Returns a single column of the texture, in BGRA8 format
const uint32_t* GetColumnBgra(unsigned int column, const FSoftwareTextureSpan** spans_out)
{
column = WrapColumn(column);
const uint32_t* pixels = GetPixelsBgra();
if (spans_out)
*spans_out = Spandata[2][column];
return pixels + column * GetPhysicalHeight();
}
unsigned int WrapColumn(unsigned int column)
{
if ((unsigned)column >= (unsigned)GetPhysicalWidth())
{
if (WidthMask + 1 == GetPhysicalWidth())
{
column &= WidthMask;
}
else
{
column %= GetPhysicalWidth();
}
}
return column;
}
// Returns the whole texture, stored in column-major order, in BGRA8 format
const uint32_t* GetPixelsBgra()
{
int style = 2;
if (Unlockeddata[2].LastUpdate == CurrentUpdate)
{
return static_cast<const uint32_t*>(Unlockeddata[style].Pixels);
}
else
{
UpdatePixels(style);
return static_cast<const uint32_t*>(Unlockeddata[style].Pixels);
}
}
// Returns the whole texture, stored in column-major order
const uint8_t* GetPixels(int style)
{
if (Unlockeddata[style].LastUpdate == CurrentUpdate)
{
return static_cast<const uint8_t*>(Unlockeddata[style].Pixels);
}
else
{
UpdatePixels(style);
return static_cast<const uint8_t*>(Unlockeddata[style].Pixels);
}
}
const uint8_t *GetPixels(FRenderStyle style)
{
bool alpha = !!(style.Flags & STYLEF_RedIsAlpha);
return GetPixels(alpha);
}
// Checks if the pixel data is loaded.
bool CheckPixels() const
{
return V_IsTrueColor() ? PixelsBgra.Size() > 0 : Pixels.Size() > 0;
}
const uint8_t *GetColumn(FRenderStyle style, unsigned int column, const FSoftwareTextureSpan **spans_out)
{
bool alpha = !!(style.Flags & STYLEF_RedIsAlpha);
return GetColumn(alpha, column, spans_out);
}
static int CurrentUpdate;
void UpdatePixels(int style);
virtual const uint32_t* GetPixelsBgraLocked();
virtual const uint8_t* GetPixelsLocked(int style);
};
// A texture that returns a wiggly version of another texture.
class FWarpTexture : public FSoftwareTexture
{
TArray<uint8_t> WarpedPixels[2];
TArray<uint32_t> WarpedPixelsRgba;
int bWarped = 0;
uint64_t GenTime[3] = { UINT64_MAX, UINT64_MAX, UINT64_MAX };
int WidthOffsetMultiplier, HeightOffsetMultiplier; // [mxd]
public:
FWarpTexture (FGameTexture *source, int warptype);
const uint32_t *GetPixelsBgraLocked() override;
const uint8_t *GetPixelsLocked(int style) override;
bool CheckModified (int which) override;
void GenerateBgraMipmapsFast();
private:
int NextPo2 (int v); // [mxd]
void SetupMultipliers (int width, int height); // [mxd]
};
class FSWCanvasTexture : public FSoftwareTexture
{
void MakeTexture();
void MakeTextureBgra();
DCanvas *Canvas = nullptr;
DCanvas *CanvasBgra = nullptr;
public:
FSWCanvasTexture(FGameTexture* source);
~FSWCanvasTexture();
// Returns the whole texture, stored in column-major order
const uint32_t *GetPixelsBgraLocked() override;
const uint8_t *GetPixelsLocked(int style) override;
virtual void Unload() override;
void UpdatePixels(bool truecolor);
DCanvas *GetCanvas() { GetPixels(0); return Canvas; }
DCanvas *GetCanvasBgra() { GetPixelsBgra(); return CanvasBgra; }
bool Mipmapped() override { return false; }
};
FSoftwareTexture* GetSoftwareTexture(FGameTexture* tex);
FSoftwareTexture* GetPalettedSWTexture(FTextureID texid, bool animate, bool checkcompat = false, bool allownull = false);