Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / tests / graw / tri.c
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 <stdio.h>
6
7 #include "state_tracker/graw.h"
8 #include "pipe/p_screen.h"
9 #include "pipe/p_context.h"
10 #include "pipe/p_state.h"
11 #include "pipe/p_defines.h"
12
13 #include "util/u_memory.h"      /* Offset() */
14 #include "util/u_draw_quad.h"
15
16 enum pipe_format formats[] = {
17    PIPE_FORMAT_R8G8B8A8_UNORM,
18    PIPE_FORMAT_B8G8R8A8_UNORM,
19    PIPE_FORMAT_NONE
20 };
21
22 static const int WIDTH = 300;
23 static const int HEIGHT = 300;
24
25 static struct pipe_screen *screen = NULL;
26 static struct pipe_context *ctx = NULL;
27 static struct pipe_surface *surf = NULL;
28 static struct pipe_resource *tex = NULL;
29 static void *window = NULL;
30
31 struct vertex {
32    float position[4];
33    float color[4];
34 };
35
36 static struct vertex vertices[3] =
37 {
38    {
39       { 0.0f, -0.9f, 0.0f, 1.0f },
40       { 1.0f, 0.0f, 0.0f, 1.0f }
41    },
42    {
43       { -0.9f, 0.9f, 0.0f, 1.0f },
44       { 0.0f, 1.0f, 0.0f, 1.0f }
45    },
46    {
47       { 0.9f, 0.9f, 0.0f, 1.0f },
48       { 0.0f, 0.0f, 1.0f, 1.0f }
49    }
50 };
51
52
53
54
55 static void set_viewport( float x, float y,
56                           float width, float height,
57                           float near, float far)
58 {
59    float z = far;
60    float half_width = (float)width / 2.0f;
61    float half_height = (float)height / 2.0f;
62    float half_depth = ((float)far - (float)near) / 2.0f;
63    struct pipe_viewport_state vp;
64
65    vp.scale[0] = half_width;
66    vp.scale[1] = half_height;
67    vp.scale[2] = half_depth;
68    vp.scale[3] = 1.0f;
69
70    vp.translate[0] = half_width + x;
71    vp.translate[1] = half_height + y;
72    vp.translate[2] = half_depth + z;
73    vp.translate[3] = 0.0f;
74
75    ctx->set_viewport_state( ctx, &vp );
76 }
77
78 static void set_vertices( void )
79 {
80    struct pipe_vertex_element ve[2];
81    struct pipe_vertex_buffer vbuf;
82    void *handle;
83
84    memset(ve, 0, sizeof ve);
85
86    ve[0].src_offset = Offset(struct vertex, position);
87    ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
88    ve[1].src_offset = Offset(struct vertex, color);
89    ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
90
91    handle = ctx->create_vertex_elements_state(ctx, 2, ve);
92    ctx->bind_vertex_elements_state(ctx, handle);
93
94
95    vbuf.stride = sizeof( struct vertex );
96    vbuf.buffer_offset = 0;
97    vbuf.buffer = screen->user_buffer_create(screen,
98                                             vertices,
99                                             sizeof(vertices),
100                                             PIPE_BIND_VERTEX_BUFFER);
101
102    ctx->set_vertex_buffers(ctx, 1, &vbuf);
103 }
104
105 static void set_vertex_shader( void )
106 {
107    void *handle;
108    const char *text =
109       "VERT\n"
110       "DCL IN[0]\n"
111       "DCL IN[1]\n"
112       "DCL OUT[0], POSITION\n"
113       "DCL OUT[1], COLOR\n"
114       "  0: MOV OUT[1], IN[1]\n"
115       "  1: MOV OUT[0], IN[0]\n"
116       "  2: END\n";
117
118    handle = graw_parse_vertex_shader(ctx, text);
119    ctx->bind_vs_state(ctx, handle);
120 }
121
122 static void set_fragment_shader( void )
123 {
124    void *handle;
125    const char *text =
126       "FRAG\n"
127       "DCL IN[0], COLOR, LINEAR\n"
128       "DCL OUT[0], COLOR\n"
129       "  0: MOV OUT[0], IN[0]\n"
130       "  1: END\n";
131
132    handle = graw_parse_fragment_shader(ctx, text);
133    ctx->bind_fs_state(ctx, handle);
134 }
135
136
137 static void draw( void )
138 {
139    float clear_color[4] = {1,0,1,1};
140
141    ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
142    util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
143    ctx->flush(ctx, NULL);
144
145    graw_save_surface_to_file(ctx, surf, NULL);
146
147    screen->flush_frontbuffer(screen, tex, 0, 0, window);
148 }
149
150
151 static void init( void )
152 {
153    struct pipe_framebuffer_state fb;
154    struct pipe_resource templat;
155    struct pipe_surface surf_tmpl;
156    int i;
157
158    /* It's hard to say whether window or screen should be created
159     * first.  Different environments would prefer one or the other.
160     *
161     * Also, no easy way of querying supported formats if the screen
162     * cannot be created first.
163     */
164    for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
165       screen = graw_create_window_and_screen(0, 0, 300, 300,
166                                              formats[i],
167                                              &window);
168       if (window && screen)
169          break;
170    }
171    if (!screen || !window) {
172       fprintf(stderr, "Unable to create window\n");
173       exit(1);
174    }
175    
176    ctx = screen->context_create(screen, NULL);
177    if (ctx == NULL) {
178       fprintf(stderr, "Unable to create context!\n");
179       exit(3);
180    }
181
182    templat.target = PIPE_TEXTURE_2D;
183    templat.format = formats[i];
184    templat.width0 = WIDTH;
185    templat.height0 = HEIGHT;
186    templat.depth0 = 1;
187    templat.array_size = 1;
188    templat.last_level = 0;
189    templat.nr_samples = 1;
190    templat.bind = (PIPE_BIND_RENDER_TARGET |
191                    PIPE_BIND_DISPLAY_TARGET);
192    
193    tex = screen->resource_create(screen,
194                                  &templat);
195    if (tex == NULL) {
196       fprintf(stderr, "Unable to create screen texture!\n");
197       exit(4);
198    }
199
200    surf_tmpl.format = templat.format;
201    surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
202    surf_tmpl.u.tex.level = 0;
203    surf_tmpl.u.tex.first_layer = 0;
204    surf_tmpl.u.tex.last_layer = 0;
205    surf = ctx->create_surface(ctx, tex, &surf_tmpl);
206    if (surf == NULL) {
207       fprintf(stderr, "Unable to create tex surface!\n");
208       exit(5);
209    }
210
211    memset(&fb, 0, sizeof fb);
212    fb.nr_cbufs = 1;
213    fb.width = WIDTH;
214    fb.height = HEIGHT;
215    fb.cbufs[0] = surf;
216
217    ctx->set_framebuffer_state(ctx, &fb);
218    
219    {
220       struct pipe_blend_state blend;
221       void *handle;
222       memset(&blend, 0, sizeof blend);
223       blend.rt[0].colormask = PIPE_MASK_RGBA;
224       handle = ctx->create_blend_state(ctx, &blend);
225       ctx->bind_blend_state(ctx, handle);
226    }
227
228    {
229       struct pipe_depth_stencil_alpha_state depthstencil;
230       void *handle;
231       memset(&depthstencil, 0, sizeof depthstencil);
232       handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
233       ctx->bind_depth_stencil_alpha_state(ctx, handle);
234    }
235
236    {
237       struct pipe_rasterizer_state rasterizer;
238       void *handle;
239       memset(&rasterizer, 0, sizeof rasterizer);
240       rasterizer.cull_face = PIPE_FACE_NONE;
241       rasterizer.gl_rasterization_rules = 1;
242       handle = ctx->create_rasterizer_state(ctx, &rasterizer);
243       ctx->bind_rasterizer_state(ctx, handle);
244    }
245
246    set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
247    set_vertices();
248    set_vertex_shader();
249    set_fragment_shader();
250 }
251
252 static void args(int argc, char *argv[])
253 {
254    int i;
255
256    for (i = 1; i < argc;) {
257       if (graw_parse_args(&i, argc, argv)) {
258          continue;
259       }
260       exit(1);
261    }
262 }
263
264 int main( int argc, char *argv[] )
265 {
266    args(argc, argv);
267    init();
268
269    graw_set_display_func( draw );
270    graw_main_loop();
271    return 0;
272 }