1/* Test sRGB texturing.
2 */
3
4#include "graw_util.h"
5
6
7static const int WIDTH = 600;
8static const int HEIGHT = 300;
9
10static struct graw_info info;
11
12static struct pipe_resource *texture;
13static struct pipe_sampler_view *linear_sv, *srgb_sv;
14
15
16struct vertex {
17   float position[4];
18   float color[4];
19};
20
21static struct vertex vertices1[] =
22{
23   { { -0.1, -0.9, 0.0, 1.0 },
24     { 1, 1, 0, 1 } },
25
26   { { -0.1,  0.9, 0.0, 1.0 },
27     { 1, 0, 0, 1 } },
28
29   { {-0.9,  0.9, 0.0, 1.0 },
30     { 0, 0, 0, 1 } },
31
32   { {-0.9,  -0.9, 0.0, 1.0 },
33     { 0, 1, 0, 1 } },
34};
35
36
37static struct vertex vertices2[] =
38{
39   { { 0.9, -0.9, 0.0, 1.0 },
40     { 1, 1, 0, 1 } },
41
42   { { 0.9,  0.9, 0.0, 1.0 },
43     { 1, 0, 0, 1 } },
44
45   { { 0.1,  0.9, 0.0, 1.0 },
46     { 0, 0, 0, 1 } },
47
48   { { 0.1,  -0.9, 0.0, 1.0 },
49     { 0, 1, 0, 1 } },
50};
51
52
53
54
55static void
56set_vertices(struct vertex *verts, unsigned num_verts)
57{
58   struct pipe_vertex_element ve[2];
59   struct pipe_vertex_buffer vbuf;
60   void *handle;
61
62   memset(ve, 0, sizeof ve);
63
64   ve[0].src_offset = Offset(struct vertex, position);
65   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
66   ve[1].src_offset = Offset(struct vertex, color);
67   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
68
69   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
70   info.ctx->bind_vertex_elements_state(info.ctx, handle);
71
72
73   vbuf.stride = sizeof(struct vertex);
74   vbuf.buffer_offset = 0;
75   vbuf.buffer = pipe_buffer_create_with_data(info.ctx,
76                                              PIPE_BIND_VERTEX_BUFFER,
77                                              PIPE_USAGE_STATIC,
78                                              num_verts * sizeof(struct vertex),
79                                              verts);
80
81   info.ctx->set_vertex_buffers(info.ctx, 1, &vbuf);
82}
83
84static void set_vertex_shader( void )
85{
86   void *handle;
87   const char *text =
88      "VERT\n"
89      "DCL IN[0]\n"
90      "DCL IN[1]\n"
91      "DCL OUT[0], POSITION\n"
92      "DCL OUT[1], GENERIC[0]\n"
93      "  0: MOV OUT[1], IN[1]\n"
94      "  1: MOV OUT[0], IN[0]\n"
95      "  2: END\n";
96
97   handle = graw_parse_vertex_shader(info.ctx, text);
98   info.ctx->bind_vs_state(info.ctx, handle);
99}
100
101static void set_fragment_shader( void )
102{
103   void *handle;
104   const char *text =
105      "FRAG\n"
106      "DCL IN[0], GENERIC[0], PERSPECTIVE\n"
107      "DCL OUT[0], COLOR\n"
108      "DCL TEMP[0]\n"
109      "DCL SAMP[0]\n"
110      "  0: TXP TEMP[0], IN[0], SAMP[0], 2D\n"
111      "  1: MOV OUT[0], TEMP[0]\n"
112      "  2: END\n";
113
114   handle = graw_parse_fragment_shader(info.ctx, text);
115   info.ctx->bind_fs_state(info.ctx, handle);
116}
117
118
119static void draw( void )
120{
121   union pipe_color_union clear_color;
122
123   clear_color.f[0] = 0.5;
124   clear_color.f[1] = 0.5;
125   clear_color.f[2] = 0.5;
126   clear_color.f[3] = 1.0;
127
128   info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
129
130   info.ctx->set_fragment_sampler_views(info.ctx, 1, &linear_sv);
131   set_vertices(vertices1, 4);
132   util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, 4);
133
134   info.ctx->set_fragment_sampler_views(info.ctx, 1, &srgb_sv);
135   set_vertices(vertices2, 4);
136   util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, 4);
137
138   info.ctx->flush(info.ctx, NULL);
139
140   graw_util_flush_front(&info);
141}
142
143
144static void init_tex( void )
145{
146#define SIZE 64
147   ubyte tex2d[SIZE][SIZE][4];
148   int s, t;
149
150   for (s = 0; s < SIZE; s++) {
151      for (t = 0; t < SIZE; t++) {
152         tex2d[t][s][0] = 0;
153         tex2d[t][s][1] = s * 255 / SIZE;
154         tex2d[t][s][2] = t * 255 / SIZE;
155         tex2d[t][s][3] = 255;
156      }
157   }
158
159   texture = graw_util_create_tex2d(&info, SIZE, SIZE,
160                                    PIPE_FORMAT_B8G8R8A8_UNORM, tex2d);
161
162   {
163      void *sampler;
164      sampler = graw_util_create_simple_sampler(&info,
165                                                PIPE_TEX_WRAP_REPEAT,
166                                                PIPE_TEX_FILTER_NEAREST);
167      info.ctx->bind_fragment_sampler_states(info.ctx, 1, &sampler);
168   }
169
170   /* linear sampler view */
171   {
172      struct pipe_sampler_view sv_temp;
173      memset(&sv_temp, 0, sizeof sv_temp);
174      sv_temp.format = PIPE_FORMAT_B8G8R8A8_UNORM;
175      sv_temp.texture = texture;
176      sv_temp.swizzle_r = PIPE_SWIZZLE_RED;
177      sv_temp.swizzle_g = PIPE_SWIZZLE_GREEN;
178      sv_temp.swizzle_b = PIPE_SWIZZLE_BLUE;
179      sv_temp.swizzle_a = PIPE_SWIZZLE_ALPHA;
180      linear_sv = info.ctx->create_sampler_view(info.ctx, texture, &sv_temp);
181      if (linear_sv == NULL)
182         exit(0);
183   }
184
185   /* srgb sampler view */
186   {
187      struct pipe_sampler_view sv_temp;
188      memset(&sv_temp, 0, sizeof sv_temp);
189      sv_temp.format = PIPE_FORMAT_B8G8R8A8_SRGB;
190      sv_temp.texture = texture;
191      sv_temp.swizzle_r = PIPE_SWIZZLE_RED;
192      sv_temp.swizzle_g = PIPE_SWIZZLE_GREEN;
193      sv_temp.swizzle_b = PIPE_SWIZZLE_BLUE;
194      sv_temp.swizzle_a = PIPE_SWIZZLE_ALPHA;
195      srgb_sv = info.ctx->create_sampler_view(info.ctx, texture, &sv_temp);
196      if (srgb_sv == NULL)
197         exit(0);
198   }
199#undef SIZE
200}
201
202static void init( void )
203{
204   if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE))
205      exit(1);
206
207   graw_util_default_state(&info, FALSE);
208
209   graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 10000);
210
211   init_tex();
212
213   set_vertex_shader();
214   set_fragment_shader();
215}
216
217
218int main( int argc, char *argv[] )
219{
220   init();
221
222   graw_set_display_func( draw );
223   graw_main_loop();
224   return 0;
225}
226