RandomTransform.c 27.1 KB
Newer Older
Andy Cedilnik's avatar
Andy Cedilnik committed
1 2
/* -*- c -*- *****************************************************************
** Copyright (C) 2003 Sandia Corporation
3 4 5 6
** Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
** the U.S. Government retains certain rights in this software.
**
** This source code is released under the New BSD License.
Andy Cedilnik's avatar
Andy Cedilnik committed
7 8 9 10 11
**
** Test that has each processor draw a randomly placed quadrilateral.
** Makes sure that all compositions are equivalent.
*****************************************************************************/

12
#include <IceTGL.h>
13 14
#include <IceTDevCommunication.h>
#include <IceTDevImage.h>
Kenneth Moreland's avatar
Kenneth Moreland committed
15 16
#include <IceTDevPorting.h>
#include <IceTDevState.h>
Andy Cedilnik's avatar
Andy Cedilnik committed
17
#include "test_codes.h"
18
#include "test_util.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
19 20 21 22 23 24 25

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>

26 27 28 29 30
static int g_tile_dim;
static float g_color[3];
static IceTFloat g_modelview[16];
static IceTImage g_refimage_opaque;
static IceTImage g_refimage_transparent;
Andy Cedilnik's avatar
Andy Cedilnik committed
31 32 33

static void draw(void)
{
34
    /* printstat("In draw\n"); */
Andy Cedilnik's avatar
Andy Cedilnik committed
35 36
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_QUADS);
37 38 39 40
      glVertex3d(-1.0, -1.0, 0.0);
      glVertex3d(1.0, -1.0, 0.0);
      glVertex3d(1.0, 1.0, 0.0);
      glVertex3d(-1.0, 1.0, 0.0);
Andy Cedilnik's avatar
Andy Cedilnik committed
41
    glEnd();
42
    /* printstat("Leaving draw\n"); */
Andy Cedilnik's avatar
Andy Cedilnik committed
43 44
}

45
#define DIFF(x, y)      ((x) < (y) ? (y) - (x) : (x) - (y))
Kenneth Moreland's avatar
Kenneth Moreland committed
46

47 48 49
static int compare_color_buffers(IceTSizeType local_width,
                                 IceTSizeType local_height,
                                 IceTImage refimage,
50
                                 IceTImage testimage)
