mojoshader.c
branchtrunk
changeset 109 48e95cf41505
parent 108 b1b685bc63cb
child 110 c9e3d24f2e5a
equal deleted inserted replaced
108:b1b685bc63cb 109:48e95cf41505
    36 // If SUPPORT_PROFILE_* isn't defined, we assume an implicit desire to support.
    36 // If SUPPORT_PROFILE_* isn't defined, we assume an implicit desire to support.
    37 //  You get all the profiles unless you go out of your way to disable them.
    37 //  You get all the profiles unless you go out of your way to disable them.
    38 
    38 
    39 #ifndef SUPPORT_PROFILE_D3D
    39 #ifndef SUPPORT_PROFILE_D3D
    40 #define SUPPORT_PROFILE_D3D 1
    40 #define SUPPORT_PROFILE_D3D 1
       
    41 #endif
       
    42 
       
    43 #ifndef SUPPORT_PROFILE_PASSTHROUGH
       
    44 #define SUPPORT_PROFILE_PASSTHROUGH 1
    41 #endif
    45 #endif
    42 
    46 
    43 #ifndef SUPPORT_PROFILE_GLSL
    47 #ifndef SUPPORT_PROFILE_GLSL
    44 #define SUPPORT_PROFILE_GLSL 1
    48 #define SUPPORT_PROFILE_GLSL 1
    45 #endif
    49 #endif
   298     OutputList helpers;
   302     OutputList helpers;
   299     OutputList subroutines;
   303     OutputList subroutines;
   300     OutputList mainline;
   304     OutputList mainline;
   301     OutputList ignore;
   305     OutputList ignore;
   302     OutputList *output_stack[2];
   306     OutputList *output_stack[2];
       
   307     uint8 *output_bytes;  // can be used instead of the OutputLists.
   303     int indent_stack[2];
   308     int indent_stack[2];
   304     int output_stack_len;
   309     int output_stack_len;
   305     int output_len; // total strlen; prevents walking the lists just to malloc.
   310     int output_len; // total strlen; prevents walking the lists just to malloc.
   306     int indent;
   311     int indent;
   307     const char *endline;
   312     const char *endline;
  1369 #undef EMIT_D3D_OPCODE_DSSSS_FUNC
  1374 #undef EMIT_D3D_OPCODE_DSSSS_FUNC
  1370 
  1375 
  1371 #endif  // SUPPORT_PROFILE_D3D
  1376 #endif  // SUPPORT_PROFILE_D3D
  1372 
  1377 
  1373 
  1378 
       
  1379 #if !SUPPORT_PROFILE_PASSTHROUGH
       
  1380 #define PROFILE_EMITTER_PASSTHROUGH(op)
       
  1381 #else
       
  1382 #undef AT_LEAST_ONE_PROFILE
       
  1383 #define AT_LEAST_ONE_PROFILE 1
       
  1384 #define PROFILE_EMITTER_PASSTHROUGH(op) emit_PASSTHROUGH_##op,
       
  1385 
       
  1386 static void emit_PASSTHROUGH_start(Context *ctx)
       
  1387 {
       
  1388     // just copy the whole token stream and make all other emitters no-ops.
       
  1389     ctx->output_len = (ctx->tokencount * sizeof (uint32));
       
  1390     ctx->output_bytes = Malloc(ctx, ctx->output_len);
       
  1391     if (ctx->output_bytes == NULL)
       
  1392         out_of_memory(ctx);
       
  1393     else
       
  1394         memcpy(ctx->output_bytes, ctx->tokens, ctx->output_len);
       
  1395 } // emit_PASSTHROUGH_start
       
  1396 
       
  1397 static void emit_PASSTHROUGH_RESERVED(Context *ctx) {}
       
  1398 static void emit_PASSTHROUGH_NOP(Context *ctx) {}
       
  1399 static void emit_PASSTHROUGH_MOV(Context *ctx) {}
       
  1400 static void emit_PASSTHROUGH_ADD(Context *ctx) {}
       
  1401 static void emit_PASSTHROUGH_SUB(Context *ctx) {}
       
  1402 static void emit_PASSTHROUGH_MAD(Context *ctx) {}
       
  1403 static void emit_PASSTHROUGH_MUL(Context *ctx) {}
       
  1404 static void emit_PASSTHROUGH_RCP(Context *ctx) {}
       
  1405 static void emit_PASSTHROUGH_RSQ(Context *ctx) {}
       
  1406 static void emit_PASSTHROUGH_DP3(Context *ctx) {}
       
  1407 static void emit_PASSTHROUGH_DP4(Context *ctx) {}
       
  1408 static void emit_PASSTHROUGH_MIN(Context *ctx) {}
       
  1409 static void emit_PASSTHROUGH_MAX(Context *ctx) {}
       
  1410 static void emit_PASSTHROUGH_SLT(Context *ctx) {}
       
  1411 static void emit_PASSTHROUGH_SGE(Context *ctx) {}
       
  1412 static void emit_PASSTHROUGH_EXP(Context *ctx) {}
       
  1413 static void emit_PASSTHROUGH_LOG(Context *ctx) {}
       
  1414 static void emit_PASSTHROUGH_LIT(Context *ctx) {}
       
  1415 static void emit_PASSTHROUGH_DST(Context *ctx) {}
       
  1416 static void emit_PASSTHROUGH_LRP(Context *ctx) {}
       
  1417 static void emit_PASSTHROUGH_FRC(Context *ctx) {}
       
  1418 static void emit_PASSTHROUGH_M4X4(Context *ctx) {}
       
  1419 static void emit_PASSTHROUGH_M4X3(Context *ctx) {}
       
  1420 static void emit_PASSTHROUGH_M3X4(Context *ctx) {}
       
  1421 static void emit_PASSTHROUGH_M3X3(Context *ctx) {}
       
  1422 static void emit_PASSTHROUGH_M3X2(Context *ctx) {}
       
  1423 static void emit_PASSTHROUGH_CALL(Context *ctx) {}
       
  1424 static void emit_PASSTHROUGH_CALLNZ(Context *ctx) {}
       
  1425 static void emit_PASSTHROUGH_LOOP(Context *ctx) {}
       
  1426 static void emit_PASSTHROUGH_RET(Context *ctx) {}
       
  1427 static void emit_PASSTHROUGH_ENDLOOP(Context *ctx) {}
       
  1428 static void emit_PASSTHROUGH_LABEL(Context *ctx) {}
       
  1429 static void emit_PASSTHROUGH_POW(Context *ctx) {}
       
  1430 static void emit_PASSTHROUGH_CRS(Context *ctx) {}
       
  1431 static void emit_PASSTHROUGH_SGN(Context *ctx) {}
       
  1432 static void emit_PASSTHROUGH_ABS(Context *ctx) {}
       
  1433 static void emit_PASSTHROUGH_NRM(Context *ctx) {}
       
  1434 static void emit_PASSTHROUGH_SINCOS(Context *ctx) {}
       
  1435 static void emit_PASSTHROUGH_REP(Context *ctx) {}
       
  1436 static void emit_PASSTHROUGH_ENDREP(Context *ctx) {}
       
  1437 static void emit_PASSTHROUGH_IF(Context *ctx) {}
       
  1438 static void emit_PASSTHROUGH_ELSE(Context *ctx) {}
       
  1439 static void emit_PASSTHROUGH_ENDIF(Context *ctx) {}
       
  1440 static void emit_PASSTHROUGH_BREAK(Context *ctx) {}
       
  1441 static void emit_PASSTHROUGH_MOVA(Context *ctx) {}
       
  1442 static void emit_PASSTHROUGH_TEXKILL(Context *ctx) {}
       
  1443 static void emit_PASSTHROUGH_TEXBEM(Context *ctx) {}
       
  1444 static void emit_PASSTHROUGH_TEXBEML(Context *ctx) {}
       
  1445 static void emit_PASSTHROUGH_TEXREG2AR(Context *ctx) {}
       
  1446 static void emit_PASSTHROUGH_TEXREG2GB(Context *ctx) {}
       
  1447 static void emit_PASSTHROUGH_TEXM3X2PAD(Context *ctx) {}
       
  1448 static void emit_PASSTHROUGH_TEXM3X2TEX(Context *ctx) {}
       
  1449 static void emit_PASSTHROUGH_TEXM3X3PAD(Context *ctx) {}
       
  1450 static void emit_PASSTHROUGH_TEXM3X3TEX(Context *ctx) {}
       
  1451 static void emit_PASSTHROUGH_TEXM3X3SPEC(Context *ctx) {}
       
  1452 static void emit_PASSTHROUGH_TEXM3X3VSPEC(Context *ctx) {}
       
  1453 static void emit_PASSTHROUGH_EXPP(Context *ctx) {}
       
  1454 static void emit_PASSTHROUGH_LOGP(Context *ctx) {}
       
  1455 static void emit_PASSTHROUGH_CND(Context *ctx) {}
       
  1456 static void emit_PASSTHROUGH_TEXREG2RGB(Context *ctx) {}
       
  1457 static void emit_PASSTHROUGH_TEXDP3TEX(Context *ctx) {}
       
  1458 static void emit_PASSTHROUGH_TEXM3X2DEPTH(Context *ctx) {}
       
  1459 static void emit_PASSTHROUGH_TEXDP3(Context *ctx) {}
       
  1460 static void emit_PASSTHROUGH_TEXM3X3(Context *ctx) {}
       
  1461 static void emit_PASSTHROUGH_TEXDEPTH(Context *ctx) {}
       
  1462 static void emit_PASSTHROUGH_CMP(Context *ctx) {}
       
  1463 static void emit_PASSTHROUGH_BEM(Context *ctx) {}
       
  1464 static void emit_PASSTHROUGH_DP2ADD(Context *ctx) {}
       
  1465 static void emit_PASSTHROUGH_DSX(Context *ctx) {}
       
  1466 static void emit_PASSTHROUGH_DSY(Context *ctx) {}
       
  1467 static void emit_PASSTHROUGH_TEXLDD(Context *ctx) {}
       
  1468 static void emit_PASSTHROUGH_TEXLDL(Context *ctx) {}
       
  1469 static void emit_PASSTHROUGH_BREAKP(Context *ctx) {}
       
  1470 static void emit_PASSTHROUGH_BREAKC(Context *ctx) {}
       
  1471 static void emit_PASSTHROUGH_IFC(Context *ctx) {}
       
  1472 static void emit_PASSTHROUGH_SETP(Context *ctx) {}
       
  1473 static void emit_PASSTHROUGH_DEF(Context *ctx) {}
       
  1474 static void emit_PASSTHROUGH_DEFI(Context *ctx) {}
       
  1475 static void emit_PASSTHROUGH_DEFB(Context *ctx) {}
       
  1476 static void emit_PASSTHROUGH_DCL(Context *ctx) {}
       
  1477 static void emit_PASSTHROUGH_TEXCOORD(Context *ctx) {}
       
  1478 static void emit_PASSTHROUGH_TEX(Context *ctx) {}
       
  1479 static void emit_PASSTHROUGH_end(Context *ctx) {}
       
  1480 static void emit_PASSTHROUGH_finalize(Context *ctx) {}
       
  1481 static void emit_PASSTHROUGH_global(Context *ctx, RegisterType t, int n) {}
       
  1482 static void emit_PASSTHROUGH_uniform(Context *ctx, RegisterType t, int n) {}
       
  1483 static void emit_PASSTHROUGH_comment(Context *ctx, const char *str) {}
       
  1484 static void emit_PASSTHROUGH_attribute(Context *ctx, RegisterType t, int n,
       
  1485                                        MOJOSHADER_usage u, int i, int w) {}
       
  1486 #endif  // SUPPORT_PROFILE_PASSTHROUGH
       
  1487 
  1374 
  1488 
  1375 #if !SUPPORT_PROFILE_GLSL
  1489 #if !SUPPORT_PROFILE_GLSL
  1376 #define PROFILE_EMITTER_GLSL(op)
  1490 #define PROFILE_EMITTER_GLSL(op)
  1377 #else
  1491 #else
  1378 #undef AT_LEAST_ONE_PROFILE
  1492 #undef AT_LEAST_ONE_PROFILE
  2566 static const Profile profiles[] =
  2680 static const Profile profiles[] =
  2567 {
  2681 {
  2568 #if SUPPORT_PROFILE_D3D
  2682 #if SUPPORT_PROFILE_D3D
  2569     DEFINE_PROFILE(D3D)
  2683     DEFINE_PROFILE(D3D)
  2570 #endif
  2684 #endif
       
  2685 #if SUPPORT_PROFILE_PASSTHROUGH
       
  2686     DEFINE_PROFILE(PASSTHROUGH)
       
  2687 #endif
  2571 #if SUPPORT_PROFILE_GLSL
  2688 #if SUPPORT_PROFILE_GLSL
  2572     DEFINE_PROFILE(GLSL)
  2689     DEFINE_PROFILE(GLSL)
  2573 #endif
  2690 #endif
  2574 };
  2691 };
  2575 
  2692 
  2576 #undef DEFINE_PROFILE
  2693 #undef DEFINE_PROFILE
  2577 
  2694 
  2578 // The PROFILE_EMITTER_* items MUST be in the same order as profiles[]!
  2695 // The PROFILE_EMITTER_* items MUST be in the same order as profiles[]!
  2579 #define PROFILE_EMITTERS(op) { \
  2696 #define PROFILE_EMITTERS(op) { \
  2580      PROFILE_EMITTER_D3D(op) \
  2697      PROFILE_EMITTER_D3D(op) \
       
  2698      PROFILE_EMITTER_PASSTHROUGH(op) \
  2581      PROFILE_EMITTER_GLSL(op) \
  2699      PROFILE_EMITTER_GLSL(op) \
  2582 }
  2700 }
  2583 
  2701 
  2584 static int parse_destination_token(Context *ctx, DestArgInfo *info)
  2702 static int parse_destination_token(Context *ctx, DestArgInfo *info)
  2585 {
  2703 {
  3641 {
  3759 {
  3642     if (ctx != NULL)
  3760     if (ctx != NULL)
  3643     {
  3761     {
  3644         MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : internal_free);
  3762         MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : internal_free);
  3645         void *d = ctx->malloc_data;
  3763         void *d = ctx->malloc_data;
       
  3764         if (ctx->output_bytes != NULL)
       
  3765             f(d, ctx->output_bytes);
  3646         free_output_list(f, d, ctx->globals.head.next);
  3766         free_output_list(f, d, ctx->globals.head.next);
  3647         free_output_list(f, d, ctx->helpers.head.next);
  3767         free_output_list(f, d, ctx->helpers.head.next);
  3648         free_output_list(f, d, ctx->subroutines.head.next);
  3768         free_output_list(f, d, ctx->subroutines.head.next);
  3649         free_output_list(f, d, ctx->mainline.head.next);
  3769         free_output_list(f, d, ctx->mainline.head.next);
  3650         free_output_list(f, d, ctx->ignore.head.next);
  3770         free_output_list(f, d, ctx->ignore.head.next);
  3677 } // append_list
  3797 } // append_list
  3678 
  3798 
  3679 
  3799 
  3680 static char *build_output(Context *ctx)
  3800 static char *build_output(Context *ctx)
  3681 {
  3801 {
  3682     char *retval = (char *) Malloc(ctx, ctx->output_len + 1);
  3802     // add a byte for the null terminator if we're doing text output.
       
  3803     const int plusbytes = (ctx->output_bytes == NULL) ? 1 : 0;
       
  3804     char *retval = (char *) Malloc(ctx, ctx->output_len + plusbytes);
  3683     if (retval == NULL)
  3805     if (retval == NULL)
  3684         out_of_memory(ctx);
  3806         out_of_memory(ctx);
  3685     else
  3807     else
  3686     {
  3808     {
  3687         const char *endl = ctx->endline;
  3809         const char *endl = ctx->endline;
  3688         const size_t endllen = ctx->endline_len;
  3810         const size_t endllen = ctx->endline_len;
  3689         char *wptr = retval;
  3811         char *wptr = retval;
  3690         append_list(&wptr, endl, endllen, ctx->globals.head.next);
  3812         if (ctx->output_bytes != NULL)
  3691         append_list(&wptr, endl, endllen, ctx->helpers.head.next);
  3813             memcpy(retval, ctx->output_bytes, ctx->output_len);
  3692         append_list(&wptr, endl, endllen, ctx->subroutines.head.next);
  3814         else
  3693         append_list(&wptr, endl, endllen, ctx->mainline.head.next);
  3815         {
  3694         // don't append ctx->ignore ... that's why it's called "ignore"
  3816             append_list(&wptr, endl, endllen, ctx->globals.head.next);
       
  3817             append_list(&wptr, endl, endllen, ctx->helpers.head.next);
       
  3818             append_list(&wptr, endl, endllen, ctx->subroutines.head.next);
       
  3819             append_list(&wptr, endl, endllen, ctx->mainline.head.next);
       
  3820             // don't append ctx->ignore ... that's why it's called "ignore"
       
  3821         } // else
  3695     } // else
  3822     } // else
  3696 
  3823 
  3697     return retval;
  3824     return retval;
  3698 } // build_output
  3825 } // build_output
  3699 
  3826