You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
6.9 KiB

  1. unit uengShaderGeneratorEx;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. uengShaderFile, uengShaderGenerator, uengShaderFileTypes;
  7. type
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. { almost the same as TengShaderGenerator, but this is the extended version, that creates a copy of
  10. all properties. So you can keep more then one gerator configurations at once. It is a little bit slower
  11. than the normal generator. If you not need multiple configurations at once, use the simple one (TengShaderGenerator) }
  12. TengShaderGeneratorEx = class(TObject)
  13. private
  14. fName: String;
  15. fGeneratorPart: TengShaderGenerator;
  16. function GetPropertyByIndex(const aIndex: Integer): Variant;
  17. function GetPropertyByName(const aName: String): Variant;
  18. function GetPropertyCount: Integer;
  19. function GetPropertyNames(const aIndex: Integer): String;
  20. procedure SetPropertyByIndex(const aIndex: Integer; aValue: Variant);
  21. procedure SetPropertyByName(const aName: String; aValue: Variant);
  22. procedure GeneratorPartDestroy(aSender: TObject);
  23. protected
  24. fProperties: TStringVariantMap;
  25. public
  26. property Name: String read fName write fName;
  27. property PropertyByName [const aName: String]: Variant read GetPropertyByName write SetPropertyByName;
  28. property PropertyByIndex[const aIndex: Integer]: Variant read GetPropertyByIndex write SetPropertyByIndex;
  29. property PropertyNames [const aIndex: Integer]: String read GetPropertyNames;
  30. property PropertyCount: Integer read GetPropertyCount;
  31. function TryGetProperty(const aName: String; out aValue: Variant): Boolean;
  32. function TrySetProperty(const aName: String; const aValue: Variant): Boolean;
  33. procedure ListProperties(const aPropertyNames: TStrings);
  34. procedure GenerateCode(const aCode: TengShaderCode);
  35. constructor Create(const aShaderFile: TengShaderFile; const aName: String);
  36. destructor Destroy; override;
  37. end;
  38. implementation
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. //TengShaderGeneratorEx///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. function TengShaderGeneratorEx.GetPropertyByIndex(const aIndex: Integer): Variant;
  43. begin
  44. result := fProperties.ValueAt[aIndex];
  45. end;
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. function TengShaderGeneratorEx.GetPropertyByName(const aName: String): Variant;
  48. var
  49. i: Integer;
  50. begin
  51. i := fProperties.IndexOf(aName);
  52. if (i < 0) then
  53. raise EengUnknownIdentifier.Create(aName, self);
  54. result := fProperties.ValueAt[i];
  55. end;
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. function TengShaderGeneratorEx.GetPropertyCount: Integer;
  58. begin
  59. result := fProperties.Count;
  60. end;
  61. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62. function TengShaderGeneratorEx.GetPropertyNames(const aIndex: Integer): String;
  63. begin
  64. result := fProperties.Keys[aIndex];
  65. end;
  66. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  67. procedure TengShaderGeneratorEx.SetPropertyByIndex(const aIndex: Integer; aValue: Variant);
  68. begin
  69. fProperties.ValueAt[aIndex] := aValue;
  70. end;
  71. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  72. procedure TengShaderGeneratorEx.SetPropertyByName(const aName: String; aValue: Variant);
  73. var
  74. i: Integer;
  75. begin
  76. i := fProperties.IndexOf(aName);
  77. if (i < 0) then
  78. raise EengUnknownIdentifier.Create(aName, self);
  79. fProperties.ValueAt[i] := aValue;
  80. end;
  81. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. procedure TengShaderGeneratorEx.GeneratorPartDestroy(aSender: TObject);
  83. begin
  84. fGeneratorPart := nil;
  85. end;
  86. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. function TengShaderGeneratorEx.TryGetProperty(const aName: String; out aValue: Variant): Boolean;
  88. var
  89. i: Integer;
  90. begin
  91. i := fProperties.IndexOf(aName);
  92. result := (i >= 0);
  93. if result then
  94. aValue := fProperties.ValueAt[i];
  95. end;
  96. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. function TengShaderGeneratorEx.TrySetProperty(const aName: String; const aValue: Variant): Boolean;
  98. var
  99. i: Integer;
  100. begin
  101. i := fProperties.IndexOf(aName);
  102. result := (i >= 0);
  103. if result then
  104. fProperties.ValueAt[i] := aValue;
  105. end;
  106. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  107. procedure TengShaderGeneratorEx.ListProperties(const aPropertyNames: TStrings);
  108. var
  109. s: String;
  110. begin
  111. for s in fProperties.Keys do
  112. aPropertyNames.Add(s);
  113. end;
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115. procedure TengShaderGeneratorEx.GenerateCode(const aCode: TengShaderCode);
  116. var
  117. kvp: TStringVariantMap.TKeyValuePair;
  118. begin
  119. if not Assigned(fGeneratorPart) then
  120. EengShaderPartInternal.Create('unable to generate code: generator is not available anymore');
  121. for kvp in fProperties.KeyValuePairs do
  122. fGeneratorPart.TrySetProperty(kvp.Key, kvp.Value);
  123. fGeneratorPart.GenerateCode(aCode);
  124. end;
  125. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  126. constructor TengShaderGeneratorEx.Create(const aShaderFile: TengShaderFile; const aName: String);
  127. begin
  128. inherited Create;
  129. fProperties := TStringVariantMap.Create;
  130. fGeneratorPart := aShaderFile.Generator[aName];
  131. fGeneratorPart.DuplicateProperties(fProperties);
  132. end;
  133. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  134. destructor TengShaderGeneratorEx.Destroy;
  135. begin
  136. FreeAndNil(fProperties);
  137. inherited Destroy;
  138. end;
  139. end.