51
{
52 53 54
    IceTSizeType ref_off_x, ref_off_y;
    IceTSizeType bad_pixel_count;
    IceTSizeType x, y;
55
    char filename[FILENAME_MAX];
56
    IceTUByte *refcbuf, *cb;
57
    IceTInt rank;
58

59
    printstat("Checking returned image.\n");
60

61 62
    icetGetIntegerv(ICET_RANK, &rank);

63 64
    if (    (local_width != icetImageGetWidth(testimage))
         || (local_height != icetImageGetHeight(testimage)) ) {
65 66 67 68 69
        printrank("Image dimensions not what is expected!!!!!\n");
        printrank("Expected %dx%d, received %dx%d\n",
                  (int)local_width, (int)local_height,
                  (int)icetImageGetWidth(testimage),
                  (int)icetImageGetHeight(testimage));
70 71 72
        return 0;
    }

73
    refcbuf = malloc(icetImageGetNumPixels(refimage)*4);
74
    icetImageCopyColorub(refimage, refcbuf, ICET_IMAGE_COLOR_RGBA_UBYTE);
75 76

    cb = malloc(icetImageGetNumPixels(testimage)*4);
77
    icetImageCopyColorub(testimage, cb, ICET_IMAGE_COLOR_RGBA_UBYTE);
78

79 80
    ref_off_x = (rank%g_tile_dim) * local_width;
    ref_off_y = (rank/g_tile_dim) * local_height;
81 82 83 84 85 86 87 88 89
    bad_pixel_count = 0;
#define CBR(x, y) (cb[(y)*local_width*4 + (x)*4 + 0])
#define CBG(x, y) (cb[(y)*local_width*4 + (x)*4 + 1])
#define CBB(x, y) (cb[(y)*local_width*4 + (x)*4 + 2])
#define CBA(x, y) (cb[(y)*local_width*4 + (x)*4 + 3])
#define REFCBUFR(x, y) (refcbuf[(y)*SCREEN_WIDTH*4 + (x)*4 + 0])
#define REFCBUFG(x, y) (refcbuf[(y)*SCREEN_WIDTH*4 + (x)*4 + 1])
#define REFCBUFB(x, y) (refcbuf[(y)*SCREEN_WIDTH*4 + (x)*4 + 2])
#define REFCBUFA(x, y) (refcbuf[(y)*SCREEN_WIDTH*4 + (x)*4 + 3])
90
/* #define CB_EQUALS_REF(x, y)                  \ */
91 92 93 94
/*     (   (CBR((x), (y)) == REFCBUFR((x) + ref_off_x, (y) + ref_off_y) )\ */
/*      && (CBG((x), (y)) == REFCBUFG((x) + ref_off_x, (y) + ref_off_y) )\ */
/*      && (CBB((x), (y)) == REFCBUFB((x) + ref_off_x, (y) + ref_off_y) )\ */
/*      && (   CBA((x), (y)) == REFCBUFA((x) + ref_off_x, (y) + ref_off_y)\ */
95 96
/*       || CBA((x), (y)) == 0 ) ) */
#define CB_EQUALS_REF(x, y)                     \
97 98 99 100
    (   (DIFF(CBR((x), (y)), REFCBUFR((x) + ref_off_x, (y) + ref_off_y)) < 5) \
     && (DIFF(CBG((x), (y)), REFCBUFG((x) + ref_off_x, (y) + ref_off_y)) < 5) \
     && (DIFF(CBB((x), (y)), REFCBUFB((x) + ref_off_x, (y) + ref_off_y)) < 5) \
     && (DIFF(CBA((x), (y)), REFCBUFA((x) + ref_off_x, (y) + ref_off_y)) < 5) )
101 102

    for (y = 0; y < local_height; y++) {
103 104 105 106 107 108 109 110 111 112
        for (x = 0; x < local_width; x++) {
            if (!CB_EQUALS_REF(x, y)) {
              /* Uh, oh.  Pixels don't match.  This could be a genuine
               * error or it could be a floating point offset when
               * projecting edge boundries to pixels.  If the latter is the
               * case, there will be very few errors.  Count the errors,
               * and make sure there are not too many.  */
                bad_pixel_count++;
            }
        }
113 114 115 116
    }

  /* Check to make sure there were not too many errors. */
    if (   (bad_pixel_count > 0.001*local_width*local_height)
117 118
        && (bad_pixel_count > local_width)
        && (bad_pixel_count > local_height) )
119 120
    {
      /* Too many errors.  Call it bad. */
121
        printrank("Too many bad pixels!!!!!!\n");
122
      /* Write current images. */
123
        icetSnprintf(filename, FILENAME_MAX, "ref%03d.ppm", rank);
124
        write_ppm(filename, refcbuf, (int)SCREEN_WIDTH, (int)SCREEN_HEIGHT);
125
        icetSnprintf(filename, FILENAME_MAX, "bad%03d.ppm", rank);
126
        write_ppm(filename, cb, (int)local_width, (int)local_height);
127
      /* Write difference image. */
128 129
        for (y = 0; y < local_height; y++) {
            for (x = 0; x < local_width; x++) {
130 131
                IceTSizeType off_x = x + ref_off_x;
                IceTSizeType off_y = y + ref_off_y;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
                if (CBR(x, y) < REFCBUFR(off_x, off_y)){
                    CBR(x,y) = REFCBUFR(off_x,off_y) - CBR(x,y);
                } else {
                    CBR(x,y) = CBR(x,y) - REFCBUFR(off_x,off_y);
                }
                if (CBG(x, y) < REFCBUFG(off_x, off_y)){
                    CBG(x,y) = REFCBUFG(off_x,off_y) - CBG(x,y);
                } else {
                    CBG(x,y) = CBG(x,y) - REFCBUFG(off_x,off_y);
                }
                if (CBB(x, y) < REFCBUFB(off_x, off_y)){
                    CBB(x,y) = REFCBUFB(off_x,off_y) - CBB(x,y);
                } else {
                    CBB(x,y) = CBB(x,y) - REFCBUFB(off_x,off_y);
                }
            }
        }
149
        icetSnprintf(filename, FILENAME_MAX, "diff%03d.ppm", rank);
150
        write_ppm(filename, cb, (int)local_width, (int)local_height);
151
        return 0;
152 153 154 155 156 157 158 159 160 161 162
    }
#undef CBR
#undef CBG
#undef CBB
#undef CBA
#undef REFCBUFR
#undef REFCBUFG
#undef REFCBUFB
#undef REFCBUFA
#undef CB_EQUALS_REF

163 164 165
    free(refcbuf);
    free(cb);

166
    return 1;
167

168 169
}

