[PATCH vkd3d v4 2/4] vkd3d: Factor ReadFrom/WriteToSubresource box validation.

Rémi Bernon rbernon at codeweavers.com
Wed Oct 2 10:35:00 CDT 2019


Signed-off-by: Rémi Bernon <rbernon at codeweavers.com>
---
 libs/vkd3d/resource.c | 89 ++++++++++++++++++++-----------------------
 1 file changed, 41 insertions(+), 48 deletions(-)

diff --git a/libs/vkd3d/resource.c b/libs/vkd3d/resource.c
index ccd1230c..dd66f175 100644
--- a/libs/vkd3d/resource.c
+++ b/libs/vkd3d/resource.c
@@ -1072,6 +1072,43 @@ static bool d3d12_resource_validate_box(const struct d3d12_resource *resource,
             && !(box->bottom & height_mask);
 }
 
+static HRESULT d3d12_resource_validate_box_or_default(const struct d3d12_resource *resource,
+        unsigned int sub_resource_idx, const D3D12_BOX *box_in, D3D12_BOX *box_out)
+{
+    unsigned int mip_level = sub_resource_idx % resource->desc.MipLevels;
+    D3D12_BOX box;
+
+    if (box_in)
+    {
+        if (!d3d12_resource_validate_box(resource, sub_resource_idx, box_in))
+        {
+            WARN("Invalid box %s.\n", debug_d3d12_box(box_in));
+            return E_INVALIDARG;
+        }
+
+        box = *box_in;
+    }
+    else
+    {
+        box.left = 0;
+        box.top = 0;
+        box.front = 0;
+        box.right = d3d12_resource_desc_get_width(&resource->desc, mip_level);
+        box.bottom = d3d12_resource_desc_get_height(&resource->desc, mip_level);
+        box.back = d3d12_resource_desc_get_depth(&resource->desc, mip_level);
+    }
+
+    if (box.right <= box.left || box.bottom <= box.top || box.back <= box.front)
+    {
+        WARN("Empty box %s.\n", debug_d3d12_box(box_in));
+        return S_FALSE;
+    }
+
+    assert(box_out);
+    *box_out = box;
+    return S_OK;
+}
+
 /* ID3D12Resource */
 static inline struct d3d12_resource *impl_from_ID3D12Resource(ID3D12Resource *iface)
 {
@@ -1330,30 +1367,8 @@ static HRESULT STDMETHODCALLTYPE d3d12_resource_WriteToSubresource(ID3D12Resourc
     vk_sub_resource.mipLevel = dst_sub_resource % resource->desc.MipLevels;
     vk_sub_resource.aspectMask = format->vk_aspect_mask;
 
-    if (dst_box)
-    {
-        if (!d3d12_resource_validate_box(resource, dst_sub_resource, dst_box))
-        {
-            WARN("Invalid box %s.\n", debug_d3d12_box(dst_box));
-            return E_INVALIDARG;
-        }
-
-        box = *dst_box;
-    }
-    else
-    {
-        box.left = 0;
-        box.top = 0;
-        box.front = 0;
-        box.right = d3d12_resource_desc_get_width(&resource->desc, vk_sub_resource.mipLevel);
-        box.bottom = d3d12_resource_desc_get_height(&resource->desc, vk_sub_resource.mipLevel);
-        box.back = d3d12_resource_desc_get_depth(&resource->desc, vk_sub_resource.mipLevel);
-    }
-    if (box.right <= box.left || box.bottom <= box.top || box.back <= box.front)
-    {
-        WARN("Empty box %s.\n", debug_d3d12_box(dst_box));
-        return S_OK;
-    }
+    if ((hr = d3d12_resource_validate_box_or_default(resource, dst_sub_resource, dst_box, &box)))
+        return SUCCEEDED(hr) ? S_OK : hr;
 
     if (!d3d12_resource_is_cpu_accessible(resource))
     {
@@ -1440,30 +1455,8 @@ static HRESULT STDMETHODCALLTYPE d3d12_resource_ReadFromSubresource(ID3D12Resour
     vk_sub_resource.mipLevel = src_sub_resource % resource->desc.MipLevels;
     vk_sub_resource.aspectMask = format->vk_aspect_mask;
 
-    if (src_box)
-    {
-        if (!d3d12_resource_validate_box(resource, src_sub_resource, src_box))
-        {
-            WARN("Invalid box %s.\n", debug_d3d12_box(src_box));
-            return E_INVALIDARG;
-        }
-
-        box = *src_box;
-    }
-    else
-    {
-        box.left = 0;
-        box.top = 0;
-        box.front = 0;
-        box.right = d3d12_resource_desc_get_width(&resource->desc, vk_sub_resource.mipLevel);
-        box.bottom = d3d12_resource_desc_get_height(&resource->desc, vk_sub_resource.mipLevel);
-        box.back = d3d12_resource_desc_get_depth(&resource->desc, vk_sub_resource.mipLevel);
-    }
-    if (box.right <= box.left || box.bottom <= box.top || box.back <= box.front)
-    {
-        WARN("Empty box %s.\n", debug_d3d12_box(src_box));
-        return S_OK;
-    }
+    if ((hr = d3d12_resource_validate_box_or_default(resource, src_sub_resource, src_box, &box)))
+        return SUCCEEDED(hr) ? S_OK : hr;
 
     if (!d3d12_resource_is_cpu_accessible(resource))
     {
-- 
2.23.0




More information about the wine-devel mailing list