-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpbgl.c
186 lines (147 loc) · 5.04 KB
/
pbgl.c
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
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <pbkit/pbkit.h>
#include "GL/gl.h"
#include "GL/glext.h"
#include "types.h"
#include "matrix.h"
#include "texture.h"
#include "array.h"
#include "error.h"
#include "texenv.h"
#include "push.h"
#include "state.h"
#include "misc.h"
#include "pbgl.h"
static GLboolean pbkit_initialized = GL_FALSE;
static GLboolean pbkit_do_deinit = GL_FALSE;
int pbgl_init(int init_pbkit) {
if (pbgl.active)
return -1; // don't double init
// remember who has to deinit pbkit later
pbkit_do_deinit = init_pbkit;
// user says they've already initialized pbkit
if (!init_pbkit) pbkit_initialized = GL_TRUE;
// init pbkit if needed
if (!pbkit_initialized) {
int err;
if ((err = pb_init()) != 0)
return -10 - err;
pbkit_initialized = GL_TRUE;
}
pb_show_front_screen();
GLuint *p = pb_begin();
// set fixed pipeline mode
p = push_command_parameter(p, NV097_SET_TRANSFORM_EXECUTION_MODE,
PBGL_MASK(NV097_SET_TRANSFORM_EXECUTION_MODE_MODE, NV097_SET_TRANSFORM_EXECUTION_MODE_MODE_FIXED) |
PBGL_MASK(NV097_SET_TRANSFORM_EXECUTION_MODE_RANGE_MODE, NV097_SET_TRANSFORM_EXECUTION_MODE_RANGE_MODE_PRIV));
// set default clear parameters that never change (for now)
p = push_command_parameter(p, NV097_SET_CLEAR_RECT_VERTICAL, (pb_back_buffer_height() << 16));
p = push_command_parameter(p, NV097_SET_CLEAR_RECT_HORIZONTAL, (pb_back_buffer_width() << 16));
// set some default crap
p = push_command_boolean(p, NV097_SET_SPECULAR_ENABLE, 1);
p = push_command_boolean(p, NV097_SET_NORMALIZATION_ENABLE, 0);
p = push_command_boolean(p, NV097_SET_ZMIN_MAX_CONTROL, 1);
p = push_command_boolean(p, NV097_SET_COMPRESS_ZBUFFER_EN, 1);
p = push_command_parameter(p, NV097_SET_CONTROL0, NV097_SET_CONTROL0_TEXTUREPERSPECTIVE);
p = push_command_parameter(p, NV097_SET_SKIN_MODE, NV097_SET_SKIN_MODE_OFF);
p = push_command_parameter(p, NV097_SET_SHADER_OTHER_STAGE_INPUT,
PBGL_MASK(NV097_SET_SHADER_OTHER_STAGE_INPUT_STAGE1, 0)
| PBGL_MASK(NV097_SET_SHADER_OTHER_STAGE_INPUT_STAGE2, 0)
| PBGL_MASK(NV097_SET_SHADER_OTHER_STAGE_INPUT_STAGE3, 0));
// push identity viewport parameters
p = push_command(p, NV097_SET_VIEWPORT_OFFSET, 4);
p = push_float(p, 0.f);
p = push_float(p, 0.f);
p = push_float(p, 0.f);
p = push_float(p, 0.f);
p = push_command(p, NV097_SET_VIEWPORT_SCALE, 4);
p = push_float(p, 1.f);
p = push_float(p, 1.f);
p = push_float(p, 1.f);
p = push_float(p, 1.f);
// push default z clip range
p = push_command_float(p, NV097_SET_CLIP_MIN, 0.f);
p = push_command_float(p, NV097_SET_CLIP_MAX, (GLfloat)PBGL_Z_MAX);
pb_end(p);
// push identity matrices everywhere
p = pb_begin();
p = push_command_matrix4x4(p, NV097_SET_PROJECTION_MATRIX, mat4_identity.v);
p = push_command_matrix4x4(p, NV097_SET_MODEL_VIEW_MATRIX, mat4_identity.v);
p = push_command_matrix4x4(p, NV097_SET_INVERSE_MODEL_VIEW_MATRIX, mat4_identity.v);
p = push_command_matrix4x4(p, NV097_SET_COMPOSITE_MATRIX, mat4_identity.v);
pb_end(p);
p = pb_begin();
for (GLuint i = 0, ofs = 0; i < TEXUNIT_COUNT; ++i, ofs += 4 * 4 * 4) {
p = pb_push1(p, NV20_TCL_PRIMITIVE_3D_TX_ENABLE(i), 0x0003FFC0);
p = push_command_matrix4x4(p, NV097_SET_TEXTURE_MATRIX + ofs, mat4_identity.v);
}
pb_end(p);
while (pb_busy());
// init texture manager
if (!pbgl_tex_init()) {
pb_kill();
pbkit_initialized = GL_FALSE;
return -3;
}
// HACK: preserve some vars that might've been set before init
const int swap_interval = pbgl.swap_interval;
// init default state
pbgl_state_init();
// disable all vertex arrays
pbgl_array_reset_all();
// flush state right away just in case
pbgl_state_flush();
// prepare for drawing the first frame
pb_reset();
pb_target_back_buffer();
// wait for anything that's still happening just in case (probably not necessary)
while (pb_busy());
// restore vars
pbgl.swap_interval = swap_interval;
// we're live
pbgl.active = GL_TRUE;
return 0;
}
void pbgl_set_swap_interval(int interval) {
pbgl.swap_interval = interval;
}
int pbgl_get_swap_interval(void) {
return pbgl.swap_interval;
}
void pbgl_swap_buffers(void) {
// swap buffers
while (pb_busy());
while (pb_finished());
// wait up to $swap_interval vblanks
// same code as in xsm64
GLint now = pb_get_vbl_counter();
while ((now - pbgl.last_swap) < pbgl.swap_interval)
now = pb_wait_for_vbl();
pbgl.last_swap = now;
pb_reset();
pb_target_back_buffer();
}
void *pbgl_alloc(unsigned int size, int dynamic) {
return MmAllocateContiguousMemoryEx(size, 0, PBGL_MAXRAM, 0, dynamic ? 0x404 : 0x04);
}
void pbgl_free(void *addr) {
if (addr) MmFreeContiguousMemory(addr);
}
void pbgl_shutdown(void) {
if (!pbgl.active)
return;
if (pbkit_initialized) {
while (pb_busy());
// only deinit pbkit if it was us who inited it
if (pbkit_do_deinit) {
pb_kill();
pbkit_initialized = GL_FALSE;
pbkit_do_deinit = GL_FALSE;
}
}
// free all textures
pbgl_tex_free();
pbgl.active = GL_FALSE;
}