170 171 172
static int compare_depth_buffers(IceTSizeType local_width,
                                 IceTSizeType local_height,
                                 IceTImage refimage,
173
                                 IceTImage testimage)
174
{
175 176 177
    IceTSizeType ref_off_x, ref_off_y;
    IceTSizeType bad_pixel_count;
    IceTSizeType x, y;
178
    char filename[FILENAME_MAX];
179 180
    IceTFloat *refdbuf;
    IceTFloat *db;
181
    IceTInt rank;
182

183
    printstat("Checking returned image.\n");
184 185 186

    icetGetIntegerv(ICET_RANK, &rank);

187 188
    refdbuf = icetImageGetDepthf(refimage);
    db = icetImageGetDepthf(testimage);
189 190
    ref_off_x = (rank%g_tile_dim) * local_width;
    ref_off_y = (rank/g_tile_dim) * local_height;
191 192 193
    bad_pixel_count = 0;

    for (y = 0; y < local_height; y++) {
194 195 196
        for (x = 0; x < local_width; x++) {
            if (DIFF(db[y*local_width + x],
                     refdbuf[(y+ref_off_y)*SCREEN_WIDTH
197
                            +x + ref_off_x]) > 0.00001) {
198 199 200 201 202 203 204 205
              /* Uh, oh.  Pixels don't match.  This could be a genuine
               * error or it could be a floating point offset when
               * projecting edge boundries to pixels.  If the latter is the
               * case, there will be very few errors.  Count the errors,
               * and make sure there are not too many.  */
                bad_pixel_count++;
            }
        }
206 207 208 209
    }

  /* Check to make sure there were not too many errors. */
    if (   (bad_pixel_count > 0.001*local_width*local_height)
210 211
        && (bad_pixel_count > local_width)
        && (bad_pixel_count > local_height) )
212
    {
213 214
        IceTUByte *errbuf;

215
      /* Too many errors.  Call it bad. */
216
        printrank("Too many bad pixels!!!!!!\n");
217

218 219
        errbuf = malloc(4*local_width*local_height);

220
      /* Write encoded image. */
221 222
        for (y = 0; y < local_height; y++) {
            for (x = 0; x < local_width; x++) {
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
                IceTFloat ref = refdbuf[(y+ref_off_y)*SCREEN_WIDTH
                                        +x + ref_off_x];
                IceTFloat rendered = db[y*local_width + x];
                IceTUByte *encoded = &errbuf[4*(y*local_width+x)];
                IceTFloat error = rendered - ref;
                if (error < 0) {
                    encoded[0] = 0;
                    encoded[1] = 0;
                    encoded[2] = (IceTUByte)(-error*255);
                } else {
                    encoded[0] = (IceTUByte)(error*255);
                    encoded[1] = 0;
                    encoded[2] = 0;
                }
                encoded[3] = 255;
238 239
            }
        }
240
        icetSnprintf(filename, FILENAME_MAX, "depth_error%03d.ppm", rank);
241
        write_ppm(filename, (IceTUByte *)errbuf,
242
                  (int)local_width, (int)local_height);
243

244 245
        free(errbuf);

246
        return 0;
247 248 249 250 251 252 253 254 255 256 257 258 259
    }
    return 1;
}

static void check_results(int result)
{
    int rank, num_proc;
    int fail = (result != TEST_PASSED);

    icetGetIntegerv(ICET_RANK, &rank);
    icetGetIntegerv(ICET_NUM_PROCESSES, &num_proc);

    if (rank+1 < num_proc) {
260
        int in_fail;
261
        icetCommRecv(&in_fail, 1, ICET_INT, rank+1, 1111);
262
        fail |= in_fail;
263 264
    }
    if (rank-1 >= 0) {
265 266
        icetCommSend(&fail, 1, ICET_INT, rank-1, 1111);
        icetCommRecv(&fail, 1, ICET_INT, rank-1, 2222);
267 268
    }
    if (rank+1 < num_proc) {
269
        icetCommSend(&fail, 1, ICET_INT, rank+1, 2222);
270 271 272
    }

    if (fail) {
273
        finalize_test(TEST_FAILED);
274
        exit(TEST_FAILED);
275 276 277
    }
}

