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