• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test the writing Z in fragment shader.
2  * The red quad should be entirely in front of the blue quad even
3  * though the overlap and intersect in Z.
4  */
5 
6 #include <stdio.h>
7 
8 #include "graw_util.h"
9 
10 
11 static int width = 300;
12 static int height = 300;
13 
14 static struct graw_info info;
15 
16 
17 struct vertex {
18    float position[4];
19    float color[4];
20 };
21 
22 #define z0 0.2
23 #define z01 0.5
24 #define z1 0.4
25 
26 
27 static struct vertex vertices[] =
28 {
29    /* left quad: clock-wise, front-facing, red */
30    {
31       {-0.8, -0.9, z0, 1.0 },
32       { 1, 0, 0, 1 }
33    },
34 
35    {
36       { -0.2, -0.9, z0, 1.0 },
37       { 1, 0, 0, 1 }
38    },
39 
40    {
41       { 0.2,  0.9, z01, 1.0 },
42       { 1, 0, 0, 1 }
43    },
44 
45    {
46       {-0.9,  0.9, z01, 1.0 },
47       { 1, 0, 0, 1 }
48    },
49 
50    /* right quad : counter-clock-wise, back-facing, green */
51    {
52       { 0.2,  -0.9, z1, 1.0 },
53       { 0, 0, 1, -1 }
54    },
55 
56    {
57       { -0.2,  0.8, z1, 1.0 },
58       { 0, 0, 1, -1 }
59    },
60 
61    {
62       { 0.9,  0.8, z1, 1.0 },
63       { 0, 0, 1, -1 }
64    },
65 
66    {
67       { 0.8, -0.9, z1, 1.0 },
68       { 0, 0, 1, -1 }
69    },
70 };
71 
72 #define NUM_VERTS (sizeof(vertices) / sizeof(vertices[0]))
73 
74 
75 
76 static void
set_vertices(void)77 set_vertices(void)
78 {
79    struct pipe_vertex_element ve[2];
80    struct pipe_vertex_buffer vbuf;
81    void *handle;
82 
83    memset(ve, 0, sizeof ve);
84 
85    ve[0].src_offset = Offset(struct vertex, position);
86    ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
87    ve[1].src_offset = Offset(struct vertex, color);
88    ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
89 
90    handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
91    info.ctx->bind_vertex_elements_state(info.ctx, handle);
92 
93    memset(&vbuf, 0, sizeof vbuf);
94 
95    vbuf.stride = sizeof(struct vertex);
96    vbuf.buffer_offset = 0;
97    vbuf.buffer = pipe_buffer_create_with_data(info.ctx,
98                                               PIPE_BIND_VERTEX_BUFFER,
99                                               PIPE_USAGE_DEFAULT,
100                                               sizeof(vertices),
101                                               vertices);
102 
103    info.ctx->set_vertex_buffers(info.ctx, 0, 1, &vbuf);
104 }
105 
106 
107 static void
set_vertex_shader(void)108 set_vertex_shader(void)
109 {
110    void *handle;
111    const char *text =
112       "VERT\n"
113       "DCL IN[0]\n"
114       "DCL IN[1]\n"
115       "DCL OUT[0], POSITION\n"
116       "DCL OUT[1], GENERIC[0]\n"
117       "  0: MOV OUT[0], IN[0]\n"
118       "  1: MOV OUT[1], IN[1]\n"
119       "  2: END\n";
120 
121    handle = graw_parse_vertex_shader(info.ctx, text);
122    info.ctx->bind_vs_state(info.ctx, handle);
123 }
124 
125 
126 static void
set_fragment_shader(void)127 set_fragment_shader(void)
128 {
129    void *handle;
130    const char *text =
131       "FRAG\n"
132       "DCL IN[0], GENERIC, CONSTANT\n"
133       "DCL OUT[0], COLOR\n"
134       "DCL OUT[1], POSITION\n"
135       "DCL TEMP[0]\n"
136       "IMM FLT32 {    1.0,     0.0,     0.0,     0.0 }\n"
137       "IMM FLT32 {    0.0,     1.0,     0.0,     0.0 }\n"
138       "IMM FLT32 {    0.5,     0.4,     0.0,     0.0 }\n"
139       " 0: MOV OUT[0], IN[0]\n"    /* front-facing: red */
140       " 1: IF IN[0].xxxx :3\n"
141       " 2:   MOV OUT[1].z, IMM[2].yyyy\n"   /* red: Z = 0.4 */
142       " 3: ELSE :5\n"
143       " 4:   MOV OUT[1].z, IMM[2].xxxx\n"   /* blue: Z = 0.5 */
144       " 5: ENDIF\n"
145       " 6: END\n";
146 
147    handle = graw_parse_fragment_shader(info.ctx, text);
148    info.ctx->bind_fs_state(info.ctx, handle);
149 }
150 
151 
152 
153 static void
draw(void)154 draw(void)
155 {
156    union pipe_color_union clear_color;
157 
158    clear_color.f[0] = 0.25;
159    clear_color.f[1] = 0.25;
160    clear_color.f[2] = 0.25;
161    clear_color.f[3] = 1.00;
162 
163    info.ctx->clear(info.ctx,
164               PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
165               &clear_color, 1.0, 0);
166    util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
167    info.ctx->flush(info.ctx, NULL, 0);
168 
169 #if 0
170    /* At the moment, libgraw leaks out/makes available some of the
171     * symbols from gallium/auxiliary, including these debug helpers.
172     * Will eventually want to bless some of these paths, and lock the
173     * others down so they aren't accessible from test programs.
174     *
175     * This currently just happens to work on debug builds - a release
176     * build will probably fail to link here:
177     */
178    debug_dump_surface_bmp(info.ctx, "result.bmp", surf);
179 #endif
180 
181    graw_util_flush_front(&info);
182 }
183 
184 
185 #if 0
186 static void
187 resize(int w, int h)
188 {
189    width = w;
190    height = h;
191 
192    graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
193 }
194 #endif
195 
196 
197 static void
init(void)198 init(void)
199 {
200    if (!graw_util_create_window(&info, width, height, 1, TRUE))
201       exit(1);
202 
203    graw_util_default_state(&info, TRUE);
204 
205    graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
206 
207    set_vertices();
208    set_vertex_shader();
209    set_fragment_shader();
210 }
211 
212 
213 int
main(int argc,char * argv[])214 main(int argc, char *argv[])
215 {
216    init();
217 
218    printf("The red quad should be entirely in front of the blue quad.\n");
219 
220    graw_set_display_func(draw);
221    /*graw_set_reshape_func(resize);*/
222    graw_main_loop();
223    return 0;
224 }
225