278 279 280 281
static void check_image(IceTImage image,
                        IceTBoolean transparent,
                        IceTSizeType local_width,
                        IceTSizeType local_height)
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
    IceTInt rank;
    int result = TEST_PASSED;

    icetGetIntegerv(ICET_RANK, &rank);

    if (rank < g_tile_dim*g_tile_dim) {
        if (transparent) {
            if (!compare_color_buffers(local_width, local_height,
                                       g_refimage_transparent, image)) {
                result = TEST_FAILED;
            }
        } else {
            if (icetImageGetColorFormat(image) != ICET_IMAGE_COLOR_NONE) {
                if (!compare_color_buffers(local_width, local_height,
                                           g_refimage_opaque, image)) {
                    result = TEST_FAILED;
                }
            }
            if (icetImageGetDepthFormat(image) != ICET_IMAGE_DEPTH_NONE) {
                if (!compare_depth_buffers(local_width, local_height,
                                           g_refimage_opaque, image)) {
                    result = TEST_FAILED;
                }
            }
        }
    } else {
309
        /* printrank("Not a display node.  Not testing image.\n"); */
310 311 312 313
    }
    check_results(result);
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
static void RandomTransformDoRender(IceTBoolean transparent,
                                    IceTSizeType local_width,
                                    IceTSizeType local_height)
{
    IceTImage image;

    if (transparent) {
        glColor4f(0.5f*g_color[0], 0.5f*g_color[1], 0.5f*g_color[2], 0.5f);
    } else {
        glColor4f(g_color[0], g_color[1], g_color[2], 1.0f);
    }
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0f,
            (GLfloat)((2.0*local_width*g_tile_dim)/SCREEN_WIDTH-1.0),
            -1.0f,
            (GLfloat)((2.0*local_height*g_tile_dim)/SCREEN_HEIGHT-1.0),
            -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(g_modelview);

    printstat("Prerendering frame for composite.\n");
    {
        IceTFloat background_color[4];
        IceTVoid *color_buffer = NULL;
        IceTVoid *depth_buffer = NULL;
        IceTEnum color_format;
        IceTEnum depth_format;
        IceTDouble projection_matrix[16];
        IceTDouble modelview_matrix[16];
        IceTInt physical_viewport[4];
345 346 347
        IceTInt global_viewport[4];
        IceTSizeType width;
        IceTSizeType height;
348 349 350 351 352 353

        /* When using draw to render the image directly, we have to change */
        /* a few OpenGL state variables and then restore them. */
        glGetFloatv(GL_COLOR_CLEAR_VALUE, background_color);
        if (transparent) { glClearColor(0.0, 0.0, 0.0, 0.0); }
        glGetIntegerv(GL_VIEWPORT, physical_viewport);
354 355 356
        icetGetIntegerv(ICET_GLOBAL_VIEWPORT, global_viewport);
        width = global_viewport[2]; height = global_viewport[3];
        glViewport(0, 0, width, height);
357 358 359 360 361 362 363 364 365 366 367
        draw();
        glViewport(physical_viewport[0], physical_viewport[1],
                   physical_viewport[2], physical_viewport[3]);
        glClearColor(background_color[0], background_color[1],
                     background_color[2], background_color[3]);
        glReadBuffer(GL_BACK);
        glPixelStorei(GL_PACK_ROW_LENGTH, 0);

        icetGetEnumv(ICET_COLOR_FORMAT, &color_format);
        switch (color_format) {
        case ICET_IMAGE_COLOR_RGBA_UBYTE:
368
            color_buffer = malloc(width*height*4*sizeof(IceTUByte));
369
            glReadPixels(0, 0,
370
                         width, height,
371 372 373 374 375
                         GL_RGBA,
                         GL_UNSIGNED_BYTE,
                         color_buffer);
            break;
        case ICET_IMAGE_COLOR_RGBA_FLOAT:
376
            color_buffer = malloc(width*height*4*sizeof(IceTFloat));
377
            glReadPixels(0, 0,
378
                         width, height,
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
                         GL_RGBA,
                         GL_FLOAT,
                         color_buffer);
            break;
        case ICET_IMAGE_COLOR_NONE:
            /* Do nothing. */
            break;
        default:
            printf("********* Invalid color format 0x%x!\n", color_format);
            finalize_test(TEST_FAILED);
            exit(TEST_FAILED);
        }

        icetGetEnumv(ICET_DEPTH_FORMAT, &depth_format);
        switch (depth_format) {
        case ICET_IMAGE_DEPTH_FLOAT:
395
            depth_buffer = malloc(width*height*sizeof(IceTFloat));
396
            glReadPixels(0, 0,
397
                         width, height,
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
                         GL_DEPTH_COMPONENT,
                         GL_FLOAT,
                         depth_buffer);
            break;
        case ICET_IMAGE_DEPTH_NONE:
            /* Do nothing. */
            break;
        default:
            printf("********* Invalid depth format 0x%x!\n", depth_format);
            finalize_test(TEST_FAILED);
            exit(TEST_FAILED);
        }
        glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix);
        glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix);
        image = icetCompositeImage(color_buffer,
                                   depth_buffer,
                                   NULL,
                                   projection_matrix,
                                   modelview_matrix,
                                   background_color);
        if (color_buffer) { free(color_buffer); }
        if (depth_buffer) { free(depth_buffer); }
        check_image(image, transparent, local_width, local_height);
    }

    printstat("Rendering frame with callbacks.\n");
    image = icetGLDrawFrame();
    swap_buffers();
    check_image(image, transparent, local_width, local_height);
}

