TextureMapExampleComponent.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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::IndexBufferView m_indexBufferView;
  53. AZ::RHI::InputStreamLayout m_inputStreamLayout;
  54. };
  55. // Component
  56. void Activate() override;
  57. void Deactivate() override;
  58. // RHISystemNotificationBus::Handler
  59. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  60. // TickBus::Handler
  61. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  62. void CreateInputAssemblyBufferPool();
  63. void InitRenderTargetBufferView();
  64. void InitTexture1DBufferView();
  65. void InitTexture1DArrayBufferView();
  66. void InitTexture2DArrayBufferView();
  67. void InitCubemapBufferView();
  68. void InitCubemapArrayBufferView();
  69. void InitTexture3DBufferView();
  70. void InitRenderTargets();
  71. void LoadRenderTargetShader();
  72. void LoadRasterShader(const char* shaderFilePath, int target);
  73. void CreateTextureScope(int target, const AZ::RHI::ImageViewDescriptor& imageViewDescriptor, const char* scopeName);
  74. void CreateRasterScope(int target, const AZ::RHI::ImageViewDescriptor& imageViewDescriptor, const char* scopeName);
  75. void SetVertexPositionTransform(VertexPosition* positionBuffer, int bufferIndex, AZ::Matrix3x3 transform, float translateX, float translateY);
  76. void SetVertexPositionArray(VertexPosition* positionBuffer, float scale, float translateX, float translateY );
  77. void SetVertexPositionCubemap(VertexPosition* positionBuffer, int bufferIndex, float translateX, float translateY);
  78. void SetVertexIndexRectsCounterClock(uint16_t* indexBuffer, size_t arraySize);
  79. void SetVertexUVWArray(VertexUVW* uvwBuffer, int arraySize);
  80. void SetVertexUVWCubemap(VertexUVW* uvwBuffer);
  81. void SetVertexUVWXCubemapArray(VertexUVWX* uvwxBuffer, int arraySize);
  82. void InitBufferView(int target,
  83. uint32_t posSize, void* posData,
  84. uint32_t uvSize, void* uvData, uint32_t uvTypeSize,
  85. uint32_t indexSize, void* indexData);
  86. // -------------------
  87. // Input Assembly Data
  88. // -------------------
  89. AZStd::array<VertexPosition, 72> m_positions;
  90. AZStd::array<uint16_t, 108> m_indices;
  91. AZStd::array<VertexUV, 12> m_uvs;
  92. AZStd::array<VertexUVW, 24> m_uvws;
  93. AZStd::array<VertexUVWX, 72> m_uvwxs;
  94. // -------------------------------------
  95. // Input Assembly buffer and buffer view
  96. // -------------------------------------
  97. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_inputAssemblyBufferPool;
  98. // array for buffer and buffer view ( all render targets + screen)
  99. AZStd::array< AZ::RHI::Ptr<AZ::RHI::Buffer>, s_numOfTargets + 1> m_positionBuffer;
  100. AZStd::array< AZ::RHI::Ptr<AZ::RHI::Buffer>, s_numOfTargets + 1> m_uvBuffer;
  101. AZStd::array< AZ::RHI::Ptr<AZ::RHI::Buffer>, s_numOfTargets + 1> m_indexBuffer;
  102. AZStd::array<BufferViewData, s_numOfTargets + 1> m_bufferViews;
  103. // ---------------------------------------
  104. // Pipeline state, SRG, shader input index
  105. // ---------------------------------------
  106. AZStd::array<AZ::RHI::ConstPtr<AZ::RHI::PipelineState>, s_numOfTargets> m_targetPipelineStates;
  107. AZStd::array<AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, s_numOfTargets> m_targetSRGs;
  108. AZStd::array<AZ::RHI::ShaderInputConstantIndex, s_numOfTargets> m_shaderInputConstantIndices;
  109. AZStd::array<AZ::RHI::ConstPtr<AZ::RHI::PipelineState>, s_numOfTargets> m_screenPipelineStates;
  110. AZStd::array<AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, s_numOfTargets> m_screenSRGs;
  111. AZStd::array<AZ::RHI::ShaderInputImageIndex, s_numOfTargets> m_shaderInputImageIndices;
  112. // ------
  113. // Others
  114. // ------
  115. float m_time = 0.0f;
  116. AZ::RHI::ClearValue m_clearValue = AZ::RHI::ClearValue::CreateVector4Float(0.0f, 0.0f, 0.0f, 0.0f);
  117. AZStd::array<AZ::RHI::TransientImageDescriptor, s_numOfTargets> m_renderTargetImageDescriptors;
  118. AZStd::array<AZ::RHI::AttachmentId, s_numOfTargets> m_attachmentID;
  119. AZStd::array<AZ::Vector3, 4> m_baseRectangle;
  120. };
  121. }