1// Copyright (c) 2020 Qualcomm Technologies Incorporated
2//
3// SPDX-License-Identifier: CC-BY-4.0
4
5[[copies-images-scaling-rotation]]
6=== Image Blits With Scaling and Rotation
7
8When slink:VkCopyCommandTransformInfoQCOM is in the pname:pNext chain of
9slink:VkImageBlit2, the specified region is rotated during the blit.
10The following description of rotated addressing replaces the description in
11flink:vkCmdBlitImage.
12
13The following code computes rotation of normalized coordinates.
14
15[source,c]
16----
17// rotation of normalized coordinates
18VkOffset2D RotateNormUV(VkOffset2D in, VkSurfaceTransformFlagBitsKHR flags)
19{
20    VkOffset2D output;
21    switch (flags)
22    {
23        case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
24            out.x = in.x;
25            out.y = in.y;
26            break;
27        case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
28            out.x = in.y;
29            out.y = 1.0 - in.x;
30            break;
31        case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
32            out.x = 1.0 - in.x;
33            out.y = 1.0 - in.y;
34            break;
35        case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
36            out.x = 1.0 - in.y;
37            out.y = in.x;
38            break;
39    }
40    return out;
41}
42----
43
44  * For each destination texel, the integer coordinate of that texel is
45    converted to an unnormalized texture coordinate, using the effective
46    inverse of the equations described in
47    <<textures-unnormalized-to-integer, unnormalized to integer
48    conversion>>:
49  {empty}:: [eq]#u~base~ = i {plus} {onehalf}#
50  {empty}:: [eq]#v~base~ = j {plus} {onehalf}#
51  {empty}:: [eq]#w~base~ = k {plus} {onehalf}#
52  * These base coordinates are then offset by the first destination offset:
53  {empty}:: [eq]#u~offset~ = u~base~ - x~dst0~#
54  {empty}:: [eq]#v~offset~ = v~base~ - y~dst0~#
55  {empty}:: [eq]#w~offset~ = w~base~ - z~dst0~#
56  {empty}:: [eq]#a~offset~ = a - pname:baseArrayCount~dst~#
57
58  * The UV destination coordinates are scaled by the destination region,
59    rotated, and scaled by the source region.
60  {empty}:: [eq]#u~dest_scaled~ = u~offset~ / (x~dst1~ - x~dst0~)#
61  {empty}:: [eq]#v~dest_scaled~ = v~offset~ / (y~dst1~ - y~dst0~)#
62  {empty}:: [eq]#(u~src_scaled~, v~src_scaled~) =
63            code:RotateNormUV(u~dest_scaled~, v~dest_scaled~,
64            pname:transform)#
65  {empty}:: [eq]#u~scaled~ = u~src_scaled~ {times} (x~Src1~ - x~Src0~)#
66  {empty}:: [eq]#v~scaled~ = v~src_scaled~ {times} (y~Src1~ - y~Src0~)#
67
68  * The W coordinate is unaffected by rotation.
69    The scale is determined from the ratio of source and destination
70    regions, and applied to the offset coordinate:
71  {empty}:: [eq]#scale~w~ = (z~Src1~ - z~Src0~) / (z~dst1~ - z~dst0~)#
72  {empty}:: [eq]#w~scaled~ = w~offset~ {times} scale~w~#
73
74
75  * Finally the source offset is added to the scaled source coordinates, to
76    determine the final unnormalized coordinates used to sample from
77    pname:srcImage:
78  {empty}:: [eq]#u = u~scaled~ {plus} x~Src0~#
79  {empty}:: [eq]#v = v~scaled~ {plus} y~Src0~#
80  {empty}:: [eq]#w = w~scaled~ {plus} z~Src0~#
81  {empty}:: [eq]#q = pname:mipLevel#
82  {empty}:: [eq]#a = a~offset~ {plus} pname:baseArrayCount~src~#
83
84These coordinates are used to sample from the source image as described for
85<<textures, Image Operations>>, with the filter mode equal to that of
86pname:filter; a mipmap mode of ename:VK_SAMPLER_MIPMAP_MODE_NEAREST; and an
87address mode of ename:VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE.
88Implementations must: clamp at the edge of the source image, and may:
89additionally clamp to the edge of the source region.
90