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