graw: emit warnings when context/surface creation failes
[mesa.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_debug.h" /* debug_dump_surface_bmp() */
14 #include "util/u_memory.h" /* Offset() */
15 #include "util/u_draw_quad.h"
16
17 enum pipe_format formats[] = {
18 PIPE_FORMAT_R8G8B8A8_UNORM,
19 PIPE_FORMAT_B8G8R8A8_UNORM,
20 PIPE_FORMAT_NONE
21 };
22
23 static const int WIDTH = 300;
24 static const int HEIGHT = 300;
25
26 static struct pipe_screen *screen = NULL;
27 static struct pipe_context *ctx = NULL;
28 static struct pipe_surface *surf = 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.max_index = sizeof(vertices) / vbuf.stride;
97 vbuf.buffer_offset = 0;
98 vbuf.buffer = screen->user_buffer_create(screen,
99 vertices,
100 sizeof(vertices),
101 PIPE_BIND_VERTEX_BUFFER);
102
103 ctx->set_vertex_buffers(ctx, 1, &vbuf);
104 }
105
106 static void set_vertex_shader( void )
107 {
108 void *handle;
109 const char *text =
110 "VERT\n"
111 "DCL IN[0]\n"
112 "DCL IN[1]\n"
113 "DCL OUT[0], POSITION\n"
114 "DCL OUT[1], COLOR\n"
115 " 0: MOV OUT[1], IN[1]\n"
116 " 1: MOV OUT[0], IN[0]\n"
117 " 2: END\n";
118
119 handle = graw_parse_vertex_shader(ctx, text);
120 ctx->bind_vs_state(ctx, handle);
121 }
122
123 static void set_fragment_shader( void )
124 {
125 void *handle;
126 const char *text =
127 "FRAG\n"
128 "DCL IN[0], COLOR, LINEAR\n"
129 "DCL OUT[0], COLOR\n"
130 " 0: MOV OUT[0], IN[0]\n"
131 " 1: END\n";
132
133 handle = graw_parse_fragment_shader(ctx, text);
134 ctx->bind_fs_state(ctx, handle);
135 }
136
137
138 static void draw( void )
139 {
140 float clear_color[4] = {1,0,1,1};
141
142 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
143 util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
144 ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
145
146 #if 0
147 /* At the moment, libgraw leaks out/makes available some of the
148 * symbols from gallium/auxiliary, including these debug helpers.
149 * Will eventually want to bless some of these paths, and lock the
150 * others down so they aren't accessible from test programs.
151 *
152 * This currently just happens to work on debug builds - a release
153 * build will probably fail to link here:
154 */
155 debug_dump_surface_bmp(ctx, "result.bmp", surf);
156 #endif
157
158 screen->flush_frontbuffer(screen, surf, window);
159 }
160
161
162 static void init( void )
163 {
164 struct pipe_framebuffer_state fb;
165 struct pipe_resource *tex, templat;
166 int i;
167
168 /* It's hard to say whether window or screen should be created
169 * first. Different environments would prefer one or the other.
170 *
171 * Also, no easy way of querying supported formats if the screen
172 * cannot be created first.
173 */
174 for (i = 0;
175 window == NULL && formats[i] != PIPE_FORMAT_NONE;
176 i++) {
177
178 screen = graw_create_window_and_screen(0,0,300,300,
179 formats[i],
180 &window);
181 }
182
183 ctx = screen->context_create(screen, NULL);
184 if (ctx == NULL) {
185 fprintf(stderr, "Unable to create context!\n");
186 exit(3);
187 }
188
189 templat.target = PIPE_TEXTURE_2D;
190 templat.format = formats[i];
191 templat.width0 = WIDTH;
192 templat.height0 = HEIGHT;
193 templat.depth0 = 1;
194 templat.last_level = 0;
195 templat.nr_samples = 1;
196 templat.bind = (PIPE_BIND_RENDER_TARGET |
197 PIPE_BIND_DISPLAY_TARGET);
198
199 tex = screen->resource_create(screen,
200 &templat);
201 if (tex == NULL) {
202 fprintf(stderr, "Unable to create screen texture!\n");
203 exit(4);
204 }
205
206 surf = screen->get_tex_surface(screen, tex, 0, 0, 0,
207 PIPE_BIND_RENDER_TARGET |
208 PIPE_BIND_DISPLAY_TARGET);
209 if (surf == NULL) {
210 fprintf(stderr, "Unable to get tex surface!\n");
211 exit(5);
212 }
213
214 memset(&fb, 0, sizeof fb);
215 fb.nr_cbufs = 1;
216 fb.width = WIDTH;
217 fb.height = HEIGHT;
218 fb.cbufs[0] = surf;
219
220 ctx->set_framebuffer_state(ctx, &fb);
221
222 {
223 struct pipe_blend_state blend;
224 void *handle;
225 memset(&blend, 0, sizeof blend);
226 blend.rt[0].colormask = PIPE_MASK_RGBA;
227 handle = ctx->create_blend_state(ctx, &blend);
228 ctx->bind_blend_state(ctx, handle);
229 }
230
231 {
232 struct pipe_depth_stencil_alpha_state depthstencil;
233 void *handle;
234 memset(&depthstencil, 0, sizeof depthstencil);
235 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
236 ctx->bind_depth_stencil_alpha_state(ctx, handle);
237 }
238
239 {
240 struct pipe_rasterizer_state rasterizer;
241 void *handle;
242 memset(&rasterizer, 0, sizeof rasterizer);
243 rasterizer.cull_face = PIPE_FACE_NONE;
244 rasterizer.gl_rasterization_rules = 1;
245 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
246 ctx->bind_rasterizer_state(ctx, handle);
247 }
248
249 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
250 set_vertices();
251 set_vertex_shader();
252 set_fragment_shader();
253 }
254
255
256 int main( int argc, char *argv[] )
257 {
258 init();
259
260 graw_set_display_func( draw );
261 graw_main_loop();
262 return 0;
263 }