GLS.CUDADataAccess.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLS.CUDADataAccess;
  5. (* CUDA data access implementation *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. System.SysUtils,
  10. GLS.Utils,
  11. GLS.Logger,
  12. GLS.Strings,
  13. GLCrossPlatform;
  14. type
  15. GCUDAHostElementAccess<TScalar> = class
  16. public
  17. const
  18. ElementSize = SizeOf(TScalar);
  19. type
  20. TVector2 = array[0..1] of TScalar;
  21. TVector3 = array[0..2] of TScalar;
  22. TVector4 = array[0..3] of TScalar;
  23. private
  24. class procedure CheckElementSize(ACNum: Cardinal); inline;
  25. class function GetScalar: TScalar;
  26. class function GetVector2: TVector2;
  27. class function GetVector3: TVector3;
  28. class function GetVector4: TVector4;
  29. class procedure SetScalar(const AValue: TScalar);
  30. class procedure SetVector2(const AValue: TVector2);
  31. class procedure SetVector3(const AValue: TVector3);
  32. class procedure SetVector4(const AValue: TVector4);
  33. public
  34. property Scalar: TScalar read GetScalar write SetScalar;
  35. property Vector2: TVector2 read GetVector2 write SetVector2;
  36. property Vector3: TVector3 read GetVector3 write SetVector3;
  37. property Vector4: TVector4 read GetVector4 write SetVector4;
  38. end;
  39. UByteElement = GCUDAHostElementAccess<Byte>;
  40. ByteElement = GCUDAHostElementAccess<ShortInt>;
  41. UShortElement= GCUDAHostElementAccess<Word>;
  42. ShortElement = GCUDAHostElementAccess<SmallInt>;
  43. UIntElement = GCUDAHostElementAccess<LongWord>;
  44. IntElement = GCUDAHostElementAccess<LongInt>;
  45. HalfElement = GCUDAHostElementAccess<THalfFloat>;
  46. FloatElement = GCUDAHostElementAccess<Single>;
  47. DoubleElement = GCUDAHostElementAccess<Double>;
  48. procedure SetElementAccessAddress(AValue: PByte; ASize: Cardinal);
  49. function GetElementAccessAddress: PByte;
  50. function GetElementAccessSize: Cardinal;
  51. //-----------------------------------------------
  52. implementation
  53. //-----------------------------------------------
  54. threadvar
  55. vElementAccessAddress: PByte;
  56. vElementAccessElementSize: Cardinal;
  57. function GetElementAccessAddress: PByte;
  58. begin
  59. Result := vElementAccessAddress;
  60. end;
  61. function GetElementAccessSize: Cardinal;
  62. begin
  63. Result := vElementAccessElementSize;
  64. end;
  65. procedure SetElementAccessAddress(AValue: PByte; ASize: Cardinal);
  66. begin
  67. vElementAccessAddress := AValue;
  68. vElementAccessElementSize := ASize;
  69. end;
  70. class procedure GCUDAHostElementAccess<TScalar>.CheckElementSize(ACNum: Cardinal);
  71. begin
  72. if GetElementAccessSize <> ACNum * SizeOf(TScalar) then
  73. begin
  74. GLSLogger.LogError(strSizeMismatch);
  75. Abort;
  76. end;
  77. end;
  78. class function GCUDAHostElementAccess<TScalar>.GetScalar: TScalar;
  79. begin
  80. CheckElementSize(1);
  81. Move(GetElementAccessAddress^, Result, SizeOf(TScalar));
  82. end;
  83. class function GCUDAHostElementAccess<TScalar>.GetVector2: TVector2;
  84. begin
  85. CheckElementSize(2);
  86. Move(GetElementAccessAddress^, Result, 2*SizeOf(TScalar));
  87. end;
  88. class function GCUDAHostElementAccess<TScalar>.GetVector3: TVector3;
  89. begin
  90. CheckElementSize(3);
  91. Move(GetElementAccessAddress^, Result, 3*SizeOf(TScalar));
  92. end;
  93. class function GCUDAHostElementAccess<TScalar>.GetVector4: TVector4;
  94. begin
  95. CheckElementSize(4);
  96. Move(GetElementAccessAddress^, Result, 4*SizeOf(TScalar));
  97. end;
  98. class procedure GCUDAHostElementAccess<TScalar>.SetScalar(const AValue: TScalar);
  99. begin
  100. CheckElementSize(1);
  101. Move(AValue, GetElementAccessAddress^, SizeOf(TScalar));
  102. end;
  103. class procedure GCUDAHostElementAccess<TScalar>.SetVector2(const AValue: TVector2);
  104. begin
  105. CheckElementSize(2);
  106. Move(AValue, GetElementAccessAddress^, 2*SizeOf(TScalar));
  107. end;
  108. class procedure GCUDAHostElementAccess<TScalar>.SetVector3(const AValue: TVector3);
  109. begin
  110. CheckElementSize(3);
  111. Move(AValue, GetElementAccessAddress^, 3*SizeOf(TScalar));
  112. end;
  113. class procedure GCUDAHostElementAccess<TScalar>.SetVector4(const AValue: TVector4);
  114. begin
  115. CheckElementSize(4);
  116. Move(AValue, GetElementAccessAddress^, 4*SizeOf(TScalar));
  117. end;
  118. end.