| 1505 | } |
| 1506 | |
| 1507 | id emit_load(const expression &exp, bool force_new_id) override |
| 1508 | { |
| 1509 | if (exp.is_constant) |
| 1510 | return emit_constant(exp.type, exp.constant); |
| 1511 | else if (exp.chain.empty() && !force_new_id) // Can refer to values without access chain directly |
| 1512 | return exp.base; |
| 1513 | |
| 1514 | const id res = make_id(); |
| 1515 | |
| 1516 | std::string type, expr_code = id_to_name(exp.base); |
| 1517 | |
| 1518 | for (const expression::operation &op : exp.chain) |
| 1519 | { |
| 1520 | switch (op.op) |
| 1521 | { |
| 1522 | case expression::operation::op_cast: |
| 1523 | type.clear(); |
| 1524 | write_type<false, false>(type, op.to); |
| 1525 | expr_code = type + '(' + expr_code + ')'; |
| 1526 | break; |
| 1527 | case expression::operation::op_member: |
| 1528 | expr_code += '.'; |
| 1529 | expr_code += escape_name(get_struct(op.from.struct_definition).member_list[op.index].name); |
| 1530 | break; |
| 1531 | case expression::operation::op_dynamic_index: |
| 1532 | // For matrices this will extract a column, but that is fine, since they are initialized column-wise too |
| 1533 | // Also cast to an integer, since it could be a boolean too, but GLSL does not allow those in index expressions |
| 1534 | expr_code += "[int(" + id_to_name(op.index) + ")]"; |
| 1535 | break; |
| 1536 | case expression::operation::op_constant_index: |
| 1537 | if (op.from.is_vector() && !op.from.is_array()) |
| 1538 | expr_code += '.', |
| 1539 | expr_code += "xyzw"[op.index]; |
| 1540 | else |
| 1541 | expr_code += '[' + std::to_string(op.index) + ']'; |
| 1542 | break; |
| 1543 | case expression::operation::op_swizzle: |
| 1544 | expr_code += '.'; |
| 1545 | for (int i = 0; i < 4 && op.swizzle[i] >= 0; ++i) |
| 1546 | expr_code += "xyzw"[op.swizzle[i]]; |
| 1547 | break; |
| 1548 | case expression::operation::op_matrix_swizzle: |
| 1549 | if (op.swizzle[1] < 0) |
| 1550 | { |
| 1551 | const char row = (op.swizzle[0] % 4); |
| 1552 | const char col = (op.swizzle[0] - row) / 4; |
| 1553 | |
| 1554 | expr_code += '['; |
| 1555 | expr_code += to_digit(row); |
| 1556 | expr_code += "]["; |
| 1557 | expr_code += to_digit(col); |
| 1558 | expr_code += ']'; |
| 1559 | } |
| 1560 | else |
| 1561 | { |
| 1562 | // TODO: Implement matrix to vector swizzles |
| 1563 | assert(false); |
| 1564 | expr_code += "_NOT_IMPLEMENTED_"; // Make sure compilation fails |
no test coverage detected