|
551 | 551 | } |
552 | 552 |
|
553 | 553 | void |
554 | | -pl_graphics_copy_buffer_to_texture(plBlitEncoder* ptEncoder, plBufferHandle tBufferHandle, plTextureHandle tTextureHandle, uint32_t uRegionCount, const plBufferImageCopy* ptRegions) |
| 554 | +pl_graphics_copy_buffer_to_texture( |
| 555 | + plBlitEncoder* ptEncoder, |
| 556 | + plBufferHandle tBufferHandle, |
| 557 | + plTextureHandle tTextureHandle, |
| 558 | + uint32_t uRegionCount, |
| 559 | + const plBufferImageCopy* ptRegions |
| 560 | +) |
555 | 561 | { |
556 | 562 | plCommandBuffer* ptCmdBuffer = ptEncoder->ptCommandBuffer; |
557 | | - |
558 | | - // if(!ptEncoder->bActive) |
559 | | - // { |
560 | | - // ptEncoder->tEncoder = [ptCmdBuffer->tCmdBuffer4 computeCommandEncoder]; |
561 | | - // ptEncoder->bActive = true; |
562 | | - // } |
563 | | - |
564 | 563 | plDevice* ptDevice = ptCmdBuffer->ptDevice; |
565 | 564 |
|
566 | | - plMetalBuffer* ptBuffer = &ptDevice->sbtBuffersHot[tBufferHandle.uIndex]; |
567 | | - plMetalTexture* ptTexture = &ptDevice->sbtTexturesHot[tTextureHandle.uIndex]; |
568 | | - plTexture* ptColdTexture = pl_graphics_get_texture(ptDevice, tTextureHandle); |
569 | | - plBuffer* ptColdBuffer = pl_graphics_get_buffer(ptDevice, tBufferHandle); |
| 565 | + plMetalBuffer* ptBuffer = &ptDevice->sbtBuffersHot[tBufferHandle.uIndex]; |
| 566 | + plMetalTexture* ptTexture = &ptDevice->sbtTexturesHot[tTextureHandle.uIndex]; |
| 567 | + plTexture* ptColdTexture = pl_graphics_get_texture(ptDevice, tTextureHandle); |
| 568 | + |
| 569 | + const uint32_t uFormatStride = pl__format_stride(ptColdTexture->tDesc.tFormat); |
570 | 570 |
|
571 | 571 | for(uint32_t i = 0; i < uRegionCount; i++) |
572 | 572 | { |
| 573 | + const plBufferImageCopy* ptRegion = &ptRegions[i]; |
| 574 | + |
| 575 | + MTLOrigin tOrigin = { |
| 576 | + .x = ptRegion->iImageOffsetX, |
| 577 | + .y = ptRegion->iImageOffsetY, |
| 578 | + .z = ptRegion->iImageOffsetZ |
| 579 | + }; |
| 580 | + |
| 581 | + MTLSize tSize = { |
| 582 | + .width = ptRegion->uImageWidth, |
| 583 | + .height = ptRegion->uImageHeight, |
| 584 | + .depth = ptRegion->uImageDepth |
| 585 | + }; |
| 586 | + |
| 587 | + const NSUInteger uBytesPerRow = |
| 588 | + ptRegion->uBufferRowLength ? |
| 589 | + ptRegion->uBufferRowLength * uFormatStride : |
| 590 | + tSize.width * uFormatStride; |
| 591 | + |
| 592 | + const NSUInteger uBytesPerImage = |
| 593 | + ptRegion->uBufferImageHeight ? |
| 594 | + uBytesPerRow * ptRegion->uBufferImageHeight : |
| 595 | + uBytesPerRow * tSize.height; |
573 | 596 |
|
574 | | - MTLOrigin tOrigin; |
575 | | - tOrigin.x = ptRegions[i].iImageOffsetX; |
576 | | - tOrigin.y = ptRegions[i].iImageOffsetY; |
577 | | - tOrigin.z = ptRegions[i].iImageOffsetZ; |
578 | | - |
579 | | - MTLSize tSize; |
580 | | - tSize.width = ptRegions[i].uImageWidth; |
581 | | - tSize.height = ptRegions[i].uImageHeight; |
582 | | - tSize.depth = ptRegions[i].uImageDepth; |
583 | | - |
584 | | - NSUInteger uBytesPerRow = tSize.width * pl__format_stride(ptColdTexture->tDesc.tFormat); |
585 | | - [ptEncoder->tEncoder copyFromBuffer:ptBuffer->tBuffer |
586 | | - sourceOffset:ptRegions[i].szBufferOffset |
587 | | - sourceBytesPerRow:uBytesPerRow |
588 | | - sourceBytesPerImage:0 |
589 | | - sourceSize:tSize |
590 | | - toTexture:ptTexture->tTexture |
591 | | - destinationSlice:ptRegions[i].uBaseArrayLayer |
592 | | - destinationLevel:ptRegions[i].uMipLevel |
593 | | - destinationOrigin:tOrigin]; |
| 597 | + const uint32_t uLayerCount = |
| 598 | + ptRegion->uLayerCount ? ptRegion->uLayerCount : 1; |
| 599 | + |
| 600 | + for(uint32_t uLayer = 0; uLayer < uLayerCount; uLayer++) |
| 601 | + { |
| 602 | + const NSUInteger uSourceOffset = |
| 603 | + ptRegion->szBufferOffset + uLayer * uBytesPerImage; |
| 604 | + |
| 605 | + const NSUInteger uDestinationSlice = |
| 606 | + ptRegion->uBaseArrayLayer + uLayer; |
| 607 | + |
| 608 | + [ptEncoder->tEncoder copyFromBuffer:ptBuffer->tBuffer |
| 609 | + sourceOffset:uSourceOffset |
| 610 | + sourceBytesPerRow:uBytesPerRow |
| 611 | + sourceBytesPerImage:uBytesPerImage |
| 612 | + sourceSize:tSize |
| 613 | + toTexture:ptTexture->tTexture |
| 614 | + destinationSlice:uDestinationSlice |
| 615 | + destinationLevel:ptRegion->uMipLevel |
| 616 | + destinationOrigin:tOrigin]; |
| 617 | + } |
594 | 618 |
|
595 | 619 | pl__metal_barrier_in_encoder(ptEncoder->tEncoder); |
596 | 620 | } |
|
644 | 668 | } |
645 | 669 |
|
646 | 670 | void |
647 | | -pl_graphics_copy_texture_to_buffer(plBlitEncoder* ptEncoder, plTextureHandle tTextureHandle, plBufferHandle tBufferHandle, uint32_t uRegionCount, const plBufferImageCopy* ptRegions) |
| 671 | +pl_graphics_copy_texture_to_buffer( |
| 672 | + plBlitEncoder* ptEncoder, |
| 673 | + plTextureHandle tTextureHandle, |
| 674 | + plBufferHandle tBufferHandle, |
| 675 | + uint32_t uRegionCount, |
| 676 | + const plBufferImageCopy* ptRegions |
| 677 | +) |
648 | 678 | { |
649 | 679 | plCommandBuffer* ptCmdBuffer = ptEncoder->ptCommandBuffer; |
650 | | - |
651 | | - // if(!ptEncoder->bActive) |
652 | | - // { |
653 | | - // ptEncoder->tEncoder = [ptCmdBuffer->tCmdBuffer4 computeCommandEncoder]; |
654 | | - // ptEncoder->bActive = true; |
655 | | - // } |
656 | | - |
657 | 680 | plDevice* ptDevice = ptCmdBuffer->ptDevice; |
| 681 | + |
658 | 682 | const plTexture* ptTexture = pl_graphics_get_texture(ptDevice, tTextureHandle); |
659 | 683 | const plMetalTexture* ptMetalTexture = &ptDevice->sbtTexturesHot[tTextureHandle.uIndex]; |
660 | 684 | const plMetalBuffer* ptMetalBuffer = &ptDevice->sbtBuffersHot[tBufferHandle.uIndex]; |
661 | 685 |
|
| 686 | + const uint32_t uFormatStride = pl__format_stride(ptTexture->tDesc.tFormat); |
| 687 | + |
662 | 688 | for(uint32_t i = 0; i < uRegionCount; i++) |
663 | 689 | { |
| 690 | + const plBufferImageCopy* ptRegion = &ptRegions[i]; |
664 | 691 |
|
665 | | - MTLOrigin tOrigin; |
666 | | - tOrigin.x = ptRegions[i].iImageOffsetX; |
667 | | - tOrigin.y = ptRegions[i].iImageOffsetY; |
668 | | - tOrigin.z = ptRegions[i].iImageOffsetZ; |
669 | | - |
670 | | - MTLSize tSize; |
671 | | - tSize.width = ptRegions[i].uImageWidth; |
672 | | - tSize.height = ptRegions[i].uImageHeight; |
673 | | - tSize.depth = ptRegions[i].uImageDepth; |
674 | | - |
675 | | - const uint32_t uFormatStride = pl__format_stride(ptTexture->tDesc.tFormat); |
676 | | - |
677 | | - [ptEncoder->tEncoder copyFromTexture:ptMetalTexture->tTexture |
678 | | - sourceSlice:ptRegions[i].uBaseArrayLayer |
679 | | - sourceLevel:ptRegions[i].uMipLevel |
680 | | - sourceOrigin:tOrigin |
681 | | - sourceSize:tSize |
682 | | - toBuffer:ptMetalBuffer->tBuffer |
683 | | - destinationOffset:ptRegions[i].szBufferOffset |
684 | | - destinationBytesPerRow:ptTexture->tDesc.tDimensions.x * uFormatStride |
685 | | - destinationBytesPerImage:0]; |
| 692 | + MTLOrigin tOrigin = { |
| 693 | + .x = ptRegion->iImageOffsetX, |
| 694 | + .y = ptRegion->iImageOffsetY, |
| 695 | + .z = ptRegion->iImageOffsetZ |
| 696 | + }; |
| 697 | + |
| 698 | + MTLSize tSize = { |
| 699 | + .width = ptRegion->uImageWidth, |
| 700 | + .height = ptRegion->uImageHeight, |
| 701 | + .depth = ptRegion->uImageDepth |
| 702 | + }; |
| 703 | + |
| 704 | + const NSUInteger uBytesPerRow = |
| 705 | + ptRegion->uBufferRowLength ? |
| 706 | + ptRegion->uBufferRowLength * uFormatStride : |
| 707 | + tSize.width * uFormatStride; |
| 708 | + |
| 709 | + const NSUInteger uBytesPerImage = |
| 710 | + ptRegion->uBufferImageHeight ? |
| 711 | + uBytesPerRow * ptRegion->uBufferImageHeight : |
| 712 | + uBytesPerRow * tSize.height; |
| 713 | + |
| 714 | + const uint32_t uLayerCount = |
| 715 | + ptRegion->uLayerCount ? ptRegion->uLayerCount : 1; |
| 716 | + |
| 717 | + for(uint32_t uLayer = 0; uLayer < uLayerCount; uLayer++) |
| 718 | + { |
| 719 | + const NSUInteger uDestinationOffset = |
| 720 | + ptRegion->szBufferOffset + uLayer * uBytesPerImage; |
| 721 | + |
| 722 | + const NSUInteger uSourceSlice = |
| 723 | + ptRegion->uBaseArrayLayer + uLayer; |
| 724 | + |
| 725 | + [ptEncoder->tEncoder copyFromTexture:ptMetalTexture->tTexture |
| 726 | + sourceSlice:uSourceSlice |
| 727 | + sourceLevel:ptRegion->uMipLevel |
| 728 | + sourceOrigin:tOrigin |
| 729 | + sourceSize:tSize |
| 730 | + toBuffer:ptMetalBuffer->tBuffer |
| 731 | + destinationOffset:uDestinationOffset |
| 732 | + destinationBytesPerRow:uBytesPerRow |
| 733 | + destinationBytesPerImage:uBytesPerImage]; |
| 734 | + } |
686 | 735 |
|
687 | 736 | pl__metal_barrier_in_encoder(ptEncoder->tEncoder); |
688 | 737 | } |
|
0 commit comments