DepthBuffer.cpp 966 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibSoftGPU/DepthBuffer.h>
  7. namespace SoftGPU {
  8. DepthBuffer::DepthBuffer(Gfx::IntSize const& size)
  9. : m_size(size)
  10. , m_data(new float[size.width() * size.height()])
  11. {
  12. }
  13. DepthBuffer::~DepthBuffer()
  14. {
  15. delete[] m_data;
  16. }
  17. float* DepthBuffer::scanline(int y)
  18. {
  19. VERIFY(y >= 0 && y < m_size.height());
  20. return &m_data[y * m_size.width()];
  21. }
  22. void DepthBuffer::clear(float depth)
  23. {
  24. int num_entries = m_size.width() * m_size.height();
  25. for (int i = 0; i < num_entries; ++i) {
  26. m_data[i] = depth;
  27. }
  28. }
  29. void DepthBuffer::clear(Gfx::IntRect bounds, float depth)
  30. {
  31. bounds.intersect({ 0, 0, m_size.width(), m_size.height() });
  32. for (int y = bounds.top(); y <= bounds.bottom(); ++y)
  33. for (int x = bounds.left(); x <= bounds.right(); ++x)
  34. m_data[y * m_size.width() + x] = depth;
  35. }
  36. }