429 430 431 432
static void RandomTransformTryInterlace(IceTBoolean transparent,
                                        IceTSizeType local_width,
                                        IceTSizeType local_height)
{
433
    printstat("\nTurning image interlace on.\n");
434 435 436
    icetEnable(ICET_INTERLACE_IMAGES);
    RandomTransformDoRender(transparent, local_width, local_height);

437
    printstat("\nTurning image interlace off.\n");
438 439 440 441
    icetDisable(ICET_INTERLACE_IMAGES);
    RandomTransformDoRender(transparent, local_width, local_height);
}

442 443 444 445 446 447 448 449 450 451 452 453 454
static void RandomTransformTryStrategy()
{
    IceTSizeType local_width = SCREEN_WIDTH/g_tile_dim;
    IceTSizeType local_height = SCREEN_HEIGHT/g_tile_dim;
    IceTSizeType viewport_width = SCREEN_WIDTH;
    IceTSizeType viewport_height = SCREEN_HEIGHT;
    IceTSizeType viewport_offset_x = 0;
    IceTSizeType viewport_offset_y = 0;
    IceTBoolean test_ordering;
    int x, y;

    icetGetBooleanv(ICET_STRATEGY_SUPPORTS_ORDERING, &test_ordering);

455
    printstat("\nRunning on a %d x %d display.\n", g_tile_dim, g_tile_dim);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    icetResetTiles();
    for (y = 0; y < g_tile_dim; y++) {
        for (x = 0; x < g_tile_dim; x++) {
            icetAddTile((IceTInt)(x*local_width),
                        (IceTInt)(y*local_height),
                        local_width,
                        local_height,
                        y*g_tile_dim + x);
        }
    }

    if (g_tile_dim > 1) {
        viewport_width
            = rand()%(SCREEN_WIDTH-local_width) + local_width;
        viewport_height
            = rand()%(SCREEN_HEIGHT-local_height) + local_height;
    }
    if (viewport_width < SCREEN_WIDTH) {
        viewport_offset_x = rand()%(SCREEN_WIDTH-viewport_width);
    }
    if (viewport_width < SCREEN_HEIGHT) {
        viewport_offset_y = rand()%(SCREEN_HEIGHT-viewport_height);
    }
479

480 481 482 483
    glViewport((GLint)viewport_offset_x, (GLint)viewport_offset_y,
               (GLsizei)viewport_width, (GLsizei)viewport_height);
    /* glViewport(0, 0, local_width, local_height); */

484
    printstat("\nDoing color buffer.\n");
485 486 487 488 489 490
    icetSetColorFormat(ICET_IMAGE_COLOR_RGBA_UBYTE);
    icetSetDepthFormat(ICET_IMAGE_DEPTH_FLOAT);
    icetCompositeMode(ICET_COMPOSITE_MODE_Z_BUFFER);
    icetDisable(ICET_COMPOSITE_ONE_BUFFER);
    icetDisable(ICET_ORDERED_COMPOSITE);

491
    RandomTransformTryInterlace(ICET_FALSE, local_width, local_height);
492

493
    printstat("\nDoing float color buffer.\n");
494 495 496 497 498 499
    icetSetColorFormat(ICET_IMAGE_COLOR_RGBA_FLOAT);
    icetSetDepthFormat(ICET_IMAGE_DEPTH_FLOAT);
    icetCompositeMode(ICET_COMPOSITE_MODE_Z_BUFFER);
    icetEnable(ICET_COMPOSITE_ONE_BUFFER);
    icetDisable(ICET_ORDERED_COMPOSITE);

500
    RandomTransformTryInterlace(ICET_FALSE, local_width, local_height);
501

502
    printstat("\nDoing depth buffer.\n");
503 504 505 506 507
    icetSetColorFormat(ICET_IMAGE_COLOR_NONE);
    icetSetDepthFormat(ICET_IMAGE_DEPTH_FLOAT);
    icetCompositeMode(ICET_COMPOSITE_MODE_Z_BUFFER);
    icetDisable(ICET_ORDERED_COMPOSITE);

508
    RandomTransformTryInterlace(ICET_FALSE, local_width, local_height);
509 510

    if (test_ordering) {
511
        printstat("\nDoing blended color buffer.\n");
512 513 514 515 516
        icetSetColorFormat(ICET_IMAGE_COLOR_RGBA_UBYTE);
        icetSetDepthFormat(ICET_IMAGE_DEPTH_NONE);
        icetCompositeMode(ICET_COMPOSITE_MODE_BLEND);
        icetEnable(ICET_ORDERED_COMPOSITE);

517
        RandomTransformTryInterlace(ICET_TRUE, local_width, local_height);
518

519
        printstat("\nDoing blended float color buffer.\n");
520 521 522 523 524
        icetSetColorFormat(ICET_IMAGE_COLOR_RGBA_FLOAT);
        icetSetDepthFormat(ICET_IMAGE_DEPTH_NONE);
        icetCompositeMode(ICET_COMPOSITE_MODE_BLEND);
        icetEnable(ICET_ORDERED_COMPOSITE);

525
        RandomTransformTryInterlace(ICET_TRUE, local_width, local_height);
526
    } else {
527
        printstat("\nStrategy does not support ordering, skipping.\n");
528 529 530
    }
}

