CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Software/TransformUnit.h
Views: 1401
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include "CommonTypes.h"
21
#include "GPU/Common/DrawEngineCommon.h"
22
#include "GPU/Common/GPUDebugInterface.h"
23
#include "GPU/Software/SoftGpu.h"
24
#include "GPU/Math3D.h"
25
26
using namespace Math3D;
27
28
static constexpr int32_t SCREEN_SCALE_FACTOR = 16;
29
30
typedef u16 u10; // TODO: erm... :/
31
32
typedef Vec3<float> ModelCoords;
33
typedef Vec3<float> WorldCoords;
34
typedef Vec3<float> ViewCoords;
35
typedef Vec4<float> ClipCoords; // Range: -w <= x/y/z <= w
36
37
class BinManager;
38
struct TransformState;
39
40
enum class CullType {
41
CW = 0,
42
CCW = 1,
43
OFF = 2,
44
};
45
46
struct ScreenCoords
47
{
48
ScreenCoords() {}
49
ScreenCoords(int x, int y, u16 z) : x(x), y(y), z(z) {}
50
51
int x;
52
int y;
53
u16 z;
54
55
Vec2<int> xy() const { return Vec2<int>(x, y); }
56
57
ScreenCoords operator * (const float t) const
58
{
59
return ScreenCoords((int)(x * t), (int)(y * t), (u16)(z * t));
60
}
61
62
ScreenCoords operator / (const int t) const
63
{
64
return ScreenCoords(x / t, y / t, z / t);
65
}
66
67
ScreenCoords operator + (const ScreenCoords& oth) const
68
{
69
return ScreenCoords(x + oth.x, y + oth.y, z + oth.z);
70
}
71
};
72
73
struct DrawingCoords {
74
DrawingCoords() {}
75
DrawingCoords(s16 x, s16 y) : x(x), y(y) {}
76
77
s16 x;
78
s16 y;
79
};
80
81
struct alignas(16) VertexData {
82
Vec3Packedf texturecoords;
83
float clipw;
84
uint32_t color0;
85
uint32_t color1;
86
ScreenCoords screenpos;
87
float fogdepth;
88
};
89
90
struct ClipVertexData {
91
void Lerp(float t, const ClipVertexData &a, const ClipVertexData &b) {
92
clippos = ::Lerp(a.clippos, b.clippos, t);
93
// Ignore screenpos because Lerp() is only used pre-calculation of screenpos.
94
v.texturecoords = ::Lerp(a.v.texturecoords, b.v.texturecoords, t);
95
v.fogdepth = ::Lerp(a.v.fogdepth, b.v.fogdepth, t);
96
97
u16 t_int = (u16)(t * 256);
98
v.color0 = LerpInt<Vec4<int>, 256>(Vec4<int>::FromRGBA(a.v.color0), Vec4<int>::FromRGBA(b.v.color0), t_int).ToRGBA();
99
v.color1 = LerpInt<Vec3<int>, 256>(Vec3<int>::FromRGB(a.v.color1), Vec3<int>::FromRGB(b.v.color1), t_int).ToRGB();
100
}
101
102
bool OutsideRange() const {
103
return v.screenpos.x == 0x7FFFFFFF;
104
}
105
106
ClipCoords clippos;
107
VertexData v;
108
};
109
110
class VertexReader;
111
112
class SoftwareDrawEngine;
113
class SoftwareVertexReader;
114
115
class TransformUnit {
116
public:
117
TransformUnit();
118
~TransformUnit();
119
120
bool IsStarted();
121
122
static WorldCoords ModelToWorldNormal(const ModelCoords& coords);
123
static WorldCoords ModelToWorld(const ModelCoords& coords);
124
static ViewCoords WorldToView(const WorldCoords& coords);
125
static ClipCoords ViewToClip(const ViewCoords& coords);
126
static ScreenCoords ClipToScreen(const ClipCoords &coords, bool *outsideRangeFlag);
127
static inline DrawingCoords ScreenToDrawing(int x, int y) {
128
DrawingCoords ret;
129
// When offset > coord, this is negative and force-scissors.
130
ret.x = x / SCREEN_SCALE_FACTOR;
131
ret.y = y / SCREEN_SCALE_FACTOR;
132
return ret;
133
}
134
static inline DrawingCoords ScreenToDrawing(const ScreenCoords &coords) {
135
return ScreenToDrawing(coords.x, coords.y);
136
}
137
static ScreenCoords DrawingToScreen(const DrawingCoords &coords, u16 z);
138
139
void SubmitPrimitive(const void* vertices, const void* indices, GEPrimitiveType prim_type, int vertex_count, u32 vertex_type, int *bytesRead, SoftwareDrawEngine *drawEngine);
140
void SubmitImmVertex(const ClipVertexData &vert, SoftwareDrawEngine *drawEngine);
141
142
static bool GetCurrentSimpleVertices(int count, std::vector<GPUDebugVertex> &vertices, std::vector<u16> &indices);
143
144
void Flush(const char *reason);
145
void FlushIfOverlap(const char *reason, bool modifying, uint32_t addr, uint32_t stride, uint32_t w, uint32_t h);
146
void NotifyClutUpdate(const void *src);
147
148
void GetStats(char *buffer, size_t bufsize);
149
150
void SetDirty(SoftDirty flags);
151
SoftDirty GetDirty();
152
153
private:
154
ClipVertexData ReadVertex(const VertexReader &vreader, const TransformState &state);
155
void SendTriangle(CullType cullType, const ClipVertexData *verts, int provoking = 2);
156
157
u8 *decoded_ = nullptr;
158
BinManager *binner_ = nullptr;
159
160
// Normally max verts per prim is 3, but we temporarily need 4 to detect rectangles from strips.
161
ClipVertexData data_[4];
162
// This is the index of the next vert in data (or higher, may need modulus.)
163
int data_index_ = 0;
164
GEPrimitiveType prev_prim_ = GE_PRIM_POINTS;
165
bool hasDraws_ = false;
166
bool isImmDraw_ = false;
167
168
friend SoftwareVertexReader;
169
};
170
171
class SoftwareDrawEngine : public DrawEngineCommon {
172
public:
173
SoftwareDrawEngine();
174
~SoftwareDrawEngine();
175
176
void DeviceLost() override {}
177
void DeviceRestore(Draw::DrawContext *draw) override {}
178
179
void NotifyConfigChanged() override;
180
void DispatchFlush() override;
181
void DispatchSubmitPrim(const void *verts, const void *inds, GEPrimitiveType prim, int vertexCount, u32 vertType, bool clockwise, int *bytesRead) override;
182
void DispatchSubmitImm(GEPrimitiveType prim, TransformedVertex *buffer, int vertexCount, int cullMode, bool continuation) override;
183
184
VertexDecoder *FindVertexDecoder(u32 vtype);
185
186
TransformUnit transformUnit;
187
188
#if PPSSPP_ARCH(32BIT)
189
#undef new
190
void *operator new(size_t s) {
191
return AllocateAlignedMemory(s, 16);
192
}
193
void operator delete(void *p) {
194
FreeAlignedMemory(p);
195
}
196
#endif
197
198
protected:
199
bool UpdateUseHWTessellation(bool enable) const override { return false; }
200
};
201
202