Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/tests/graw/quad-sample.c
4565 views
1
/* Display a cleared blue window. This demo has no dependencies on
2
* any utility code, just the graw interface and gallium.
3
*/
4
5
#include "frontend/graw.h"
6
#include "pipe/p_screen.h"
7
#include "pipe/p_context.h"
8
#include "pipe/p_shader_tokens.h"
9
#include "pipe/p_state.h"
10
#include "pipe/p_defines.h"
11
12
#include "util/u_debug.h" /* debug_dump_surface_bmp() */
13
#include "util/u_inlines.h"
14
#include "util/u_memory.h" /* Offset() */
15
#include "util/u_draw_quad.h"
16
#include "util/u_box.h"
17
18
#include <stdio.h>
19
20
enum pipe_format formats[] = {
21
PIPE_FORMAT_RGBA8888_UNORM,
22
PIPE_FORMAT_BGRA8888_UNORM,
23
PIPE_FORMAT_NONE
24
};
25
26
static const int WIDTH = 300;
27
static const int HEIGHT = 300;
28
29
static struct pipe_screen *screen = NULL;
30
static struct pipe_context *ctx = NULL;
31
static struct pipe_resource *rttex = NULL;
32
static struct pipe_resource *samptex = NULL;
33
static struct pipe_surface *surf = NULL;
34
static struct pipe_sampler_view *sv = NULL;
35
static void *sampler = NULL;
36
static void *window = NULL;
37
38
struct vertex {
39
float position[4];
40
float color[4];
41
};
42
43
static struct vertex vertices[] =
44
{
45
{ { 0.9, -0.9, 0.0, 1.0 },
46
{ 1, 0, 0, 1 } },
47
48
{ { 0.9, 0.9, 0.0, 1.0 },
49
{ 1, 1, 0, 1 } },
50
51
{ {-0.9, 0.9, 0.0, 1.0 },
52
{ 0, 1, 0, 1 } },
53
54
{ {-0.9, -0.9, 0.0, 1.0 },
55
{ 0, 0, 0, 1 } },
56
};
57
58
59
60
61
static void set_viewport( float x, float y,
62
float width, float height,
63
float zNear, float zFar)
64
{
65
float z = zFar;
66
float half_width = (float)width / 2.0f;
67
float half_height = (float)height / 2.0f;
68
float half_depth = ((float)zFar - (float)zNear) / 2.0f;
69
struct pipe_viewport_state vp;
70
71
vp.scale[0] = half_width;
72
vp.scale[1] = half_height;
73
vp.scale[2] = half_depth;
74
75
vp.translate[0] = half_width + x;
76
vp.translate[1] = half_height + y;
77
vp.translate[2] = half_depth + z;
78
79
vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
80
vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
81
vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
82
vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
83
84
ctx->set_viewport_states( ctx, 0, 1, &vp );
85
}
86
87
static void set_vertices( void )
88
{
89
struct pipe_vertex_element ve[2];
90
struct pipe_vertex_buffer vbuf;
91
void *handle;
92
93
memset(ve, 0, sizeof ve);
94
95
ve[0].src_offset = Offset(struct vertex, position);
96
ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
97
ve[1].src_offset = Offset(struct vertex, color);
98
ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
99
100
handle = ctx->create_vertex_elements_state(ctx, 2, ve);
101
ctx->bind_vertex_elements_state(ctx, handle);
102
103
memset(&vbuf, 0, sizeof vbuf);
104
105
vbuf.stride = sizeof( struct vertex );
106
vbuf.buffer_offset = 0;
107
vbuf.buffer.resource = pipe_buffer_create_with_data(ctx,
108
PIPE_BIND_VERTEX_BUFFER,
109
PIPE_USAGE_DEFAULT,
110
sizeof(vertices),
111
vertices);
112
113
ctx->set_vertex_buffers(ctx, 0, 1, 0, false, &vbuf);
114
}
115
116
static void set_vertex_shader( void )
117
{
118
void *handle;
119
const char *text =
120
"VERT\n"
121
"DCL IN[0]\n"
122
"DCL IN[1]\n"
123
"DCL OUT[0], POSITION\n"
124
"DCL OUT[1], GENERIC[0]\n"
125
" 0: MOV OUT[1], IN[1]\n"
126
" 1: MOV OUT[0], IN[0]\n"
127
" 2: END\n";
128
129
handle = graw_parse_vertex_shader(ctx, text);
130
ctx->bind_vs_state(ctx, handle);
131
}
132
133
static void set_fragment_shader( void )
134
{
135
void *handle;
136
const char *text =
137
"FRAG\n"
138
"DCL IN[0], GENERIC[0], PERSPECTIVE\n"
139
"DCL OUT[0], COLOR\n"
140
"DCL TEMP[0]\n"
141
"DCL SAMP[0]\n"
142
"DCL RES[0], 2D, FLOAT\n"
143
" 0: SAMPLE TEMP[0], IN[0], RES[0], SAMP[0]\n"
144
" 1: MOV OUT[0], TEMP[0]\n"
145
" 2: END\n";
146
147
handle = graw_parse_fragment_shader(ctx, text);
148
ctx->bind_fs_state(ctx, handle);
149
}
150
151
152
static void draw( void )
153
{
154
union pipe_color_union clear_color = { {.5,.5,.5,1} };
155
156
ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);
157
util_draw_arrays(ctx, PIPE_PRIM_QUADS, 0, 4);
158
ctx->flush(ctx, NULL, 0);
159
160
graw_save_surface_to_file(ctx, surf, NULL);
161
162
screen->flush_frontbuffer(screen, ctx, rttex, 0, 0, window, NULL);
163
}
164
165
#define SIZE 16
166
167
static void init_tex( void )
168
{
169
struct pipe_sampler_view sv_template;
170
struct pipe_sampler_state sampler_desc;
171
struct pipe_resource templat;
172
struct pipe_box box;
173
ubyte tex2d[SIZE][SIZE][4];
174
int s, t;
175
176
#if (SIZE != 2)
177
for (s = 0; s < SIZE; s++) {
178
for (t = 0; t < SIZE; t++) {
179
if (0) {
180
int x = (s ^ t) & 1;
181
tex2d[t][s][0] = (x) ? 0 : 63;
182
tex2d[t][s][1] = (x) ? 0 : 128;
183
tex2d[t][s][2] = 0;
184
tex2d[t][s][3] = 0xff;
185
}
186
else {
187
int x = ((s ^ t) >> 2) & 1;
188
tex2d[t][s][0] = s*255/(SIZE-1);
189
tex2d[t][s][1] = t*255/(SIZE-1);
190
tex2d[t][s][2] = (x) ? 0 : 128;
191
tex2d[t][s][3] = 0xff;
192
}
193
}
194
}
195
#else
196
tex2d[0][0][0] = 0;
197
tex2d[0][0][1] = 255;
198
tex2d[0][0][2] = 255;
199
tex2d[0][0][3] = 0;
200
201
tex2d[0][1][0] = 0;
202
tex2d[0][1][1] = 0;
203
tex2d[0][1][2] = 255;
204
tex2d[0][1][3] = 255;
205
206
tex2d[1][0][0] = 255;
207
tex2d[1][0][1] = 255;
208
tex2d[1][0][2] = 0;
209
tex2d[1][0][3] = 255;
210
211
tex2d[1][1][0] = 255;
212
tex2d[1][1][1] = 0;
213
tex2d[1][1][2] = 0;
214
tex2d[1][1][3] = 255;
215
#endif
216
217
memset(&templat, 0, sizeof(templat));
218
templat.target = PIPE_TEXTURE_2D;
219
templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
220
templat.width0 = SIZE;
221
templat.height0 = SIZE;
222
templat.depth0 = 1;
223
templat.last_level = 0;
224
templat.bind = PIPE_BIND_SAMPLER_VIEW;
225
226
227
samptex = screen->resource_create(screen,
228
&templat);
229
if (samptex == NULL)
230
exit(4);
231
232
u_box_2d(0,0,SIZE,SIZE, &box);
233
234
ctx->texture_subdata(ctx,
235
samptex,
236
0,
237
PIPE_MAP_WRITE,
238
&box,
239
tex2d,
240
sizeof tex2d[0],
241
sizeof tex2d);
242
243
/* Possibly read back & compare against original data:
244
*/
245
if (0)
246
{
247
struct pipe_transfer *t;
248
uint32_t *ptr;
249
ptr = pipe_texture_map(ctx, samptex,
250
0, 0, /* level, layer */
251
PIPE_MAP_READ,
252
0, 0, SIZE, SIZE, &t); /* x, y, width, height */
253
254
if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
255
assert(0);
256
exit(9);
257
}
258
259
ctx->texture_unmap(ctx, t);
260
}
261
262
memset(&sv_template, 0, sizeof sv_template);
263
sv_template.format = samptex->format;
264
sv_template.texture = samptex;
265
sv_template.swizzle_r = 0;
266
sv_template.swizzle_g = 1;
267
sv_template.swizzle_b = 2;
268
sv_template.swizzle_a = 3;
269
sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
270
if (sv == NULL)
271
exit(5);
272
273
ctx->set_sampler_views(ctx, PIPE_SHADER_FRAGMENT, 0, 1, 0, &sv);
274
275
276
memset(&sampler_desc, 0, sizeof sampler_desc);
277
sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
278
sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
279
sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
280
sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
281
sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
282
sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
283
sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
284
sampler_desc.compare_func = 0;
285
sampler_desc.normalized_coords = 1;
286
sampler_desc.max_anisotropy = 0;
287
288
sampler = ctx->create_sampler_state(ctx, &sampler_desc);
289
if (sampler == NULL)
290
exit(6);
291
292
ctx->bind_sampler_states(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sampler);
293
294
}
295
296
static void init( void )
297
{
298
struct pipe_framebuffer_state fb;
299
struct pipe_resource templat;
300
struct pipe_surface surf_tmpl;
301
int i;
302
303
/* It's hard to say whether window or screen should be created
304
* first. Different environments would prefer one or the other.
305
*
306
* Also, no easy way of querying supported formats if the screen
307
* cannot be created first.
308
*/
309
for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
310
screen = graw_create_window_and_screen(0, 0, 300, 300,
311
formats[i],
312
&window);
313
if (window && screen)
314
break;
315
}
316
if (!screen || !window) {
317
fprintf(stderr, "Unable to create window\n");
318
exit(1);
319
}
320
321
ctx = screen->context_create(screen, NULL, 0);
322
if (ctx == NULL)
323
exit(3);
324
325
memset(&templat, 0, sizeof(templat));
326
templat.target = PIPE_TEXTURE_2D;
327
templat.format = formats[i];
328
templat.width0 = WIDTH;
329
templat.height0 = HEIGHT;
330
templat.depth0 = 1;
331
templat.array_size = 1;
332
templat.last_level = 0;
333
templat.bind = (PIPE_BIND_RENDER_TARGET |
334
PIPE_BIND_DISPLAY_TARGET);
335
336
rttex = screen->resource_create(screen,
337
&templat);
338
if (rttex == NULL)
339
exit(4);
340
341
surf_tmpl.format = templat.format;
342
surf_tmpl.u.tex.level = 0;
343
surf_tmpl.u.tex.first_layer = 0;
344
surf_tmpl.u.tex.last_layer = 0;
345
surf = ctx->create_surface(ctx, rttex, &surf_tmpl);
346
if (surf == NULL)
347
exit(5);
348
349
memset(&fb, 0, sizeof fb);
350
fb.nr_cbufs = 1;
351
fb.width = WIDTH;
352
fb.height = HEIGHT;
353
fb.cbufs[0] = surf;
354
355
ctx->set_framebuffer_state(ctx, &fb);
356
357
{
358
struct pipe_blend_state blend;
359
void *handle;
360
memset(&blend, 0, sizeof blend);
361
blend.rt[0].colormask = PIPE_MASK_RGBA;
362
handle = ctx->create_blend_state(ctx, &blend);
363
ctx->bind_blend_state(ctx, handle);
364
}
365
366
{
367
struct pipe_depth_stencil_alpha_state depthstencil;
368
void *handle;
369
memset(&depthstencil, 0, sizeof depthstencil);
370
handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
371
ctx->bind_depth_stencil_alpha_state(ctx, handle);
372
}
373
374
{
375
struct pipe_rasterizer_state rasterizer;
376
void *handle;
377
memset(&rasterizer, 0, sizeof rasterizer);
378
rasterizer.cull_face = PIPE_FACE_NONE;
379
rasterizer.half_pixel_center = 1;
380
rasterizer.bottom_edge_rule = 1;
381
rasterizer.depth_clip_near = 1;
382
rasterizer.depth_clip_far = 1;
383
handle = ctx->create_rasterizer_state(ctx, &rasterizer);
384
ctx->bind_rasterizer_state(ctx, handle);
385
}
386
387
set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
388
389
init_tex();
390
391
set_vertices();
392
set_vertex_shader();
393
set_fragment_shader();
394
}
395
396
static void args(int argc, char *argv[])
397
{
398
int i;
399
400
for (i = 1; i < argc;) {
401
if (graw_parse_args(&i, argc, argv)) {
402
continue;
403
}
404
exit(1);
405
}
406
}
407
408
409
int main( int argc, char *argv[] )
410
{
411
args(argc, argv);
412
init();
413
414
graw_set_display_func( draw );
415
graw_main_loop();
416
return 0;
417
}
418
419