531
static int RandomTransformRun()
Andy Cedilnik's avatar
Andy Cedilnik committed
532
{
533
    int i;
534 535 536
    IceTImage image;
    IceTVoid *refbuf;
    IceTVoid *refbuf2;
Andy Cedilnik's avatar
Andy Cedilnik committed
537 538
    int result = TEST_PASSED;
    int rank, num_proc;
539 540
    IceTInt *image_order;
    IceTInt rep_group_size;
541
    const IceTInt *rep_group;
542
    IceTFloat background_color[3];
543
    unsigned int seed;
544
    int strategy_idx;
Andy Cedilnik's avatar
Andy Cedilnik committed
545 546

    icetGetIntegerv(ICET_RANK, &rank);
547
    icetGetIntegerv(ICET_NUM_PROCESSES, &num_proc);
Andy Cedilnik's avatar
Andy Cedilnik committed
548

549
  /* Decide on an image order and data replication group size. */
550
    image_order = malloc(num_proc * sizeof(IceTInt));
551
    if (rank == 0) {
552
        seed = (int)time(NULL);
553
        printstat("Base seed = %u\n", seed);
554
        srand(seed);
555
        for (i = 0; i < num_proc; i++) image_order[i] = i;
556
        printstat("Image order:\n");
557 558 559 560 561
        for (i = 0; i < num_proc; i++) {
            int swap_idx = rand()%(num_proc-i) + i;
            int swap = image_order[swap_idx];
            image_order[swap_idx] = image_order[i];
            image_order[i] = swap;
562
            printstat("%4d", image_order[i]);
563
        }
564
        printstat("\n");
565 566 567 568 569 570
        if (rand()%2) {
          /* No data replication. */
            rep_group_size = 1;
        } else {
            rep_group_size = rand()%num_proc + 1;
        }
571
        printstat("Data replication group sizes: %d\n", rep_group_size);
572
        for (i = 1; i < num_proc; i++) {
573 574 575
            icetCommSend(&seed, 1, ICET_INT, i, 29);
            icetCommSend(image_order, num_proc, ICET_INT, i, 30);
            icetCommSend(&rep_group_size, 1, ICET_INT, i, 31);
576
        }
577
    } else {
578 579 580
        icetCommRecv(&seed, 1, ICET_INT, 0, 29);
        icetCommRecv(image_order, num_proc, ICET_INT, 0, 30);
        icetCommRecv(&rep_group_size, 1, ICET_INT, 0, 31);
581
        srand(seed + rank);
582 583 584
    }
    icetCompositeOrder(image_order);

585 586
  /* Agree on background color. */
    if (rank == 0) {
587 588 589 590
        background_color[0] = (float)rand()/(float)RAND_MAX;
        background_color[1] = (float)rand()/(float)RAND_MAX;
        background_color[2] = (float)rand()/(float)RAND_MAX;
        for (i = 1; i < num_proc; i++) {
591
            icetCommSend(background_color, 3, ICET_FLOAT, i, 32);
592
        }
593
    } else {
594
        icetCommRecv(background_color, 3, ICET_FLOAT, 0, 32);
595 596
    }

Kenneth Moreland's avatar
Kenneth Moreland committed
597
  /* Set up IceT. */
598
    icetGLDrawCallback(draw);
599
    icetBoundingBoxd(-1.0, 1.0, -1.0, 1.0, -0.125, 0.125);
600 601 602 603
    icetEnable(ICET_CORRECT_COLORED_BACKGROUND);

  /* Set up OpenGL. */
    glClearColor(background_color[0], background_color[1],
604
                 background_color[2], 1.0f);
605
    glDisable(GL_LIGHTING);
Andy Cedilnik's avatar
Andy Cedilnik committed
606 607 608 609 610 611 612 613 614

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1, 1, -1, 1, -1, 1);

  /* Get random transformation matrix. */
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(2.0f*rand()/RAND_MAX - 1.0f,
615
                 2.0f*rand()/RAND_MAX - 1.0f,
616
                 ((rand()%19998) - 9999) * 0.0001f);
