1
/* Display a cleared blue window. This demo has no dependencies on
2
* any utility code, just the graw interface and gallium.
5
#include "state_tracker/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
#include <stdio.h> /* for fread(), etc */
13
#include "util/u_debug.h" /* debug_dump_surface_bmp() */
14
#include "util/u_inlines.h"
15
#include "util/u_memory.h" /* Offset() */
16
#include "util/u_draw_quad.h"
17
#include "util/u_box.h"
19
static const char *filename = NULL;
20
unsigned show_fps = 0;
21
unsigned draw_strip = 0;
24
static void usage(char *name)
26
fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
28
fprintf(stderr, "\n" );
29
fprintf(stderr, "options:\n");
30
fprintf(stderr, " -fps show frames per second\n");
31
fprintf(stderr, " -strip renders a triangle strip\n");
36
enum pipe_format formats[] = {
37
PIPE_FORMAT_R8G8B8A8_UNORM,
38
PIPE_FORMAT_B8G8R8A8_UNORM,
42
static const int WIDTH = 250;
43
static const int HEIGHT = 250;
45
static struct pipe_screen *screen = NULL;
46
static struct pipe_context *ctx = NULL;
47
static struct pipe_resource *rttex = NULL;
48
static struct pipe_resource *constbuf1 = NULL;
49
static struct pipe_resource *constbuf2 = NULL;
50
static struct pipe_surface *surf = NULL;
51
static struct pipe_sampler_view *sv = NULL;
52
static void *sampler = NULL;
53
static void *window = NULL;
54
static struct pipe_resource *samptex = NULL;
63
/* Vertex data matches progs/fp/fp-tri.c, but flipped in Y dimension
64
* so that the final images are the same.
66
static struct vertex vertices[] =
68
{ { 0.9, 0.9, 0.0, 1.0 },
74
{ { 0.9, -0.9, 0.0, 1.0 },
80
{ {-0.9, 0.0, 0.0, 1.0 },
87
static struct vertex vertices_strip[] =
89
{ { 0.9, 0.9, 0.0, 1.0 },
95
{ { 0.9, -0.9, 0.0, 1.0 },
101
{ {-0.9, 0.9, 0.0, 1.0 },
107
{ {-0.9, -0.9, 0.0, 1.0 },
114
static float constants1[] =
132
static float constants2[] =
150
static void init_fs_constbuf( void )
152
struct pipe_resource templat;
155
templat.target = PIPE_BUFFER;
156
templat.format = PIPE_FORMAT_R8_UNORM;
157
templat.width0 = sizeof(constants1);
160
templat.last_level = 0;
161
templat.nr_samples = 1;
162
templat.bind = PIPE_BIND_CONSTANT_BUFFER;
164
constbuf1 = screen->resource_create(screen, &templat);
165
if (constbuf1 == NULL)
167
constbuf2 = screen->resource_create(screen, &templat);
168
if (constbuf2 == NULL)
172
u_box_2d(0,0,sizeof(constants1),1, &box);
174
ctx->transfer_inline_write(ctx,
184
ctx->set_constant_buffer(ctx,
185
PIPE_SHADER_GEOMETRY, 0,
189
u_box_2d(0,0,sizeof(constants2),1, &box);
191
ctx->transfer_inline_write(ctx,
201
ctx->set_constant_buffer(ctx,
202
PIPE_SHADER_GEOMETRY, 1,
208
static void set_viewport( float x, float y,
209
float width, float height,
210
float near, float far)
213
float half_width = (float)width / 2.0f;
214
float half_height = (float)height / 2.0f;
215
float half_depth = ((float)far - (float)near) / 2.0f;
216
struct pipe_viewport_state vp;
218
vp.scale[0] = half_width;
219
vp.scale[1] = half_height;
220
vp.scale[2] = half_depth;
223
vp.translate[0] = half_width + x;
224
vp.translate[1] = half_height + y;
225
vp.translate[2] = half_depth + z;
226
vp.translate[3] = 0.0f;
228
ctx->set_viewport_state( ctx, &vp );
231
static void set_vertices( void )
233
struct pipe_vertex_element ve[4];
234
struct pipe_vertex_buffer vbuf;
237
memset(ve, 0, sizeof ve);
239
ve[0].src_offset = Offset(struct vertex, position);
240
ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
241
ve[1].src_offset = Offset(struct vertex, color);
242
ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
243
ve[2].src_offset = Offset(struct vertex, texcoord);
244
ve[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
245
ve[3].src_offset = Offset(struct vertex, generic);
246
ve[3].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
248
handle = ctx->create_vertex_elements_state(ctx, 4, ve);
249
ctx->bind_vertex_elements_state(ctx, handle);
251
vbuf.stride = sizeof( struct vertex );
252
vbuf.buffer_offset = 0;
254
vbuf.max_index = sizeof(vertices_strip) / vbuf.stride;
255
vbuf.buffer = screen->user_buffer_create(screen,
257
sizeof(vertices_strip),
258
PIPE_BIND_VERTEX_BUFFER);
260
vbuf.max_index = sizeof(vertices) / vbuf.stride;
261
vbuf.buffer = screen->user_buffer_create(screen,
264
PIPE_BIND_VERTEX_BUFFER);
267
ctx->set_vertex_buffers(ctx, 1, &vbuf);
270
static void set_vertex_shader( void )
279
"DCL OUT[0], POSITION\n"
280
"DCL OUT[1], COLOR[0]\n"
281
"DCL OUT[2], GENERIC[0]\n"
282
"DCL OUT[3], GENERIC[1]\n"
283
" MOV OUT[0], IN[0]\n"
284
" MOV OUT[1], IN[1]\n"
285
" MOV OUT[2], IN[2]\n"
286
" MOV OUT[3], IN[3]\n"
289
handle = graw_parse_vertex_shader(ctx, text);
290
ctx->bind_vs_state(ctx, handle);
293
static void set_fragment_shader( void )
298
"DCL IN[0], COLOR, LINEAR\n"
299
"DCL OUT[0], COLOR\n"
300
" 0: MOV OUT[0], IN[0]\n"
303
handle = graw_parse_fragment_shader(ctx, text);
304
ctx->bind_fs_state(ctx, handle);
308
static void set_geometry_shader( void )
315
if ((f = fopen(filename, "r")) == NULL) {
316
fprintf(stderr, "Couldn't open %s\n", filename);
320
sz = fread(buf, 1, sizeof(buf), f);
322
printf("file too long\n");
325
printf("%.*s\n", sz, buf);
328
handle = graw_parse_geometry_shader(ctx, buf);
329
ctx->bind_gs_state(ctx, handle);
334
static void draw( void )
336
float clear_color[4] = {.1,.3,.5,0};
338
ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
340
util_draw_arrays(ctx, PIPE_PRIM_TRIANGLE_STRIP, 0, 4);
342
util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
344
ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
347
/* At the moment, libgraw leaks out/makes available some of the
348
* symbols from gallium/auxiliary, including these debug helpers.
349
* Will eventually want to bless some of these paths, and lock the
350
* others down so they aren't accessible from test programs.
352
* This currently just happens to work on debug builds - a release
353
* build will probably fail to link here:
355
debug_dump_surface_bmp(ctx, "result.bmp", surf);
358
screen->flush_frontbuffer(screen, surf, window);
363
static void init_tex( void )
365
struct pipe_sampler_view sv_template;
366
struct pipe_sampler_state sampler_desc;
367
struct pipe_resource templat;
369
ubyte tex2d[SIZE][SIZE][4];
373
for (s = 0; s < SIZE; s++) {
374
for (t = 0; t < SIZE; t++) {
377
tex2d[t][s][0] = (x) ? 0 : 63;
378
tex2d[t][s][1] = (x) ? 0 : 128;
380
tex2d[t][s][3] = 0xff;
383
int x = ((s ^ t) >> 2) & 1;
384
tex2d[t][s][0] = s*255/(SIZE-1);
385
tex2d[t][s][1] = t*255/(SIZE-1);
386
tex2d[t][s][2] = (x) ? 0 : 128;
387
tex2d[t][s][3] = 0xff;
393
tex2d[0][0][1] = 255;
394
tex2d[0][0][2] = 255;
399
tex2d[0][1][2] = 255;
400
tex2d[0][1][3] = 255;
402
tex2d[1][0][0] = 255;
403
tex2d[1][0][1] = 255;
405
tex2d[1][0][3] = 255;
407
tex2d[1][1][0] = 255;
410
tex2d[1][1][3] = 255;
413
templat.target = PIPE_TEXTURE_2D;
414
templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
415
templat.width0 = SIZE;
416
templat.height0 = SIZE;
418
templat.last_level = 0;
419
templat.nr_samples = 1;
420
templat.bind = PIPE_BIND_SAMPLER_VIEW;
423
samptex = screen->resource_create(screen,
428
u_box_2d(0,0,SIZE,SIZE, &box);
430
ctx->transfer_inline_write(ctx,
439
/* Possibly read back & compare against original data:
443
struct pipe_transfer *t;
445
t = pipe_get_transfer(ctx, samptex,
446
0, 0, 0, /* face, level, zslice */
448
0, 0, SIZE, SIZE); /* x, y, width, height */
450
ptr = ctx->transfer_map(ctx, t);
452
if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
457
ctx->transfer_unmap(ctx, t);
459
ctx->transfer_destroy(ctx, t);
462
memset(&sv_template, 0, sizeof sv_template);
463
sv_template.format = samptex->format;
464
sv_template.texture = samptex;
465
sv_template.first_level = 0;
466
sv_template.last_level = 0;
467
sv_template.swizzle_r = 0;
468
sv_template.swizzle_g = 1;
469
sv_template.swizzle_b = 2;
470
sv_template.swizzle_a = 3;
471
sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
475
ctx->set_fragment_sampler_views(ctx, 1, &sv);
478
memset(&sampler_desc, 0, sizeof sampler_desc);
479
sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
480
sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
481
sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
482
sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
483
sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
484
sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
485
sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
486
sampler_desc.compare_func = 0;
487
sampler_desc.normalized_coords = 1;
488
sampler_desc.max_anisotropy = 0;
490
sampler = ctx->create_sampler_state(ctx, &sampler_desc);
494
ctx->bind_fragment_sampler_states(ctx, 1, &sampler);
498
static void init( void )
500
struct pipe_framebuffer_state fb;
501
struct pipe_resource templat;
504
/* It's hard to say whether window or screen should be created
505
* first. Different environments would prefer one or the other.
507
* Also, no easy way of querying supported formats if the screen
508
* cannot be created first.
511
window == NULL && formats[i] != PIPE_FORMAT_NONE;
514
screen = graw_create_window_and_screen(0,0,WIDTH,HEIGHT,
519
ctx = screen->context_create(screen, NULL);
523
templat.target = PIPE_TEXTURE_2D;
524
templat.format = formats[i];
525
templat.width0 = WIDTH;
526
templat.height0 = HEIGHT;
528
templat.last_level = 0;
529
templat.nr_samples = 1;
530
templat.bind = (PIPE_BIND_RENDER_TARGET |
531
PIPE_BIND_DISPLAY_TARGET);
533
rttex = screen->resource_create(screen,
538
surf = screen->get_tex_surface(screen, rttex, 0, 0, 0,
539
PIPE_BIND_RENDER_TARGET |
540
PIPE_BIND_DISPLAY_TARGET);
544
memset(&fb, 0, sizeof fb);
550
ctx->set_framebuffer_state(ctx, &fb);
553
struct pipe_blend_state blend;
555
memset(&blend, 0, sizeof blend);
556
blend.rt[0].colormask = PIPE_MASK_RGBA;
557
handle = ctx->create_blend_state(ctx, &blend);
558
ctx->bind_blend_state(ctx, handle);
562
struct pipe_depth_stencil_alpha_state depthstencil;
564
memset(&depthstencil, 0, sizeof depthstencil);
565
handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
566
ctx->bind_depth_stencil_alpha_state(ctx, handle);
570
struct pipe_rasterizer_state rasterizer;
572
memset(&rasterizer, 0, sizeof rasterizer);
573
rasterizer.cull_face = PIPE_FACE_NONE;
574
rasterizer.gl_rasterization_rules = 1;
575
handle = ctx->create_rasterizer_state(ctx, &rasterizer);
576
ctx->bind_rasterizer_state(ctx, handle);
579
set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
586
set_fragment_shader();
587
set_geometry_shader();
590
static void args(int argc, char *argv[])
594
for (i = 1; i < argc; i++) {
595
if (strcmp(argv[i], "-fps") == 0) {
598
else if (strcmp(argv[i], "-strip") == 0) {
601
else if (i == argc - 1) {
616
int main( int argc, char *argv[] )
621
graw_set_display_func( draw );