Skip to content

ID3D12GraphicsCommandList1::ClearRenderTargetView - array params could possibly be projected better #790

Closed

Description

I'm not sure what the best rust projection would be here - and I don't think that win32metadata has everything needed yet, but I figured we should figure out what can be possible in rust first.

Here's the declaration of ClearRenderTargetView:

virtual void STDMETHODCALLTYPE ClearRenderTargetView( 
            _In_  D3D12_CPU_DESCRIPTOR_HANDLE RenderTargetView,
            _In_  const FLOAT ColorRGBA[ 4 ],
            _In_  UINT NumRects,
            _In_reads_(NumRects)  const D3D12_RECT *pRects) = 0;

Notice the ColorRGBA is explicitly an array for 4 elements. pRects reads the number of rects specified by NumRects.

Here's the rust binding:

pub unsafe fn ClearRenderTargetView<'a>(
    &self,
    rendertargetview: impl ::windows::IntoParam<'a, D3D12_CPU_DESCRIPTOR_HANDLE>,
    colorrgba: *const f32,
    numrects: u32,
    prects: *const super::super::UI::DisplayDevices::RECT,
)

Ideally, I'd be able to specify the color inline. Maybe that's possible? This is what I came up with:

let clear_color: [f32; 4] = [0.0, 0.2, 0.4, 1.0];
command_list.ClearRenderTargetView(rtv_handle, &clear_color[0], 0, std::ptr::null());

It's a bit clunky having to take a reference to the first element of the array.

For pRects, I notice that the rust binding isn't make any use of the relationship between prects and numrects. Maybe there's not anything it could do with it, but I thought I'd mention it while I was here.

Activity

  1. samlh commented on May 11, 2021

    @samlh

    For pRects, I notice that the rust binding isn't make any use of the relationship between prects and numrects. Maybe there's not anything it could do with it, but I thought I'd mention it while I was here.

    Ideally, that annotation would let you pass a rust slice to the prects parameter.

    Something like:

    let clear_color: [f32; 4] = [0.0, 0.2, 0.4, 1.0];
    let rects: [RECT; _] = [RECT { /*...*/ }, RECT { /*...*/ }]; // or Vec<RECT> and vec![...]
    command_list.ClearRenderTargetView(rtv_handle, &clear_color, &rects[..]);
  2. kennykerr commented on May 11, 2021

    @kennykerr
    Collaborator

    Yes, I still have a ways to go to make the most of the array metadata.

  3. MarijnS95 commented on May 11, 2021

    @MarijnS95
    Contributor

    I reckon colorrgba could be projected as a simple static-sized array pointer: colorrgba: *const [f32; 4]? That way you can pass &clear_color from the let clear_color: [f32; 4] example above or &[0.0, 0.2, 0.4, 1.0] directly in-line.

    Pointers to static-sized arrays - unlike slices - are (expectably so) not fat pointers:

    [src/main.rs:7] std::mem::size_of::<&[f32; 4]>() = 8
    [src/main.rs:8] std::mem::size_of::<*const [f32; 4]>() = 8
    [src/main.rs:9] std::mem::size_of::<&[f32]>() = 16
    [src/main.rs:10] std::mem::size_of::<*const [f32]>() = 16

    EDIT: Suggested in the DX12 review to use clear_color.as_ptr() instead of &clear_color[0] 👍

  4. damyanp commented on Jun 16, 2021

    @damyanp
    MemberAuthor

    Similar issue for ExecuteCommandLists:

    unsafe void ExecuteCommandLists([In] uint NumCommandLists, [In][NativeArrayInfo(CountParamIndex = 0)] ID3D12CommandList* ppCommandLists);
    

    (mentioning it here so that it shows up in searchs)

  5. kennykerr commented on Feb 23, 2022

    @kennykerr
    Collaborator

    Sorry for the delay, #1562 adds support for Win32 array parameters and fixes this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions