添加批量drawcall

This commit is contained in:
2025-05-19 16:22:34 +08:00
parent f6ccb823c7
commit e67a193c77
58 changed files with 1358 additions and 162 deletions

View File

@ -1,9 +1,23 @@
set(PROJECT_NAME "Sandbox")
project(${PROJECT_NAME})
file(GLOB_RECURSE SOURCES "src/*.cpp")
# SandBox
project(${PROJECT_NAME})
file(GLOB_RECURSE SOURCES
src/SandboxApp.cpp
src/SandBox2D/*.cpp)
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE Hazel)
# demo App
set(DEMO_PROJECT "${PROJECT_NAME}-Demo")
file(GLOB_RECURSE DEMO_SOURCES
src/SandboxApp.cpp
src/DemoBox/*.cpp)
add_executable(${DEMO_PROJECT} ${DEMO_SOURCES})
target_link_libraries(${DEMO_PROJECT} PRIVATE Hazel)

View File

@ -0,0 +1,26 @@
// Flat Color Shader
#type vertex
#version 460 core
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec2 a_TexCoord;
uniform mat4 u_ViewProjection;
uniform mat4 u_Transform;
void main() {
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0f);
}
#type fragment
#version 460 core
layout(location = 0) out vec4 color;
uniform vec4 u_Color;
void main() {
color = u_Color;
}

View File

@ -2,16 +2,23 @@
#version 460 core
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec2 a_TexCoord;
layout(location = 1) in vec4 a_Color;
layout(location = 2) in vec2 a_TexCoord;
layout(location = 3) in float a_TexIndex;
uniform mat4 u_ViewProjection;
uniform mat4 u_Transform;
//uniform mat4 u_Transform;
out vec2 v_TexCoord;
out vec4 v_Color;
out float v_TexIndex;
void main() {
v_TexCoord = a_TexCoord;
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0f);
v_Color = a_Color;
v_TexIndex = a_TexIndex;
gl_Position = u_ViewProjection * vec4(a_Position, 1.0f);
}
#type fragment
@ -20,19 +27,15 @@ void main() {
layout(location = 0) out vec4 color;
in vec2 v_TexCoord;
in vec4 v_Color;
in float v_TexIndex;
uniform sampler2D u_Texture;
uniform float u_TilingFactor;
uniform vec4 u_Color;
uniform sampler2D u_Textures[32];
void main() {
vec4 tmpColor = texture(u_Texture, v_TexCoord);
if(gl_FragCoord.x < 635){
color = tmpColor;
}
else{
if(tmpColor.a < 0.1){
discard;
}else{
color = vec4(v_TexCoord, 0.0f, 1.0f);
}
}
color = texture(u_Textures[int(v_TexIndex)], v_TexCoord * u_TilingFactor) * v_Color;
// color = v_Color;
}

View File

@ -0,0 +1,59 @@
//
// Created by sfd on 25-5-18.
//
#include "GameLayer.h"
#include <imgui_internal.h>
#include <Hazel/Core/Application.h>
GameLayer::GameLayer() : Layer("GameLayer")
{
auto& window = Application::Get().GetWindow();
CreateCamera(window.GetWidth(), window.GetHeight());
Random::Init();
}
void GameLayer::OnAttach()
{
m_Level.Init();
ImGuiIO& io = ImGui::GetIO();
}
void GameLayer::OnDetech()
{
}
void GameLayer::OnUpdate(TimeStep& ts)
{
m_Time += ts;
}
void GameLayer::OnEvent(SDL_Event& e)
{
if (const auto& window = Application::Get().GetWindow(); e.type == SDL_EVENT_WINDOW_RESIZED && e.window.windowID == window.GetMainWindowID())
{
CreateCamera(window.GetWidth(), window.GetHeight());
}
}
void GameLayer::OnImGuiRender()
{
}
void GameLayer::CreateCamera(uint32_t width, uint32_t height)
{
const float aspect = static_cast<float>(width) / static_cast<float>(height);
const float camWidth = 8.0f;
const float bottom = -camWidth;
const float top = camWidth;
const float left = bottom * aspect;
const float right = top * aspect;
m_Camera = OrthographicCamera(left, right, bottom, top);
}

View File

@ -0,0 +1,43 @@
//
// Created by sfd on 25-5-18.
//
#ifndef GAMELAYER_H
#define GAMELAYER_H
#include <Hazel/Core/Layer.h>
#include <Hazel/Renderer/OrthographicCamera.h>
#include "Random.h"
using namespace Hazel;
class GameLayer : public Layer{
public:
GameLayer();
~GameLayer() = default;
void OnAttach() override;
void OnDetech() override;
void OnUpdate(TimeStep& ts) override;
void OnEvent(SDL_Event& e) override;
void OnImGuiRender() override;
void CreateCamera(uint32_t width, uint32_t height);
private:
std::unique_ptr<OrthographicCamera> m_Camera;
float m_Time = 0.0f;
bool m_Blink = false;
enum class GameState
{
Play = 0, MainMenu, GameOver
};
GameState m_GameState = GameState::MainMenu;
};
#endif //GAMELAYER_H

View File

@ -0,0 +1,5 @@
//
// Created by sfd on 25-5-18.
//
#include "Random.h"

View File

@ -0,0 +1,28 @@
//
// Created by sfd on 25-5-18.
//
#ifndef RANDOM_H
#define RANDOM_H
#include <random>
class Random {
public:
static void Init() {
s_RandomEngine.seed(std::random_device()());
}
static float Float()
{
return (float)s_Distribution(s_RandomEngine) / (float)std::numeric_limits<uint32_t>::max();
}
private:
static std::mt19937 s_RandomEngine;
static std::uniform_int_distribution<std::mt19937::result_type> s_Distribution;
};
#endif //RANDOM_H

View File

@ -0,0 +1,99 @@
//
// Created by sfd on 25-5-17.
//
#include "SandBox2D.h"
#include <imgui.h>
#include "glm/gtc/type_ptr.hpp"
SandBox2D::SandBox2D()
: Layer("SandBox2D"), m_CameraController(1280.f /720.f)
{
}
void SandBox2D::OnAttach()
{
HZ_PROFILE_FUNCTION();
m_LogoTexture = Hazel::Texture2D::Create("assets/textures/iceLogo.png");
}
void SandBox2D::OnDetech()
{
HZ_PROFILE_FUNCTION();
}
void SandBox2D::OnUpdate(Hazel::TimeStep& ts)
{
// PROFILE_SCOPE("SandBox2D::OnUpdate");
HZ_PROFILE_FUNCTION();
m_CameraController.OnUpdate(ts);
{
HZ_PROFILE_SCOPE("Renderer Prep");
Hazel::RendererCommand::SetClearColor({0.2f, 0.2f, 0.2f, 1.0f});
Hazel::RendererCommand::Clear();
}
{
HZ_PROFILE_SCOPE("Renderer Draw");
Hazel::Renderer2D::BeginScene(m_CameraController.GetCamera());
// Hazel::Renderer2D::DrawQuad({0.0f, 0.0f}, {0.31f, 0.31f}, {0.2f, 0.3f, 0.8f, 1.0f});
Hazel::Renderer2D::DrawQuad({-1.0f, 0.0f}, {0.7f, 0.7f}, {0.2f, 0.3f, 0.8f, 1.0f});
Hazel::Renderer2D::DrawQuad({0.5f, 0.5f}, {0.5f, 0.7f}, {0.8f, 0.2f, 0.3f, 1.0f});
// Hazel::Renderer2D::DrawQuad({0.0f, 0.0f, -0.1f}, {2.0f, 2.0f}, m_LogoTexture, 10.0f);
// Hazel::Renderer2D::DrawRotateQuad({0.0f, 0.0f, 0.1f}, {2.0f, 2.0f}, glm::radians(45.f), m_LogoTexture, 10.0f, glm::vec4(1.0f, 1.0f, 1.0f, 1.f));
Hazel::Renderer2D::DrawQuad({0.0f, 0.0f, -0.1f}, {2.0f, 2.0f}, m_LogoTexture, 10.0f);
Hazel::Renderer2D::EndScene();
}
/*
std::dynamic_pointer_cast<Hazel::OpenGLShader>(m_FlatColorShader)->Bind();
std::dynamic_pointer_cast<Hazel::OpenGLShader>(m_FlatColorShader)->UploadUniformFloat4("u_Color", m_SquareColor);
Hazel::Renderer::Submit(m_FlatColorShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f)));
*/
}
void SandBox2D::OnImGuiRender()
{
const auto cameraRotation = m_CameraController.GetCamera().GetRotation();
const auto cameraPosition = m_CameraController.GetCamera().GetPosition();
const auto windowWidth = Hazel::Application::Get().GetWindow().GetWidth();
const auto windowHeight = Hazel::Application::Get().GetWindow().GetHeight();
ImGui::Begin("Hazel Layer");
ImGui::Text("WindowSize: (%u, %u)", windowWidth, windowHeight);
ImGui::Text("Camera");
ImGui::Text("Rotation: %f", cameraRotation);
ImGui::Text("Position: ( %.2f, %.2f, %.2f)", cameraPosition.x, cameraPosition.y, cameraPosition.z);
ImGui::NewLine();
ImGui::Text("frame: %.3f", ImGui::GetIO().Framerate);
ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor));
ImGui::NewLine();
for (auto& profileResult : m_ProfileResults)
{
ImGui::Text("%s: %.3fms", profileResult.Name, profileResult.Time);
}
m_ProfileResults.clear();
ImGui::NewLine();
static bool isSync = Hazel::Application::Get().GetWindow().IsVSync();
bool laststate = isSync;
ImGui::Checkbox("isVSync", &isSync);
if (isSync != laststate)
{
Hazel::Application::Get().GetWindow().SetVSync(isSync);
}
ImGui::End();
}
void SandBox2D::OnEvent(SDL_Event& e)
{
m_CameraController.OnEvent(e);
}

