TestRender.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
  3. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/LexicalPath.h>
  8. #include <AK/String.h>
  9. #include <LibCore/FileStream.h>
  10. #include <LibGL/GL/gl.h>
  11. #include <LibGL/GLContext.h>
  12. #include <LibGfx/Bitmap.h>
  13. #include <LibGfx/QOIWriter.h>
  14. #include <LibTest/TestCase.h>
  15. #ifdef __serenity__
  16. # define REFERENCE_IMAGE_DIR "/usr/Tests/LibGL/reference-images"
  17. #else
  18. # define REFERENCE_IMAGE_DIR "reference-images"
  19. #endif
  20. #define SAVE_OUTPUT false
  21. static NonnullOwnPtr<GL::GLContext> create_testing_context(int width, int height)
  22. {
  23. auto bitmap = MUST(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { width, height }));
  24. auto context = GL::create_context(*bitmap);
  25. GL::make_context_current(context);
  26. return context;
  27. }
  28. static void expect_bitmap_equals_reference(Gfx::Bitmap const& bitmap, StringView test_name)
  29. {
  30. auto reference_filename = String::formatted("{}.qoi", test_name);
  31. if constexpr (SAVE_OUTPUT) {
  32. auto target_path = LexicalPath("/home/anon").append(reference_filename);
  33. auto qoi_buffer = Gfx::QOIWriter::encode(bitmap);
  34. auto qoi_output_stream = MUST(Core::OutputFileStream::open(target_path.string()));
  35. auto number_of_bytes_written = qoi_output_stream.write(qoi_buffer);
  36. qoi_output_stream.close();
  37. EXPECT_EQ(number_of_bytes_written, qoi_buffer.size());
  38. }
  39. auto reference_image_path = String::formatted(REFERENCE_IMAGE_DIR "/{}", reference_filename);
  40. auto reference_bitmap = MUST(Gfx::Bitmap::try_load_from_file(reference_image_path));
  41. EXPECT_EQ(reference_bitmap->visually_equals(bitmap), true);
  42. }
  43. TEST_CASE(0001_simple_triangle)
  44. {
  45. auto context = create_testing_context(64, 64);
  46. glFrontFace(GL_CCW);
  47. glCullFace(GL_BACK);
  48. glEnable(GL_CULL_FACE);
  49. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  50. glClear(GL_COLOR_BUFFER_BIT);
  51. glBegin(GL_TRIANGLES);
  52. glColor3f(1, 1, 1);
  53. glVertex2f(0, 1);
  54. glVertex2f(-1, -1);
  55. glVertex2f(1, -1);
  56. glEnd();
  57. EXPECT_EQ(glGetError(), 0u);
  58. context->present();
  59. expect_bitmap_equals_reference(context->frontbuffer(), "0001_simple_triangle");
  60. }
  61. TEST_CASE(0002_quad_color_interpolation)
  62. {
  63. auto context = create_testing_context(64, 64);
  64. glFrontFace(GL_CCW);
  65. glCullFace(GL_BACK);
  66. glEnable(GL_CULL_FACE);
  67. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  68. glClear(GL_COLOR_BUFFER_BIT);
  69. glBegin(GL_QUADS);
  70. glColor3f(1, 0, 0);
  71. glVertex2i(-1, -1);
  72. glColor3f(0, 1, 0);
  73. glVertex2i(1, -1);
  74. glColor3f(0, 0, 1);
  75. glVertex2i(1, 1);
  76. glColor3f(1, 0, 1);
  77. glVertex2i(-1, 1);
  78. glEnd();
  79. EXPECT_EQ(glGetError(), 0u);
  80. context->present();
  81. expect_bitmap_equals_reference(context->frontbuffer(), "0002_quad_color_interpolation");
  82. }