Recent

Author Topic: Problem with Generic Const  (Read 492 times)

SonnyBoyXXl

  • Jr. Member
  • **
  • Posts: 59
Problem with Generic Const
« on: February 09, 2026, 11:30:12 pm »
Hi together,
I'm currently working on a relaunch of the DX12 headers, since Free Pascal has made big improvements over the last 10 years  :)
But I have now one problem with the UpdateSubresources for the Stack.
This ist the Microsoft Header snippet
Quote
//------------------------------------------------------------------------------------------------
// Stack-allocating UpdateSubresources implementation
template <UINT MaxSubresources>
inline UINT64 UpdateSubresources(
    _In_ ID3D12GraphicsCommandList* pCmdList,
    _In_ ID3D12Resource* pDestinationResource,
    _In_ ID3D12Resource* pIntermediate,
    UINT64 IntermediateOffset,
    _In_range_(0,MaxSubresources) UINT FirstSubresource,
    _In_range_(1,MaxSubresources-FirstSubresource) UINT NumSubresources,
    _In_reads_(NumSubresources) const D3D12_SUBRESOURCE_DATA* pSrcData) noexcept

This is my pascal definition:
Code: Pascal  [Select][+][-]
  1. //------------------------------------------------------------------------------------------------
  2. // Stack-allocating UpdateSubresources implementation
  3. generic function UpdateSubresources_Stack<const MaxSubresources: integer>(
  4.     {_In_ } pCmdList: ID3D12GraphicsCommandList;
  5.     {_In_ } pDestinationResource: ID3D12Resource;
  6.     {_In_ } pIntermediate: ID3D12Resource; IntermediateOffset: uint64;
  7.     {_In_range_(0,MaxSubresources) } FirstSubresource: UINT;
  8.     {_In_range_(1,MaxSubresources-FirstSubresource) } NumSubresources: UINT;
  9.     {_In_reads_(NumSubresources) } pSrcData: PD3D12_SUBRESOURCE_DATA_ARRAY): uint64;

and my pascal implementation
Code: Pascal  [Select][+][-]
  1. //------------------------------------------------------------------------------------------------
  2. // Stack-allocating UpdateSubresources implementation
  3. generic function UpdateSubresources_Stack<const MaxSubresources: integer>(pCmdList: ID3D12GraphicsCommandList; pDestinationResource: ID3D12Resource; pIntermediate: ID3D12Resource; IntermediateOffset: uint64; FirstSubresource: UINT;
  4.     NumSubresources: UINT; pSrcData: PD3D12_SUBRESOURCE_DATA_ARRAY): uint64;
  5. var
  6.     RequiredSize: uint64 = 0;
  7.     Layouts: array [0..MaxSubresources-1] of TD3D12_PLACED_SUBRESOURCE_FOOTPRINT;
  8.     NumRows: array [0..MaxSubresources-1] of UINT;
  9.     RowSizesInBytes: array [0..MaxSubresources - 1] of uint64;
  10.     Desc: TD3D12_RESOURCE_DESC;
  11.     pDevice: ID3D12Device = nil;
  12. begin
  13.     if ((NumSubresources + FirstSubresource) > MaxSubresources) then
  14.     begin
  15.         Result := 0;
  16.         Exit;
  17.     end;
  18.  
  19.     pDestinationResource.GetDesc(@Desc);
  20.  
  21.     pDestinationResource.GetDevice(@IID_ID3D12Device, pDevice);
  22.     pDevice.GetCopyableFootprints(@Desc, FirstSubresource, NumSubresources, IntermediateOffset, @Layouts[0], NumRows, RowSizesInBytes, @RequiredSize);
  23.     pDevice := nil;
  24.  
  25.     Result := UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, @Layouts[0], @NumRows[0], @RowSizesInBytes[0], pSrcData);
  26. end;    

but the compiler (CT 8.8, FPC 3.3.1) fails with:

Quote
DX12.D3DX12_Resource_Helpers.pas(144,18) Error: Forward declaration not solved "UpdateSubresources_Stack$1(ID3D12GraphicsCommandList;ID3D12Resource;ID3D12Resource;QWord;LongWord;LongWord;PD3D12_SUBRESOURCE_DATA_ARRAY):System.QWord;"

the funny thing: commenting the declaration the headers will compile. so the code itselfs is working. just the declaration fails.

Why does the compiler complain? Any hints or ideas?

best regards

PS: for the Microsoft multithreading sample I've use the heap version at the moment, since the heap code is compiling. See attached screenshot.





Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12195
  • Debugger - SynEdit - and more
    • wiki
Re: Problem with Generic Const
« Reply #1 on: February 09, 2026, 11:47:40 pm »
I don't know if it matches your issue / sorry late tonight, not going to read up details....

But related to constraints gen funct...
https://forum.lazarus.freepascal.org/index.php/topic,52157.msg383991.html#msg383991

And at least one bug in that field:
https://gitlab.com/freepascal.org/fpc/source/-/issues/41521

And not sure how new CT is currently. The IDE autocompletion was fixed (as far as tests with the compiler allowed), but only very recently. So if you used code completion for the forward declaration, then that may have generated it wrong (and still may in edge cases, if so need to be reported - when/if fpc can handle it)

SonnyBoyXXl

  • Jr. Member
  • **
  • Posts: 59
Re: Problem with Generic Const
« Reply #2 on: February 10, 2026, 07:59:15 am »
Thx, that solved the problem.

The working implementation definition is

Code: Pascal  [Select][+][-]
  1. generic function UpdateSubresources_Stack<MaxSubresources>(pCmdList: ID3D12GraphicsCommandList; pDestinationResource: ID3D12Resource; pIntermediate: ID3D12Resource; IntermediateOffset: uint64;
  2.     FirstSubresource: UINT; NumSubresources: UINT; pResourceData: Pvoid; pSrcData: PD3D12_SUBRESOURCE_INFO_ARRAY): uint64;            
  3. begin {} end;
  4.  

Thank you

 

TinyPortal © 2005-2018