Andy Cedilnik's avatar
Andy Cedilnik committed
617
    glRotatef(360.0f*rand()/RAND_MAX, 0.0f, 0.0f,
618
              (float)rand()/RAND_MAX);
Andy Cedilnik's avatar
Andy Cedilnik committed
619
    glScalef((float)(1.0/sqrt(num_proc) - 1.0)*(float)rand()/RAND_MAX + 1.0f,
620 621
             (float)(1.0/sqrt(num_proc) - 1.0)*(float)rand()/RAND_MAX + 1.0f,
             1.0f);
622
    glGetFloatv(GL_MODELVIEW_MATRIX, g_modelview);
Andy Cedilnik's avatar
Andy Cedilnik committed
623

624 625 626 627 628
  /* Set up data replication groups and ensure that they all share the same
   * transformation. */
  /* Pick a color based on my index into the object ordering. */
    for (i = 0; image_order[i] != rank; i++);
    i /= rep_group_size;
629
    printstat("My data replication group: %d\n", i);
630 631
    icetDataReplicationGroupColor(i);
    if ((i&0x07) == 0) {
632
        g_color[0] = 0.5f;  g_color[1] = 0.5f;  g_color[2] = 0.5f;
633
    } else {
634 635 636
        g_color[0] = 1.0f*((i&0x01) == 0x01);
        g_color[1] = 1.0f*((i&0x02) == 0x02);
        g_color[2] = 1.0f*((i&0x04) == 0x04);
637 638 639
    }
  /* Get the true group size. */
    icetGetIntegerv(ICET_DATA_REPLICATION_GROUP_SIZE, &rep_group_size);
640
    rep_group = icetUnsafeStateGetInteger(ICET_DATA_REPLICATION_GROUP);
641
    if (rep_group[0] == rank) {
642
        for (i = 1; i < rep_group_size; i++) {
643
            icetCommSend(g_modelview, 16, ICET_FLOAT, rep_group[i], 40);
644
        }
645
    } else {
646
        icetCommRecv(g_modelview, 16, ICET_FLOAT, rep_group[0], 40);
647 648
    }

649 650 651 652 653 654 655 656 657
    printstat("Transformation:\n");
    printstat("    %f %f %f %f\n",
              g_modelview[0], g_modelview[4], g_modelview[8], g_modelview[12]);
    printstat("    %f %f %f %f\n",
              g_modelview[1], g_modelview[5], g_modelview[9], g_modelview[13]);
    printstat("    %f %f %f %f\n",
              g_modelview[2], g_modelview[6], g_modelview[10], g_modelview[14]);
    printstat("    %f %f %f %f\n",
              g_modelview[3], g_modelview[7], g_modelview[11], g_modelview[15]);
Andy Cedilnik's avatar
Andy Cedilnik committed
658 659

  /* Let everyone get a base image for comparison. */
660
    glViewport(0, 0, (GLsizei)SCREEN_WIDTH, (GLsizei)SCREEN_HEIGHT);
661
    icetStrategy(ICET_STRATEGY_SEQUENTIAL);
Andy Cedilnik's avatar
Andy Cedilnik committed
662 663
    icetResetTiles();
    for (i = 0; i < num_proc; i++) {
664
        icetAddTile(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, i);
Andy Cedilnik's avatar
Andy Cedilnik committed
665
    }
666

667
    printstat("\nGetting base images for z compare.\n");
668 669
    icetSetColorFormat(ICET_IMAGE_COLOR_RGBA_UBYTE);
    icetSetDepthFormat(ICET_IMAGE_DEPTH_FLOAT);
670 671
    icetCompositeMode(ICET_COMPOSITE_MODE_Z_BUFFER);
    icetDisable(ICET_COMPOSITE_ONE_BUFFER);
672
    glColor4f(g_color[0], g_color[1], g_color[2], 1.0f);
Andy Cedilnik's avatar
Andy Cedilnik committed
673
    glMatrixMode(GL_MODELVIEW);
674
    glLoadMatrixf(g_modelview);
675
    image = icetGLDrawFrame();
Andy Cedilnik's avatar
Andy Cedilnik committed
676 677
    swap_buffers();

678 679
    refbuf = malloc(icetImageBufferSize(icetImageGetWidth(image),
                                        icetImageGetHeight(image)));
680 681 682 683 684 685 686
    g_refimage_opaque = icetImageAssignBuffer(refbuf, icetImageGetWidth(image),
                                              icetImageGetHeight(image));
    icetImageCopyPixels(image,
                        0,
                        g_refimage_opaque,
                        0,
                        icetImageGetNumPixels(image));
Andy Cedilnik's avatar
Andy Cedilnik committed
687

688
    printstat("Getting base image for color blend.\n");
689 690
    icetSetColorFormat(ICET_IMAGE_COLOR_RGBA_UBYTE);
    icetSetDepthFormat(ICET_IMAGE_DEPTH_NONE);
691
    icetCompositeMode(ICET_COMPOSITE_MODE_BLEND);
692
    icetEnable(ICET_ORDERED_COMPOSITE);
693
    glColor4f(0.5f*g_color[0], 0.5f*g_color[1], 0.5f*g_color[2], 0.5f);
694
    image = icetGLDrawFrame();
695 696
    swap_buffers();

697 698
    refbuf2 = malloc(icetImageBufferSize(icetImageGetWidth(image),
                                         icetImageGetHeight(image)));
699 700 701 702 703 704 705 706
    g_refimage_transparent = icetImageAssignBuffer(refbuf2,
                                                   icetImageGetWidth(image),
                                                   icetImageGetHeight(image));
    icetImageCopyPixels(image,
                        0,
                        g_refimage_transparent,
                        0,
                        icetImageGetNumPixels(image));
707

708
    glViewport(0, 0, (GLsizei)SCREEN_WIDTH, (GLsizei)SCREEN_HEIGHT);
Andy Cedilnik's avatar
Andy Cedilnik committed
709

710 711 712 713 714 715
    for (strategy_idx = 0; strategy_idx < STRATEGY_LIST_SIZE; strategy_idx++) {
        IceTEnum strategy = strategy_list[strategy_idx];
        int si_strategy_idx;
        int num_single_image_strategy;

        icetStrategy(strategy);
716
        printstat("\n\nUsing %s strategy.\n", icetGetStrategyName());
717

718 719 720 721 722 723 724 725 726 727 728 729 730 731
        if (strategy_uses_single_image_strategy(strategy)) {
            num_single_image_strategy = SINGLE_IMAGE_STRATEGY_LIST_SIZE;
        } else {
          /* Set to one since single image strategy does not matter. */
            num_single_image_strategy = 1;
        }

        for (si_strategy_idx = 0;
             si_strategy_idx < num_single_image_strategy;
             si_strategy_idx++) {
            IceTEnum single_image_strategy
                = single_image_strategy_list[si_strategy_idx];

            icetSingleImageStrategy(single_image_strategy);
732
            printstat("\nUsing %s single image sub-strategy.\n",
733 734 735 736 737 738 739
                   icetGetSingleImageStrategyName());

            for (g_tile_dim = 1;
                 g_tile_dim*g_tile_dim <= num_proc;
                 g_tile_dim++) {
                RandomTransformTryStrategy();
            }
740
        }
Andy Cedilnik's avatar
Andy Cedilnik committed
741 742
    }

743
    printstat("Cleaning up.\n");
744
    free(image_order);
745 746
    free(refbuf);
    free(refbuf2);
747
    glViewport(0, 0, (GLsizei)SCREEN_WIDTH, (GLsizei)SCREEN_HEIGHT);
Andy Cedilnik's avatar
Andy Cedilnik committed
748 749 750

    return result;
}
751 752 753 754 755 756 757 758 759

int RandomTransform(int argc, char *argv[])
{
    /* To remove warning */
    (void)argc;
    (void)argv;

    return run_test(RandomTransformRun);
}