• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Display a huge triangle on a 8192x8192 canvas.
2  * This demo has no dependencies on any utility code,
3  * just the graw interface and gallium.
4  */
5 
6 #include "graw_util.h"
7 #include "util/u_debug.h"
8 
9 #include <stdio.h>
10 
11 static struct graw_info info;
12 
13 static const int WIDTH = 4*2048;
14 static const int HEIGHT = 4*2048;
15 
16 
17 struct vertex {
18    float position[4];
19    float color[4];
20 };
21 
22 static boolean FlatShade = FALSE;
23 
24 
25 static struct vertex vertices[3] =
26 {
27    {
28       { -1.0f, -1.0f, 0.0f, 1.0f },
29       { 1.0f, 0.0f, 0.0f, 1.0f }
30    },
31    {
32       { -1.0f, 1.0f, 0.0f, 1.0f },
33       { 0.0f, 1.0f, 0.0f, 1.0f }
34    },
35    {
36       { 1.0f, 1.0f, 0.0f, 1.0f },
37       { 0.0f, 0.0f, 1.0f, 1.0f }
38    }
39 };
40 
41 
set_vertices(void)42 static void set_vertices( void )
43 {
44    struct pipe_vertex_element ve[2];
45    struct pipe_vertex_buffer vbuf;
46    void *handle;
47 
48    memset(ve, 0, sizeof ve);
49 
50    ve[0].src_offset = Offset(struct vertex, position);
51    ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
52    ve[1].src_offset = Offset(struct vertex, color);
53    ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
54 
55    handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
56    info.ctx->bind_vertex_elements_state(info.ctx, handle);
57 
58    memset(&vbuf, 0, sizeof vbuf);
59 
60    vbuf.stride = sizeof( struct vertex );
61    vbuf.buffer_offset = 0;
62    vbuf.buffer = pipe_buffer_create_with_data(info.ctx,
63                                               PIPE_BIND_VERTEX_BUFFER,
64                                               PIPE_USAGE_DEFAULT,
65                                               sizeof(vertices),
66                                               vertices);
67 
68    info.ctx->set_vertex_buffers(info.ctx, 0, 1, &vbuf);
69 }
70 
71 
set_vertex_shader(void)72 static void set_vertex_shader( void )
73 {
74    void *handle;
75    const char *text =
76       "VERT\n"
77       "DCL IN[0]\n"
78       "DCL IN[1]\n"
79       "DCL OUT[0], POSITION\n"
80       "DCL OUT[1], COLOR\n"
81       "  0: MOV OUT[1], IN[1]\n"
82       "  1: MOV OUT[0], IN[0]\n"
83       "  2: END\n";
84 
85    handle = graw_parse_vertex_shader(info.ctx, text);
86    info.ctx->bind_vs_state(info.ctx, handle);
87 }
88 
89 
set_fragment_shader(void)90 static void set_fragment_shader( void )
91 {
92    void *handle;
93    const char *text =
94       "FRAG\n"
95       "DCL IN[0], COLOR, LINEAR\n"
96       "DCL OUT[0], COLOR\n"
97       "  0: MOV OUT[0], IN[0]\n"
98       "  1: END\n";
99 
100    handle = graw_parse_fragment_shader(info.ctx, text);
101    info.ctx->bind_fs_state(info.ctx, handle);
102 }
103 
104 
draw(void)105 static void draw( void )
106 {
107    union pipe_color_union clear_color = { {1,0,1,1} };
108 
109    info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
110    util_draw_arrays(info.ctx, PIPE_PRIM_TRIANGLES, 0, 3);
111    info.ctx->flush(info.ctx, NULL, 0);
112 
113    graw_save_surface_to_file(info.ctx, info.color_surf[0], NULL);
114 
115    graw_util_flush_front(&info);
116 }
117 
118 
init(void)119 static void init( void )
120 {
121    if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE))
122       exit(1);
123 
124    graw_util_default_state(&info, FALSE);
125 
126    {
127       struct pipe_rasterizer_state rasterizer;
128       void *handle;
129       memset(&rasterizer, 0, sizeof rasterizer);
130       rasterizer.cull_face = PIPE_FACE_NONE;
131       rasterizer.half_pixel_center = 1;
132       rasterizer.bottom_edge_rule = 1;
133       rasterizer.flatshade = FlatShade;
134       rasterizer.depth_clip = 1;
135       handle = info.ctx->create_rasterizer_state(info.ctx, &rasterizer);
136       info.ctx->bind_rasterizer_state(info.ctx, handle);
137    }
138 
139 
140    graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 1000);
141 
142    set_vertices();
143    set_vertex_shader();
144    set_fragment_shader();
145 }
146 
args(int argc,char * argv[])147 static void args(int argc, char *argv[])
148 {
149    int i;
150 
151    for (i = 1; i < argc; ) {
152       if (graw_parse_args(&i, argc, argv)) {
153          /* ok */
154       }
155       else if (strcmp(argv[i], "-f") == 0) {
156          FlatShade = TRUE;
157          i++;
158       }
159       else {
160          printf("Invalid arg %s\n", argv[i]);
161          exit(1);
162       }
163    }
164 }
165 
main(int argc,char * argv[])166 int main( int argc, char *argv[] )
167 {
168    args(argc, argv);
169    init();
170 
171    graw_set_display_func( draw );
172    graw_main_loop();
173    return 0;
174 }
175