Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / vulkan / anv_compiler.cpp
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27
28 #include "anv_private.h"
29
30 #include <brw_context.h>
31 #include <brw_wm.h> /* brw_new_shader_program is here */
32 #include <brw_nir.h>
33
34 #include <brw_vs.h>
35 #include <brw_gs.h>
36 #include <brw_cs.h>
37
38 #include <mesa/main/shaderobj.h>
39 #include <mesa/main/fbobject.h>
40 #include <mesa/main/context.h>
41 #include <mesa/program/program.h>
42 #include <glsl/program.h>
43
44 /* XXX: We need this to keep symbols in nir.h from conflicting with the
45 * generated GEN command packing headers. We need to fix *both* to not
46 * define something as generic as LOAD.
47 */
48 #undef LOAD
49
50 #include <glsl/nir/nir_spirv.h>
51
52 #define SPIR_V_MAGIC_NUMBER 0x07230203
53
54 static void
55 fail_if(int cond, const char *format, ...)
56 {
57 va_list args;
58
59 if (!cond)
60 return;
61
62 va_start(args, format);
63 vfprintf(stderr, format, args);
64 va_end(args);
65
66 exit(1);
67 }
68
69 static VkResult
70 set_binding_table_layout(struct brw_stage_prog_data *prog_data,
71 struct anv_pipeline *pipeline, uint32_t stage)
72 {
73 uint32_t bias, count, k, *map;
74 struct anv_pipeline_layout *layout = pipeline->layout;
75
76 /* No layout is valid for shaders that don't bind any resources. */
77 if (pipeline->layout == NULL)
78 return VK_SUCCESS;
79
80 if (stage == VK_SHADER_STAGE_FRAGMENT)
81 bias = MAX_RTS;
82 else
83 bias = 0;
84
85 count = layout->stage[stage].surface_count;
86 prog_data->map_entries =
87 (uint32_t *) malloc(count * sizeof(prog_data->map_entries[0]));
88 if (prog_data->map_entries == NULL)
89 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
90
91 k = bias;
92 map = prog_data->map_entries;
93 for (uint32_t i = 0; i < layout->num_sets; i++) {
94 prog_data->bind_map[i].index = map;
95 for (uint32_t j = 0; j < layout->set[i].layout->stage[stage].surface_count; j++)
96 *map++ = k++;
97
98 prog_data->bind_map[i].index_count =
99 layout->set[i].layout->stage[stage].surface_count;
100 }
101
102 return VK_SUCCESS;
103 }
104
105 static uint32_t
106 upload_kernel(struct anv_pipeline *pipeline, const void *data, size_t size)
107 {
108 struct anv_state state =
109 anv_state_stream_alloc(&pipeline->program_stream, size, 64);
110
111 assert(size < pipeline->program_stream.block_pool->block_size);
112
113 memcpy(state.map, data, size);
114
115 return state.offset;
116 }
117
118 static void
119 brw_vs_populate_key(struct brw_context *brw,
120 struct brw_vertex_program *vp,
121 struct brw_vs_prog_key *key)
122 {
123 struct gl_context *ctx = &brw->ctx;
124 /* BRW_NEW_VERTEX_PROGRAM */
125 struct gl_program *prog = (struct gl_program *) vp;
126
127 memset(key, 0, sizeof(*key));
128
129 /* Just upload the program verbatim for now. Always send it all
130 * the inputs it asks for, whether they are varying or not.
131 */
132 key->base.program_string_id = vp->id;
133 brw_setup_vue_key_clip_info(brw, &key->base,
134 vp->program.Base.UsesClipDistanceOut);
135
136 /* _NEW_POLYGON */
137 if (brw->gen < 6) {
138 key->copy_edgeflag = (ctx->Polygon.FrontMode != GL_FILL ||
139 ctx->Polygon.BackMode != GL_FILL);
140 }
141
142 if (prog->OutputsWritten & (VARYING_BIT_COL0 | VARYING_BIT_COL1 |
143 VARYING_BIT_BFC0 | VARYING_BIT_BFC1)) {
144 /* _NEW_LIGHT | _NEW_BUFFERS */
145 key->clamp_vertex_color = ctx->Light._ClampVertexColor;
146 }
147
148 /* _NEW_POINT */
149 if (brw->gen < 6 && ctx->Point.PointSprite) {
150 for (int i = 0; i < 8; i++) {
151 if (ctx->Point.CoordReplace[i])
152 key->point_coord_replace |= (1 << i);
153 }
154 }
155
156 /* _NEW_TEXTURE */
157 brw_populate_sampler_prog_key_data(ctx, prog, brw->vs.base.sampler_count,
158 &key->base.tex);
159 }
160
161 static bool
162 really_do_vs_prog(struct brw_context *brw,
163 struct gl_shader_program *prog,
164 struct brw_vertex_program *vp,
165 struct brw_vs_prog_key *key, struct anv_pipeline *pipeline)
166 {
167 GLuint program_size;
168 const GLuint *program;
169 struct brw_vs_prog_data *prog_data = &pipeline->vs_prog_data;
170 struct brw_stage_prog_data *stage_prog_data = &prog_data->base.base;
171 void *mem_ctx;
172 struct gl_shader *vs = NULL;
173
174 if (prog)
175 vs = prog->_LinkedShaders[MESA_SHADER_VERTEX];
176
177 memset(prog_data, 0, sizeof(*prog_data));
178
179 mem_ctx = ralloc_context(NULL);
180
181 /* Allocate the references to the uniforms that will end up in the
182 * prog_data associated with the compiled program, and which will be freed
183 * by the state cache.
184 */
185 int param_count;
186 if (vs) {
187 /* We add padding around uniform values below vec4 size, with the worst
188 * case being a float value that gets blown up to a vec4, so be
189 * conservative here.
190 */
191 param_count = vs->num_uniform_components * 4;
192
193 } else {
194 param_count = vp->program.Base.Parameters->NumParameters * 4;
195 }
196 /* vec4_visitor::setup_uniform_clipplane_values() also uploads user clip
197 * planes as uniforms.
198 */
199 param_count += key->base.nr_userclip_plane_consts * 4;
200
201 /* Setting nr_params here NOT to the size of the param and pull_param
202 * arrays, but to the number of uniform components vec4_visitor
203 * needs. vec4_visitor::setup_uniforms() will set it back to a proper value.
204 */
205 stage_prog_data->nr_params = ALIGN(param_count, 4) / 4;
206 if (vs) {
207 stage_prog_data->nr_params += vs->num_samplers;
208 }
209
210 GLbitfield64 outputs_written = vp->program.Base.OutputsWritten;
211 prog_data->inputs_read = vp->program.Base.InputsRead;
212
213 if (key->copy_edgeflag) {
214 outputs_written |= BITFIELD64_BIT(VARYING_SLOT_EDGE);
215 prog_data->inputs_read |= VERT_BIT_EDGEFLAG;
216 }
217
218 if (brw->gen < 6) {
219 /* Put dummy slots into the VUE for the SF to put the replaced
220 * point sprite coords in. We shouldn't need these dummy slots,
221 * which take up precious URB space, but it would mean that the SF
222 * doesn't get nice aligned pairs of input coords into output
223 * coords, which would be a pain to handle.
224 */
225 for (int i = 0; i < 8; i++) {
226 if (key->point_coord_replace & (1 << i))
227 outputs_written |= BITFIELD64_BIT(VARYING_SLOT_TEX0 + i);
228 }
229
230 /* if back colors are written, allocate slots for front colors too */
231 if (outputs_written & BITFIELD64_BIT(VARYING_SLOT_BFC0))
232 outputs_written |= BITFIELD64_BIT(VARYING_SLOT_COL0);
233 if (outputs_written & BITFIELD64_BIT(VARYING_SLOT_BFC1))
234 outputs_written |= BITFIELD64_BIT(VARYING_SLOT_COL1);
235 }
236
237 /* In order for legacy clipping to work, we need to populate the clip
238 * distance varying slots whenever clipping is enabled, even if the vertex
239 * shader doesn't write to gl_ClipDistance.
240 */
241 if (key->base.userclip_active) {
242 outputs_written |= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST0);
243 outputs_written |= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST1);
244 }
245
246 brw_compute_vue_map(brw->intelScreen->devinfo,
247 &prog_data->base.vue_map, outputs_written);
248 \
249 set_binding_table_layout(&prog_data->base.base, pipeline,
250 VK_SHADER_STAGE_VERTEX);
251
252 /* Emit GEN4 code.
253 */
254 program = brw_vs_emit(brw, mem_ctx, key, prog_data, &vp->program,
255 prog, &program_size);
256 if (program == NULL) {
257 ralloc_free(mem_ctx);
258 return false;
259 }
260
261 pipeline->vs_simd8 = upload_kernel(pipeline, program, program_size);
262
263 ralloc_free(mem_ctx);
264
265 return true;
266 }
267
268 void brw_wm_populate_key(struct brw_context *brw,
269 struct brw_fragment_program *fp,
270 struct brw_wm_prog_key *key)
271 {
272 struct gl_context *ctx = &brw->ctx;
273 struct gl_program *prog = (struct gl_program *) brw->fragment_program;
274 GLuint lookup = 0;
275 GLuint line_aa;
276 bool program_uses_dfdy = fp->program.UsesDFdy;
277 struct gl_framebuffer draw_buffer;
278 bool multisample_fbo;
279
280 memset(key, 0, sizeof(*key));
281
282 for (int i = 0; i < MAX_SAMPLERS; i++) {
283 /* Assume color sampler, no swizzling. */
284 key->tex.swizzles[i] = SWIZZLE_XYZW;
285 }
286
287 /* A non-zero framebuffer name indicates that the framebuffer was created by
288 * the user rather than the window system. */
289 draw_buffer.Name = 1;
290 draw_buffer.Visual.samples = 1;
291 draw_buffer._NumColorDrawBuffers = 1;
292 draw_buffer._NumColorDrawBuffers = 1;
293 draw_buffer.Width = 400;
294 draw_buffer.Height = 400;
295 ctx->DrawBuffer = &draw_buffer;
296
297 multisample_fbo = ctx->DrawBuffer->Visual.samples > 1;
298
299 /* Build the index for table lookup
300 */
301 if (brw->gen < 6) {
302 /* _NEW_COLOR */
303 if (fp->program.UsesKill || ctx->Color.AlphaEnabled)
304 lookup |= IZ_PS_KILL_ALPHATEST_BIT;
305
306 if (fp->program.Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
307 lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
308
309 /* _NEW_DEPTH */
310 if (ctx->Depth.Test)
311 lookup |= IZ_DEPTH_TEST_ENABLE_BIT;
312
313 if (ctx->Depth.Test && ctx->Depth.Mask) /* ?? */
314 lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;
315
316 /* _NEW_STENCIL | _NEW_BUFFERS */
317 if (ctx->Stencil._Enabled) {
318 lookup |= IZ_STENCIL_TEST_ENABLE_BIT;
319
320 if (ctx->Stencil.WriteMask[0] ||
321 ctx->Stencil.WriteMask[ctx->Stencil._BackFace])
322 lookup |= IZ_STENCIL_WRITE_ENABLE_BIT;
323 }
324 key->iz_lookup = lookup;
325 }
326
327 line_aa = AA_NEVER;
328
329 /* _NEW_LINE, _NEW_POLYGON, BRW_NEW_REDUCED_PRIMITIVE */
330 if (ctx->Line.SmoothFlag) {
331 if (brw->reduced_primitive == GL_LINES) {
332 line_aa = AA_ALWAYS;
333 }
334 else if (brw->reduced_primitive == GL_TRIANGLES) {
335 if (ctx->Polygon.FrontMode == GL_LINE) {
336 line_aa = AA_SOMETIMES;
337
338 if (ctx->Polygon.BackMode == GL_LINE ||
339 (ctx->Polygon.CullFlag &&
340 ctx->Polygon.CullFaceMode == GL_BACK))
341 line_aa = AA_ALWAYS;
342 }
343 else if (ctx->Polygon.BackMode == GL_LINE) {
344 line_aa = AA_SOMETIMES;
345
346 if ((ctx->Polygon.CullFlag &&
347 ctx->Polygon.CullFaceMode == GL_FRONT))
348 line_aa = AA_ALWAYS;
349 }
350 }
351 }
352
353 key->line_aa = line_aa;
354
355 /* _NEW_HINT */
356 key->high_quality_derivatives =
357 ctx->Hint.FragmentShaderDerivative == GL_NICEST;
358
359 if (brw->gen < 6)
360 key->stats_wm = brw->stats_wm;
361
362 /* _NEW_LIGHT */
363 key->flat_shade = (ctx->Light.ShadeModel == GL_FLAT);
364
365 /* _NEW_FRAG_CLAMP | _NEW_BUFFERS */
366 key->clamp_fragment_color = ctx->Color._ClampFragmentColor;
367
368 /* _NEW_TEXTURE */
369 brw_populate_sampler_prog_key_data(ctx, prog, brw->wm.base.sampler_count,
370 &key->tex);
371
372 /* _NEW_BUFFERS */
373 /*
374 * Include the draw buffer origin and height so that we can calculate
375 * fragment position values relative to the bottom left of the drawable,
376 * from the incoming screen origin relative position we get as part of our
377 * payload.
378 *
379 * This is only needed for the WM_WPOSXY opcode when the fragment program
380 * uses the gl_FragCoord input.
381 *
382 * We could avoid recompiling by including this as a constant referenced by
383 * our program, but if we were to do that it would also be nice to handle
384 * getting that constant updated at batchbuffer submit time (when we
385 * hold the lock and know where the buffer really is) rather than at emit
386 * time when we don't hold the lock and are just guessing. We could also
387 * just avoid using this as key data if the program doesn't use
388 * fragment.position.
389 *
390 * For DRI2 the origin_x/y will always be (0,0) but we still need the
391 * drawable height in order to invert the Y axis.
392 */
393 if (fp->program.Base.InputsRead & VARYING_BIT_POS) {
394 key->drawable_height = ctx->DrawBuffer->Height;
395 }
396
397 if ((fp->program.Base.InputsRead & VARYING_BIT_POS) || program_uses_dfdy) {
398 key->render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
399 }
400
401 /* _NEW_BUFFERS */
402 key->nr_color_regions = ctx->DrawBuffer->_NumColorDrawBuffers;
403
404 /* _NEW_MULTISAMPLE, _NEW_COLOR, _NEW_BUFFERS */
405 key->replicate_alpha = ctx->DrawBuffer->_NumColorDrawBuffers > 1 &&
406 (ctx->Multisample.SampleAlphaToCoverage || ctx->Color.AlphaEnabled);
407
408 /* _NEW_BUFFERS _NEW_MULTISAMPLE */
409 /* Ignore sample qualifier while computing this flag. */
410 key->persample_shading =
411 _mesa_get_min_invocations_per_fragment(ctx, &fp->program, true) > 1;
412 if (key->persample_shading)
413 key->persample_2x = ctx->DrawBuffer->Visual.samples == 2;
414
415 key->compute_pos_offset =
416 _mesa_get_min_invocations_per_fragment(ctx, &fp->program, false) > 1 &&
417 fp->program.Base.SystemValuesRead & SYSTEM_BIT_SAMPLE_POS;
418
419 key->compute_sample_id =
420 multisample_fbo &&
421 ctx->Multisample.Enabled &&
422 (fp->program.Base.SystemValuesRead & SYSTEM_BIT_SAMPLE_ID);
423
424 /* BRW_NEW_VUE_MAP_GEOM_OUT */
425 if (brw->gen < 6 || _mesa_bitcount_64(fp->program.Base.InputsRead &
426 BRW_FS_VARYING_INPUT_MASK) > 16)
427 key->input_slots_valid = brw->vue_map_geom_out.slots_valid;
428
429
430 /* _NEW_COLOR | _NEW_BUFFERS */
431 /* Pre-gen6, the hardware alpha test always used each render
432 * target's alpha to do alpha test, as opposed to render target 0's alpha
433 * like GL requires. Fix that by building the alpha test into the
434 * shader, and we'll skip enabling the fixed function alpha test.
435 */
436 if (brw->gen < 6 && ctx->DrawBuffer->_NumColorDrawBuffers > 1 && ctx->Color.AlphaEnabled) {
437 key->alpha_test_func = ctx->Color.AlphaFunc;
438 key->alpha_test_ref = ctx->Color.AlphaRef;
439 }
440
441 /* The unique fragment program ID */
442 key->program_string_id = fp->id;
443
444 ctx->DrawBuffer = NULL;
445 }
446
447 static uint8_t
448 computed_depth_mode(struct gl_fragment_program *fp)
449 {
450 if (fp->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
451 switch (fp->FragDepthLayout) {
452 case FRAG_DEPTH_LAYOUT_NONE:
453 case FRAG_DEPTH_LAYOUT_ANY:
454 return BRW_PSCDEPTH_ON;
455 case FRAG_DEPTH_LAYOUT_GREATER:
456 return BRW_PSCDEPTH_ON_GE;
457 case FRAG_DEPTH_LAYOUT_LESS:
458 return BRW_PSCDEPTH_ON_LE;
459 case FRAG_DEPTH_LAYOUT_UNCHANGED:
460 return BRW_PSCDEPTH_OFF;
461 }
462 }
463 return BRW_PSCDEPTH_OFF;
464 }
465
466 static bool
467 really_do_wm_prog(struct brw_context *brw,
468 struct gl_shader_program *prog,
469 struct brw_fragment_program *fp,
470 struct brw_wm_prog_key *key, struct anv_pipeline *pipeline)
471 {
472 struct gl_context *ctx = &brw->ctx;
473 void *mem_ctx = ralloc_context(NULL);
474 struct brw_wm_prog_data *prog_data = &pipeline->wm_prog_data;
475 struct gl_shader *fs = NULL;
476 unsigned int program_size;
477 const uint32_t *program;
478
479 if (prog)
480 fs = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
481
482 memset(prog_data, 0, sizeof(*prog_data));
483
484 /* key->alpha_test_func means simulating alpha testing via discards,
485 * so the shader definitely kills pixels.
486 */
487 prog_data->uses_kill = fp->program.UsesKill || key->alpha_test_func;
488
489 prog_data->computed_depth_mode = computed_depth_mode(&fp->program);
490
491 /* Allocate the references to the uniforms that will end up in the
492 * prog_data associated with the compiled program, and which will be freed
493 * by the state cache.
494 */
495 int param_count;
496 if (fs) {
497 param_count = fs->num_uniform_components;
498 } else {
499 param_count = fp->program.Base.Parameters->NumParameters * 4;
500 }
501 /* The backend also sometimes adds params for texture size. */
502 param_count += 2 * ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
503 prog_data->base.param =
504 rzalloc_array(NULL, const gl_constant_value *, param_count);
505 prog_data->base.pull_param =
506 rzalloc_array(NULL, const gl_constant_value *, param_count);
507 prog_data->base.nr_params = param_count;
508
509 prog_data->barycentric_interp_modes =
510 brw_compute_barycentric_interp_modes(brw, key->flat_shade,
511 key->persample_shading,
512 &fp->program);
513
514 set_binding_table_layout(&prog_data->base, pipeline,
515 VK_SHADER_STAGE_FRAGMENT);
516 /* This needs to come after shader time and pull constant entries, but we
517 * don't have those set up now, so just put it after the layout entries.
518 */
519 prog_data->binding_table.render_target_start = 0;
520
521 program = brw_wm_fs_emit(brw, mem_ctx, key, prog_data,
522 &fp->program, prog, &program_size);
523 if (program == NULL) {
524 ralloc_free(mem_ctx);
525 return false;
526 }
527
528 uint32_t offset = upload_kernel(pipeline, program, program_size);
529
530 if (prog_data->no_8)
531 pipeline->ps_simd8 = NO_KERNEL;
532 else
533 pipeline->ps_simd8 = offset;
534
535 if (prog_data->no_8 || prog_data->prog_offset_16) {
536 pipeline->ps_simd16 = offset + prog_data->prog_offset_16;
537 } else {
538 pipeline->ps_simd16 = NO_KERNEL;
539 }
540
541 ralloc_free(mem_ctx);
542
543 return true;
544 }
545
546 static void
547 brw_gs_populate_key(struct brw_context *brw,
548 struct anv_pipeline *pipeline,
549 struct brw_geometry_program *gp,
550 struct brw_gs_prog_key *key)
551 {
552 struct gl_context *ctx = &brw->ctx;
553 struct brw_stage_state *stage_state = &brw->gs.base;
554 struct gl_program *prog = &gp->program.Base;
555
556 memset(key, 0, sizeof(*key));
557
558 key->base.program_string_id = gp->id;
559 brw_setup_vue_key_clip_info(brw, &key->base,
560 gp->program.Base.UsesClipDistanceOut);
561
562 /* _NEW_TEXTURE */
563 brw_populate_sampler_prog_key_data(ctx, prog, stage_state->sampler_count,
564 &key->base.tex);
565
566 struct brw_vs_prog_data *prog_data = &pipeline->vs_prog_data;
567
568 /* BRW_NEW_VUE_MAP_VS */
569 key->input_varyings = prog_data->base.vue_map.slots_valid;
570 }
571
572 static bool
573 really_do_gs_prog(struct brw_context *brw,
574 struct gl_shader_program *prog,
575 struct brw_geometry_program *gp,
576 struct brw_gs_prog_key *key, struct anv_pipeline *pipeline)
577 {
578 struct brw_gs_compile_output output;
579
580 /* FIXME: We pass the bind map to the compile in the output struct. Need
581 * something better. */
582 set_binding_table_layout(&output.prog_data.base.base,
583 pipeline, VK_SHADER_STAGE_GEOMETRY);
584
585 brw_compile_gs_prog(brw, prog, gp, key, &output);
586
587 pipeline->gs_vec4 = upload_kernel(pipeline, output.program, output.program_size);
588 pipeline->gs_vertex_count = gp->program.VerticesIn;
589
590 ralloc_free(output.mem_ctx);
591
592 return true;
593 }
594
595 static bool
596 brw_codegen_cs_prog(struct brw_context *brw,
597 struct gl_shader_program *prog,
598 struct brw_compute_program *cp,
599 struct brw_cs_prog_key *key, struct anv_pipeline *pipeline)
600 {
601 struct gl_context *ctx = &brw->ctx;
602 const GLuint *program;
603 void *mem_ctx = ralloc_context(NULL);
604 GLuint program_size;
605 struct brw_cs_prog_data *prog_data = &pipeline->cs_prog_data;
606
607 struct gl_shader *cs = prog->_LinkedShaders[MESA_SHADER_COMPUTE];
608 assert (cs);
609
610 memset(prog_data, 0, sizeof(*prog_data));
611
612 set_binding_table_layout(&prog_data->base, pipeline, VK_SHADER_STAGE_COMPUTE);
613
614 /* Allocate the references to the uniforms that will end up in the
615 * prog_data associated with the compiled program, and which will be freed
616 * by the state cache.
617 */
618 int param_count = cs->num_uniform_components;
619
620 /* The backend also sometimes adds params for texture size. */
621 param_count += 2 * ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits;
622 prog_data->base.param =
623 rzalloc_array(NULL, const gl_constant_value *, param_count);
624 prog_data->base.pull_param =
625 rzalloc_array(NULL, const gl_constant_value *, param_count);
626 prog_data->base.nr_params = param_count;
627
628 program = brw_cs_emit(brw, mem_ctx, key, prog_data,
629 &cp->program, prog, &program_size);
630 if (program == NULL) {
631 ralloc_free(mem_ctx);
632 return false;
633 }
634
635 if (unlikely(INTEL_DEBUG & DEBUG_CS))
636 fprintf(stderr, "\n");
637
638 pipeline->cs_simd = upload_kernel(pipeline, program, program_size);
639
640 ralloc_free(mem_ctx);
641
642 return true;
643 }
644
645 static void
646 brw_cs_populate_key(struct brw_context *brw,
647 struct brw_compute_program *bcp, struct brw_cs_prog_key *key)
648 {
649 memset(key, 0, sizeof(*key));
650
651 /* The unique compute program ID */
652 key->program_string_id = bcp->id;
653 }
654
655 static void
656 fail_on_compile_error(int status, const char *msg)
657 {
658 int source, line, column;
659 char error[256];
660
661 if (status)
662 return;
663
664 if (sscanf(msg, "%d:%d(%d): error: %255[^\n]", &source, &line, &column, error) == 4)
665 fail_if(!status, "%d:%s\n", line, error);
666 else
667 fail_if(!status, "%s\n", msg);
668 }
669
670 struct anv_compiler {
671 struct anv_device *device;
672 struct intel_screen *screen;
673 struct brw_context *brw;
674 struct gl_pipeline_object pipeline;
675 };
676
677 extern "C" {
678
679 struct anv_compiler *
680 anv_compiler_create(struct anv_device *device)
681 {
682 const struct brw_device_info *devinfo = &device->info;
683 struct anv_compiler *compiler;
684 struct gl_context *ctx;
685
686 compiler = rzalloc(NULL, struct anv_compiler);
687 if (compiler == NULL)
688 return NULL;
689
690 compiler->screen = rzalloc(compiler, struct intel_screen);
691 if (compiler->screen == NULL)
692 goto fail;
693
694 compiler->brw = rzalloc(compiler, struct brw_context);
695 if (compiler->brw == NULL)
696 goto fail;
697
698 compiler->device = device;
699
700 compiler->brw->optionCache.info = NULL;
701 compiler->brw->bufmgr = NULL;
702 compiler->brw->gen = devinfo->gen;
703 compiler->brw->is_g4x = devinfo->is_g4x;
704 compiler->brw->is_baytrail = devinfo->is_baytrail;
705 compiler->brw->is_haswell = devinfo->is_haswell;
706 compiler->brw->is_cherryview = devinfo->is_cherryview;
707
708 /* We need this at least for CS, which will check brw->max_cs_threads
709 * against the work group size. */
710 compiler->brw->max_vs_threads = devinfo->max_vs_threads;
711 compiler->brw->max_hs_threads = devinfo->max_hs_threads;
712 compiler->brw->max_ds_threads = devinfo->max_ds_threads;
713 compiler->brw->max_gs_threads = devinfo->max_gs_threads;
714 compiler->brw->max_wm_threads = devinfo->max_wm_threads;
715 compiler->brw->max_cs_threads = devinfo->max_cs_threads;
716 compiler->brw->urb.size = devinfo->urb.size;
717 compiler->brw->urb.min_vs_entries = devinfo->urb.min_vs_entries;
718 compiler->brw->urb.max_vs_entries = devinfo->urb.max_vs_entries;
719 compiler->brw->urb.max_hs_entries = devinfo->urb.max_hs_entries;
720 compiler->brw->urb.max_ds_entries = devinfo->urb.max_ds_entries;
721 compiler->brw->urb.max_gs_entries = devinfo->urb.max_gs_entries;
722
723 compiler->brw->intelScreen = compiler->screen;
724 compiler->screen->devinfo = &device->info;
725
726 brw_process_intel_debug_variable(compiler->screen);
727
728 compiler->screen->compiler = brw_compiler_create(compiler, &device->info);
729
730 ctx = &compiler->brw->ctx;
731 _mesa_init_shader_object_functions(&ctx->Driver);
732
733 _mesa_init_constants(&ctx->Const, API_OPENGL_CORE);
734
735 brw_initialize_context_constants(compiler->brw);
736
737 intelInitExtensions(ctx);
738
739 /* Set dd::NewShader */
740 brwInitFragProgFuncs(&ctx->Driver);
741
742 ctx->_Shader = &compiler->pipeline;
743
744 compiler->brw->precompile = false;
745
746 return compiler;
747
748 fail:
749 ralloc_free(compiler);
750 return NULL;
751 }
752
753 void
754 anv_compiler_destroy(struct anv_compiler *compiler)
755 {
756 _mesa_free_errors_data(&compiler->brw->ctx);
757 ralloc_free(compiler);
758 }
759
760 /* From gen7_urb.c */
761
762 /* FIXME: Add to struct intel_device_info */
763
764 static const int gen8_push_size = 32 * 1024;
765
766 static void
767 gen7_compute_urb_partition(struct anv_pipeline *pipeline)
768 {
769 const struct brw_device_info *devinfo = &pipeline->device->info;
770 bool vs_present = pipeline->vs_simd8 != NO_KERNEL;
771 unsigned vs_size = vs_present ? pipeline->vs_prog_data.base.urb_entry_size : 1;
772 unsigned vs_entry_size_bytes = vs_size * 64;
773 bool gs_present = pipeline->gs_vec4 != NO_KERNEL;
774 unsigned gs_size = gs_present ? pipeline->gs_prog_data.base.urb_entry_size : 1;
775 unsigned gs_entry_size_bytes = gs_size * 64;
776
777 /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
778 *
779 * VS Number of URB Entries must be divisible by 8 if the VS URB Entry
780 * Allocation Size is less than 9 512-bit URB entries.
781 *
782 * Similar text exists for GS.
783 */
784 unsigned vs_granularity = (vs_size < 9) ? 8 : 1;
785 unsigned gs_granularity = (gs_size < 9) ? 8 : 1;
786
787 /* URB allocations must be done in 8k chunks. */
788 unsigned chunk_size_bytes = 8192;
789
790 /* Determine the size of the URB in chunks. */
791 unsigned urb_chunks = devinfo->urb.size * 1024 / chunk_size_bytes;
792
793 /* Reserve space for push constants */
794 unsigned push_constant_bytes = gen8_push_size;
795 unsigned push_constant_chunks =
796 push_constant_bytes / chunk_size_bytes;
797
798 /* Initially, assign each stage the minimum amount of URB space it needs,
799 * and make a note of how much additional space it "wants" (the amount of
800 * additional space it could actually make use of).
801 */
802
803 /* VS has a lower limit on the number of URB entries */
804 unsigned vs_chunks =
805 ALIGN(devinfo->urb.min_vs_entries * vs_entry_size_bytes,
806 chunk_size_bytes) / chunk_size_bytes;
807 unsigned vs_wants =
808 ALIGN(devinfo->urb.max_vs_entries * vs_entry_size_bytes,
809 chunk_size_bytes) / chunk_size_bytes - vs_chunks;
810
811 unsigned gs_chunks = 0;
812 unsigned gs_wants = 0;
813 if (gs_present) {
814 /* There are two constraints on the minimum amount of URB space we can
815 * allocate:
816 *
817 * (1) We need room for at least 2 URB entries, since we always operate
818 * the GS in DUAL_OBJECT mode.
819 *
820 * (2) We can't allocate less than nr_gs_entries_granularity.
821 */
822 gs_chunks = ALIGN(MAX2(gs_granularity, 2) * gs_entry_size_bytes,
823 chunk_size_bytes) / chunk_size_bytes;
824 gs_wants =
825 ALIGN(devinfo->urb.max_gs_entries * gs_entry_size_bytes,
826 chunk_size_bytes) / chunk_size_bytes - gs_chunks;
827 }
828
829 /* There should always be enough URB space to satisfy the minimum
830 * requirements of each stage.
831 */
832 unsigned total_needs = push_constant_chunks + vs_chunks + gs_chunks;
833 assert(total_needs <= urb_chunks);
834
835 /* Mete out remaining space (if any) in proportion to "wants". */
836 unsigned total_wants = vs_wants + gs_wants;
837 unsigned remaining_space = urb_chunks - total_needs;
838 if (remaining_space > total_wants)
839 remaining_space = total_wants;
840 if (remaining_space > 0) {
841 unsigned vs_additional = (unsigned)
842 round(vs_wants * (((double) remaining_space) / total_wants));
843 vs_chunks += vs_additional;
844 remaining_space -= vs_additional;
845 gs_chunks += remaining_space;
846 }
847
848 /* Sanity check that we haven't over-allocated. */
849 assert(push_constant_chunks + vs_chunks + gs_chunks <= urb_chunks);
850
851 /* Finally, compute the number of entries that can fit in the space
852 * allocated to each stage.
853 */
854 unsigned nr_vs_entries = vs_chunks * chunk_size_bytes / vs_entry_size_bytes;
855 unsigned nr_gs_entries = gs_chunks * chunk_size_bytes / gs_entry_size_bytes;
856
857 /* Since we rounded up when computing *_wants, this may be slightly more
858 * than the maximum allowed amount, so correct for that.
859 */
860 nr_vs_entries = MIN2(nr_vs_entries, devinfo->urb.max_vs_entries);
861 nr_gs_entries = MIN2(nr_gs_entries, devinfo->urb.max_gs_entries);
862
863 /* Ensure that we program a multiple of the granularity. */
864 nr_vs_entries = ROUND_DOWN_TO(nr_vs_entries, vs_granularity);
865 nr_gs_entries = ROUND_DOWN_TO(nr_gs_entries, gs_granularity);
866
867 /* Finally, sanity check to make sure we have at least the minimum number
868 * of entries needed for each stage.
869 */
870 assert(nr_vs_entries >= devinfo->urb.min_vs_entries);
871 if (gs_present)
872 assert(nr_gs_entries >= 2);
873
874 /* Lay out the URB in the following order:
875 * - push constants
876 * - VS
877 * - GS
878 */
879 pipeline->urb.vs_start = push_constant_chunks;
880 pipeline->urb.vs_size = vs_size;
881 pipeline->urb.nr_vs_entries = nr_vs_entries;
882
883 pipeline->urb.gs_start = push_constant_chunks + vs_chunks;
884 pipeline->urb.gs_size = gs_size;
885 pipeline->urb.nr_gs_entries = nr_gs_entries;
886 }
887
888 static const struct {
889 uint32_t token;
890 gl_shader_stage stage;
891 const char *name;
892 } stage_info[] = {
893 { GL_VERTEX_SHADER, MESA_SHADER_VERTEX, "vertex" },
894 { GL_TESS_CONTROL_SHADER, (gl_shader_stage)-1,"tess control" },
895 { GL_TESS_EVALUATION_SHADER, (gl_shader_stage)-1, "tess evaluation" },
896 { GL_GEOMETRY_SHADER, MESA_SHADER_GEOMETRY, "geometry" },
897 { GL_FRAGMENT_SHADER, MESA_SHADER_FRAGMENT, "fragment" },
898 { GL_COMPUTE_SHADER, MESA_SHADER_COMPUTE, "compute" },
899 };
900
901 struct spirv_header{
902 uint32_t magic;
903 uint32_t version;
904 uint32_t gen_magic;
905 };
906
907 static const char *
908 src_as_glsl(const char *data)
909 {
910 const struct spirv_header *as_spirv = (const struct spirv_header *)data;
911
912 /* Check alignment */
913 if ((intptr_t)data & 0x3) {
914 return data;
915 }
916
917 if (as_spirv->magic == SPIR_V_MAGIC_NUMBER) {
918 /* LunarG back-door */
919 if (as_spirv->version == 0)
920 return data + 12;
921 else
922 return NULL;
923 } else {
924 return data;
925 }
926 }
927
928 static void
929 anv_compile_shader_glsl(struct anv_compiler *compiler,
930 struct gl_shader_program *program,
931 struct anv_pipeline *pipeline, uint32_t stage)
932 {
933 struct brw_context *brw = compiler->brw;
934 struct gl_shader *shader;
935 int name = 0;
936
937 shader = brw_new_shader(&brw->ctx, name, stage_info[stage].token);
938 fail_if(shader == NULL, "failed to create %s shader\n", stage_info[stage].name);
939
940 shader->Source = strdup(src_as_glsl(pipeline->shaders[stage]->module->data));
941 _mesa_glsl_compile_shader(&brw->ctx, shader, false, false);
942 fail_on_compile_error(shader->CompileStatus, shader->InfoLog);
943
944 program->Shaders[program->NumShaders] = shader;
945 program->NumShaders++;
946 }
947
948 static void
949 setup_nir_io(struct gl_program *prog,
950 nir_shader *shader)
951 {
952 foreach_list_typed(nir_variable, var, node, &shader->inputs) {
953 prog->InputsRead |= BITFIELD64_BIT(var->data.location);
954 }
955
956 foreach_list_typed(nir_variable, var, node, &shader->outputs) {
957 prog->OutputsWritten |= BITFIELD64_BIT(var->data.location);
958 }
959 }
960
961 static void
962 anv_compile_shader_spirv(struct anv_compiler *compiler,
963 struct gl_shader_program *program,
964 struct anv_pipeline *pipeline, uint32_t stage)
965 {
966 struct brw_context *brw = compiler->brw;
967 struct anv_shader *shader = pipeline->shaders[stage];
968 struct gl_shader *mesa_shader;
969 int name = 0;
970
971 mesa_shader = brw_new_shader(&brw->ctx, name, stage_info[stage].token);
972 fail_if(mesa_shader == NULL,
973 "failed to create %s shader\n", stage_info[stage].name);
974
975 switch (stage) {
976 case VK_SHADER_STAGE_VERTEX:
977 mesa_shader->Program = &rzalloc(mesa_shader, struct brw_vertex_program)->program.Base;
978 break;
979 case VK_SHADER_STAGE_GEOMETRY:
980 mesa_shader->Program = &rzalloc(mesa_shader, struct brw_geometry_program)->program.Base;
981 break;
982 case VK_SHADER_STAGE_FRAGMENT:
983 mesa_shader->Program = &rzalloc(mesa_shader, struct brw_fragment_program)->program.Base;
984 break;
985 case VK_SHADER_STAGE_COMPUTE:
986 mesa_shader->Program = &rzalloc(mesa_shader, struct brw_compute_program)->program.Base;
987 break;
988 }
989
990 mesa_shader->Program->Parameters =
991 rzalloc(mesa_shader, struct gl_program_parameter_list);
992
993 mesa_shader->Type = stage_info[stage].token;
994 mesa_shader->Stage = stage_info[stage].stage;
995
996 assert(shader->module->size % 4 == 0);
997
998 struct gl_shader_compiler_options *glsl_options =
999 &compiler->screen->compiler->glsl_compiler_options[stage_info[stage].stage];
1000
1001 mesa_shader->Program->nir =
1002 spirv_to_nir((uint32_t *)shader->module->data, shader->module->size / 4,
1003 glsl_options->NirOptions);
1004 nir_validate_shader(mesa_shader->Program->nir);
1005
1006 brw_process_nir(mesa_shader->Program->nir,
1007 compiler->screen->devinfo,
1008 NULL, mesa_shader->Stage, false);
1009
1010 setup_nir_io(mesa_shader->Program, mesa_shader->Program->nir);
1011
1012 fail_if(mesa_shader->Program->nir == NULL,
1013 "failed to translate SPIR-V to NIR\n");
1014
1015 program->Shaders[program->NumShaders] = mesa_shader;
1016 program->NumShaders++;
1017 }
1018
1019 static void
1020 add_compiled_stage(struct anv_pipeline *pipeline, uint32_t stage,
1021 struct brw_stage_prog_data *prog_data)
1022 {
1023 struct brw_device_info *devinfo = &pipeline->device->info;
1024 uint32_t max_threads[] = {
1025 [VK_SHADER_STAGE_VERTEX] = devinfo->max_vs_threads,
1026 [VK_SHADER_STAGE_TESS_CONTROL] = 0,
1027 [VK_SHADER_STAGE_TESS_EVALUATION] = 0,
1028 [VK_SHADER_STAGE_GEOMETRY] = devinfo->max_gs_threads,
1029 [VK_SHADER_STAGE_FRAGMENT] = devinfo->max_wm_threads,
1030 [VK_SHADER_STAGE_COMPUTE] = devinfo->max_cs_threads,
1031 };
1032
1033 pipeline->prog_data[stage] = prog_data;
1034 pipeline->active_stages |= 1 << stage;
1035 pipeline->scratch_start[stage] = pipeline->total_scratch;
1036 pipeline->total_scratch =
1037 align_u32(pipeline->total_scratch, 1024) +
1038 prog_data->total_scratch * max_threads[stage];
1039 }
1040
1041 int
1042 anv_compiler_run(struct anv_compiler *compiler, struct anv_pipeline *pipeline)
1043 {
1044 struct gl_shader_program *program;
1045 int name = 0;
1046 struct brw_context *brw = compiler->brw;
1047
1048 pipeline->writes_point_size = false;
1049
1050 /* When we free the pipeline, we detect stages based on the NULL status
1051 * of various prog_data pointers. Make them NULL by default.
1052 */
1053 memset(pipeline->prog_data, 0, sizeof(pipeline->prog_data));
1054 memset(pipeline->scratch_start, 0, sizeof(pipeline->scratch_start));
1055
1056 brw->use_rep_send = pipeline->use_repclear;
1057 brw->no_simd8 = pipeline->use_repclear;
1058
1059 program = brw->ctx.Driver.NewShaderProgram(name);
1060 program->Shaders = (struct gl_shader **)
1061 calloc(VK_SHADER_STAGE_NUM, sizeof(struct gl_shader *));
1062 fail_if(program == NULL || program->Shaders == NULL,
1063 "failed to create program\n");
1064
1065 bool all_spirv = true;
1066 for (unsigned i = 0; i < VK_SHADER_STAGE_NUM; i++) {
1067 if (pipeline->shaders[i] == NULL)
1068 continue;
1069
1070 /* You need at least this much for "void main() { }" anyway */
1071 assert(pipeline->shaders[i]->module->size >= 12);
1072
1073 if (src_as_glsl(pipeline->shaders[i]->module->data)) {
1074 all_spirv = false;
1075 break;
1076 }
1077
1078 assert(pipeline->shaders[i]->module->size % 4 == 0);
1079 }
1080
1081 if (all_spirv) {
1082 for (unsigned i = 0; i < VK_SHADER_STAGE_NUM; i++) {
1083 if (pipeline->shaders[i])
1084 anv_compile_shader_spirv(compiler, program, pipeline, i);
1085 }
1086
1087 for (unsigned i = 0; i < program->NumShaders; i++) {
1088 struct gl_shader *shader = program->Shaders[i];
1089 program->_LinkedShaders[shader->Stage] = shader;
1090 }
1091 } else {
1092 for (unsigned i = 0; i < VK_SHADER_STAGE_NUM; i++) {
1093 if (pipeline->shaders[i])
1094 anv_compile_shader_glsl(compiler, program, pipeline, i);
1095 }
1096
1097 _mesa_glsl_link_shader(&brw->ctx, program);
1098 fail_on_compile_error(program->LinkStatus,
1099 program->InfoLog);
1100 }
1101
1102 bool success;
1103 pipeline->active_stages = 0;
1104 pipeline->total_scratch = 0;
1105
1106 if (pipeline->shaders[VK_SHADER_STAGE_VERTEX]) {
1107 struct brw_vs_prog_key vs_key;
1108 struct gl_vertex_program *vp = (struct gl_vertex_program *)
1109 program->_LinkedShaders[MESA_SHADER_VERTEX]->Program;
1110 struct brw_vertex_program *bvp = brw_vertex_program(vp);
1111
1112 brw_vs_populate_key(brw, bvp, &vs_key);
1113
1114 success = really_do_vs_prog(brw, program, bvp, &vs_key, pipeline);
1115 fail_if(!success, "do_wm_prog failed\n");
1116 add_compiled_stage(pipeline, VK_SHADER_STAGE_VERTEX,
1117 &pipeline->vs_prog_data.base.base);
1118
1119 if (vp->Base.OutputsWritten & VARYING_SLOT_PSIZ)
1120 pipeline->writes_point_size = true;
1121 } else {
1122 memset(&pipeline->vs_prog_data, 0, sizeof(pipeline->vs_prog_data));
1123 pipeline->vs_simd8 = NO_KERNEL;
1124 }
1125
1126
1127 if (pipeline->shaders[VK_SHADER_STAGE_GEOMETRY]) {
1128 struct brw_gs_prog_key gs_key;
1129 struct gl_geometry_program *gp = (struct gl_geometry_program *)
1130 program->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program;
1131 struct brw_geometry_program *bgp = brw_geometry_program(gp);
1132
1133 brw_gs_populate_key(brw, pipeline, bgp, &gs_key);
1134
1135 success = really_do_gs_prog(brw, program, bgp, &gs_key, pipeline);
1136 fail_if(!success, "do_gs_prog failed\n");
1137 add_compiled_stage(pipeline, VK_SHADER_STAGE_GEOMETRY,
1138 &pipeline->gs_prog_data.base.base);
1139
1140 if (gp->Base.OutputsWritten & VARYING_SLOT_PSIZ)
1141 pipeline->writes_point_size = true;
1142 } else {
1143 pipeline->gs_vec4 = NO_KERNEL;
1144 }
1145
1146 if (pipeline->shaders[VK_SHADER_STAGE_FRAGMENT]) {
1147 struct brw_wm_prog_key wm_key;
1148 struct gl_fragment_program *fp = (struct gl_fragment_program *)
1149 program->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program;
1150 struct brw_fragment_program *bfp = brw_fragment_program(fp);
1151
1152 brw_wm_populate_key(brw, bfp, &wm_key);
1153
1154 success = really_do_wm_prog(brw, program, bfp, &wm_key, pipeline);
1155 fail_if(!success, "do_wm_prog failed\n");
1156 add_compiled_stage(pipeline, VK_SHADER_STAGE_FRAGMENT,
1157 &pipeline->wm_prog_data.base);
1158 }
1159
1160 if (pipeline->shaders[VK_SHADER_STAGE_COMPUTE]) {
1161 struct brw_cs_prog_key cs_key;
1162 struct gl_compute_program *cp = (struct gl_compute_program *)
1163 program->_LinkedShaders[MESA_SHADER_COMPUTE]->Program;
1164 struct brw_compute_program *bcp = brw_compute_program(cp);
1165
1166 brw_cs_populate_key(brw, bcp, &cs_key);
1167
1168 success = brw_codegen_cs_prog(brw, program, bcp, &cs_key, pipeline);
1169 fail_if(!success, "brw_codegen_cs_prog failed\n");
1170 add_compiled_stage(pipeline, VK_SHADER_STAGE_COMPUTE,
1171 &pipeline->cs_prog_data.base);
1172 }
1173
1174 /* XXX: Deleting the shader is broken with our current SPIR-V hacks. We
1175 * need to fix this ASAP.
1176 */
1177 if (!all_spirv)
1178 brw->ctx.Driver.DeleteShaderProgram(&brw->ctx, program);
1179
1180 struct anv_device *device = compiler->device;
1181 while (device->scratch_block_pool.bo.size < pipeline->total_scratch)
1182 anv_block_pool_alloc(&device->scratch_block_pool);
1183
1184 gen7_compute_urb_partition(pipeline);
1185
1186 return 0;
1187 }
1188
1189 /* This badly named function frees the struct anv_pipeline data that the compiler
1190 * allocates. Currently just the prog_data structs.
1191 */
1192 void
1193 anv_compiler_free(struct anv_pipeline *pipeline)
1194 {
1195 for (uint32_t stage = 0; stage < VK_SHADER_STAGE_NUM; stage++) {
1196 if (pipeline->prog_data[stage]) {
1197 free(pipeline->prog_data[stage]->map_entries);
1198 ralloc_free(pipeline->prog_data[stage]->param);
1199 ralloc_free(pipeline->prog_data[stage]->pull_param);
1200 }
1201 }
1202 }
1203
1204 }