TextureMapExampleComponent.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Component/TickBus.h>
  10. #include <AzCore/Math/Matrix3x3.h>
  11. #include <Atom/RHI/PipelineState.h>
  12. #include <RHI/BasicRHIComponent.h>
  13. namespace AtomSampleViewer
  14. {
  15. /*
  16. * All Texture testing
  17. * Testing render to and sample from 1d, 1d array, 2d array, cubemap, cubemap array and 3d texture
  18. * Showing as flashing rectangles for each texture layer when success
  19. * From left to right are: 1d, 1d array, 2d array, cubemap, cubemap array, 3d
  20. */
  21. class TextureMapExampleComponent final
  22. : public BasicRHIComponent
  23. , public AZ::TickBus::Handler
  24. {
  25. public:
  26. AZ_COMPONENT(TextureMapExampleComponent, "{AF3E2D0C-7C5B-476C-B4D5-41526D88BF3C}", AZ::Component);
  27. static void Reflect(AZ::ReflectContext* context);
  28. TextureMapExampleComponent();
  29. ~TextureMapExampleComponent() = default;
  30. protected:
  31. AZ_DISABLE_COPY(TextureMapExampleComponent);
  32. static const char* s_textureMapExampleName;
  33. static const int s_numOfTargets = 6;
  34. static const int s_textureWidth = 8;
  35. static const int s_textureHeight = 8;
  36. static const int s_textureDepth = 8;
  37. static const int s_arraySize = 3;
  38. static const AZ::RHI::Format s_textureFormat = AZ::RHI::Format::R8G8B8A8_UNORM_SRGB;
  39. enum RenderTargetIndex
  40. {
  41. Texture1D = 0,
  42. Texture1DArray,
  43. Texture2DArray,
  44. TextureCubemap,
  45. TextureCubemapArray,
  46. Texture3D,
  47. BufferViewIndex
  48. };
  49. struct BufferViewData
  50. {
  51. AZ::RHI::GeometryView m_geometryView;
  52. AZ::RHI::InputStreamLayout m_inputStreamLayout;
  53. };
  54. // Component
  55. void Activate() override;
  56. void Deactivate() override;
  57. // RHISystemNotificationBus::Handler
  58. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  59. // TickBus::Handler
  60. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  61. void CreateInputAssemblyBufferPool();
  62. void InitRenderTargetBufferView();
  63. void InitTexture1DBufferView();
  64. void InitTexture1DArrayBufferView();
  65. void InitTexture2DArrayBufferView();
  66. void InitCubemapBufferView();
  67. void InitCubemapArrayBufferView();
  68. void InitTexture3DBufferView();
  69. void InitRenderTargets();
  70. void LoadRenderTargetShader();
  71. void LoadRasterShader(const char* shaderFilePath, int target);
  72. void CreateTextureScope(int target, const AZ::RHI::ImageViewDescriptor& imageViewDescriptor, const char* scopeName);
  73. void CreateRasterScope(int target, const AZ::RHI::ImageViewDescriptor& imageViewDescriptor, const char* scopeName);
  74. void SetVertexPositionTransform(VertexPosition* positionBuffer, int bufferIndex, AZ::Matrix3x3 transform, float translateX, float translateY);
  75. void SetVertexPositionArray(VertexPosition* positionBuffer, float scale, float translateX, float translateY );
  76. void SetVertexPositionCubemap(VertexPosition* positionBuffer, int bufferIndex, float translateX, float translateY);
  77. void SetVertexIndexRectsCounterClock(uint16_t* indexBuffer, size_t arraySize);
  78. void SetVertexUVWArray(VertexUVW* uvwBuffer, int arraySize);
  79. void SetVertexUVWCubemap(VertexUVW* uvwBuffer);
  80. void SetVertexUVWXCubemapArray(VertexUVWX* uvwxBuffer, int arraySize);
  81. void InitBufferView(int target,
  82. uint32_t posSize, void* posData,
  83. uint32_t uvSize, void* uvData, uint32_t uvTypeSize,
  84. uint32_t indexSize, void* indexData);
  85. // -------------------
  86. // Input Assembly Data
  87. // -------------------
  88. AZStd::array<VertexPosition, 72> m_positions;
  89. AZStd::array<uint16_t, 108> m_indices;
  90. AZStd::array<VertexUV, 12> m_uvs;
  91. AZStd::array<VertexUVW, 24> m_uvws;
  92. AZStd::array<VertexUVWX, 72> m_uvwxs;
  93. // -------------------------------------
  94. // Input Assembly buffer and buffer view
  95. // -------------------------------------
  96. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_inputAssemblyBufferPool;
  97. // array for buffer and buffer view ( all render targets + screen)
  98. AZStd::array< AZ::RHI::Ptr<AZ::RHI::Buffer>, s_numOfTargets + 1> m_positionBuffer;
  99. AZStd::array< AZ::RHI::Ptr<AZ::RHI::Buffer>, s_numOfTargets + 1> m_uvBuffer;
  100. AZStd::array< AZ::RHI::Ptr<AZ::RHI::Buffer>, s_numOfTargets + 1> m_indexBuffer;
  101. AZStd::array<BufferViewData, s_numOfTargets + 1> m_bufferViews;
  102. // ---------------------------------------
  103. // Pipeline state, SRG, shader input index
  104. // ---------------------------------------
  105. AZStd::array<AZ::RHI::ConstPtr<AZ::RHI::PipelineState>, s_numOfTargets> m_targetPipelineStates;
  106. AZStd::array<AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, s_numOfTargets> m_targetSRGs;
  107. AZStd::array<AZ::RHI::ShaderInputConstantIndex, s_numOfTargets> m_shaderInputConstantIndices;
  108. AZStd::array<AZ::RHI::ConstPtr<AZ::RHI::PipelineState>, s_numOfTargets> m_screenPipelineStates;
  109. AZStd::array<AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, s_numOfTargets> m_screenSRGs;
  110. AZStd::array<AZ::RHI::ShaderInputImageIndex, s_numOfTargets> m_shaderInputImageIndices;
  111. // ------
  112. // Others
  113. // ------
  114. float m_time = 0.0f;
  115. AZ::RHI::ClearValue m_clearValue = AZ::RHI::ClearValue::CreateVector4Float(0.0f, 0.0f, 0.0f, 0.0f);
  116. AZStd::array<AZ::RHI::TransientImageDescriptor, s_numOfTargets> m_renderTargetImageDescriptors;
  117. AZStd::array<AZ::RHI::AttachmentId, s_numOfTargets> m_attachmentID;
  118. AZStd::array<AZ::Vector3, 4> m_baseRectangle;
  119. };
  120. }