See header for documentation. */
| 724 | |
| 725 | /* See header for documentation. */ |
| 726 | astcenc_error astcenc_context_alloc( |
| 727 | const astcenc_config* configp, |
| 728 | unsigned int thread_count, |
| 729 | astcenc_context** context, |
| 730 | const astcenc_context* parent_context |
| 731 | ) { |
| 732 | astcenc_error status; |
| 733 | |
| 734 | status = validate_cpu_float(); |
| 735 | if (status != ASTCENC_SUCCESS) |
| 736 | { |
| 737 | return status; |
| 738 | } |
| 739 | |
| 740 | if (thread_count == 0) |
| 741 | { |
| 742 | return ASTCENC_ERR_BAD_PARAM; |
| 743 | } |
| 744 | |
| 745 | #if defined(ASTCENC_DIAGNOSTICS) |
| 746 | // Force single threaded compressor use in diagnostic mode |
| 747 | if (thread_count != 1) |
| 748 | { |
| 749 | return ASTCENC_ERR_BAD_PARAM; |
| 750 | } |
| 751 | #endif |
| 752 | |
| 753 | // Exactly one of config or parent_context must be set |
| 754 | bool has_config = configp != nullptr; |
| 755 | bool has_parent = parent_context != nullptr; |
| 756 | if (!(has_config ^ has_parent)) |
| 757 | { |
| 758 | return ASTCENC_ERR_BAD_PARAM; |
| 759 | } |
| 760 | |
| 761 | if (has_parent) |
| 762 | { |
| 763 | configp = &parent_context->context.config; |
| 764 | } |
| 765 | |
| 766 | const astcenc_config& config = *configp; |
| 767 | astcenc_context* ctxo = new astcenc_context; |
| 768 | astcenc_contexti* ctx = &ctxo->context; |
| 769 | ctx->thread_count = thread_count; |
| 770 | ctx->config = *configp; |
| 771 | ctx->working_buffers = nullptr; |
| 772 | |
| 773 | // These are allocated per-compress, as they depend on image size |
| 774 | ctx->input_alpha_averages = nullptr; |
| 775 | |
| 776 | // Copy the config first and validate the copy (we may modify it) |
| 777 | status = validate_config(ctx->config); |
| 778 | if (status != ASTCENC_SUCCESS) |
| 779 | { |
| 780 | delete ctxo; |
| 781 | return status; |
| 782 | } |
| 783 |