brw_blorp_blit.cpp revision 19e9b24626c2b9d7abef054d57bb2a52106c545b
1/*
2 * Copyright © 2012 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "main/teximage.h"
25
26#include "glsl/ralloc.h"
27
28#include "intel_fbo.h"
29
30#include "brw_blorp.h"
31#include "brw_context.h"
32#include "brw_eu.h"
33#include "brw_state.h"
34
35
36/**
37 * Helper function for handling mirror image blits.
38 *
39 * If coord0 > coord1, swap them and invert the "mirror" boolean.
40 */
41static inline void
42fixup_mirroring(bool &mirror, GLint &coord0, GLint &coord1)
43{
44   if (coord0 > coord1) {
45      mirror = !mirror;
46      GLint tmp = coord0;
47      coord0 = coord1;
48      coord1 = tmp;
49   }
50}
51
52
53static bool
54try_blorp_blit(struct intel_context *intel,
55               GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
56               GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
57               GLenum filter, GLbitfield buffer_bit)
58{
59   struct gl_context *ctx = &intel->ctx;
60
61   /* Sync up the state of window system buffers.  We need to do this before
62    * we go looking for the buffers.
63    */
64   intel_prepare_render(intel);
65
66   /* Find buffers */
67   const struct gl_framebuffer *read_fb = ctx->ReadBuffer;
68   const struct gl_framebuffer *draw_fb = ctx->DrawBuffer;
69   struct gl_renderbuffer *src_rb;
70   struct gl_renderbuffer *dst_rb;
71   switch (buffer_bit) {
72   case GL_COLOR_BUFFER_BIT:
73      src_rb = read_fb->_ColorReadBuffer;
74      dst_rb =
75         draw_fb->Attachment[
76            draw_fb->_ColorDrawBufferIndexes[0]].Renderbuffer;
77      break;
78   case GL_DEPTH_BUFFER_BIT:
79      src_rb = read_fb->Attachment[BUFFER_DEPTH].Renderbuffer;
80      dst_rb = draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer;
81      break;
82   case GL_STENCIL_BUFFER_BIT:
83      src_rb = read_fb->Attachment[BUFFER_STENCIL].Renderbuffer;
84      dst_rb = draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer;
85      break;
86   default:
87      assert(false);
88   }
89
90   /* Validate source */
91   if (!src_rb) return false;
92   struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
93   struct intel_mipmap_tree *src_mt = src_irb->mt;
94   if (!src_mt) return false;
95   if (buffer_bit == GL_STENCIL_BUFFER_BIT && src_mt->stencil_mt)
96      src_mt = src_mt->stencil_mt;
97   switch (src_mt->format) {
98   case MESA_FORMAT_ARGB8888:
99   case MESA_FORMAT_X8_Z24:
100   case MESA_FORMAT_S8:
101      break; /* Supported */
102   default:
103      /* Unsupported format.
104       *
105       * TODO: need to support all formats that are allowed as multisample
106       * render targets.
107       */
108      return false;
109   }
110
111   /* Validate destination */
112   if (!dst_rb) return false;
113   struct intel_renderbuffer *dst_irb = intel_renderbuffer(dst_rb);
114   struct intel_mipmap_tree *dst_mt = dst_irb->mt;
115   if (!dst_mt) return false;
116   if (buffer_bit == GL_STENCIL_BUFFER_BIT && dst_mt->stencil_mt)
117      dst_mt = dst_mt->stencil_mt;
118   switch (dst_mt->format) {
119   case MESA_FORMAT_ARGB8888:
120   case MESA_FORMAT_X8_Z24:
121   case MESA_FORMAT_S8:
122      break; /* Supported */
123   default:
124      /* Unsupported format.
125       *
126       * TODO: need to support all formats that are allowed as multisample
127       * render targets.
128       */
129      return false;
130   }
131
132   /* Account for the fact that in the system framebuffer, the origin is at
133    * the lower left.
134    */
135   if (read_fb->Name == 0) {
136      srcY0 = read_fb->Height - srcY0;
137      srcY1 = read_fb->Height - srcY1;
138   }
139   if (draw_fb->Name == 0) {
140      dstY0 = draw_fb->Height - dstY0;
141      dstY1 = draw_fb->Height - dstY1;
142   }
143
144   /* Detect if the blit needs to be mirrored */
145   bool mirror_x = false, mirror_y = false;
146   fixup_mirroring(mirror_x, srcX0, srcX1);
147   fixup_mirroring(mirror_x, dstX0, dstX1);
148   fixup_mirroring(mirror_y, srcY0, srcY1);
149   fixup_mirroring(mirror_y, dstY0, dstY1);
150
151   /* Make sure width and height match */
152   GLsizei width = srcX1 - srcX0;
153   GLsizei height = srcY1 - srcY0;
154   if (width != dstX1 - dstX0) return false;
155   if (height != dstY1 - dstY0) return false;
156
157   /* Make sure width and height don't need to be clipped or scissored.
158    * TODO: support clipping and scissoring.
159    */
160   if (srcX0 < 0 || (GLuint) srcX1 > read_fb->Width) return false;
161   if (srcY0 < 0 || (GLuint) srcY1 > read_fb->Height) return false;
162   if (dstX0 < 0 || (GLuint) dstX1 > draw_fb->Width) return false;
163   if (dstY0 < 0 || (GLuint) dstY1 > draw_fb->Height) return false;
164   if (ctx->Scissor.Enabled) return false;
165
166   /* Get ready to blit.  This includes depth resolving the src and dst
167    * buffers if necessary.
168    */
169   intel_renderbuffer_resolve_depth(intel, src_irb);
170   intel_renderbuffer_resolve_depth(intel, dst_irb);
171
172   /* Do the blit */
173   brw_blorp_blit_params params(src_mt, dst_mt,
174                                srcX0, srcY0, dstX0, dstY0, dstX1, dstY1,
175                                mirror_x, mirror_y);
176   params.exec(intel);
177
178   /* Mark the dst buffer as needing a HiZ resolve if necessary. */
179   intel_renderbuffer_set_needs_hiz_resolve(dst_irb);
180
181   return true;
182}
183
184GLbitfield
185brw_blorp_framebuffer(struct intel_context *intel,
186                      GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
187                      GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
188                      GLbitfield mask, GLenum filter)
189{
190   /* BLORP is only supported on Gen6.  TODO: implement on Gen7. */
191   if (intel->gen != 6)
192      return mask;
193
194   static GLbitfield buffer_bits[] = {
195      GL_COLOR_BUFFER_BIT,
196      GL_DEPTH_BUFFER_BIT,
197      GL_STENCIL_BUFFER_BIT,
198   };
199
200   for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
201      if ((mask & buffer_bits[i]) &&
202       try_blorp_blit(intel,
203                      srcX0, srcY0, srcX1, srcY1,
204                      dstX0, dstY0, dstX1, dstY1,
205                      filter, buffer_bits[i])) {
206         mask &= ~buffer_bits[i];
207      }
208   }
209
210   return mask;
211}
212
213/**
214 * Generator for WM programs used in BLORP blits.
215 *
216 * The bulk of the work done by the WM program is to wrap and unwrap the
217 * coordinate transformations used by the hardware to store surfaces in
218 * memory.  The hardware transforms a pixel location (X, Y, S) (where S is the
219 * sample index for a multisampled surface) to a memory offset by the
220 * following formulas:
221 *
222 *   offset = tile(tiling_format, encode_msaa(num_samples, X, Y, S))
223 *   (X, Y, S) = decode_msaa(num_samples, detile(tiling_format, offset))
224 *
225 * For a single-sampled surface, encode_msaa() and decode_msaa are the
226 * identity function:
227 *
228 *   encode_msaa(1, X, Y, 0) = (X, Y)
229 *   decode_msaa(1, X, Y) = (X, Y, 0)
230 *
231 * For a 4x multisampled surface, encode_msaa() embeds the sample number into
232 * bit 1 of the X and Y coordinates:
233 *
234 *   encode_msaa(4, X, Y, S) = (X', Y')
235 *     where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
236 *           Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
237 *   decode_msaa(4, X, Y) = (X', Y', S)
238 *     where X' = (X & ~0b11) >> 1 | (X & 0b1)
239 *           Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
240 *           S = (Y & 0b10) | (X & 0b10) >> 1
241 *
242 * For X tiling, tile() combines together the low-order bits of the X and Y
243 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
244 * bytes wide and 8 rows high:
245 *
246 *   tile(x_tiled, X, Y) = A
247 *     where A = tile_num << 12 | offset
248 *           tile_num = (Y >> 3) * tile_pitch + (X' >> 9)
249 *           offset = (Y & 0b111) << 9
250 *                    | (X & 0b111111111)
251 *           X' = X * cpp
252 *   detile(x_tiled, A) = (X, Y)
253 *     where X = X' / cpp
254 *           Y = (tile_num / tile_pitch) << 3
255 *               | (A & 0b111000000000) >> 9
256 *           X' = (tile_num % tile_pitch) << 9
257 *                | (A & 0b111111111)
258 *
259 * (In all tiling formulas, cpp is the number of bytes occupied by a single
260 * sample ("chars per pixel"), and tile_pitch is the number of 4k tiles
261 * required to fill the width of the surface).
262 *
263 * For Y tiling, tile() combines together the low-order bits of the X and Y
264 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
265 * bytes wide and 32 rows high:
266 *
267 *   tile(y_tiled, X, Y) = A
268 *     where A = tile_num << 12 | offset
269 *           tile_num = (Y >> 5) * tile_pitch + (X' >> 7)
270 *           offset = (X' & 0b1110000) << 5
271 *                    | (Y' & 0b11111) << 4
272 *                    | (X' & 0b1111)
273 *           X' = X * cpp
274 *   detile(y_tiled, A) = (X, Y)
275 *     where X = X' / cpp
276 *           Y = (tile_num / tile_pitch) << 5
277 *               | (A & 0b111110000) >> 4
278 *           X' = (tile_num % tile_pitch) << 7
279 *                | (A & 0b111000000000) >> 5
280 *                | (A & 0b1111)
281 *
282 * For W tiling, tile() combines together the low-order bits of the X and Y
283 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
284 * bytes wide and 64 rows high (note that W tiling is only used for stencil
285 * buffers, which always have cpp = 1):
286 *
287 *   tile(w_tiled, X, Y) = A
288 *     where A = tile_num << 12 | offset
289 *           tile_num = (Y >> 6) * tile_pitch + (X' >> 6)
290 *           offset = (X' & 0b111000) << 6
291 *                    | (Y & 0b111100) << 3
292 *                    | (X' & 0b100) << 2
293 *                    | (Y & 0b10) << 2
294 *                    | (X' & 0b10) << 1
295 *                    | (Y & 0b1) << 1
296 *                    | (X' & 0b1)
297 *           X' = X * cpp = X
298 *   detile(w_tiled, A) = (X, Y)
299 *     where X = X' / cpp = X'
300 *           Y = (tile_num / tile_pitch) << 6
301 *               | (A & 0b111100000) >> 3
302 *               | (A & 0b1000) >> 2
303 *               | (A & 0b10) >> 1
304 *           X' = (tile_num % tile_pitch) << 6
305 *                | (A & 0b111000000000) >> 6
306 *                | (A & 0b10000) >> 2
307 *                | (A & 0b100) >> 1
308 *                | (A & 0b1)
309 *
310 * Finally, for a non-tiled surface, tile() simply combines together the X and
311 * Y coordinates in the natural way:
312 *
313 *   tile(untiled, X, Y) = A
314 *     where A = Y * pitch + X'
315 *           X' = X * cpp
316 *   detile(untiled, A) = (X, Y)
317 *     where X = X' / cpp
318 *           Y = A / pitch
319 *           X' = A % pitch
320 *
321 * (In these formulas, pitch is the number of bytes occupied by a single row
322 * of samples).
323 */
324class brw_blorp_blit_program
325{
326public:
327   brw_blorp_blit_program(struct brw_context *brw,
328                          const brw_blorp_blit_prog_key *key);
329   ~brw_blorp_blit_program();
330
331   const GLuint *compile(struct brw_context *brw, GLuint *program_size);
332
333   brw_blorp_prog_data prog_data;
334
335private:
336   void alloc_regs();
337   void alloc_push_const_regs(int base_reg);
338   void compute_frag_coords();
339   void translate_tiling(bool old_tiled_w, bool new_tiled_w);
340   void encode_msaa(unsigned num_samples);
341   void decode_msaa(unsigned num_samples);
342   void kill_if_outside_dst_rect();
343   void translate_dst_to_src();
344   void single_to_blend();
345   void sample();
346   void texel_fetch();
347   void texture_lookup(GLuint msg_type,
348                       struct brw_reg mrf_u, struct brw_reg mrf_v);
349   void render_target_write();
350
351   void *mem_ctx;
352   struct brw_context *brw;
353   const brw_blorp_blit_prog_key *key;
354   struct brw_compile func;
355
356   /* Thread dispatch header */
357   struct brw_reg R0;
358
359   /* Pixel X/Y coordinates (always in R1). */
360   struct brw_reg R1;
361
362   /* Push constants */
363   struct brw_reg dst_x0;
364   struct brw_reg dst_x1;
365   struct brw_reg dst_y0;
366   struct brw_reg dst_y1;
367   struct {
368      struct brw_reg multiplier;
369      struct brw_reg offset;
370   } x_transform, y_transform;
371
372   /* Data returned from texture lookup (4 vec16's) */
373   struct brw_reg Rdata;
374
375   /* X coordinates.  We have two of them so that we can perform coordinate
376    * transformations easily.
377    */
378   struct brw_reg x_coords[2];
379
380   /* Y coordinates.  We have two of them so that we can perform coordinate
381    * transformations easily.
382    */
383   struct brw_reg y_coords[2];
384
385   /* Which element of x_coords and y_coords is currently in use.
386    */
387   int xy_coord_index;
388
389   /* True if, at the point in the program currently being compiled, the
390    * sample index is known to be zero.
391    */
392   bool s_is_zero;
393
394   /* Register storing the sample index when s_is_zero is false. */
395   struct brw_reg sample_index;
396
397   /* Temporaries */
398   struct brw_reg t1;
399   struct brw_reg t2;
400
401   /* M2-3: u coordinate */
402   GLuint base_mrf;
403   struct brw_reg mrf_u_float;
404
405   /* M4-5: v coordinate */
406   struct brw_reg mrf_v_float;
407};
408
409brw_blorp_blit_program::brw_blorp_blit_program(
410      struct brw_context *brw,
411      const brw_blorp_blit_prog_key *key)
412   : mem_ctx(ralloc_context(NULL)),
413     brw(brw),
414     key(key)
415{
416   brw_init_compile(brw, &func, mem_ctx);
417}
418
419brw_blorp_blit_program::~brw_blorp_blit_program()
420{
421   ralloc_free(mem_ctx);
422}
423
424const GLuint *
425brw_blorp_blit_program::compile(struct brw_context *brw,
426                                GLuint *program_size)
427{
428   /* Sanity checks */
429   if (key->src_tiled_w) {
430      /* If the source image is W tiled, then tex_samples must be 0.
431       * Otherwise, after conversion between W and Y tiling, there's no
432       * guarantee that the sample index will be 0.
433       */
434      assert(key->tex_samples == 0);
435   }
436
437   if (key->dst_tiled_w) {
438      /* If the destination image is W tiled, then dst_samples must be 0.
439       * Otherwise, after conversion between W and Y tiling, there's no
440       * guarantee that all samples corresponding to a single pixel will still
441       * be together.
442       */
443      assert(key->rt_samples == 0);
444   }
445
446   if (key->blend) {
447      /* We are blending, which means we'll be using a SAMPLE message, which
448       * causes the hardware to pick up the all of the samples corresponding
449       * to this pixel and average them together.  Since we'll be relying on
450       * the hardware to find all of the samples and combine them together,
451       * the surface state for the texture must be configured with the correct
452       * tiling and sample count.
453       */
454      assert(!key->src_tiled_w);
455      assert(key->tex_samples == key->src_samples);
456      assert(key->tex_samples > 0);
457   }
458
459   brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
460
461   alloc_regs();
462   compute_frag_coords();
463
464   /* Render target and texture hardware don't support W tiling. */
465   const bool rt_tiled_w = false;
466   const bool tex_tiled_w = false;
467
468   /* The address that data will be written to is determined by the
469    * coordinates supplied to the WM thread and the tiling and sample count of
470    * the render target, according to the formula:
471    *
472    * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
473    *
474    * If the actual tiling and sample count of the destination surface are not
475    * the same as the configuration of the render target, then these
476    * coordinates are wrong and we have to adjust them to compensate for the
477    * difference.
478    */
479   if (rt_tiled_w != key->dst_tiled_w ||
480       key->rt_samples != key->dst_samples) {
481      encode_msaa(key->rt_samples);
482      /* Now (X, Y) = detile(rt_tiling, offset) */
483      translate_tiling(rt_tiled_w, key->dst_tiled_w);
484      /* Now (X, Y) = detile(dst_tiling, offset) */
485      decode_msaa(key->dst_samples);
486   }
487
488   /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
489    *
490    * That is: X, Y and S now contain the true coordinates and sample index of
491    * the data that the WM thread should output.
492    *
493    * If we need to kill pixels that are outside the destination rectangle,
494    * now is the time to do it.
495    */
496
497   if (key->use_kill)
498      kill_if_outside_dst_rect();
499
500   /* Next, apply a translation to obtain coordinates in the source image. */
501   translate_dst_to_src();
502
503   /* If the source image is not multisampled, then we want to fetch sample
504    * number 0, because that's the only sample there is.
505    */
506   if (key->src_samples == 0)
507      s_is_zero = true;
508
509   /* X, Y, and S are now the coordinates of the pixel in the source image
510    * that we want to texture from.  Exception: if we are blending, then S is
511    * irrelevant, because we are going to fetch all samples.
512    */
513   if (key->blend) {
514      single_to_blend();
515      sample();
516   } else {
517      /* We aren't blending, which means we just want to fetch a single sample
518       * from the source surface.  The address that we want to fetch from is
519       * related to the X, Y and S values according to the formula:
520       *
521       * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
522       *
523       * If the actual tiling and sample count of the source surface are not
524       * the same as the configuration of the texture, then we need to adjust
525       * the coordinates to compensate for the difference.
526       */
527      if (tex_tiled_w != key->src_tiled_w ||
528          key->tex_samples != key->src_samples) {
529         encode_msaa(key->src_samples);
530         /* Now (X, Y) = detile(src_tiling, offset) */
531         translate_tiling(key->src_tiled_w, tex_tiled_w);
532         /* Now (X, Y) = detile(tex_tiling, offset) */
533         decode_msaa(key->tex_samples);
534      }
535
536      /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
537       *
538       * In other words: X, Y, and S now contain values which, when passed to
539       * the texturing unit, will cause data to be read from the correct
540       * memory location.  So we can fetch the texel now.
541       */
542      texel_fetch();
543   }
544
545   /* Finally, write the fetched (or blended) value to the render target and
546    * terminate the thread.
547    */
548   render_target_write();
549   return brw_get_program(&func, program_size);
550}
551
552void
553brw_blorp_blit_program::alloc_push_const_regs(int base_reg)
554{
555#define CONST_LOC(name) offsetof(brw_blorp_wm_push_constants, name)
556#define ALLOC_REG(name) \
557   this->name = \
558      brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, base_reg, CONST_LOC(name) / 2)
559
560   ALLOC_REG(dst_x0);
561   ALLOC_REG(dst_x1);
562   ALLOC_REG(dst_y0);
563   ALLOC_REG(dst_y1);
564   ALLOC_REG(x_transform.multiplier);
565   ALLOC_REG(x_transform.offset);
566   ALLOC_REG(y_transform.multiplier);
567   ALLOC_REG(y_transform.offset);
568#undef CONST_LOC
569#undef ALLOC_REG
570}
571
572void
573brw_blorp_blit_program::alloc_regs()
574{
575   int reg = 0;
576   this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
577   this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
578   prog_data.first_curbe_grf = reg;
579   alloc_push_const_regs(reg);
580   reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
581   this->Rdata = vec16(brw_vec8_grf(reg, 0)); reg += 8;
582   for (int i = 0; i < 2; ++i) {
583      this->x_coords[i]
584         = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
585      this->y_coords[i]
586         = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
587   }
588   this->xy_coord_index = 0;
589   this->sample_index
590      = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
591   this->t1 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
592   this->t2 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
593
594   int mrf = 2;
595   this->base_mrf = mrf;
596   this->mrf_u_float = vec16(brw_message_reg(mrf)); mrf += 2;
597   this->mrf_v_float = vec16(brw_message_reg(mrf)); mrf += 2;
598}
599
600/* In the code that follows, X and Y can be used to quickly refer to the
601 * active elements of x_coords and y_coords, and Xp and Yp ("X prime" and "Y
602 * prime") to the inactive elements.
603 *
604 * S can be used to quickly refer to sample_index.
605 */
606#define X x_coords[xy_coord_index]
607#define Y y_coords[xy_coord_index]
608#define Xp x_coords[!xy_coord_index]
609#define Yp y_coords[!xy_coord_index]
610#define S sample_index
611
612/* Quickly swap the roles of (X, Y) and (Xp, Yp).  Saves us from having to do
613 * MOVs to transfor (Xp, Yp) to (X, Y) after a coordinate transformation.
614 */
615#define SWAP_XY_AND_XPYP() xy_coord_index = !xy_coord_index;
616
617/**
618 * Emit code to compute the X and Y coordinates of the pixels being rendered
619 * by this WM invocation.
620 *
621 * Assuming the render target is set up for Y tiling, these (X, Y) values are
622 * related to the address offset where outputs will be written by the formula:
623 *
624 *   (X, Y, S) = decode_msaa(detile(offset)).
625 *
626 * (See brw_blorp_blit_program).
627 */
628void
629brw_blorp_blit_program::compute_frag_coords()
630{
631   /* R1.2[15:0] = X coordinate of upper left pixel of subspan 0 (pixel 0)
632    * R1.3[15:0] = X coordinate of upper left pixel of subspan 1 (pixel 4)
633    * R1.4[15:0] = X coordinate of upper left pixel of subspan 2 (pixel 8)
634    * R1.5[15:0] = X coordinate of upper left pixel of subspan 3 (pixel 12)
635    *
636    * Pixels within a subspan are laid out in this arrangement:
637    * 0 1
638    * 2 3
639    *
640    * So, to compute the coordinates of each pixel, we need to read every 2nd
641    * 16-bit value (vstride=2) from R1, starting at the 4th 16-bit value
642    * (suboffset=4), and duplicate each value 4 times (hstride=0, width=4).
643    * In other words, the data we want to access is R1.4<2;4,0>UW.
644    *
645    * Then, we need to add the repeating sequence (0, 1, 0, 1, ...) to the
646    * result, since pixels n+1 and n+3 are in the right half of the subspan.
647    */
648   brw_ADD(&func, X, stride(suboffset(R1, 4), 2, 4, 0), brw_imm_v(0x10101010));
649
650   /* Similarly, Y coordinates for subspans come from R1.2[31:16] through
651    * R1.5[31:16], so to get pixel Y coordinates we need to start at the 5th
652    * 16-bit value instead of the 4th (R1.5<2;4,0>UW instead of
653    * R1.4<2;4,0>UW).
654    *
655    * And we need to add the repeating sequence (0, 0, 1, 1, ...), since
656    * pixels n+2 and n+3 are in the bottom half of the subspan.
657    */
658   brw_ADD(&func, Y, stride(suboffset(R1, 5), 2, 4, 0), brw_imm_v(0x11001100));
659
660   /* Since we always run the WM in a mode that causes a single fragment
661    * dispatch per pixel, it's not meaningful to compute a sample value.  Just
662    * set it to 0.
663    */
664   s_is_zero = true;
665}
666
667/**
668 * Emit code to compensate for the difference between Y and W tiling.
669 *
670 * This code modifies the X and Y coordinates according to the formula:
671 *
672 *   (X', Y') = detile(new_tiling, tile(old_tiling, X, Y))
673 *
674 * (See brw_blorp_blit_program).
675 *
676 * It can only translate between W and Y tiling, so new_tiling and old_tiling
677 * are booleans where true represents W tiling and false represents Y tiling.
678 */
679void
680brw_blorp_blit_program::translate_tiling(bool old_tiled_w, bool new_tiled_w)
681{
682   if (old_tiled_w == new_tiled_w)
683      return;
684
685   if (new_tiled_w) {
686      /* Given X and Y coordinates that describe an address using Y tiling,
687       * translate to the X and Y coordinates that describe the same address
688       * using W tiling.
689       *
690       * If we break down the low order bits of X and Y, using a
691       * single letter to represent each low-order bit:
692       *
693       *   X = A << 7 | 0bBCDEFGH
694       *   Y = J << 5 | 0bKLMNP                                       (1)
695       *
696       * Then we can apply the Y tiling formula to see the memory offset being
697       * addressed:
698       *
699       *   offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH       (2)
700       *
701       * If we apply the W detiling formula to this memory location, that the
702       * corresponding X' and Y' coordinates are:
703       *
704       *   X' = A << 6 | 0bBCDPFH                                     (3)
705       *   Y' = J << 6 | 0bKLMNEG
706       *
707       * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
708       * we need to make the following computation:
709       *
710       *   X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1         (4)
711       *   Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
712       */
713      brw_AND(&func, t1, X, brw_imm_uw(0xfff4)); /* X & ~0b1011 */
714      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b1011) >> 1 */
715      brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
716      brw_SHL(&func, t2, t2, brw_imm_uw(2)); /* (Y & 0b1) << 2 */
717      brw_OR(&func, t1, t1, t2); /* (X & ~0b1011) >> 1 | (Y & 0b1) << 2 */
718      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
719      brw_OR(&func, Xp, t1, t2);
720      brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
721      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
722      brw_AND(&func, t2, X, brw_imm_uw(8)); /* X & 0b1000 */
723      brw_SHR(&func, t2, t2, brw_imm_uw(2)); /* (X & 0b1000) >> 2 */
724      brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (X & 0b1000) >> 2 */
725      brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
726      brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
727      brw_OR(&func, Yp, t1, t2);
728      SWAP_XY_AND_XPYP();
729   } else {
730      /* Applying the same logic as above, but in reverse, we obtain the
731       * formulas:
732       *
733       * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
734       * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
735       */
736      brw_AND(&func, t1, X, brw_imm_uw(0xfffa)); /* X & ~0b101 */
737      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b101) << 1 */
738      brw_AND(&func, t2, Y, brw_imm_uw(2)); /* Y & 0b10 */
739      brw_SHL(&func, t2, t2, brw_imm_uw(2)); /* (Y & 0b10) << 2 */
740      brw_OR(&func, t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2 */
741      brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
742      brw_SHL(&func, t2, t2, brw_imm_uw(1)); /* (Y & 0b1) << 1 */
743      brw_OR(&func, t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2
744                                    | (Y & 0b1) << 1 */
745      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
746      brw_OR(&func, Xp, t1, t2);
747      brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
748      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
749      brw_AND(&func, t2, X, brw_imm_uw(4)); /* X & 0b100 */
750      brw_SHR(&func, t2, t2, brw_imm_uw(2)); /* (X & 0b100) >> 2 */
751      brw_OR(&func, Yp, t1, t2);
752      SWAP_XY_AND_XPYP();
753   }
754}
755
756/**
757 * Emit code to compensate for the difference between MSAA and non-MSAA
758 * surfaces.
759 *
760 * This code modifies the X and Y coordinates according to the formula:
761 *
762 *   (X', Y') = encode_msaa_4x(X, Y, S)
763 *
764 * (See brw_blorp_blit_program).
765 */
766void
767brw_blorp_blit_program::encode_msaa(unsigned num_samples)
768{
769   if (num_samples == 0) {
770      /* No translation necessary. */
771   } else {
772      /* encode_msaa_4x(X, Y, S) = (X', Y')
773       *   where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
774       *         Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
775       */
776      brw_AND(&func, t1, X, brw_imm_uw(0xfffe)); /* X & ~0b1 */
777      if (!s_is_zero) {
778         brw_AND(&func, t2, S, brw_imm_uw(1)); /* S & 0b1 */
779         brw_OR(&func, t1, t1, t2); /* (X & ~0b1) | (S & 0b1) */
780      }
781      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b1) << 1
782                                                | (S & 0b1) << 1 */
783      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
784      brw_OR(&func, Xp, t1, t2);
785      brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
786      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
787      if (!s_is_zero) {
788         brw_AND(&func, t2, S, brw_imm_uw(2)); /* S & 0b10 */
789         brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (S & 0b10) */
790      }
791      brw_AND(&func, t2, Y, brw_imm_uw(1));
792      brw_OR(&func, Yp, t1, t2);
793      SWAP_XY_AND_XPYP();
794   }
795}
796
797/**
798 * Emit code to compensate for the difference between MSAA and non-MSAA
799 * surfaces.
800 *
801 * This code modifies the X and Y coordinates according to the formula:
802 *
803 *   (X', Y', S) = decode_msaa(num_samples, X, Y)
804 *
805 * (See brw_blorp_blit_program).
806 */
807void
808brw_blorp_blit_program::decode_msaa(unsigned num_samples)
809{
810   if (num_samples == 0) {
811      /* No translation necessary. */
812      s_is_zero = true;
813   } else {
814      /* decode_msaa_4x(X, Y) = (X', Y', S)
815       *   where X' = (X & ~0b11) >> 1 | (X & 0b1)
816       *         Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
817       *         S = (Y & 0b10) | (X & 0b10) >> 1
818       */
819      brw_AND(&func, t1, X, brw_imm_uw(0xfffc)); /* X & ~0b11 */
820      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b11) >> 1 */
821      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
822      brw_OR(&func, Xp, t1, t2);
823      brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
824      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
825      brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
826      brw_OR(&func, Yp, t1, t2);
827      brw_AND(&func, t1, Y, brw_imm_uw(2)); /* Y & 0b10 */
828      brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
829      brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
830      brw_OR(&func, S, t1, t2);
831      s_is_zero = false;
832      SWAP_XY_AND_XPYP();
833   }
834}
835
836/**
837 * Emit code that kills pixels whose X and Y coordinates are outside the
838 * boundary of the rectangle defined by the push constants (dst_x0, dst_y0,
839 * dst_x1, dst_y1).
840 */
841void
842brw_blorp_blit_program::kill_if_outside_dst_rect()
843{
844   struct brw_reg f0 = brw_flag_reg();
845   struct brw_reg g1 = retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW);
846   struct brw_reg null16 = vec16(retype(brw_null_reg(), BRW_REGISTER_TYPE_UW));
847
848   brw_CMP(&func, null16, BRW_CONDITIONAL_GE, X, dst_x0);
849   brw_CMP(&func, null16, BRW_CONDITIONAL_GE, Y, dst_y0);
850   brw_CMP(&func, null16, BRW_CONDITIONAL_L, X, dst_x1);
851   brw_CMP(&func, null16, BRW_CONDITIONAL_L, Y, dst_y1);
852
853   brw_set_predicate_control(&func, BRW_PREDICATE_NONE);
854   brw_push_insn_state(&func);
855   brw_set_mask_control(&func, BRW_MASK_DISABLE);
856   brw_AND(&func, g1, f0, g1);
857   brw_pop_insn_state(&func);
858}
859
860/**
861 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
862 * coordinates.
863 */
864void
865brw_blorp_blit_program::translate_dst_to_src()
866{
867   brw_MUL(&func, Xp, X, x_transform.multiplier);
868   brw_MUL(&func, Yp, Y, y_transform.multiplier);
869   brw_ADD(&func, Xp, Xp, x_transform.offset);
870   brw_ADD(&func, Yp, Yp, y_transform.offset);
871   SWAP_XY_AND_XPYP();
872}
873
874/**
875 * Emit code to transform the X and Y coordinates as needed for blending
876 * together the different samples in an MSAA texture.
877 */
878void
879brw_blorp_blit_program::single_to_blend()
880{
881   /* When looking up samples in an MSAA texture using the SAMPLE message,
882    * Gen6 requires the texture coordinates to be odd integers (so that they
883    * correspond to the center of a 2x2 block representing the four samples
884    * that maxe up a pixel).  So we need to multiply our X and Y coordinates
885    * each by 2 and then add 1.
886    */
887   brw_SHL(&func, t1, X, brw_imm_w(1));
888   brw_SHL(&func, t2, Y, brw_imm_w(1));
889   brw_ADD(&func, Xp, t1, brw_imm_w(1));
890   brw_ADD(&func, Yp, t2, brw_imm_w(1));
891   SWAP_XY_AND_XPYP();
892}
893
894/**
895 * Emit code to look up a value in the texture using the SAMPLE message (which
896 * does blending of MSAA surfaces).
897 */
898void
899brw_blorp_blit_program::sample()
900{
901   texture_lookup(GEN5_SAMPLER_MESSAGE_SAMPLE, mrf_u_float, mrf_v_float);
902}
903
904/**
905 * Emit code to look up a value in the texture using the SAMPLE_LD message
906 * (which does a simple texel fetch).
907 */
908void
909brw_blorp_blit_program::texel_fetch()
910{
911   assert(s_is_zero);
912   texture_lookup(GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
913                  retype(mrf_u_float, BRW_REGISTER_TYPE_UD),
914                  retype(mrf_v_float, BRW_REGISTER_TYPE_UD));
915}
916
917void
918brw_blorp_blit_program::texture_lookup(GLuint msg_type,
919                                       struct brw_reg mrf_u,
920                                       struct brw_reg mrf_v)
921{
922   /* Expand X and Y coordinates from 16 bits to 32 bits. */
923   brw_MOV(&func, vec8(mrf_u), vec8(X));
924   brw_set_compression_control(&func, BRW_COMPRESSION_2NDHALF);
925   brw_MOV(&func, offset(vec8(mrf_u), 1), suboffset(vec8(X), 8));
926   brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
927   brw_MOV(&func, vec8(mrf_v), vec8(Y));
928   brw_set_compression_control(&func, BRW_COMPRESSION_2NDHALF);
929   brw_MOV(&func, offset(vec8(mrf_v), 1), suboffset(vec8(Y), 8));
930   brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
931
932   brw_SAMPLE(&func,
933              retype(Rdata, BRW_REGISTER_TYPE_UW) /* dest */,
934              base_mrf /* msg_reg_nr */,
935              vec8(mrf_u) /* src0 */,
936              BRW_BLORP_TEXTURE_BINDING_TABLE_INDEX,
937              0 /* sampler -- ignored for SAMPLE_LD message */,
938              WRITEMASK_XYZW,
939              msg_type,
940              8 /* response_length.  TODO: should be smaller for non-RGBA formats? */,
941              4 /* msg_length */,
942              0 /* header_present */,
943              BRW_SAMPLER_SIMD_MODE_SIMD16,
944              BRW_SAMPLER_RETURN_FORMAT_FLOAT32);
945}
946
947#undef X
948#undef Y
949#undef U
950#undef V
951#undef S
952#undef SWAP_XY_AND_XPYP
953
954void
955brw_blorp_blit_program::render_target_write()
956{
957   struct brw_reg mrf_rt_write = vec16(brw_message_reg(base_mrf));
958   int mrf_offset = 0;
959
960   /* If we may have killed pixels, then we need to send R0 and R1 in a header
961    * so that the render target knows which pixels we killed.
962    */
963   bool use_header = key->use_kill;
964   if (use_header) {
965      /* Copy R0/1 to MRF */
966      brw_MOV(&func, retype(mrf_rt_write, BRW_REGISTER_TYPE_UD),
967              retype(R0, BRW_REGISTER_TYPE_UD));
968      mrf_offset += 2;
969   }
970
971   /* Copy texture data to MRFs */
972   for (int i = 0; i < 4; ++i) {
973      /* E.g. mov(16) m2.0<1>:f r2.0<8;8,1>:f { Align1, H1 } */
974      brw_MOV(&func, offset(mrf_rt_write, mrf_offset), offset(vec8(Rdata), 2*i));
975      mrf_offset += 2;
976   }
977
978   /* Now write to the render target and terminate the thread */
979   brw_fb_WRITE(&func,
980                16 /* dispatch_width */,
981                base_mrf /* msg_reg_nr */,
982                mrf_rt_write /* src0 */,
983                BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
984                mrf_offset /* msg_length.  TODO: Should be smaller for non-RGBA formats. */,
985                0 /* response_length */,
986                true /* eot */,
987                use_header);
988}
989
990
991void
992brw_blorp_coord_transform_params::setup(GLuint src0, GLuint dst0, GLuint dst1,
993                                        bool mirror)
994{
995   if (!mirror) {
996      /* When not mirroring a coordinate (say, X), we need:
997       *   x' - src_x0 = x - dst_x0
998       * Therefore:
999       *   x' = 1*x + (src_x0 - dst_x0)
1000       */
1001      multiplier = 1;
1002      offset = src0 - dst0;
1003   } else {
1004      /* When mirroring X we need:
1005       *   x' - src_x0 = dst_x1 - x - 1
1006       * Therefore:
1007       *   x' = -1*x + (src_x0 + dst_x1 - 1)
1008       */
1009      multiplier = -1;
1010      offset = src0 + dst1 - 1;
1011   }
1012}
1013
1014
1015brw_blorp_blit_params::brw_blorp_blit_params(struct intel_mipmap_tree *src_mt,
1016                                             struct intel_mipmap_tree *dst_mt,
1017                                             GLuint src_x0, GLuint src_y0,
1018                                             GLuint dst_x0, GLuint dst_y0,
1019                                             GLuint dst_x1, GLuint dst_y1,
1020                                             bool mirror_x, bool mirror_y)
1021{
1022   src.set(src_mt, 0, 0);
1023   dst.set(dst_mt, 0, 0);
1024
1025   use_wm_prog = true;
1026   memset(&wm_prog_key, 0, sizeof(wm_prog_key));
1027
1028   if (src_mt->num_samples > 0 && dst_mt->num_samples > 0) {
1029      /* We are blitting from a multisample buffer to a multisample buffer, so
1030       * we must preserve samples within a pixel.  This means we have to
1031       * configure the render target and texture surface states as
1032       * single-sampled, so that the WM program can access each sample
1033       * individually.
1034       */
1035      src.num_samples = dst.num_samples = 0;
1036   }
1037
1038   /* The render path must be configured to use the same number of samples as
1039    * the destination buffer.
1040    */
1041   num_samples = dst.num_samples;
1042
1043   GLenum base_format = _mesa_get_format_base_format(src_mt->format);
1044   if (base_format != GL_DEPTH_COMPONENT && /* TODO: what about depth/stencil? */
1045       base_format != GL_STENCIL_INDEX &&
1046       src_mt->num_samples > 0 && dst_mt->num_samples == 0) {
1047      /* We are downsampling a color buffer, so blend. */
1048      wm_prog_key.blend = true;
1049   }
1050
1051   /* src_samples and dst_samples are the true sample counts */
1052   wm_prog_key.src_samples = src_mt->num_samples;
1053   wm_prog_key.dst_samples = dst_mt->num_samples;
1054
1055   /* tex_samples and rt_samples are the sample counts that are set up in
1056    * SURFACE_STATE.
1057    */
1058   wm_prog_key.tex_samples = src.num_samples;
1059   wm_prog_key.rt_samples  = dst.num_samples;
1060
1061   wm_prog_key.src_tiled_w = src.map_stencil_as_y_tiled;
1062   wm_prog_key.dst_tiled_w = dst.map_stencil_as_y_tiled;
1063   x0 = wm_push_consts.dst_x0 = dst_x0;
1064   y0 = wm_push_consts.dst_y0 = dst_y0;
1065   x1 = wm_push_consts.dst_x1 = dst_x1;
1066   y1 = wm_push_consts.dst_y1 = dst_y1;
1067   wm_push_consts.x_transform.setup(src_x0, dst_x0, dst_x1, mirror_x);
1068   wm_push_consts.y_transform.setup(src_y0, dst_y0, dst_y1, mirror_y);
1069
1070   if (dst.num_samples == 0 && dst_mt->num_samples > 0) {
1071      /* We must expand the rectangle we send through the rendering pipeline,
1072       * to account for the fact that we are mapping the destination region as
1073       * single-sampled when it is in fact multisampled.  We must also align
1074       * it to a multiple of the multisampling pattern, because the
1075       * differences between multisampled and single-sampled surface formats
1076       * will mean that pixels are scrambled within the multisampling pattern.
1077       * TODO: what if this makes the coordinates too large?
1078       */
1079      x0 = (x0 * 2) & ~3;
1080      y0 = (y0 * 2) & ~3;
1081      x1 = ALIGN(x1 * 2, 4);
1082      y1 = ALIGN(y1 * 2, 4);
1083      wm_prog_key.use_kill = true;
1084   }
1085
1086   if (dst.map_stencil_as_y_tiled) {
1087      /* We must modify the rectangle we send through the rendering pipeline,
1088       * to account for the fact that we are mapping it as Y-tiled when it is
1089       * in fact W-tiled.  Y tiles have dimensions 128x32 whereas W tiles have
1090       * dimensions 64x64.  We must also align it to a multiple of the tile
1091       * size, because the differences between W and Y tiling formats will
1092       * mean that pixels are scrambled within the tile.
1093       * TODO: what if this makes the coordinates too large?
1094       */
1095      x0 = (x0 * 2) & ~127;
1096      y0 = (y0 / 2) & ~31;
1097      x1 = ALIGN(x1 * 2, 128);
1098      y1 = ALIGN(y1 / 2, 32);
1099      wm_prog_key.use_kill = true;
1100   }
1101}
1102
1103uint32_t
1104brw_blorp_blit_params::get_wm_prog(struct brw_context *brw,
1105                                   brw_blorp_prog_data **prog_data) const
1106{
1107   uint32_t prog_offset;
1108   if (!brw_search_cache(&brw->cache, BRW_BLORP_BLIT_PROG,
1109                         &this->wm_prog_key, sizeof(this->wm_prog_key),
1110                         &prog_offset, prog_data)) {
1111      brw_blorp_blit_program prog(brw, &this->wm_prog_key);
1112      GLuint program_size;
1113      const GLuint *program = prog.compile(brw, &program_size);
1114      brw_upload_cache(&brw->cache, BRW_BLORP_BLIT_PROG,
1115                       &this->wm_prog_key, sizeof(this->wm_prog_key),
1116                       program, program_size,
1117                       &prog.prog_data, sizeof(prog.prog_data),
1118                       &prog_offset, prog_data);
1119   }
1120   return prog_offset;
1121}
1122