View File

@ -0,0 +1,40 @@
//
// Created by sfd on 25-5-17.
//
#ifndef SANDBOX2D_H
#define SANDBOX2D_H
#include <Hazel.h>
class SandBox2D : public Hazel::Layer{
public:
SandBox2D();
virtual ~SandBox2D() = default;
virtual void OnAttach() override;
virtual void OnDetech() override;
void OnUpdate(Hazel::TimeStep& ts) override;
virtual void OnImGuiRender() override;
void OnEvent(SDL_Event& e) override;
private:
Hazel::OrthographicCameraController m_CameraController;
Hazel::Ref<Hazel::VertexArray> m_SquareVA;
Hazel::Ref<Hazel::Shader> m_FlatColorShader;
Hazel::Ref<Hazel::Texture2D> m_Texture;
Hazel::Ref<Hazel::Texture2D> m_LogoTexture;
glm::vec4 m_SquareColor = {0.2f, 0.3f, 0.8f, 1.0f};
struct ProfileResult
{
const char* Name;
float Time;
};
std::vector<ProfileResult> m_ProfileResults;
};
#endif //SANDBOX2D_H

View File

@ -1,27 +1,20 @@
#include <Hazel.h>
#include "Hazel/Core/EntryPoint.h"
#include "imgui.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <Hazel/OrthographicCameraController.h>
#include <Platform/OpenGL/OpenGLShader.h>
#include <Platform/OpenGL/OpenGLTexture.h>
#include "glm/gtc/type_ptr.hpp"
#include "glm/gtx/transform.hpp"
// #include "SandBox2D/SandBox2D.h"
// #include "DemoBox/GameLayer.h"
#include "SandBox2D/SandBox2D.h"
/*
class ExampleLayer : public Hazel::Layer
{
public:
// ExampleLayer() : Layer("ExampleLayer"), m_Camera(-1.0f, 1.0f, -1.0f, 1.0f), m_CameraPosition(0.0f)
ExampleLayer() : Layer("ExampleLayer"), m_CameraController(1280.0f / 720.0f), m_SquarePosition(glm::vec3(0.0f))
{
// ------------------------------------------------------------test------------------------------------------------------------
// Vertex Array
m_VertexArray.reset(Hazel::VertexArray::Create());
m_VertexArray = Hazel::VertexArray::Create();
// Vertex Buffer
float vertices[3 * 7] = {
@ -79,7 +72,7 @@ public:
m_Shader = Hazel::Shader::Create("demoShader", vertexSrc, fragmentSrc);
m_SquareVA.reset(Hazel::VertexArray::Create());
m_SquareVA = Hazel::VertexArray::Create();
float squareVertices[5 * 4] = {
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
@ -233,6 +226,8 @@ private:
glm::vec4 m_ScreenClearColor = {0.1f, 0.1f, 0.1f, 1.0f};
};
*/
class Sandbox : public Hazel::Application
@ -240,7 +235,8 @@ class Sandbox : public Hazel::Application
public:
Sandbox()
{
PushLayer(new ExampleLayer());
// PushLayer(new ExampleLayer());
PushLayer(new SandBox2D());
}
~Sandbox();