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