add many source files
This commit is contained in:
218
3pfollow.c
Normal file
218
3pfollow.c
Normal file
@@ -0,0 +1,218 @@
|
|||||||
|
#include "3pfollow.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
// void ThirdPersonFollow::_ready() {
|
||||||
|
// Target = dynamic_cast<Spatial*>(get_node(TargetPath));
|
||||||
|
// ExternalFrame = dynamic_cast<Spatial*>(get_node(ExternalFramePath));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void ThirdPersonFollow::_process(float delta_time) {
|
||||||
|
|
||||||
|
// if (Target != nullptr) {
|
||||||
|
// // Get the frame of reference
|
||||||
|
// if (has_node(ExternalFramePath) && get_node(ExternalFramePath) != this)
|
||||||
|
// TFOR = dynamic_cast<Spatial*>(get_node(ExternalFramePath))->get_global_transform();
|
||||||
|
// else
|
||||||
|
// TFOR = Transform(Basis(glm::vec3::RIGHT(), glm::vec3::UP(), glm::vec3::FORWARD()), glm::vec3::ZERO());
|
||||||
|
|
||||||
|
// CalculateTargetOffset();
|
||||||
|
// TargetPosition = CalculatePosition();
|
||||||
|
|
||||||
|
// translate(to_local(FrameBasedVectorLerp(get_global_translation(), TargetPosition, PositionSpeeds, delta_time)));
|
||||||
|
|
||||||
|
// set_rotation_degrees(get_rotation_degrees().linear_interpolate(RotationOffset, RotationSpeed * delta_time));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // For rotation
|
||||||
|
// // TODO: Add rotation offset in properly
|
||||||
|
// //this->SetActorRotation(FMath::Lerp(this->GetActorRotation(), TargetRotation, RotationSpeed * DeltaTime));
|
||||||
|
// //this->set_rotation(glm::quat(this->get_rotation().slerp(RotationOffset, RotationSpeed * DeltaTime)).get_euler());
|
||||||
|
|
||||||
|
// //set_global_rotation_degrees(get_global_rotation_degrees().linear_interpolate(RotationOffset, RotationSpeed * delta_time));
|
||||||
|
|
||||||
|
// // TODO: Figure out how to translate rotation to a global rotation
|
||||||
|
// }
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
glm::vec3 ThirdPersonFollow::CalculatePosition()
|
||||||
|
{
|
||||||
|
glm::vec3 p1 = CalculateCenter();
|
||||||
|
|
||||||
|
glm::vec3 p2 =
|
||||||
|
XDirPosts ? GetPostsOffset(TFOR.Right(),
|
||||||
|
FloatWidths.
|
||||||
|
x) : GetExtentsOffset(TFOR.Right(),
|
||||||
|
FloatWidths.x,
|
||||||
|
TargetOffset.x,
|
||||||
|
AnchorWidths.x);
|
||||||
|
glm::vec3 p3 =
|
||||||
|
YDirPosts ? GetPostsOffset(TFOR.Up(),
|
||||||
|
FloatWidths.
|
||||||
|
y) : GetExtentsOffset(TFOR.Up(),
|
||||||
|
FloatWidths.y,
|
||||||
|
TargetOffset.y,
|
||||||
|
AnchorWidths.y);
|
||||||
|
glm::vec3 p4 =
|
||||||
|
ZDirPosts ? GetPostsOffset(TFOR.Back(),
|
||||||
|
FloatWidths.
|
||||||
|
z) : GetExtentsOffset(TFOR.Back(),
|
||||||
|
FloatWidths.z,
|
||||||
|
TargetOffset.z,
|
||||||
|
AnchorWidths.z);
|
||||||
|
|
||||||
|
return p1 + p2 + p3 + p4;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 ThirdPersonFollow::CalculateCenter()
|
||||||
|
{
|
||||||
|
return Target->get_global_translation() +
|
||||||
|
TFOR.TransformDirection(Offset) + (mytransform->Back() * Distance);
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 ThirdPersonFollow::
|
||||||
|
GetPostsOffset(const glm::vec3 & DirectionVector, float AnchorWidth)
|
||||||
|
{
|
||||||
|
float dot = glm::dot(Target->Forward(), DirectionVector);
|
||||||
|
|
||||||
|
return DirectionVector * (dot >= 0 ? AnchorWidth : AnchorWidth * -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 ThirdPersonFollow::
|
||||||
|
GetExtentsOffset(const glm::vec3 & DirectionVector, float AnchorWidth,
|
||||||
|
float TOffset, float Width)
|
||||||
|
{
|
||||||
|
|
||||||
|
float negated_offset_sign = ((0 <= TOffset) - (TOffset < 0)) * -1.f;
|
||||||
|
float TotalWidth = AnchorWidth + Width;
|
||||||
|
|
||||||
|
if (glm::abs(TOffset) > TotalWidth
|
||||||
|
&& !glm::epsilonEqual(glm::abs(TOffset), TotalWidth, 0.5f))
|
||||||
|
return DirectionVector * TotalWidth * negated_offset_sign;
|
||||||
|
else {
|
||||||
|
if (glm::abs(TOffset) >= AnchorWidth)
|
||||||
|
return DirectionVector * AnchorWidth * negated_offset_sign;
|
||||||
|
else
|
||||||
|
return DirectionVector * TOffset * -1.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
return glm::vec3(0.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 ThirdPersonFollow::FrameBasedVectorLerp(const glm::vec3 & From,
|
||||||
|
const glm::vec3 & To,
|
||||||
|
const glm::vec3 & Speeds,
|
||||||
|
float Tick)
|
||||||
|
{
|
||||||
|
// Previously "FORTransform.TransformVector(Speeds)
|
||||||
|
glm::vec3 TSpeed = glm::abs(TFOR.TransformDirection(Speeds));
|
||||||
|
glm::vec3 TOffset = glm::abs(TFOR.TransformDirection(TargetOffset));
|
||||||
|
glm::vec3 TAnchorWidths =
|
||||||
|
glm::abs(TFOR.TransformDirection(AnchorWidths));
|
||||||
|
glm::vec3 TFloatWidths =
|
||||||
|
glm::abs(TFOR.TransformDirection(FloatWidths));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// If these are true, that means to use the anchor speed instead of the normal speed.
|
||||||
|
// True if the offset is beyond the anchor plus the width
|
||||||
|
bool bUseX = GetLerpParam(TOffset.x, TAnchorWidths.x, TFloatWidths.x);
|
||||||
|
bool bUseY = GetLerpParam(TOffset.y, TAnchorWidths.y, TFloatWidths.y);
|
||||||
|
bool bUseZ = GetLerpParam(TOffset.z, TAnchorWidths.z, TFloatWidths.z);
|
||||||
|
|
||||||
|
|
||||||
|
float xAlpha =
|
||||||
|
glm::clamp((bUseX ? AnchorSpeed : TSpeed.x) * Tick, 0.f, 1.f);
|
||||||
|
float yAlpha =
|
||||||
|
glm::clamp((bUseY ? AnchorSpeed : TSpeed.y) * Tick, 0.f, 1.f);
|
||||||
|
float zAlpha =
|
||||||
|
glm::clamp((bUseZ ? AnchorSpeed : TSpeed.z) * Tick, 0.f, 1.f);
|
||||||
|
|
||||||
|
return VectorLerpPiecewise(From, To,
|
||||||
|
glm::vec3(xAlpha, yAlpha, zAlpha));
|
||||||
|
}
|
||||||
|
|
||||||
|
int float_epsilon(mfloat_t a, mfloat_t b, mfloat_t e)
|
||||||
|
{
|
||||||
|
return fabs(a - b) < e;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lerpparam(float offset, float anchorwidth, float floatwidth)
|
||||||
|
{
|
||||||
|
return (offset > (anchorwidth + floatwidth)) && !float_epsilon(anchorwidth + floatwidth, offset, 0.5f);
|
||||||
|
}
|
||||||
|
|
||||||
|
mfloat_t *vec3lerp(mfloat_t from[3], mfloat_t to[3], mfloat_t a[3])
|
||||||
|
{
|
||||||
|
from[0] = from[0] + (to[0] - from[0]) * a[0];
|
||||||
|
from[1] = from[1] + (to[1] - from[1]) * a[1];
|
||||||
|
from[2] = from[2] + (to[2] - from[2]) * a[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
void follow_calctargets()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThirdPersonFollow::CalculateTargets()
|
||||||
|
{
|
||||||
|
// For translation
|
||||||
|
TargetPosition = CalculatePosition();
|
||||||
|
|
||||||
|
|
||||||
|
// For rotation
|
||||||
|
// TODO: Check of this implementation is the same as UKismetMath FindLookAtRotation
|
||||||
|
TargetRotation =
|
||||||
|
RemoveLockedRotation(glm::quat
|
||||||
|
(mytransform->
|
||||||
|
get_global_transform()->get_origin() -
|
||||||
|
Target->
|
||||||
|
get_global_transform()->get_origin()));
|
||||||
|
}
|
||||||
|
|
||||||
|
follow_removelockedrot()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::quat ThirdPersonFollow::
|
||||||
|
RemoveLockedRotation(const glm::quat & CurrentRotation)
|
||||||
|
{
|
||||||
|
glm::vec3 NewRotator;
|
||||||
|
glm::vec3 CurrentRotator = glm::eulerAngles(CurrentRotation);
|
||||||
|
//
|
||||||
|
|
||||||
|
NewRotator.x =
|
||||||
|
LockRoll ? mytransform->get_rotation().x : CurrentRotator.x;
|
||||||
|
NewRotator.y =
|
||||||
|
LockPitch ? mytransform->get_rotation().y : CurrentRotator.y;
|
||||||
|
NewRotator.z =
|
||||||
|
LockYaw ? mytransform->get_rotation().z : CurrentRotator.z;
|
||||||
|
|
||||||
|
return glm::quat(NewRotator);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The target offset is the value of where the target is compared to where he "should" be based on where
|
||||||
|
// the camera is. It is what the camera needs to move to be centered on the target
|
||||||
|
void ThirdPersonFollow::CalculateTargetOffset()
|
||||||
|
{
|
||||||
|
glm::vec3 p1 =
|
||||||
|
(mytransform->Forward() * Distance) +
|
||||||
|
TFOR.TransformDirection(Offset) +
|
||||||
|
mytransform->get_global_translation();
|
||||||
|
glm::vec3 p2 =
|
||||||
|
TFOR.InverseTransformDirection(Target->get_global_translation());
|
||||||
|
glm::vec3 p3 = TFOR.InverseTransformDirection(p1);
|
||||||
|
|
||||||
|
TargetOffset = p2 - p3;
|
||||||
|
}
|
||||||
|
|
||||||
|
void follow_targetoffset()
|
||||||
|
{
|
||||||
|
mfloat_t p1[3], p2[3], p3[3] = {0};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
168
3pfollow.h
Normal file
168
3pfollow.h
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
#ifndef THIRDPERSONFOLLOW_H
|
||||||
|
#define THIRDPERSONFOLLOW_H
|
||||||
|
|
||||||
|
#include "transform.h"
|
||||||
|
#include "HandmadeMath.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
class ThirdPersonFollow {
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum CameraType {
|
||||||
|
STATIONARY,
|
||||||
|
TRANSLATING,
|
||||||
|
ROTATING,
|
||||||
|
SPLINE
|
||||||
|
};
|
||||||
|
|
||||||
|
enum CameraTransition {
|
||||||
|
NONE,
|
||||||
|
CROSSDISSOLVE,
|
||||||
|
WIPE,
|
||||||
|
DIP
|
||||||
|
};
|
||||||
|
|
||||||
|
enum FrameOfReference {
|
||||||
|
LOCAL,
|
||||||
|
WORLD,
|
||||||
|
EXTERNAL
|
||||||
|
};
|
||||||
|
|
||||||
|
ThirdPersonFollow() {
|
||||||
|
// Rotation
|
||||||
|
RotationSpeed = 10.0f;
|
||||||
|
LockPitch = LockYaw = LockRoll = true;
|
||||||
|
|
||||||
|
XDirPosts = false;
|
||||||
|
YDirPosts = false;
|
||||||
|
ZDirPosts = false;
|
||||||
|
|
||||||
|
|
||||||
|
// Translation
|
||||||
|
//FloatWidths = AnchorWidths = CenterVector = glm::vec3(0, 0, 0);
|
||||||
|
PositionSpeeds = glm::vec3(2.f, 2.f, 2.f);
|
||||||
|
//TranslationScales = glm::vec3(1, 1, 1);
|
||||||
|
|
||||||
|
// Frame settings
|
||||||
|
Offset = glm::vec3(0.f, 0.f, 0.f);
|
||||||
|
Distance = 10;
|
||||||
|
|
||||||
|
AnchorSpeed = 80;
|
||||||
|
} ~ThirdPersonFollow() {
|
||||||
|
}
|
||||||
|
|
||||||
|
Transform *mytransform;
|
||||||
|
|
||||||
|
// An actor that can be given for the camera to base its movement around
|
||||||
|
// instead of itself. Makes most sense for this to be stationary
|
||||||
|
Transform *ExternalFrame = nullptr;
|
||||||
|
|
||||||
|
|
||||||
|
void SetExternalFrame(Transform * val) {
|
||||||
|
ExternalFrame = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The target the camera "looks" at, used for calculations
|
||||||
|
Transform *Target = nullptr;
|
||||||
|
|
||||||
|
void SetTarget(Transform * val) {
|
||||||
|
Target = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Offset from the target
|
||||||
|
glm::vec3 Offset;
|
||||||
|
|
||||||
|
// How far away should the camera act from the target
|
||||||
|
float Distance;
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
// Translation variables. In this mode, the camera doesn't rotate to look
|
||||||
|
// at the target, it only moves around the world.
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// "Posts" for each direction in 3D space. These are items that the target
|
||||||
|
/// is allowed to move within without the camera following along.
|
||||||
|
bool XDirPosts;
|
||||||
|
bool YDirPosts;
|
||||||
|
bool ZDirPosts;
|
||||||
|
|
||||||
|
/// The range in ecah direction the camera floats. While within this range,
|
||||||
|
/// the camera will smoothly glide to the desired position.
|
||||||
|
glm::vec3 FloatWidths;
|
||||||
|
|
||||||
|
/// The clamp range for each direction. If the camera reaches this range,
|
||||||
|
/// it will stick and not move any further.
|
||||||
|
glm::vec3 AnchorWidths;
|
||||||
|
|
||||||
|
/// When floating to the target, the speed to float.
|
||||||
|
glm::vec3 PositionSpeeds;
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Rotation variables. Used for the camera's rotation mode, where it
|
||||||
|
// follows the Target without translating.
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// Variables to lock its rotation in any of the three directions
|
||||||
|
bool LockRoll;
|
||||||
|
bool LockPitch;
|
||||||
|
bool LockYaw;
|
||||||
|
|
||||||
|
glm::vec3 RotationOffset;
|
||||||
|
|
||||||
|
/// The speed of rotation
|
||||||
|
float RotationSpeed;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void CalculateTargetOffset();
|
||||||
|
|
||||||
|
// Transform of frame of reference
|
||||||
|
Transform TFOR;
|
||||||
|
|
||||||
|
/// The calculated offset based on frames of reference
|
||||||
|
glm::vec3 TargetOffset;
|
||||||
|
|
||||||
|
glm::vec3 TargetPosition;
|
||||||
|
|
||||||
|
glm::quat TargetRotation;
|
||||||
|
|
||||||
|
|
||||||
|
void CalculateTargets();
|
||||||
|
|
||||||
|
// Calculates
|
||||||
|
|
||||||
|
glm::vec3 CalculatePosition();
|
||||||
|
|
||||||
|
glm::vec3 CalculateCenter();
|
||||||
|
|
||||||
|
/// Given a direction and width, find the offsets.
|
||||||
|
glm::vec3 GetPostsOffset(const glm::vec3 & DirectionVector,
|
||||||
|
float AnchorWidth);
|
||||||
|
|
||||||
|
/// Given anchors, what's the anchor width?
|
||||||
|
glm::vec3 GetExtentsOffset(const glm::vec3 & DirectionVector,
|
||||||
|
float AnchorWidth, float TOffset,
|
||||||
|
float Width);
|
||||||
|
|
||||||
|
glm::quat RemoveLockedRotation(const glm::quat & CurrentRotation);
|
||||||
|
|
||||||
|
glm::vec3 FrameBasedVectorLerp(const glm::vec3 & From,
|
||||||
|
const glm::vec3 & To,
|
||||||
|
const glm::vec3 & Speeds, float Tick);
|
||||||
|
|
||||||
|
glm::vec3 VectorLerpPiecewise(const glm::vec3 & From,
|
||||||
|
const glm::vec3 & To,
|
||||||
|
const glm::vec3 & Alpha);
|
||||||
|
|
||||||
|
bool GetLerpParam(const float Offst, const float AnchorWidth,
|
||||||
|
const float FloatWidth);
|
||||||
|
|
||||||
|
/// Set to a value that gives good clamping, smoothly. Activates when
|
||||||
|
/// the target is out of range.
|
||||||
|
float AnchorSpeed;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#endif
|
||||||
2019
HandmadeMath.c
Normal file
2019
HandmadeMath.c
Normal file
File diff suppressed because it is too large
Load Diff
673
HandmadeMath.h
Normal file
673
HandmadeMath.h
Normal file
@@ -0,0 +1,673 @@
|
|||||||
|
/*
|
||||||
|
HandmadeMath.h v2.0.0
|
||||||
|
|
||||||
|
This is a single header file with a bunch of useful types and functions for
|
||||||
|
games and graphics. Consider it a lightweight alternative to GLM that works
|
||||||
|
both C and C++.
|
||||||
|
|
||||||
|
=============================================================================
|
||||||
|
CONFIG
|
||||||
|
=============================================================================
|
||||||
|
|
||||||
|
By default, all angles in Handmade Math are specified in radians. However, it
|
||||||
|
can be configured to use degrees or turns instead. Use one of the following
|
||||||
|
defines to specify the default unit for angles:
|
||||||
|
|
||||||
|
#define HANDMADE_MATH_USE_RADIANS
|
||||||
|
#define HANDMADE_MATH_USE_DEGREES
|
||||||
|
#define HANDMADE_MATH_USE_TURNS
|
||||||
|
|
||||||
|
Regardless of the default angle, you can use the following functions to
|
||||||
|
specify an angle in a particular unit:
|
||||||
|
|
||||||
|
HMM_AngleRad(radians)
|
||||||
|
HMM_AngleDeg(degrees)
|
||||||
|
HMM_AngleTurn(turns)
|
||||||
|
|
||||||
|
The definitions of these functions change depending on the default unit.
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
Handmade Math ships with SSE (SIMD) implementations of several common
|
||||||
|
operations. To disable the use of SSE intrinsics, you must define
|
||||||
|
HANDMADE_MATH_NO_SSE before including this file:
|
||||||
|
|
||||||
|
#define HANDMADE_MATH_NO_SSE
|
||||||
|
#include "HandmadeMath.h"
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
To use Handmade Math without the C runtime library, you must provide your own
|
||||||
|
implementations of basic math functions. Otherwise, HandmadeMath.h will use
|
||||||
|
the runtime library implementation of these functions.
|
||||||
|
|
||||||
|
Define HANDMADE_MATH_PROVIDE_MATH_FUNCTIONS and provide your own
|
||||||
|
implementations of HMM_SINF, HMM_COSF, HMM_TANF, HMM_ACOSF, and HMM_SQRTF
|
||||||
|
before including HandmadeMath.h, like so:
|
||||||
|
|
||||||
|
#define HANDMADE_MATH_PROVIDE_MATH_FUNCTIONS
|
||||||
|
#define HMM_SINF MySinF
|
||||||
|
#define HMM_COSF MyCosF
|
||||||
|
#define HMM_TANF MyTanF
|
||||||
|
#define HMM_ACOSF MyACosF
|
||||||
|
#define HMM_SQRTF MySqrtF
|
||||||
|
#include "HandmadeMath.h"
|
||||||
|
|
||||||
|
By default, it is assumed that your math functions take radians. To use
|
||||||
|
different units, you must define HMM_ANGLE_USER_TO_INTERNAL and
|
||||||
|
HMM_ANGLE_INTERNAL_TO_USER. For example, if you want to use degrees in your
|
||||||
|
code but your math functions use turns:
|
||||||
|
|
||||||
|
#define HMM_ANGLE_USER_TO_INTERNAL(a) ((a)*HMM_DegToTurn)
|
||||||
|
#define HMM_ANGLE_INTERNAL_TO_USER(a) ((a)*HMM_TurnToDeg)
|
||||||
|
|
||||||
|
=============================================================================
|
||||||
|
|
||||||
|
LICENSE
|
||||||
|
|
||||||
|
This software is in the public domain. Where that dedictaion is not
|
||||||
|
recognized, you are granted a perpetual, irrevocable license to copy,
|
||||||
|
distribute, and modify this file as you see fit.
|
||||||
|
|
||||||
|
=============================================================================
|
||||||
|
|
||||||
|
CREDITS
|
||||||
|
|
||||||
|
Originally written by Zakary Strange.
|
||||||
|
|
||||||
|
Functionality:
|
||||||
|
Zakary Strange (strangezak@protonmail.com && @strangezak)
|
||||||
|
Matt Mascarenhas (@miblo_)
|
||||||
|
Aleph
|
||||||
|
FieryDrake (@fierydrake)
|
||||||
|
Gingerbill (@TheGingerBill)
|
||||||
|
Ben Visness (@bvisness)
|
||||||
|
Trinton Bullard (@Peliex_Dev)
|
||||||
|
@AntonDan
|
||||||
|
Logan Forman (@dev_dwarf)
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
Jeroen van Rijn (@J_vanRijn)
|
||||||
|
Kiljacken (@Kiljacken)
|
||||||
|
Insofaras (@insofaras)
|
||||||
|
Daniel Gibson (@DanielGibson)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef HANDMADE_MATH_H
|
||||||
|
#define HANDMADE_MATH_H
|
||||||
|
|
||||||
|
#if !defined(HANDMADE_MATH_NO_SIMD)
|
||||||
|
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
|
||||||
|
#define HANDMADE_MATH__USE_NEON 1
|
||||||
|
#include <arm_neon.h>
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
#if defined(_M_AMD64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 1)
|
||||||
|
#define HANDMADE_MATH__USE_SSE 1
|
||||||
|
#include <xmmintrin.h>
|
||||||
|
#endif
|
||||||
|
#elif defined(__SSE__)
|
||||||
|
#define HANDMADE_MATH__USE_SSE 1
|
||||||
|
#include <xmmintrin.h>
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(disable : 4201)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define HMM_DEPRECATED(msg) __attribute__((deprecated(msg)))
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
#define HMM_DEPRECATED(msg) __declspec(deprecated(msg))
|
||||||
|
#else
|
||||||
|
#define HMM_DEPRECATED(msg)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(HANDMADE_MATH_USE_DEGREES) && !defined(HANDMADE_MATH_USE_TURNS) && !defined(HANDMADE_MATH_USE_RADIANS)
|
||||||
|
#define HANDMADE_MATH_USE_RADIANS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define HMM_PI 3.14159265358979323846
|
||||||
|
#define HMM_PI32 3.14159265359f
|
||||||
|
#define HMM_DEG180 180.0
|
||||||
|
#define HMM_DEG18032 180.0f
|
||||||
|
#define HMM_TURNHALF 0.5
|
||||||
|
#define HMM_TURNHALF32 0.5f
|
||||||
|
#define HMM_RadToDeg ((float)(HMM_DEG180 / HMM_PI))
|
||||||
|
#define HMM_RadToTurn ((float)(HMM_TURNHALF / HMM_PI))
|
||||||
|
#define HMM_DegToRad ((float)(HMM_PI / HMM_DEG180))
|
||||||
|
#define HMM_DegToTurn ((float)(HMM_TURNHALF / HMM_DEG180))
|
||||||
|
#define HMM_TurnToRad ((float)(HMM_PI / HMM_TURNHALF))
|
||||||
|
#define HMM_TurnToDeg ((float)(HMM_DEG180 / HMM_TURNHALF))
|
||||||
|
|
||||||
|
#if defined(HANDMADE_MATH_USE_RADIANS)
|
||||||
|
#define HMM_AngleRad(a) (a)
|
||||||
|
#define HMM_AngleDeg(a) ((a)*HMM_DegToRad)
|
||||||
|
#define HMM_AngleTurn(a) ((a)*HMM_TurnToRad)
|
||||||
|
#elif defined(HANDMADE_MATH_USE_DEGREES)
|
||||||
|
#define HMM_AngleRad(a) ((a)*HMM_RadToDeg)
|
||||||
|
#define HMM_AngleDeg(a) (a)
|
||||||
|
#define HMM_AngleTurn(a) ((a)*HMM_TurnToDeg)
|
||||||
|
#elif defined(HANDMADE_MATH_USE_TURNS)
|
||||||
|
#define HMM_AngleRad(a) ((a)*HMM_RadToTurn)
|
||||||
|
#define HMM_AngleDeg(a) ((a)*HMM_DegToTurn)
|
||||||
|
#define HMM_AngleTurn(a) (a)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(HANDMADE_MATH_PROVIDE_MATH_FUNCTIONS)
|
||||||
|
#include <math.h>
|
||||||
|
#define HMM_SINF sinf
|
||||||
|
#define HMM_COSF cosf
|
||||||
|
#define HMM_TANF tanf
|
||||||
|
#define HMM_SQRTF sqrtf
|
||||||
|
#define HMM_ACOSF acosf
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(HMM_ANGLE_USER_TO_INTERNAL)
|
||||||
|
#define HMM_ANGLE_USER_TO_INTERNAL(a) (HMM_ToRad(a))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(HMM_ANGLE_INTERNAL_TO_USER)
|
||||||
|
#if defined(HANDMADE_MATH_USE_RADIANS)
|
||||||
|
#define HMM_ANGLE_INTERNAL_TO_USER(a) (a)
|
||||||
|
#elif defined(HANDMADE_MATH_USE_DEGREES)
|
||||||
|
#define HMM_ANGLE_INTERNAL_TO_USER(a) ((a)*HMM_RadToDeg)
|
||||||
|
#elif defined(HANDMADE_MATH_USE_TURNS)
|
||||||
|
#define HMM_ANGLE_INTERNAL_TO_USER(a) ((a)*HMM_RadToTurn)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define HMM_MIN(a, b) ((a) > (b) ? (b) : (a))
|
||||||
|
#define HMM_MAX(a, b) ((a) < (b) ? (b) : (a))
|
||||||
|
#define HMM_ABS(a) ((a) > 0 ? (a) : -(a))
|
||||||
|
#define HMM_MOD(a, m) (((a) % (m)) >= 0 ? ((a) % (m)) : (((a) % (m)) + (m)))
|
||||||
|
#define HMM_SQUARE(x) ((x) * (x))
|
||||||
|
|
||||||
|
#define HMMFMT_VEC3 "[%g,%g,%g]"
|
||||||
|
#define HMMPRINT_VEC3(vec) vec.x, vec.y, vec.z
|
||||||
|
|
||||||
|
#define FMT_VEC4 "[%g,%g,%g,%g]"
|
||||||
|
#define PRINT_VEC4(vec) vec.x, vec.y, vec.z, vec.w
|
||||||
|
|
||||||
|
#define FMT_M4 "[%g,%g,%g,%g\n%g,%g,%g,%g\n%g,%g,%g,%g\n%g,%g,%g,%g]"
|
||||||
|
#define PRINT_M4(m) m.e[0][0], m.e[0][1], m.e[0][2], m.e[0][3], m.e[1][0], m.e[1][1], m.e[1][2], m.e[1][3], m.e[2][0], m.e[2][1], m.e[2][2], m.e[2][3], m.e[3][0], m.e[3][1], m.e[3][2], m.e[3][3]
|
||||||
|
|
||||||
|
typedef union HMM_Vec2 {
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float X, Y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
float x, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float U, V;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float Left, Right;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float Width, Height;
|
||||||
|
};
|
||||||
|
|
||||||
|
float Elements[2];
|
||||||
|
float e[2];
|
||||||
|
|
||||||
|
} HMM_Vec2;
|
||||||
|
|
||||||
|
typedef union HMM_Vec3 {
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float X, Y, Z;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct { float x, y, z; };
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float U, V, W;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float R, G, B;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
HMM_Vec2 XY;
|
||||||
|
float _Ignored0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
HMM_Vec2 xy;
|
||||||
|
float _Ignored4;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float _Ignored1;
|
||||||
|
HMM_Vec2 YZ;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
HMM_Vec2 UV;
|
||||||
|
float _Ignored2;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float _Ignored3;
|
||||||
|
HMM_Vec2 VW;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
HMM_Vec2 cp;
|
||||||
|
float _Ignored5;
|
||||||
|
};
|
||||||
|
|
||||||
|
float Elements[3];
|
||||||
|
float e[3];
|
||||||
|
|
||||||
|
} HMM_Vec3;
|
||||||
|
|
||||||
|
typedef union HMM_Quat {
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
HMM_Vec3 XYZ;
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float X, Y, Z;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
float W;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {float x, y, z, w;};
|
||||||
|
|
||||||
|
float Elements[4];
|
||||||
|
float e[4];
|
||||||
|
|
||||||
|
#ifdef HANDMADE_MATH__USE_SSE
|
||||||
|
__m128 SSE;
|
||||||
|
#endif
|
||||||
|
} HMM_Quat;
|
||||||
|
|
||||||
|
|
||||||
|
typedef union HMM_Vec4 {
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
HMM_Vec3 XYZ;
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float X, Y, Z;
|
||||||
|
};
|
||||||
|
HMM_Vec3 xyz;
|
||||||
|
};
|
||||||
|
|
||||||
|
float W;
|
||||||
|
};
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
HMM_Vec3 RGB;
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float R, G, B;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
float A;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
HMM_Vec2 XY;
|
||||||
|
float _Ignored0;
|
||||||
|
float _Ignored1;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
HMM_Vec2 xy;
|
||||||
|
float _ig0;
|
||||||
|
float _ig1;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
HMM_Vec2 _2;
|
||||||
|
float _ig2;
|
||||||
|
float _ig3;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct {
|
||||||
|
HMM_Vec3 _3;
|
||||||
|
float _ig4;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float _Ignored2;
|
||||||
|
HMM_Vec2 YZ;
|
||||||
|
float _Ignored3;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float _Ignored4;
|
||||||
|
float _Ignored5;
|
||||||
|
HMM_Vec2 ZW;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
HMM_Vec2 cp;
|
||||||
|
HMM_Vec2 wh;
|
||||||
|
};
|
||||||
|
|
||||||
|
HMM_Quat quat;
|
||||||
|
struct {float x, y, z, w; };
|
||||||
|
struct {float r, g, b, a; };
|
||||||
|
struct {float u0, u1, v0, v1;};
|
||||||
|
|
||||||
|
float Elements[4];
|
||||||
|
float e[4];
|
||||||
|
|
||||||
|
#ifdef HANDMADE_MATH__USE_SSE
|
||||||
|
__m128 SSE;
|
||||||
|
#endif
|
||||||
|
#ifdef HANDMADE_MATH__USE_NEON
|
||||||
|
float32x4_t NEON;
|
||||||
|
#endif
|
||||||
|
} HMM_Vec4;
|
||||||
|
|
||||||
|
typedef union HMM_Mat2 {
|
||||||
|
float Elements[2][2];
|
||||||
|
HMM_Vec2 Columns[2];
|
||||||
|
|
||||||
|
} HMM_Mat2;
|
||||||
|
|
||||||
|
typedef union HMM_Mat3 {
|
||||||
|
float Elements[3][3];
|
||||||
|
HMM_Vec3 Columns[3];
|
||||||
|
|
||||||
|
} HMM_Mat3;
|
||||||
|
|
||||||
|
typedef union HMM_Mat4 {
|
||||||
|
float Elements[4][4];
|
||||||
|
HMM_Vec4 Columns[4];
|
||||||
|
HMM_Vec4 col[4];
|
||||||
|
float e[4][4];
|
||||||
|
float em[16];
|
||||||
|
} HMM_Mat4;
|
||||||
|
|
||||||
|
extern const HMM_Vec2 v2zero;
|
||||||
|
extern const HMM_Vec2 v2one;
|
||||||
|
extern const HMM_Vec3 v3zero;
|
||||||
|
extern const HMM_Vec3 v3one;
|
||||||
|
extern const HMM_Vec4 v4zero;
|
||||||
|
|
||||||
|
typedef signed int HMM_Bool;
|
||||||
|
|
||||||
|
extern const HMM_Vec3 vX;
|
||||||
|
extern const HMM_Vec3 vY;
|
||||||
|
extern const HMM_Vec3 vZ;
|
||||||
|
|
||||||
|
extern const HMM_Vec3 vUP;
|
||||||
|
extern const HMM_Vec3 vDOWN;
|
||||||
|
extern const HMM_Vec3 vFWD;
|
||||||
|
extern const HMM_Vec3 vBKWD;
|
||||||
|
extern const HMM_Vec3 vLEFT;
|
||||||
|
extern const HMM_Vec3 vRIGHT;
|
||||||
|
|
||||||
|
extern const HMM_Mat4 MAT1;
|
||||||
|
|
||||||
|
extern const HMM_Quat QUAT1;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Angle unit conversion functions
|
||||||
|
*/
|
||||||
|
float HMM_ToRad(float Angle);
|
||||||
|
float HMM_ToDeg(float Angle);
|
||||||
|
float HMM_ToTurn(float Angle);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Floating-point math functions
|
||||||
|
*/
|
||||||
|
float HMM_SinF(float Angle);
|
||||||
|
float HMM_CosF(float Angle);
|
||||||
|
float HMM_TanF(float Angle);
|
||||||
|
float HMM_ACosF(float Arg);
|
||||||
|
float HMM_SqrtF(float Float);
|
||||||
|
float HMM_InvSqrtF(float Float);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Utility functions
|
||||||
|
*/
|
||||||
|
float HMM_Lerp(float A, float Time, float B);
|
||||||
|
float HMM_Clamp(float Min, float Value, float Max);
|
||||||
|
float frand(float max);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Vector initialization
|
||||||
|
*/
|
||||||
|
HMM_Vec2 HMM_V2(float X, float Y);
|
||||||
|
HMM_Vec3 HMM_V3(float X, float Y, float Z);
|
||||||
|
HMM_Vec3 HMM_V3i(float i);
|
||||||
|
HMM_Vec4 HMM_V4(float X, float Y, float Z, float W);
|
||||||
|
HMM_Vec4 HMM_V4V(HMM_Vec3 Vector, float W);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Binary vector operations
|
||||||
|
*/
|
||||||
|
HMM_Vec2 HMM_AddV2(HMM_Vec2 Left, HMM_Vec2 Right);
|
||||||
|
HMM_Vec3 HMM_AddV3(HMM_Vec3 Left, HMM_Vec3 Right);
|
||||||
|
HMM_Vec4 HMM_AddV4(HMM_Vec4 Left, HMM_Vec4 Right);
|
||||||
|
HMM_Vec2 HMM_SubV2(HMM_Vec2 Left, HMM_Vec2 Right);
|
||||||
|
HMM_Vec3 HMM_SubV3(HMM_Vec3 Left, HMM_Vec3 Right);
|
||||||
|
HMM_Vec4 HMM_SubV4(HMM_Vec4 Left, HMM_Vec4 Right);
|
||||||
|
HMM_Vec2 HMM_MulV2(HMM_Vec2 Left, HMM_Vec2 Right);
|
||||||
|
HMM_Vec2 HMM_MulV2F(HMM_Vec2 Left, float Right);
|
||||||
|
HMM_Vec3 HMM_MulV3(HMM_Vec3 Left, HMM_Vec3 Right);
|
||||||
|
HMM_Vec3 HMM_MulV3F(HMM_Vec3 Left, float Right);
|
||||||
|
HMM_Vec4 HMM_MulV4(HMM_Vec4 Left, HMM_Vec4 Right);
|
||||||
|
HMM_Vec4 HMM_MulV4F(HMM_Vec4 Left, float Right);
|
||||||
|
HMM_Vec2 HMM_DivV2(HMM_Vec2 Left, HMM_Vec2 Right);
|
||||||
|
HMM_Vec2 HMM_DivV2F(HMM_Vec2 Left, float Right);
|
||||||
|
HMM_Vec3 HMM_DivV3(HMM_Vec3 Left, HMM_Vec3 Right);
|
||||||
|
HMM_Vec3 HMM_DivV3F(HMM_Vec3 Left, float Right);
|
||||||
|
HMM_Vec4 HMM_DivV4(HMM_Vec4 Left, HMM_Vec4 Right);
|
||||||
|
HMM_Vec4 HMM_DivV4F(HMM_Vec4 Left, float Right);
|
||||||
|
HMM_Bool HMM_EqV2(HMM_Vec2 Left, HMM_Vec2 Right);
|
||||||
|
HMM_Bool HMM_EqV3(HMM_Vec3 Left, HMM_Vec3 Right);
|
||||||
|
HMM_Bool HMM_EqV4(HMM_Vec4 Left, HMM_Vec4 Right);
|
||||||
|
float HMM_DotV2(HMM_Vec2 Left, HMM_Vec2 Right);
|
||||||
|
HMM_Vec2 HMM_ProjV2(HMM_Vec2 a, HMM_Vec2 b);
|
||||||
|
float HMM_DotV3(HMM_Vec3 Left, HMM_Vec3 Right);
|
||||||
|
float HMM_DotV4(HMM_Vec4 Left, HMM_Vec4 Right);
|
||||||
|
HMM_Vec3 HMM_Cross(HMM_Vec3 Left, HMM_Vec3 Right);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Unary vector operations
|
||||||
|
*/
|
||||||
|
float HMM_LenSqrV2(HMM_Vec2 A);
|
||||||
|
float HMM_LenSqrV3(HMM_Vec3 A);
|
||||||
|
float HMM_LenSqrV4(HMM_Vec4 A);
|
||||||
|
float HMM_LenV2(HMM_Vec2 A);
|
||||||
|
float HMM_AngleV2(HMM_Vec2 a, HMM_Vec2 b);
|
||||||
|
float HMM_DistV2(HMM_Vec2 a, HMM_Vec2 b);
|
||||||
|
HMM_Vec2 HMM_V2Rotate(HMM_Vec2 v, float angle);
|
||||||
|
float HMM_LenV3(HMM_Vec3 A);
|
||||||
|
float HMM_DistV3(HMM_Vec3 a, HMM_Vec3 b);
|
||||||
|
float HMM_AngleV3(HMM_Vec3 a, HMM_Vec3 b);
|
||||||
|
float HMM_LenV4(HMM_Vec4 A);
|
||||||
|
float HMM_AngleV4(HMM_Vec4 a, HMM_Vec4 b);
|
||||||
|
HMM_Vec2 HMM_NormV2(HMM_Vec2 A);
|
||||||
|
HMM_Vec3 HMM_NormV3(HMM_Vec3 A);
|
||||||
|
HMM_Vec4 HMM_NormV4(HMM_Vec4 A);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Utility vector functions
|
||||||
|
*/
|
||||||
|
HMM_Vec2 HMM_LerpV2(HMM_Vec2 A, float Time, HMM_Vec2 B);
|
||||||
|
HMM_Vec3 HMM_LerpV3(HMM_Vec3 A, float Time, HMM_Vec3 B);
|
||||||
|
HMM_Vec4 HMM_LerpV4(HMM_Vec4 A, float Time, HMM_Vec4 B);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* SSE stuff
|
||||||
|
*/
|
||||||
|
HMM_Vec4 HMM_LinearCombineV4M4(HMM_Vec4 Left, HMM_Mat4 Right);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 2x2 Matrices
|
||||||
|
*/
|
||||||
|
HMM_Mat2 HMM_M2(void);
|
||||||
|
HMM_Mat2 HMM_M2D(float Diagonal);
|
||||||
|
HMM_Mat2 HMM_TransposeM2(HMM_Mat2 Matrix);
|
||||||
|
HMM_Mat2 HMM_RotateM2(float angle);
|
||||||
|
HMM_Mat2 HMM_AddM2(HMM_Mat2 Left, HMM_Mat2 Right);
|
||||||
|
HMM_Mat2 HMM_SubM2(HMM_Mat2 Left, HMM_Mat2 Right);
|
||||||
|
HMM_Vec2 HMM_MulM2V2(HMM_Mat2 Matrix, HMM_Vec2 Vector);
|
||||||
|
HMM_Mat2 HMM_MulM2(HMM_Mat2 Left, HMM_Mat2 Right);
|
||||||
|
HMM_Mat2 HMM_MulM2F(HMM_Mat2 Matrix, float Scalar);
|
||||||
|
HMM_Mat2 HMM_DivM2F(HMM_Mat2 Matrix, float Scalar);
|
||||||
|
float HMM_DeterminantM2(HMM_Mat2 Matrix);
|
||||||
|
HMM_Mat2 HMM_InvGeneralM2(HMM_Mat2 Matrix);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 3x3 Matrices
|
||||||
|
*/
|
||||||
|
HMM_Mat3 HMM_M3(void);
|
||||||
|
HMM_Mat3 HMM_M3D(float Diagonal);
|
||||||
|
HMM_Mat3 HMM_Translate2D(HMM_Vec2 p);
|
||||||
|
HMM_Mat3 HMM_RotateM3(float angle);
|
||||||
|
HMM_Mat3 HMM_ScaleM3(HMM_Vec2 s);
|
||||||
|
HMM_Mat3 HMM_TransposeM3(HMM_Mat3 Matrix);
|
||||||
|
HMM_Mat3 HMM_AddM3(HMM_Mat3 Left, HMM_Mat3 Right);
|
||||||
|
HMM_Mat3 HMM_SubM3(HMM_Mat3 Left, HMM_Mat3 Right);
|
||||||
|
HMM_Vec3 HMM_MulM3V3(HMM_Mat3 Matrix, HMM_Vec3 Vector);
|
||||||
|
HMM_Mat3 HMM_MulM3(HMM_Mat3 Left, HMM_Mat3 Right);
|
||||||
|
HMM_Mat3 HMM_MulM3F(HMM_Mat3 Matrix, float Scalar);
|
||||||
|
HMM_Mat3 HMM_DivM3F(HMM_Mat3 Matrix, float Scalar);
|
||||||
|
HMM_Mat2 HMM_ScaleM2(HMM_Vec2 Scale);
|
||||||
|
float HMM_DeterminantM3(HMM_Mat3 Matrix);
|
||||||
|
HMM_Mat3 HMM_M2Basis(HMM_Mat2 basis);
|
||||||
|
HMM_Mat3 HMM_InvGeneralM3(HMM_Mat3 Matrix);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 4x4 Matrices
|
||||||
|
*/
|
||||||
|
HMM_Mat4 HMM_M4(void);
|
||||||
|
HMM_Mat4 HMM_M4D(float Diagonal);
|
||||||
|
HMM_Mat4 HMM_TransposeM4(HMM_Mat4 Matrix);
|
||||||
|
HMM_Mat4 HMM_AddM4(HMM_Mat4 Left, HMM_Mat4 Right);
|
||||||
|
HMM_Mat4 HMM_SubM4(HMM_Mat4 Left, HMM_Mat4 Right);
|
||||||
|
HMM_Mat4 HMM_MulM4(HMM_Mat4 Left, HMM_Mat4 Right);
|
||||||
|
HMM_Mat4 HMM_MulM4_P(HMM_Mat4 *Left, HMM_Mat4 *Right);
|
||||||
|
HMM_Mat4 HMM_MulM4F(HMM_Mat4 Matrix, float Scalar);
|
||||||
|
HMM_Vec4 HMM_MulM4V4(HMM_Mat4 Matrix, HMM_Vec4 Vector);
|
||||||
|
HMM_Vec4 HMM_MulM4V4_P(HMM_Mat4 *Matrix, HMM_Vec4 *Vector);
|
||||||
|
HMM_Mat4 HMM_DivM4F(HMM_Mat4 Matrix, float Scalar);
|
||||||
|
float HMM_DeterminantM4(HMM_Mat4 Matrix);
|
||||||
|
|
||||||
|
// Returns a general-purpose inverse of an HMM_Mat4. Note that special-purpose inverses of many transformations
|
||||||
|
// are available and will be more efficient.
|
||||||
|
HMM_Mat4 HMM_InvGeneralM4(HMM_Mat4 Matrix);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Common graphics transformations
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Produces a right-handed orthographic projection matrix with Z ranging from -1 to 1 (the GL convention).
|
||||||
|
// Left, Right, Bottom, and Top specify the coordinates of their respective clipping planes.
|
||||||
|
// Near and Far specify the distances to the near and far clipping planes.
|
||||||
|
HMM_Mat4 HMM_Orthographic_RH_NO(float Left, float Right, float Bottom, float Top, float Near, float Far);
|
||||||
|
|
||||||
|
// Produces a right-handed orthographic projection matrix with Z ranging from 0 to 1 (the DirectX convention).
|
||||||
|
// Left, Right, Bottom, and Top specify the coordinates of their respective clipping planes.
|
||||||
|
// Near and Far specify the distances to the near and far clipping planes.
|
||||||
|
HMM_Mat4 HMM_Orthographic_RH_ZO(float Left, float Right, float Bottom, float Top, float Near, float Far);
|
||||||
|
|
||||||
|
// Produces a left-handed orthographic projection matrix with Z ranging from -1 to 1 (the GL convention).
|
||||||
|
// Left, Right, Bottom, and Top specify the coordinates of their respective clipping planes.
|
||||||
|
// Near and Far specify the distances to the near and far clipping planes.
|
||||||
|
HMM_Mat4 HMM_Orthographic_LH_NO(float Left, float Right, float Bottom, float Top, float Near, float Far);
|
||||||
|
|
||||||
|
// Produces a left-handed orthographic projection matrix with Z ranging from 0 to 1 (the DirectX convention).
|
||||||
|
// Left, Right, Bottom, and Top specify the coordinates of their respective clipping planes.
|
||||||
|
// Near and Far specify the distances to the near and far clipping planes.
|
||||||
|
HMM_Mat4 HMM_Orthographic_LH_ZO(float Left, float Right, float Bottom, float Top, float Near, float Far);
|
||||||
|
|
||||||
|
// Returns an inverse for the given orthographic projection matrix. Works for all orthographic
|
||||||
|
// projection matrices, regardless of handedness or NDC convention.
|
||||||
|
HMM_Mat4 HMM_InvOrthographic(HMM_Mat4 OrthoMatrix);
|
||||||
|
HMM_Mat4 HMM_Perspective_RH_NO(float FOV, float AspectRatio, float Near, float Far);
|
||||||
|
HMM_Mat4 HMM_Perspective_RH_ZO(float FOV, float AspectRatio, float Near, float Far);
|
||||||
|
HMM_Mat4 HMM_Perspective_LH_NO(float FOV, float AspectRatio, float Near, float Far);
|
||||||
|
HMM_Mat4 HMM_Perspective_LH_ZO(float FOV, float AspectRatio, float Near, float Far);
|
||||||
|
|
||||||
|
HMM_Mat4 HMM_Perspective_Metal(float FOV, float AspectRation, float Near, float Far);
|
||||||
|
HMM_Mat4 HMM_Orthographic_Metal(float l, float r, float b, float t, float near, float far);
|
||||||
|
|
||||||
|
HMM_Mat4 HMM_Orthographic_DX(float l, float r, float b, float t, float near, float far);
|
||||||
|
HMM_Mat4 HMM_Orthographic_GL(float l, float r, float b, float t, float near, float far);
|
||||||
|
|
||||||
|
HMM_Mat4 HMM_InvPerspective_RH(HMM_Mat4 PerspectiveMatrix);
|
||||||
|
HMM_Mat4 HMM_InvPerspective_LH(HMM_Mat4 PerspectiveMatrix);
|
||||||
|
HMM_Mat4 HMM_Translate(HMM_Vec3 Translation);
|
||||||
|
HMM_Mat4 HMM_InvTranslate(HMM_Mat4 TranslationMatrix);
|
||||||
|
HMM_Mat4 HMM_Rotate_RH(float Angle, HMM_Vec3 Axis);
|
||||||
|
HMM_Mat4 HMM_Rotate_LH(float Angle, HMM_Vec3 Axis);
|
||||||
|
HMM_Mat4 HMM_InvRotate(HMM_Mat4 RotationMatrix);
|
||||||
|
HMM_Mat4 HMM_Scale(HMM_Vec3 Scale);
|
||||||
|
HMM_Mat4 HMM_InvScale(HMM_Mat4 ScaleMatrix);
|
||||||
|
HMM_Mat4 _HMM_LookAt(HMM_Vec3 F, HMM_Vec3 S, HMM_Vec3 U, HMM_Vec3 Eye);
|
||||||
|
HMM_Mat4 HMM_LookAt_RH(HMM_Vec3 Eye, HMM_Vec3 Center, HMM_Vec3 Up);
|
||||||
|
HMM_Mat4 HMM_LookAt_LH(HMM_Vec3 Eye, HMM_Vec3 Center, HMM_Vec3 Up);
|
||||||
|
HMM_Mat4 HMM_InvLookAt(HMM_Mat4 Matrix);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Quaternion operations
|
||||||
|
*/
|
||||||
|
HMM_Vec3 HMM_QVRot(HMM_Vec3 v, HMM_Quat q)
|
||||||
|
;
|
||||||
|
HMM_Quat HMM_Q(float X, float Y, float Z, float W);
|
||||||
|
HMM_Quat HMM_QV4(HMM_Vec4 Vector);
|
||||||
|
HMM_Quat HMM_AddQ(HMM_Quat Left, HMM_Quat Right);
|
||||||
|
HMM_Quat HMM_SubQ(HMM_Quat Left, HMM_Quat Right);
|
||||||
|
HMM_Quat HMM_MulQ(HMM_Quat Left, HMM_Quat Right);
|
||||||
|
HMM_Quat HMM_MulQF(HMM_Quat Left, float Multiplicative);
|
||||||
|
HMM_Quat HMM_DivQF(HMM_Quat Left, float Divnd);
|
||||||
|
float HMM_DotQ(HMM_Quat Left, HMM_Quat Right);
|
||||||
|
HMM_Quat HMM_InvQ(HMM_Quat Left);
|
||||||
|
HMM_Quat HMM_NormQ(HMM_Quat Quat);
|
||||||
|
HMM_Quat _HMM_MixQ(HMM_Quat Left, float MixLeft, HMM_Quat Right, float MixRight);
|
||||||
|
HMM_Quat HMM_NLerp(HMM_Quat Left, float Time, HMM_Quat Right);
|
||||||
|
HMM_Quat HMM_SLerp(HMM_Quat Left, float Time, HMM_Quat Right);
|
||||||
|
HMM_Mat4 HMM_QToM4(HMM_Quat Left);
|
||||||
|
HMM_Mat4 HMM_M4TRS(HMM_Vec3 t, HMM_Quat q, HMM_Vec3 s);
|
||||||
|
HMM_Mat3 HMM_M3TRS(HMM_Vec2 t, float angle, HMM_Vec2 s);
|
||||||
|
HMM_Mat3 HMM_Mat4ToMat3(HMM_Mat4 m);
|
||||||
|
|
||||||
|
HMM_Quat HMM_M4ToQ_RH(HMM_Mat4 M);
|
||||||
|
HMM_Quat HMM_M4ToQ_LH(HMM_Mat4 M);
|
||||||
|
HMM_Quat HMM_QFromAxisAngle_RH(HMM_Vec3 Axis, float AngleOfRotation);
|
||||||
|
HMM_Quat HMM_QFromAxisAngle_LH(HMM_Vec3 Axis, float AngleOfRotation);
|
||||||
|
|
||||||
|
float HMM_Q_Roll(HMM_Quat q);
|
||||||
|
float HMM_Q_Yaw(HMM_Quat q);
|
||||||
|
float HMM_Q_Pitch(HMM_Quat q);
|
||||||
|
|
||||||
|
#endif /* HANDMADE_MATH_H */
|
||||||
2
Makefile
Normal file
2
Makefile
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
default:
|
||||||
|
cc -fPIC -shared *.c -I../cell/source -Iimgui -lSDL3 -lcell -o prosperon.dylib
|
||||||
63
anim.c
Normal file
63
anim.c
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#include "anim.h"
|
||||||
|
#include "stb_ds.h"
|
||||||
|
|
||||||
|
void animation_run(struct animation *anim, float now)
|
||||||
|
{
|
||||||
|
float elapsed = now - anim->time;
|
||||||
|
elapsed = fmod(elapsed,2);
|
||||||
|
if (!anim->channels) return;
|
||||||
|
|
||||||
|
for (int i = 0; i < arrlen(anim->channels); i++) {
|
||||||
|
struct anim_channel *ch = anim->channels+i;
|
||||||
|
HMM_Vec4 s = sample_sampler(ch->sampler, elapsed);
|
||||||
|
*(ch->target) = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
HMM_Vec4 sample_cubicspline(sampler *sampler, float t, int prev, int next)
|
||||||
|
{
|
||||||
|
HMM_Vec4 ret;
|
||||||
|
HMM_Quat qv = HMM_SLerp(HMM_QV4(sampler->data[prev]), t, HMM_QV4(sampler->data[next]));
|
||||||
|
memcpy(ret.e, qv.e, sizeof(ret.e));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
HMM_Vec4 sample_sampler(sampler *sampler, float time)
|
||||||
|
{
|
||||||
|
if (arrlen(sampler->data) == 0) return v4zero;
|
||||||
|
if (arrlen(sampler->data) == 1) return sampler->data[0];
|
||||||
|
int previous_time=0;
|
||||||
|
int next_time=0;
|
||||||
|
|
||||||
|
for (int i = 1; i < arrlen(sampler->times); i++) {
|
||||||
|
if (time < sampler->times[i]) {
|
||||||
|
previous_time = i-1;
|
||||||
|
next_time = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float td = sampler->times[next_time]-sampler->times[previous_time];
|
||||||
|
float t = (time - sampler->times[previous_time])/td;
|
||||||
|
|
||||||
|
HMM_Vec4 ret;
|
||||||
|
HMM_Quat qv;
|
||||||
|
|
||||||
|
switch(sampler->type) {
|
||||||
|
case LINEAR:
|
||||||
|
return HMM_LerpV4(sampler->data[previous_time],time,sampler->data[next_time]);
|
||||||
|
break;
|
||||||
|
case STEP:
|
||||||
|
return sampler->data[previous_time];
|
||||||
|
break;
|
||||||
|
case CUBICSPLINE:
|
||||||
|
return sample_cubicspline(sampler,t, previous_time, next_time);
|
||||||
|
break;
|
||||||
|
case SLERP:
|
||||||
|
qv = HMM_SLerp(sampler->data[previous_time].quat, time, sampler->data[next_time].quat);
|
||||||
|
memcpy(ret.e,qv.e,sizeof(ret.e));
|
||||||
|
return ret;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return sample_cubicspline(sampler,t, previous_time, next_time);
|
||||||
|
}
|
||||||
37
anim.h
Normal file
37
anim.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef ANIM_H
|
||||||
|
#define ANIM_H
|
||||||
|
|
||||||
|
#include "HandmadeMath.h"
|
||||||
|
|
||||||
|
struct keyframe {
|
||||||
|
double time;
|
||||||
|
double val;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define LINEAR 0
|
||||||
|
#define STEP 1
|
||||||
|
#define CUBICSPLINE 2
|
||||||
|
#define SLERP 3
|
||||||
|
|
||||||
|
typedef struct sampler {
|
||||||
|
float *times;
|
||||||
|
HMM_Vec4 *data;
|
||||||
|
int type;
|
||||||
|
} sampler;
|
||||||
|
|
||||||
|
struct anim_channel {
|
||||||
|
HMM_Vec4 *target;
|
||||||
|
int comps;
|
||||||
|
struct sampler *sampler;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct animation {
|
||||||
|
double time;
|
||||||
|
struct anim_channel *channels;
|
||||||
|
sampler *samplers;
|
||||||
|
} animation;
|
||||||
|
|
||||||
|
void animation_run(struct animation *anim, float now);
|
||||||
|
HMM_Vec4 sample_sampler(sampler *sampler, float time);
|
||||||
|
|
||||||
|
#endif
|
||||||
180
cell_qoi.c
Normal file
180
cell_qoi.c
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
#define QOI_IMPLEMENTATION
|
||||||
|
#include "qoi.h"
|
||||||
|
#include "cell.h"
|
||||||
|
#include "qjs_sdl.h"
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Helper function to check for integer overflow in size calculations
|
||||||
|
static int check_size_overflow(size_t a, size_t b, size_t c, size_t *result)
|
||||||
|
{
|
||||||
|
if (a > SIZE_MAX / b) return 1;
|
||||||
|
size_t temp = a * b;
|
||||||
|
if (temp > SIZE_MAX / c) return 1;
|
||||||
|
*result = temp * c;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// QOI compression/encoding
|
||||||
|
JSValue js_qoi_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
if (argc < 1)
|
||||||
|
return JS_ThrowTypeError(js, "compress_qoi requires an object argument");
|
||||||
|
|
||||||
|
// Check if width/height properties exist
|
||||||
|
JSValue width_val = JS_GetPropertyStr(js, argv[0], "width");
|
||||||
|
JSValue height_val = JS_GetPropertyStr(js, argv[0], "height");
|
||||||
|
|
||||||
|
if (JS_IsNull(width_val) || JS_IsNull(height_val)) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "compress_qoi requires width and height properties");
|
||||||
|
}
|
||||||
|
|
||||||
|
int width, height;
|
||||||
|
if (JS_ToInt32(js, &width, width_val) < 0 || JS_ToInt32(js, &height, height_val) < 0) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "width and height must be numbers");
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
|
||||||
|
if (width < 1 || height < 1)
|
||||||
|
return JS_ThrowRangeError(js, "width and height must be at least 1");
|
||||||
|
|
||||||
|
// Get pixel format
|
||||||
|
JSValue format_val = JS_GetPropertyStr(js, argv[0], "format");
|
||||||
|
SDL_PixelFormat format = js2SDL_PixelFormat(js, format_val);
|
||||||
|
JS_FreeValue(js, format_val);
|
||||||
|
|
||||||
|
if (format == SDL_PIXELFORMAT_UNKNOWN)
|
||||||
|
return JS_ThrowTypeError(js, "Invalid or missing pixel format");
|
||||||
|
|
||||||
|
// Get pixels
|
||||||
|
JSValue pixels_val = JS_GetPropertyStr(js, argv[0], "pixels");
|
||||||
|
size_t pixel_len;
|
||||||
|
void *pixel_data = js_get_blob_data(js, &pixel_len, pixels_val);
|
||||||
|
JS_FreeValue(js, pixels_val);
|
||||||
|
|
||||||
|
if (!pixel_data)
|
||||||
|
return JS_ThrowTypeError(js, "pixels property must be a stoned blob");
|
||||||
|
|
||||||
|
// Validate buffer size
|
||||||
|
int bytes_per_pixel = SDL_BYTESPERPIXEL(format);
|
||||||
|
size_t required_size;
|
||||||
|
if (check_size_overflow(width, height, bytes_per_pixel, &required_size)) {
|
||||||
|
JS_FreeValue(js, pixels_val);
|
||||||
|
return JS_ThrowRangeError(js, "Image dimensions too large");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pixel_len < required_size)
|
||||||
|
return JS_ThrowRangeError(js, "pixels buffer too small for %dx%d format (need %zu bytes, got %zu)",
|
||||||
|
width, height, required_size, pixel_len);
|
||||||
|
|
||||||
|
// Get colorspace (optional, default to sRGB)
|
||||||
|
int colorspace = 0; // QOI_SRGB
|
||||||
|
if (argc > 1) {
|
||||||
|
colorspace = JS_ToBool(js, argv[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine number of channels based on format
|
||||||
|
int channels = SDL_ISPIXELFORMAT_ALPHA(format) ? 4 : 3;
|
||||||
|
|
||||||
|
// Prepare QOI descriptor
|
||||||
|
qoi_desc desc = {
|
||||||
|
.width = width,
|
||||||
|
.height = height,
|
||||||
|
.channels = channels,
|
||||||
|
.colorspace = colorspace
|
||||||
|
};
|
||||||
|
|
||||||
|
// Encode to QOI
|
||||||
|
int out_len;
|
||||||
|
void *qoi_data = qoi_encode(pixel_data, &desc, &out_len);
|
||||||
|
|
||||||
|
if (!qoi_data)
|
||||||
|
return JS_ThrowInternalError(js, "QOI encoding failed");
|
||||||
|
|
||||||
|
// Create result object
|
||||||
|
JSValue result = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, result, "format", JS_NewString(js, "qoi"));
|
||||||
|
JS_SetPropertyStr(js, result, "channels", JS_NewInt32(js, channels));
|
||||||
|
JS_SetPropertyStr(js, result, "colorspace", JS_NewInt32(js, colorspace));
|
||||||
|
|
||||||
|
JSValue compressed_pixels = js_new_blob_stoned_copy(js, qoi_data, out_len);
|
||||||
|
free(qoi_data); // Free the QOI buffer after copying to blob
|
||||||
|
JS_SetPropertyStr(js, result, "pixels", compressed_pixels);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// QOI decompression/decoding
|
||||||
|
JSValue js_qoi_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
void *raw = js_get_blob_data(js, &len, argv[0]);
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not get QOI data from array buffer");
|
||||||
|
|
||||||
|
qoi_desc desc;
|
||||||
|
void *data = qoi_decode(raw, len, &desc, 0); // 0 means use channels from file
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return JS_NULL; // Return null if not valid QOI
|
||||||
|
|
||||||
|
// QOI always decodes to either RGB or RGBA based on the file's channel count
|
||||||
|
int channels = desc.channels;
|
||||||
|
int pitch = desc.width * channels;
|
||||||
|
size_t pixels_size = pitch * desc.height;
|
||||||
|
|
||||||
|
// If it's RGB, convert to RGBA for consistency
|
||||||
|
void *rgba_data = data;
|
||||||
|
if (channels == 3) {
|
||||||
|
rgba_data = malloc(desc.width * desc.height * 4);
|
||||||
|
if (!rgba_data) {
|
||||||
|
free(data);
|
||||||
|
return JS_ThrowOutOfMemory(js);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert RGB to RGBA
|
||||||
|
unsigned char *src = (unsigned char*)data;
|
||||||
|
unsigned char *dst = (unsigned char*)rgba_data;
|
||||||
|
for (int i = 0; i < desc.width * desc.height; i++) {
|
||||||
|
dst[i*4] = src[i*3];
|
||||||
|
dst[i*4+1] = src[i*3+1];
|
||||||
|
dst[i*4+2] = src[i*3+2];
|
||||||
|
dst[i*4+3] = 255;
|
||||||
|
}
|
||||||
|
free(data);
|
||||||
|
pitch = desc.width * 4;
|
||||||
|
pixels_size = pitch * desc.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create JS object with surface data
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, desc.width));
|
||||||
|
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, desc.height));
|
||||||
|
JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, pitch));
|
||||||
|
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, rgba_data, pixels_size));
|
||||||
|
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, 8));
|
||||||
|
JS_SetPropertyStr(js, obj, "hdr", JS_NewBool(js, 0));
|
||||||
|
JS_SetPropertyStr(js, obj, "colorspace", JS_NewInt32(js, desc.colorspace));
|
||||||
|
|
||||||
|
free(rgba_data);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_qoi_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(qoi, encode, 1),
|
||||||
|
MIST_FUNC_DEF(qoi, decode, 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
JSValue js_qoi_use(JSContext *js)
|
||||||
|
{
|
||||||
|
JSValue mod = JS_NewObject(js);
|
||||||
|
JS_SetPropertyFunctionList(js, mod, js_qoi_funcs, countof(js_qoi_funcs));
|
||||||
|
return mod;
|
||||||
|
}
|
||||||
1397
cute_aseprite.h
Normal file
1397
cute_aseprite.h
Normal file
File diff suppressed because it is too large
Load Diff
113
datastream.c
Normal file
113
datastream.c
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
#define PL_MPEG_IMPLEMENTATION
|
||||||
|
|
||||||
|
#include "datastream.h"
|
||||||
|
#include "cell.h"
|
||||||
|
#include "jsffi.h"
|
||||||
|
|
||||||
|
#include "limits.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "font.h"
|
||||||
|
#include "render.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "cbuf.h"
|
||||||
|
|
||||||
|
QJSCLASS(datastream,)
|
||||||
|
|
||||||
|
void datastream_free(JSRuntime *rt,datastream *ds)
|
||||||
|
{
|
||||||
|
plm_destroy(ds->plm);
|
||||||
|
free(ds);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct datastream *ds_openvideo(void *raw, size_t rawlen)
|
||||||
|
{
|
||||||
|
struct datastream *ds = malloc(sizeof(*ds));
|
||||||
|
void *newraw = malloc(rawlen);
|
||||||
|
memcpy(newraw,raw,rawlen);
|
||||||
|
ds->plm = plm_create_with_memory(newraw, rawlen, 1);
|
||||||
|
|
||||||
|
if (!ds->plm) {
|
||||||
|
free(ds);
|
||||||
|
free(newraw);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ds;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ds_advance(struct datastream *ds, double s) {
|
||||||
|
plm_decode(ds->plm, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ds_seek(struct datastream *ds, double time) {
|
||||||
|
plm_seek(ds->plm, time, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
double ds_length(struct datastream *ds) {
|
||||||
|
return plm_get_duration(ds->plm);
|
||||||
|
}
|
||||||
|
|
||||||
|
JSC_CCALL(datastream_time, return number2js(js,plm_get_time(js2datastream(js,self)->plm)); )
|
||||||
|
JSC_CCALL(datastream_seek, ds_seek(js2datastream(js,self), js2number(js,argv[0])))
|
||||||
|
JSC_CCALL(datastream_advance, ds_advance(js2datastream(js,self), js2number(js,argv[0])))
|
||||||
|
JSC_CCALL(datastream_duration, return number2js(js,ds_length(js2datastream(js,self))))
|
||||||
|
JSC_CCALL(datastream_framerate, return number2js(js,plm_get_framerate(js2datastream(js,self)->plm)))
|
||||||
|
|
||||||
|
JSC_GETSET_CALLBACK(datastream, callback)
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_datastream_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(datastream, time, 0),
|
||||||
|
MIST_FUNC_DEF(datastream, seek, 1),
|
||||||
|
MIST_FUNC_DEF(datastream, advance, 1),
|
||||||
|
MIST_FUNC_DEF(datastream, duration, 0),
|
||||||
|
MIST_FUNC_DEF(datastream, framerate, 0),
|
||||||
|
CGETSET_ADD(datastream, callback),
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static void render_frame(plm_t *mpeg, plm_frame_t *frame, void *d) {
|
||||||
|
datastream *ds = d;
|
||||||
|
if (JS_IsNull(ds->callback)) return;
|
||||||
|
uint8_t *rgb = malloc(frame->height*frame->width*4);
|
||||||
|
memset(rgb,255,frame->height*frame->width*4);
|
||||||
|
plm_frame_to_rgba(frame, rgb, frame->width*4);
|
||||||
|
|
||||||
|
// Create surface data object instead of SDL_Surface
|
||||||
|
JSValue surfData = JS_NewObject(ds->js);
|
||||||
|
JS_SetPropertyStr(ds->js, surfData, "width", JS_NewInt32(ds->js, frame->width));
|
||||||
|
JS_SetPropertyStr(ds->js, surfData, "height", JS_NewInt32(ds->js, frame->height));
|
||||||
|
JS_SetPropertyStr(ds->js, surfData, "format", JS_NewString(ds->js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(ds->js, surfData, "pitch", JS_NewInt32(ds->js, frame->width*4));
|
||||||
|
JS_SetPropertyStr(ds->js, surfData, "pixels", js_new_blob_stoned_copy(ds->js, rgb, frame->height*frame->width*4));
|
||||||
|
|
||||||
|
JSValue s[1];
|
||||||
|
s[0] = surfData;
|
||||||
|
JSValue cb = JS_DupValue(ds->js,ds->callback);
|
||||||
|
JSValue ret = JS_Call(ds->js, cb, JS_NULL, 1, s);
|
||||||
|
JS_FreeValue(ds->js,cb);
|
||||||
|
free(rgb);
|
||||||
|
uncaught_exception(ds->js,ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
JSC_CCALL(os_make_video,
|
||||||
|
size_t len;
|
||||||
|
void *data = js_get_blob_data(js,&len,argv[0]);
|
||||||
|
datastream *ds = ds_openvideo(data, len);
|
||||||
|
if (!ds) return JS_ThrowReferenceError(js, "Video file was not valid.");
|
||||||
|
ds->js = js;
|
||||||
|
ds->callback = JS_NULL;
|
||||||
|
plm_set_video_decode_callback(ds->plm, render_frame, ds);
|
||||||
|
return datastream2js(js,ds);
|
||||||
|
)
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_video_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(os, make_video, 1),
|
||||||
|
};
|
||||||
|
|
||||||
|
JSValue js_video_use(JSContext *js) {
|
||||||
|
JSValue mod = JS_NewObject(js);
|
||||||
|
JS_SetPropertyFunctionList(js,mod,js_video_funcs,countof(js_video_funcs));
|
||||||
|
return mod;
|
||||||
|
}
|
||||||
31
datastream.h
Normal file
31
datastream.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef DATASTREAM_H
|
||||||
|
#define DATASTREAM_H
|
||||||
|
|
||||||
|
#include "pl_mpeg.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "cell.h"
|
||||||
|
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
struct datastream {
|
||||||
|
plm_t *plm;
|
||||||
|
JSValue callback;
|
||||||
|
JSContext *js;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct datastream datastream;
|
||||||
|
|
||||||
|
void datastream_free(JSRuntime *rt,datastream *ds);
|
||||||
|
|
||||||
|
struct datastream *ds_openvideo(void *raw, size_t rawlen);
|
||||||
|
void ds_advance(struct datastream *ds, double); // advance time in seconds
|
||||||
|
void ds_seek(struct datastream *ds, double);
|
||||||
|
void ds_pause(struct datastream *ds);
|
||||||
|
void ds_stop(struct datastream *ds);
|
||||||
|
int ds_videodone(struct datastream *ds);
|
||||||
|
double ds_remainingtime(struct datastream *ds);
|
||||||
|
double ds_length(struct datastream *ds);
|
||||||
|
|
||||||
|
#endif
|
||||||
325
font.c
Normal file
325
font.c
Normal file
@@ -0,0 +1,325 @@
|
|||||||
|
#include "font.h"
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "render.h"
|
||||||
|
|
||||||
|
#include "stb_image_write.h"
|
||||||
|
#include "stb_rect_pack.h"
|
||||||
|
|
||||||
|
#define STB_TRUETYPE_IMPLEMENTATION
|
||||||
|
#define STB_TRUETYPE_NO_STDIO
|
||||||
|
#include "stb_truetype.h"
|
||||||
|
|
||||||
|
#define STB_DS_IMPLEMENTATION
|
||||||
|
#include "stb_ds.h"
|
||||||
|
|
||||||
|
#include "HandmadeMath.h"
|
||||||
|
|
||||||
|
typedef struct { const char *start, *end; float width; } line_t;
|
||||||
|
typedef struct { line_t *lines; float max_width; } layout_lines;
|
||||||
|
|
||||||
|
struct sFont *use_font;
|
||||||
|
|
||||||
|
void font_free(JSRuntime *rt, font *f)
|
||||||
|
{
|
||||||
|
if (f->surface) SDL_DestroySurface(f->surface);
|
||||||
|
free(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sFont *MakeFont(void *ttf_buffer, size_t len, int height) {
|
||||||
|
if (!ttf_buffer)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
int packsize = 1024;
|
||||||
|
|
||||||
|
struct sFont *newfont = calloc(1, sizeof(struct sFont));
|
||||||
|
newfont->height = height;
|
||||||
|
|
||||||
|
unsigned char *bitmap = malloc(packsize * packsize);
|
||||||
|
|
||||||
|
stbtt_packedchar glyphs[95];
|
||||||
|
|
||||||
|
stbtt_pack_context pc;
|
||||||
|
|
||||||
|
int pad = 2;
|
||||||
|
|
||||||
|
stbtt_PackBegin(&pc, bitmap, packsize, packsize, 0, pad, NULL);
|
||||||
|
stbtt_PackFontRange(&pc, ttf_buffer, 0, height, 32, 95, glyphs);
|
||||||
|
stbtt_PackEnd(&pc);
|
||||||
|
|
||||||
|
stbtt_fontinfo fontinfo;
|
||||||
|
if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer, 0))) {
|
||||||
|
// YughError("Failed to make font %s", fontfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ascent, descent, linegap;
|
||||||
|
|
||||||
|
stbtt_GetFontVMetrics(&fontinfo, &ascent, &descent, &linegap);
|
||||||
|
float s = stbtt_ScaleForPixelHeight(&fontinfo, height);
|
||||||
|
newfont->ascent = ascent * s;
|
||||||
|
newfont->descent = descent * s; /* descent is negative */
|
||||||
|
newfont->linegap = linegap * s;
|
||||||
|
newfont->line_height = (newfont->ascent - newfont->descent) + newfont->linegap;
|
||||||
|
newfont->surface = SDL_CreateSurface(packsize,packsize, SDL_PIXELFORMAT_RGBA32);
|
||||||
|
if (!newfont->surface) printf("SDL ERROR: %s\n", SDL_GetError());
|
||||||
|
for (int i = 0; i < packsize; i++)
|
||||||
|
for (int j = 0; j < packsize; j++)
|
||||||
|
if (!SDL_WriteSurfacePixel(newfont->surface, j, i, 255,255,255,bitmap[i*packsize+j]))
|
||||||
|
printf("SDLERROR: %s\n", SDL_GetError());
|
||||||
|
|
||||||
|
for (unsigned char c = 32; c < 127; c++) {
|
||||||
|
stbtt_packedchar glyph = glyphs[c - 32];
|
||||||
|
|
||||||
|
rect uv;
|
||||||
|
uv.x = (glyph.x0) / (float)packsize;
|
||||||
|
uv.w = (glyph.x1-glyph.x0) / (float)packsize;
|
||||||
|
uv.y = (glyph.y1) / (float)packsize;
|
||||||
|
uv.h = (glyph.y0-glyph.y1) / (float)packsize;
|
||||||
|
newfont->Characters[c].uv = uv;
|
||||||
|
|
||||||
|
rect quad;
|
||||||
|
quad.x = glyph.xoff;
|
||||||
|
quad.y = -glyph.yoff2; // Top of glyph relative to baseline
|
||||||
|
quad.w = glyph.xoff2 - glyph.xoff;
|
||||||
|
quad.h = glyph.yoff2 - glyph.yoff; // Height from baseline
|
||||||
|
newfont->Characters[c].quad = quad;
|
||||||
|
newfont->Characters[c].advance = glyph.xadvance;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(bitmap);
|
||||||
|
|
||||||
|
return newfont;
|
||||||
|
}
|
||||||
|
|
||||||
|
layout_lines layout_text_lines(const char *text, font *f,
|
||||||
|
float letter_spacing,
|
||||||
|
float wrap,
|
||||||
|
TEXT_BREAK break_at)
|
||||||
|
{
|
||||||
|
layout_lines out = {0};
|
||||||
|
int break_at_word = (break_at == WORD);
|
||||||
|
|
||||||
|
const char *line_start = text;
|
||||||
|
const char *word_start = text;
|
||||||
|
float line_width = 0;
|
||||||
|
float word_width = 0;
|
||||||
|
|
||||||
|
float max_width = 0;
|
||||||
|
|
||||||
|
for (const char *p = text; *p; p++) {
|
||||||
|
if (*p == '\n') {
|
||||||
|
line_t L = { line_start, p, line_width };
|
||||||
|
arrput(out.lines, L);
|
||||||
|
if (line_width > max_width) max_width = line_width;
|
||||||
|
|
||||||
|
line_start = p + 1;
|
||||||
|
word_start = p + 1;
|
||||||
|
line_width = 0;
|
||||||
|
word_width = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char ch = (unsigned char)*p;
|
||||||
|
float char_w = f->Characters[ch].advance + letter_spacing;
|
||||||
|
|
||||||
|
if (wrap > 0 && line_width + char_w > wrap) {
|
||||||
|
const char *break_pt = p;
|
||||||
|
float break_w = line_width;
|
||||||
|
|
||||||
|
if (break_at_word && *p != ' ' && word_width > 0 && word_start > line_start) {
|
||||||
|
break_pt = word_start;
|
||||||
|
break_w = line_width - word_width;
|
||||||
|
}
|
||||||
|
|
||||||
|
line_t L = { line_start, break_pt, break_w };
|
||||||
|
arrput(out.lines, L);
|
||||||
|
if (break_w > max_width) max_width = break_w;
|
||||||
|
|
||||||
|
line_start = break_pt;
|
||||||
|
if (break_at_word && *line_start == ' ') line_start++;
|
||||||
|
|
||||||
|
p = line_start - 1;
|
||||||
|
word_start = line_start;
|
||||||
|
line_width = 0;
|
||||||
|
word_width = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
line_width += char_w;
|
||||||
|
|
||||||
|
if (break_at_word) {
|
||||||
|
if (*p == ' ') {
|
||||||
|
word_width = 0;
|
||||||
|
word_start = p + 1;
|
||||||
|
} else {
|
||||||
|
word_width += char_w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line_start < text + strlen(text)) {
|
||||||
|
line_t L = { line_start, text + strlen(text), line_width };
|
||||||
|
arrput(out.lines, L);
|
||||||
|
if (line_width > max_width) max_width = line_width;
|
||||||
|
}
|
||||||
|
|
||||||
|
out.max_width = max_width;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sdrawCharacter(struct text_vert **buffer, stbtt_packedchar c, HMM_Vec2 cursor, float scale, struct rgba color) {
|
||||||
|
struct text_vert vert;
|
||||||
|
|
||||||
|
// vert.pos.x = cursor.X + c.leftbearing;
|
||||||
|
// vert.pos.y = cursor.Y + c.topbearing;
|
||||||
|
// vert.wh = c.size;
|
||||||
|
|
||||||
|
// if (vert.pos.x > frame.l || vert.pos.y > frame.t || (vert.pos.y + vert.wh.y) < frame.b || (vert.pos.x + vert.wh.x) < frame.l) return;
|
||||||
|
|
||||||
|
// vert.uv.x = c.rect.x;
|
||||||
|
// vert.uv.y = c.rect.y;
|
||||||
|
// vert.st.x = c.rect.w;
|
||||||
|
// vert.st.y = c.rect.h;
|
||||||
|
rgba2floats(vert.color.e, color);
|
||||||
|
|
||||||
|
arrput(*buffer, vert);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_char_verts(struct text_vert **buffer, struct character c, HMM_Vec2 cursor, colorf color)
|
||||||
|
{
|
||||||
|
// packedchar has
|
||||||
|
// Adds four verts: bottom left, bottom right, top left, top right
|
||||||
|
text_vert bl;
|
||||||
|
bl.pos.x = cursor.X + c.quad.x;
|
||||||
|
bl.pos.y = cursor.Y + c.quad.y;
|
||||||
|
bl.uv.x = c.uv.x;
|
||||||
|
bl.uv.y = c.uv.y;
|
||||||
|
bl.color = color;
|
||||||
|
arrput(*buffer, bl);
|
||||||
|
|
||||||
|
|
||||||
|
text_vert br = bl;
|
||||||
|
br.pos.x += c.quad.w;
|
||||||
|
br.uv.x += c.uv.w;
|
||||||
|
arrput(*buffer, br);
|
||||||
|
|
||||||
|
text_vert ul = bl;
|
||||||
|
ul.pos.y += c.quad.h;
|
||||||
|
ul.uv.y += c.uv.h;
|
||||||
|
arrput(*buffer, ul);
|
||||||
|
|
||||||
|
text_vert ur = ul;
|
||||||
|
ur.pos.x = br.pos.x;
|
||||||
|
ur.uv.x = br.uv.x;
|
||||||
|
arrput(*buffer, ur);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *esc_color(const char *c, struct rgba *color, struct rgba defc)
|
||||||
|
{
|
||||||
|
struct rgba d;
|
||||||
|
if (!color) color = &d;
|
||||||
|
if (*c != '\033') return c;
|
||||||
|
c++;
|
||||||
|
if (*c != '[') return c;
|
||||||
|
c++;
|
||||||
|
if (*c == '0') {
|
||||||
|
*color = defc;
|
||||||
|
c++;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
else if (!strncmp(c, "38;2;", 5)) {
|
||||||
|
c += 5;
|
||||||
|
*color = (struct rgba){0,0,0,255};
|
||||||
|
color->r = atoi(c);
|
||||||
|
c = strchr(c, ';')+1;
|
||||||
|
color->g = atoi(c);
|
||||||
|
c = strchr(c,';')+1;
|
||||||
|
color->b = atoi(c);
|
||||||
|
c = strchr(c,';')+1;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
// text is a string, font f, wrap is how long a line is before wrapping. -1 to not wrap
|
||||||
|
HMM_Vec2 measure_text(const char *text, font *f, float letter_spacing, float wrap, TEXT_BREAK break_at, TEXT_ALIGN align)
|
||||||
|
{
|
||||||
|
layout_lines lay = layout_text_lines(text, f, letter_spacing, wrap, break_at);
|
||||||
|
|
||||||
|
float lh = f->line_height;
|
||||||
|
int line_count = arrlen(lay.lines);
|
||||||
|
float height = line_count > 0 ? line_count * lh : 0;
|
||||||
|
|
||||||
|
float width = lay.max_width;
|
||||||
|
if (wrap > 0 && align != LEFT && align != JUSTIFY) {
|
||||||
|
// For RIGHT or CENTER we might shift lines visually, but width is still max(line.width, wrap ? min(wrap, ...) ).
|
||||||
|
// Usually you'd report max(wrap, width) or just wrap, depending on how you want containers to size.
|
||||||
|
}
|
||||||
|
|
||||||
|
HMM_Vec2 dim = { width, height };
|
||||||
|
|
||||||
|
arrfree(lay.lines);
|
||||||
|
return dim;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* pos given in screen coordinates */
|
||||||
|
struct text_vert *renderText(const char *text, HMM_Vec2 pos, font *f, colorf color,
|
||||||
|
float wrap, TEXT_BREAK break_at, TEXT_ALIGN align,
|
||||||
|
float letter_spacing)
|
||||||
|
{
|
||||||
|
text_vert *buffer = NULL;
|
||||||
|
float lh = f->line_height;
|
||||||
|
|
||||||
|
layout_lines lay = layout_text_lines(text, f, letter_spacing, wrap, break_at);
|
||||||
|
line_t *lines = lay.lines;
|
||||||
|
|
||||||
|
HMM_Vec2 cursor = pos;
|
||||||
|
|
||||||
|
for (int i = 0; i < arrlen(lines); i++) {
|
||||||
|
line_t *L = &lines[i];
|
||||||
|
float start_x = pos.x;
|
||||||
|
float extra_space = 0;
|
||||||
|
int space_count = 0;
|
||||||
|
|
||||||
|
if (wrap > 0) {
|
||||||
|
switch (align) {
|
||||||
|
case RIGHT: start_x = pos.x + wrap - L->width; break;
|
||||||
|
case CENTER: start_x = pos.x + (wrap - L->width) * 0.5f; break;
|
||||||
|
case JUSTIFY:
|
||||||
|
if (i < arrlen(lines) - 1 && *(L->end) != '\n') {
|
||||||
|
for (const char *p = L->start; p < L->end; p++) if (*p == ' ') space_count++;
|
||||||
|
if (space_count > 0) extra_space = (wrap - L->width) / space_count;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LEFT: default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor.x = start_x;
|
||||||
|
|
||||||
|
const char *p = L->start;
|
||||||
|
while (p < L->end) {
|
||||||
|
unsigned char ch = (unsigned char)*p;
|
||||||
|
struct character g = f->Characters[ch];
|
||||||
|
|
||||||
|
if (!isspace(*p)) draw_char_verts(&buffer, g, cursor, color);
|
||||||
|
|
||||||
|
cursor.x += g.advance;
|
||||||
|
/* add letter spacing unless this is last char of the line */
|
||||||
|
if (p + 1 < L->end) cursor.x += letter_spacing;
|
||||||
|
if (align == JUSTIFY && *p == ' ') cursor.x += extra_space;
|
||||||
|
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor.x = pos.x;
|
||||||
|
cursor.y -= lh;
|
||||||
|
}
|
||||||
|
|
||||||
|
arrfree(lines);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
69
font.h
Normal file
69
font.h
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#ifndef FONT_H
|
||||||
|
#define FONT_H
|
||||||
|
|
||||||
|
#include "render.h"
|
||||||
|
#include "HandmadeMath.h"
|
||||||
|
#include "cell.h"
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include "render.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
LEFT,
|
||||||
|
RIGHT,
|
||||||
|
CENTER,
|
||||||
|
JUSTIFY
|
||||||
|
} TEXT_ALIGN;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
WORD,
|
||||||
|
CHARACTER
|
||||||
|
} TEXT_BREAK;
|
||||||
|
|
||||||
|
struct text_vert {
|
||||||
|
HMM_Vec2 pos;
|
||||||
|
HMM_Vec2 uv;
|
||||||
|
HMM_Vec4 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct text_vert text_vert;
|
||||||
|
|
||||||
|
struct text_char {
|
||||||
|
rect pos;
|
||||||
|
rect uv;
|
||||||
|
HMM_Vec4 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct shader;
|
||||||
|
struct window;
|
||||||
|
|
||||||
|
struct character {
|
||||||
|
float advance;
|
||||||
|
rect quad;
|
||||||
|
rect uv;
|
||||||
|
float xoff;
|
||||||
|
float yoff;
|
||||||
|
float width;
|
||||||
|
float height;
|
||||||
|
};
|
||||||
|
|
||||||
|
// text data
|
||||||
|
struct sFont {
|
||||||
|
uint32_t height; /* in pixels */
|
||||||
|
float ascent; // pixels
|
||||||
|
float descent; // pixels
|
||||||
|
float linegap; //pixels
|
||||||
|
float line_height; // pixels
|
||||||
|
struct character Characters[256];
|
||||||
|
SDL_Surface *surface;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct sFont font;
|
||||||
|
typedef struct Character glyph;
|
||||||
|
|
||||||
|
void font_free(JSRuntime *rt,font *f);
|
||||||
|
|
||||||
|
struct sFont *MakeFont(void *data, size_t len, int height);
|
||||||
|
struct text_vert *renderText(const char *text, HMM_Vec2 pos, font *f, colorf color, float wrap, TEXT_BREAK breakAt, TEXT_ALIGN align, float letter_spacing);
|
||||||
|
HMM_Vec2 measure_text(const char *text, font *f, float letterSpacing, float wrap, TEXT_BREAK breakAt, TEXT_ALIGN align);
|
||||||
|
|
||||||
|
#endif
|
||||||
287
graphics.c
Normal file
287
graphics.c
Normal file
@@ -0,0 +1,287 @@
|
|||||||
|
#include "cell.h"
|
||||||
|
#include "jsffi.h"
|
||||||
|
#include "HandmadeMath.h"
|
||||||
|
#include "datastream.h"
|
||||||
|
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#define STBI_FAILURE_USERMSG
|
||||||
|
#define STBI_NO_STDIO
|
||||||
|
#include "stb_image.h"
|
||||||
|
|
||||||
|
#define STB_RECT_PACK_IMPLEMENTATION
|
||||||
|
#include "stb_rect_pack.h"
|
||||||
|
|
||||||
|
#define CUTE_ASEPRITE_IMPLEMENTATION
|
||||||
|
#include "cute_aseprite.h"
|
||||||
|
|
||||||
|
// input: (encoded image data of jpg, png, bmp, tiff)
|
||||||
|
JSC_CCALL(os_image_decode,
|
||||||
|
size_t len;
|
||||||
|
void *raw = js_get_blob_data(js, &len, argv[0]);
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not load texture with array buffer");
|
||||||
|
|
||||||
|
int n, width, height;
|
||||||
|
void *data = stbi_load_from_memory(raw, len, &width, &height, &n, 4);
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return JS_ThrowReferenceError(js, "no known image type from pixel data: %s", stbi_failure_reason());
|
||||||
|
|
||||||
|
if (width <= 0 || height <= 0) {
|
||||||
|
free(data);
|
||||||
|
return JS_ThrowReferenceError(js, "decoded image has invalid size: %dx%d", width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pitch = width*4;
|
||||||
|
size_t pixels_size = pitch * height;
|
||||||
|
|
||||||
|
// Create JS object with surface data
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, pitch));
|
||||||
|
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, data, pixels_size));
|
||||||
|
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, 8));
|
||||||
|
JS_SetPropertyStr(js, obj, "hdr", JS_NewBool(js,0));
|
||||||
|
|
||||||
|
free(data);
|
||||||
|
ret = obj;
|
||||||
|
)
|
||||||
|
|
||||||
|
JSC_CCALL(graphics_hsl_to_rgb,
|
||||||
|
float h, s, l;
|
||||||
|
JS_ToFloat64(js, &h, argv[0]);
|
||||||
|
JS_ToFloat64(js, &s, argv[1]);
|
||||||
|
JS_ToFloat64(js, &l, argv[2]);
|
||||||
|
float c = (1 - abs(2 * l - 1)) * s;
|
||||||
|
float x = c * (1 - abs(fmod((h/60),2) - 1));
|
||||||
|
float m = l - c / 2;
|
||||||
|
float r = 0, g = 0, b = 0;
|
||||||
|
|
||||||
|
if (h < 60) { r = c; g = x; }
|
||||||
|
else if (h < 120) { r = x; g = c; }
|
||||||
|
else if (h < 180) { g = c; b = x; }
|
||||||
|
else if (h < 240) { g = x; b = c; }
|
||||||
|
else if (h < 300) { r = x; b = c; }
|
||||||
|
else { r = c; b = x; }
|
||||||
|
|
||||||
|
return color2js(js, (colorf){r+m, g+m, b+m, 1});
|
||||||
|
)
|
||||||
|
|
||||||
|
JSC_CCALL(os_make_line_prim,
|
||||||
|
return JS_NULL;
|
||||||
|
/*
|
||||||
|
JSValue prim = JS_NewObject(js);
|
||||||
|
HMM_Vec2 *v = js2cpvec2arr(js,argv[0]);
|
||||||
|
|
||||||
|
parsl_context *par_ctx = parsl_create_context((parsl_config){
|
||||||
|
.thickness = js2number(js,argv[1]),
|
||||||
|
.flags= PARSL_FLAG_ANNOTATIONS,
|
||||||
|
.u_mode = js2number(js,argv[2])
|
||||||
|
});
|
||||||
|
|
||||||
|
uint16_t spine_lens[] = {arrlen(v)};
|
||||||
|
|
||||||
|
parsl_mesh *m = parsl_mesh_from_lines(par_ctx, (parsl_spine_list){
|
||||||
|
.num_vertices = arrlen(v),
|
||||||
|
.num_spines = 1,
|
||||||
|
.vertices = v,
|
||||||
|
.spine_lengths = spine_lens,
|
||||||
|
.closed = JS_ToBool(js,argv[3])
|
||||||
|
});
|
||||||
|
|
||||||
|
JS_SetPropertyStr(js, prim, "pos", make_gpu_buffer(js,m->positions,sizeof(*m->positions)*m->num_vertices, 0, 2,1,0));
|
||||||
|
|
||||||
|
JS_SetPropertyStr(js, prim, "indices", make_gpu_buffer(js,m->triangle_indices,sizeof(*m->triangle_indices)*m->num_triangles*3, JS_TYPED_ARRAY_UINT32, 1,1,1));
|
||||||
|
|
||||||
|
float uv[m->num_vertices*2];
|
||||||
|
for (int i = 0; i < m->num_vertices; i++) {
|
||||||
|
uv[i*2] = m->annotations[i].u_along_curve;
|
||||||
|
uv[i*2+1] = m->annotations[i].v_across_curve;
|
||||||
|
}
|
||||||
|
|
||||||
|
JS_SetPropertyStr(js, prim, "uv", make_gpu_buffer(js, uv, sizeof(uv), 0,2,1,0));
|
||||||
|
JS_SetPropertyStr(js,prim,"vertices", number2js(js,m->num_vertices));
|
||||||
|
JS_SetPropertyStr(js,prim,"num_indices", number2js(js,m->num_triangles*3));
|
||||||
|
JS_SetPropertyStr(js,prim,"first_index", number2js(js,0));
|
||||||
|
|
||||||
|
parsl_destroy_context(par_ctx);
|
||||||
|
|
||||||
|
return prim;
|
||||||
|
*/
|
||||||
|
)
|
||||||
|
|
||||||
|
JSC_CCALL(os_rectpack,
|
||||||
|
int width = js2number(js,argv[0]);
|
||||||
|
int height = js2number(js,argv[1]);
|
||||||
|
int num = JS_ArrayLength(js,argv[2]);
|
||||||
|
stbrp_context ctx[1];
|
||||||
|
stbrp_rect rects[num];
|
||||||
|
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
HMM_Vec2 wh = js2vec2(js,JS_GetPropertyUint32(js, argv[2], i));
|
||||||
|
rects[i].w = wh.x;
|
||||||
|
rects[i].h = wh.y;
|
||||||
|
rects[i].id = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
stbrp_node nodes[width];
|
||||||
|
stbrp_init_target(ctx, width, height, nodes, width);
|
||||||
|
int packed = stbrp_pack_rects(ctx, rects, num);
|
||||||
|
|
||||||
|
if (!packed) {
|
||||||
|
return JS_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = JS_NewArray(js);
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
HMM_Vec2 pos;
|
||||||
|
pos.x = rects[i].x;
|
||||||
|
pos.y = rects[i].y;
|
||||||
|
JS_SetPropertyUint32(js, ret, i, vec22js(js,pos));
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
// input: (gif image data)
|
||||||
|
JSC_CCALL(os_make_gif,
|
||||||
|
size_t rawlen;
|
||||||
|
void *raw = js_get_blob_data(js, &rawlen, argv[0]);
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not load gif from supplied array buffer");
|
||||||
|
|
||||||
|
int n;
|
||||||
|
int frames;
|
||||||
|
int *delays;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
void *pixels = stbi_load_gif_from_memory(raw, rawlen, &delays, &width, &height, &frames, &n, 4);
|
||||||
|
|
||||||
|
if (!pixels) {
|
||||||
|
return JS_ThrowReferenceError(js, "1decode GIF: %s", stbi_failure_reason());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always return an array of surfaces, even for single frame
|
||||||
|
JSValue surface_array = JS_NewArray(js);
|
||||||
|
ret = surface_array;
|
||||||
|
|
||||||
|
for (int i = 0; i < frames; i++) {
|
||||||
|
// Create surface data object
|
||||||
|
JSValue surfData = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, surfData, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, surfData, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, surfData, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, surfData, "pitch", JS_NewInt32(js, width*4));
|
||||||
|
|
||||||
|
void *frame_pixels = (unsigned char*)pixels+(width*height*4*i);
|
||||||
|
JS_SetPropertyStr(js, surfData, "pixels", js_new_blob_stoned_copy(js, frame_pixels, width*height*4));
|
||||||
|
|
||||||
|
// Add time property for animation frames
|
||||||
|
if (frames > 1 && delays) {
|
||||||
|
JS_SetPropertyStr(js, surfData, "time", number2js(js,(float)delays[i]/1000.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
JS_SetPropertyUint32(js, surface_array, i, surfData);
|
||||||
|
}
|
||||||
|
|
||||||
|
CLEANUP:
|
||||||
|
if (delays) free(delays);
|
||||||
|
if (pixels) free(pixels);
|
||||||
|
)
|
||||||
|
|
||||||
|
JSValue aseframe2js(JSContext *js, ase_frame_t aframe)
|
||||||
|
{
|
||||||
|
JSValue frame = JS_NewObject(js);
|
||||||
|
|
||||||
|
// Create surface data object instead of SDL_Surface
|
||||||
|
JSValue surfData = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, surfData, "width", JS_NewInt32(js, aframe.ase->w));
|
||||||
|
JS_SetPropertyStr(js, surfData, "height", JS_NewInt32(js, aframe.ase->h));
|
||||||
|
JS_SetPropertyStr(js, surfData, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, surfData, "pitch", JS_NewInt32(js, aframe.ase->w*4));
|
||||||
|
JS_SetPropertyStr(js, surfData, "pixels", js_new_blob_stoned_copy(js, aframe.pixels, aframe.ase->w*aframe.ase->h*4));
|
||||||
|
|
||||||
|
JS_SetPropertyStr(js, frame, "surface", surfData);
|
||||||
|
JS_SetPropertyStr(js, frame, "time", number2js(js,(float)aframe.duration_milliseconds/1000.0));
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
// input: (aseprite data)
|
||||||
|
JSC_CCALL(os_make_aseprite,
|
||||||
|
size_t rawlen;
|
||||||
|
void *raw = js_get_blob_data(js,&rawlen,argv[0]);
|
||||||
|
|
||||||
|
ase_t *ase = cute_aseprite_load_from_memory(raw, rawlen, NULL);
|
||||||
|
|
||||||
|
if (!ase)
|
||||||
|
return JS_ThrowReferenceError(js, "could not load aseprite from supplied array buffer: %s", aseprite_GetError());
|
||||||
|
|
||||||
|
if (ase->tag_count == 0) {
|
||||||
|
// we're dealing with a single frame image, or single animation
|
||||||
|
if (ase->frame_count == 1) {
|
||||||
|
JSValue obj = aseframe2js(js,ase->frames[0]);
|
||||||
|
cute_aseprite_free(ase);
|
||||||
|
return obj;
|
||||||
|
} else {
|
||||||
|
// Multiple frames but no tags - create a simple animation
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
JSValue frames = JS_NewArray(js);
|
||||||
|
for (int f = 0; f < ase->frame_count; f++) {
|
||||||
|
JSValue frame = aseframe2js(js,ase->frames[f]);
|
||||||
|
JS_SetPropertyUint32(js, frames, f, frame);
|
||||||
|
}
|
||||||
|
JS_SetPropertyStr(js, obj, "frames", frames);
|
||||||
|
JS_SetPropertyStr(js, obj, "loop", JS_NewBool(js, true));
|
||||||
|
ret = obj;
|
||||||
|
cute_aseprite_free(ase);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
|
||||||
|
for (int t = 0; t < ase->tag_count; t++) {
|
||||||
|
ase_tag_t tag = ase->tags[t];
|
||||||
|
JSValue anim = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, anim, "repeat", number2js(js,tag.repeat));
|
||||||
|
switch(tag.loop_animation_direction) {
|
||||||
|
case ASE_ANIMATION_DIRECTION_FORWARDS:
|
||||||
|
JS_SetPropertyStr(js, anim, "loop", JS_NewString(js,"forward"));
|
||||||
|
break;
|
||||||
|
case ASE_ANIMATION_DIRECTION_BACKWORDS:
|
||||||
|
JS_SetPropertyStr(js, anim, "loop", JS_NewString(js,"backward"));
|
||||||
|
break;
|
||||||
|
case ASE_ANIMATION_DIRECTION_PINGPONG:
|
||||||
|
JS_SetPropertyStr(js, anim, "loop", JS_NewString(js,"pingpong"));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _frame = 0;
|
||||||
|
JSValue frames = JS_NewArray(js);
|
||||||
|
for (int f = tag.from_frame; f <= tag.to_frame; f++) {
|
||||||
|
JSValue frame = aseframe2js(js,ase->frames[f]);
|
||||||
|
JS_SetPropertyUint32(js, frames, _frame, frame);
|
||||||
|
_frame++;
|
||||||
|
}
|
||||||
|
JS_SetPropertyStr(js, anim, "frames", frames);
|
||||||
|
JS_SetPropertyStr(js, obj, tag.name, anim);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = obj;
|
||||||
|
|
||||||
|
cute_aseprite_free(ase);
|
||||||
|
)
|
||||||
|
|
||||||
|
const JSCFunctionListEntry js_graphics_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(os, rectpack, 3),
|
||||||
|
MIST_FUNC_DEF(os, image_decode, 1),
|
||||||
|
MIST_FUNC_DEF(os, make_gif, 1),
|
||||||
|
MIST_FUNC_DEF(os, make_aseprite, 1),
|
||||||
|
MIST_FUNC_DEF(os, make_line_prim, 5),
|
||||||
|
MIST_FUNC_DEF(graphics, hsl_to_rgb, 3),
|
||||||
|
};
|
||||||
|
|
||||||
|
JSValue js_graphics_use(JSContext *js) {
|
||||||
|
JSValue mod = JS_NewObject(js);
|
||||||
|
JS_SetPropertyFunctionList(js,mod,js_graphics_funcs,countof(js_graphics_funcs));
|
||||||
|
return mod;
|
||||||
|
}
|
||||||
1107
imgui/GraphEditor.cpp
Normal file
1107
imgui/GraphEditor.cpp
Normal file
File diff suppressed because it is too large
Load Diff
151
imgui/GraphEditor.h
Normal file
151
imgui/GraphEditor.h
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
// https://github.com/CedricGuillemet/ImGuizmo
|
||||||
|
// v 1.89 WIP
|
||||||
|
//
|
||||||
|
// The MIT License(MIT)
|
||||||
|
//
|
||||||
|
// Copyright(c) 2021 Cedric Guillemet
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files(the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions :
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string>
|
||||||
|
#include "imgui.h"
|
||||||
|
#include "imgui_internal.h"
|
||||||
|
|
||||||
|
namespace GraphEditor {
|
||||||
|
|
||||||
|
typedef size_t NodeIndex;
|
||||||
|
typedef size_t SlotIndex;
|
||||||
|
typedef size_t LinkIndex;
|
||||||
|
typedef size_t TemplateIndex;
|
||||||
|
|
||||||
|
// Force the view to be respositionned and zoom to fit nodes with Show function.
|
||||||
|
// Parameter value will be changed to Fit_None by the function.
|
||||||
|
enum FitOnScreen
|
||||||
|
{
|
||||||
|
Fit_None,
|
||||||
|
Fit_AllNodes,
|
||||||
|
Fit_SelectedNodes
|
||||||
|
};
|
||||||
|
|
||||||
|
// Display options and colors
|
||||||
|
struct Options
|
||||||
|
{
|
||||||
|
ImRect mMinimap{{0.75f, 0.8f, 0.99f, 0.99f}}; // rectangle coordinates of minimap
|
||||||
|
ImU32 mBackgroundColor{ IM_COL32(40, 40, 40, 255) }; // full background color
|
||||||
|
ImU32 mGridColor{ IM_COL32(0, 0, 0, 60) }; // grid lines color
|
||||||
|
ImU32 mGridColor2{ IM_COL32(0, 0, 0, 160) }; // grid lines color every 10th
|
||||||
|
ImU32 mSelectedNodeBorderColor{ IM_COL32(255, 130, 30, 255) }; // node border color when it's selected
|
||||||
|
ImU32 mNodeBorderColor{ IM_COL32(100, 100, 100, 0) }; // node border color when it's not selected
|
||||||
|
ImU32 mQuadSelection{ IM_COL32(255, 32, 32, 64) }; // quad selection inside color
|
||||||
|
ImU32 mQuadSelectionBorder{ IM_COL32(255, 32, 32, 255) }; // quad selection border color
|
||||||
|
ImU32 mDefaultSlotColor{ IM_COL32(128, 128, 128, 255) }; // when no color is provided in node template, use this value
|
||||||
|
ImU32 mFrameFocus{ IM_COL32(64, 128, 255, 255) }; // rectangle border when graph editor has focus
|
||||||
|
float mLineThickness{ 5 }; // links width in pixels when zoom value is 1
|
||||||
|
float mGridSize{ 64.f }; // background grid size in pixels when zoom value is 1
|
||||||
|
float mRounding{ 3.f }; // rounding at node corners
|
||||||
|
float mZoomRatio{ 0.1f }; // factor per mouse wheel delta
|
||||||
|
float mZoomLerpFactor{ 0.25f }; // the smaller, the smoother
|
||||||
|
float mBorderSelectionThickness{ 6.f }; // thickness of selection border around nodes
|
||||||
|
float mBorderThickness{ 6.f }; // thickness of selection border around nodes
|
||||||
|
float mNodeSlotRadius{ 8.f }; // circle radius for inputs and outputs
|
||||||
|
float mNodeSlotHoverFactor{ 1.2f }; // increase size when hovering
|
||||||
|
float mMinZoom{ 0.2f }, mMaxZoom { 1.1f };
|
||||||
|
float mSnap{ 5.f };
|
||||||
|
bool mDisplayLinksAsCurves{ true }; // false is straight and 45deg lines
|
||||||
|
bool mAllowQuadSelection{ true }; // multiple selection using drag and drop
|
||||||
|
bool mRenderGrid{ true }; // grid or nothing
|
||||||
|
bool mDrawIONameOnHover{ true }; // only draw node input/output when hovering
|
||||||
|
};
|
||||||
|
|
||||||
|
// View state: scroll position and zoom factor
|
||||||
|
struct ViewState
|
||||||
|
{
|
||||||
|
ImVec2 mPosition{0.0f, 0.0f}; // scroll position
|
||||||
|
float mFactor{ 1.0f }; // current zoom factor
|
||||||
|
float mFactorTarget{ 1.0f }; // targeted zoom factor interpolated using Options.mZoomLerpFactor
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Template
|
||||||
|
{
|
||||||
|
ImU32 mHeaderColor;
|
||||||
|
ImU32 mBackgroundColor;
|
||||||
|
ImU32 mBackgroundColorOver;
|
||||||
|
ImU8 mInputCount;
|
||||||
|
const char** mInputNames; // can be nullptr. No text displayed.
|
||||||
|
ImU32* mInputColors; // can be nullptr, default slot color will be used.
|
||||||
|
ImU8 mOutputCount;
|
||||||
|
const char** mOutputNames; // can be nullptr. No text displayed.
|
||||||
|
ImU32* mOutputColors; // can be nullptr, default slot color will be used.
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Node
|
||||||
|
{
|
||||||
|
const char* mName;
|
||||||
|
TemplateIndex mTemplateIndex;
|
||||||
|
ImRect mRect;
|
||||||
|
bool mSelected{ false };
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Link
|
||||||
|
{
|
||||||
|
NodeIndex mInputNodeIndex;
|
||||||
|
SlotIndex mInputSlotIndex;
|
||||||
|
NodeIndex mOutputNodeIndex;
|
||||||
|
SlotIndex mOutputSlotIndex;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Delegate
|
||||||
|
{
|
||||||
|
virtual bool AllowedLink(NodeIndex from, NodeIndex to) = 0;
|
||||||
|
|
||||||
|
virtual void SelectNode(NodeIndex nodeIndex, bool selected) = 0;
|
||||||
|
virtual void MoveSelectedNodes(const ImVec2 delta) = 0;
|
||||||
|
|
||||||
|
virtual void AddLink(NodeIndex inputNodeIndex, SlotIndex inputSlotIndex, NodeIndex outputNodeIndex, SlotIndex outputSlotIndex) = 0;
|
||||||
|
virtual void DelLink(LinkIndex linkIndex) = 0;
|
||||||
|
|
||||||
|
// user is responsible for clipping
|
||||||
|
virtual void CustomDraw(ImDrawList* drawList, ImRect rectangle, NodeIndex nodeIndex) = 0;
|
||||||
|
|
||||||
|
// use mouse position to open context menu
|
||||||
|
// if nodeIndex != -1, right click happens on the specified node
|
||||||
|
virtual void RightClick(NodeIndex nodeIndex, SlotIndex slotIndexInput, SlotIndex slotIndexOutput) = 0;
|
||||||
|
|
||||||
|
virtual const size_t GetTemplateCount() = 0;
|
||||||
|
virtual const Template GetTemplate(TemplateIndex index) = 0;
|
||||||
|
|
||||||
|
virtual const size_t GetNodeCount() = 0;
|
||||||
|
virtual const Node GetNode(NodeIndex index) = 0;
|
||||||
|
|
||||||
|
virtual const size_t GetLinkCount() = 0;
|
||||||
|
virtual const Link GetLink(LinkIndex index) = 0;
|
||||||
|
|
||||||
|
virtual ~Delegate() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
void Show(Delegate& delegate, const Options& options, ViewState& viewState, bool enabled, FitOnScreen* fit = nullptr);
|
||||||
|
void GraphEditorClear();
|
||||||
|
|
||||||
|
bool EditOptions(Options& options);
|
||||||
|
|
||||||
|
} // namespace
|
||||||
457
imgui/ImCurveEdit.cpp
Normal file
457
imgui/ImCurveEdit.cpp
Normal file
@@ -0,0 +1,457 @@
|
|||||||
|
// https://github.com/CedricGuillemet/ImGuizmo
|
||||||
|
// v 1.89 WIP
|
||||||
|
//
|
||||||
|
// The MIT License(MIT)
|
||||||
|
//
|
||||||
|
// Copyright(c) 2021 Cedric Guillemet
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files(the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions :
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
#include "ImCurveEdit.h"
|
||||||
|
#include "imgui.h"
|
||||||
|
#include "imgui_internal.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <set>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||||
|
#include <malloc.h>
|
||||||
|
#endif
|
||||||
|
#if !defined(_MSC_VER) && !defined(__MINGW64_VERSION_MAJOR)
|
||||||
|
#define _malloca(x) alloca(x)
|
||||||
|
#define _freea(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace ImCurveEdit
|
||||||
|
{
|
||||||
|
|
||||||
|
#ifndef IMGUI_DEFINE_MATH_OPERATORS
|
||||||
|
static ImVec2 operator+(const ImVec2& a, const ImVec2& b) {
|
||||||
|
return ImVec2(a.x + b.x, a.y + b.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ImVec2 operator-(const ImVec2& a, const ImVec2& b) {
|
||||||
|
return ImVec2(a.x - b.x, a.y - b.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ImVec2 operator*(const ImVec2& a, const ImVec2& b) {
|
||||||
|
return ImVec2(a.x * b.x, a.y * b.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ImVec2 operator/(const ImVec2& a, const ImVec2& b) {
|
||||||
|
return ImVec2(a.x / b.x, a.y / b.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ImVec2 operator*(const ImVec2& a, const float b) {
|
||||||
|
return ImVec2(a.x * b, a.y * b);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static float smoothstep(float edge0, float edge1, float x)
|
||||||
|
{
|
||||||
|
x = ImClamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
|
||||||
|
return x * x * (3 - 2 * x);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float distance(float x, float y, float x1, float y1, float x2, float y2)
|
||||||
|
{
|
||||||
|
float A = x - x1;
|
||||||
|
float B = y - y1;
|
||||||
|
float C = x2 - x1;
|
||||||
|
float D = y2 - y1;
|
||||||
|
|
||||||
|
float dot = A * C + B * D;
|
||||||
|
float len_sq = C * C + D * D;
|
||||||
|
float param = -1.f;
|
||||||
|
if (len_sq > FLT_EPSILON)
|
||||||
|
param = dot / len_sq;
|
||||||
|
|
||||||
|
float xx, yy;
|
||||||
|
|
||||||
|
if (param < 0.f) {
|
||||||
|
xx = x1;
|
||||||
|
yy = y1;
|
||||||
|
}
|
||||||
|
else if (param > 1.f) {
|
||||||
|
xx = x2;
|
||||||
|
yy = y2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
xx = x1 + param * C;
|
||||||
|
yy = y1 + param * D;
|
||||||
|
}
|
||||||
|
|
||||||
|
float dx = x - xx;
|
||||||
|
float dy = y - yy;
|
||||||
|
return sqrtf(dx * dx + dy * dy);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int DrawPoint(ImDrawList* draw_list, ImVec2 pos, const ImVec2 size, const ImVec2 offset, bool edited)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
static const ImVec2 localOffsets[4] = { ImVec2(1,0), ImVec2(0,1), ImVec2(-1,0), ImVec2(0,-1) };
|
||||||
|
ImVec2 offsets[4];
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
offsets[i] = pos * size + localOffsets[i] * 4.5f + offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ImVec2 center = pos * size + offset;
|
||||||
|
const ImRect anchor(center - ImVec2(5, 5), center + ImVec2(5, 5));
|
||||||
|
draw_list->AddConvexPolyFilled(offsets, 4, 0xFF000000);
|
||||||
|
if (anchor.Contains(io.MousePos))
|
||||||
|
{
|
||||||
|
ret = 1;
|
||||||
|
if (io.MouseDown[0])
|
||||||
|
ret = 2;
|
||||||
|
}
|
||||||
|
if (edited)
|
||||||
|
draw_list->AddPolyline(offsets, 4, 0xFFFFFFFF, true, 3.0f);
|
||||||
|
else if (ret)
|
||||||
|
draw_list->AddPolyline(offsets, 4, 0xFF80B0FF, true, 2.0f);
|
||||||
|
else
|
||||||
|
draw_list->AddPolyline(offsets, 4, 0xFF0080FF, true, 2.0f);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Edit(Delegate& delegate, const ImVec2& size, unsigned int id, const ImRect* clippingRect, ImVector<EditPoint>* selectedPoints)
|
||||||
|
{
|
||||||
|
static bool selectingQuad = false;
|
||||||
|
static ImVec2 quadSelection;
|
||||||
|
static int overCurve = -1;
|
||||||
|
static int movingCurve = -1;
|
||||||
|
static bool scrollingV = false;
|
||||||
|
static std::set<EditPoint> selection;
|
||||||
|
static bool overSelectedPoint = false;
|
||||||
|
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
|
||||||
|
ImGui::PushStyleColor(ImGuiCol_Border, 0);
|
||||||
|
ImGui::BeginChildFrame(id, size);
|
||||||
|
delegate.focused = ImGui::IsWindowFocused();
|
||||||
|
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||||
|
if (clippingRect)
|
||||||
|
draw_list->PushClipRect(clippingRect->Min, clippingRect->Max, true);
|
||||||
|
|
||||||
|
const ImVec2 offset = ImGui::GetCursorScreenPos() + ImVec2(0.f, size.y);
|
||||||
|
const ImVec2 ssize(size.x, -size.y);
|
||||||
|
const ImRect container(offset + ImVec2(0.f, ssize.y), offset + ImVec2(ssize.x, 0.f));
|
||||||
|
ImVec2& min = delegate.GetMin();
|
||||||
|
ImVec2& max = delegate.GetMax();
|
||||||
|
|
||||||
|
// handle zoom and VScroll
|
||||||
|
if (container.Contains(io.MousePos))
|
||||||
|
{
|
||||||
|
if (fabsf(io.MouseWheel) > FLT_EPSILON)
|
||||||
|
{
|
||||||
|
const float r = (io.MousePos.y - offset.y) / ssize.y;
|
||||||
|
float ratioY = ImLerp(min.y, max.y, r);
|
||||||
|
auto scaleValue = [&](float v) {
|
||||||
|
v -= ratioY;
|
||||||
|
v *= (1.f - io.MouseWheel * 0.05f);
|
||||||
|
v += ratioY;
|
||||||
|
return v;
|
||||||
|
};
|
||||||
|
min.y = scaleValue(min.y);
|
||||||
|
max.y = scaleValue(max.y);
|
||||||
|
}
|
||||||
|
if (!scrollingV && ImGui::IsMouseDown(2))
|
||||||
|
{
|
||||||
|
scrollingV = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImVec2 range = max - min + ImVec2(1.f, 0.f); // +1 because of inclusive last frame
|
||||||
|
|
||||||
|
const ImVec2 viewSize(size.x, -size.y);
|
||||||
|
const ImVec2 sizeOfPixel = ImVec2(1.f, 1.f) / viewSize;
|
||||||
|
const size_t curveCount = delegate.GetCurveCount();
|
||||||
|
|
||||||
|
if (scrollingV)
|
||||||
|
{
|
||||||
|
float deltaH = io.MouseDelta.y * range.y * sizeOfPixel.y;
|
||||||
|
min.y -= deltaH;
|
||||||
|
max.y -= deltaH;
|
||||||
|
if (!ImGui::IsMouseDown(2))
|
||||||
|
scrollingV = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_list->AddRectFilled(offset, offset + ssize, delegate.GetBackgroundColor());
|
||||||
|
|
||||||
|
auto pointToRange = [&](ImVec2 pt) { return (pt - min) / range; };
|
||||||
|
auto rangeToPoint = [&](ImVec2 pt) { return (pt * range) + min; };
|
||||||
|
|
||||||
|
draw_list->AddLine(ImVec2(-1.f, -min.y / range.y) * viewSize + offset, ImVec2(1.f, -min.y / range.y) * viewSize + offset, 0xFF000000, 1.5f);
|
||||||
|
bool overCurveOrPoint = false;
|
||||||
|
|
||||||
|
int localOverCurve = -1;
|
||||||
|
// make sure highlighted curve is rendered last
|
||||||
|
int* curvesIndex = (int*)_malloca(sizeof(int) * curveCount);
|
||||||
|
for (size_t c = 0; c < curveCount; c++)
|
||||||
|
curvesIndex[c] = int(c);
|
||||||
|
int highLightedCurveIndex = -1;
|
||||||
|
if (overCurve != -1 && curveCount)
|
||||||
|
{
|
||||||
|
ImSwap(curvesIndex[overCurve], curvesIndex[curveCount - 1]);
|
||||||
|
highLightedCurveIndex = overCurve;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t cur = 0; cur < curveCount; cur++)
|
||||||
|
{
|
||||||
|
int c = curvesIndex[cur];
|
||||||
|
if (!delegate.IsVisible(c))
|
||||||
|
continue;
|
||||||
|
const size_t ptCount = delegate.GetPointCount(c);
|
||||||
|
if (ptCount < 1)
|
||||||
|
continue;
|
||||||
|
CurveType curveType = delegate.GetCurveType(c);
|
||||||
|
if (curveType == CurveNone)
|
||||||
|
continue;
|
||||||
|
const ImVec2* pts = delegate.GetPoints(c);
|
||||||
|
uint32_t curveColor = delegate.GetCurveColor(c);
|
||||||
|
if ((c == highLightedCurveIndex && selection.empty() && !selectingQuad) || movingCurve == c)
|
||||||
|
curveColor = 0xFFFFFFFF;
|
||||||
|
|
||||||
|
for (size_t p = 0; p < ptCount - 1; p++)
|
||||||
|
{
|
||||||
|
const ImVec2 p1 = pointToRange(pts[p]);
|
||||||
|
const ImVec2 p2 = pointToRange(pts[p + 1]);
|
||||||
|
|
||||||
|
if (curveType == CurveSmooth || curveType == CurveLinear)
|
||||||
|
{
|
||||||
|
size_t subStepCount = (curveType == CurveSmooth) ? 20 : 2;
|
||||||
|
float step = 1.f / float(subStepCount - 1);
|
||||||
|
for (size_t substep = 0; substep < subStepCount - 1; substep++)
|
||||||
|
{
|
||||||
|
float t = float(substep) * step;
|
||||||
|
|
||||||
|
const ImVec2 sp1 = ImLerp(p1, p2, t);
|
||||||
|
const ImVec2 sp2 = ImLerp(p1, p2, t + step);
|
||||||
|
|
||||||
|
const float rt1 = smoothstep(p1.x, p2.x, sp1.x);
|
||||||
|
const float rt2 = smoothstep(p1.x, p2.x, sp2.x);
|
||||||
|
|
||||||
|
const ImVec2 pos1 = ImVec2(sp1.x, ImLerp(p1.y, p2.y, rt1)) * viewSize + offset;
|
||||||
|
const ImVec2 pos2 = ImVec2(sp2.x, ImLerp(p1.y, p2.y, rt2)) * viewSize + offset;
|
||||||
|
|
||||||
|
if (distance(io.MousePos.x, io.MousePos.y, pos1.x, pos1.y, pos2.x, pos2.y) < 8.f && !scrollingV)
|
||||||
|
{
|
||||||
|
localOverCurve = int(c);
|
||||||
|
overCurve = int(c);
|
||||||
|
overCurveOrPoint = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_list->AddLine(pos1, pos2, curveColor, 1.3f);
|
||||||
|
} // substep
|
||||||
|
}
|
||||||
|
else if (curveType == CurveDiscrete)
|
||||||
|
{
|
||||||
|
ImVec2 dp1 = p1 * viewSize + offset;
|
||||||
|
ImVec2 dp2 = ImVec2(p2.x, p1.y) * viewSize + offset;
|
||||||
|
ImVec2 dp3 = p2 * viewSize + offset;
|
||||||
|
draw_list->AddLine(dp1, dp2, curveColor, 1.3f);
|
||||||
|
draw_list->AddLine(dp2, dp3, curveColor, 1.3f);
|
||||||
|
|
||||||
|
if ((distance(io.MousePos.x, io.MousePos.y, dp1.x, dp1.y, dp3.x, dp1.y) < 8.f ||
|
||||||
|
distance(io.MousePos.x, io.MousePos.y, dp3.x, dp1.y, dp3.x, dp3.y) < 8.f)
|
||||||
|
/*&& localOverCurve == -1*/)
|
||||||
|
{
|
||||||
|
localOverCurve = int(c);
|
||||||
|
overCurve = int(c);
|
||||||
|
overCurveOrPoint = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // point loop
|
||||||
|
|
||||||
|
for (size_t p = 0; p < ptCount; p++)
|
||||||
|
{
|
||||||
|
const int drawState = DrawPoint(draw_list, pointToRange(pts[p]), viewSize, offset, (selection.find({ int(c), int(p) }) != selection.end() && movingCurve == -1 && !scrollingV));
|
||||||
|
if (drawState && movingCurve == -1 && !selectingQuad)
|
||||||
|
{
|
||||||
|
overCurveOrPoint = true;
|
||||||
|
overSelectedPoint = true;
|
||||||
|
overCurve = -1;
|
||||||
|
if (drawState == 2)
|
||||||
|
{
|
||||||
|
if (!io.KeyShift && selection.find({ int(c), int(p) }) == selection.end())
|
||||||
|
selection.clear();
|
||||||
|
selection.insert({ int(c), int(p) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // curves loop
|
||||||
|
|
||||||
|
if (localOverCurve == -1)
|
||||||
|
overCurve = -1;
|
||||||
|
|
||||||
|
// move selection
|
||||||
|
static bool pointsMoved = false;
|
||||||
|
static ImVec2 mousePosOrigin;
|
||||||
|
static std::vector<ImVec2> originalPoints;
|
||||||
|
if (overSelectedPoint && io.MouseDown[0])
|
||||||
|
{
|
||||||
|
if ((fabsf(io.MouseDelta.x) > 0.f || fabsf(io.MouseDelta.y) > 0.f) && !selection.empty())
|
||||||
|
{
|
||||||
|
if (!pointsMoved)
|
||||||
|
{
|
||||||
|
delegate.BeginEdit(0);
|
||||||
|
mousePosOrigin = io.MousePos;
|
||||||
|
originalPoints.resize(selection.size());
|
||||||
|
int index = 0;
|
||||||
|
for (auto& sel : selection)
|
||||||
|
{
|
||||||
|
const ImVec2* pts = delegate.GetPoints(sel.curveIndex);
|
||||||
|
originalPoints[index++] = pts[sel.pointIndex];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pointsMoved = true;
|
||||||
|
ret = 1;
|
||||||
|
auto prevSelection = selection;
|
||||||
|
int originalIndex = 0;
|
||||||
|
for (auto& sel : prevSelection)
|
||||||
|
{
|
||||||
|
const ImVec2 p = rangeToPoint(pointToRange(originalPoints[originalIndex]) + (io.MousePos - mousePosOrigin) * sizeOfPixel);
|
||||||
|
const int newIndex = delegate.EditPoint(sel.curveIndex, sel.pointIndex, p);
|
||||||
|
if (newIndex != sel.pointIndex)
|
||||||
|
{
|
||||||
|
selection.erase(sel);
|
||||||
|
selection.insert({ sel.curveIndex, newIndex });
|
||||||
|
}
|
||||||
|
originalIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (overSelectedPoint && !io.MouseDown[0])
|
||||||
|
{
|
||||||
|
overSelectedPoint = false;
|
||||||
|
if (pointsMoved)
|
||||||
|
{
|
||||||
|
pointsMoved = false;
|
||||||
|
delegate.EndEdit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add point
|
||||||
|
if (overCurve != -1 && io.MouseDoubleClicked[0])
|
||||||
|
{
|
||||||
|
const ImVec2 np = rangeToPoint((io.MousePos - offset) / viewSize);
|
||||||
|
delegate.BeginEdit(overCurve);
|
||||||
|
delegate.AddPoint(overCurve, np);
|
||||||
|
delegate.EndEdit();
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// move curve
|
||||||
|
|
||||||
|
if (movingCurve != -1)
|
||||||
|
{
|
||||||
|
const size_t ptCount = delegate.GetPointCount(movingCurve);
|
||||||
|
const ImVec2* pts = delegate.GetPoints(movingCurve);
|
||||||
|
if (!pointsMoved)
|
||||||
|
{
|
||||||
|
mousePosOrigin = io.MousePos;
|
||||||
|
pointsMoved = true;
|
||||||
|
originalPoints.resize(ptCount);
|
||||||
|
for (size_t index = 0; index < ptCount; index++)
|
||||||
|
{
|
||||||
|
originalPoints[index] = pts[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ptCount >= 1)
|
||||||
|
{
|
||||||
|
for (size_t p = 0; p < ptCount; p++)
|
||||||
|
{
|
||||||
|
delegate.EditPoint(movingCurve, int(p), rangeToPoint(pointToRange(originalPoints[p]) + (io.MousePos - mousePosOrigin) * sizeOfPixel));
|
||||||
|
}
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
if (!io.MouseDown[0])
|
||||||
|
{
|
||||||
|
movingCurve = -1;
|
||||||
|
pointsMoved = false;
|
||||||
|
delegate.EndEdit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (movingCurve == -1 && overCurve != -1 && ImGui::IsMouseClicked(0) && selection.empty() && !selectingQuad)
|
||||||
|
{
|
||||||
|
movingCurve = overCurve;
|
||||||
|
delegate.BeginEdit(overCurve);
|
||||||
|
}
|
||||||
|
|
||||||
|
// quad selection
|
||||||
|
if (selectingQuad)
|
||||||
|
{
|
||||||
|
const ImVec2 bmin = ImMin(quadSelection, io.MousePos);
|
||||||
|
const ImVec2 bmax = ImMax(quadSelection, io.MousePos);
|
||||||
|
draw_list->AddRectFilled(bmin, bmax, 0x40FF0000, 1.f);
|
||||||
|
draw_list->AddRect(bmin, bmax, 0xFFFF0000, 1.f);
|
||||||
|
const ImRect selectionQuad(bmin, bmax);
|
||||||
|
if (!io.MouseDown[0])
|
||||||
|
{
|
||||||
|
if (!io.KeyShift)
|
||||||
|
selection.clear();
|
||||||
|
// select everythnig is quad
|
||||||
|
for (size_t c = 0; c < curveCount; c++)
|
||||||
|
{
|
||||||
|
if (!delegate.IsVisible(c))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const size_t ptCount = delegate.GetPointCount(c);
|
||||||
|
if (ptCount < 1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const ImVec2* pts = delegate.GetPoints(c);
|
||||||
|
for (size_t p = 0; p < ptCount; p++)
|
||||||
|
{
|
||||||
|
const ImVec2 center = pointToRange(pts[p]) * viewSize + offset;
|
||||||
|
if (selectionQuad.Contains(center))
|
||||||
|
selection.insert({ int(c), int(p) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// done
|
||||||
|
selectingQuad = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!overCurveOrPoint && ImGui::IsMouseClicked(0) && !selectingQuad && movingCurve == -1 && !overSelectedPoint && container.Contains(io.MousePos))
|
||||||
|
{
|
||||||
|
selectingQuad = true;
|
||||||
|
quadSelection = io.MousePos;
|
||||||
|
}
|
||||||
|
if (clippingRect)
|
||||||
|
draw_list->PopClipRect();
|
||||||
|
|
||||||
|
ImGui::EndChildFrame();
|
||||||
|
ImGui::PopStyleVar();
|
||||||
|
ImGui::PopStyleColor(1);
|
||||||
|
|
||||||
|
if (selectedPoints)
|
||||||
|
{
|
||||||
|
selectedPoints->resize(int(selection.size()));
|
||||||
|
int index = 0;
|
||||||
|
for (auto& point : selection)
|
||||||
|
(*selectedPoints)[index++] = point;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
82
imgui/ImCurveEdit.h
Normal file
82
imgui/ImCurveEdit.h
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
// https://github.com/CedricGuillemet/ImGuizmo
|
||||||
|
// v 1.89 WIP
|
||||||
|
//
|
||||||
|
// The MIT License(MIT)
|
||||||
|
//
|
||||||
|
// Copyright(c) 2021 Cedric Guillemet
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files(the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions :
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "imgui.h"
|
||||||
|
|
||||||
|
struct ImRect;
|
||||||
|
|
||||||
|
namespace ImCurveEdit
|
||||||
|
{
|
||||||
|
enum CurveType
|
||||||
|
{
|
||||||
|
CurveNone,
|
||||||
|
CurveDiscrete,
|
||||||
|
CurveLinear,
|
||||||
|
CurveSmooth,
|
||||||
|
CurveBezier,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct EditPoint
|
||||||
|
{
|
||||||
|
int curveIndex;
|
||||||
|
int pointIndex;
|
||||||
|
bool operator <(const EditPoint& other) const
|
||||||
|
{
|
||||||
|
if (curveIndex < other.curveIndex)
|
||||||
|
return true;
|
||||||
|
if (curveIndex > other.curveIndex)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (pointIndex < other.pointIndex)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Delegate
|
||||||
|
{
|
||||||
|
bool focused = false;
|
||||||
|
virtual size_t GetCurveCount() = 0;
|
||||||
|
virtual bool IsVisible(size_t /*curveIndex*/) { return true; }
|
||||||
|
virtual CurveType GetCurveType(size_t /*curveIndex*/) const { return CurveLinear; }
|
||||||
|
virtual ImVec2& GetMin() = 0;
|
||||||
|
virtual ImVec2& GetMax() = 0;
|
||||||
|
virtual size_t GetPointCount(size_t curveIndex) = 0;
|
||||||
|
virtual uint32_t GetCurveColor(size_t curveIndex) = 0;
|
||||||
|
virtual ImVec2* GetPoints(size_t curveIndex) = 0;
|
||||||
|
virtual int EditPoint(size_t curveIndex, int pointIndex, ImVec2 value) = 0;
|
||||||
|
virtual void AddPoint(size_t curveIndex, ImVec2 value) = 0;
|
||||||
|
virtual unsigned int GetBackgroundColor() { return 0xFF202020; }
|
||||||
|
// handle undo/redo thru this functions
|
||||||
|
virtual void BeginEdit(int /*index*/) {}
|
||||||
|
virtual void EndEdit() {}
|
||||||
|
|
||||||
|
virtual ~Delegate() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
int Edit(Delegate& delegate, const ImVec2& size, unsigned int id, const ImRect* clippingRect = NULL, ImVector<EditPoint>* selectedPoints = NULL);
|
||||||
|
}
|
||||||
116
imgui/ImGradient.cpp
Normal file
116
imgui/ImGradient.cpp
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
// https://github.com/CedricGuillemet/ImGuizmo
|
||||||
|
// v 1.89 WIP
|
||||||
|
//
|
||||||
|
// The MIT License(MIT)
|
||||||
|
//
|
||||||
|
// Copyright(c) 2021 Cedric Guillemet
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files(the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions :
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
#include "ImGradient.h"
|
||||||
|
#include "imgui.h"
|
||||||
|
#include "imgui_internal.h"
|
||||||
|
|
||||||
|
namespace ImGradient
|
||||||
|
{
|
||||||
|
#ifndef IMGUI_DEFINE_MATH_OPERATORS
|
||||||
|
static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); }
|
||||||
|
static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); }
|
||||||
|
static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); }
|
||||||
|
static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); }
|
||||||
|
static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); }
|
||||||
|
static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int DrawPoint(ImDrawList* draw_list, ImVec4 color, const ImVec2 size, bool editing, ImVec2 pos)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
ImVec2 p1 = ImLerp(pos, ImVec2(pos + ImVec2(size.x - size.y, 0.f)), color.w) + ImVec2(3, 3);
|
||||||
|
ImVec2 p2 = ImLerp(pos + ImVec2(size.y, size.y), ImVec2(pos + size), color.w) - ImVec2(3, 3);
|
||||||
|
ImRect rc(p1, p2);
|
||||||
|
|
||||||
|
color.w = 1.f;
|
||||||
|
draw_list->AddRectFilled(p1, p2, ImColor(color));
|
||||||
|
if (editing)
|
||||||
|
draw_list->AddRect(p1, p2, 0xFFFFFFFF, 2.f, 15, 2.5f);
|
||||||
|
else
|
||||||
|
draw_list->AddRect(p1, p2, 0x80FFFFFF, 2.f, 15, 1.25f);
|
||||||
|
|
||||||
|
if (rc.Contains(io.MousePos))
|
||||||
|
{
|
||||||
|
if (io.MouseClicked[0])
|
||||||
|
return 2;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Edit(Delegate& delegate, const ImVec2& size, int& selection)
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
|
||||||
|
ImGui::BeginChildFrame(137, size);
|
||||||
|
|
||||||
|
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||||
|
const ImVec2 offset = ImGui::GetCursorScreenPos();
|
||||||
|
|
||||||
|
const ImVec4* pts = delegate.GetPoints();
|
||||||
|
static int currentSelection = -1;
|
||||||
|
static int movingPt = -1;
|
||||||
|
if (currentSelection >= int(delegate.GetPointCount()))
|
||||||
|
currentSelection = -1;
|
||||||
|
if (movingPt != -1)
|
||||||
|
{
|
||||||
|
ImVec4 current = pts[movingPt];
|
||||||
|
current.w += io.MouseDelta.x / size.x;
|
||||||
|
current.w = ImClamp(current.w, 0.f, 1.f);
|
||||||
|
delegate.EditPoint(movingPt, current);
|
||||||
|
ret = true;
|
||||||
|
if (!io.MouseDown[0])
|
||||||
|
movingPt = -1;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < delegate.GetPointCount(); i++)
|
||||||
|
{
|
||||||
|
int ptSel = DrawPoint(draw_list, pts[i], size, i == currentSelection, offset);
|
||||||
|
if (ptSel == 2)
|
||||||
|
{
|
||||||
|
currentSelection = int(i);
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
if (ptSel == 1 && io.MouseDown[0] && movingPt == -1)
|
||||||
|
{
|
||||||
|
movingPt = int(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImRect rc(offset, offset + size);
|
||||||
|
if (rc.Contains(io.MousePos) && io.MouseDoubleClicked[0])
|
||||||
|
{
|
||||||
|
float t = (io.MousePos.x - offset.x) / size.x;
|
||||||
|
delegate.AddPoint(delegate.GetPoint(t));
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
ImGui::EndChildFrame();
|
||||||
|
ImGui::PopStyleVar();
|
||||||
|
|
||||||
|
selection = currentSelection;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
45
imgui/ImGradient.h
Normal file
45
imgui/ImGradient.h
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
// https://github.com/CedricGuillemet/ImGuizmo
|
||||||
|
// v 1.89 WIP
|
||||||
|
//
|
||||||
|
// The MIT License(MIT)
|
||||||
|
//
|
||||||
|
// Copyright(c) 2021 Cedric Guillemet
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files(the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions :
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
struct ImVec4;
|
||||||
|
struct ImVec2;
|
||||||
|
|
||||||
|
namespace ImGradient
|
||||||
|
{
|
||||||
|
struct Delegate
|
||||||
|
{
|
||||||
|
virtual size_t GetPointCount() = 0;
|
||||||
|
virtual ImVec4* GetPoints() = 0;
|
||||||
|
virtual int EditPoint(int pointIndex, ImVec4 value) = 0;
|
||||||
|
virtual ImVec4 GetPoint(float t) = 0;
|
||||||
|
virtual void AddPoint(ImVec4 value) = 0;
|
||||||
|
virtual ~Delegate() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool Edit(Delegate& delegate, const ImVec2& size, int& selection);
|
||||||
|
}
|
||||||
2996
imgui/ImGuizmo.cpp
Executable file
2996
imgui/ImGuizmo.cpp
Executable file
File diff suppressed because it is too large
Load Diff
272
imgui/ImGuizmo.h
Normal file
272
imgui/ImGuizmo.h
Normal file
@@ -0,0 +1,272 @@
|
|||||||
|
// https://github.com/CedricGuillemet/ImGuizmo
|
||||||
|
// v 1.89 WIP
|
||||||
|
//
|
||||||
|
// The MIT License(MIT)
|
||||||
|
//
|
||||||
|
// Copyright(c) 2021 Cedric Guillemet
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files(the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions :
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
// -------------------------------------------------------------------------------------------
|
||||||
|
// History :
|
||||||
|
// 2019/11/03 View gizmo
|
||||||
|
// 2016/09/11 Behind camera culling. Scaling Delta matrix not multiplied by source matrix scales. local/world rotation and translation fixed. Display message is incorrect (X: ... Y:...) in local mode.
|
||||||
|
// 2016/09/09 Hatched negative axis. Snapping. Documentation update.
|
||||||
|
// 2016/09/04 Axis switch and translation plan autohiding. Scale transform stability improved
|
||||||
|
// 2016/09/01 Mogwai changed to Manipulate. Draw debug cube. Fixed inverted scale. Mixing scale and translation/rotation gives bad results.
|
||||||
|
// 2016/08/31 First version
|
||||||
|
//
|
||||||
|
// -------------------------------------------------------------------------------------------
|
||||||
|
// Future (no order):
|
||||||
|
//
|
||||||
|
// - Multi view
|
||||||
|
// - display rotation/translation/scale infos in local/world space and not only local
|
||||||
|
// - finish local/world matrix application
|
||||||
|
// - OPERATION as bitmask
|
||||||
|
//
|
||||||
|
// -------------------------------------------------------------------------------------------
|
||||||
|
// Example
|
||||||
|
#if 0
|
||||||
|
void EditTransform(const Camera& camera, matrix_t& matrix)
|
||||||
|
{
|
||||||
|
static ImGuizmo::OPERATION mCurrentGizmoOperation(ImGuizmo::ROTATE);
|
||||||
|
static ImGuizmo::MODE mCurrentGizmoMode(ImGuizmo::WORLD);
|
||||||
|
if (ImGui::IsKeyPressed(90))
|
||||||
|
mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
|
||||||
|
if (ImGui::IsKeyPressed(69))
|
||||||
|
mCurrentGizmoOperation = ImGuizmo::ROTATE;
|
||||||
|
if (ImGui::IsKeyPressed(82)) // r Key
|
||||||
|
mCurrentGizmoOperation = ImGuizmo::SCALE;
|
||||||
|
if (ImGui::RadioButton("Translate", mCurrentGizmoOperation == ImGuizmo::TRANSLATE))
|
||||||
|
mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
|
||||||
|
ImGui::SameLine();
|
||||||
|
if (ImGui::RadioButton("Rotate", mCurrentGizmoOperation == ImGuizmo::ROTATE))
|
||||||
|
mCurrentGizmoOperation = ImGuizmo::ROTATE;
|
||||||
|
ImGui::SameLine();
|
||||||
|
if (ImGui::RadioButton("Scale", mCurrentGizmoOperation == ImGuizmo::SCALE))
|
||||||
|
mCurrentGizmoOperation = ImGuizmo::SCALE;
|
||||||
|
float matrixTranslation[3], matrixRotation[3], matrixScale[3];
|
||||||
|
ImGuizmo::DecomposeMatrixToComponents(matrix.m16, matrixTranslation, matrixRotation, matrixScale);
|
||||||
|
ImGui::InputFloat3("Tr", matrixTranslation, 3);
|
||||||
|
ImGui::InputFloat3("Rt", matrixRotation, 3);
|
||||||
|
ImGui::InputFloat3("Sc", matrixScale, 3);
|
||||||
|
ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, matrix.m16);
|
||||||
|
|
||||||
|
if (mCurrentGizmoOperation != ImGuizmo::SCALE)
|
||||||
|
{
|
||||||
|
if (ImGui::RadioButton("Local", mCurrentGizmoMode == ImGuizmo::LOCAL))
|
||||||
|
mCurrentGizmoMode = ImGuizmo::LOCAL;
|
||||||
|
ImGui::SameLine();
|
||||||
|
if (ImGui::RadioButton("World", mCurrentGizmoMode == ImGuizmo::WORLD))
|
||||||
|
mCurrentGizmoMode = ImGuizmo::WORLD;
|
||||||
|
}
|
||||||
|
static bool useSnap(false);
|
||||||
|
if (ImGui::IsKeyPressed(83))
|
||||||
|
useSnap = !useSnap;
|
||||||
|
ImGui::Checkbox("", &useSnap);
|
||||||
|
ImGui::SameLine();
|
||||||
|
vec_t snap;
|
||||||
|
switch (mCurrentGizmoOperation)
|
||||||
|
{
|
||||||
|
case ImGuizmo::TRANSLATE:
|
||||||
|
snap = config.mSnapTranslation;
|
||||||
|
ImGui::InputFloat3("Snap", &snap.x);
|
||||||
|
break;
|
||||||
|
case ImGuizmo::ROTATE:
|
||||||
|
snap = config.mSnapRotation;
|
||||||
|
ImGui::InputFloat("Angle Snap", &snap.x);
|
||||||
|
break;
|
||||||
|
case ImGuizmo::SCALE:
|
||||||
|
snap = config.mSnapScale;
|
||||||
|
ImGui::InputFloat("Scale Snap", &snap.x);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
|
||||||
|
ImGuizmo::Manipulate(camera.mView.m16, camera.mProjection.m16, mCurrentGizmoOperation, mCurrentGizmoMode, matrix.m16, NULL, useSnap ? &snap.x : NULL);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef USE_IMGUI_API
|
||||||
|
#include "imconfig.h"
|
||||||
|
#endif
|
||||||
|
#ifndef IMGUI_API
|
||||||
|
#define IMGUI_API
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef IMGUIZMO_NAMESPACE
|
||||||
|
#define IMGUIZMO_NAMESPACE ImGuizmo
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace IMGUIZMO_NAMESPACE
|
||||||
|
{
|
||||||
|
// call inside your own window and before Manipulate() in order to draw gizmo to that window.
|
||||||
|
// Or pass a specific ImDrawList to draw to (e.g. ImGui::GetForegroundDrawList()).
|
||||||
|
IMGUI_API void SetDrawlist(ImDrawList* drawlist = nullptr);
|
||||||
|
|
||||||
|
// call BeginFrame right after ImGui_XXXX_NewFrame();
|
||||||
|
IMGUI_API void BeginFrame();
|
||||||
|
|
||||||
|
// this is necessary because when imguizmo is compiled into a dll, and imgui into another
|
||||||
|
// globals are not shared between them.
|
||||||
|
// More details at https://stackoverflow.com/questions/19373061/what-happens-to-global-and-static-variables-in-a-shared-library-when-it-is-dynam
|
||||||
|
// expose method to set imgui context
|
||||||
|
IMGUI_API void SetImGuiContext(ImGuiContext* ctx);
|
||||||
|
|
||||||
|
// return true if mouse cursor is over any gizmo control (axis, plan or screen component)
|
||||||
|
IMGUI_API bool IsOver();
|
||||||
|
|
||||||
|
// return true if mouse IsOver or if the gizmo is in moving state
|
||||||
|
IMGUI_API bool IsUsing();
|
||||||
|
|
||||||
|
// return true if any gizmo is in moving state
|
||||||
|
IMGUI_API bool IsUsingAny();
|
||||||
|
|
||||||
|
// enable/disable the gizmo. Stay in the state until next call to Enable.
|
||||||
|
// gizmo is rendered with gray half transparent color when disabled
|
||||||
|
IMGUI_API void Enable(bool enable);
|
||||||
|
|
||||||
|
// helper functions for manualy editing translation/rotation/scale with an input float
|
||||||
|
// translation, rotation and scale float points to 3 floats each
|
||||||
|
// Angles are in degrees (more suitable for human editing)
|
||||||
|
// example:
|
||||||
|
// float matrixTranslation[3], matrixRotation[3], matrixScale[3];
|
||||||
|
// ImGuizmo::DecomposeMatrixToComponents(gizmoMatrix.m16, matrixTranslation, matrixRotation, matrixScale);
|
||||||
|
// ImGui::InputFloat3("Tr", matrixTranslation, 3);
|
||||||
|
// ImGui::InputFloat3("Rt", matrixRotation, 3);
|
||||||
|
// ImGui::InputFloat3("Sc", matrixScale, 3);
|
||||||
|
// ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, gizmoMatrix.m16);
|
||||||
|
//
|
||||||
|
// These functions have some numerical stability issues for now. Use with caution.
|
||||||
|
IMGUI_API void DecomposeMatrixToComponents(const float* matrix, float* translation, float* rotation, float* scale);
|
||||||
|
IMGUI_API void RecomposeMatrixFromComponents(const float* translation, const float* rotation, const float* scale, float* matrix);
|
||||||
|
|
||||||
|
IMGUI_API void SetRect(float x, float y, float width, float height);
|
||||||
|
// default is false
|
||||||
|
IMGUI_API void SetOrthographic(bool isOrthographic);
|
||||||
|
|
||||||
|
// Render a cube with face color corresponding to face normal. Usefull for debug/tests
|
||||||
|
IMGUI_API void DrawCubes(const float* view, const float* projection, const float* matrices, int matrixCount);
|
||||||
|
IMGUI_API void DrawGrid(const float* view, const float* projection, const float* matrix, const float gridSize);
|
||||||
|
|
||||||
|
// call it when you want a gizmo
|
||||||
|
// Needs view and projection matrices.
|
||||||
|
// matrix parameter is the source matrix (where will be gizmo be drawn) and might be transformed by the function. Return deltaMatrix is optional
|
||||||
|
// translation is applied in world space
|
||||||
|
enum OPERATION
|
||||||
|
{
|
||||||
|
TRANSLATE_X = (1u << 0),
|
||||||
|
TRANSLATE_Y = (1u << 1),
|
||||||
|
TRANSLATE_Z = (1u << 2),
|
||||||
|
ROTATE_X = (1u << 3),
|
||||||
|
ROTATE_Y = (1u << 4),
|
||||||
|
ROTATE_Z = (1u << 5),
|
||||||
|
ROTATE_SCREEN = (1u << 6),
|
||||||
|
SCALE_X = (1u << 7),
|
||||||
|
SCALE_Y = (1u << 8),
|
||||||
|
SCALE_Z = (1u << 9),
|
||||||
|
BOUNDS = (1u << 10),
|
||||||
|
SCALE_XU = (1u << 11),
|
||||||
|
SCALE_YU = (1u << 12),
|
||||||
|
SCALE_ZU = (1u << 13),
|
||||||
|
|
||||||
|
TRANSLATE = TRANSLATE_X | TRANSLATE_Y | TRANSLATE_Z,
|
||||||
|
ROTATE = ROTATE_X | ROTATE_Y | ROTATE_Z | ROTATE_SCREEN,
|
||||||
|
SCALE = SCALE_X | SCALE_Y | SCALE_Z,
|
||||||
|
SCALEU = SCALE_XU | SCALE_YU | SCALE_ZU, // universal
|
||||||
|
UNIVERSAL = TRANSLATE | ROTATE | SCALEU
|
||||||
|
};
|
||||||
|
|
||||||
|
inline OPERATION operator|(OPERATION lhs, OPERATION rhs)
|
||||||
|
{
|
||||||
|
return static_cast<OPERATION>(static_cast<int>(lhs) | static_cast<int>(rhs));
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MODE
|
||||||
|
{
|
||||||
|
LOCAL,
|
||||||
|
WORLD
|
||||||
|
};
|
||||||
|
|
||||||
|
IMGUI_API bool Manipulate(const float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float* deltaMatrix = NULL, const float* snap = NULL, const float* localBounds = NULL, const float* boundsSnap = NULL);
|
||||||
|
//
|
||||||
|
// Please note that this cubeview is patented by Autodesk : https://patents.google.com/patent/US7782319B2/en
|
||||||
|
// It seems to be a defensive patent in the US. I don't think it will bring troubles using it as
|
||||||
|
// other software are using the same mechanics. But just in case, you are now warned!
|
||||||
|
//
|
||||||
|
IMGUI_API void ViewManipulate(float* view, float length, ImVec2 position, ImVec2 size, ImU32 backgroundColor);
|
||||||
|
|
||||||
|
// use this version if you did not call Manipulate before and you are just using ViewManipulate
|
||||||
|
IMGUI_API void ViewManipulate(float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float length, ImVec2 position, ImVec2 size, ImU32 backgroundColor);
|
||||||
|
|
||||||
|
IMGUI_API void SetID(int id);
|
||||||
|
|
||||||
|
// return true if the cursor is over the operation's gizmo
|
||||||
|
IMGUI_API bool IsOver(OPERATION op);
|
||||||
|
IMGUI_API void SetGizmoSizeClipSpace(float value);
|
||||||
|
|
||||||
|
// Allow axis to flip
|
||||||
|
// When true (default), the guizmo axis flip for better visibility
|
||||||
|
// When false, they always stay along the positive world/local axis
|
||||||
|
IMGUI_API void AllowAxisFlip(bool value);
|
||||||
|
|
||||||
|
// Configure the limit where axis are hidden
|
||||||
|
IMGUI_API void SetAxisLimit(float value);
|
||||||
|
// Configure the limit where planes are hiden
|
||||||
|
IMGUI_API void SetPlaneLimit(float value);
|
||||||
|
|
||||||
|
enum COLOR
|
||||||
|
{
|
||||||
|
DIRECTION_X, // directionColor[0]
|
||||||
|
DIRECTION_Y, // directionColor[1]
|
||||||
|
DIRECTION_Z, // directionColor[2]
|
||||||
|
PLANE_X, // planeColor[0]
|
||||||
|
PLANE_Y, // planeColor[1]
|
||||||
|
PLANE_Z, // planeColor[2]
|
||||||
|
SELECTION, // selectionColor
|
||||||
|
INACTIVE, // inactiveColor
|
||||||
|
TRANSLATION_LINE, // translationLineColor
|
||||||
|
SCALE_LINE,
|
||||||
|
ROTATION_USING_BORDER,
|
||||||
|
ROTATION_USING_FILL,
|
||||||
|
HATCHED_AXIS_LINES,
|
||||||
|
TEXT,
|
||||||
|
TEXT_SHADOW,
|
||||||
|
COUNT
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Style
|
||||||
|
{
|
||||||
|
IMGUI_API Style();
|
||||||
|
|
||||||
|
float TranslationLineThickness; // Thickness of lines for translation gizmo
|
||||||
|
float TranslationLineArrowSize; // Size of arrow at the end of lines for translation gizmo
|
||||||
|
float RotationLineThickness; // Thickness of lines for rotation gizmo
|
||||||
|
float RotationOuterLineThickness; // Thickness of line surrounding the rotation gizmo
|
||||||
|
float ScaleLineThickness; // Thickness of lines for scale gizmo
|
||||||
|
float ScaleLineCircleSize; // Size of circle at the end of lines for scale gizmo
|
||||||
|
float HatchedAxisLineThickness; // Thickness of hatched axis lines
|
||||||
|
float CenterCircleSize; // Size of circle at the center of the translate/scale gizmo
|
||||||
|
|
||||||
|
ImVec4 Colors[COLOR::COUNT];
|
||||||
|
};
|
||||||
|
|
||||||
|
IMGUI_API Style& GetStyle();
|
||||||
|
}
|
||||||
245
imgui/ImZoomSlider.h
Normal file
245
imgui/ImZoomSlider.h
Normal file
@@ -0,0 +1,245 @@
|
|||||||
|
// https://github.com/CedricGuillemet/ImGuizmo
|
||||||
|
// v 1.89 WIP
|
||||||
|
//
|
||||||
|
// The MIT License(MIT)
|
||||||
|
//
|
||||||
|
// Copyright(c) 2021 Cedric Guillemet
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files(the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions :
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace ImZoomSlider
|
||||||
|
{
|
||||||
|
typedef int ImGuiZoomSliderFlags;
|
||||||
|
enum ImGuiPopupFlags_
|
||||||
|
{
|
||||||
|
ImGuiZoomSliderFlags_None = 0,
|
||||||
|
ImGuiZoomSliderFlags_Vertical = 1,
|
||||||
|
ImGuiZoomSliderFlags_NoAnchors = 2,
|
||||||
|
ImGuiZoomSliderFlags_NoMiddleCarets = 4,
|
||||||
|
ImGuiZoomSliderFlags_NoWheel = 8,
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T> bool ImZoomSlider(const T lower, const T higher, T& viewLower, T& viewHigher, float wheelRatio = 0.01f, ImGuiZoomSliderFlags flags = ImGuiZoomSliderFlags_None)
|
||||||
|
{
|
||||||
|
bool interacted = false;
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||||
|
|
||||||
|
static const float handleSize = 12;
|
||||||
|
static const float roundRadius = 3.f;
|
||||||
|
static const char* controlName = "ImZoomSlider";
|
||||||
|
|
||||||
|
static bool movingScrollBarSvg = false;
|
||||||
|
static bool sizingRBarSvg = false;
|
||||||
|
static bool sizingLBarSvg = false;
|
||||||
|
static ImGuiID editingId = (ImGuiID)-1;
|
||||||
|
static float scrollingSource = 0.f;
|
||||||
|
static float saveViewLower;
|
||||||
|
static float saveViewHigher;
|
||||||
|
|
||||||
|
const bool isVertical = flags & ImGuiZoomSliderFlags_Vertical;
|
||||||
|
const ImVec2 canvasPos = ImGui::GetCursorScreenPos();
|
||||||
|
const ImVec2 canvasSize = ImGui::GetContentRegionAvail();
|
||||||
|
const float canvasSizeLength = isVertical ? ImGui::GetItemRectSize().y : canvasSize.x;
|
||||||
|
const ImVec2 scrollBarSize = isVertical ? ImVec2(14.f, canvasSizeLength) : ImVec2(canvasSizeLength, 14.f);
|
||||||
|
|
||||||
|
ImGui::InvisibleButton(controlName, scrollBarSize);
|
||||||
|
const ImGuiID currentId = ImGui::GetID(controlName);
|
||||||
|
|
||||||
|
const bool usingEditingId = currentId == editingId;
|
||||||
|
const bool canUseControl = usingEditingId || editingId == -1;
|
||||||
|
const bool movingScrollBar = usingEditingId ? movingScrollBarSvg : false;
|
||||||
|
const bool sizingRBar = usingEditingId ? sizingRBarSvg : false;
|
||||||
|
const bool sizingLBar = usingEditingId ? sizingLBarSvg : false;
|
||||||
|
const int componentIndex = isVertical ? 1 : 0;
|
||||||
|
const ImVec2 scrollBarMin = ImGui::GetItemRectMin();
|
||||||
|
const ImVec2 scrollBarMax = ImGui::GetItemRectMax();
|
||||||
|
const ImVec2 scrollBarA = ImVec2(scrollBarMin.x, scrollBarMin.y) - (isVertical ? ImVec2(2,0) : ImVec2(0,2));
|
||||||
|
const ImVec2 scrollBarB = isVertical ? ImVec2(scrollBarMax.x - 1.f, scrollBarMin.y + canvasSizeLength) : ImVec2(scrollBarMin.x + canvasSizeLength, scrollBarMax.y - 1.f);
|
||||||
|
const float scrollStart = ((viewLower - lower) / (higher - lower)) * canvasSizeLength + scrollBarMin[componentIndex];
|
||||||
|
const float scrollEnd = ((viewHigher - lower) / (higher - lower)) * canvasSizeLength + scrollBarMin[componentIndex];
|
||||||
|
const float screenSize = scrollEnd - scrollStart;
|
||||||
|
const ImVec2 scrollTopLeft = isVertical ? ImVec2(scrollBarMin.x, scrollStart) : ImVec2(scrollStart, scrollBarMin.y);
|
||||||
|
const ImVec2 scrollBottomRight = isVertical ? ImVec2(scrollBarMax.x - 2.f, scrollEnd) : ImVec2(scrollEnd, scrollBarMax.y - 2.f);
|
||||||
|
const bool inScrollBar = canUseControl && ImRect(scrollTopLeft, scrollBottomRight).Contains(io.MousePos);
|
||||||
|
const ImRect scrollBarRect(scrollBarA, scrollBarB);
|
||||||
|
const float deltaScreen = io.MousePos[componentIndex] - scrollingSource;
|
||||||
|
const float deltaView = ((higher - lower) / canvasSizeLength) * deltaScreen;
|
||||||
|
const uint32_t barColor = ImGui::GetColorU32((inScrollBar || movingScrollBar) ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
|
||||||
|
const float middleCoord = (scrollStart + scrollEnd) * 0.5f;
|
||||||
|
const bool insideControl = canUseControl && ImRect(scrollBarMin, scrollBarMax).Contains(io.MousePos);
|
||||||
|
const bool hasAnchors = !(flags & ImGuiZoomSliderFlags_NoAnchors);
|
||||||
|
const float viewMinSize = ((3.f * handleSize) / canvasSizeLength) * (higher - lower);
|
||||||
|
const auto ClipView = [lower, higher, &viewLower, &viewHigher]() {
|
||||||
|
if (viewLower < lower)
|
||||||
|
{
|
||||||
|
const float deltaClip = lower - viewLower;
|
||||||
|
viewLower += deltaClip;
|
||||||
|
viewHigher += deltaClip;
|
||||||
|
}
|
||||||
|
if (viewHigher > higher)
|
||||||
|
{
|
||||||
|
const float deltaClip = viewHigher - higher;
|
||||||
|
viewLower -= deltaClip;
|
||||||
|
viewHigher -= deltaClip;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool onLeft = false;
|
||||||
|
bool onRight = false;
|
||||||
|
|
||||||
|
draw_list->AddRectFilled(scrollBarA, scrollBarB, 0xFF101010, roundRadius);
|
||||||
|
draw_list->AddRectFilled(scrollBarA, scrollBarB, 0xFF222222, 0);
|
||||||
|
draw_list->AddRectFilled(scrollTopLeft, scrollBottomRight, barColor, roundRadius);
|
||||||
|
|
||||||
|
if (!(flags & ImGuiZoomSliderFlags_NoMiddleCarets))
|
||||||
|
{
|
||||||
|
for (float i = 0.5f; i < 3.f; i += 1.f)
|
||||||
|
{
|
||||||
|
const float coordA = middleCoord - handleSize * 0.5f;
|
||||||
|
const float coordB = middleCoord + handleSize * 0.5f;
|
||||||
|
ImVec2 base = scrollBarMin;
|
||||||
|
base.x += scrollBarSize.x * 0.25f * i;
|
||||||
|
base.y += scrollBarSize.y * 0.25f * i;
|
||||||
|
|
||||||
|
if (isVertical)
|
||||||
|
{
|
||||||
|
draw_list->AddLine(ImVec2(base.x, coordA), ImVec2(base.x, coordB), ImGui::GetColorU32(ImGuiCol_SliderGrab));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
draw_list->AddLine(ImVec2(coordA, base.y), ImVec2(coordB, base.y), ImGui::GetColorU32(ImGuiCol_SliderGrab));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mouse wheel
|
||||||
|
if (io.MouseClicked[0] && insideControl && !inScrollBar)
|
||||||
|
{
|
||||||
|
const float ratio = (io.MousePos[componentIndex] - scrollBarMin[componentIndex]) / (scrollBarMax[componentIndex] - scrollBarMin[componentIndex]);
|
||||||
|
const float size = (higher - lower);
|
||||||
|
const float halfViewSize = (viewHigher - viewLower) * 0.5f;
|
||||||
|
const float middle = ratio * size + lower;
|
||||||
|
viewLower = middle - halfViewSize;
|
||||||
|
viewHigher = middle + halfViewSize;
|
||||||
|
ClipView();
|
||||||
|
interacted = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(flags & ImGuiZoomSliderFlags_NoWheel) && inScrollBar && fabsf(io.MouseWheel) > 0.f)
|
||||||
|
{
|
||||||
|
const float ratio = (io.MousePos[componentIndex] - scrollStart) / (scrollEnd - scrollStart);
|
||||||
|
const float amount = io.MouseWheel * wheelRatio * (viewHigher - viewLower);
|
||||||
|
|
||||||
|
viewLower -= ratio * amount;
|
||||||
|
viewHigher += (1.f - ratio) * amount;
|
||||||
|
ClipView();
|
||||||
|
interacted = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (screenSize > handleSize * 2.f && hasAnchors)
|
||||||
|
{
|
||||||
|
const ImRect barHandleLeft(scrollTopLeft, isVertical ? ImVec2(scrollBottomRight.x, scrollTopLeft.y + handleSize) : ImVec2(scrollTopLeft.x + handleSize, scrollBottomRight.y));
|
||||||
|
const ImRect barHandleRight(isVertical ? ImVec2(scrollTopLeft.x, scrollBottomRight.y - handleSize) : ImVec2(scrollBottomRight.x - handleSize, scrollTopLeft.y), scrollBottomRight);
|
||||||
|
|
||||||
|
onLeft = barHandleLeft.Contains(io.MousePos);
|
||||||
|
onRight = barHandleRight.Contains(io.MousePos);
|
||||||
|
|
||||||
|
draw_list->AddRectFilled(barHandleLeft.Min, barHandleLeft.Max, ImGui::GetColorU32((onLeft || sizingLBar) ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), roundRadius);
|
||||||
|
draw_list->AddRectFilled(barHandleRight.Min, barHandleRight.Max, ImGui::GetColorU32((onRight || sizingRBar) ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), roundRadius);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizingRBar)
|
||||||
|
{
|
||||||
|
if (!io.MouseDown[0])
|
||||||
|
{
|
||||||
|
sizingRBarSvg = false;
|
||||||
|
editingId = (ImGuiID)-1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
viewHigher = ImMin(saveViewHigher + deltaView, higher);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (sizingLBar)
|
||||||
|
{
|
||||||
|
if (!io.MouseDown[0])
|
||||||
|
{
|
||||||
|
sizingLBarSvg = false;
|
||||||
|
editingId = (ImGuiID)-1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
viewLower = ImMax(saveViewLower + deltaView, lower);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (movingScrollBar)
|
||||||
|
{
|
||||||
|
if (!io.MouseDown[0])
|
||||||
|
{
|
||||||
|
movingScrollBarSvg = false;
|
||||||
|
editingId = (ImGuiID)-1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
viewLower = saveViewLower + deltaView;
|
||||||
|
viewHigher = saveViewHigher + deltaView;
|
||||||
|
ClipView();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (inScrollBar && ImGui::IsMouseClicked(0))
|
||||||
|
{
|
||||||
|
movingScrollBarSvg = true;
|
||||||
|
scrollingSource = io.MousePos[componentIndex];
|
||||||
|
saveViewLower = viewLower;
|
||||||
|
saveViewHigher = viewHigher;
|
||||||
|
editingId = currentId;
|
||||||
|
}
|
||||||
|
if (!sizingRBar && onRight && ImGui::IsMouseClicked(0) && hasAnchors)
|
||||||
|
{
|
||||||
|
sizingRBarSvg = true;
|
||||||
|
editingId = currentId;
|
||||||
|
}
|
||||||
|
if (!sizingLBar && onLeft && ImGui::IsMouseClicked(0) && hasAnchors)
|
||||||
|
{
|
||||||
|
sizingLBarSvg = true;
|
||||||
|
editingId = currentId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// minimal size check
|
||||||
|
if ((viewHigher - viewLower) < viewMinSize)
|
||||||
|
{
|
||||||
|
const float middle = (viewLower + viewHigher) * 0.5f;
|
||||||
|
viewLower = middle - viewMinSize * 0.5f;
|
||||||
|
viewHigher = middle + viewMinSize * 0.5f;
|
||||||
|
ClipView();
|
||||||
|
}
|
||||||
|
|
||||||
|
return movingScrollBar || sizingRBar || sizingLBar || interacted;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
670
imgui/backends/imgui_impl_allegro5.cpp
Normal file
670
imgui/backends/imgui_impl_allegro5.cpp
Normal file
@@ -0,0 +1,670 @@
|
|||||||
|
// dear imgui: Renderer + Platform Backend for Allegro 5
|
||||||
|
// (Info: Allegro 5 is a cross-platform general purpose library for handling windows, inputs, graphics, etc.)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'ALLEGRO_BITMAP*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy ALLEGRO_KEY_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Clipboard support (from Allegro 5.1.12).
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
// Missing features or Issues:
|
||||||
|
// [ ] Renderer: The renderer is suboptimal as we need to convert vertices manually.
|
||||||
|
// [ ] Platform: Missing gamepad support.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-07-07: Fixed texture update broken on some platforms where ALLEGRO_LOCK_WRITEONLY needed all texels to be rewritten.
|
||||||
|
// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplSDLGPU3_CreateFontsTexture() and ImGui_ImplSDLGPU3_DestroyFontsTexture().
|
||||||
|
// 2025-02-18: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support.
|
||||||
|
// 2025-01-06: Avoid calling al_set_mouse_cursor() repeatedly since it appears to leak on on X11 (#8256).
|
||||||
|
// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
|
||||||
|
// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
|
||||||
|
// - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
|
||||||
|
// 2022-11-30: Renderer: Restoring using al_draw_indexed_prim() when Allegro version is >= 5.2.5.
|
||||||
|
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
|
||||||
|
// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
|
||||||
|
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
|
||||||
|
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
|
||||||
|
// 2022-01-17: Inputs: always calling io.AddKeyModsEvent() next and before key event (not in NewFrame) to fix input queue with very low framerates.
|
||||||
|
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
|
||||||
|
// 2021-12-08: Renderer: Fixed mishandling of the ImDrawCmd::IdxOffset field! This is an old bug but it never had an effect until some internal rendering changes in 1.86.
|
||||||
|
// 2021-08-17: Calling io.AddFocusEvent() on ALLEGRO_EVENT_DISPLAY_SWITCH_OUT/ALLEGRO_EVENT_DISPLAY_SWITCH_IN events.
|
||||||
|
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
|
||||||
|
// 2021-05-19: Renderer: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
|
||||||
|
// 2021-02-18: Change blending equation to preserve alpha in output buffer.
|
||||||
|
// 2020-08-10: Inputs: Fixed horizontal mouse wheel direction.
|
||||||
|
// 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
|
||||||
|
// 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
|
||||||
|
// 2019-05-11: Inputs: Don't filter character value from ALLEGRO_EVENT_KEY_CHAR before calling AddInputCharacter().
|
||||||
|
// 2019-04-30: Renderer: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
|
||||||
|
// 2018-11-30: Platform: Added touchscreen support.
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendPlatformName/io.BackendRendererName so they can be displayed in the About Window.
|
||||||
|
// 2018-06-13: Platform: Added clipboard support (from Allegro 5.1.12).
|
||||||
|
// 2018-06-13: Renderer: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
|
||||||
|
// 2018-06-13: Renderer: Stopped using al_draw_indexed_prim() as it is buggy in Allegro's DX9 backend.
|
||||||
|
// 2018-06-13: Renderer: Backup/restore transform and clipping rectangle.
|
||||||
|
// 2018-06-11: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
|
||||||
|
// 2018-04-18: Misc: Renamed file from imgui_impl_a5.cpp to imgui_impl_allegro5.cpp.
|
||||||
|
// 2018-04-18: Misc: Added support for 32-bit vertex indices to avoid conversion at runtime. Added imconfig_allegro5.h to enforce 32-bit indices when included from imgui.h.
|
||||||
|
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplAllegro5_RenderDrawData() in the .h file so you can call it yourself.
|
||||||
|
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
||||||
|
// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_allegro5.h"
|
||||||
|
#include <stdint.h> // uint64_t
|
||||||
|
#include <cstring> // memcpy
|
||||||
|
|
||||||
|
// Allegro
|
||||||
|
#include <allegro5/allegro.h>
|
||||||
|
#include <allegro5/allegro_primitives.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <allegro5/allegro_windows.h>
|
||||||
|
#endif
|
||||||
|
#define ALLEGRO_HAS_CLIPBOARD ((ALLEGRO_VERSION_INT & ~ALLEGRO_UNSTABLE_BIT) >= ((5 << 24) | (1 << 16) | (12 << 8))) // Clipboard only supported from Allegro 5.1.12
|
||||||
|
#define ALLEGRO_HAS_DRAW_INDEXED_PRIM ((ALLEGRO_VERSION_INT & ~ALLEGRO_UNSTABLE_BIT) >= ((5 << 24) | (2 << 16) | ( 5 << 8))) // DX9 implementation of al_draw_indexed_prim() got fixed in Allegro 5.2.5
|
||||||
|
|
||||||
|
// Visual Studio warnings
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning (disable: 4127) // condition expression is constant
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct ImDrawVertAllegro
|
||||||
|
{
|
||||||
|
ImVec2 pos;
|
||||||
|
ImVec2 uv;
|
||||||
|
ALLEGRO_COLOR col;
|
||||||
|
};
|
||||||
|
|
||||||
|
// FIXME-OPT: Unfortunately Allegro doesn't support 32-bit packed colors so we have to convert them to 4 float as well..
|
||||||
|
// FIXME-OPT: Consider inlining al_map_rgba()?
|
||||||
|
// see https://github.com/liballeg/allegro5/blob/master/src/pixels.c#L554
|
||||||
|
// and https://github.com/liballeg/allegro5/blob/master/include/allegro5/internal/aintern_pixels.h
|
||||||
|
#define DRAW_VERT_IMGUI_TO_ALLEGRO(DST, SRC) { (DST)->pos = (SRC)->pos; (DST)->uv = (SRC)->uv; unsigned char* c = (unsigned char*)&(SRC)->col; (DST)->col = al_map_rgba(c[0], c[1], c[2], c[3]); }
|
||||||
|
|
||||||
|
// Allegro Data
|
||||||
|
struct ImGui_ImplAllegro5_Data
|
||||||
|
{
|
||||||
|
ALLEGRO_DISPLAY* Display;
|
||||||
|
ALLEGRO_BITMAP* Texture;
|
||||||
|
double Time;
|
||||||
|
ALLEGRO_MOUSE_CURSOR* MouseCursorInvisible;
|
||||||
|
ALLEGRO_VERTEX_DECL* VertexDecl;
|
||||||
|
char* ClipboardTextData;
|
||||||
|
ImGuiMouseCursor LastCursor;
|
||||||
|
|
||||||
|
ImVector<ImDrawVertAllegro> BufVertices;
|
||||||
|
ImVector<int> BufIndices;
|
||||||
|
|
||||||
|
ImGui_ImplAllegro5_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
// FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
|
||||||
|
static ImGui_ImplAllegro5_Data* ImGui_ImplAllegro5_GetBackendData() { return ImGui::GetCurrentContext() ? (ImGui_ImplAllegro5_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; }
|
||||||
|
|
||||||
|
static void ImGui_ImplAllegro5_SetupRenderState(ImDrawData* draw_data)
|
||||||
|
{
|
||||||
|
// Setup blending
|
||||||
|
al_set_separate_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
|
||||||
|
|
||||||
|
// Setup orthographic projection matrix
|
||||||
|
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
|
||||||
|
{
|
||||||
|
float L = draw_data->DisplayPos.x;
|
||||||
|
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
||||||
|
float T = draw_data->DisplayPos.y;
|
||||||
|
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
||||||
|
ALLEGRO_TRANSFORM transform;
|
||||||
|
al_identity_transform(&transform);
|
||||||
|
al_use_transform(&transform);
|
||||||
|
al_orthographic_transform(&transform, L, T, 1.0f, R, B, -1.0f);
|
||||||
|
al_use_projection_transform(&transform);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render function.
|
||||||
|
void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data)
|
||||||
|
{
|
||||||
|
// Avoid rendering when minimized
|
||||||
|
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplAllegro5_UpdateTexture(tex);
|
||||||
|
|
||||||
|
// Backup Allegro state that will be modified
|
||||||
|
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
|
||||||
|
ALLEGRO_TRANSFORM last_transform = *al_get_current_transform();
|
||||||
|
ALLEGRO_TRANSFORM last_projection_transform = *al_get_current_projection_transform();
|
||||||
|
int last_clip_x, last_clip_y, last_clip_w, last_clip_h;
|
||||||
|
al_get_clipping_rectangle(&last_clip_x, &last_clip_y, &last_clip_w, &last_clip_h);
|
||||||
|
int last_blender_op, last_blender_src, last_blender_dst;
|
||||||
|
al_get_blender(&last_blender_op, &last_blender_src, &last_blender_dst);
|
||||||
|
|
||||||
|
// Setup desired render state
|
||||||
|
ImGui_ImplAllegro5_SetupRenderState(draw_data);
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
|
||||||
|
ImVector<ImDrawVertAllegro>& vertices = bd->BufVertices;
|
||||||
|
#if ALLEGRO_HAS_DRAW_INDEXED_PRIM
|
||||||
|
vertices.resize(draw_list->VtxBuffer.Size);
|
||||||
|
for (int i = 0; i < draw_list->VtxBuffer.Size; i++)
|
||||||
|
{
|
||||||
|
const ImDrawVert* src_v = &draw_list->VtxBuffer[i];
|
||||||
|
ImDrawVertAllegro* dst_v = &vertices[i];
|
||||||
|
DRAW_VERT_IMGUI_TO_ALLEGRO(dst_v, src_v);
|
||||||
|
}
|
||||||
|
const int* indices = nullptr;
|
||||||
|
if (sizeof(ImDrawIdx) == 2)
|
||||||
|
{
|
||||||
|
// FIXME-OPT: Allegro doesn't support 16-bit indices.
|
||||||
|
// You can '#define ImDrawIdx int' in imconfig.h to request Dear ImGui to output 32-bit indices.
|
||||||
|
// Otherwise, we convert them from 16-bit to 32-bit at runtime here, which works perfectly but is a little wasteful.
|
||||||
|
bd->BufIndices.resize(draw_list->IdxBuffer.Size);
|
||||||
|
for (int i = 0; i < draw_list->IdxBuffer.Size; ++i)
|
||||||
|
bd->BufIndices[i] = (int)draw_list->IdxBuffer.Data[i];
|
||||||
|
indices = bd->BufIndices.Data;
|
||||||
|
}
|
||||||
|
else if (sizeof(ImDrawIdx) == 4)
|
||||||
|
{
|
||||||
|
indices = (const int*)draw_list->IdxBuffer.Data;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
// Allegro's implementation of al_draw_indexed_prim() for DX9 was broken until 5.2.5. Unindex buffers ourselves while converting vertex format.
|
||||||
|
vertices.resize(draw_list->IdxBuffer.Size);
|
||||||
|
for (int i = 0; i < draw_list->IdxBuffer.Size; i++)
|
||||||
|
{
|
||||||
|
const ImDrawVert* src_v = &draw_list->VtxBuffer[draw_list->IdxBuffer[i]];
|
||||||
|
ImDrawVertAllegro* dst_v = &vertices[i];
|
||||||
|
DRAW_VERT_IMGUI_TO_ALLEGRO(dst_v, src_v);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos;
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplAllegro5_SetupRenderState(draw_data);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y);
|
||||||
|
ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y);
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Apply scissor/clipping rectangle, Draw
|
||||||
|
ALLEGRO_BITMAP* texture = (ALLEGRO_BITMAP*)pcmd->GetTexID();
|
||||||
|
al_set_clipping_rectangle(clip_min.x, clip_min.y, clip_max.x - clip_min.x, clip_max.y - clip_min.y);
|
||||||
|
#if ALLEGRO_HAS_DRAW_INDEXED_PRIM
|
||||||
|
al_draw_indexed_prim(&vertices[0], bd->VertexDecl, texture, &indices[pcmd->IdxOffset], pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST);
|
||||||
|
#else
|
||||||
|
al_draw_prim(&vertices[0], bd->VertexDecl, texture, pcmd->IdxOffset, pcmd->IdxOffset + pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore modified Allegro state
|
||||||
|
al_set_blender(last_blender_op, last_blender_src, last_blender_dst);
|
||||||
|
al_set_clipping_rectangle(last_clip_x, last_clip_y, last_clip_w, last_clip_h);
|
||||||
|
al_use_transform(&last_transform);
|
||||||
|
al_use_projection_transform(&last_projection_transform);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplAllegro5_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
|
||||||
|
|
||||||
|
// Create an invisible mouse cursor
|
||||||
|
// Because al_hide_mouse_cursor() seems to mess up with the actual inputs..
|
||||||
|
ALLEGRO_BITMAP* mouse_cursor = al_create_bitmap(8, 8);
|
||||||
|
bd->MouseCursorInvisible = al_create_mouse_cursor(mouse_cursor, 0, 0);
|
||||||
|
al_destroy_bitmap(mouse_cursor);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplAllegro5_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
|
||||||
|
// Create texture
|
||||||
|
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
|
||||||
|
const int new_bitmap_flags = al_get_new_bitmap_flags();
|
||||||
|
int new_bitmap_format = al_get_new_bitmap_format();
|
||||||
|
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP | ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);
|
||||||
|
al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE);
|
||||||
|
ALLEGRO_BITMAP* cpu_bitmap = al_create_bitmap(tex->Width, tex->Height);
|
||||||
|
al_set_new_bitmap_flags(new_bitmap_flags);
|
||||||
|
al_set_new_bitmap_format(new_bitmap_format);
|
||||||
|
IM_ASSERT(cpu_bitmap != nullptr && "Backend failed to create texture!");
|
||||||
|
|
||||||
|
// Upload pixels
|
||||||
|
ALLEGRO_LOCKED_REGION* locked_region = al_lock_bitmap(cpu_bitmap, al_get_bitmap_format(cpu_bitmap), ALLEGRO_LOCK_WRITEONLY);
|
||||||
|
IM_ASSERT(locked_region != nullptr && "Backend failed to create texture!");
|
||||||
|
memcpy(locked_region->data, tex->GetPixels(), tex->GetSizeInBytes());
|
||||||
|
al_unlock_bitmap(cpu_bitmap);
|
||||||
|
|
||||||
|
// Convert software texture to hardware texture.
|
||||||
|
ALLEGRO_BITMAP* gpu_bitmap = al_clone_bitmap(cpu_bitmap);
|
||||||
|
al_destroy_bitmap(cpu_bitmap);
|
||||||
|
IM_ASSERT(gpu_bitmap != nullptr && "Backend failed to create texture!");
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)(intptr_t)gpu_bitmap);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
// Update selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
|
||||||
|
ImTextureRect r = tex->UpdateRect; // Bounding box encompassing all individual updates
|
||||||
|
ALLEGRO_BITMAP* gpu_bitmap = (ALLEGRO_BITMAP*)(intptr_t)tex->TexID;
|
||||||
|
ALLEGRO_LOCKED_REGION* locked_region = al_lock_bitmap_region(gpu_bitmap, r.x, r.y, r.w, r.h, al_get_bitmap_format(gpu_bitmap), ALLEGRO_LOCK_WRITEONLY);
|
||||||
|
IM_ASSERT(locked_region && "Backend failed to update texture!");
|
||||||
|
for (int y = 0; y < r.h; y++)
|
||||||
|
memcpy((unsigned char*)locked_region->data + locked_region->pitch * y, tex->GetPixelsAt(r.x, r.y + y), r.w * tex->BytesPerPixel); // dst, src, block pitch
|
||||||
|
al_unlock_bitmap(gpu_bitmap);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantDestroy)
|
||||||
|
{
|
||||||
|
ALLEGRO_BITMAP* backend_tex = (ALLEGRO_BITMAP*)(intptr_t)tex->TexID;
|
||||||
|
if (backend_tex)
|
||||||
|
al_destroy_bitmap(backend_tex);
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplAllegro5_InvalidateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
|
||||||
|
|
||||||
|
// Destroy all textures
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
{
|
||||||
|
tex->SetStatus(ImTextureStatus_WantDestroy);
|
||||||
|
ImGui_ImplAllegro5_UpdateTexture(tex);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroy mouse cursor
|
||||||
|
if (bd->MouseCursorInvisible)
|
||||||
|
{
|
||||||
|
al_destroy_mouse_cursor(bd->MouseCursorInvisible);
|
||||||
|
bd->MouseCursorInvisible = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if ALLEGRO_HAS_CLIPBOARD
|
||||||
|
static const char* ImGui_ImplAllegro5_GetClipboardText(ImGuiContext*)
|
||||||
|
{
|
||||||
|
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
|
||||||
|
if (bd->ClipboardTextData)
|
||||||
|
al_free(bd->ClipboardTextData);
|
||||||
|
bd->ClipboardTextData = al_get_clipboard_text(bd->Display);
|
||||||
|
return bd->ClipboardTextData;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplAllegro5_SetClipboardText(ImGuiContext*, const char* text)
|
||||||
|
{
|
||||||
|
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
|
||||||
|
al_set_clipboard_text(bd->Display, text);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Not static to allow third-party code to use that if they want to (but undocumented)
|
||||||
|
ImGuiKey ImGui_ImplAllegro5_KeyCodeToImGuiKey(int key_code);
|
||||||
|
ImGuiKey ImGui_ImplAllegro5_KeyCodeToImGuiKey(int key_code)
|
||||||
|
{
|
||||||
|
switch (key_code)
|
||||||
|
{
|
||||||
|
case ALLEGRO_KEY_TAB: return ImGuiKey_Tab;
|
||||||
|
case ALLEGRO_KEY_LEFT: return ImGuiKey_LeftArrow;
|
||||||
|
case ALLEGRO_KEY_RIGHT: return ImGuiKey_RightArrow;
|
||||||
|
case ALLEGRO_KEY_UP: return ImGuiKey_UpArrow;
|
||||||
|
case ALLEGRO_KEY_DOWN: return ImGuiKey_DownArrow;
|
||||||
|
case ALLEGRO_KEY_PGUP: return ImGuiKey_PageUp;
|
||||||
|
case ALLEGRO_KEY_PGDN: return ImGuiKey_PageDown;
|
||||||
|
case ALLEGRO_KEY_HOME: return ImGuiKey_Home;
|
||||||
|
case ALLEGRO_KEY_END: return ImGuiKey_End;
|
||||||
|
case ALLEGRO_KEY_INSERT: return ImGuiKey_Insert;
|
||||||
|
case ALLEGRO_KEY_DELETE: return ImGuiKey_Delete;
|
||||||
|
case ALLEGRO_KEY_BACKSPACE: return ImGuiKey_Backspace;
|
||||||
|
case ALLEGRO_KEY_SPACE: return ImGuiKey_Space;
|
||||||
|
case ALLEGRO_KEY_ENTER: return ImGuiKey_Enter;
|
||||||
|
case ALLEGRO_KEY_ESCAPE: return ImGuiKey_Escape;
|
||||||
|
case ALLEGRO_KEY_QUOTE: return ImGuiKey_Apostrophe;
|
||||||
|
case ALLEGRO_KEY_COMMA: return ImGuiKey_Comma;
|
||||||
|
case ALLEGRO_KEY_MINUS: return ImGuiKey_Minus;
|
||||||
|
case ALLEGRO_KEY_FULLSTOP: return ImGuiKey_Period;
|
||||||
|
case ALLEGRO_KEY_SLASH: return ImGuiKey_Slash;
|
||||||
|
case ALLEGRO_KEY_SEMICOLON: return ImGuiKey_Semicolon;
|
||||||
|
case ALLEGRO_KEY_EQUALS: return ImGuiKey_Equal;
|
||||||
|
case ALLEGRO_KEY_OPENBRACE: return ImGuiKey_LeftBracket;
|
||||||
|
case ALLEGRO_KEY_BACKSLASH: return ImGuiKey_Backslash;
|
||||||
|
case ALLEGRO_KEY_CLOSEBRACE: return ImGuiKey_RightBracket;
|
||||||
|
case ALLEGRO_KEY_TILDE: return ImGuiKey_GraveAccent;
|
||||||
|
case ALLEGRO_KEY_CAPSLOCK: return ImGuiKey_CapsLock;
|
||||||
|
case ALLEGRO_KEY_SCROLLLOCK: return ImGuiKey_ScrollLock;
|
||||||
|
case ALLEGRO_KEY_NUMLOCK: return ImGuiKey_NumLock;
|
||||||
|
case ALLEGRO_KEY_PRINTSCREEN: return ImGuiKey_PrintScreen;
|
||||||
|
case ALLEGRO_KEY_PAUSE: return ImGuiKey_Pause;
|
||||||
|
case ALLEGRO_KEY_PAD_0: return ImGuiKey_Keypad0;
|
||||||
|
case ALLEGRO_KEY_PAD_1: return ImGuiKey_Keypad1;
|
||||||
|
case ALLEGRO_KEY_PAD_2: return ImGuiKey_Keypad2;
|
||||||
|
case ALLEGRO_KEY_PAD_3: return ImGuiKey_Keypad3;
|
||||||
|
case ALLEGRO_KEY_PAD_4: return ImGuiKey_Keypad4;
|
||||||
|
case ALLEGRO_KEY_PAD_5: return ImGuiKey_Keypad5;
|
||||||
|
case ALLEGRO_KEY_PAD_6: return ImGuiKey_Keypad6;
|
||||||
|
case ALLEGRO_KEY_PAD_7: return ImGuiKey_Keypad7;
|
||||||
|
case ALLEGRO_KEY_PAD_8: return ImGuiKey_Keypad8;
|
||||||
|
case ALLEGRO_KEY_PAD_9: return ImGuiKey_Keypad9;
|
||||||
|
case ALLEGRO_KEY_PAD_DELETE: return ImGuiKey_KeypadDecimal;
|
||||||
|
case ALLEGRO_KEY_PAD_SLASH: return ImGuiKey_KeypadDivide;
|
||||||
|
case ALLEGRO_KEY_PAD_ASTERISK: return ImGuiKey_KeypadMultiply;
|
||||||
|
case ALLEGRO_KEY_PAD_MINUS: return ImGuiKey_KeypadSubtract;
|
||||||
|
case ALLEGRO_KEY_PAD_PLUS: return ImGuiKey_KeypadAdd;
|
||||||
|
case ALLEGRO_KEY_PAD_ENTER: return ImGuiKey_KeypadEnter;
|
||||||
|
case ALLEGRO_KEY_PAD_EQUALS: return ImGuiKey_KeypadEqual;
|
||||||
|
case ALLEGRO_KEY_LCTRL: return ImGuiKey_LeftCtrl;
|
||||||
|
case ALLEGRO_KEY_LSHIFT: return ImGuiKey_LeftShift;
|
||||||
|
case ALLEGRO_KEY_ALT: return ImGuiKey_LeftAlt;
|
||||||
|
case ALLEGRO_KEY_LWIN: return ImGuiKey_LeftSuper;
|
||||||
|
case ALLEGRO_KEY_RCTRL: return ImGuiKey_RightCtrl;
|
||||||
|
case ALLEGRO_KEY_RSHIFT: return ImGuiKey_RightShift;
|
||||||
|
case ALLEGRO_KEY_ALTGR: return ImGuiKey_RightAlt;
|
||||||
|
case ALLEGRO_KEY_RWIN: return ImGuiKey_RightSuper;
|
||||||
|
case ALLEGRO_KEY_MENU: return ImGuiKey_Menu;
|
||||||
|
case ALLEGRO_KEY_0: return ImGuiKey_0;
|
||||||
|
case ALLEGRO_KEY_1: return ImGuiKey_1;
|
||||||
|
case ALLEGRO_KEY_2: return ImGuiKey_2;
|
||||||
|
case ALLEGRO_KEY_3: return ImGuiKey_3;
|
||||||
|
case ALLEGRO_KEY_4: return ImGuiKey_4;
|
||||||
|
case ALLEGRO_KEY_5: return ImGuiKey_5;
|
||||||
|
case ALLEGRO_KEY_6: return ImGuiKey_6;
|
||||||
|
case ALLEGRO_KEY_7: return ImGuiKey_7;
|
||||||
|
case ALLEGRO_KEY_8: return ImGuiKey_8;
|
||||||
|
case ALLEGRO_KEY_9: return ImGuiKey_9;
|
||||||
|
case ALLEGRO_KEY_A: return ImGuiKey_A;
|
||||||
|
case ALLEGRO_KEY_B: return ImGuiKey_B;
|
||||||
|
case ALLEGRO_KEY_C: return ImGuiKey_C;
|
||||||
|
case ALLEGRO_KEY_D: return ImGuiKey_D;
|
||||||
|
case ALLEGRO_KEY_E: return ImGuiKey_E;
|
||||||
|
case ALLEGRO_KEY_F: return ImGuiKey_F;
|
||||||
|
case ALLEGRO_KEY_G: return ImGuiKey_G;
|
||||||
|
case ALLEGRO_KEY_H: return ImGuiKey_H;
|
||||||
|
case ALLEGRO_KEY_I: return ImGuiKey_I;
|
||||||
|
case ALLEGRO_KEY_J: return ImGuiKey_J;
|
||||||
|
case ALLEGRO_KEY_K: return ImGuiKey_K;
|
||||||
|
case ALLEGRO_KEY_L: return ImGuiKey_L;
|
||||||
|
case ALLEGRO_KEY_M: return ImGuiKey_M;
|
||||||
|
case ALLEGRO_KEY_N: return ImGuiKey_N;
|
||||||
|
case ALLEGRO_KEY_O: return ImGuiKey_O;
|
||||||
|
case ALLEGRO_KEY_P: return ImGuiKey_P;
|
||||||
|
case ALLEGRO_KEY_Q: return ImGuiKey_Q;
|
||||||
|
case ALLEGRO_KEY_R: return ImGuiKey_R;
|
||||||
|
case ALLEGRO_KEY_S: return ImGuiKey_S;
|
||||||
|
case ALLEGRO_KEY_T: return ImGuiKey_T;
|
||||||
|
case ALLEGRO_KEY_U: return ImGuiKey_U;
|
||||||
|
case ALLEGRO_KEY_V: return ImGuiKey_V;
|
||||||
|
case ALLEGRO_KEY_W: return ImGuiKey_W;
|
||||||
|
case ALLEGRO_KEY_X: return ImGuiKey_X;
|
||||||
|
case ALLEGRO_KEY_Y: return ImGuiKey_Y;
|
||||||
|
case ALLEGRO_KEY_Z: return ImGuiKey_Z;
|
||||||
|
case ALLEGRO_KEY_F1: return ImGuiKey_F1;
|
||||||
|
case ALLEGRO_KEY_F2: return ImGuiKey_F2;
|
||||||
|
case ALLEGRO_KEY_F3: return ImGuiKey_F3;
|
||||||
|
case ALLEGRO_KEY_F4: return ImGuiKey_F4;
|
||||||
|
case ALLEGRO_KEY_F5: return ImGuiKey_F5;
|
||||||
|
case ALLEGRO_KEY_F6: return ImGuiKey_F6;
|
||||||
|
case ALLEGRO_KEY_F7: return ImGuiKey_F7;
|
||||||
|
case ALLEGRO_KEY_F8: return ImGuiKey_F8;
|
||||||
|
case ALLEGRO_KEY_F9: return ImGuiKey_F9;
|
||||||
|
case ALLEGRO_KEY_F10: return ImGuiKey_F10;
|
||||||
|
case ALLEGRO_KEY_F11: return ImGuiKey_F11;
|
||||||
|
case ALLEGRO_KEY_F12: return ImGuiKey_F12;
|
||||||
|
default: return ImGuiKey_None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY* display)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplAllegro5_Data* bd = IM_NEW(ImGui_ImplAllegro5_Data)();
|
||||||
|
io.BackendPlatformUserData = (void*)bd;
|
||||||
|
io.BackendPlatformName = io.BackendRendererName = "imgui_impl_allegro5";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
bd->Display = display;
|
||||||
|
bd->LastCursor = ALLEGRO_SYSTEM_MOUSE_CURSOR_NONE;
|
||||||
|
|
||||||
|
// Create custom vertex declaration.
|
||||||
|
// Unfortunately Allegro doesn't support 32-bit packed colors so we have to convert them to 4 floats.
|
||||||
|
// We still use a custom declaration to use 'ALLEGRO_PRIM_TEX_COORD' instead of 'ALLEGRO_PRIM_TEX_COORD_PIXEL' else we can't do a reliable conversion.
|
||||||
|
ALLEGRO_VERTEX_ELEMENT elems[] =
|
||||||
|
{
|
||||||
|
{ ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, pos) },
|
||||||
|
{ ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, uv) },
|
||||||
|
{ ALLEGRO_PRIM_COLOR_ATTR, 0, offsetof(ImDrawVertAllegro, col) },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
};
|
||||||
|
bd->VertexDecl = al_create_vertex_decl(elems, sizeof(ImDrawVertAllegro));
|
||||||
|
|
||||||
|
#if ALLEGRO_HAS_CLIPBOARD
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
platform_io.Platform_SetClipboardTextFn = ImGui_ImplAllegro5_SetClipboardText;
|
||||||
|
platform_io.Platform_GetClipboardTextFn = ImGui_ImplAllegro5_GetClipboardText;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplAllegro5_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
ImGui_ImplAllegro5_InvalidateDeviceObjects();
|
||||||
|
if (bd->VertexDecl)
|
||||||
|
al_destroy_vertex_decl(bd->VertexDecl);
|
||||||
|
if (bd->ClipboardTextData)
|
||||||
|
al_free(bd->ClipboardTextData);
|
||||||
|
|
||||||
|
io.BackendPlatformName = io.BackendRendererName = nullptr;
|
||||||
|
io.BackendPlatformUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ev->keyboard.modifiers seems always zero so using that...
|
||||||
|
static void ImGui_ImplAllegro5_UpdateKeyModifiers()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
ALLEGRO_KEYBOARD_STATE keys;
|
||||||
|
al_get_keyboard_state(&keys);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Ctrl, al_key_down(&keys, ALLEGRO_KEY_LCTRL) || al_key_down(&keys, ALLEGRO_KEY_RCTRL));
|
||||||
|
io.AddKeyEvent(ImGuiMod_Shift, al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT));
|
||||||
|
io.AddKeyEvent(ImGuiMod_Alt, al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR));
|
||||||
|
io.AddKeyEvent(ImGuiMod_Super, al_key_down(&keys, ALLEGRO_KEY_LWIN) || al_key_down(&keys, ALLEGRO_KEY_RWIN));
|
||||||
|
}
|
||||||
|
|
||||||
|
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
||||||
|
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
|
||||||
|
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
|
||||||
|
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
||||||
|
bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT* ev)
|
||||||
|
{
|
||||||
|
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplAllegro5_Init()?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
switch (ev->type)
|
||||||
|
{
|
||||||
|
case ALLEGRO_EVENT_MOUSE_AXES:
|
||||||
|
if (ev->mouse.display == bd->Display)
|
||||||
|
{
|
||||||
|
io.AddMousePosEvent(ev->mouse.x, ev->mouse.y);
|
||||||
|
io.AddMouseWheelEvent(-ev->mouse.dw, ev->mouse.dz);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
|
||||||
|
case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
|
||||||
|
if (ev->mouse.display == bd->Display && ev->mouse.button > 0 && ev->mouse.button <= 5)
|
||||||
|
io.AddMouseButtonEvent(ev->mouse.button - 1, ev->type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN);
|
||||||
|
return true;
|
||||||
|
case ALLEGRO_EVENT_TOUCH_MOVE:
|
||||||
|
if (ev->touch.display == bd->Display)
|
||||||
|
io.AddMousePosEvent(ev->touch.x, ev->touch.y);
|
||||||
|
return true;
|
||||||
|
case ALLEGRO_EVENT_TOUCH_BEGIN:
|
||||||
|
case ALLEGRO_EVENT_TOUCH_END:
|
||||||
|
case ALLEGRO_EVENT_TOUCH_CANCEL:
|
||||||
|
if (ev->touch.display == bd->Display && ev->touch.primary)
|
||||||
|
io.AddMouseButtonEvent(0, ev->type == ALLEGRO_EVENT_TOUCH_BEGIN);
|
||||||
|
return true;
|
||||||
|
case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
|
||||||
|
if (ev->mouse.display == bd->Display)
|
||||||
|
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
|
||||||
|
return true;
|
||||||
|
case ALLEGRO_EVENT_KEY_CHAR:
|
||||||
|
if (ev->keyboard.display == bd->Display)
|
||||||
|
if (ev->keyboard.unichar != 0)
|
||||||
|
io.AddInputCharacter((unsigned int)ev->keyboard.unichar);
|
||||||
|
return true;
|
||||||
|
case ALLEGRO_EVENT_KEY_DOWN:
|
||||||
|
case ALLEGRO_EVENT_KEY_UP:
|
||||||
|
if (ev->keyboard.display == bd->Display)
|
||||||
|
{
|
||||||
|
ImGui_ImplAllegro5_UpdateKeyModifiers();
|
||||||
|
ImGuiKey key = ImGui_ImplAllegro5_KeyCodeToImGuiKey(ev->keyboard.keycode);
|
||||||
|
io.AddKeyEvent(key, (ev->type == ALLEGRO_EVENT_KEY_DOWN));
|
||||||
|
io.SetKeyEventNativeData(key, ev->keyboard.keycode, -1); // To support legacy indexing (<1.87 user code)
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
|
||||||
|
if (ev->display.source == bd->Display)
|
||||||
|
io.AddFocusEvent(false);
|
||||||
|
return true;
|
||||||
|
case ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
|
||||||
|
if (ev->display.source == bd->Display)
|
||||||
|
{
|
||||||
|
io.AddFocusEvent(true);
|
||||||
|
#if defined(ALLEGRO_UNSTABLE)
|
||||||
|
al_clear_keyboard_state(bd->Display);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplAllegro5_UpdateMouseCursor()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
|
||||||
|
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
|
||||||
|
|
||||||
|
// Hide OS mouse cursor if imgui is drawing it
|
||||||
|
if (io.MouseDrawCursor)
|
||||||
|
imgui_cursor = ImGuiMouseCursor_None;
|
||||||
|
|
||||||
|
if (bd->LastCursor == imgui_cursor)
|
||||||
|
return;
|
||||||
|
bd->LastCursor = imgui_cursor;
|
||||||
|
if (imgui_cursor == ImGuiMouseCursor_None)
|
||||||
|
{
|
||||||
|
al_set_mouse_cursor(bd->Display, bd->MouseCursorInvisible);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
|
||||||
|
switch (imgui_cursor)
|
||||||
|
{
|
||||||
|
case ImGuiMouseCursor_TextInput: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT; break;
|
||||||
|
case ImGuiMouseCursor_ResizeAll: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE; break;
|
||||||
|
case ImGuiMouseCursor_ResizeNS: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N; break;
|
||||||
|
case ImGuiMouseCursor_ResizeEW: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E; break;
|
||||||
|
case ImGuiMouseCursor_ResizeNESW: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE; break;
|
||||||
|
case ImGuiMouseCursor_ResizeNWSE: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW; break;
|
||||||
|
case ImGuiMouseCursor_Wait: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_BUSY; break;
|
||||||
|
case ImGuiMouseCursor_Progress: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_PROGRESS; break;
|
||||||
|
case ImGuiMouseCursor_NotAllowed: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_UNAVAILABLE; break;
|
||||||
|
}
|
||||||
|
al_set_system_mouse_cursor(bd->Display, cursor_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplAllegro5_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplAllegro5_Init()?");
|
||||||
|
|
||||||
|
if (!bd->MouseCursorInvisible)
|
||||||
|
ImGui_ImplAllegro5_CreateDeviceObjects();
|
||||||
|
|
||||||
|
// Setup display size (every frame to accommodate for window resizing)
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
int w, h;
|
||||||
|
w = al_get_display_width(bd->Display);
|
||||||
|
h = al_get_display_height(bd->Display);
|
||||||
|
io.DisplaySize = ImVec2((float)w, (float)h);
|
||||||
|
|
||||||
|
// Setup time step
|
||||||
|
double current_time = al_get_time();
|
||||||
|
io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f);
|
||||||
|
bd->Time = current_time;
|
||||||
|
|
||||||
|
// Setup mouse cursor shape
|
||||||
|
ImGui_ImplAllegro5_UpdateMouseCursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
43
imgui/backends/imgui_impl_allegro5.h
Normal file
43
imgui/backends/imgui_impl_allegro5.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
// dear imgui: Renderer + Platform Backend for Allegro 5
|
||||||
|
// (Info: Allegro 5 is a cross-platform general purpose library for handling windows, inputs, graphics, etc.)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'ALLEGRO_BITMAP*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy ALLEGRO_KEY_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Clipboard support (from Allegro 5.1.12).
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
// Missing features or Issues:
|
||||||
|
// [ ] Renderer: The renderer is suboptimal as we need to unindex our buffers and convert vertices manually.
|
||||||
|
// [ ] Platform: Missing gamepad support.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
struct ALLEGRO_DISPLAY;
|
||||||
|
union ALLEGRO_EVENT;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY* display);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplAllegro5_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplAllegro5_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT* event);
|
||||||
|
|
||||||
|
// Use if you want to reset your rendering device without losing Dear ImGui state.
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplAllegro5_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplAllegro5_InvalidateDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplAllegro5_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
308
imgui/backends/imgui_impl_android.cpp
Normal file
308
imgui/backends/imgui_impl_android.cpp
Normal file
@@ -0,0 +1,308 @@
|
|||||||
|
// dear imgui: Platform Binding for Android native app
|
||||||
|
// This needs to be used along with the OpenGL 3 Renderer (imgui_impl_opengl3)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy AKEYCODE_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen.
|
||||||
|
// Missing features or Issues:
|
||||||
|
// [ ] Platform: Clipboard support.
|
||||||
|
// [ ] Platform: Gamepad support.
|
||||||
|
// [ ] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: Check if this is even possible with Android.
|
||||||
|
// Important:
|
||||||
|
// - Consider using SDL or GLFW backend on Android, which will be more full-featured than this.
|
||||||
|
// - FIXME: On-screen keyboard currently needs to be enabled by the application (see examples/ and issue #3446)
|
||||||
|
// - FIXME: Unicode character inputs needs to be passed by Dear ImGui by the application (see examples/ and issue #3446)
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
|
||||||
|
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
|
||||||
|
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
|
||||||
|
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
|
||||||
|
// 2021-03-04: Initial version.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_android.h"
|
||||||
|
#include <time.h>
|
||||||
|
#include <android/native_window.h>
|
||||||
|
#include <android/input.h>
|
||||||
|
#include <android/keycodes.h>
|
||||||
|
#include <android/log.h>
|
||||||
|
|
||||||
|
// Android data
|
||||||
|
static double g_Time = 0.0;
|
||||||
|
static ANativeWindow* g_Window;
|
||||||
|
static char g_LogTag[] = "ImGuiExample";
|
||||||
|
|
||||||
|
static ImGuiKey ImGui_ImplAndroid_KeyCodeToImGuiKey(int32_t key_code)
|
||||||
|
{
|
||||||
|
switch (key_code)
|
||||||
|
{
|
||||||
|
case AKEYCODE_TAB: return ImGuiKey_Tab;
|
||||||
|
case AKEYCODE_DPAD_LEFT: return ImGuiKey_LeftArrow;
|
||||||
|
case AKEYCODE_DPAD_RIGHT: return ImGuiKey_RightArrow;
|
||||||
|
case AKEYCODE_DPAD_UP: return ImGuiKey_UpArrow;
|
||||||
|
case AKEYCODE_DPAD_DOWN: return ImGuiKey_DownArrow;
|
||||||
|
case AKEYCODE_PAGE_UP: return ImGuiKey_PageUp;
|
||||||
|
case AKEYCODE_PAGE_DOWN: return ImGuiKey_PageDown;
|
||||||
|
case AKEYCODE_MOVE_HOME: return ImGuiKey_Home;
|
||||||
|
case AKEYCODE_MOVE_END: return ImGuiKey_End;
|
||||||
|
case AKEYCODE_INSERT: return ImGuiKey_Insert;
|
||||||
|
case AKEYCODE_FORWARD_DEL: return ImGuiKey_Delete;
|
||||||
|
case AKEYCODE_DEL: return ImGuiKey_Backspace;
|
||||||
|
case AKEYCODE_SPACE: return ImGuiKey_Space;
|
||||||
|
case AKEYCODE_ENTER: return ImGuiKey_Enter;
|
||||||
|
case AKEYCODE_ESCAPE: return ImGuiKey_Escape;
|
||||||
|
case AKEYCODE_APOSTROPHE: return ImGuiKey_Apostrophe;
|
||||||
|
case AKEYCODE_COMMA: return ImGuiKey_Comma;
|
||||||
|
case AKEYCODE_MINUS: return ImGuiKey_Minus;
|
||||||
|
case AKEYCODE_PERIOD: return ImGuiKey_Period;
|
||||||
|
case AKEYCODE_SLASH: return ImGuiKey_Slash;
|
||||||
|
case AKEYCODE_SEMICOLON: return ImGuiKey_Semicolon;
|
||||||
|
case AKEYCODE_EQUALS: return ImGuiKey_Equal;
|
||||||
|
case AKEYCODE_LEFT_BRACKET: return ImGuiKey_LeftBracket;
|
||||||
|
case AKEYCODE_BACKSLASH: return ImGuiKey_Backslash;
|
||||||
|
case AKEYCODE_RIGHT_BRACKET: return ImGuiKey_RightBracket;
|
||||||
|
case AKEYCODE_GRAVE: return ImGuiKey_GraveAccent;
|
||||||
|
case AKEYCODE_CAPS_LOCK: return ImGuiKey_CapsLock;
|
||||||
|
case AKEYCODE_SCROLL_LOCK: return ImGuiKey_ScrollLock;
|
||||||
|
case AKEYCODE_NUM_LOCK: return ImGuiKey_NumLock;
|
||||||
|
case AKEYCODE_SYSRQ: return ImGuiKey_PrintScreen;
|
||||||
|
case AKEYCODE_BREAK: return ImGuiKey_Pause;
|
||||||
|
case AKEYCODE_NUMPAD_0: return ImGuiKey_Keypad0;
|
||||||
|
case AKEYCODE_NUMPAD_1: return ImGuiKey_Keypad1;
|
||||||
|
case AKEYCODE_NUMPAD_2: return ImGuiKey_Keypad2;
|
||||||
|
case AKEYCODE_NUMPAD_3: return ImGuiKey_Keypad3;
|
||||||
|
case AKEYCODE_NUMPAD_4: return ImGuiKey_Keypad4;
|
||||||
|
case AKEYCODE_NUMPAD_5: return ImGuiKey_Keypad5;
|
||||||
|
case AKEYCODE_NUMPAD_6: return ImGuiKey_Keypad6;
|
||||||
|
case AKEYCODE_NUMPAD_7: return ImGuiKey_Keypad7;
|
||||||
|
case AKEYCODE_NUMPAD_8: return ImGuiKey_Keypad8;
|
||||||
|
case AKEYCODE_NUMPAD_9: return ImGuiKey_Keypad9;
|
||||||
|
case AKEYCODE_NUMPAD_DOT: return ImGuiKey_KeypadDecimal;
|
||||||
|
case AKEYCODE_NUMPAD_DIVIDE: return ImGuiKey_KeypadDivide;
|
||||||
|
case AKEYCODE_NUMPAD_MULTIPLY: return ImGuiKey_KeypadMultiply;
|
||||||
|
case AKEYCODE_NUMPAD_SUBTRACT: return ImGuiKey_KeypadSubtract;
|
||||||
|
case AKEYCODE_NUMPAD_ADD: return ImGuiKey_KeypadAdd;
|
||||||
|
case AKEYCODE_NUMPAD_ENTER: return ImGuiKey_KeypadEnter;
|
||||||
|
case AKEYCODE_NUMPAD_EQUALS: return ImGuiKey_KeypadEqual;
|
||||||
|
case AKEYCODE_CTRL_LEFT: return ImGuiKey_LeftCtrl;
|
||||||
|
case AKEYCODE_SHIFT_LEFT: return ImGuiKey_LeftShift;
|
||||||
|
case AKEYCODE_ALT_LEFT: return ImGuiKey_LeftAlt;
|
||||||
|
case AKEYCODE_META_LEFT: return ImGuiKey_LeftSuper;
|
||||||
|
case AKEYCODE_CTRL_RIGHT: return ImGuiKey_RightCtrl;
|
||||||
|
case AKEYCODE_SHIFT_RIGHT: return ImGuiKey_RightShift;
|
||||||
|
case AKEYCODE_ALT_RIGHT: return ImGuiKey_RightAlt;
|
||||||
|
case AKEYCODE_META_RIGHT: return ImGuiKey_RightSuper;
|
||||||
|
case AKEYCODE_MENU: return ImGuiKey_Menu;
|
||||||
|
case AKEYCODE_0: return ImGuiKey_0;
|
||||||
|
case AKEYCODE_1: return ImGuiKey_1;
|
||||||
|
case AKEYCODE_2: return ImGuiKey_2;
|
||||||
|
case AKEYCODE_3: return ImGuiKey_3;
|
||||||
|
case AKEYCODE_4: return ImGuiKey_4;
|
||||||
|
case AKEYCODE_5: return ImGuiKey_5;
|
||||||
|
case AKEYCODE_6: return ImGuiKey_6;
|
||||||
|
case AKEYCODE_7: return ImGuiKey_7;
|
||||||
|
case AKEYCODE_8: return ImGuiKey_8;
|
||||||
|
case AKEYCODE_9: return ImGuiKey_9;
|
||||||
|
case AKEYCODE_A: return ImGuiKey_A;
|
||||||
|
case AKEYCODE_B: return ImGuiKey_B;
|
||||||
|
case AKEYCODE_C: return ImGuiKey_C;
|
||||||
|
case AKEYCODE_D: return ImGuiKey_D;
|
||||||
|
case AKEYCODE_E: return ImGuiKey_E;
|
||||||
|
case AKEYCODE_F: return ImGuiKey_F;
|
||||||
|
case AKEYCODE_G: return ImGuiKey_G;
|
||||||
|
case AKEYCODE_H: return ImGuiKey_H;
|
||||||
|
case AKEYCODE_I: return ImGuiKey_I;
|
||||||
|
case AKEYCODE_J: return ImGuiKey_J;
|
||||||
|
case AKEYCODE_K: return ImGuiKey_K;
|
||||||
|
case AKEYCODE_L: return ImGuiKey_L;
|
||||||
|
case AKEYCODE_M: return ImGuiKey_M;
|
||||||
|
case AKEYCODE_N: return ImGuiKey_N;
|
||||||
|
case AKEYCODE_O: return ImGuiKey_O;
|
||||||
|
case AKEYCODE_P: return ImGuiKey_P;
|
||||||
|
case AKEYCODE_Q: return ImGuiKey_Q;
|
||||||
|
case AKEYCODE_R: return ImGuiKey_R;
|
||||||
|
case AKEYCODE_S: return ImGuiKey_S;
|
||||||
|
case AKEYCODE_T: return ImGuiKey_T;
|
||||||
|
case AKEYCODE_U: return ImGuiKey_U;
|
||||||
|
case AKEYCODE_V: return ImGuiKey_V;
|
||||||
|
case AKEYCODE_W: return ImGuiKey_W;
|
||||||
|
case AKEYCODE_X: return ImGuiKey_X;
|
||||||
|
case AKEYCODE_Y: return ImGuiKey_Y;
|
||||||
|
case AKEYCODE_Z: return ImGuiKey_Z;
|
||||||
|
case AKEYCODE_F1: return ImGuiKey_F1;
|
||||||
|
case AKEYCODE_F2: return ImGuiKey_F2;
|
||||||
|
case AKEYCODE_F3: return ImGuiKey_F3;
|
||||||
|
case AKEYCODE_F4: return ImGuiKey_F4;
|
||||||
|
case AKEYCODE_F5: return ImGuiKey_F5;
|
||||||
|
case AKEYCODE_F6: return ImGuiKey_F6;
|
||||||
|
case AKEYCODE_F7: return ImGuiKey_F7;
|
||||||
|
case AKEYCODE_F8: return ImGuiKey_F8;
|
||||||
|
case AKEYCODE_F9: return ImGuiKey_F9;
|
||||||
|
case AKEYCODE_F10: return ImGuiKey_F10;
|
||||||
|
case AKEYCODE_F11: return ImGuiKey_F11;
|
||||||
|
case AKEYCODE_F12: return ImGuiKey_F12;
|
||||||
|
default: return ImGuiKey_None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t ImGui_ImplAndroid_HandleInputEvent(const AInputEvent* input_event)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
int32_t event_type = AInputEvent_getType(input_event);
|
||||||
|
switch (event_type)
|
||||||
|
{
|
||||||
|
case AINPUT_EVENT_TYPE_KEY:
|
||||||
|
{
|
||||||
|
int32_t event_key_code = AKeyEvent_getKeyCode(input_event);
|
||||||
|
int32_t event_scan_code = AKeyEvent_getScanCode(input_event);
|
||||||
|
int32_t event_action = AKeyEvent_getAction(input_event);
|
||||||
|
int32_t event_meta_state = AKeyEvent_getMetaState(input_event);
|
||||||
|
|
||||||
|
io.AddKeyEvent(ImGuiMod_Ctrl, (event_meta_state & AMETA_CTRL_ON) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Shift, (event_meta_state & AMETA_SHIFT_ON) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Alt, (event_meta_state & AMETA_ALT_ON) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Super, (event_meta_state & AMETA_META_ON) != 0);
|
||||||
|
|
||||||
|
switch (event_action)
|
||||||
|
{
|
||||||
|
// FIXME: AKEY_EVENT_ACTION_DOWN and AKEY_EVENT_ACTION_UP occur at once as soon as a touch pointer
|
||||||
|
// goes up from a key. We use a simple key event queue/ and process one event per key per frame in
|
||||||
|
// ImGui_ImplAndroid_NewFrame()...or consider using IO queue, if suitable: https://github.com/ocornut/imgui/issues/2787
|
||||||
|
case AKEY_EVENT_ACTION_DOWN:
|
||||||
|
case AKEY_EVENT_ACTION_UP:
|
||||||
|
{
|
||||||
|
ImGuiKey key = ImGui_ImplAndroid_KeyCodeToImGuiKey(event_key_code);
|
||||||
|
if (key != ImGuiKey_None)
|
||||||
|
{
|
||||||
|
io.AddKeyEvent(key, event_action == AKEY_EVENT_ACTION_DOWN);
|
||||||
|
io.SetKeyEventNativeData(key, event_key_code, event_scan_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case AINPUT_EVENT_TYPE_MOTION:
|
||||||
|
{
|
||||||
|
int32_t event_action = AMotionEvent_getAction(input_event);
|
||||||
|
int32_t event_pointer_index = (event_action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
|
||||||
|
event_action &= AMOTION_EVENT_ACTION_MASK;
|
||||||
|
|
||||||
|
switch (AMotionEvent_getToolType(input_event, event_pointer_index))
|
||||||
|
{
|
||||||
|
case AMOTION_EVENT_TOOL_TYPE_MOUSE:
|
||||||
|
io.AddMouseSourceEvent(ImGuiMouseSource_Mouse);
|
||||||
|
break;
|
||||||
|
case AMOTION_EVENT_TOOL_TYPE_STYLUS:
|
||||||
|
case AMOTION_EVENT_TOOL_TYPE_ERASER:
|
||||||
|
io.AddMouseSourceEvent(ImGuiMouseSource_Pen);
|
||||||
|
break;
|
||||||
|
case AMOTION_EVENT_TOOL_TYPE_FINGER:
|
||||||
|
default:
|
||||||
|
io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (event_action)
|
||||||
|
{
|
||||||
|
case AMOTION_EVENT_ACTION_DOWN:
|
||||||
|
case AMOTION_EVENT_ACTION_UP:
|
||||||
|
{
|
||||||
|
// Physical mouse buttons (and probably other physical devices) also invoke the actions AMOTION_EVENT_ACTION_DOWN/_UP,
|
||||||
|
// but we have to process them separately to identify the actual button pressed. This is done below via
|
||||||
|
// AMOTION_EVENT_ACTION_BUTTON_PRESS/_RELEASE. Here, we only process "FINGER" input (and "UNKNOWN", as a fallback).
|
||||||
|
int tool_type = AMotionEvent_getToolType(input_event, event_pointer_index);
|
||||||
|
if (tool_type == AMOTION_EVENT_TOOL_TYPE_FINGER || tool_type == AMOTION_EVENT_TOOL_TYPE_UNKNOWN)
|
||||||
|
{
|
||||||
|
io.AddMousePosEvent(AMotionEvent_getX(input_event, event_pointer_index), AMotionEvent_getY(input_event, event_pointer_index));
|
||||||
|
io.AddMouseButtonEvent(0, event_action == AMOTION_EVENT_ACTION_DOWN);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case AMOTION_EVENT_ACTION_BUTTON_PRESS:
|
||||||
|
case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
|
||||||
|
{
|
||||||
|
int32_t button_state = AMotionEvent_getButtonState(input_event);
|
||||||
|
io.AddMouseButtonEvent(0, (button_state & AMOTION_EVENT_BUTTON_PRIMARY) != 0);
|
||||||
|
io.AddMouseButtonEvent(1, (button_state & AMOTION_EVENT_BUTTON_SECONDARY) != 0);
|
||||||
|
io.AddMouseButtonEvent(2, (button_state & AMOTION_EVENT_BUTTON_TERTIARY) != 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case AMOTION_EVENT_ACTION_HOVER_MOVE: // Hovering: Tool moves while NOT pressed (such as a physical mouse)
|
||||||
|
case AMOTION_EVENT_ACTION_MOVE: // Touch pointer moves while DOWN
|
||||||
|
io.AddMousePosEvent(AMotionEvent_getX(input_event, event_pointer_index), AMotionEvent_getY(input_event, event_pointer_index));
|
||||||
|
break;
|
||||||
|
case AMOTION_EVENT_ACTION_SCROLL:
|
||||||
|
io.AddMouseWheelEvent(AMotionEvent_getAxisValue(input_event, AMOTION_EVENT_AXIS_HSCROLL, event_pointer_index), AMotionEvent_getAxisValue(input_event, AMOTION_EVENT_AXIS_VSCROLL, event_pointer_index));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplAndroid_Init(ANativeWindow* window)
|
||||||
|
{
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
|
||||||
|
g_Window = window;
|
||||||
|
g_Time = 0.0;
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.BackendPlatformName = "imgui_impl_android";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplAndroid_Shutdown()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.BackendPlatformName = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplAndroid_NewFrame()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Setup display size (every frame to accommodate for window resizing)
|
||||||
|
int32_t window_width = ANativeWindow_getWidth(g_Window);
|
||||||
|
int32_t window_height = ANativeWindow_getHeight(g_Window);
|
||||||
|
int display_width = window_width;
|
||||||
|
int display_height = window_height;
|
||||||
|
|
||||||
|
io.DisplaySize = ImVec2((float)window_width, (float)window_height);
|
||||||
|
if (window_width > 0 && window_height > 0)
|
||||||
|
io.DisplayFramebufferScale = ImVec2((float)display_width / window_width, (float)display_height / window_height);
|
||||||
|
|
||||||
|
// Setup time step
|
||||||
|
struct timespec current_timespec;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, ¤t_timespec);
|
||||||
|
double current_time = (double)(current_timespec.tv_sec) + (current_timespec.tv_nsec / 1000000000.0);
|
||||||
|
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f);
|
||||||
|
g_Time = current_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
37
imgui/backends/imgui_impl_android.h
Normal file
37
imgui/backends/imgui_impl_android.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
// dear imgui: Platform Binding for Android native app
|
||||||
|
// This needs to be used along with the OpenGL 3 Renderer (imgui_impl_opengl3)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy AKEYCODE_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen.
|
||||||
|
// Missing features or Issues:
|
||||||
|
// [ ] Platform: Clipboard support.
|
||||||
|
// [ ] Platform: Gamepad support.
|
||||||
|
// [ ] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: Check if this is even possible with Android.
|
||||||
|
// Important:
|
||||||
|
// - Consider using SDL or GLFW backend on Android, which will be more full-featured than this.
|
||||||
|
// - FIXME: On-screen keyboard currently needs to be enabled by the application (see examples/ and issue #3446)
|
||||||
|
// - FIXME: Unicode character inputs needs to be passed by Dear ImGui by the application (see examples/ and issue #3446)
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
struct ANativeWindow;
|
||||||
|
struct AInputEvent;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplAndroid_Init(ANativeWindow* window);
|
||||||
|
IMGUI_IMPL_API int32_t ImGui_ImplAndroid_HandleInputEvent(const AInputEvent* input_event);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplAndroid_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplAndroid_NewFrame();
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
657
imgui/backends/imgui_impl_dx10.cpp
Normal file
657
imgui/backends/imgui_impl_dx10.cpp
Normal file
@@ -0,0 +1,657 @@
|
|||||||
|
// dear imgui: Renderer Backend for DirectX10
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. Win32)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'ID3D10ShaderResourceView*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-06-11: DirectX10: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas.
|
||||||
|
// 2025-05-07: DirectX10: Honor draw_data->FramebufferScale to allow for custom backends and experiment using it (consistently with other renderer backends, even though in normal condition it is not set under Windows).
|
||||||
|
// 2025-01-06: DirectX10: Expose selected render state in ImGui_ImplDX10_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
|
||||||
|
// 2024-10-07: DirectX10: Changed default texture sampler to Clamp instead of Repeat/Wrap.
|
||||||
|
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
|
||||||
|
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
|
||||||
|
// 2021-05-19: DirectX10: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
|
||||||
|
// 2021-02-18: DirectX10: Change blending equation to preserve alpha in output buffer.
|
||||||
|
// 2019-07-21: DirectX10: Backup, clear and restore Geometry Shader is any is bound when calling ImGui_ImplDX10_RenderDrawData().
|
||||||
|
// 2019-05-29: DirectX10: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
|
||||||
|
// 2019-04-30: DirectX10: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
|
||||||
|
// 2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile().
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
|
||||||
|
// 2018-07-13: DirectX10: Fixed unreleased resources in Init and Shutdown functions.
|
||||||
|
// 2018-06-08: Misc: Extracted imgui_impl_dx10.cpp/.h away from the old combined DX10+Win32 example.
|
||||||
|
// 2018-06-08: DirectX10: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
|
||||||
|
// 2018-04-09: Misc: Fixed erroneous call to io.Fonts->ClearInputData() + ClearTexData() that was left in DX10 example but removed in 1.47 (Nov 2015) on other backends.
|
||||||
|
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX10_RenderDrawData() in the .h file so you can call it yourself.
|
||||||
|
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
||||||
|
// 2016-05-07: DirectX10: Disabling depth-write.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_dx10.h"
|
||||||
|
|
||||||
|
// DirectX
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <d3d10_1.h>
|
||||||
|
#include <d3d10.h>
|
||||||
|
#include <d3dcompiler.h>
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Clang/GCC warnings with -Weverything
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
|
||||||
|
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// DirectX10 data
|
||||||
|
struct ImGui_ImplDX10_Texture
|
||||||
|
{
|
||||||
|
ID3D10Texture2D* pTexture;
|
||||||
|
ID3D10ShaderResourceView* pTextureView;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImGui_ImplDX10_Data
|
||||||
|
{
|
||||||
|
ID3D10Device* pd3dDevice;
|
||||||
|
IDXGIFactory* pFactory;
|
||||||
|
ID3D10Buffer* pVB;
|
||||||
|
ID3D10Buffer* pIB;
|
||||||
|
ID3D10VertexShader* pVertexShader;
|
||||||
|
ID3D10InputLayout* pInputLayout;
|
||||||
|
ID3D10Buffer* pVertexConstantBuffer;
|
||||||
|
ID3D10PixelShader* pPixelShader;
|
||||||
|
ID3D10SamplerState* pFontSampler;
|
||||||
|
ID3D10RasterizerState* pRasterizerState;
|
||||||
|
ID3D10BlendState* pBlendState;
|
||||||
|
ID3D10DepthStencilState* pDepthStencilState;
|
||||||
|
int VertexBufferSize;
|
||||||
|
int IndexBufferSize;
|
||||||
|
|
||||||
|
ImGui_ImplDX10_Data() { memset((void*)this, 0, sizeof(*this)); VertexBufferSize = 5000; IndexBufferSize = 10000; }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VERTEX_CONSTANT_BUFFER_DX10
|
||||||
|
{
|
||||||
|
float mvp[4][4];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
static ImGui_ImplDX10_Data* ImGui_ImplDX10_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplDX10_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
static void ImGui_ImplDX10_SetupRenderState(ImDrawData* draw_data, ID3D10Device* device)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
|
||||||
|
|
||||||
|
// Setup viewport
|
||||||
|
D3D10_VIEWPORT vp = {};
|
||||||
|
vp.Width = (UINT)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
|
||||||
|
vp.Height = (UINT)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
|
||||||
|
vp.MinDepth = 0.0f;
|
||||||
|
vp.MaxDepth = 1.0f;
|
||||||
|
vp.TopLeftX = vp.TopLeftY = 0;
|
||||||
|
device->RSSetViewports(1, &vp);
|
||||||
|
|
||||||
|
// Setup orthographic projection matrix into our constant buffer
|
||||||
|
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
|
||||||
|
void* mapped_resource;
|
||||||
|
if (bd->pVertexConstantBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &mapped_resource) == S_OK)
|
||||||
|
{
|
||||||
|
VERTEX_CONSTANT_BUFFER_DX10* constant_buffer = (VERTEX_CONSTANT_BUFFER_DX10*)mapped_resource;
|
||||||
|
float L = draw_data->DisplayPos.x;
|
||||||
|
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
||||||
|
float T = draw_data->DisplayPos.y;
|
||||||
|
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
||||||
|
float mvp[4][4] =
|
||||||
|
{
|
||||||
|
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, 0.5f, 0.0f },
|
||||||
|
{ (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
|
||||||
|
};
|
||||||
|
memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
|
||||||
|
bd->pVertexConstantBuffer->Unmap();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup shader and vertex buffers
|
||||||
|
unsigned int stride = sizeof(ImDrawVert);
|
||||||
|
unsigned int offset = 0;
|
||||||
|
device->IASetInputLayout(bd->pInputLayout);
|
||||||
|
device->IASetVertexBuffers(0, 1, &bd->pVB, &stride, &offset);
|
||||||
|
device->IASetIndexBuffer(bd->pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
|
||||||
|
device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||||
|
device->VSSetShader(bd->pVertexShader);
|
||||||
|
device->VSSetConstantBuffers(0, 1, &bd->pVertexConstantBuffer);
|
||||||
|
device->PSSetShader(bd->pPixelShader);
|
||||||
|
device->PSSetSamplers(0, 1, &bd->pFontSampler);
|
||||||
|
device->GSSetShader(nullptr);
|
||||||
|
|
||||||
|
// Setup render state
|
||||||
|
const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
|
||||||
|
device->OMSetBlendState(bd->pBlendState, blend_factor, 0xffffffff);
|
||||||
|
device->OMSetDepthStencilState(bd->pDepthStencilState, 0);
|
||||||
|
device->RSSetState(bd->pRasterizerState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render function
|
||||||
|
void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
|
||||||
|
{
|
||||||
|
// Avoid rendering when minimized
|
||||||
|
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
|
||||||
|
ID3D10Device* device = bd->pd3dDevice;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplDX10_UpdateTexture(tex);
|
||||||
|
|
||||||
|
// Create and grow vertex/index buffers if needed
|
||||||
|
if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount)
|
||||||
|
{
|
||||||
|
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
|
||||||
|
bd->VertexBufferSize = draw_data->TotalVtxCount + 5000;
|
||||||
|
D3D10_BUFFER_DESC desc = {};
|
||||||
|
desc.Usage = D3D10_USAGE_DYNAMIC;
|
||||||
|
desc.ByteWidth = bd->VertexBufferSize * sizeof(ImDrawVert);
|
||||||
|
desc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
|
||||||
|
desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
|
||||||
|
desc.MiscFlags = 0;
|
||||||
|
if (device->CreateBuffer(&desc, nullptr, &bd->pVB) < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bd->pIB || bd->IndexBufferSize < draw_data->TotalIdxCount)
|
||||||
|
{
|
||||||
|
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
|
||||||
|
bd->IndexBufferSize = draw_data->TotalIdxCount + 10000;
|
||||||
|
D3D10_BUFFER_DESC desc = {};
|
||||||
|
desc.Usage = D3D10_USAGE_DYNAMIC;
|
||||||
|
desc.ByteWidth = bd->IndexBufferSize * sizeof(ImDrawIdx);
|
||||||
|
desc.BindFlags = D3D10_BIND_INDEX_BUFFER;
|
||||||
|
desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
|
||||||
|
if (device->CreateBuffer(&desc, nullptr, &bd->pIB) < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy and convert all vertices into a single contiguous buffer
|
||||||
|
ImDrawVert* vtx_dst = nullptr;
|
||||||
|
ImDrawIdx* idx_dst = nullptr;
|
||||||
|
bd->pVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&vtx_dst);
|
||||||
|
bd->pIB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&idx_dst);
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
|
||||||
|
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
|
||||||
|
vtx_dst += draw_list->VtxBuffer.Size;
|
||||||
|
idx_dst += draw_list->IdxBuffer.Size;
|
||||||
|
}
|
||||||
|
bd->pVB->Unmap();
|
||||||
|
bd->pIB->Unmap();
|
||||||
|
|
||||||
|
// Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
|
||||||
|
struct BACKUP_DX10_STATE
|
||||||
|
{
|
||||||
|
UINT ScissorRectsCount, ViewportsCount;
|
||||||
|
D3D10_RECT ScissorRects[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
|
||||||
|
D3D10_VIEWPORT Viewports[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
|
||||||
|
ID3D10RasterizerState* RS;
|
||||||
|
ID3D10BlendState* BlendState;
|
||||||
|
FLOAT BlendFactor[4];
|
||||||
|
UINT SampleMask;
|
||||||
|
UINT StencilRef;
|
||||||
|
ID3D10DepthStencilState* DepthStencilState;
|
||||||
|
ID3D10ShaderResourceView* PSShaderResource;
|
||||||
|
ID3D10SamplerState* PSSampler;
|
||||||
|
ID3D10PixelShader* PS;
|
||||||
|
ID3D10VertexShader* VS;
|
||||||
|
ID3D10GeometryShader* GS;
|
||||||
|
D3D10_PRIMITIVE_TOPOLOGY PrimitiveTopology;
|
||||||
|
ID3D10Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
|
||||||
|
UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
|
||||||
|
DXGI_FORMAT IndexBufferFormat;
|
||||||
|
ID3D10InputLayout* InputLayout;
|
||||||
|
};
|
||||||
|
BACKUP_DX10_STATE old = {};
|
||||||
|
old.ScissorRectsCount = old.ViewportsCount = D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
|
||||||
|
device->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
|
||||||
|
device->RSGetViewports(&old.ViewportsCount, old.Viewports);
|
||||||
|
device->RSGetState(&old.RS);
|
||||||
|
device->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
|
||||||
|
device->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
|
||||||
|
device->PSGetShaderResources(0, 1, &old.PSShaderResource);
|
||||||
|
device->PSGetSamplers(0, 1, &old.PSSampler);
|
||||||
|
device->PSGetShader(&old.PS);
|
||||||
|
device->VSGetShader(&old.VS);
|
||||||
|
device->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
|
||||||
|
device->GSGetShader(&old.GS);
|
||||||
|
device->IAGetPrimitiveTopology(&old.PrimitiveTopology);
|
||||||
|
device->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
|
||||||
|
device->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
|
||||||
|
device->IAGetInputLayout(&old.InputLayout);
|
||||||
|
|
||||||
|
// Setup desired DX state
|
||||||
|
ImGui_ImplDX10_SetupRenderState(draw_data, device);
|
||||||
|
// Setup render state structure (for callbacks and custom texture bindings)
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
ImGui_ImplDX10_RenderState render_state;
|
||||||
|
render_state.Device = bd->pd3dDevice;
|
||||||
|
render_state.SamplerDefault = bd->pFontSampler;
|
||||||
|
render_state.VertexConstantBuffer = bd->pVertexConstantBuffer;
|
||||||
|
platform_io.Renderer_RenderState = &render_state;
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
// (Because we merged all buffers into a single one, we maintain our own offset into them)
|
||||||
|
int global_vtx_offset = 0;
|
||||||
|
int global_idx_offset = 0;
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos;
|
||||||
|
ImVec2 clip_scale = draw_data->FramebufferScale;
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback != nullptr)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplDX10_SetupRenderState(draw_data, device);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
|
||||||
|
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Apply scissor/clipping rectangle
|
||||||
|
const D3D10_RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y };
|
||||||
|
device->RSSetScissorRects(1, &r);
|
||||||
|
|
||||||
|
// Bind texture, Draw
|
||||||
|
ID3D10ShaderResourceView* texture_srv = (ID3D10ShaderResourceView*)pcmd->GetTexID();
|
||||||
|
device->PSSetShaderResources(0, 1, &texture_srv);
|
||||||
|
device->DrawIndexed(pcmd->ElemCount, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
global_idx_offset += draw_list->IdxBuffer.Size;
|
||||||
|
global_vtx_offset += draw_list->VtxBuffer.Size;
|
||||||
|
}
|
||||||
|
platform_io.Renderer_RenderState = nullptr;
|
||||||
|
|
||||||
|
// Restore modified DX state
|
||||||
|
device->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
|
||||||
|
device->RSSetViewports(old.ViewportsCount, old.Viewports);
|
||||||
|
device->RSSetState(old.RS); if (old.RS) old.RS->Release();
|
||||||
|
device->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
|
||||||
|
device->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release();
|
||||||
|
device->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
|
||||||
|
device->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
|
||||||
|
device->PSSetShader(old.PS); if (old.PS) old.PS->Release();
|
||||||
|
device->VSSetShader(old.VS); if (old.VS) old.VS->Release();
|
||||||
|
device->GSSetShader(old.GS); if (old.GS) old.GS->Release();
|
||||||
|
device->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
|
||||||
|
device->IASetPrimitiveTopology(old.PrimitiveTopology);
|
||||||
|
device->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
|
||||||
|
device->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
|
||||||
|
device->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplDX10_DestroyTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX10_Texture* backend_tex = (ImGui_ImplDX10_Texture*)tex->BackendUserData;
|
||||||
|
if (backend_tex == nullptr)
|
||||||
|
return;
|
||||||
|
IM_ASSERT(backend_tex->pTextureView == (ID3D10ShaderResourceView*)(intptr_t)tex->TexID);
|
||||||
|
backend_tex->pTexture->Release();
|
||||||
|
backend_tex->pTextureView->Release();
|
||||||
|
IM_DELETE(backend_tex);
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
tex->BackendUserData = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX10_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
unsigned int* pixels = (unsigned int*)tex->GetPixels();
|
||||||
|
ImGui_ImplDX10_Texture* backend_tex = IM_NEW(ImGui_ImplDX10_Texture)();
|
||||||
|
|
||||||
|
// Create texture
|
||||||
|
D3D10_TEXTURE2D_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.Width = (UINT)tex->Width;
|
||||||
|
desc.Height = (UINT)tex->Height;
|
||||||
|
desc.MipLevels = 1;
|
||||||
|
desc.ArraySize = 1;
|
||||||
|
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
desc.SampleDesc.Count = 1;
|
||||||
|
desc.Usage = D3D10_USAGE_DEFAULT;
|
||||||
|
desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
|
||||||
|
desc.CPUAccessFlags = 0;
|
||||||
|
|
||||||
|
D3D10_SUBRESOURCE_DATA subResource;
|
||||||
|
subResource.pSysMem = pixels;
|
||||||
|
subResource.SysMemPitch = desc.Width * 4;
|
||||||
|
subResource.SysMemSlicePitch = 0;
|
||||||
|
bd->pd3dDevice->CreateTexture2D(&desc, &subResource, &backend_tex->pTexture);
|
||||||
|
IM_ASSERT(backend_tex->pTexture != nullptr && "Backend failed to create texture!");
|
||||||
|
|
||||||
|
// Create texture view
|
||||||
|
D3D10_SHADER_RESOURCE_VIEW_DESC srv_desc;
|
||||||
|
ZeroMemory(&srv_desc, sizeof(srv_desc));
|
||||||
|
srv_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
srv_desc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
|
||||||
|
srv_desc.Texture2D.MipLevels = desc.MipLevels;
|
||||||
|
srv_desc.Texture2D.MostDetailedMip = 0;
|
||||||
|
bd->pd3dDevice->CreateShaderResourceView(backend_tex->pTexture, &srv_desc, &backend_tex->pTextureView);
|
||||||
|
IM_ASSERT(backend_tex->pTextureView != nullptr && "Backend failed to create texture!");
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)(intptr_t)backend_tex->pTextureView);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
tex->BackendUserData = backend_tex;
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
// Update selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
|
||||||
|
ImGui_ImplDX10_Texture* backend_tex = (ImGui_ImplDX10_Texture*)tex->BackendUserData;
|
||||||
|
IM_ASSERT(backend_tex->pTextureView == (ID3D10ShaderResourceView*)(intptr_t)tex->TexID);
|
||||||
|
for (ImTextureRect& r : tex->Updates)
|
||||||
|
{
|
||||||
|
D3D10_BOX box = { (UINT)r.x, (UINT)r.y, (UINT)0, (UINT)(r.x + r.w), (UINT)(r.y + r.h), (UINT)1 };
|
||||||
|
bd->pd3dDevice->UpdateSubresource(backend_tex->pTexture, 0, &box, tex->GetPixelsAt(r.x, r.y), (UINT)tex->GetPitch(), 0);
|
||||||
|
}
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
|
||||||
|
ImGui_ImplDX10_DestroyTexture(tex);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplDX10_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
|
||||||
|
if (!bd->pd3dDevice)
|
||||||
|
return false;
|
||||||
|
ImGui_ImplDX10_InvalidateDeviceObjects();
|
||||||
|
|
||||||
|
// By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
|
||||||
|
// If you would like to use this DX10 sample code but remove this dependency you can:
|
||||||
|
// 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
|
||||||
|
// 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
|
||||||
|
// See https://github.com/ocornut/imgui/pull/638 for sources and details.
|
||||||
|
|
||||||
|
// Create the vertex shader
|
||||||
|
{
|
||||||
|
static const char* vertexShader =
|
||||||
|
"cbuffer vertexBuffer : register(b0) \
|
||||||
|
{\
|
||||||
|
float4x4 ProjectionMatrix; \
|
||||||
|
};\
|
||||||
|
struct VS_INPUT\
|
||||||
|
{\
|
||||||
|
float2 pos : POSITION;\
|
||||||
|
float4 col : COLOR0;\
|
||||||
|
float2 uv : TEXCOORD0;\
|
||||||
|
};\
|
||||||
|
\
|
||||||
|
struct PS_INPUT\
|
||||||
|
{\
|
||||||
|
float4 pos : SV_POSITION;\
|
||||||
|
float4 col : COLOR0;\
|
||||||
|
float2 uv : TEXCOORD0;\
|
||||||
|
};\
|
||||||
|
\
|
||||||
|
PS_INPUT main(VS_INPUT input)\
|
||||||
|
{\
|
||||||
|
PS_INPUT output;\
|
||||||
|
output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
|
||||||
|
output.col = input.col;\
|
||||||
|
output.uv = input.uv;\
|
||||||
|
return output;\
|
||||||
|
}";
|
||||||
|
|
||||||
|
ID3DBlob* vertexShaderBlob;
|
||||||
|
if (FAILED(D3DCompile(vertexShader, strlen(vertexShader), nullptr, nullptr, nullptr, "main", "vs_4_0", 0, 0, &vertexShaderBlob, nullptr)))
|
||||||
|
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
|
||||||
|
if (bd->pd3dDevice->CreateVertexShader(vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), &bd->pVertexShader) != S_OK)
|
||||||
|
{
|
||||||
|
vertexShaderBlob->Release();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the input layout
|
||||||
|
D3D10_INPUT_ELEMENT_DESC local_layout[] =
|
||||||
|
{
|
||||||
|
{ "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, pos), D3D10_INPUT_PER_VERTEX_DATA, 0 },
|
||||||
|
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, uv), D3D10_INPUT_PER_VERTEX_DATA, 0 },
|
||||||
|
{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (UINT)offsetof(ImDrawVert, col), D3D10_INPUT_PER_VERTEX_DATA, 0 },
|
||||||
|
};
|
||||||
|
if (bd->pd3dDevice->CreateInputLayout(local_layout, 3, vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), &bd->pInputLayout) != S_OK)
|
||||||
|
{
|
||||||
|
vertexShaderBlob->Release();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
vertexShaderBlob->Release();
|
||||||
|
|
||||||
|
// Create the constant buffer
|
||||||
|
{
|
||||||
|
D3D10_BUFFER_DESC desc = {};
|
||||||
|
desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER_DX10);
|
||||||
|
desc.Usage = D3D10_USAGE_DYNAMIC;
|
||||||
|
desc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
|
||||||
|
desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
|
||||||
|
desc.MiscFlags = 0;
|
||||||
|
bd->pd3dDevice->CreateBuffer(&desc, nullptr, &bd->pVertexConstantBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the pixel shader
|
||||||
|
{
|
||||||
|
static const char* pixelShader =
|
||||||
|
"struct PS_INPUT\
|
||||||
|
{\
|
||||||
|
float4 pos : SV_POSITION;\
|
||||||
|
float4 col : COLOR0;\
|
||||||
|
float2 uv : TEXCOORD0;\
|
||||||
|
};\
|
||||||
|
sampler sampler0;\
|
||||||
|
Texture2D texture0;\
|
||||||
|
\
|
||||||
|
float4 main(PS_INPUT input) : SV_Target\
|
||||||
|
{\
|
||||||
|
float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
|
||||||
|
return out_col; \
|
||||||
|
}";
|
||||||
|
|
||||||
|
ID3DBlob* pixelShaderBlob;
|
||||||
|
if (FAILED(D3DCompile(pixelShader, strlen(pixelShader), nullptr, nullptr, nullptr, "main", "ps_4_0", 0, 0, &pixelShaderBlob, nullptr)))
|
||||||
|
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
|
||||||
|
if (bd->pd3dDevice->CreatePixelShader(pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize(), &bd->pPixelShader) != S_OK)
|
||||||
|
{
|
||||||
|
pixelShaderBlob->Release();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
pixelShaderBlob->Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the blending setup
|
||||||
|
{
|
||||||
|
D3D10_BLEND_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.AlphaToCoverageEnable = false;
|
||||||
|
desc.BlendEnable[0] = true;
|
||||||
|
desc.SrcBlend = D3D10_BLEND_SRC_ALPHA;
|
||||||
|
desc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
|
||||||
|
desc.BlendOp = D3D10_BLEND_OP_ADD;
|
||||||
|
desc.SrcBlendAlpha = D3D10_BLEND_ONE;
|
||||||
|
desc.DestBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA;
|
||||||
|
desc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
|
||||||
|
desc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;
|
||||||
|
bd->pd3dDevice->CreateBlendState(&desc, &bd->pBlendState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the rasterizer state
|
||||||
|
{
|
||||||
|
D3D10_RASTERIZER_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.FillMode = D3D10_FILL_SOLID;
|
||||||
|
desc.CullMode = D3D10_CULL_NONE;
|
||||||
|
desc.ScissorEnable = true;
|
||||||
|
desc.DepthClipEnable = true;
|
||||||
|
bd->pd3dDevice->CreateRasterizerState(&desc, &bd->pRasterizerState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create depth-stencil State
|
||||||
|
{
|
||||||
|
D3D10_DEPTH_STENCIL_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.DepthEnable = false;
|
||||||
|
desc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL;
|
||||||
|
desc.DepthFunc = D3D10_COMPARISON_ALWAYS;
|
||||||
|
desc.StencilEnable = false;
|
||||||
|
desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
|
||||||
|
desc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
|
||||||
|
desc.BackFace = desc.FrontFace;
|
||||||
|
bd->pd3dDevice->CreateDepthStencilState(&desc, &bd->pDepthStencilState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create texture sampler
|
||||||
|
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
|
||||||
|
{
|
||||||
|
D3D10_SAMPLER_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
|
||||||
|
desc.AddressU = D3D10_TEXTURE_ADDRESS_CLAMP;
|
||||||
|
desc.AddressV = D3D10_TEXTURE_ADDRESS_CLAMP;
|
||||||
|
desc.AddressW = D3D10_TEXTURE_ADDRESS_CLAMP;
|
||||||
|
desc.MipLODBias = 0.f;
|
||||||
|
desc.ComparisonFunc = D3D10_COMPARISON_ALWAYS;
|
||||||
|
desc.MinLOD = 0.f;
|
||||||
|
desc.MaxLOD = 0.f;
|
||||||
|
bd->pd3dDevice->CreateSamplerState(&desc, &bd->pFontSampler);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX10_InvalidateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
|
||||||
|
if (!bd->pd3dDevice)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Destroy all textures
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
ImGui_ImplDX10_DestroyTexture(tex);
|
||||||
|
if (bd->pFontSampler) { bd->pFontSampler->Release(); bd->pFontSampler = nullptr; }
|
||||||
|
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
|
||||||
|
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
|
||||||
|
if (bd->pBlendState) { bd->pBlendState->Release(); bd->pBlendState = nullptr; }
|
||||||
|
if (bd->pDepthStencilState) { bd->pDepthStencilState->Release(); bd->pDepthStencilState = nullptr; }
|
||||||
|
if (bd->pRasterizerState) { bd->pRasterizerState->Release(); bd->pRasterizerState = nullptr; }
|
||||||
|
if (bd->pPixelShader) { bd->pPixelShader->Release(); bd->pPixelShader = nullptr; }
|
||||||
|
if (bd->pVertexConstantBuffer) { bd->pVertexConstantBuffer->Release(); bd->pVertexConstantBuffer = nullptr; }
|
||||||
|
if (bd->pInputLayout) { bd->pInputLayout->Release(); bd->pInputLayout = nullptr; }
|
||||||
|
if (bd->pVertexShader) { bd->pVertexShader->Release(); bd->pVertexShader = nullptr; }
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplDX10_Init(ID3D10Device* device)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplDX10_Data* bd = IM_NEW(ImGui_ImplDX10_Data)();
|
||||||
|
io.BackendRendererUserData = (void*)bd;
|
||||||
|
io.BackendRendererName = "imgui_impl_dx10";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION;
|
||||||
|
|
||||||
|
// Get factory from device
|
||||||
|
IDXGIDevice* pDXGIDevice = nullptr;
|
||||||
|
IDXGIAdapter* pDXGIAdapter = nullptr;
|
||||||
|
IDXGIFactory* pFactory = nullptr;
|
||||||
|
if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK)
|
||||||
|
if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK)
|
||||||
|
if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK)
|
||||||
|
{
|
||||||
|
bd->pd3dDevice = device;
|
||||||
|
bd->pFactory = pFactory;
|
||||||
|
}
|
||||||
|
if (pDXGIDevice) pDXGIDevice->Release();
|
||||||
|
if (pDXGIAdapter) pDXGIAdapter->Release();
|
||||||
|
bd->pd3dDevice->AddRef();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX10_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
ImGui_ImplDX10_InvalidateDeviceObjects();
|
||||||
|
if (bd->pFactory) { bd->pFactory->Release(); }
|
||||||
|
if (bd->pd3dDevice) { bd->pd3dDevice->Release(); }
|
||||||
|
io.BackendRendererName = nullptr;
|
||||||
|
io.BackendRendererUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX10_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX10_Init()?");
|
||||||
|
|
||||||
|
if (!bd->pVertexShader)
|
||||||
|
if (!ImGui_ImplDX10_CreateDeviceObjects())
|
||||||
|
IM_ASSERT(0 && "ImGui_ImplDX10_CreateDeviceObjects() failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
48
imgui/backends/imgui_impl_dx10.h
Normal file
48
imgui/backends/imgui_impl_dx10.h
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
// dear imgui: Renderer Backend for DirectX10
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. Win32)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'ID3D10ShaderResourceView*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
struct ID3D10Device;
|
||||||
|
struct ID3D10SamplerState;
|
||||||
|
struct ID3D10Buffer;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplDX10_Init(ID3D10Device* device);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX10_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX10_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data);
|
||||||
|
|
||||||
|
// Use if you want to reset your rendering device without losing Dear ImGui state.
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplDX10_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX10_InvalidateDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX10_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
// [BETA] Selected render state data shared with callbacks.
|
||||||
|
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplDX10_RenderDrawData() call.
|
||||||
|
// (Please open an issue if you feel you need access to more data)
|
||||||
|
struct ImGui_ImplDX10_RenderState
|
||||||
|
{
|
||||||
|
ID3D10Device* Device;
|
||||||
|
ID3D10SamplerState* SamplerDefault;
|
||||||
|
ID3D10Buffer* VertexConstantBuffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
677
imgui/backends/imgui_impl_dx11.cpp
Normal file
677
imgui/backends/imgui_impl_dx11.cpp
Normal file
@@ -0,0 +1,677 @@
|
|||||||
|
// dear imgui: Renderer Backend for DirectX11
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. Win32)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'ID3D11ShaderResourceView*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-06-11: DirectX11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas.
|
||||||
|
// 2025-05-07: DirectX11: Honor draw_data->FramebufferScale to allow for custom backends and experiment using it (consistently with other renderer backends, even though in normal condition it is not set under Windows).
|
||||||
|
// 2025-01-06: DirectX11: Expose VertexConstantBuffer in ImGui_ImplDX11_RenderState. Reset projection matrix in ImDrawCallback_ResetRenderState handler.
|
||||||
|
// 2024-10-07: DirectX11: Changed default texture sampler to Clamp instead of Repeat/Wrap.
|
||||||
|
// 2024-10-07: DirectX11: Expose selected render state in ImGui_ImplDX11_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
|
||||||
|
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
|
||||||
|
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
|
||||||
|
// 2021-05-19: DirectX11: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
|
||||||
|
// 2021-02-18: DirectX11: Change blending equation to preserve alpha in output buffer.
|
||||||
|
// 2019-08-01: DirectX11: Fixed code querying the Geometry Shader state (would generally error with Debug layer enabled).
|
||||||
|
// 2019-07-21: DirectX11: Backup, clear and restore Geometry Shader is any is bound when calling ImGui_ImplDX11_RenderDrawData. Clearing Hull/Domain/Compute shaders without backup/restore.
|
||||||
|
// 2019-05-29: DirectX11: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
|
||||||
|
// 2019-04-30: DirectX11: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
|
||||||
|
// 2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile().
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
|
||||||
|
// 2018-08-01: DirectX11: Querying for IDXGIFactory instead of IDXGIFactory1 to increase compatibility.
|
||||||
|
// 2018-07-13: DirectX11: Fixed unreleased resources in Init and Shutdown functions.
|
||||||
|
// 2018-06-08: Misc: Extracted imgui_impl_dx11.cpp/.h away from the old combined DX11+Win32 example.
|
||||||
|
// 2018-06-08: DirectX11: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
|
||||||
|
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX11_RenderDrawData() in the .h file so you can call it yourself.
|
||||||
|
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
||||||
|
// 2016-05-07: DirectX11: Disabling depth-write.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_dx11.h"
|
||||||
|
|
||||||
|
// DirectX
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <d3d11.h>
|
||||||
|
#include <d3dcompiler.h>
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Clang/GCC warnings with -Weverything
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
|
||||||
|
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// DirectX11 data
|
||||||
|
struct ImGui_ImplDX11_Texture
|
||||||
|
{
|
||||||
|
ID3D11Texture2D* pTexture;
|
||||||
|
ID3D11ShaderResourceView* pTextureView;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImGui_ImplDX11_Data
|
||||||
|
{
|
||||||
|
ID3D11Device* pd3dDevice;
|
||||||
|
ID3D11DeviceContext* pd3dDeviceContext;
|
||||||
|
IDXGIFactory* pFactory;
|
||||||
|
ID3D11Buffer* pVB;
|
||||||
|
ID3D11Buffer* pIB;
|
||||||
|
ID3D11VertexShader* pVertexShader;
|
||||||
|
ID3D11InputLayout* pInputLayout;
|
||||||
|
ID3D11Buffer* pVertexConstantBuffer;
|
||||||
|
ID3D11PixelShader* pPixelShader;
|
||||||
|
ID3D11SamplerState* pFontSampler;
|
||||||
|
ID3D11RasterizerState* pRasterizerState;
|
||||||
|
ID3D11BlendState* pBlendState;
|
||||||
|
ID3D11DepthStencilState* pDepthStencilState;
|
||||||
|
int VertexBufferSize;
|
||||||
|
int IndexBufferSize;
|
||||||
|
|
||||||
|
ImGui_ImplDX11_Data() { memset((void*)this, 0, sizeof(*this)); VertexBufferSize = 5000; IndexBufferSize = 10000; }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VERTEX_CONSTANT_BUFFER_DX11
|
||||||
|
{
|
||||||
|
float mvp[4][4];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
static ImGui_ImplDX11_Data* ImGui_ImplDX11_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplDX11_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
static void ImGui_ImplDX11_SetupRenderState(ImDrawData* draw_data, ID3D11DeviceContext* device_ctx)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
|
||||||
|
|
||||||
|
// Setup viewport
|
||||||
|
D3D11_VIEWPORT vp = {};
|
||||||
|
vp.Width = draw_data->DisplaySize.x * draw_data->FramebufferScale.x;
|
||||||
|
vp.Height = draw_data->DisplaySize.y * draw_data->FramebufferScale.y;
|
||||||
|
vp.MinDepth = 0.0f;
|
||||||
|
vp.MaxDepth = 1.0f;
|
||||||
|
vp.TopLeftX = vp.TopLeftY = 0;
|
||||||
|
device_ctx->RSSetViewports(1, &vp);
|
||||||
|
|
||||||
|
// Setup orthographic projection matrix into our constant buffer
|
||||||
|
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
|
||||||
|
D3D11_MAPPED_SUBRESOURCE mapped_resource;
|
||||||
|
if (device_ctx->Map(bd->pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource) == S_OK)
|
||||||
|
{
|
||||||
|
VERTEX_CONSTANT_BUFFER_DX11* constant_buffer = (VERTEX_CONSTANT_BUFFER_DX11*)mapped_resource.pData;
|
||||||
|
float L = draw_data->DisplayPos.x;
|
||||||
|
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
||||||
|
float T = draw_data->DisplayPos.y;
|
||||||
|
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
||||||
|
float mvp[4][4] =
|
||||||
|
{
|
||||||
|
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, 0.5f, 0.0f },
|
||||||
|
{ (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
|
||||||
|
};
|
||||||
|
memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
|
||||||
|
device_ctx->Unmap(bd->pVertexConstantBuffer, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup shader and vertex buffers
|
||||||
|
unsigned int stride = sizeof(ImDrawVert);
|
||||||
|
unsigned int offset = 0;
|
||||||
|
device_ctx->IASetInputLayout(bd->pInputLayout);
|
||||||
|
device_ctx->IASetVertexBuffers(0, 1, &bd->pVB, &stride, &offset);
|
||||||
|
device_ctx->IASetIndexBuffer(bd->pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
|
||||||
|
device_ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||||
|
device_ctx->VSSetShader(bd->pVertexShader, nullptr, 0);
|
||||||
|
device_ctx->VSSetConstantBuffers(0, 1, &bd->pVertexConstantBuffer);
|
||||||
|
device_ctx->PSSetShader(bd->pPixelShader, nullptr, 0);
|
||||||
|
device_ctx->PSSetSamplers(0, 1, &bd->pFontSampler);
|
||||||
|
device_ctx->GSSetShader(nullptr, nullptr, 0);
|
||||||
|
device_ctx->HSSetShader(nullptr, nullptr, 0); // In theory we should backup and restore this as well.. very infrequently used..
|
||||||
|
device_ctx->DSSetShader(nullptr, nullptr, 0); // In theory we should backup and restore this as well.. very infrequently used..
|
||||||
|
device_ctx->CSSetShader(nullptr, nullptr, 0); // In theory we should backup and restore this as well.. very infrequently used..
|
||||||
|
|
||||||
|
// Setup render state
|
||||||
|
const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
|
||||||
|
device_ctx->OMSetBlendState(bd->pBlendState, blend_factor, 0xffffffff);
|
||||||
|
device_ctx->OMSetDepthStencilState(bd->pDepthStencilState, 0);
|
||||||
|
device_ctx->RSSetState(bd->pRasterizerState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render function
|
||||||
|
void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
|
||||||
|
{
|
||||||
|
// Avoid rendering when minimized
|
||||||
|
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
|
||||||
|
ID3D11DeviceContext* device = bd->pd3dDeviceContext;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplDX11_UpdateTexture(tex);
|
||||||
|
|
||||||
|
// Create and grow vertex/index buffers if needed
|
||||||
|
if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount)
|
||||||
|
{
|
||||||
|
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
|
||||||
|
bd->VertexBufferSize = draw_data->TotalVtxCount + 5000;
|
||||||
|
D3D11_BUFFER_DESC desc = {};
|
||||||
|
desc.Usage = D3D11_USAGE_DYNAMIC;
|
||||||
|
desc.ByteWidth = bd->VertexBufferSize * sizeof(ImDrawVert);
|
||||||
|
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||||
|
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||||
|
desc.MiscFlags = 0;
|
||||||
|
if (bd->pd3dDevice->CreateBuffer(&desc, nullptr, &bd->pVB) < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!bd->pIB || bd->IndexBufferSize < draw_data->TotalIdxCount)
|
||||||
|
{
|
||||||
|
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
|
||||||
|
bd->IndexBufferSize = draw_data->TotalIdxCount + 10000;
|
||||||
|
D3D11_BUFFER_DESC desc = {};
|
||||||
|
desc.Usage = D3D11_USAGE_DYNAMIC;
|
||||||
|
desc.ByteWidth = bd->IndexBufferSize * sizeof(ImDrawIdx);
|
||||||
|
desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
||||||
|
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||||
|
if (bd->pd3dDevice->CreateBuffer(&desc, nullptr, &bd->pIB) < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upload vertex/index data into a single contiguous GPU buffer
|
||||||
|
D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
|
||||||
|
if (device->Map(bd->pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
|
||||||
|
return;
|
||||||
|
if (device->Map(bd->pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK)
|
||||||
|
return;
|
||||||
|
ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData;
|
||||||
|
ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData;
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
|
||||||
|
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
|
||||||
|
vtx_dst += draw_list->VtxBuffer.Size;
|
||||||
|
idx_dst += draw_list->IdxBuffer.Size;
|
||||||
|
}
|
||||||
|
device->Unmap(bd->pVB, 0);
|
||||||
|
device->Unmap(bd->pIB, 0);
|
||||||
|
|
||||||
|
// Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
|
||||||
|
struct BACKUP_DX11_STATE
|
||||||
|
{
|
||||||
|
UINT ScissorRectsCount, ViewportsCount;
|
||||||
|
D3D11_RECT ScissorRects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
|
||||||
|
D3D11_VIEWPORT Viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
|
||||||
|
ID3D11RasterizerState* RS;
|
||||||
|
ID3D11BlendState* BlendState;
|
||||||
|
FLOAT BlendFactor[4];
|
||||||
|
UINT SampleMask;
|
||||||
|
UINT StencilRef;
|
||||||
|
ID3D11DepthStencilState* DepthStencilState;
|
||||||
|
ID3D11ShaderResourceView* PSShaderResource;
|
||||||
|
ID3D11SamplerState* PSSampler;
|
||||||
|
ID3D11PixelShader* PS;
|
||||||
|
ID3D11VertexShader* VS;
|
||||||
|
ID3D11GeometryShader* GS;
|
||||||
|
UINT PSInstancesCount, VSInstancesCount, GSInstancesCount;
|
||||||
|
ID3D11ClassInstance *PSInstances[256], *VSInstances[256], *GSInstances[256]; // 256 is max according to PSSetShader documentation
|
||||||
|
D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology;
|
||||||
|
ID3D11Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
|
||||||
|
UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
|
||||||
|
DXGI_FORMAT IndexBufferFormat;
|
||||||
|
ID3D11InputLayout* InputLayout;
|
||||||
|
};
|
||||||
|
BACKUP_DX11_STATE old = {};
|
||||||
|
old.ScissorRectsCount = old.ViewportsCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
|
||||||
|
device->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
|
||||||
|
device->RSGetViewports(&old.ViewportsCount, old.Viewports);
|
||||||
|
device->RSGetState(&old.RS);
|
||||||
|
device->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
|
||||||
|
device->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
|
||||||
|
device->PSGetShaderResources(0, 1, &old.PSShaderResource);
|
||||||
|
device->PSGetSamplers(0, 1, &old.PSSampler);
|
||||||
|
old.PSInstancesCount = old.VSInstancesCount = old.GSInstancesCount = 256;
|
||||||
|
device->PSGetShader(&old.PS, old.PSInstances, &old.PSInstancesCount);
|
||||||
|
device->VSGetShader(&old.VS, old.VSInstances, &old.VSInstancesCount);
|
||||||
|
device->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
|
||||||
|
device->GSGetShader(&old.GS, old.GSInstances, &old.GSInstancesCount);
|
||||||
|
|
||||||
|
device->IAGetPrimitiveTopology(&old.PrimitiveTopology);
|
||||||
|
device->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
|
||||||
|
device->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
|
||||||
|
device->IAGetInputLayout(&old.InputLayout);
|
||||||
|
|
||||||
|
// Setup desired DX state
|
||||||
|
ImGui_ImplDX11_SetupRenderState(draw_data, device);
|
||||||
|
|
||||||
|
// Setup render state structure (for callbacks and custom texture bindings)
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
ImGui_ImplDX11_RenderState render_state;
|
||||||
|
render_state.Device = bd->pd3dDevice;
|
||||||
|
render_state.DeviceContext = bd->pd3dDeviceContext;
|
||||||
|
render_state.SamplerDefault = bd->pFontSampler;
|
||||||
|
render_state.VertexConstantBuffer = bd->pVertexConstantBuffer;
|
||||||
|
platform_io.Renderer_RenderState = &render_state;
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
// (Because we merged all buffers into a single one, we maintain our own offset into them)
|
||||||
|
int global_idx_offset = 0;
|
||||||
|
int global_vtx_offset = 0;
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos;
|
||||||
|
ImVec2 clip_scale = draw_data->FramebufferScale;
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback != nullptr)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplDX11_SetupRenderState(draw_data, device);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
|
||||||
|
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Apply scissor/clipping rectangle
|
||||||
|
const D3D11_RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y };
|
||||||
|
device->RSSetScissorRects(1, &r);
|
||||||
|
|
||||||
|
// Bind texture, Draw
|
||||||
|
ID3D11ShaderResourceView* texture_srv = (ID3D11ShaderResourceView*)pcmd->GetTexID();
|
||||||
|
device->PSSetShaderResources(0, 1, &texture_srv);
|
||||||
|
device->DrawIndexed(pcmd->ElemCount, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
global_idx_offset += draw_list->IdxBuffer.Size;
|
||||||
|
global_vtx_offset += draw_list->VtxBuffer.Size;
|
||||||
|
}
|
||||||
|
platform_io.Renderer_RenderState = nullptr;
|
||||||
|
|
||||||
|
// Restore modified DX state
|
||||||
|
device->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
|
||||||
|
device->RSSetViewports(old.ViewportsCount, old.Viewports);
|
||||||
|
device->RSSetState(old.RS); if (old.RS) old.RS->Release();
|
||||||
|
device->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
|
||||||
|
device->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release();
|
||||||
|
device->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
|
||||||
|
device->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
|
||||||
|
device->PSSetShader(old.PS, old.PSInstances, old.PSInstancesCount); if (old.PS) old.PS->Release();
|
||||||
|
for (UINT i = 0; i < old.PSInstancesCount; i++) if (old.PSInstances[i]) old.PSInstances[i]->Release();
|
||||||
|
device->VSSetShader(old.VS, old.VSInstances, old.VSInstancesCount); if (old.VS) old.VS->Release();
|
||||||
|
device->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
|
||||||
|
device->GSSetShader(old.GS, old.GSInstances, old.GSInstancesCount); if (old.GS) old.GS->Release();
|
||||||
|
for (UINT i = 0; i < old.VSInstancesCount; i++) if (old.VSInstances[i]) old.VSInstances[i]->Release();
|
||||||
|
device->IASetPrimitiveTopology(old.PrimitiveTopology);
|
||||||
|
device->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
|
||||||
|
device->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
|
||||||
|
device->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplDX11_DestroyTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX11_Texture* backend_tex = (ImGui_ImplDX11_Texture*)tex->BackendUserData;
|
||||||
|
if (backend_tex == nullptr)
|
||||||
|
return;
|
||||||
|
IM_ASSERT(backend_tex->pTextureView == (ID3D11ShaderResourceView*)(intptr_t)tex->TexID);
|
||||||
|
backend_tex->pTextureView->Release();
|
||||||
|
backend_tex->pTexture->Release();
|
||||||
|
IM_DELETE(backend_tex);
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
tex->BackendUserData = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX11_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
unsigned int* pixels = (unsigned int*)tex->GetPixels();
|
||||||
|
ImGui_ImplDX11_Texture* backend_tex = IM_NEW(ImGui_ImplDX11_Texture)();
|
||||||
|
|
||||||
|
// Create texture
|
||||||
|
D3D11_TEXTURE2D_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.Width = (UINT)tex->Width;
|
||||||
|
desc.Height = (UINT)tex->Height;
|
||||||
|
desc.MipLevels = 1;
|
||||||
|
desc.ArraySize = 1;
|
||||||
|
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
desc.SampleDesc.Count = 1;
|
||||||
|
desc.Usage = D3D11_USAGE_DEFAULT;
|
||||||
|
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
||||||
|
desc.CPUAccessFlags = 0;
|
||||||
|
D3D11_SUBRESOURCE_DATA subResource;
|
||||||
|
subResource.pSysMem = pixels;
|
||||||
|
subResource.SysMemPitch = desc.Width * 4;
|
||||||
|
subResource.SysMemSlicePitch = 0;
|
||||||
|
bd->pd3dDevice->CreateTexture2D(&desc, &subResource, &backend_tex->pTexture);
|
||||||
|
IM_ASSERT(backend_tex->pTexture != nullptr && "Backend failed to create texture!");
|
||||||
|
|
||||||
|
// Create texture view
|
||||||
|
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
|
||||||
|
ZeroMemory(&srvDesc, sizeof(srvDesc));
|
||||||
|
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
||||||
|
srvDesc.Texture2D.MipLevels = desc.MipLevels;
|
||||||
|
srvDesc.Texture2D.MostDetailedMip = 0;
|
||||||
|
bd->pd3dDevice->CreateShaderResourceView(backend_tex->pTexture, &srvDesc, &backend_tex->pTextureView);
|
||||||
|
IM_ASSERT(backend_tex->pTextureView != nullptr && "Backend failed to create texture!");
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)(intptr_t)backend_tex->pTextureView);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
tex->BackendUserData = backend_tex;
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
// Update selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
|
||||||
|
ImGui_ImplDX11_Texture* backend_tex = (ImGui_ImplDX11_Texture*)tex->BackendUserData;
|
||||||
|
IM_ASSERT(backend_tex->pTextureView == (ID3D11ShaderResourceView*)(intptr_t)tex->TexID);
|
||||||
|
for (ImTextureRect& r : tex->Updates)
|
||||||
|
{
|
||||||
|
D3D11_BOX box = { (UINT)r.x, (UINT)r.y, (UINT)0, (UINT)(r.x + r.w), (UINT)(r.y + r .h), (UINT)1 };
|
||||||
|
bd->pd3dDeviceContext->UpdateSubresource(backend_tex->pTexture, 0, &box, tex->GetPixelsAt(r.x, r.y), (UINT)tex->GetPitch(), 0);
|
||||||
|
}
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
|
||||||
|
ImGui_ImplDX11_DestroyTexture(tex);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplDX11_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
|
||||||
|
if (!bd->pd3dDevice)
|
||||||
|
return false;
|
||||||
|
ImGui_ImplDX11_InvalidateDeviceObjects();
|
||||||
|
|
||||||
|
// By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
|
||||||
|
// If you would like to use this DX11 sample code but remove this dependency you can:
|
||||||
|
// 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
|
||||||
|
// 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
|
||||||
|
// See https://github.com/ocornut/imgui/pull/638 for sources and details.
|
||||||
|
|
||||||
|
// Create the vertex shader
|
||||||
|
{
|
||||||
|
static const char* vertexShader =
|
||||||
|
"cbuffer vertexBuffer : register(b0) \
|
||||||
|
{\
|
||||||
|
float4x4 ProjectionMatrix; \
|
||||||
|
};\
|
||||||
|
struct VS_INPUT\
|
||||||
|
{\
|
||||||
|
float2 pos : POSITION;\
|
||||||
|
float4 col : COLOR0;\
|
||||||
|
float2 uv : TEXCOORD0;\
|
||||||
|
};\
|
||||||
|
\
|
||||||
|
struct PS_INPUT\
|
||||||
|
{\
|
||||||
|
float4 pos : SV_POSITION;\
|
||||||
|
float4 col : COLOR0;\
|
||||||
|
float2 uv : TEXCOORD0;\
|
||||||
|
};\
|
||||||
|
\
|
||||||
|
PS_INPUT main(VS_INPUT input)\
|
||||||
|
{\
|
||||||
|
PS_INPUT output;\
|
||||||
|
output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
|
||||||
|
output.col = input.col;\
|
||||||
|
output.uv = input.uv;\
|
||||||
|
return output;\
|
||||||
|
}";
|
||||||
|
|
||||||
|
ID3DBlob* vertexShaderBlob;
|
||||||
|
if (FAILED(D3DCompile(vertexShader, strlen(vertexShader), nullptr, nullptr, nullptr, "main", "vs_4_0", 0, 0, &vertexShaderBlob, nullptr)))
|
||||||
|
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
|
||||||
|
if (bd->pd3dDevice->CreateVertexShader(vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), nullptr, &bd->pVertexShader) != S_OK)
|
||||||
|
{
|
||||||
|
vertexShaderBlob->Release();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the input layout
|
||||||
|
D3D11_INPUT_ELEMENT_DESC local_layout[] =
|
||||||
|
{
|
||||||
|
{ "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, pos), D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
||||||
|
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
||||||
|
{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (UINT)offsetof(ImDrawVert, col), D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
||||||
|
};
|
||||||
|
if (bd->pd3dDevice->CreateInputLayout(local_layout, 3, vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), &bd->pInputLayout) != S_OK)
|
||||||
|
{
|
||||||
|
vertexShaderBlob->Release();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
vertexShaderBlob->Release();
|
||||||
|
|
||||||
|
// Create the constant buffer
|
||||||
|
{
|
||||||
|
D3D11_BUFFER_DESC desc = {};
|
||||||
|
desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER_DX11);
|
||||||
|
desc.Usage = D3D11_USAGE_DYNAMIC;
|
||||||
|
desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||||
|
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||||
|
desc.MiscFlags = 0;
|
||||||
|
bd->pd3dDevice->CreateBuffer(&desc, nullptr, &bd->pVertexConstantBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the pixel shader
|
||||||
|
{
|
||||||
|
static const char* pixelShader =
|
||||||
|
"struct PS_INPUT\
|
||||||
|
{\
|
||||||
|
float4 pos : SV_POSITION;\
|
||||||
|
float4 col : COLOR0;\
|
||||||
|
float2 uv : TEXCOORD0;\
|
||||||
|
};\
|
||||||
|
sampler sampler0;\
|
||||||
|
Texture2D texture0;\
|
||||||
|
\
|
||||||
|
float4 main(PS_INPUT input) : SV_Target\
|
||||||
|
{\
|
||||||
|
float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
|
||||||
|
return out_col; \
|
||||||
|
}";
|
||||||
|
|
||||||
|
ID3DBlob* pixelShaderBlob;
|
||||||
|
if (FAILED(D3DCompile(pixelShader, strlen(pixelShader), nullptr, nullptr, nullptr, "main", "ps_4_0", 0, 0, &pixelShaderBlob, nullptr)))
|
||||||
|
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
|
||||||
|
if (bd->pd3dDevice->CreatePixelShader(pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize(), nullptr, &bd->pPixelShader) != S_OK)
|
||||||
|
{
|
||||||
|
pixelShaderBlob->Release();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
pixelShaderBlob->Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the blending setup
|
||||||
|
{
|
||||||
|
D3D11_BLEND_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.AlphaToCoverageEnable = false;
|
||||||
|
desc.RenderTarget[0].BlendEnable = true;
|
||||||
|
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
|
||||||
|
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
|
||||||
|
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
|
||||||
|
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
|
||||||
|
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
|
||||||
|
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
|
||||||
|
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
|
||||||
|
bd->pd3dDevice->CreateBlendState(&desc, &bd->pBlendState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the rasterizer state
|
||||||
|
{
|
||||||
|
D3D11_RASTERIZER_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.FillMode = D3D11_FILL_SOLID;
|
||||||
|
desc.CullMode = D3D11_CULL_NONE;
|
||||||
|
desc.ScissorEnable = true;
|
||||||
|
desc.DepthClipEnable = true;
|
||||||
|
bd->pd3dDevice->CreateRasterizerState(&desc, &bd->pRasterizerState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create depth-stencil State
|
||||||
|
{
|
||||||
|
D3D11_DEPTH_STENCIL_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.DepthEnable = false;
|
||||||
|
desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
|
||||||
|
desc.DepthFunc = D3D11_COMPARISON_ALWAYS;
|
||||||
|
desc.StencilEnable = false;
|
||||||
|
desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
|
||||||
|
desc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
|
||||||
|
desc.BackFace = desc.FrontFace;
|
||||||
|
bd->pd3dDevice->CreateDepthStencilState(&desc, &bd->pDepthStencilState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create texture sampler
|
||||||
|
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
|
||||||
|
{
|
||||||
|
D3D11_SAMPLER_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||||
|
desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||||
|
desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||||
|
desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||||
|
desc.MipLODBias = 0.f;
|
||||||
|
desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||||
|
desc.MinLOD = 0.f;
|
||||||
|
desc.MaxLOD = 0.f;
|
||||||
|
bd->pd3dDevice->CreateSamplerState(&desc, &bd->pFontSampler);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX11_InvalidateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
|
||||||
|
if (!bd->pd3dDevice)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Destroy all textures
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
ImGui_ImplDX11_DestroyTexture(tex);
|
||||||
|
|
||||||
|
if (bd->pFontSampler) { bd->pFontSampler->Release(); bd->pFontSampler = nullptr; }
|
||||||
|
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
|
||||||
|
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
|
||||||
|
if (bd->pBlendState) { bd->pBlendState->Release(); bd->pBlendState = nullptr; }
|
||||||
|
if (bd->pDepthStencilState) { bd->pDepthStencilState->Release(); bd->pDepthStencilState = nullptr; }
|
||||||
|
if (bd->pRasterizerState) { bd->pRasterizerState->Release(); bd->pRasterizerState = nullptr; }
|
||||||
|
if (bd->pPixelShader) { bd->pPixelShader->Release(); bd->pPixelShader = nullptr; }
|
||||||
|
if (bd->pVertexConstantBuffer) { bd->pVertexConstantBuffer->Release(); bd->pVertexConstantBuffer = nullptr; }
|
||||||
|
if (bd->pInputLayout) { bd->pInputLayout->Release(); bd->pInputLayout = nullptr; }
|
||||||
|
if (bd->pVertexShader) { bd->pVertexShader->Release(); bd->pVertexShader = nullptr; }
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplDX11_Data* bd = IM_NEW(ImGui_ImplDX11_Data)();
|
||||||
|
io.BackendRendererUserData = (void*)bd;
|
||||||
|
io.BackendRendererName = "imgui_impl_dx11";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION;
|
||||||
|
|
||||||
|
// Get factory from device
|
||||||
|
IDXGIDevice* pDXGIDevice = nullptr;
|
||||||
|
IDXGIAdapter* pDXGIAdapter = nullptr;
|
||||||
|
IDXGIFactory* pFactory = nullptr;
|
||||||
|
|
||||||
|
if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK)
|
||||||
|
if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK)
|
||||||
|
if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK)
|
||||||
|
{
|
||||||
|
bd->pd3dDevice = device;
|
||||||
|
bd->pd3dDeviceContext = device_context;
|
||||||
|
bd->pFactory = pFactory;
|
||||||
|
}
|
||||||
|
if (pDXGIDevice) pDXGIDevice->Release();
|
||||||
|
if (pDXGIAdapter) pDXGIAdapter->Release();
|
||||||
|
bd->pd3dDevice->AddRef();
|
||||||
|
bd->pd3dDeviceContext->AddRef();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX11_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
ImGui_ImplDX11_InvalidateDeviceObjects();
|
||||||
|
if (bd->pFactory) { bd->pFactory->Release(); }
|
||||||
|
if (bd->pd3dDevice) { bd->pd3dDevice->Release(); }
|
||||||
|
if (bd->pd3dDeviceContext) { bd->pd3dDeviceContext->Release(); }
|
||||||
|
io.BackendRendererName = nullptr;
|
||||||
|
io.BackendRendererUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX11_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX11_Init()?");
|
||||||
|
|
||||||
|
if (!bd->pVertexShader)
|
||||||
|
if (!ImGui_ImplDX11_CreateDeviceObjects())
|
||||||
|
IM_ASSERT(0 && "ImGui_ImplDX11_CreateDeviceObjects() failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
51
imgui/backends/imgui_impl_dx11.h
Normal file
51
imgui/backends/imgui_impl_dx11.h
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
// dear imgui: Renderer Backend for DirectX11
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. Win32)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'ID3D11ShaderResourceView*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
struct ID3D11Device;
|
||||||
|
struct ID3D11DeviceContext;
|
||||||
|
struct ID3D11SamplerState;
|
||||||
|
struct ID3D11Buffer;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX11_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX11_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data);
|
||||||
|
|
||||||
|
// Use if you want to reset your rendering device without losing Dear ImGui state.
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplDX11_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX11_InvalidateDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX11_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
// [BETA] Selected render state data shared with callbacks.
|
||||||
|
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplDX11_RenderDrawData() call.
|
||||||
|
// (Please open an issue if you feel you need access to more data)
|
||||||
|
struct ImGui_ImplDX11_RenderState
|
||||||
|
{
|
||||||
|
ID3D11Device* Device;
|
||||||
|
ID3D11DeviceContext* DeviceContext;
|
||||||
|
ID3D11SamplerState* SamplerDefault;
|
||||||
|
ID3D11Buffer* VertexConstantBuffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
934
imgui/backends/imgui_impl_dx12.cpp
Normal file
934
imgui/backends/imgui_impl_dx12.cpp
Normal file
@@ -0,0 +1,934 @@
|
|||||||
|
// dear imgui: Renderer Backend for DirectX12
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. Win32)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
|
||||||
|
// The aim of imgui_impl_dx12.h/.cpp is to be usable in your engine without any modification.
|
||||||
|
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-06-19: Fixed build on MinGW. (#8702, #4594)
|
||||||
|
// 2025-06-11: DirectX12: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas.
|
||||||
|
// 2025-05-07: DirectX12: Honor draw_data->FramebufferScale to allow for custom backends and experiment using it (consistently with other renderer backends, even though in normal condition it is not set under Windows).
|
||||||
|
// 2025-02-24: DirectX12: Fixed an issue where ImGui_ImplDX12_Init() signature change from 2024-11-15 combined with change from 2025-01-15 made legacy ImGui_ImplDX12_Init() crash. (#8429)
|
||||||
|
// 2025-01-15: DirectX12: Texture upload use the command queue provided in ImGui_ImplDX12_InitInfo instead of creating its own.
|
||||||
|
// 2024-12-09: DirectX12: Let user specifies the DepthStencilView format by setting ImGui_ImplDX12_InitInfo::DSVFormat.
|
||||||
|
// 2024-11-15: DirectX12: *BREAKING CHANGE* Changed ImGui_ImplDX12_Init() signature to take a ImGui_ImplDX12_InitInfo struct. Legacy ImGui_ImplDX12_Init() signature is still supported (will obsolete).
|
||||||
|
// 2024-11-15: DirectX12: *BREAKING CHANGE* User is now required to pass function pointers to allocate/free SRV Descriptors. We provide convenience legacy fields to pass a single descriptor, matching the old API, but upcoming features will want multiple.
|
||||||
|
// 2024-10-23: DirectX12: Unmap() call specify written range. The range is informational and may be used by debug tools.
|
||||||
|
// 2024-10-07: DirectX12: Changed default texture sampler to Clamp instead of Repeat/Wrap.
|
||||||
|
// 2024-10-07: DirectX12: Expose selected render state in ImGui_ImplDX12_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
|
||||||
|
// 2024-10-07: DirectX12: Compiling with '#define ImTextureID=ImU64' is unnecessary now that dear imgui defaults ImTextureID to u64 instead of void*.
|
||||||
|
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
|
||||||
|
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
|
||||||
|
// 2021-05-19: DirectX12: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
|
||||||
|
// 2021-02-18: DirectX12: Change blending equation to preserve alpha in output buffer.
|
||||||
|
// 2021-01-11: DirectX12: Improve Windows 7 compatibility (for D3D12On7) by loading d3d12.dll dynamically.
|
||||||
|
// 2020-09-16: DirectX12: Avoid rendering calls with zero-sized scissor rectangle since it generates a validation layer warning.
|
||||||
|
// 2020-09-08: DirectX12: Clarified support for building on 32-bit systems by redefining ImTextureID.
|
||||||
|
// 2019-10-18: DirectX12: *BREAKING CHANGE* Added extra ID3D12DescriptorHeap parameter to ImGui_ImplDX12_Init() function.
|
||||||
|
// 2019-05-29: DirectX12: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
|
||||||
|
// 2019-04-30: DirectX12: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
|
||||||
|
// 2019-03-29: Misc: Various minor tidying up.
|
||||||
|
// 2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile().
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
|
||||||
|
// 2018-06-12: DirectX12: Moved the ID3D12GraphicsCommandList* parameter from NewFrame() to RenderDrawData().
|
||||||
|
// 2018-06-08: Misc: Extracted imgui_impl_dx12.cpp/.h away from the old combined DX12+Win32 example.
|
||||||
|
// 2018-06-08: DirectX12: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle (to ease support for future multi-viewport).
|
||||||
|
// 2018-02-22: Merged into master with all Win32 code synchronized to other examples.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_dx12.h"
|
||||||
|
|
||||||
|
// DirectX
|
||||||
|
#include <d3d12.h>
|
||||||
|
#include <dxgi1_4.h>
|
||||||
|
#include <d3dcompiler.h>
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Clang/GCC warnings with -Weverything
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
|
||||||
|
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// MinGW workaround, see #4594
|
||||||
|
typedef decltype(D3D12SerializeRootSignature) *_PFN_D3D12_SERIALIZE_ROOT_SIGNATURE;
|
||||||
|
|
||||||
|
// DirectX12 data
|
||||||
|
struct ImGui_ImplDX12_RenderBuffers;
|
||||||
|
|
||||||
|
struct ImGui_ImplDX12_Texture
|
||||||
|
{
|
||||||
|
ID3D12Resource* pTextureResource;
|
||||||
|
D3D12_CPU_DESCRIPTOR_HANDLE hFontSrvCpuDescHandle;
|
||||||
|
D3D12_GPU_DESCRIPTOR_HANDLE hFontSrvGpuDescHandle;
|
||||||
|
|
||||||
|
ImGui_ImplDX12_Texture() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImGui_ImplDX12_Data
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_InitInfo InitInfo;
|
||||||
|
ID3D12Device* pd3dDevice;
|
||||||
|
ID3D12RootSignature* pRootSignature;
|
||||||
|
ID3D12PipelineState* pPipelineState;
|
||||||
|
ID3D12CommandQueue* pCommandQueue;
|
||||||
|
bool commandQueueOwned;
|
||||||
|
DXGI_FORMAT RTVFormat;
|
||||||
|
DXGI_FORMAT DSVFormat;
|
||||||
|
ID3D12DescriptorHeap* pd3dSrvDescHeap;
|
||||||
|
UINT numFramesInFlight;
|
||||||
|
|
||||||
|
ImGui_ImplDX12_RenderBuffers* pFrameResources;
|
||||||
|
UINT frameIndex;
|
||||||
|
|
||||||
|
ImGui_ImplDX12_Texture FontTexture;
|
||||||
|
bool LegacySingleDescriptorUsed;
|
||||||
|
|
||||||
|
ImGui_ImplDX12_Data() { memset((void*)this, 0, sizeof(*this)); frameIndex = UINT_MAX; }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
static ImGui_ImplDX12_Data* ImGui_ImplDX12_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplDX12_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Buffers used during the rendering of a frame
|
||||||
|
struct ImGui_ImplDX12_RenderBuffers
|
||||||
|
{
|
||||||
|
ID3D12Resource* IndexBuffer;
|
||||||
|
ID3D12Resource* VertexBuffer;
|
||||||
|
int IndexBufferSize;
|
||||||
|
int VertexBufferSize;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VERTEX_CONSTANT_BUFFER_DX12
|
||||||
|
{
|
||||||
|
float mvp[4][4];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
static void ImGui_ImplDX12_SetupRenderState(ImDrawData* draw_data, ID3D12GraphicsCommandList* command_list, ImGui_ImplDX12_RenderBuffers* fr)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
|
||||||
|
// Setup orthographic projection matrix into our constant buffer
|
||||||
|
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
|
||||||
|
VERTEX_CONSTANT_BUFFER_DX12 vertex_constant_buffer;
|
||||||
|
{
|
||||||
|
float L = draw_data->DisplayPos.x;
|
||||||
|
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
||||||
|
float T = draw_data->DisplayPos.y;
|
||||||
|
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
||||||
|
float mvp[4][4] =
|
||||||
|
{
|
||||||
|
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, 0.5f, 0.0f },
|
||||||
|
{ (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
|
||||||
|
};
|
||||||
|
memcpy(&vertex_constant_buffer.mvp, mvp, sizeof(mvp));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup viewport
|
||||||
|
D3D12_VIEWPORT vp = {};
|
||||||
|
vp.Width = draw_data->DisplaySize.x * draw_data->FramebufferScale.x;
|
||||||
|
vp.Height = draw_data->DisplaySize.y * draw_data->FramebufferScale.y;
|
||||||
|
vp.MinDepth = 0.0f;
|
||||||
|
vp.MaxDepth = 1.0f;
|
||||||
|
vp.TopLeftX = vp.TopLeftY = 0.0f;
|
||||||
|
command_list->RSSetViewports(1, &vp);
|
||||||
|
|
||||||
|
// Bind shader and vertex buffers
|
||||||
|
unsigned int stride = sizeof(ImDrawVert);
|
||||||
|
unsigned int offset = 0;
|
||||||
|
D3D12_VERTEX_BUFFER_VIEW vbv = {};
|
||||||
|
vbv.BufferLocation = fr->VertexBuffer->GetGPUVirtualAddress() + offset;
|
||||||
|
vbv.SizeInBytes = fr->VertexBufferSize * stride;
|
||||||
|
vbv.StrideInBytes = stride;
|
||||||
|
command_list->IASetVertexBuffers(0, 1, &vbv);
|
||||||
|
D3D12_INDEX_BUFFER_VIEW ibv = {};
|
||||||
|
ibv.BufferLocation = fr->IndexBuffer->GetGPUVirtualAddress();
|
||||||
|
ibv.SizeInBytes = fr->IndexBufferSize * sizeof(ImDrawIdx);
|
||||||
|
ibv.Format = sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT;
|
||||||
|
command_list->IASetIndexBuffer(&ibv);
|
||||||
|
command_list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||||
|
command_list->SetPipelineState(bd->pPipelineState);
|
||||||
|
command_list->SetGraphicsRootSignature(bd->pRootSignature);
|
||||||
|
command_list->SetGraphicsRoot32BitConstants(0, 16, &vertex_constant_buffer, 0);
|
||||||
|
|
||||||
|
// Setup blend factor
|
||||||
|
const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
|
||||||
|
command_list->OMSetBlendFactor(blend_factor);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static inline void SafeRelease(T*& res)
|
||||||
|
{
|
||||||
|
if (res)
|
||||||
|
res->Release();
|
||||||
|
res = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render function
|
||||||
|
void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandList* command_list)
|
||||||
|
{
|
||||||
|
// Avoid rendering when minimized
|
||||||
|
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplDX12_UpdateTexture(tex);
|
||||||
|
|
||||||
|
// FIXME: We are assuming that this only gets called once per frame!
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
bd->frameIndex = bd->frameIndex + 1;
|
||||||
|
ImGui_ImplDX12_RenderBuffers* fr = &bd->pFrameResources[bd->frameIndex % bd->numFramesInFlight];
|
||||||
|
|
||||||
|
// Create and grow vertex/index buffers if needed
|
||||||
|
if (fr->VertexBuffer == nullptr || fr->VertexBufferSize < draw_data->TotalVtxCount)
|
||||||
|
{
|
||||||
|
SafeRelease(fr->VertexBuffer);
|
||||||
|
fr->VertexBufferSize = draw_data->TotalVtxCount + 5000;
|
||||||
|
D3D12_HEAP_PROPERTIES props = {};
|
||||||
|
props.Type = D3D12_HEAP_TYPE_UPLOAD;
|
||||||
|
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||||
|
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||||
|
D3D12_RESOURCE_DESC desc = {};
|
||||||
|
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||||
|
desc.Width = fr->VertexBufferSize * sizeof(ImDrawVert);
|
||||||
|
desc.Height = 1;
|
||||||
|
desc.DepthOrArraySize = 1;
|
||||||
|
desc.MipLevels = 1;
|
||||||
|
desc.Format = DXGI_FORMAT_UNKNOWN;
|
||||||
|
desc.SampleDesc.Count = 1;
|
||||||
|
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||||
|
desc.Flags = D3D12_RESOURCE_FLAG_NONE;
|
||||||
|
if (bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&fr->VertexBuffer)) < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (fr->IndexBuffer == nullptr || fr->IndexBufferSize < draw_data->TotalIdxCount)
|
||||||
|
{
|
||||||
|
SafeRelease(fr->IndexBuffer);
|
||||||
|
fr->IndexBufferSize = draw_data->TotalIdxCount + 10000;
|
||||||
|
D3D12_HEAP_PROPERTIES props = {};
|
||||||
|
props.Type = D3D12_HEAP_TYPE_UPLOAD;
|
||||||
|
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||||
|
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||||
|
D3D12_RESOURCE_DESC desc = {};
|
||||||
|
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||||
|
desc.Width = fr->IndexBufferSize * sizeof(ImDrawIdx);
|
||||||
|
desc.Height = 1;
|
||||||
|
desc.DepthOrArraySize = 1;
|
||||||
|
desc.MipLevels = 1;
|
||||||
|
desc.Format = DXGI_FORMAT_UNKNOWN;
|
||||||
|
desc.SampleDesc.Count = 1;
|
||||||
|
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||||
|
desc.Flags = D3D12_RESOURCE_FLAG_NONE;
|
||||||
|
if (bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&fr->IndexBuffer)) < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upload vertex/index data into a single contiguous GPU buffer
|
||||||
|
// During Map() we specify a null read range (as per DX12 API, this is informational and for tooling only)
|
||||||
|
void* vtx_resource, *idx_resource;
|
||||||
|
D3D12_RANGE range = { 0, 0 };
|
||||||
|
if (fr->VertexBuffer->Map(0, &range, &vtx_resource) != S_OK)
|
||||||
|
return;
|
||||||
|
if (fr->IndexBuffer->Map(0, &range, &idx_resource) != S_OK)
|
||||||
|
return;
|
||||||
|
ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource;
|
||||||
|
ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource;
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
|
||||||
|
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
|
||||||
|
vtx_dst += draw_list->VtxBuffer.Size;
|
||||||
|
idx_dst += draw_list->IdxBuffer.Size;
|
||||||
|
}
|
||||||
|
|
||||||
|
// During Unmap() we specify the written range (as per DX12 API, this is informational and for tooling only)
|
||||||
|
range.End = (SIZE_T)((intptr_t)vtx_dst - (intptr_t)vtx_resource);
|
||||||
|
IM_ASSERT(range.End == draw_data->TotalVtxCount * sizeof(ImDrawVert));
|
||||||
|
fr->VertexBuffer->Unmap(0, &range);
|
||||||
|
range.End = (SIZE_T)((intptr_t)idx_dst - (intptr_t)idx_resource);
|
||||||
|
IM_ASSERT(range.End == draw_data->TotalIdxCount * sizeof(ImDrawIdx));
|
||||||
|
fr->IndexBuffer->Unmap(0, &range);
|
||||||
|
|
||||||
|
// Setup desired DX state
|
||||||
|
ImGui_ImplDX12_SetupRenderState(draw_data, command_list, fr);
|
||||||
|
|
||||||
|
// Setup render state structure (for callbacks and custom texture bindings)
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
ImGui_ImplDX12_RenderState render_state;
|
||||||
|
render_state.Device = bd->pd3dDevice;
|
||||||
|
render_state.CommandList = command_list;
|
||||||
|
platform_io.Renderer_RenderState = &render_state;
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
// (Because we merged all buffers into a single one, we maintain our own offset into them)
|
||||||
|
int global_vtx_offset = 0;
|
||||||
|
int global_idx_offset = 0;
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos;
|
||||||
|
ImVec2 clip_scale = draw_data->FramebufferScale;
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback != nullptr)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplDX12_SetupRenderState(draw_data, command_list, fr);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
|
||||||
|
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Apply scissor/clipping rectangle
|
||||||
|
const D3D12_RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y };
|
||||||
|
command_list->RSSetScissorRects(1, &r);
|
||||||
|
|
||||||
|
// Bind texture, Draw
|
||||||
|
D3D12_GPU_DESCRIPTOR_HANDLE texture_handle = {};
|
||||||
|
texture_handle.ptr = (UINT64)pcmd->GetTexID();
|
||||||
|
command_list->SetGraphicsRootDescriptorTable(1, texture_handle);
|
||||||
|
command_list->DrawIndexedInstanced(pcmd->ElemCount, 1, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
global_idx_offset += draw_list->IdxBuffer.Size;
|
||||||
|
global_vtx_offset += draw_list->VtxBuffer.Size;
|
||||||
|
}
|
||||||
|
platform_io.Renderer_RenderState = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplDX12_DestroyTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_Texture* backend_tex = (ImGui_ImplDX12_Texture*)tex->BackendUserData;
|
||||||
|
if (backend_tex == nullptr)
|
||||||
|
return;
|
||||||
|
IM_ASSERT(backend_tex->hFontSrvGpuDescHandle.ptr == (UINT64)tex->TexID);
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
bd->InitInfo.SrvDescriptorFreeFn(&bd->InitInfo, backend_tex->hFontSrvCpuDescHandle, backend_tex->hFontSrvGpuDescHandle);
|
||||||
|
SafeRelease(backend_tex->pTextureResource);
|
||||||
|
backend_tex->hFontSrvCpuDescHandle.ptr = 0;
|
||||||
|
backend_tex->hFontSrvGpuDescHandle.ptr = 0;
|
||||||
|
IM_DELETE(backend_tex);
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
tex->BackendUserData = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX12_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
bool need_barrier_before_copy = true; // Do we need a resource barrier before we copy new data in?
|
||||||
|
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
ImGui_ImplDX12_Texture* backend_tex = IM_NEW(ImGui_ImplDX12_Texture)();
|
||||||
|
bd->InitInfo.SrvDescriptorAllocFn(&bd->InitInfo, &backend_tex->hFontSrvCpuDescHandle, &backend_tex->hFontSrvGpuDescHandle); // Allocate a desctriptor handle
|
||||||
|
|
||||||
|
D3D12_HEAP_PROPERTIES props = {};
|
||||||
|
props.Type = D3D12_HEAP_TYPE_DEFAULT;
|
||||||
|
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||||
|
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||||
|
|
||||||
|
D3D12_RESOURCE_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
||||||
|
desc.Alignment = 0;
|
||||||
|
desc.Width = tex->Width;
|
||||||
|
desc.Height = tex->Height;
|
||||||
|
desc.DepthOrArraySize = 1;
|
||||||
|
desc.MipLevels = 1;
|
||||||
|
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
desc.SampleDesc.Count = 1;
|
||||||
|
desc.SampleDesc.Quality = 0;
|
||||||
|
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
|
||||||
|
desc.Flags = D3D12_RESOURCE_FLAG_NONE;
|
||||||
|
|
||||||
|
ID3D12Resource* pTexture = nullptr;
|
||||||
|
bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,
|
||||||
|
D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pTexture));
|
||||||
|
|
||||||
|
// Create SRV
|
||||||
|
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
|
||||||
|
ZeroMemory(&srvDesc, sizeof(srvDesc));
|
||||||
|
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
|
||||||
|
srvDesc.Texture2D.MipLevels = desc.MipLevels;
|
||||||
|
srvDesc.Texture2D.MostDetailedMip = 0;
|
||||||
|
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
||||||
|
bd->pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, backend_tex->hFontSrvCpuDescHandle);
|
||||||
|
SafeRelease(backend_tex->pTextureResource);
|
||||||
|
backend_tex->pTextureResource = pTexture;
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)backend_tex->hFontSrvGpuDescHandle.ptr);
|
||||||
|
tex->BackendUserData = backend_tex;
|
||||||
|
need_barrier_before_copy = false; // Because this is a newly-created texture it will be in D3D12_RESOURCE_STATE_COMMON and thus we don't need a barrier
|
||||||
|
// We don't set tex->Status to ImTextureStatus_OK to let the code fallthrough below.
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_Texture* backend_tex = (ImGui_ImplDX12_Texture*)tex->BackendUserData;
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
|
||||||
|
// We could use the smaller rect on _WantCreate but using the full rect allows us to clear the texture.
|
||||||
|
// FIXME-OPT: Uploading single box even when using ImTextureStatus_WantUpdates. Could use tex->Updates[]
|
||||||
|
// - Copy all blocks contiguously in upload buffer.
|
||||||
|
// - Barrier before copy, submit all CopyTextureRegion(), barrier after copy.
|
||||||
|
const int upload_x = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.x;
|
||||||
|
const int upload_y = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.y;
|
||||||
|
const int upload_w = (tex->Status == ImTextureStatus_WantCreate) ? tex->Width : tex->UpdateRect.w;
|
||||||
|
const int upload_h = (tex->Status == ImTextureStatus_WantCreate) ? tex->Height : tex->UpdateRect.h;
|
||||||
|
|
||||||
|
// Update full texture or selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->UpdateRect but you can use tex->Updates[] to upload individual regions.
|
||||||
|
UINT upload_pitch_src = upload_w * tex->BytesPerPixel;
|
||||||
|
UINT upload_pitch_dst = (upload_pitch_src + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u);
|
||||||
|
UINT upload_size = upload_pitch_dst * upload_h;
|
||||||
|
|
||||||
|
D3D12_RESOURCE_DESC desc;
|
||||||
|
ZeroMemory(&desc, sizeof(desc));
|
||||||
|
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||||
|
desc.Alignment = 0;
|
||||||
|
desc.Width = upload_size;
|
||||||
|
desc.Height = 1;
|
||||||
|
desc.DepthOrArraySize = 1;
|
||||||
|
desc.MipLevels = 1;
|
||||||
|
desc.Format = DXGI_FORMAT_UNKNOWN;
|
||||||
|
desc.SampleDesc.Count = 1;
|
||||||
|
desc.SampleDesc.Quality = 0;
|
||||||
|
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||||
|
desc.Flags = D3D12_RESOURCE_FLAG_NONE;
|
||||||
|
|
||||||
|
D3D12_HEAP_PROPERTIES props;
|
||||||
|
memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));
|
||||||
|
props.Type = D3D12_HEAP_TYPE_UPLOAD;
|
||||||
|
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||||
|
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||||
|
|
||||||
|
// FIXME-OPT: Can upload buffer be reused?
|
||||||
|
ID3D12Resource* uploadBuffer = nullptr;
|
||||||
|
HRESULT hr = bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,
|
||||||
|
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&uploadBuffer));
|
||||||
|
IM_ASSERT(SUCCEEDED(hr));
|
||||||
|
|
||||||
|
// Create temporary command list and execute immediately
|
||||||
|
ID3D12Fence* fence = nullptr;
|
||||||
|
hr = bd->pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
|
||||||
|
IM_ASSERT(SUCCEEDED(hr));
|
||||||
|
|
||||||
|
HANDLE event = ::CreateEvent(0, 0, 0, 0);
|
||||||
|
IM_ASSERT(event != nullptr);
|
||||||
|
|
||||||
|
// FIXME-OPT: Create once and reuse?
|
||||||
|
ID3D12CommandAllocator* cmdAlloc = nullptr;
|
||||||
|
hr = bd->pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmdAlloc));
|
||||||
|
IM_ASSERT(SUCCEEDED(hr));
|
||||||
|
|
||||||
|
// FIXME-OPT: Can be use the one from user? (pass ID3D12GraphicsCommandList* to ImGui_ImplDX12_UpdateTextures)
|
||||||
|
ID3D12GraphicsCommandList* cmdList = nullptr;
|
||||||
|
hr = bd->pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, cmdAlloc, nullptr, IID_PPV_ARGS(&cmdList));
|
||||||
|
IM_ASSERT(SUCCEEDED(hr));
|
||||||
|
|
||||||
|
// Copy to upload buffer
|
||||||
|
void* mapped = nullptr;
|
||||||
|
D3D12_RANGE range = { 0, upload_size };
|
||||||
|
hr = uploadBuffer->Map(0, &range, &mapped);
|
||||||
|
IM_ASSERT(SUCCEEDED(hr));
|
||||||
|
for (int y = 0; y < upload_h; y++)
|
||||||
|
memcpy((void*)((uintptr_t)mapped + y * upload_pitch_dst), tex->GetPixelsAt(upload_x, upload_y + y), upload_pitch_src);
|
||||||
|
uploadBuffer->Unmap(0, &range);
|
||||||
|
|
||||||
|
if (need_barrier_before_copy)
|
||||||
|
{
|
||||||
|
D3D12_RESOURCE_BARRIER barrier = {};
|
||||||
|
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
||||||
|
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
|
||||||
|
barrier.Transition.pResource = backend_tex->pTextureResource;
|
||||||
|
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
|
||||||
|
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
|
||||||
|
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
|
||||||
|
cmdList->ResourceBarrier(1, &barrier);
|
||||||
|
}
|
||||||
|
|
||||||
|
D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
|
||||||
|
D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
|
||||||
|
{
|
||||||
|
srcLocation.pResource = uploadBuffer;
|
||||||
|
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||||
|
srcLocation.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
srcLocation.PlacedFootprint.Footprint.Width = upload_w;
|
||||||
|
srcLocation.PlacedFootprint.Footprint.Height = upload_h;
|
||||||
|
srcLocation.PlacedFootprint.Footprint.Depth = 1;
|
||||||
|
srcLocation.PlacedFootprint.Footprint.RowPitch = upload_pitch_dst;
|
||||||
|
dstLocation.pResource = backend_tex->pTextureResource;
|
||||||
|
dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||||
|
dstLocation.SubresourceIndex = 0;
|
||||||
|
}
|
||||||
|
cmdList->CopyTextureRegion(&dstLocation, upload_x, upload_y, 0, &srcLocation, nullptr);
|
||||||
|
|
||||||
|
{
|
||||||
|
D3D12_RESOURCE_BARRIER barrier = {};
|
||||||
|
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
||||||
|
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
|
||||||
|
barrier.Transition.pResource = backend_tex->pTextureResource;
|
||||||
|
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
|
||||||
|
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
|
||||||
|
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
|
||||||
|
cmdList->ResourceBarrier(1, &barrier);
|
||||||
|
}
|
||||||
|
|
||||||
|
hr = cmdList->Close();
|
||||||
|
IM_ASSERT(SUCCEEDED(hr));
|
||||||
|
|
||||||
|
ID3D12CommandQueue* cmdQueue = bd->pCommandQueue;
|
||||||
|
cmdQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&cmdList);
|
||||||
|
hr = cmdQueue->Signal(fence, 1);
|
||||||
|
IM_ASSERT(SUCCEEDED(hr));
|
||||||
|
|
||||||
|
// FIXME-OPT: Suboptimal?
|
||||||
|
// - To remove this may need to create NumFramesInFlight x ImGui_ImplDX12_FrameContext in backend data (mimick docking version)
|
||||||
|
// - Store per-frame in flight: upload buffer?
|
||||||
|
// - Where do cmdList and cmdAlloc fit?
|
||||||
|
fence->SetEventOnCompletion(1, event);
|
||||||
|
::WaitForSingleObject(event, INFINITE);
|
||||||
|
|
||||||
|
cmdList->Release();
|
||||||
|
cmdAlloc->Release();
|
||||||
|
::CloseHandle(event);
|
||||||
|
fence->Release();
|
||||||
|
uploadBuffer->Release();
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames >= (int)bd->numFramesInFlight)
|
||||||
|
ImGui_ImplDX12_DestroyTexture(tex);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplDX12_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
if (!bd || !bd->pd3dDevice)
|
||||||
|
return false;
|
||||||
|
if (bd->pPipelineState)
|
||||||
|
ImGui_ImplDX12_InvalidateDeviceObjects();
|
||||||
|
|
||||||
|
// Create the root signature
|
||||||
|
{
|
||||||
|
D3D12_DESCRIPTOR_RANGE descRange = {};
|
||||||
|
descRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
|
||||||
|
descRange.NumDescriptors = 1;
|
||||||
|
descRange.BaseShaderRegister = 0;
|
||||||
|
descRange.RegisterSpace = 0;
|
||||||
|
descRange.OffsetInDescriptorsFromTableStart = 0;
|
||||||
|
|
||||||
|
D3D12_ROOT_PARAMETER param[2] = {};
|
||||||
|
|
||||||
|
param[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
|
||||||
|
param[0].Constants.ShaderRegister = 0;
|
||||||
|
param[0].Constants.RegisterSpace = 0;
|
||||||
|
param[0].Constants.Num32BitValues = 16;
|
||||||
|
param[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
|
||||||
|
|
||||||
|
param[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
|
||||||
|
param[1].DescriptorTable.NumDescriptorRanges = 1;
|
||||||
|
param[1].DescriptorTable.pDescriptorRanges = &descRange;
|
||||||
|
param[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
|
||||||
|
|
||||||
|
// Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling.
|
||||||
|
D3D12_STATIC_SAMPLER_DESC staticSampler = {};
|
||||||
|
staticSampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
|
||||||
|
staticSampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
|
||||||
|
staticSampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
|
||||||
|
staticSampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
|
||||||
|
staticSampler.MipLODBias = 0.f;
|
||||||
|
staticSampler.MaxAnisotropy = 0;
|
||||||
|
staticSampler.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
|
||||||
|
staticSampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
|
||||||
|
staticSampler.MinLOD = 0.f;
|
||||||
|
staticSampler.MaxLOD = D3D12_FLOAT32_MAX;
|
||||||
|
staticSampler.ShaderRegister = 0;
|
||||||
|
staticSampler.RegisterSpace = 0;
|
||||||
|
staticSampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
|
||||||
|
|
||||||
|
D3D12_ROOT_SIGNATURE_DESC desc = {};
|
||||||
|
desc.NumParameters = _countof(param);
|
||||||
|
desc.pParameters = param;
|
||||||
|
desc.NumStaticSamplers = 1;
|
||||||
|
desc.pStaticSamplers = &staticSampler;
|
||||||
|
desc.Flags =
|
||||||
|
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT |
|
||||||
|
D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS |
|
||||||
|
D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS |
|
||||||
|
D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS;
|
||||||
|
|
||||||
|
// Load d3d12.dll and D3D12SerializeRootSignature() function address dynamically to facilitate using with D3D12On7.
|
||||||
|
// See if any version of d3d12.dll is already loaded in the process. If so, give preference to that.
|
||||||
|
static HINSTANCE d3d12_dll = ::GetModuleHandleA("d3d12.dll");
|
||||||
|
if (d3d12_dll == nullptr)
|
||||||
|
{
|
||||||
|
// Attempt to load d3d12.dll from local directories. This will only succeed if
|
||||||
|
// (1) the current OS is Windows 7, and
|
||||||
|
// (2) there exists a version of d3d12.dll for Windows 7 (D3D12On7) in one of the following directories.
|
||||||
|
// See https://github.com/ocornut/imgui/pull/3696 for details.
|
||||||
|
const char* localD3d12Paths[] = { ".\\d3d12.dll", ".\\d3d12on7\\d3d12.dll", ".\\12on7\\d3d12.dll" }; // A. current directory, B. used by some games, C. used in Microsoft D3D12On7 sample
|
||||||
|
for (int i = 0; i < IM_ARRAYSIZE(localD3d12Paths); i++)
|
||||||
|
if ((d3d12_dll = ::LoadLibraryA(localD3d12Paths[i])) != nullptr)
|
||||||
|
break;
|
||||||
|
|
||||||
|
// If failed, we are on Windows >= 10.
|
||||||
|
if (d3d12_dll == nullptr)
|
||||||
|
d3d12_dll = ::LoadLibraryA("d3d12.dll");
|
||||||
|
|
||||||
|
if (d3d12_dll == nullptr)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignatureFn = (_PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)(void*)::GetProcAddress(d3d12_dll, "D3D12SerializeRootSignature");
|
||||||
|
if (D3D12SerializeRootSignatureFn == nullptr)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
ID3DBlob* blob = nullptr;
|
||||||
|
if (D3D12SerializeRootSignatureFn(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, nullptr) != S_OK)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
bd->pd3dDevice->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&bd->pRootSignature));
|
||||||
|
blob->Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
// By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
|
||||||
|
// If you would like to use this DX12 sample code but remove this dependency you can:
|
||||||
|
// 1) compile once, save the compiled shader blobs into a file or source code and assign them to psoDesc.VS/PS [preferred solution]
|
||||||
|
// 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
|
||||||
|
// See https://github.com/ocornut/imgui/pull/638 for sources and details.
|
||||||
|
|
||||||
|
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
|
||||||
|
psoDesc.NodeMask = 1;
|
||||||
|
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
||||||
|
psoDesc.pRootSignature = bd->pRootSignature;
|
||||||
|
psoDesc.SampleMask = UINT_MAX;
|
||||||
|
psoDesc.NumRenderTargets = 1;
|
||||||
|
psoDesc.RTVFormats[0] = bd->RTVFormat;
|
||||||
|
psoDesc.DSVFormat = bd->DSVFormat;
|
||||||
|
psoDesc.SampleDesc.Count = 1;
|
||||||
|
psoDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
|
||||||
|
|
||||||
|
ID3DBlob* vertexShaderBlob;
|
||||||
|
ID3DBlob* pixelShaderBlob;
|
||||||
|
|
||||||
|
// Create the vertex shader
|
||||||
|
{
|
||||||
|
static const char* vertexShader =
|
||||||
|
"cbuffer vertexBuffer : register(b0) \
|
||||||
|
{\
|
||||||
|
float4x4 ProjectionMatrix; \
|
||||||
|
};\
|
||||||
|
struct VS_INPUT\
|
||||||
|
{\
|
||||||
|
float2 pos : POSITION;\
|
||||||
|
float4 col : COLOR0;\
|
||||||
|
float2 uv : TEXCOORD0;\
|
||||||
|
};\
|
||||||
|
\
|
||||||
|
struct PS_INPUT\
|
||||||
|
{\
|
||||||
|
float4 pos : SV_POSITION;\
|
||||||
|
float4 col : COLOR0;\
|
||||||
|
float2 uv : TEXCOORD0;\
|
||||||
|
};\
|
||||||
|
\
|
||||||
|
PS_INPUT main(VS_INPUT input)\
|
||||||
|
{\
|
||||||
|
PS_INPUT output;\
|
||||||
|
output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
|
||||||
|
output.col = input.col;\
|
||||||
|
output.uv = input.uv;\
|
||||||
|
return output;\
|
||||||
|
}";
|
||||||
|
|
||||||
|
if (FAILED(D3DCompile(vertexShader, strlen(vertexShader), nullptr, nullptr, nullptr, "main", "vs_5_0", 0, 0, &vertexShaderBlob, nullptr)))
|
||||||
|
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
|
||||||
|
psoDesc.VS = { vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize() };
|
||||||
|
|
||||||
|
// Create the input layout
|
||||||
|
static D3D12_INPUT_ELEMENT_DESC local_layout[] =
|
||||||
|
{
|
||||||
|
{ "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
||||||
|
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
||||||
|
{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (UINT)offsetof(ImDrawVert, col), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
||||||
|
};
|
||||||
|
psoDesc.InputLayout = { local_layout, 3 };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the pixel shader
|
||||||
|
{
|
||||||
|
static const char* pixelShader =
|
||||||
|
"struct PS_INPUT\
|
||||||
|
{\
|
||||||
|
float4 pos : SV_POSITION;\
|
||||||
|
float4 col : COLOR0;\
|
||||||
|
float2 uv : TEXCOORD0;\
|
||||||
|
};\
|
||||||
|
SamplerState sampler0 : register(s0);\
|
||||||
|
Texture2D texture0 : register(t0);\
|
||||||
|
\
|
||||||
|
float4 main(PS_INPUT input) : SV_Target\
|
||||||
|
{\
|
||||||
|
float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
|
||||||
|
return out_col; \
|
||||||
|
}";
|
||||||
|
|
||||||
|
if (FAILED(D3DCompile(pixelShader, strlen(pixelShader), nullptr, nullptr, nullptr, "main", "ps_5_0", 0, 0, &pixelShaderBlob, nullptr)))
|
||||||
|
{
|
||||||
|
vertexShaderBlob->Release();
|
||||||
|
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
|
||||||
|
}
|
||||||
|
psoDesc.PS = { pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize() };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the blending setup
|
||||||
|
{
|
||||||
|
D3D12_BLEND_DESC& desc = psoDesc.BlendState;
|
||||||
|
desc.AlphaToCoverageEnable = false;
|
||||||
|
desc.RenderTarget[0].BlendEnable = true;
|
||||||
|
desc.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA;
|
||||||
|
desc.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA;
|
||||||
|
desc.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;
|
||||||
|
desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_ONE;
|
||||||
|
desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA;
|
||||||
|
desc.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;
|
||||||
|
desc.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the rasterizer state
|
||||||
|
{
|
||||||
|
D3D12_RASTERIZER_DESC& desc = psoDesc.RasterizerState;
|
||||||
|
desc.FillMode = D3D12_FILL_MODE_SOLID;
|
||||||
|
desc.CullMode = D3D12_CULL_MODE_NONE;
|
||||||
|
desc.FrontCounterClockwise = FALSE;
|
||||||
|
desc.DepthBias = D3D12_DEFAULT_DEPTH_BIAS;
|
||||||
|
desc.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;
|
||||||
|
desc.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;
|
||||||
|
desc.DepthClipEnable = true;
|
||||||
|
desc.MultisampleEnable = FALSE;
|
||||||
|
desc.AntialiasedLineEnable = FALSE;
|
||||||
|
desc.ForcedSampleCount = 0;
|
||||||
|
desc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create depth-stencil State
|
||||||
|
{
|
||||||
|
D3D12_DEPTH_STENCIL_DESC& desc = psoDesc.DepthStencilState;
|
||||||
|
desc.DepthEnable = false;
|
||||||
|
desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
|
||||||
|
desc.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
|
||||||
|
desc.StencilEnable = false;
|
||||||
|
desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
|
||||||
|
desc.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;
|
||||||
|
desc.BackFace = desc.FrontFace;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT result_pipeline_state = bd->pd3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&bd->pPipelineState));
|
||||||
|
vertexShaderBlob->Release();
|
||||||
|
pixelShaderBlob->Release();
|
||||||
|
if (result_pipeline_state != S_OK)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX12_InvalidateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
if (!bd || !bd->pd3dDevice)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (bd->commandQueueOwned)
|
||||||
|
SafeRelease(bd->pCommandQueue);
|
||||||
|
bd->commandQueueOwned = false;
|
||||||
|
SafeRelease(bd->pRootSignature);
|
||||||
|
SafeRelease(bd->pPipelineState);
|
||||||
|
|
||||||
|
// Destroy all textures
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
ImGui_ImplDX12_DestroyTexture(tex);
|
||||||
|
|
||||||
|
for (UINT i = 0; i < bd->numFramesInFlight; i++)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_RenderBuffers* fr = &bd->pFrameResources[i];
|
||||||
|
SafeRelease(fr->IndexBuffer);
|
||||||
|
SafeRelease(fr->VertexBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||||
|
static void ImGui_ImplDX12_InitLegacySingleDescriptorMode(ImGui_ImplDX12_InitInfo* init_info)
|
||||||
|
{
|
||||||
|
// Wrap legacy behavior of passing space for a single descriptor
|
||||||
|
IM_ASSERT(init_info->LegacySingleSrvCpuDescriptor.ptr != 0 && init_info->LegacySingleSrvGpuDescriptor.ptr != 0);
|
||||||
|
init_info->SrvDescriptorAllocFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE* out_cpu_handle, D3D12_GPU_DESCRIPTOR_HANDLE* out_gpu_handle)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
IM_ASSERT(bd->LegacySingleDescriptorUsed == false && "Only 1 simultaneous texture allowed with legacy ImGui_ImplDX12_Init() signature!");
|
||||||
|
*out_cpu_handle = bd->InitInfo.LegacySingleSrvCpuDescriptor;
|
||||||
|
*out_gpu_handle = bd->InitInfo.LegacySingleSrvGpuDescriptor;
|
||||||
|
bd->LegacySingleDescriptorUsed = true;
|
||||||
|
};
|
||||||
|
init_info->SrvDescriptorFreeFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_GPU_DESCRIPTOR_HANDLE)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
IM_ASSERT(bd->LegacySingleDescriptorUsed == true);
|
||||||
|
bd->LegacySingleDescriptorUsed = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool ImGui_ImplDX12_Init(ImGui_ImplDX12_InitInfo* init_info)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplDX12_Data* bd = IM_NEW(ImGui_ImplDX12_Data)();
|
||||||
|
bd->InitInfo = *init_info; // Deep copy
|
||||||
|
init_info = &bd->InitInfo;
|
||||||
|
|
||||||
|
bd->pd3dDevice = init_info->Device;
|
||||||
|
IM_ASSERT(init_info->CommandQueue != NULL);
|
||||||
|
bd->pCommandQueue = init_info->CommandQueue;
|
||||||
|
bd->RTVFormat = init_info->RTVFormat;
|
||||||
|
bd->DSVFormat = init_info->DSVFormat;
|
||||||
|
bd->numFramesInFlight = init_info->NumFramesInFlight;
|
||||||
|
bd->pd3dSrvDescHeap = init_info->SrvDescriptorHeap;
|
||||||
|
|
||||||
|
io.BackendRendererUserData = (void*)bd;
|
||||||
|
io.BackendRendererName = "imgui_impl_dx12";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||||
|
if (init_info->SrvDescriptorAllocFn == nullptr)
|
||||||
|
ImGui_ImplDX12_InitLegacySingleDescriptorMode(init_info);
|
||||||
|
#endif
|
||||||
|
IM_ASSERT(init_info->SrvDescriptorAllocFn != nullptr && init_info->SrvDescriptorFreeFn != nullptr);
|
||||||
|
|
||||||
|
// Create buffers with a default size (they will later be grown as needed)
|
||||||
|
bd->frameIndex = UINT_MAX;
|
||||||
|
bd->pFrameResources = new ImGui_ImplDX12_RenderBuffers[bd->numFramesInFlight];
|
||||||
|
for (int i = 0; i < (int)bd->numFramesInFlight; i++)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_RenderBuffers* fr = &bd->pFrameResources[i];
|
||||||
|
fr->IndexBuffer = nullptr;
|
||||||
|
fr->VertexBuffer = nullptr;
|
||||||
|
fr->IndexBufferSize = 10000;
|
||||||
|
fr->VertexBufferSize = 5000;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||||
|
// Legacy initialization API Obsoleted in 1.91.5
|
||||||
|
// font_srv_cpu_desc_handle and font_srv_gpu_desc_handle are handles to a single SRV descriptor to use for the internal font texture, they must be in 'srv_descriptor_heap'
|
||||||
|
bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FORMAT rtv_format, ID3D12DescriptorHeap* srv_descriptor_heap, D3D12_CPU_DESCRIPTOR_HANDLE font_srv_cpu_desc_handle, D3D12_GPU_DESCRIPTOR_HANDLE font_srv_gpu_desc_handle)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_InitInfo init_info;
|
||||||
|
init_info.Device = device;
|
||||||
|
init_info.NumFramesInFlight = num_frames_in_flight;
|
||||||
|
init_info.RTVFormat = rtv_format;
|
||||||
|
init_info.SrvDescriptorHeap = srv_descriptor_heap;
|
||||||
|
init_info.LegacySingleSrvCpuDescriptor = font_srv_cpu_desc_handle;
|
||||||
|
init_info.LegacySingleSrvGpuDescriptor = font_srv_gpu_desc_handle;
|
||||||
|
|
||||||
|
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
|
||||||
|
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||||
|
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
|
||||||
|
queueDesc.NodeMask = 1;
|
||||||
|
HRESULT hr = device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&init_info.CommandQueue));
|
||||||
|
IM_ASSERT(SUCCEEDED(hr));
|
||||||
|
|
||||||
|
bool ret = ImGui_ImplDX12_Init(&init_info);
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
bd->commandQueueOwned = true;
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.BackendFlags &= ~ImGuiBackendFlags_RendererHasTextures; // Using legacy ImGui_ImplDX12_Init() call with 1 SRV descriptor we cannot support multiple textures.
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void ImGui_ImplDX12_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Clean up windows and device objects
|
||||||
|
ImGui_ImplDX12_InvalidateDeviceObjects();
|
||||||
|
delete[] bd->pFrameResources;
|
||||||
|
|
||||||
|
io.BackendRendererName = nullptr;
|
||||||
|
io.BackendRendererUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX12_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX12_Init()?");
|
||||||
|
|
||||||
|
if (!bd->pPipelineState)
|
||||||
|
if (!ImGui_ImplDX12_CreateDeviceObjects())
|
||||||
|
IM_ASSERT(0 && "ImGui_ImplDX12_CreateDeviceObjects() failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
79
imgui/backends/imgui_impl_dx12.h
Normal file
79
imgui/backends/imgui_impl_dx12.h
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
// dear imgui: Renderer Backend for DirectX12
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. Win32)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
|
||||||
|
// The aim of imgui_impl_dx12.h/.cpp is to be usable in your engine without any modification.
|
||||||
|
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include <dxgiformat.h> // DXGI_FORMAT
|
||||||
|
#include <d3d12.h> // D3D12_CPU_DESCRIPTOR_HANDLE
|
||||||
|
|
||||||
|
// Initialization data, for ImGui_ImplDX12_Init()
|
||||||
|
struct ImGui_ImplDX12_InitInfo
|
||||||
|
{
|
||||||
|
ID3D12Device* Device;
|
||||||
|
ID3D12CommandQueue* CommandQueue; // Command queue used for queuing texture uploads.
|
||||||
|
int NumFramesInFlight;
|
||||||
|
DXGI_FORMAT RTVFormat; // RenderTarget format.
|
||||||
|
DXGI_FORMAT DSVFormat; // DepthStencilView format.
|
||||||
|
void* UserData;
|
||||||
|
|
||||||
|
// Allocating SRV descriptors for textures is up to the application, so we provide callbacks.
|
||||||
|
// (current version of the backend will only allocate one descriptor, from 1.92 the backend will need to allocate more)
|
||||||
|
ID3D12DescriptorHeap* SrvDescriptorHeap;
|
||||||
|
void (*SrvDescriptorAllocFn)(ImGui_ImplDX12_InitInfo* info, D3D12_CPU_DESCRIPTOR_HANDLE* out_cpu_desc_handle, D3D12_GPU_DESCRIPTOR_HANDLE* out_gpu_desc_handle);
|
||||||
|
void (*SrvDescriptorFreeFn)(ImGui_ImplDX12_InitInfo* info, D3D12_CPU_DESCRIPTOR_HANDLE cpu_desc_handle, D3D12_GPU_DESCRIPTOR_HANDLE gpu_desc_handle);
|
||||||
|
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||||
|
D3D12_CPU_DESCRIPTOR_HANDLE LegacySingleSrvCpuDescriptor; // To facilitate transition from single descriptor to allocator callback, you may use those.
|
||||||
|
D3D12_GPU_DESCRIPTOR_HANDLE LegacySingleSrvGpuDescriptor;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ImGui_ImplDX12_InitInfo() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplDX12_Init(ImGui_ImplDX12_InitInfo* info);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX12_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX12_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandList* graphics_command_list);
|
||||||
|
|
||||||
|
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||||
|
// Legacy initialization API Obsoleted in 1.91.5
|
||||||
|
// - font_srv_cpu_desc_handle and font_srv_gpu_desc_handle are handles to a single SRV descriptor to use for the internal font texture, they must be in 'srv_descriptor_heap'
|
||||||
|
// - When we introduced the ImGui_ImplDX12_InitInfo struct we also added a 'ID3D12CommandQueue* CommandQueue' field.
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FORMAT rtv_format, ID3D12DescriptorHeap* srv_descriptor_heap, D3D12_CPU_DESCRIPTOR_HANDLE font_srv_cpu_desc_handle, D3D12_GPU_DESCRIPTOR_HANDLE font_srv_gpu_desc_handle);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Use if you want to reset your rendering device without losing Dear ImGui state.
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplDX12_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX12_InvalidateDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX12_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
// [BETA] Selected render state data shared with callbacks.
|
||||||
|
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplDX12_RenderDrawData() call.
|
||||||
|
// (Please open an issue if you feel you need access to more data)
|
||||||
|
struct ImGui_ImplDX12_RenderState
|
||||||
|
{
|
||||||
|
ID3D12Device* Device;
|
||||||
|
ID3D12GraphicsCommandList* CommandList;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
478
imgui/backends/imgui_impl_dx9.cpp
Normal file
478
imgui/backends/imgui_impl_dx9.cpp
Normal file
@@ -0,0 +1,478 @@
|
|||||||
|
// dear imgui: Renderer Backend for DirectX9
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. Win32)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'LPDIRECT3DTEXTURE9' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: IMGUI_USE_BGRA_PACKED_COLOR support, as this is the optimal color encoding for DirectX9.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-06-11: DirectX9: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas.
|
||||||
|
// 2024-10-07: DirectX9: Changed default texture sampler to Clamp instead of Repeat/Wrap.
|
||||||
|
// 2024-02-12: DirectX9: Using RGBA format when supported by the driver to avoid CPU side conversion. (#6575)
|
||||||
|
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
|
||||||
|
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
|
||||||
|
// 2021-06-25: DirectX9: Explicitly disable texture state stages after >= 1.
|
||||||
|
// 2021-05-19: DirectX9: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
|
||||||
|
// 2021-04-23: DirectX9: Explicitly setting up more graphics states to increase compatibility with unusual non-default states.
|
||||||
|
// 2021-03-18: DirectX9: Calling IDirect3DStateBlock9::Capture() after CreateStateBlock() as a workaround for state restoring issues (see #3857).
|
||||||
|
// 2021-03-03: DirectX9: Added support for IMGUI_USE_BGRA_PACKED_COLOR in user's imconfig file.
|
||||||
|
// 2021-02-18: DirectX9: Change blending equation to preserve alpha in output buffer.
|
||||||
|
// 2019-05-29: DirectX9: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
|
||||||
|
// 2019-04-30: DirectX9: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
|
||||||
|
// 2019-03-29: Misc: Fixed erroneous assert in ImGui_ImplDX9_InvalidateDeviceObjects().
|
||||||
|
// 2019-01-16: Misc: Disabled fog before drawing UI's. Fixes issue #2288.
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
|
||||||
|
// 2018-06-08: Misc: Extracted imgui_impl_dx9.cpp/.h away from the old combined DX9+Win32 example.
|
||||||
|
// 2018-06-08: DirectX9: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
|
||||||
|
// 2018-05-07: Render: Saving/restoring Transform because they don't seem to be included in the StateBlock. Setting shading mode to Gouraud.
|
||||||
|
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX9_RenderDrawData() in the .h file so you can call it yourself.
|
||||||
|
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_dx9.h"
|
||||||
|
|
||||||
|
// DirectX
|
||||||
|
#include <d3d9.h>
|
||||||
|
|
||||||
|
// Clang/GCC warnings with -Weverything
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
|
||||||
|
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// DirectX data
|
||||||
|
struct ImGui_ImplDX9_Data
|
||||||
|
{
|
||||||
|
LPDIRECT3DDEVICE9 pd3dDevice;
|
||||||
|
LPDIRECT3DVERTEXBUFFER9 pVB;
|
||||||
|
LPDIRECT3DINDEXBUFFER9 pIB;
|
||||||
|
int VertexBufferSize;
|
||||||
|
int IndexBufferSize;
|
||||||
|
bool HasRgbaSupport;
|
||||||
|
|
||||||
|
ImGui_ImplDX9_Data() { memset((void*)this, 0, sizeof(*this)); VertexBufferSize = 5000; IndexBufferSize = 10000; }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CUSTOMVERTEX
|
||||||
|
{
|
||||||
|
float pos[3];
|
||||||
|
D3DCOLOR col;
|
||||||
|
float uv[2];
|
||||||
|
};
|
||||||
|
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
|
||||||
|
|
||||||
|
#ifdef IMGUI_USE_BGRA_PACKED_COLOR
|
||||||
|
#define IMGUI_COL_TO_DX9_ARGB(_COL) (_COL)
|
||||||
|
#else
|
||||||
|
#define IMGUI_COL_TO_DX9_ARGB(_COL) (((_COL) & 0xFF00FF00) | (((_COL) & 0xFF0000) >> 16) | (((_COL) & 0xFF) << 16))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
static ImGui_ImplDX9_Data* ImGui_ImplDX9_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplDX9_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
static void ImGui_ImplDX9_SetupRenderState(ImDrawData* draw_data)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
|
||||||
|
|
||||||
|
// Setup viewport
|
||||||
|
D3DVIEWPORT9 vp;
|
||||||
|
vp.X = vp.Y = 0;
|
||||||
|
vp.Width = (DWORD)draw_data->DisplaySize.x;
|
||||||
|
vp.Height = (DWORD)draw_data->DisplaySize.y;
|
||||||
|
vp.MinZ = 0.0f;
|
||||||
|
vp.MaxZ = 1.0f;
|
||||||
|
|
||||||
|
LPDIRECT3DDEVICE9 device = bd->pd3dDevice;
|
||||||
|
device->SetViewport(&vp);
|
||||||
|
|
||||||
|
// Setup render state: fixed-pipeline, alpha-blending, no face culling, no depth testing, shade mode (for gradient), bilinear sampling.
|
||||||
|
device->SetPixelShader(nullptr);
|
||||||
|
device->SetVertexShader(nullptr);
|
||||||
|
device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
||||||
|
device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
|
||||||
|
device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
|
||||||
|
device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
|
||||||
|
device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
||||||
|
device->SetRenderState(D3DRS_ZENABLE, FALSE);
|
||||||
|
device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
|
||||||
|
device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
|
||||||
|
device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
|
||||||
|
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
|
||||||
|
device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
|
||||||
|
device->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_ONE);
|
||||||
|
device->SetRenderState(D3DRS_DESTBLENDALPHA, D3DBLEND_INVSRCALPHA);
|
||||||
|
device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
|
||||||
|
device->SetRenderState(D3DRS_FOGENABLE, FALSE);
|
||||||
|
device->SetRenderState(D3DRS_RANGEFOGENABLE, FALSE);
|
||||||
|
device->SetRenderState(D3DRS_SPECULARENABLE, FALSE);
|
||||||
|
device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
|
||||||
|
device->SetRenderState(D3DRS_CLIPPING, TRUE);
|
||||||
|
device->SetRenderState(D3DRS_LIGHTING, FALSE);
|
||||||
|
device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
|
||||||
|
device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
|
||||||
|
device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
|
||||||
|
device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
|
||||||
|
device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
|
||||||
|
device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
|
||||||
|
device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
|
||||||
|
device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
|
||||||
|
device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
||||||
|
device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
||||||
|
device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
|
||||||
|
device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
|
||||||
|
|
||||||
|
// Setup orthographic projection matrix
|
||||||
|
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
|
||||||
|
// Being agnostic of whether <d3dx9.h> or <DirectXMath.h> can be used, we aren't relying on D3DXMatrixIdentity()/D3DXMatrixOrthoOffCenterLH() or DirectX::XMMatrixIdentity()/DirectX::XMMatrixOrthographicOffCenterLH()
|
||||||
|
{
|
||||||
|
float L = draw_data->DisplayPos.x + 0.5f;
|
||||||
|
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x + 0.5f;
|
||||||
|
float T = draw_data->DisplayPos.y + 0.5f;
|
||||||
|
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y + 0.5f;
|
||||||
|
D3DMATRIX mat_identity = { { { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } } };
|
||||||
|
D3DMATRIX mat_projection =
|
||||||
|
{ { {
|
||||||
|
2.0f/(R-L), 0.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 2.0f/(T-B), 0.0f, 0.0f,
|
||||||
|
0.0f, 0.0f, 0.5f, 0.0f,
|
||||||
|
(L+R)/(L-R), (T+B)/(B-T), 0.5f, 1.0f
|
||||||
|
} } };
|
||||||
|
device->SetTransform(D3DTS_WORLD, &mat_identity);
|
||||||
|
device->SetTransform(D3DTS_VIEW, &mat_identity);
|
||||||
|
device->SetTransform(D3DTS_PROJECTION, &mat_projection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render function.
|
||||||
|
void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data)
|
||||||
|
{
|
||||||
|
// Avoid rendering when minimized
|
||||||
|
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
|
||||||
|
LPDIRECT3DDEVICE9 device = bd->pd3dDevice;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplDX9_UpdateTexture(tex);
|
||||||
|
|
||||||
|
// Create and grow buffers if needed
|
||||||
|
if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount)
|
||||||
|
{
|
||||||
|
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
|
||||||
|
bd->VertexBufferSize = draw_data->TotalVtxCount + 5000;
|
||||||
|
if (device->CreateVertexBuffer(bd->VertexBufferSize * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &bd->pVB, nullptr) < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!bd->pIB || bd->IndexBufferSize < draw_data->TotalIdxCount)
|
||||||
|
{
|
||||||
|
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
|
||||||
|
bd->IndexBufferSize = draw_data->TotalIdxCount + 10000;
|
||||||
|
if (device->CreateIndexBuffer(bd->IndexBufferSize * sizeof(ImDrawIdx), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, sizeof(ImDrawIdx) == 2 ? D3DFMT_INDEX16 : D3DFMT_INDEX32, D3DPOOL_DEFAULT, &bd->pIB, nullptr) < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backup the DX9 state
|
||||||
|
IDirect3DStateBlock9* state_block = nullptr;
|
||||||
|
if (device->CreateStateBlock(D3DSBT_ALL, &state_block) < 0)
|
||||||
|
return;
|
||||||
|
if (state_block->Capture() < 0)
|
||||||
|
{
|
||||||
|
state_block->Release();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backup the DX9 transform (DX9 documentation suggests that it is included in the StateBlock but it doesn't appear to)
|
||||||
|
D3DMATRIX last_world, last_view, last_projection;
|
||||||
|
device->GetTransform(D3DTS_WORLD, &last_world);
|
||||||
|
device->GetTransform(D3DTS_VIEW, &last_view);
|
||||||
|
device->GetTransform(D3DTS_PROJECTION, &last_projection);
|
||||||
|
|
||||||
|
// Allocate buffers
|
||||||
|
CUSTOMVERTEX* vtx_dst;
|
||||||
|
ImDrawIdx* idx_dst;
|
||||||
|
if (bd->pVB->Lock(0, (UINT)(draw_data->TotalVtxCount * sizeof(CUSTOMVERTEX)), (void**)&vtx_dst, D3DLOCK_DISCARD) < 0)
|
||||||
|
{
|
||||||
|
state_block->Release();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (bd->pIB->Lock(0, (UINT)(draw_data->TotalIdxCount * sizeof(ImDrawIdx)), (void**)&idx_dst, D3DLOCK_DISCARD) < 0)
|
||||||
|
{
|
||||||
|
bd->pVB->Unlock();
|
||||||
|
state_block->Release();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy and convert all vertices into a single contiguous buffer, convert colors to DX9 default format.
|
||||||
|
// FIXME-OPT: This is a minor waste of resource, the ideal is to use imconfig.h and
|
||||||
|
// 1) to avoid repacking colors: #define IMGUI_USE_BGRA_PACKED_COLOR
|
||||||
|
// 2) to avoid repacking vertices: #define IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT struct ImDrawVert { ImVec2 pos; float z; ImU32 col; ImVec2 uv; }
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
const ImDrawVert* vtx_src = draw_list->VtxBuffer.Data;
|
||||||
|
for (int i = 0; i < draw_list->VtxBuffer.Size; i++)
|
||||||
|
{
|
||||||
|
vtx_dst->pos[0] = vtx_src->pos.x;
|
||||||
|
vtx_dst->pos[1] = vtx_src->pos.y;
|
||||||
|
vtx_dst->pos[2] = 0.0f;
|
||||||
|
vtx_dst->col = IMGUI_COL_TO_DX9_ARGB(vtx_src->col);
|
||||||
|
vtx_dst->uv[0] = vtx_src->uv.x;
|
||||||
|
vtx_dst->uv[1] = vtx_src->uv.y;
|
||||||
|
vtx_dst++;
|
||||||
|
vtx_src++;
|
||||||
|
}
|
||||||
|
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
|
||||||
|
idx_dst += draw_list->IdxBuffer.Size;
|
||||||
|
}
|
||||||
|
bd->pVB->Unlock();
|
||||||
|
bd->pIB->Unlock();
|
||||||
|
device->SetStreamSource(0, bd->pVB, 0, sizeof(CUSTOMVERTEX));
|
||||||
|
device->SetIndices(bd->pIB);
|
||||||
|
device->SetFVF(D3DFVF_CUSTOMVERTEX);
|
||||||
|
|
||||||
|
// Setup desired DX state
|
||||||
|
ImGui_ImplDX9_SetupRenderState(draw_data);
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
// (Because we merged all buffers into a single one, we maintain our own offset into them)
|
||||||
|
int global_vtx_offset = 0;
|
||||||
|
int global_idx_offset = 0;
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos;
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback != nullptr)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplDX9_SetupRenderState(draw_data);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y);
|
||||||
|
ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y);
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Apply scissor/clipping rectangle
|
||||||
|
const RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y };
|
||||||
|
device->SetScissorRect(&r);
|
||||||
|
|
||||||
|
// Bind texture, Draw
|
||||||
|
const LPDIRECT3DTEXTURE9 texture = (LPDIRECT3DTEXTURE9)pcmd->GetTexID();
|
||||||
|
device->SetTexture(0, texture);
|
||||||
|
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, pcmd->VtxOffset + global_vtx_offset, 0, (UINT)draw_list->VtxBuffer.Size, pcmd->IdxOffset + global_idx_offset, pcmd->ElemCount / 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
global_idx_offset += draw_list->IdxBuffer.Size;
|
||||||
|
global_vtx_offset += draw_list->VtxBuffer.Size;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore the DX9 transform
|
||||||
|
device->SetTransform(D3DTS_WORLD, &last_world);
|
||||||
|
device->SetTransform(D3DTS_VIEW, &last_view);
|
||||||
|
device->SetTransform(D3DTS_PROJECTION, &last_projection);
|
||||||
|
|
||||||
|
// Restore the DX9 state
|
||||||
|
state_block->Apply();
|
||||||
|
state_block->Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ImGui_ImplDX9_CheckFormatSupport(LPDIRECT3DDEVICE9 pDevice, D3DFORMAT format)
|
||||||
|
{
|
||||||
|
LPDIRECT3D9 pd3d = nullptr;
|
||||||
|
if (pDevice->GetDirect3D(&pd3d) != D3D_OK)
|
||||||
|
return false;
|
||||||
|
D3DDEVICE_CREATION_PARAMETERS param = {};
|
||||||
|
D3DDISPLAYMODE mode = {};
|
||||||
|
if (pDevice->GetCreationParameters(¶m) != D3D_OK || pDevice->GetDisplayMode(0, &mode) != D3D_OK)
|
||||||
|
{
|
||||||
|
pd3d->Release();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Font texture should support linear filter, color blend and write to render-target
|
||||||
|
bool support = (pd3d->CheckDeviceFormat(param.AdapterOrdinal, param.DeviceType, mode.Format, D3DUSAGE_DYNAMIC | D3DUSAGE_QUERY_FILTER | D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, format)) == D3D_OK;
|
||||||
|
pd3d->Release();
|
||||||
|
return support;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplDX9_Init(IDirect3DDevice9* device)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplDX9_Data* bd = IM_NEW(ImGui_ImplDX9_Data)();
|
||||||
|
io.BackendRendererUserData = (void*)bd;
|
||||||
|
io.BackendRendererName = "imgui_impl_dx9";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = 4096;
|
||||||
|
|
||||||
|
bd->pd3dDevice = device;
|
||||||
|
bd->pd3dDevice->AddRef();
|
||||||
|
bd->HasRgbaSupport = ImGui_ImplDX9_CheckFormatSupport(bd->pd3dDevice, D3DFMT_A8B8G8R8);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX9_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
ImGui_ImplDX9_InvalidateDeviceObjects();
|
||||||
|
if (bd->pd3dDevice) { bd->pd3dDevice->Release(); }
|
||||||
|
io.BackendRendererName = nullptr;
|
||||||
|
io.BackendRendererUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert RGBA32 to BGRA32 (because RGBA32 is not well supported by DX9 devices)
|
||||||
|
static void ImGui_ImplDX9_CopyTextureRegion(bool tex_use_colors, const ImU32* src, int src_pitch, ImU32* dst, int dst_pitch, int w, int h)
|
||||||
|
{
|
||||||
|
#ifndef IMGUI_USE_BGRA_PACKED_COLOR
|
||||||
|
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
|
||||||
|
const bool convert_rgba_to_bgra = (!bd->HasRgbaSupport && tex_use_colors);
|
||||||
|
#else
|
||||||
|
const bool convert_rgba_to_bgra = false;
|
||||||
|
IM_UNUSED(tex_use_colors);
|
||||||
|
#endif
|
||||||
|
for (int y = 0; y < h; y++)
|
||||||
|
{
|
||||||
|
const ImU32* src_p = (const ImU32*)(const void*)((const unsigned char*)src + src_pitch * y);
|
||||||
|
ImU32* dst_p = (ImU32*)(void*)((unsigned char*)dst + dst_pitch * y);
|
||||||
|
if (convert_rgba_to_bgra)
|
||||||
|
for (int x = w; x > 0; x--, src_p++, dst_p++) // Convert copy
|
||||||
|
*dst_p = IMGUI_COL_TO_DX9_ARGB(*src_p);
|
||||||
|
else
|
||||||
|
memcpy(dst_p, src_p, w * 4); // Raw copy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX9_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
|
||||||
|
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
LPDIRECT3DTEXTURE9 dx_tex = nullptr;
|
||||||
|
HRESULT hr = bd->pd3dDevice->CreateTexture(tex->Width, tex->Height, 1, D3DUSAGE_DYNAMIC, bd->HasRgbaSupport ? D3DFMT_A8B8G8R8 : D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &dx_tex, nullptr);
|
||||||
|
if (hr < 0)
|
||||||
|
{
|
||||||
|
IM_ASSERT(hr >= 0 && "Backend failed to create texture!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
D3DLOCKED_RECT locked_rect;
|
||||||
|
if (dx_tex->LockRect(0, &locked_rect, nullptr, 0) == D3D_OK)
|
||||||
|
{
|
||||||
|
ImGui_ImplDX9_CopyTextureRegion(tex->UseColors, (ImU32*)tex->GetPixels(), tex->Width * 4, (ImU32*)locked_rect.pBits, (ImU32)locked_rect.Pitch, tex->Width, tex->Height);
|
||||||
|
dx_tex->UnlockRect(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)(intptr_t)dx_tex);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
// Update selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
|
||||||
|
LPDIRECT3DTEXTURE9 backend_tex = (LPDIRECT3DTEXTURE9)(intptr_t)tex->TexID;
|
||||||
|
RECT update_rect = { (LONG)tex->UpdateRect.x, (LONG)tex->UpdateRect.y, (LONG)(tex->UpdateRect.x + tex->UpdateRect.w), (LONG)(tex->UpdateRect.y + tex->UpdateRect.h) };
|
||||||
|
D3DLOCKED_RECT locked_rect;
|
||||||
|
if (backend_tex->LockRect(0, &locked_rect, &update_rect, 0) == D3D_OK)
|
||||||
|
for (ImTextureRect& r : tex->Updates)
|
||||||
|
ImGui_ImplDX9_CopyTextureRegion(tex->UseColors, (ImU32*)tex->GetPixelsAt(r.x, r.y), tex->Width * 4,
|
||||||
|
(ImU32*)locked_rect.pBits + (r.x - update_rect.left) + (r.y - update_rect.top) * (locked_rect.Pitch / 4), (int)locked_rect.Pitch, r.w, r.h);
|
||||||
|
backend_tex->UnlockRect(0);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantDestroy)
|
||||||
|
{
|
||||||
|
LPDIRECT3DTEXTURE9 backend_tex = (LPDIRECT3DTEXTURE9)tex->TexID;
|
||||||
|
if (backend_tex == nullptr)
|
||||||
|
return;
|
||||||
|
IM_ASSERT(tex->TexID == (ImTextureID)(intptr_t)backend_tex);
|
||||||
|
backend_tex->Release();
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplDX9_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
|
||||||
|
if (!bd || !bd->pd3dDevice)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX9_InvalidateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
|
||||||
|
if (!bd || !bd->pd3dDevice)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Destroy all textures
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
{
|
||||||
|
tex->SetStatus(ImTextureStatus_WantDestroy);
|
||||||
|
ImGui_ImplDX9_UpdateTexture(tex);
|
||||||
|
}
|
||||||
|
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
|
||||||
|
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplDX9_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX9_Init()?");
|
||||||
|
IM_UNUSED(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
37
imgui/backends/imgui_impl_dx9.h
Normal file
37
imgui/backends/imgui_impl_dx9.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
// dear imgui: Renderer Backend for DirectX9
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. Win32)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'LPDIRECT3DTEXTURE9' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: IMGUI_USE_BGRA_PACKED_COLOR support, as this is the optimal color encoding for DirectX9.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
struct IDirect3DDevice9;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplDX9_Init(IDirect3DDevice9* device);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX9_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX9_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data);
|
||||||
|
|
||||||
|
// Use if you want to reset your rendering device without losing Dear ImGui state.
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplDX9_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX9_InvalidateDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplDX9_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
1005
imgui/backends/imgui_impl_glfw.cpp
Normal file
1005
imgui/backends/imgui_impl_glfw.cpp
Normal file
File diff suppressed because it is too large
Load Diff
70
imgui/backends/imgui_impl_glfw.h
Normal file
70
imgui/backends/imgui_impl_glfw.h
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
// dear imgui: Platform Backend for GLFW
|
||||||
|
// This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan, WebGPU..)
|
||||||
|
// (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Clipboard support.
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen (Windows only).
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLFW_KEY_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Resizing cursors requires GLFW 3.4+! Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
// [X] Multiple Dear ImGui contexts support.
|
||||||
|
// Missing features or Issues:
|
||||||
|
// [ ] Touch events are only correctly identified as Touch on Windows. This create issues with some interactions. GLFW doesn't provide a way to identify touch inputs from mouse inputs, we cannot call io.AddMouseSourceEvent() to identify the source. We provide a Windows-specific workaround.
|
||||||
|
// [ ] Missing ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress cursors.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
struct GLFWwindow;
|
||||||
|
struct GLFWmonitor;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame();
|
||||||
|
|
||||||
|
// Emscripten related initialization phase methods (call after ImGui_ImplGlfw_InitForOpenGL)
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_InstallEmscriptenCallbacks(GLFWwindow* window, const char* canvas_selector);
|
||||||
|
//static inline void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector) { ImGui_ImplGlfw_InstallEmscriptenCallbacks(nullptr, canvas_selector); } } // Renamed in 1.91.0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// GLFW callbacks install
|
||||||
|
// - When calling Init with 'install_callbacks=true': ImGui_ImplGlfw_InstallCallbacks() is called. GLFW callbacks will be installed for you. They will chain-call user's previously installed callbacks, if any.
|
||||||
|
// - When calling Init with 'install_callbacks=false': GLFW callbacks won't be installed. You will need to call individual function yourself from your own GLFW callbacks.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window);
|
||||||
|
|
||||||
|
// GFLW callbacks options:
|
||||||
|
// - Set 'chain_for_all_windows=true' to enable chaining callbacks for all windows (including secondary viewports created by backends or by user)
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_SetCallbacksChainForAllWindows(bool chain_for_all_windows);
|
||||||
|
|
||||||
|
// GLFW callbacks (individual callbacks to call yourself if you didn't install callbacks)
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused); // Since 1.84
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered); // Since 1.84
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y); // Since 1.87
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor* monitor, int event);
|
||||||
|
|
||||||
|
// GLFW helpers
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGlfw_Sleep(int milliseconds);
|
||||||
|
IMGUI_IMPL_API float ImGui_ImplGlfw_GetContentScaleForWindow(GLFWwindow* window);
|
||||||
|
IMGUI_IMPL_API float ImGui_ImplGlfw_GetContentScaleForMonitor(GLFWmonitor* monitor);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
309
imgui/backends/imgui_impl_glut.cpp
Normal file
309
imgui/backends/imgui_impl_glut.cpp
Normal file
@@ -0,0 +1,309 @@
|
|||||||
|
// dear imgui: Platform Backend for GLUT/FreeGLUT
|
||||||
|
// This needs to be used along with a Renderer (e.g. OpenGL2)
|
||||||
|
|
||||||
|
// !!! GLUT/FreeGLUT IS OBSOLETE PREHISTORIC SOFTWARE. Using GLUT is not recommended unless you really miss the 90's. !!!
|
||||||
|
// !!! If someone or something is teaching you GLUT today, you are being abused. Please show some resistance. !!!
|
||||||
|
// !!! Nowadays, prefer using GLFW or SDL instead!
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Partial keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLUT values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// Missing features or Issues:
|
||||||
|
// [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I
|
||||||
|
// [ ] Platform: Missing horizontal mouse wheel support.
|
||||||
|
// [ ] Platform: Missing mouse cursor shape/visibility support.
|
||||||
|
// [ ] Platform: Missing clipboard support (not supported by Glut).
|
||||||
|
// [ ] Platform: Missing gamepad support.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2023-04-17: BREAKING: Removed call to ImGui::NewFrame() from ImGui_ImplGLUT_NewFrame(). Needs to be called from the main application loop, like with every other backends.
|
||||||
|
// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
|
||||||
|
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
|
||||||
|
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
|
||||||
|
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
|
||||||
|
// 2019-04-03: Misc: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h.
|
||||||
|
// 2019-03-25: Misc: Made io.DeltaTime always above zero.
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
|
||||||
|
// 2018-03-22: Added GLUT Platform binding.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_glut.h"
|
||||||
|
#define GL_SILENCE_DEPRECATION
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#include <GLUT/glut.h>
|
||||||
|
#else
|
||||||
|
#include <GL/freeglut.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int g_Time = 0; // Current time, in milliseconds
|
||||||
|
|
||||||
|
// Glut has one function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above.
|
||||||
|
static ImGuiKey ImGui_ImplGLUT_KeyToImGuiKey(int key)
|
||||||
|
{
|
||||||
|
switch (key)
|
||||||
|
{
|
||||||
|
case '\t': return ImGuiKey_Tab;
|
||||||
|
case 256 + GLUT_KEY_LEFT: return ImGuiKey_LeftArrow;
|
||||||
|
case 256 + GLUT_KEY_RIGHT: return ImGuiKey_RightArrow;
|
||||||
|
case 256 + GLUT_KEY_UP: return ImGuiKey_UpArrow;
|
||||||
|
case 256 + GLUT_KEY_DOWN: return ImGuiKey_DownArrow;
|
||||||
|
case 256 + GLUT_KEY_PAGE_UP: return ImGuiKey_PageUp;
|
||||||
|
case 256 + GLUT_KEY_PAGE_DOWN: return ImGuiKey_PageDown;
|
||||||
|
case 256 + GLUT_KEY_HOME: return ImGuiKey_Home;
|
||||||
|
case 256 + GLUT_KEY_END: return ImGuiKey_End;
|
||||||
|
case 256 + GLUT_KEY_INSERT: return ImGuiKey_Insert;
|
||||||
|
case 127: return ImGuiKey_Delete;
|
||||||
|
case 8: return ImGuiKey_Backspace;
|
||||||
|
case ' ': return ImGuiKey_Space;
|
||||||
|
case 13: return ImGuiKey_Enter;
|
||||||
|
case 27: return ImGuiKey_Escape;
|
||||||
|
case 39: return ImGuiKey_Apostrophe;
|
||||||
|
case 44: return ImGuiKey_Comma;
|
||||||
|
case 45: return ImGuiKey_Minus;
|
||||||
|
case 46: return ImGuiKey_Period;
|
||||||
|
case 47: return ImGuiKey_Slash;
|
||||||
|
case 59: return ImGuiKey_Semicolon;
|
||||||
|
case 61: return ImGuiKey_Equal;
|
||||||
|
case 91: return ImGuiKey_LeftBracket;
|
||||||
|
case 92: return ImGuiKey_Backslash;
|
||||||
|
case 93: return ImGuiKey_RightBracket;
|
||||||
|
case 96: return ImGuiKey_GraveAccent;
|
||||||
|
//case 0: return ImGuiKey_CapsLock;
|
||||||
|
//case 0: return ImGuiKey_ScrollLock;
|
||||||
|
case 256 + 0x006D: return ImGuiKey_NumLock;
|
||||||
|
//case 0: return ImGuiKey_PrintScreen;
|
||||||
|
//case 0: return ImGuiKey_Pause;
|
||||||
|
//case '0': return ImGuiKey_Keypad0;
|
||||||
|
//case '1': return ImGuiKey_Keypad1;
|
||||||
|
//case '2': return ImGuiKey_Keypad2;
|
||||||
|
//case '3': return ImGuiKey_Keypad3;
|
||||||
|
//case '4': return ImGuiKey_Keypad4;
|
||||||
|
//case '5': return ImGuiKey_Keypad5;
|
||||||
|
//case '6': return ImGuiKey_Keypad6;
|
||||||
|
//case '7': return ImGuiKey_Keypad7;
|
||||||
|
//case '8': return ImGuiKey_Keypad8;
|
||||||
|
//case '9': return ImGuiKey_Keypad9;
|
||||||
|
//case 46: return ImGuiKey_KeypadDecimal;
|
||||||
|
//case 47: return ImGuiKey_KeypadDivide;
|
||||||
|
case 42: return ImGuiKey_KeypadMultiply;
|
||||||
|
//case 45: return ImGuiKey_KeypadSubtract;
|
||||||
|
case 43: return ImGuiKey_KeypadAdd;
|
||||||
|
//case 13: return ImGuiKey_KeypadEnter;
|
||||||
|
//case 0: return ImGuiKey_KeypadEqual;
|
||||||
|
case 256 + 0x0072: return ImGuiKey_LeftCtrl;
|
||||||
|
case 256 + 0x0070: return ImGuiKey_LeftShift;
|
||||||
|
case 256 + 0x0074: return ImGuiKey_LeftAlt;
|
||||||
|
//case 0: return ImGuiKey_LeftSuper;
|
||||||
|
case 256 + 0x0073: return ImGuiKey_RightCtrl;
|
||||||
|
case 256 + 0x0071: return ImGuiKey_RightShift;
|
||||||
|
case 256 + 0x0075: return ImGuiKey_RightAlt;
|
||||||
|
//case 0: return ImGuiKey_RightSuper;
|
||||||
|
//case 0: return ImGuiKey_Menu;
|
||||||
|
case '0': return ImGuiKey_0;
|
||||||
|
case '1': return ImGuiKey_1;
|
||||||
|
case '2': return ImGuiKey_2;
|
||||||
|
case '3': return ImGuiKey_3;
|
||||||
|
case '4': return ImGuiKey_4;
|
||||||
|
case '5': return ImGuiKey_5;
|
||||||
|
case '6': return ImGuiKey_6;
|
||||||
|
case '7': return ImGuiKey_7;
|
||||||
|
case '8': return ImGuiKey_8;
|
||||||
|
case '9': return ImGuiKey_9;
|
||||||
|
case 'A': case 'a': return ImGuiKey_A;
|
||||||
|
case 'B': case 'b': return ImGuiKey_B;
|
||||||
|
case 'C': case 'c': return ImGuiKey_C;
|
||||||
|
case 'D': case 'd': return ImGuiKey_D;
|
||||||
|
case 'E': case 'e': return ImGuiKey_E;
|
||||||
|
case 'F': case 'f': return ImGuiKey_F;
|
||||||
|
case 'G': case 'g': return ImGuiKey_G;
|
||||||
|
case 'H': case 'h': return ImGuiKey_H;
|
||||||
|
case 'I': case 'i': return ImGuiKey_I;
|
||||||
|
case 'J': case 'j': return ImGuiKey_J;
|
||||||
|
case 'K': case 'k': return ImGuiKey_K;
|
||||||
|
case 'L': case 'l': return ImGuiKey_L;
|
||||||
|
case 'M': case 'm': return ImGuiKey_M;
|
||||||
|
case 'N': case 'n': return ImGuiKey_N;
|
||||||
|
case 'O': case 'o': return ImGuiKey_O;
|
||||||
|
case 'P': case 'p': return ImGuiKey_P;
|
||||||
|
case 'Q': case 'q': return ImGuiKey_Q;
|
||||||
|
case 'R': case 'r': return ImGuiKey_R;
|
||||||
|
case 'S': case 's': return ImGuiKey_S;
|
||||||
|
case 'T': case 't': return ImGuiKey_T;
|
||||||
|
case 'U': case 'u': return ImGuiKey_U;
|
||||||
|
case 'V': case 'v': return ImGuiKey_V;
|
||||||
|
case 'W': case 'w': return ImGuiKey_W;
|
||||||
|
case 'X': case 'x': return ImGuiKey_X;
|
||||||
|
case 'Y': case 'y': return ImGuiKey_Y;
|
||||||
|
case 'Z': case 'z': return ImGuiKey_Z;
|
||||||
|
case 256 + GLUT_KEY_F1: return ImGuiKey_F1;
|
||||||
|
case 256 + GLUT_KEY_F2: return ImGuiKey_F2;
|
||||||
|
case 256 + GLUT_KEY_F3: return ImGuiKey_F3;
|
||||||
|
case 256 + GLUT_KEY_F4: return ImGuiKey_F4;
|
||||||
|
case 256 + GLUT_KEY_F5: return ImGuiKey_F5;
|
||||||
|
case 256 + GLUT_KEY_F6: return ImGuiKey_F6;
|
||||||
|
case 256 + GLUT_KEY_F7: return ImGuiKey_F7;
|
||||||
|
case 256 + GLUT_KEY_F8: return ImGuiKey_F8;
|
||||||
|
case 256 + GLUT_KEY_F9: return ImGuiKey_F9;
|
||||||
|
case 256 + GLUT_KEY_F10: return ImGuiKey_F10;
|
||||||
|
case 256 + GLUT_KEY_F11: return ImGuiKey_F11;
|
||||||
|
case 256 + GLUT_KEY_F12: return ImGuiKey_F12;
|
||||||
|
default: return ImGuiKey_None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplGLUT_Init()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
|
||||||
|
#ifdef FREEGLUT
|
||||||
|
io.BackendPlatformName = "imgui_impl_glut (freeglut)";
|
||||||
|
#else
|
||||||
|
io.BackendPlatformName = "imgui_impl_glut";
|
||||||
|
#endif
|
||||||
|
g_Time = 0;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplGLUT_InstallFuncs()
|
||||||
|
{
|
||||||
|
glutReshapeFunc(ImGui_ImplGLUT_ReshapeFunc);
|
||||||
|
glutMotionFunc(ImGui_ImplGLUT_MotionFunc);
|
||||||
|
glutPassiveMotionFunc(ImGui_ImplGLUT_MotionFunc);
|
||||||
|
glutMouseFunc(ImGui_ImplGLUT_MouseFunc);
|
||||||
|
#ifdef __FREEGLUT_EXT_H__
|
||||||
|
glutMouseWheelFunc(ImGui_ImplGLUT_MouseWheelFunc);
|
||||||
|
#endif
|
||||||
|
glutKeyboardFunc(ImGui_ImplGLUT_KeyboardFunc);
|
||||||
|
glutKeyboardUpFunc(ImGui_ImplGLUT_KeyboardUpFunc);
|
||||||
|
glutSpecialFunc(ImGui_ImplGLUT_SpecialFunc);
|
||||||
|
glutSpecialUpFunc(ImGui_ImplGLUT_SpecialUpFunc);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplGLUT_Shutdown()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.BackendPlatformName = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplGLUT_NewFrame()
|
||||||
|
{
|
||||||
|
// Setup time step
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
int current_time = glutGet(GLUT_ELAPSED_TIME);
|
||||||
|
int delta_time_ms = (current_time - g_Time);
|
||||||
|
if (delta_time_ms <= 0)
|
||||||
|
delta_time_ms = 1;
|
||||||
|
io.DeltaTime = delta_time_ms / 1000.0f;
|
||||||
|
g_Time = current_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplGLUT_UpdateKeyModifiers()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
int glut_key_mods = glutGetModifiers();
|
||||||
|
io.AddKeyEvent(ImGuiMod_Ctrl, (glut_key_mods & GLUT_ACTIVE_CTRL) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Shift, (glut_key_mods & GLUT_ACTIVE_SHIFT) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Alt, (glut_key_mods & GLUT_ACTIVE_ALT) != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplGLUT_AddKeyEvent(ImGuiKey key, bool down, int native_keycode)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.AddKeyEvent(key, down);
|
||||||
|
io.SetKeyEventNativeData(key, native_keycode, -1); // To support legacy indexing (<1.87 user code)
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y)
|
||||||
|
{
|
||||||
|
// Send character to imgui
|
||||||
|
//printf("char_down_func %d '%c'\n", c, c);
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
if (c >= 32)
|
||||||
|
io.AddInputCharacter((unsigned int)c);
|
||||||
|
|
||||||
|
ImGuiKey key = ImGui_ImplGLUT_KeyToImGuiKey(c);
|
||||||
|
ImGui_ImplGLUT_AddKeyEvent(key, true, c);
|
||||||
|
ImGui_ImplGLUT_UpdateKeyModifiers();
|
||||||
|
(void)x; (void)y; // Unused
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
|
||||||
|
{
|
||||||
|
//printf("char_up_func %d '%c'\n", c, c);
|
||||||
|
ImGuiKey key = ImGui_ImplGLUT_KeyToImGuiKey(c);
|
||||||
|
ImGui_ImplGLUT_AddKeyEvent(key, false, c);
|
||||||
|
ImGui_ImplGLUT_UpdateKeyModifiers();
|
||||||
|
(void)x; (void)y; // Unused
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y)
|
||||||
|
{
|
||||||
|
//printf("key_down_func %d\n", key);
|
||||||
|
ImGuiKey imgui_key = ImGui_ImplGLUT_KeyToImGuiKey(key + 256);
|
||||||
|
ImGui_ImplGLUT_AddKeyEvent(imgui_key, true, key + 256);
|
||||||
|
ImGui_ImplGLUT_UpdateKeyModifiers();
|
||||||
|
(void)x; (void)y; // Unused
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y)
|
||||||
|
{
|
||||||
|
//printf("key_up_func %d\n", key);
|
||||||
|
ImGuiKey imgui_key = ImGui_ImplGLUT_KeyToImGuiKey(key + 256);
|
||||||
|
ImGui_ImplGLUT_AddKeyEvent(imgui_key, false, key + 256);
|
||||||
|
ImGui_ImplGLUT_UpdateKeyModifiers();
|
||||||
|
(void)x; (void)y; // Unused
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplGLUT_MouseFunc(int glut_button, int state, int x, int y)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.AddMousePosEvent((float)x, (float)y);
|
||||||
|
int button = -1;
|
||||||
|
if (glut_button == GLUT_LEFT_BUTTON) button = 0;
|
||||||
|
if (glut_button == GLUT_RIGHT_BUTTON) button = 1;
|
||||||
|
if (glut_button == GLUT_MIDDLE_BUTTON) button = 2;
|
||||||
|
if (button != -1 && (state == GLUT_DOWN || state == GLUT_UP))
|
||||||
|
io.AddMouseButtonEvent(button, state == GLUT_DOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __FREEGLUT_EXT_H__
|
||||||
|
void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.AddMousePosEvent((float)x, (float)y);
|
||||||
|
if (dir != 0)
|
||||||
|
io.AddMouseWheelEvent(0.0f, dir > 0 ? 1.0f : -1.0f);
|
||||||
|
(void)button; // Unused
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void ImGui_ImplGLUT_ReshapeFunc(int w, int h)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.DisplaySize = ImVec2((float)w, (float)h);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplGLUT_MotionFunc(int x, int y)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.AddMousePosEvent((float)x, (float)y);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
47
imgui/backends/imgui_impl_glut.h
Normal file
47
imgui/backends/imgui_impl_glut.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
// dear imgui: Platform Backend for GLUT/FreeGLUT
|
||||||
|
// This needs to be used along with a Renderer (e.g. OpenGL2)
|
||||||
|
|
||||||
|
// !!! GLUT/FreeGLUT IS OBSOLETE PREHISTORIC SOFTWARE. Using GLUT is not recommended unless you really miss the 90's. !!!
|
||||||
|
// !!! If someone or something is teaching you GLUT today, you are being abused. Please show some resistance. !!!
|
||||||
|
// !!! Nowadays, prefer using GLFW or SDL instead!
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Partial keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLUT values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// Missing features or Issues:
|
||||||
|
// [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I
|
||||||
|
// [ ] Platform: Missing horizontal mouse wheel support.
|
||||||
|
// [ ] Platform: Missing mouse cursor shape/visibility support.
|
||||||
|
// [ ] Platform: Missing clipboard support (not supported by Glut).
|
||||||
|
// [ ] Platform: Missing gamepad support.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplGLUT_Init();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_InstallFuncs();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_NewFrame();
|
||||||
|
|
||||||
|
// You can call ImGui_ImplGLUT_InstallFuncs() to get all those functions installed automatically,
|
||||||
|
// or call them yourself from your own GLUT handlers. We are using the same weird names as GLUT for consistency..
|
||||||
|
//------------------------------------ GLUT name ---------------------------------------------- Decent Name ---------
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_ReshapeFunc(int w, int h); // ~ ResizeFunc
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_MotionFunc(int x, int y); // ~ MouseMoveFunc
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_MouseFunc(int button, int state, int x, int y); // ~ MouseButtonFunc
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y); // ~ MouseWheelFunc
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y); // ~ CharPressedFunc
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y); // ~ CharReleasedFunc
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y); // ~ KeyPressedFunc
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y); // ~ KeyReleasedFunc
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
78
imgui/backends/imgui_impl_metal.h
Normal file
78
imgui/backends/imgui_impl_metal.h
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
// dear imgui: Renderer Backend for Metal
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. OSX)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'MTLTexture' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// ObjC API
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifdef __OBJC__
|
||||||
|
|
||||||
|
@class MTLRenderPassDescriptor;
|
||||||
|
@protocol MTLDevice, MTLCommandBuffer, MTLRenderCommandEncoder;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplMetal_Init(id<MTLDevice> device);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplMetal_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData,
|
||||||
|
id<MTLCommandBuffer> commandBuffer,
|
||||||
|
id<MTLRenderCommandEncoder> commandEncoder);
|
||||||
|
|
||||||
|
// Called by Init/NewFrame/Shutdown
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplMetal_CreateDeviceObjects(id<MTLDevice> device);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplMetal_DestroyDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplMetal_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// C++ API
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Enable Metal C++ binding support with '#define IMGUI_IMPL_METAL_CPP' in your imconfig.h file
|
||||||
|
// More info about using Metal from C++: https://developer.apple.com/metal/cpp/
|
||||||
|
|
||||||
|
#ifdef IMGUI_IMPL_METAL_CPP
|
||||||
|
#include <Metal/Metal.hpp>
|
||||||
|
#ifndef __OBJC__
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplMetal_Init(MTL::Device* device);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplMetal_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplMetal_NewFrame(MTL::RenderPassDescriptor* renderPassDescriptor);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplMetal_RenderDrawData(ImDrawData* draw_data,
|
||||||
|
MTL::CommandBuffer* commandBuffer,
|
||||||
|
MTL::RenderCommandEncoder* commandEncoder);
|
||||||
|
|
||||||
|
// Called by Init/NewFrame/Shutdown
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplMetal_CreateDeviceObjects(MTL::Device* device);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplMetal_DestroyDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplMetal_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
652
imgui/backends/imgui_impl_metal.mm
Normal file
652
imgui/backends/imgui_impl_metal.mm
Normal file
@@ -0,0 +1,652 @@
|
|||||||
|
// dear imgui: Renderer Backend for Metal
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. OSX)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'MTLTexture' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplMetal_CreateFontsTexture() and ImGui_ImplMetal_DestroyFontsTexture().
|
||||||
|
// 2025-02-03: Metal: Crash fix. (#8367)
|
||||||
|
// 2024-01-08: Metal: Fixed memory leaks when using metal-cpp (#8276, #8166) or when using multiple contexts (#7419).
|
||||||
|
// 2022-08-23: Metal: Update deprecated property 'sampleCount'->'rasterSampleCount'.
|
||||||
|
// 2022-07-05: Metal: Add dispatch synchronization.
|
||||||
|
// 2022-06-30: Metal: Use __bridge for ARC based systems.
|
||||||
|
// 2022-06-01: Metal: Fixed null dereference on exit inside command buffer completion handler.
|
||||||
|
// 2022-04-27: Misc: Store backend data in a per-context struct, allowing to use this backend with multiple contexts.
|
||||||
|
// 2022-01-03: Metal: Ignore ImDrawCmd where ElemCount == 0 (very rare but can technically be manufactured by user code).
|
||||||
|
// 2021-12-30: Metal: Added Metal C++ support. Enable with '#define IMGUI_IMPL_METAL_CPP' in your imconfig.h file.
|
||||||
|
// 2021-08-24: Metal: Fixed a crash when clipping rect larger than framebuffer is submitted. (#4464)
|
||||||
|
// 2021-05-19: Metal: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
|
||||||
|
// 2021-02-18: Metal: Change blending equation to preserve alpha in output buffer.
|
||||||
|
// 2021-01-25: Metal: Fixed texture storage mode when building on Mac Catalyst.
|
||||||
|
// 2019-05-29: Metal: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
|
||||||
|
// 2019-04-30: Metal: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
|
||||||
|
// 2019-02-11: Metal: Projecting clipping rectangles correctly using draw_data->FramebufferScale to allow multi-viewports for retina display.
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
|
||||||
|
// 2018-07-05: Metal: Added new Metal backend implementation.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_metal.h"
|
||||||
|
#import <time.h>
|
||||||
|
#import <Metal/Metal.h>
|
||||||
|
|
||||||
|
#pragma mark - Support classes
|
||||||
|
|
||||||
|
// A wrapper around a MTLBuffer object that knows the last time it was reused
|
||||||
|
@interface MetalBuffer : NSObject
|
||||||
|
@property (nonatomic, strong) id<MTLBuffer> buffer;
|
||||||
|
@property (nonatomic, assign) double lastReuseTime;
|
||||||
|
- (instancetype)initWithBuffer:(id<MTLBuffer>)buffer;
|
||||||
|
@end
|
||||||
|
|
||||||
|
// An object that encapsulates the data necessary to uniquely identify a
|
||||||
|
// render pipeline state. These are used as cache keys.
|
||||||
|
@interface FramebufferDescriptor : NSObject<NSCopying>
|
||||||
|
@property (nonatomic, assign) unsigned long sampleCount;
|
||||||
|
@property (nonatomic, assign) MTLPixelFormat colorPixelFormat;
|
||||||
|
@property (nonatomic, assign) MTLPixelFormat depthPixelFormat;
|
||||||
|
@property (nonatomic, assign) MTLPixelFormat stencilPixelFormat;
|
||||||
|
- (instancetype)initWithRenderPassDescriptor:(MTLRenderPassDescriptor*)renderPassDescriptor;
|
||||||
|
@end
|
||||||
|
|
||||||
|
@interface MetalTexture : NSObject
|
||||||
|
@property (nonatomic, strong) id<MTLTexture> metalTexture;
|
||||||
|
- (instancetype)initWithTexture:(id<MTLTexture>)metalTexture;
|
||||||
|
@end
|
||||||
|
|
||||||
|
// A singleton that stores long-lived objects that are needed by the Metal
|
||||||
|
// renderer backend. Stores the render pipeline state cache and the default
|
||||||
|
// font texture, and manages the reusable buffer cache.
|
||||||
|
@interface MetalContext : NSObject
|
||||||
|
@property (nonatomic, strong) id<MTLDevice> device;
|
||||||
|
@property (nonatomic, strong) id<MTLDepthStencilState> depthStencilState;
|
||||||
|
@property (nonatomic, strong) FramebufferDescriptor* framebufferDescriptor; // framebuffer descriptor for current frame; transient
|
||||||
|
@property (nonatomic, strong) NSMutableDictionary* renderPipelineStateCache; // pipeline cache; keyed on framebuffer descriptors
|
||||||
|
@property (nonatomic, strong) NSMutableArray<MetalBuffer*>* bufferCache;
|
||||||
|
@property (nonatomic, assign) double lastBufferCachePurge;
|
||||||
|
- (MetalBuffer*)dequeueReusableBufferOfLength:(NSUInteger)length device:(id<MTLDevice>)device;
|
||||||
|
- (id<MTLRenderPipelineState>)renderPipelineStateForFramebufferDescriptor:(FramebufferDescriptor*)descriptor device:(id<MTLDevice>)device;
|
||||||
|
@end
|
||||||
|
|
||||||
|
struct ImGui_ImplMetal_Data
|
||||||
|
{
|
||||||
|
MetalContext* SharedMetalContext;
|
||||||
|
|
||||||
|
ImGui_ImplMetal_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
static ImGui_ImplMetal_Data* ImGui_ImplMetal_GetBackendData() { return ImGui::GetCurrentContext() ? (ImGui_ImplMetal_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; }
|
||||||
|
static void ImGui_ImplMetal_DestroyBackendData(){ IM_DELETE(ImGui_ImplMetal_GetBackendData()); }
|
||||||
|
|
||||||
|
static inline CFTimeInterval GetMachAbsoluteTimeInSeconds() { return (CFTimeInterval)(double)(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1e9); }
|
||||||
|
|
||||||
|
#ifdef IMGUI_IMPL_METAL_CPP
|
||||||
|
|
||||||
|
#pragma mark - Dear ImGui Metal C++ Backend API
|
||||||
|
|
||||||
|
bool ImGui_ImplMetal_Init(MTL::Device* device)
|
||||||
|
{
|
||||||
|
return ImGui_ImplMetal_Init((__bridge id<MTLDevice>)(device));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplMetal_NewFrame(MTL::RenderPassDescriptor* renderPassDescriptor)
|
||||||
|
{
|
||||||
|
ImGui_ImplMetal_NewFrame((__bridge MTLRenderPassDescriptor*)(renderPassDescriptor));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplMetal_RenderDrawData(ImDrawData* draw_data,
|
||||||
|
MTL::CommandBuffer* commandBuffer,
|
||||||
|
MTL::RenderCommandEncoder* commandEncoder)
|
||||||
|
{
|
||||||
|
ImGui_ImplMetal_RenderDrawData(draw_data,
|
||||||
|
(__bridge id<MTLCommandBuffer>)(commandBuffer),
|
||||||
|
(__bridge id<MTLRenderCommandEncoder>)(commandEncoder));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplMetal_CreateDeviceObjects(MTL::Device* device)
|
||||||
|
{
|
||||||
|
return ImGui_ImplMetal_CreateDeviceObjects((__bridge id<MTLDevice>)(device));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // #ifdef IMGUI_IMPL_METAL_CPP
|
||||||
|
|
||||||
|
#pragma mark - Dear ImGui Metal Backend API
|
||||||
|
|
||||||
|
bool ImGui_ImplMetal_Init(id<MTLDevice> device)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
||||||
|
|
||||||
|
ImGui_ImplMetal_Data* bd = IM_NEW(ImGui_ImplMetal_Data)();
|
||||||
|
io.BackendRendererUserData = (void*)bd;
|
||||||
|
io.BackendRendererName = "imgui_impl_metal";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
bd->SharedMetalContext = [[MetalContext alloc] init];
|
||||||
|
bd->SharedMetalContext.device = device;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplMetal_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
|
||||||
|
IM_UNUSED(bd);
|
||||||
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
||||||
|
ImGui_ImplMetal_DestroyDeviceObjects();
|
||||||
|
ImGui_ImplMetal_DestroyBackendData();
|
||||||
|
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.BackendRendererName = nullptr;
|
||||||
|
io.BackendRendererUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor)
|
||||||
|
{
|
||||||
|
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nil && "Context or backend not initialized! Did you call ImGui_ImplMetal_Init()?");
|
||||||
|
#ifdef IMGUI_IMPL_METAL_CPP
|
||||||
|
bd->SharedMetalContext.framebufferDescriptor = [[[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]autorelease];
|
||||||
|
#else
|
||||||
|
bd->SharedMetalContext.framebufferDescriptor = [[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor];
|
||||||
|
#endif
|
||||||
|
if (bd->SharedMetalContext.depthStencilState == nil)
|
||||||
|
ImGui_ImplMetal_CreateDeviceObjects(bd->SharedMetalContext.device);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplMetal_SetupRenderState(ImDrawData* draw_data, id<MTLCommandBuffer> commandBuffer,
|
||||||
|
id<MTLRenderCommandEncoder> commandEncoder, id<MTLRenderPipelineState> renderPipelineState,
|
||||||
|
MetalBuffer* vertexBuffer, size_t vertexBufferOffset)
|
||||||
|
{
|
||||||
|
IM_UNUSED(commandBuffer);
|
||||||
|
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
|
||||||
|
[commandEncoder setCullMode:MTLCullModeNone];
|
||||||
|
[commandEncoder setDepthStencilState:bd->SharedMetalContext.depthStencilState];
|
||||||
|
|
||||||
|
// Setup viewport, orthographic projection matrix
|
||||||
|
// Our visible imgui space lies from draw_data->DisplayPos (top left) to
|
||||||
|
// draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is typically (0,0) for single viewport apps.
|
||||||
|
MTLViewport viewport =
|
||||||
|
{
|
||||||
|
.originX = 0.0,
|
||||||
|
.originY = 0.0,
|
||||||
|
.width = (double)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x),
|
||||||
|
.height = (double)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y),
|
||||||
|
.znear = 0.0,
|
||||||
|
.zfar = 1.0
|
||||||
|
};
|
||||||
|
[commandEncoder setViewport:viewport];
|
||||||
|
|
||||||
|
float L = draw_data->DisplayPos.x;
|
||||||
|
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
||||||
|
float T = draw_data->DisplayPos.y;
|
||||||
|
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
||||||
|
float N = (float)viewport.znear;
|
||||||
|
float F = (float)viewport.zfar;
|
||||||
|
const float ortho_projection[4][4] =
|
||||||
|
{
|
||||||
|
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, 1/(F-N), 0.0f },
|
||||||
|
{ (R+L)/(L-R), (T+B)/(B-T), N/(F-N), 1.0f },
|
||||||
|
};
|
||||||
|
[commandEncoder setVertexBytes:&ortho_projection length:sizeof(ortho_projection) atIndex:1];
|
||||||
|
|
||||||
|
[commandEncoder setRenderPipelineState:renderPipelineState];
|
||||||
|
|
||||||
|
[commandEncoder setVertexBuffer:vertexBuffer.buffer offset:0 atIndex:0];
|
||||||
|
[commandEncoder setVertexBufferOffset:vertexBufferOffset atIndex:0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Metal Render function.
|
||||||
|
void ImGui_ImplMetal_RenderDrawData(ImDrawData* draw_data, id<MTLCommandBuffer> commandBuffer, id<MTLRenderCommandEncoder> commandEncoder)
|
||||||
|
{
|
||||||
|
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
|
||||||
|
MetalContext* ctx = bd->SharedMetalContext;
|
||||||
|
|
||||||
|
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
|
||||||
|
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
|
||||||
|
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
|
||||||
|
if (fb_width <= 0 || fb_height <= 0 || draw_data->CmdListsCount == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplMetal_UpdateTexture(tex);
|
||||||
|
|
||||||
|
// Try to retrieve a render pipeline state that is compatible with the framebuffer config for this frame
|
||||||
|
// The hit rate for this cache should be very near 100%.
|
||||||
|
id<MTLRenderPipelineState> renderPipelineState = ctx.renderPipelineStateCache[ctx.framebufferDescriptor];
|
||||||
|
if (renderPipelineState == nil)
|
||||||
|
{
|
||||||
|
// No luck; make a new render pipeline state
|
||||||
|
renderPipelineState = [ctx renderPipelineStateForFramebufferDescriptor:ctx.framebufferDescriptor device:commandBuffer.device];
|
||||||
|
|
||||||
|
// Cache render pipeline state for later reuse
|
||||||
|
ctx.renderPipelineStateCache[ctx.framebufferDescriptor] = renderPipelineState;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t vertexBufferLength = (size_t)draw_data->TotalVtxCount * sizeof(ImDrawVert);
|
||||||
|
size_t indexBufferLength = (size_t)draw_data->TotalIdxCount * sizeof(ImDrawIdx);
|
||||||
|
MetalBuffer* vertexBuffer = [ctx dequeueReusableBufferOfLength:vertexBufferLength device:commandBuffer.device];
|
||||||
|
MetalBuffer* indexBuffer = [ctx dequeueReusableBufferOfLength:indexBufferLength device:commandBuffer.device];
|
||||||
|
|
||||||
|
ImGui_ImplMetal_SetupRenderState(draw_data, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, 0);
|
||||||
|
|
||||||
|
// Will project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
|
||||||
|
ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
size_t vertexBufferOffset = 0;
|
||||||
|
size_t indexBufferOffset = 0;
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
|
||||||
|
memcpy((char*)vertexBuffer.buffer.contents + vertexBufferOffset, draw_list->VtxBuffer.Data, (size_t)draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
|
||||||
|
memcpy((char*)indexBuffer.buffer.contents + indexBufferOffset, draw_list->IdxBuffer.Data, (size_t)draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
|
||||||
|
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplMetal_SetupRenderState(draw_data, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, vertexBufferOffset);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
|
||||||
|
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
|
||||||
|
|
||||||
|
// Clamp to viewport as setScissorRect() won't accept values that are off bounds
|
||||||
|
if (clip_min.x < 0.0f) { clip_min.x = 0.0f; }
|
||||||
|
if (clip_min.y < 0.0f) { clip_min.y = 0.0f; }
|
||||||
|
if (clip_max.x > fb_width) { clip_max.x = (float)fb_width; }
|
||||||
|
if (clip_max.y > fb_height) { clip_max.y = (float)fb_height; }
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
if (pcmd->ElemCount == 0) // drawIndexedPrimitives() validation doesn't accept this
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Apply scissor/clipping rectangle
|
||||||
|
MTLScissorRect scissorRect =
|
||||||
|
{
|
||||||
|
.x = NSUInteger(clip_min.x),
|
||||||
|
.y = NSUInteger(clip_min.y),
|
||||||
|
.width = NSUInteger(clip_max.x - clip_min.x),
|
||||||
|
.height = NSUInteger(clip_max.y - clip_min.y)
|
||||||
|
};
|
||||||
|
[commandEncoder setScissorRect:scissorRect];
|
||||||
|
|
||||||
|
// Bind texture, Draw
|
||||||
|
if (ImTextureID tex_id = pcmd->GetTexID())
|
||||||
|
[commandEncoder setFragmentTexture:(__bridge id<MTLTexture>)(void*)(intptr_t)(tex_id) atIndex:0];
|
||||||
|
|
||||||
|
[commandEncoder setVertexBufferOffset:(vertexBufferOffset + pcmd->VtxOffset * sizeof(ImDrawVert)) atIndex:0];
|
||||||
|
[commandEncoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle
|
||||||
|
indexCount:pcmd->ElemCount
|
||||||
|
indexType:sizeof(ImDrawIdx) == 2 ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32
|
||||||
|
indexBuffer:indexBuffer.buffer
|
||||||
|
indexBufferOffset:indexBufferOffset + pcmd->IdxOffset * sizeof(ImDrawIdx)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vertexBufferOffset += (size_t)draw_list->VtxBuffer.Size * sizeof(ImDrawVert);
|
||||||
|
indexBufferOffset += (size_t)draw_list->IdxBuffer.Size * sizeof(ImDrawIdx);
|
||||||
|
}
|
||||||
|
|
||||||
|
MetalContext* sharedMetalContext = bd->SharedMetalContext;
|
||||||
|
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer>)
|
||||||
|
{
|
||||||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
|
@synchronized(sharedMetalContext.bufferCache)
|
||||||
|
{
|
||||||
|
[sharedMetalContext.bufferCache addObject:vertexBuffer];
|
||||||
|
[sharedMetalContext.bufferCache addObject:indexBuffer];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplMetal_DestroyTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
MetalTexture* backend_tex = (__bridge_transfer MetalTexture*)(tex->BackendUserData);
|
||||||
|
if (backend_tex == nullptr)
|
||||||
|
return;
|
||||||
|
IM_ASSERT(backend_tex.metalTexture == (__bridge id<MTLTexture>)(void*)(intptr_t)tex->TexID);
|
||||||
|
backend_tex.metalTexture = nil;
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
tex->BackendUserData = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplMetal_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
|
||||||
|
// We are retrieving and uploading the font atlas as a 4-channels RGBA texture here.
|
||||||
|
// In theory we could call GetTexDataAsAlpha8() and upload a 1-channel texture to save on memory access bandwidth.
|
||||||
|
// However, using a shader designed for 1-channel texture would make it less obvious to use the ImTextureID facility to render users own textures.
|
||||||
|
// You can make that change in your implementation.
|
||||||
|
MTLTextureDescriptor* textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
|
||||||
|
width:(NSUInteger)tex->Width
|
||||||
|
height:(NSUInteger)tex->Height
|
||||||
|
mipmapped:NO];
|
||||||
|
textureDescriptor.usage = MTLTextureUsageShaderRead;
|
||||||
|
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
|
||||||
|
textureDescriptor.storageMode = MTLStorageModeManaged;
|
||||||
|
#else
|
||||||
|
textureDescriptor.storageMode = MTLStorageModeShared;
|
||||||
|
#endif
|
||||||
|
id <MTLTexture> texture = [bd->SharedMetalContext.device newTextureWithDescriptor:textureDescriptor];
|
||||||
|
[texture replaceRegion:MTLRegionMake2D(0, 0, (NSUInteger)tex->Width, (NSUInteger)tex->Height) mipmapLevel:0 withBytes:tex->Pixels bytesPerRow:(NSUInteger)tex->Width * 4];
|
||||||
|
MetalTexture* backend_tex = [[MetalTexture alloc] initWithTexture:texture];
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)(intptr_t)texture);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
tex->BackendUserData = (__bridge_retained void*)(backend_tex);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
// Update selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
|
||||||
|
MetalTexture* backend_tex = (__bridge MetalTexture*)(tex->BackendUserData);
|
||||||
|
for (ImTextureRect& r : tex->Updates)
|
||||||
|
{
|
||||||
|
[backend_tex.metalTexture replaceRegion:MTLRegionMake2D((NSUInteger)r.x, (NSUInteger)r.y, (NSUInteger)r.w, (NSUInteger)r.h)
|
||||||
|
mipmapLevel:0
|
||||||
|
withBytes:tex->GetPixelsAt(r.x, r.y)
|
||||||
|
bytesPerRow:(NSUInteger)tex->Width * 4];
|
||||||
|
}
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
|
||||||
|
{
|
||||||
|
ImGui_ImplMetal_DestroyTexture(tex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplMetal_CreateDeviceObjects(id<MTLDevice> device)
|
||||||
|
{
|
||||||
|
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
|
||||||
|
MTLDepthStencilDescriptor* depthStencilDescriptor = [[MTLDepthStencilDescriptor alloc] init];
|
||||||
|
depthStencilDescriptor.depthWriteEnabled = NO;
|
||||||
|
depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways;
|
||||||
|
bd->SharedMetalContext.depthStencilState = [device newDepthStencilStateWithDescriptor:depthStencilDescriptor];
|
||||||
|
#ifdef IMGUI_IMPL_METAL_CPP
|
||||||
|
[depthStencilDescriptor release];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplMetal_DestroyDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
|
||||||
|
|
||||||
|
// Destroy all textures
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
ImGui_ImplMetal_DestroyTexture(tex);
|
||||||
|
|
||||||
|
[bd->SharedMetalContext.renderPipelineStateCache removeAllObjects];
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - MetalBuffer implementation
|
||||||
|
|
||||||
|
@implementation MetalBuffer
|
||||||
|
- (instancetype)initWithBuffer:(id<MTLBuffer>)buffer
|
||||||
|
{
|
||||||
|
if ((self = [super init]))
|
||||||
|
{
|
||||||
|
_buffer = buffer;
|
||||||
|
_lastReuseTime = GetMachAbsoluteTimeInSeconds();
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
@end
|
||||||
|
|
||||||
|
#pragma mark - FramebufferDescriptor implementation
|
||||||
|
|
||||||
|
@implementation FramebufferDescriptor
|
||||||
|
- (instancetype)initWithRenderPassDescriptor:(MTLRenderPassDescriptor*)renderPassDescriptor
|
||||||
|
{
|
||||||
|
if ((self = [super init]))
|
||||||
|
{
|
||||||
|
_sampleCount = renderPassDescriptor.colorAttachments[0].texture.sampleCount;
|
||||||
|
_colorPixelFormat = renderPassDescriptor.colorAttachments[0].texture.pixelFormat;
|
||||||
|
_depthPixelFormat = renderPassDescriptor.depthAttachment.texture.pixelFormat;
|
||||||
|
_stencilPixelFormat = renderPassDescriptor.stencilAttachment.texture.pixelFormat;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (nonnull id)copyWithZone:(nullable NSZone*)zone
|
||||||
|
{
|
||||||
|
FramebufferDescriptor* copy = [[FramebufferDescriptor allocWithZone:zone] init];
|
||||||
|
copy.sampleCount = self.sampleCount;
|
||||||
|
copy.colorPixelFormat = self.colorPixelFormat;
|
||||||
|
copy.depthPixelFormat = self.depthPixelFormat;
|
||||||
|
copy.stencilPixelFormat = self.stencilPixelFormat;
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSUInteger)hash
|
||||||
|
{
|
||||||
|
NSUInteger sc = _sampleCount & 0x3;
|
||||||
|
NSUInteger cf = _colorPixelFormat & 0x3FF;
|
||||||
|
NSUInteger df = _depthPixelFormat & 0x3FF;
|
||||||
|
NSUInteger sf = _stencilPixelFormat & 0x3FF;
|
||||||
|
NSUInteger hash = (sf << 22) | (df << 12) | (cf << 2) | sc;
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)isEqual:(id)object
|
||||||
|
{
|
||||||
|
FramebufferDescriptor* other = object;
|
||||||
|
if (![other isKindOfClass:[FramebufferDescriptor class]])
|
||||||
|
return NO;
|
||||||
|
return other.sampleCount == self.sampleCount &&
|
||||||
|
other.colorPixelFormat == self.colorPixelFormat &&
|
||||||
|
other.depthPixelFormat == self.depthPixelFormat &&
|
||||||
|
other.stencilPixelFormat == self.stencilPixelFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
#pragma mark - MetalTexture implementation
|
||||||
|
|
||||||
|
@implementation MetalTexture
|
||||||
|
- (instancetype)initWithTexture:(id<MTLTexture>)metalTexture
|
||||||
|
{
|
||||||
|
if ((self = [super init]))
|
||||||
|
self.metalTexture = metalTexture;
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
#pragma mark - MetalContext implementation
|
||||||
|
|
||||||
|
@implementation MetalContext
|
||||||
|
- (instancetype)init
|
||||||
|
{
|
||||||
|
if ((self = [super init]))
|
||||||
|
{
|
||||||
|
self.renderPipelineStateCache = [NSMutableDictionary dictionary];
|
||||||
|
self.bufferCache = [NSMutableArray array];
|
||||||
|
_lastBufferCachePurge = GetMachAbsoluteTimeInSeconds();
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (MetalBuffer*)dequeueReusableBufferOfLength:(NSUInteger)length device:(id<MTLDevice>)device
|
||||||
|
{
|
||||||
|
uint64_t now = GetMachAbsoluteTimeInSeconds();
|
||||||
|
|
||||||
|
@synchronized(self.bufferCache)
|
||||||
|
{
|
||||||
|
// Purge old buffers that haven't been useful for a while
|
||||||
|
if (now - self.lastBufferCachePurge > 1.0)
|
||||||
|
{
|
||||||
|
NSMutableArray* survivors = [NSMutableArray array];
|
||||||
|
for (MetalBuffer* candidate in self.bufferCache)
|
||||||
|
if (candidate.lastReuseTime > self.lastBufferCachePurge)
|
||||||
|
[survivors addObject:candidate];
|
||||||
|
self.bufferCache = [survivors mutableCopy];
|
||||||
|
self.lastBufferCachePurge = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
// See if we have a buffer we can reuse
|
||||||
|
MetalBuffer* bestCandidate = nil;
|
||||||
|
for (MetalBuffer* candidate in self.bufferCache)
|
||||||
|
if (candidate.buffer.length >= length && (bestCandidate == nil || bestCandidate.lastReuseTime > candidate.lastReuseTime))
|
||||||
|
bestCandidate = candidate;
|
||||||
|
|
||||||
|
if (bestCandidate != nil)
|
||||||
|
{
|
||||||
|
[self.bufferCache removeObject:bestCandidate];
|
||||||
|
bestCandidate.lastReuseTime = now;
|
||||||
|
return bestCandidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No luck; make a new buffer
|
||||||
|
id<MTLBuffer> backing = [device newBufferWithLength:length options:MTLResourceStorageModeShared];
|
||||||
|
return [[MetalBuffer alloc] initWithBuffer:backing];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling.
|
||||||
|
- (id<MTLRenderPipelineState>)renderPipelineStateForFramebufferDescriptor:(FramebufferDescriptor*)descriptor device:(id<MTLDevice>)device
|
||||||
|
{
|
||||||
|
NSError* error = nil;
|
||||||
|
|
||||||
|
NSString* shaderSource = @""
|
||||||
|
"#include <metal_stdlib>\n"
|
||||||
|
"using namespace metal;\n"
|
||||||
|
"\n"
|
||||||
|
"struct Uniforms {\n"
|
||||||
|
" float4x4 projectionMatrix;\n"
|
||||||
|
"};\n"
|
||||||
|
"\n"
|
||||||
|
"struct VertexIn {\n"
|
||||||
|
" float2 position [[attribute(0)]];\n"
|
||||||
|
" float2 texCoords [[attribute(1)]];\n"
|
||||||
|
" uchar4 color [[attribute(2)]];\n"
|
||||||
|
"};\n"
|
||||||
|
"\n"
|
||||||
|
"struct VertexOut {\n"
|
||||||
|
" float4 position [[position]];\n"
|
||||||
|
" float2 texCoords;\n"
|
||||||
|
" float4 color;\n"
|
||||||
|
"};\n"
|
||||||
|
"\n"
|
||||||
|
"vertex VertexOut vertex_main(VertexIn in [[stage_in]],\n"
|
||||||
|
" constant Uniforms &uniforms [[buffer(1)]]) {\n"
|
||||||
|
" VertexOut out;\n"
|
||||||
|
" out.position = uniforms.projectionMatrix * float4(in.position, 0, 1);\n"
|
||||||
|
" out.texCoords = in.texCoords;\n"
|
||||||
|
" out.color = float4(in.color) / float4(255.0);\n"
|
||||||
|
" return out;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"fragment half4 fragment_main(VertexOut in [[stage_in]],\n"
|
||||||
|
" texture2d<half, access::sample> texture [[texture(0)]]) {\n"
|
||||||
|
" constexpr sampler linearSampler(coord::normalized, min_filter::linear, mag_filter::linear, mip_filter::linear);\n"
|
||||||
|
" half4 texColor = texture.sample(linearSampler, in.texCoords);\n"
|
||||||
|
" return half4(in.color) * texColor;\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
id<MTLLibrary> library = [device newLibraryWithSource:shaderSource options:nil error:&error];
|
||||||
|
if (library == nil)
|
||||||
|
{
|
||||||
|
NSLog(@"Error: failed to create Metal library: %@", error);
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
id<MTLFunction> vertexFunction = [library newFunctionWithName:@"vertex_main"];
|
||||||
|
id<MTLFunction> fragmentFunction = [library newFunctionWithName:@"fragment_main"];
|
||||||
|
|
||||||
|
if (vertexFunction == nil || fragmentFunction == nil)
|
||||||
|
{
|
||||||
|
NSLog(@"Error: failed to find Metal shader functions in library: %@", error);
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
MTLVertexDescriptor* vertexDescriptor = [MTLVertexDescriptor vertexDescriptor];
|
||||||
|
vertexDescriptor.attributes[0].offset = offsetof(ImDrawVert, pos);
|
||||||
|
vertexDescriptor.attributes[0].format = MTLVertexFormatFloat2; // position
|
||||||
|
vertexDescriptor.attributes[0].bufferIndex = 0;
|
||||||
|
vertexDescriptor.attributes[1].offset = offsetof(ImDrawVert, uv);
|
||||||
|
vertexDescriptor.attributes[1].format = MTLVertexFormatFloat2; // texCoords
|
||||||
|
vertexDescriptor.attributes[1].bufferIndex = 0;
|
||||||
|
vertexDescriptor.attributes[2].offset = offsetof(ImDrawVert, col);
|
||||||
|
vertexDescriptor.attributes[2].format = MTLVertexFormatUChar4; // color
|
||||||
|
vertexDescriptor.attributes[2].bufferIndex = 0;
|
||||||
|
vertexDescriptor.layouts[0].stepRate = 1;
|
||||||
|
vertexDescriptor.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
|
||||||
|
vertexDescriptor.layouts[0].stride = sizeof(ImDrawVert);
|
||||||
|
|
||||||
|
MTLRenderPipelineDescriptor* pipelineDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
|
||||||
|
pipelineDescriptor.vertexFunction = vertexFunction;
|
||||||
|
pipelineDescriptor.fragmentFunction = fragmentFunction;
|
||||||
|
pipelineDescriptor.vertexDescriptor = vertexDescriptor;
|
||||||
|
pipelineDescriptor.rasterSampleCount = self.framebufferDescriptor.sampleCount;
|
||||||
|
pipelineDescriptor.colorAttachments[0].pixelFormat = self.framebufferDescriptor.colorPixelFormat;
|
||||||
|
pipelineDescriptor.colorAttachments[0].blendingEnabled = YES;
|
||||||
|
pipelineDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
|
||||||
|
pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
|
||||||
|
pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
|
||||||
|
pipelineDescriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
|
||||||
|
pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorOne;
|
||||||
|
pipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
|
||||||
|
pipelineDescriptor.depthAttachmentPixelFormat = self.framebufferDescriptor.depthPixelFormat;
|
||||||
|
pipelineDescriptor.stencilAttachmentPixelFormat = self.framebufferDescriptor.stencilPixelFormat;
|
||||||
|
|
||||||
|
id<MTLRenderPipelineState> renderPipelineState = [device newRenderPipelineStateWithDescriptor:pipelineDescriptor error:&error];
|
||||||
|
if (error != nil)
|
||||||
|
NSLog(@"Error: failed to create Metal pipeline state: %@", error);
|
||||||
|
|
||||||
|
return renderPipelineState;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
346
imgui/backends/imgui_impl_opengl2.cpp
Normal file
346
imgui/backends/imgui_impl_opengl2.cpp
Normal file
@@ -0,0 +1,346 @@
|
|||||||
|
// dear imgui: Renderer Backend for OpenGL2 (legacy OpenGL, fixed pipeline)
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// Missing features or Issues:
|
||||||
|
// [ ] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// **DO NOT USE THIS CODE IF YOUR CODE/ENGINE IS USING MODERN OPENGL (SHADERS, VBO, VAO, etc.)**
|
||||||
|
// **Prefer using the code in imgui_impl_opengl3.cpp**
|
||||||
|
// This code is mostly provided as a reference to learn how ImGui integration works, because it is shorter to read.
|
||||||
|
// If your code is using GL3+ context or any semi modern OpenGL calls, using this is likely to make everything more
|
||||||
|
// complicated, will require your code to reset every single OpenGL attributes to their initial state, and might
|
||||||
|
// confuse your GPU driver.
|
||||||
|
// The GL2 code is unable to reset attributes or even call e.g. "glUseProgram(0)" because they don't exist in that API.
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-06-11: OpenGL: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplOpenGL2_CreateFontsTexture() and ImGui_ImplOpenGL2_DestroyFontsTexture().
|
||||||
|
// 2024-10-07: OpenGL: Changed default texture sampler to Clamp instead of Repeat/Wrap.
|
||||||
|
// 2024-06-28: OpenGL: ImGui_ImplOpenGL2_NewFrame() recreates font texture if it has been destroyed by ImGui_ImplOpenGL2_DestroyFontsTexture(). (#7748)
|
||||||
|
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
|
||||||
|
// 2021-12-08: OpenGL: Fixed mishandling of the ImDrawCmd::IdxOffset field! This is an old bug but it never had an effect until some internal rendering changes in 1.86.
|
||||||
|
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
|
||||||
|
// 2021-05-19: OpenGL: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
|
||||||
|
// 2021-01-03: OpenGL: Backup, setup and restore GL_SHADE_MODEL state, disable GL_STENCIL_TEST and disable GL_NORMAL_ARRAY client state to increase compatibility with legacy OpenGL applications.
|
||||||
|
// 2020-01-23: OpenGL: Backup, setup and restore GL_TEXTURE_ENV to increase compatibility with legacy OpenGL applications.
|
||||||
|
// 2019-04-30: OpenGL: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
|
||||||
|
// 2019-02-11: OpenGL: Projecting clipping rectangles correctly using draw_data->FramebufferScale to allow multi-viewports for retina display.
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
|
||||||
|
// 2018-08-03: OpenGL: Disabling/restoring GL_LIGHTING and GL_COLOR_MATERIAL to increase compatibility with legacy OpenGL applications.
|
||||||
|
// 2018-06-08: Misc: Extracted imgui_impl_opengl2.cpp/.h away from the old combined GLFW/SDL+OpenGL2 examples.
|
||||||
|
// 2018-06-08: OpenGL: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
|
||||||
|
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplOpenGL2_RenderDrawData() in the .h file so you can call it yourself.
|
||||||
|
// 2017-09-01: OpenGL: Save and restore current polygon mode.
|
||||||
|
// 2016-09-10: OpenGL: Uploading font texture as RGBA32 to increase compatibility with users shaders (not ideal).
|
||||||
|
// 2016-09-05: OpenGL: Fixed save and restore of current scissor rectangle.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_opengl2.h"
|
||||||
|
#include <stdint.h> // intptr_t
|
||||||
|
|
||||||
|
// Clang/GCC warnings with -Weverything
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used
|
||||||
|
#pragma clang diagnostic ignored "-Wnonportable-system-include-path"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Include OpenGL header (without an OpenGL loader) requires a bit of fiddling
|
||||||
|
#if defined(_WIN32) && !defined(APIENTRY)
|
||||||
|
#define APIENTRY __stdcall // It is customary to use APIENTRY for OpenGL function pointer declarations on all platforms. Additionally, the Windows OpenGL header needs APIENTRY.
|
||||||
|
#endif
|
||||||
|
#if defined(_WIN32) && !defined(WINGDIAPI)
|
||||||
|
#define WINGDIAPI __declspec(dllimport) // Some Windows OpenGL headers need this
|
||||||
|
#endif
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
#define GL_SILENCE_DEPRECATION
|
||||||
|
#include <OpenGL/gl.h>
|
||||||
|
#else
|
||||||
|
#include <GL/gl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// [Debugging]
|
||||||
|
//#define IMGUI_IMPL_OPENGL_DEBUG
|
||||||
|
#ifdef IMGUI_IMPL_OPENGL_DEBUG
|
||||||
|
#include <stdio.h>
|
||||||
|
#define GL_CALL(_CALL) do { _CALL; GLenum gl_err = glGetError(); if (gl_err != 0) fprintf(stderr, "GL error 0x%x returned from '%s'.\n", gl_err, #_CALL); } while (0) // Call with error check
|
||||||
|
#else
|
||||||
|
#define GL_CALL(_CALL) _CALL // Call without error check
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// OpenGL data
|
||||||
|
struct ImGui_ImplOpenGL2_Data
|
||||||
|
{
|
||||||
|
ImGui_ImplOpenGL2_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
static ImGui_ImplOpenGL2_Data* ImGui_ImplOpenGL2_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplOpenGL2_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
bool ImGui_ImplOpenGL2_Init()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplOpenGL2_Data* bd = IM_NEW(ImGui_ImplOpenGL2_Data)();
|
||||||
|
io.BackendRendererUserData = (void*)bd;
|
||||||
|
io.BackendRendererName = "imgui_impl_opengl2";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplOpenGL2_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
ImGui_ImplOpenGL2_DestroyDeviceObjects();
|
||||||
|
io.BackendRendererName = nullptr;
|
||||||
|
io.BackendRendererUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplOpenGL2_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplOpenGL2_Init()?");
|
||||||
|
IM_UNUSED(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplOpenGL2_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height)
|
||||||
|
{
|
||||||
|
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers, polygon fill.
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
//glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); // In order to composite our output buffer we need to preserve alpha
|
||||||
|
glDisable(GL_CULL_FACE);
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
glDisable(GL_STENCIL_TEST);
|
||||||
|
glDisable(GL_LIGHTING);
|
||||||
|
glDisable(GL_COLOR_MATERIAL);
|
||||||
|
glEnable(GL_SCISSOR_TEST);
|
||||||
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
|
glDisableClientState(GL_NORMAL_ARRAY);
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
glShadeModel(GL_SMOOTH);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||||
|
|
||||||
|
// If you are using this code with non-legacy OpenGL header/contexts (which you should not, prefer using imgui_impl_opengl3.cpp!!),
|
||||||
|
// you may need to backup/reset/restore other state, e.g. for current shader using the commented lines below.
|
||||||
|
// (DO NOT MODIFY THIS FILE! Add the code in your calling function)
|
||||||
|
// GLint last_program;
|
||||||
|
// glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
|
||||||
|
// glUseProgram(0);
|
||||||
|
// ImGui_ImplOpenGL2_RenderDrawData(...);
|
||||||
|
// glUseProgram(last_program)
|
||||||
|
// There are potentially many more states you could need to clear/setup that we can't access from default headers.
|
||||||
|
// e.g. glBindBuffer(GL_ARRAY_BUFFER, 0), glDisable(GL_TEXTURE_CUBE_MAP).
|
||||||
|
|
||||||
|
// Setup viewport, orthographic projection matrix
|
||||||
|
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
|
||||||
|
GL_CALL(glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height));
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glPushMatrix();
|
||||||
|
glLoadIdentity();
|
||||||
|
glOrtho(draw_data->DisplayPos.x, draw_data->DisplayPos.x + draw_data->DisplaySize.x, draw_data->DisplayPos.y + draw_data->DisplaySize.y, draw_data->DisplayPos.y, -1.0f, +1.0f);
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glPushMatrix();
|
||||||
|
glLoadIdentity();
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenGL2 Render function.
|
||||||
|
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly.
|
||||||
|
// This is in order to be able to run within an OpenGL engine that doesn't do so.
|
||||||
|
void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data)
|
||||||
|
{
|
||||||
|
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
|
||||||
|
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
|
||||||
|
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
|
||||||
|
if (fb_width == 0 || fb_height == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplOpenGL2_UpdateTexture(tex);
|
||||||
|
|
||||||
|
// Backup GL state
|
||||||
|
GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
|
||||||
|
GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
|
||||||
|
GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
|
||||||
|
GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
|
||||||
|
GLint last_shade_model; glGetIntegerv(GL_SHADE_MODEL, &last_shade_model);
|
||||||
|
GLint last_tex_env_mode; glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &last_tex_env_mode);
|
||||||
|
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
|
||||||
|
|
||||||
|
// Setup desired GL state
|
||||||
|
ImGui_ImplOpenGL2_SetupRenderState(draw_data, fb_width, fb_height);
|
||||||
|
|
||||||
|
// Will project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
|
||||||
|
ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data;
|
||||||
|
const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data;
|
||||||
|
glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (const GLvoid*)((const char*)vtx_buffer + offsetof(ImDrawVert, pos)));
|
||||||
|
glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (const GLvoid*)((const char*)vtx_buffer + offsetof(ImDrawVert, uv)));
|
||||||
|
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (const GLvoid*)((const char*)vtx_buffer + offsetof(ImDrawVert, col)));
|
||||||
|
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplOpenGL2_SetupRenderState(draw_data, fb_width, fb_height);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
|
||||||
|
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Apply scissor/clipping rectangle (Y is inverted in OpenGL)
|
||||||
|
glScissor((int)clip_min.x, (int)((float)fb_height - clip_max.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y));
|
||||||
|
|
||||||
|
// Bind texture, Draw
|
||||||
|
glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID());
|
||||||
|
glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer + pcmd->IdxOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore modified GL state
|
||||||
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, (GLuint)last_texture);
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glPopMatrix();
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glPopMatrix();
|
||||||
|
glPopAttrib();
|
||||||
|
glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]);
|
||||||
|
glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
|
||||||
|
glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
|
||||||
|
glShadeModel(last_shade_model);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, last_tex_env_mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplOpenGL2_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == 0 && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
const void* pixels = tex->GetPixels();
|
||||||
|
GLuint gl_texture_id = 0;
|
||||||
|
|
||||||
|
// Upload texture to graphics system
|
||||||
|
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
|
||||||
|
GLint last_texture;
|
||||||
|
GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture));
|
||||||
|
GL_CALL(glGenTextures(1, &gl_texture_id));
|
||||||
|
GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_texture_id));
|
||||||
|
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
|
||||||
|
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
|
||||||
|
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
|
||||||
|
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
|
||||||
|
GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0));
|
||||||
|
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->Width, tex->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels));
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)(intptr_t)gl_texture_id);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
|
||||||
|
// Restore state
|
||||||
|
GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture));
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
// Update selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
|
||||||
|
GLint last_texture;
|
||||||
|
GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture));
|
||||||
|
|
||||||
|
GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID;
|
||||||
|
GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_tex_id));
|
||||||
|
GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->Width));
|
||||||
|
for (ImTextureRect& r : tex->Updates)
|
||||||
|
GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.w, r.h, GL_RGBA, GL_UNSIGNED_BYTE, tex->GetPixelsAt(r.x, r.y)));
|
||||||
|
GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0));
|
||||||
|
GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); // Restore state
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantDestroy)
|
||||||
|
{
|
||||||
|
GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID;
|
||||||
|
glDeleteTextures(1, &gl_tex_id);
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplOpenGL2_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplOpenGL2_DestroyDeviceObjects()
|
||||||
|
{
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
{
|
||||||
|
tex->SetStatus(ImTextureStatus_WantDestroy);
|
||||||
|
ImGui_ImplOpenGL2_UpdateTexture(tex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
43
imgui/backends/imgui_impl_opengl2.h
Normal file
43
imgui/backends/imgui_impl_opengl2.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
// dear imgui: Renderer Backend for OpenGL2 (legacy OpenGL, fixed pipeline)
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// Missing features or Issues:
|
||||||
|
// [ ] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// **DO NOT USE THIS CODE IF YOUR CODE/ENGINE IS USING MODERN OPENGL (SHADERS, VBO, VAO, etc.)**
|
||||||
|
// **Prefer using the code in imgui_impl_opengl3.cpp**
|
||||||
|
// This code is mostly provided as a reference to learn how ImGui integration works, because it is shorter to read.
|
||||||
|
// If your code is using GL3+ context or any semi modern OpenGL calls, using this is likely to make everything more
|
||||||
|
// complicated, will require your code to reset every single OpenGL attributes to their initial state, and might
|
||||||
|
// confuse your GPU driver.
|
||||||
|
// The GL2 code is unable to reset attributes or even call e.g. "glUseProgram(0)" because they don't exist in that API.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplOpenGL2_Init();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOpenGL2_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOpenGL2_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data);
|
||||||
|
|
||||||
|
// Called by Init/NewFrame/Shutdown
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplOpenGL2_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOpenGL2_DestroyDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOpenGL2_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
1038
imgui/backends/imgui_impl_opengl3.cpp
Normal file
1038
imgui/backends/imgui_impl_opengl3.cpp
Normal file
File diff suppressed because it is too large
Load Diff
68
imgui/backends/imgui_impl_opengl3.h
Normal file
68
imgui/backends/imgui_impl_opengl3.h
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
// dear imgui: Renderer Backend for modern OpenGL with shaders / programmatic pipeline
|
||||||
|
// - Desktop GL: 2.x 3.x 4.x
|
||||||
|
// - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0)
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [x] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset) [Desktop OpenGL only!]
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
|
||||||
|
// About WebGL/ES:
|
||||||
|
// - You need to '#define IMGUI_IMPL_OPENGL_ES2' or '#define IMGUI_IMPL_OPENGL_ES3' to use WebGL or OpenGL ES.
|
||||||
|
// - This is done automatically on iOS, Android and Emscripten targets.
|
||||||
|
// - For other targets, the define needs to be visible from the imgui_impl_opengl3.cpp compilation unit. If unsure, define globally or in imconfig.h.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// About GLSL version:
|
||||||
|
// The 'glsl_version' initialization parameter should be nullptr (default) or a "#version XXX" string.
|
||||||
|
// On computer platform the GLSL version default to "#version 130". On OpenGL ES 3 platform it defaults to "#version 300 es"
|
||||||
|
// Only override if your GL version doesn't handle this GLSL version. See GLSL version table at the top of imgui_impl_opengl3.cpp.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = nullptr);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data);
|
||||||
|
|
||||||
|
// (Optional) Called by Init/NewFrame/Shutdown
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOpenGL3_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
// Configuration flags to add in your imconfig file:
|
||||||
|
//#define IMGUI_IMPL_OPENGL_ES2 // Enable ES 2 (Auto-detected on Emscripten)
|
||||||
|
//#define IMGUI_IMPL_OPENGL_ES3 // Enable ES 3 (Auto-detected on iOS/Android)
|
||||||
|
|
||||||
|
// You can explicitly select GLES2 or GLES3 API by using one of the '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line.
|
||||||
|
#if !defined(IMGUI_IMPL_OPENGL_ES2) \
|
||||||
|
&& !defined(IMGUI_IMPL_OPENGL_ES3)
|
||||||
|
|
||||||
|
// Try to detect GLES on matching platforms
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
#include <TargetConditionals.h>
|
||||||
|
#endif
|
||||||
|
#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__))
|
||||||
|
#define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es"
|
||||||
|
#elif defined(__EMSCRIPTEN__) || defined(__amigaos4__)
|
||||||
|
#define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100"
|
||||||
|
#else
|
||||||
|
// Otherwise imgui_impl_opengl3_loader.h will be used.
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
922
imgui/backends/imgui_impl_opengl3_loader.h
Normal file
922
imgui/backends/imgui_impl_opengl3_loader.h
Normal file
@@ -0,0 +1,922 @@
|
|||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// About imgui_impl_opengl3_loader.h:
|
||||||
|
//
|
||||||
|
// We embed our own OpenGL loader to not require user to provide their own or to have to use ours,
|
||||||
|
// which proved to be endless problems for users.
|
||||||
|
// Our loader is custom-generated, based on gl3w but automatically filtered to only include
|
||||||
|
// enums/functions that we use in our imgui_impl_opengl3.cpp source file in order to be small.
|
||||||
|
//
|
||||||
|
// YOU SHOULD NOT NEED TO INCLUDE/USE THIS DIRECTLY. THIS IS USED BY imgui_impl_opengl3.cpp ONLY.
|
||||||
|
// THE REST OF YOUR APP SHOULD USE A DIFFERENT GL LOADER: ANY GL LOADER OF YOUR CHOICE.
|
||||||
|
//
|
||||||
|
// IF YOU GET BUILD ERRORS IN THIS FILE (commonly macro redefinitions or function redefinitions):
|
||||||
|
// IT LIKELY MEANS THAT YOU ARE BUILDING 'imgui_impl_opengl3.cpp' OR INCLUDING 'imgui_impl_opengl3_loader.h'
|
||||||
|
// IN THE SAME COMPILATION UNIT AS ONE OF YOUR FILE WHICH IS USING A THIRD-PARTY OPENGL LOADER.
|
||||||
|
// (e.g. COULD HAPPEN IF YOU ARE DOING A UNITY/JUMBO BUILD, OR INCLUDING .CPP FILES FROM OTHERS)
|
||||||
|
// YOU SHOULD NOT BUILD BOTH IN THE SAME COMPILATION UNIT.
|
||||||
|
// BUT IF YOU REALLY WANT TO, you can '#define IMGUI_IMPL_OPENGL_LOADER_CUSTOM' and imgui_impl_opengl3.cpp
|
||||||
|
// WILL NOT BE USING OUR LOADER, AND INSTEAD EXPECT ANOTHER/YOUR LOADER TO BE AVAILABLE IN THE COMPILATION UNIT.
|
||||||
|
//
|
||||||
|
// Regenerate with:
|
||||||
|
// python3 gl3w_gen.py --output ../imgui/backends/imgui_impl_opengl3_loader.h --ref ../imgui/backends/imgui_impl_opengl3.cpp ./extra_symbols.txt
|
||||||
|
//
|
||||||
|
// More info:
|
||||||
|
// https://github.com/dearimgui/gl3w_stripped
|
||||||
|
// https://github.com/ocornut/imgui/issues/4445
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file was generated with gl3w_gen.py, part of imgl3w
|
||||||
|
* (hosted at https://github.com/dearimgui/gl3w_stripped)
|
||||||
|
*
|
||||||
|
* This is free and unencumbered software released into the public domain.
|
||||||
|
*
|
||||||
|
* Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||||
|
* distribute this software, either in source code form or as a compiled
|
||||||
|
* binary, for any purpose, commercial or non-commercial, and by any
|
||||||
|
* means.
|
||||||
|
*
|
||||||
|
* In jurisdictions that recognize copyright laws, the author or authors
|
||||||
|
* of this software dedicate any and all copyright interest in the
|
||||||
|
* software to the public domain. We make this dedication for the benefit
|
||||||
|
* of the public at large and to the detriment of our heirs and
|
||||||
|
* successors. We intend this dedication to be an overt act of
|
||||||
|
* relinquishment in perpetuity of all present and future rights to this
|
||||||
|
* software under copyright law.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||||
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||||
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __gl3w_h_
|
||||||
|
#define __gl3w_h_
|
||||||
|
|
||||||
|
// Adapted from KHR/khrplatform.h to avoid including entire file.
|
||||||
|
#ifndef __khrplatform_h_
|
||||||
|
typedef float khronos_float_t;
|
||||||
|
typedef signed char khronos_int8_t;
|
||||||
|
typedef unsigned char khronos_uint8_t;
|
||||||
|
typedef signed short int khronos_int16_t;
|
||||||
|
typedef unsigned short int khronos_uint16_t;
|
||||||
|
#ifdef _WIN64
|
||||||
|
typedef signed long long int khronos_intptr_t;
|
||||||
|
typedef signed long long int khronos_ssize_t;
|
||||||
|
#else
|
||||||
|
typedef signed long int khronos_intptr_t;
|
||||||
|
typedef signed long int khronos_ssize_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(_MSC_VER) && !defined(__clang__)
|
||||||
|
typedef signed __int64 khronos_int64_t;
|
||||||
|
typedef unsigned __int64 khronos_uint64_t;
|
||||||
|
#elif (defined(__clang__) || defined(__GNUC__)) && (__cplusplus < 201100)
|
||||||
|
#include <stdint.h>
|
||||||
|
typedef int64_t khronos_int64_t;
|
||||||
|
typedef uint64_t khronos_uint64_t;
|
||||||
|
#else
|
||||||
|
typedef signed long long khronos_int64_t;
|
||||||
|
typedef unsigned long long khronos_uint64_t;
|
||||||
|
#endif
|
||||||
|
#endif // __khrplatform_h_
|
||||||
|
|
||||||
|
#ifndef __gl_glcorearb_h_
|
||||||
|
#define __gl_glcorearb_h_ 1
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
/*
|
||||||
|
** Copyright 2013-2020 The Khronos Group Inc.
|
||||||
|
** SPDX-License-Identifier: MIT
|
||||||
|
**
|
||||||
|
** This header is generated from the Khronos OpenGL / OpenGL ES XML
|
||||||
|
** API Registry. The current version of the Registry, generator scripts
|
||||||
|
** used to make the header, and the header can be found at
|
||||||
|
** https://github.com/KhronosGroup/OpenGL-Registry
|
||||||
|
*/
|
||||||
|
#if defined(_WIN32) && !defined(APIENTRY) && !defined(__CYGWIN__) && !defined(__SCITECH_SNAP__)
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN 1
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
#ifndef APIENTRY
|
||||||
|
#define APIENTRY
|
||||||
|
#endif
|
||||||
|
#ifndef APIENTRYP
|
||||||
|
#define APIENTRYP APIENTRY *
|
||||||
|
#endif
|
||||||
|
#ifndef GLAPI
|
||||||
|
#define GLAPI extern
|
||||||
|
#endif
|
||||||
|
/* glcorearb.h is for use with OpenGL core profile implementations.
|
||||||
|
** It should should be placed in the same directory as gl.h and
|
||||||
|
** included as <GL/glcorearb.h>.
|
||||||
|
**
|
||||||
|
** glcorearb.h includes only APIs in the latest OpenGL core profile
|
||||||
|
** implementation together with APIs in newer ARB extensions which
|
||||||
|
** can be supported by the core profile. It does not, and never will
|
||||||
|
** include functionality removed from the core profile, such as
|
||||||
|
** fixed-function vertex and fragment processing.
|
||||||
|
**
|
||||||
|
** Do not #include both <GL/glcorearb.h> and either of <GL/gl.h> or
|
||||||
|
** <GL/glext.h> in the same source file.
|
||||||
|
*/
|
||||||
|
/* Generated C header for:
|
||||||
|
* API: gl
|
||||||
|
* Profile: core
|
||||||
|
* Versions considered: .*
|
||||||
|
* Versions emitted: .*
|
||||||
|
* Default extensions included: glcore
|
||||||
|
* Additional extensions included: _nomatch_^
|
||||||
|
* Extensions removed: _nomatch_^
|
||||||
|
*/
|
||||||
|
#ifndef GL_VERSION_1_0
|
||||||
|
typedef void GLvoid;
|
||||||
|
typedef unsigned int GLenum;
|
||||||
|
|
||||||
|
typedef khronos_float_t GLfloat;
|
||||||
|
typedef int GLint;
|
||||||
|
typedef int GLsizei;
|
||||||
|
typedef unsigned int GLbitfield;
|
||||||
|
typedef double GLdouble;
|
||||||
|
typedef unsigned int GLuint;
|
||||||
|
typedef unsigned char GLboolean;
|
||||||
|
typedef khronos_uint8_t GLubyte;
|
||||||
|
#define GL_COLOR_BUFFER_BIT 0x00004000
|
||||||
|
#define GL_FALSE 0
|
||||||
|
#define GL_TRUE 1
|
||||||
|
#define GL_TRIANGLES 0x0004
|
||||||
|
#define GL_ONE 1
|
||||||
|
#define GL_SRC_ALPHA 0x0302
|
||||||
|
#define GL_ONE_MINUS_SRC_ALPHA 0x0303
|
||||||
|
#define GL_FRONT 0x0404
|
||||||
|
#define GL_BACK 0x0405
|
||||||
|
#define GL_FRONT_AND_BACK 0x0408
|
||||||
|
#define GL_POLYGON_MODE 0x0B40
|
||||||
|
#define GL_CULL_FACE 0x0B44
|
||||||
|
#define GL_DEPTH_TEST 0x0B71
|
||||||
|
#define GL_STENCIL_TEST 0x0B90
|
||||||
|
#define GL_VIEWPORT 0x0BA2
|
||||||
|
#define GL_BLEND 0x0BE2
|
||||||
|
#define GL_SCISSOR_BOX 0x0C10
|
||||||
|
#define GL_SCISSOR_TEST 0x0C11
|
||||||
|
#define GL_UNPACK_ROW_LENGTH 0x0CF2
|
||||||
|
#define GL_PACK_ALIGNMENT 0x0D05
|
||||||
|
#define GL_MAX_TEXTURE_SIZE 0x0D33
|
||||||
|
#define GL_TEXTURE_2D 0x0DE1
|
||||||
|
#define GL_UNSIGNED_BYTE 0x1401
|
||||||
|
#define GL_UNSIGNED_SHORT 0x1403
|
||||||
|
#define GL_UNSIGNED_INT 0x1405
|
||||||
|
#define GL_FLOAT 0x1406
|
||||||
|
#define GL_RGBA 0x1908
|
||||||
|
#define GL_FILL 0x1B02
|
||||||
|
#define GL_VENDOR 0x1F00
|
||||||
|
#define GL_RENDERER 0x1F01
|
||||||
|
#define GL_VERSION 0x1F02
|
||||||
|
#define GL_EXTENSIONS 0x1F03
|
||||||
|
#define GL_LINEAR 0x2601
|
||||||
|
#define GL_TEXTURE_MAG_FILTER 0x2800
|
||||||
|
#define GL_TEXTURE_MIN_FILTER 0x2801
|
||||||
|
#define GL_TEXTURE_WRAP_S 0x2802
|
||||||
|
#define GL_TEXTURE_WRAP_T 0x2803
|
||||||
|
#define GL_REPEAT 0x2901
|
||||||
|
typedef void (APIENTRYP PFNGLPOLYGONMODEPROC) (GLenum face, GLenum mode);
|
||||||
|
typedef void (APIENTRYP PFNGLSCISSORPROC) (GLint x, GLint y, GLsizei width, GLsizei height);
|
||||||
|
typedef void (APIENTRYP PFNGLTEXPARAMETERIPROC) (GLenum target, GLenum pname, GLint param);
|
||||||
|
typedef void (APIENTRYP PFNGLTEXIMAGE2DPROC) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels);
|
||||||
|
typedef void (APIENTRYP PFNGLCLEARPROC) (GLbitfield mask);
|
||||||
|
typedef void (APIENTRYP PFNGLCLEARCOLORPROC) (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
|
||||||
|
typedef void (APIENTRYP PFNGLDISABLEPROC) (GLenum cap);
|
||||||
|
typedef void (APIENTRYP PFNGLENABLEPROC) (GLenum cap);
|
||||||
|
typedef void (APIENTRYP PFNGLFLUSHPROC) (void);
|
||||||
|
typedef void (APIENTRYP PFNGLPIXELSTOREIPROC) (GLenum pname, GLint param);
|
||||||
|
typedef void (APIENTRYP PFNGLREADPIXELSPROC) (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels);
|
||||||
|
typedef GLenum (APIENTRYP PFNGLGETERRORPROC) (void);
|
||||||
|
typedef void (APIENTRYP PFNGLGETINTEGERVPROC) (GLenum pname, GLint *data);
|
||||||
|
typedef const GLubyte *(APIENTRYP PFNGLGETSTRINGPROC) (GLenum name);
|
||||||
|
typedef GLboolean (APIENTRYP PFNGLISENABLEDPROC) (GLenum cap);
|
||||||
|
typedef void (APIENTRYP PFNGLVIEWPORTPROC) (GLint x, GLint y, GLsizei width, GLsizei height);
|
||||||
|
#ifdef GL_GLEXT_PROTOTYPES
|
||||||
|
GLAPI void APIENTRY glPolygonMode (GLenum face, GLenum mode);
|
||||||
|
GLAPI void APIENTRY glScissor (GLint x, GLint y, GLsizei width, GLsizei height);
|
||||||
|
GLAPI void APIENTRY glTexParameteri (GLenum target, GLenum pname, GLint param);
|
||||||
|
GLAPI void APIENTRY glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels);
|
||||||
|
GLAPI void APIENTRY glClear (GLbitfield mask);
|
||||||
|
GLAPI void APIENTRY glClearColor (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
|
||||||
|
GLAPI void APIENTRY glDisable (GLenum cap);
|
||||||
|
GLAPI void APIENTRY glEnable (GLenum cap);
|
||||||
|
GLAPI void APIENTRY glFlush (void);
|
||||||
|
GLAPI void APIENTRY glPixelStorei (GLenum pname, GLint param);
|
||||||
|
GLAPI void APIENTRY glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels);
|
||||||
|
GLAPI GLenum APIENTRY glGetError (void);
|
||||||
|
GLAPI void APIENTRY glGetIntegerv (GLenum pname, GLint *data);
|
||||||
|
GLAPI const GLubyte *APIENTRY glGetString (GLenum name);
|
||||||
|
GLAPI GLboolean APIENTRY glIsEnabled (GLenum cap);
|
||||||
|
GLAPI void APIENTRY glViewport (GLint x, GLint y, GLsizei width, GLsizei height);
|
||||||
|
#endif
|
||||||
|
#endif /* GL_VERSION_1_0 */
|
||||||
|
#ifndef GL_VERSION_1_1
|
||||||
|
typedef khronos_float_t GLclampf;
|
||||||
|
typedef double GLclampd;
|
||||||
|
#define GL_TEXTURE_BINDING_2D 0x8069
|
||||||
|
typedef void (APIENTRYP PFNGLDRAWELEMENTSPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices);
|
||||||
|
typedef void (APIENTRYP PFNGLTEXSUBIMAGE2DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
|
||||||
|
typedef void (APIENTRYP PFNGLBINDTEXTUREPROC) (GLenum target, GLuint texture);
|
||||||
|
typedef void (APIENTRYP PFNGLDELETETEXTURESPROC) (GLsizei n, const GLuint *textures);
|
||||||
|
typedef void (APIENTRYP PFNGLGENTEXTURESPROC) (GLsizei n, GLuint *textures);
|
||||||
|
#ifdef GL_GLEXT_PROTOTYPES
|
||||||
|
GLAPI void APIENTRY glDrawElements (GLenum mode, GLsizei count, GLenum type, const void *indices);
|
||||||
|
GLAPI void APIENTRY glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
|
||||||
|
GLAPI void APIENTRY glBindTexture (GLenum target, GLuint texture);
|
||||||
|
GLAPI void APIENTRY glDeleteTextures (GLsizei n, const GLuint *textures);
|
||||||
|
GLAPI void APIENTRY glGenTextures (GLsizei n, GLuint *textures);
|
||||||
|
#endif
|
||||||
|
#endif /* GL_VERSION_1_1 */
|
||||||
|
#ifndef GL_VERSION_1_2
|
||||||
|
#define GL_CLAMP_TO_EDGE 0x812F
|
||||||
|
#endif /* GL_VERSION_1_2 */
|
||||||
|
#ifndef GL_VERSION_1_3
|
||||||
|
#define GL_TEXTURE0 0x84C0
|
||||||
|
#define GL_ACTIVE_TEXTURE 0x84E0
|
||||||
|
typedef void (APIENTRYP PFNGLACTIVETEXTUREPROC) (GLenum texture);
|
||||||
|
#ifdef GL_GLEXT_PROTOTYPES
|
||||||
|
GLAPI void APIENTRY glActiveTexture (GLenum texture);
|
||||||
|
#endif
|
||||||
|
#endif /* GL_VERSION_1_3 */
|
||||||
|
#ifndef GL_VERSION_1_4
|
||||||
|
#define GL_BLEND_DST_RGB 0x80C8
|
||||||
|
#define GL_BLEND_SRC_RGB 0x80C9
|
||||||
|
#define GL_BLEND_DST_ALPHA 0x80CA
|
||||||
|
#define GL_BLEND_SRC_ALPHA 0x80CB
|
||||||
|
#define GL_FUNC_ADD 0x8006
|
||||||
|
typedef void (APIENTRYP PFNGLBLENDFUNCSEPARATEPROC) (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha);
|
||||||
|
typedef void (APIENTRYP PFNGLBLENDEQUATIONPROC) (GLenum mode);
|
||||||
|
#ifdef GL_GLEXT_PROTOTYPES
|
||||||
|
GLAPI void APIENTRY glBlendFuncSeparate (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha);
|
||||||
|
GLAPI void APIENTRY glBlendEquation (GLenum mode);
|
||||||
|
#endif
|
||||||
|
#endif /* GL_VERSION_1_4 */
|
||||||
|
#ifndef GL_VERSION_1_5
|
||||||
|
typedef khronos_ssize_t GLsizeiptr;
|
||||||
|
typedef khronos_intptr_t GLintptr;
|
||||||
|
#define GL_ARRAY_BUFFER 0x8892
|
||||||
|
#define GL_ELEMENT_ARRAY_BUFFER 0x8893
|
||||||
|
#define GL_ARRAY_BUFFER_BINDING 0x8894
|
||||||
|
#define GL_ELEMENT_ARRAY_BUFFER_BINDING 0x8895
|
||||||
|
#define GL_STREAM_DRAW 0x88E0
|
||||||
|
typedef void (APIENTRYP PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
|
||||||
|
typedef void (APIENTRYP PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers);
|
||||||
|
typedef void (APIENTRYP PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
|
||||||
|
typedef void (APIENTRYP PFNGLBUFFERDATAPROC) (GLenum target, GLsizeiptr size, const void *data, GLenum usage);
|
||||||
|
typedef void (APIENTRYP PFNGLBUFFERSUBDATAPROC) (GLenum target, GLintptr offset, GLsizeiptr size, const void *data);
|
||||||
|
#ifdef GL_GLEXT_PROTOTYPES
|
||||||
|
GLAPI void APIENTRY glBindBuffer (GLenum target, GLuint buffer);
|
||||||
|
GLAPI void APIENTRY glDeleteBuffers (GLsizei n, const GLuint *buffers);
|
||||||
|
GLAPI void APIENTRY glGenBuffers (GLsizei n, GLuint *buffers);
|
||||||
|
GLAPI void APIENTRY glBufferData (GLenum target, GLsizeiptr size, const void *data, GLenum usage);
|
||||||
|
GLAPI void APIENTRY glBufferSubData (GLenum target, GLintptr offset, GLsizeiptr size, const void *data);
|
||||||
|
#endif
|
||||||
|
#endif /* GL_VERSION_1_5 */
|
||||||
|
#ifndef GL_VERSION_2_0
|
||||||
|
typedef char GLchar;
|
||||||
|
typedef khronos_int16_t GLshort;
|
||||||
|
typedef khronos_int8_t GLbyte;
|
||||||
|
typedef khronos_uint16_t GLushort;
|
||||||
|
#define GL_BLEND_EQUATION_RGB 0x8009
|
||||||
|
#define GL_VERTEX_ATTRIB_ARRAY_ENABLED 0x8622
|
||||||
|
#define GL_VERTEX_ATTRIB_ARRAY_SIZE 0x8623
|
||||||
|
#define GL_VERTEX_ATTRIB_ARRAY_STRIDE 0x8624
|
||||||
|
#define GL_VERTEX_ATTRIB_ARRAY_TYPE 0x8625
|
||||||
|
#define GL_VERTEX_ATTRIB_ARRAY_POINTER 0x8645
|
||||||
|
#define GL_BLEND_EQUATION_ALPHA 0x883D
|
||||||
|
#define GL_VERTEX_ATTRIB_ARRAY_NORMALIZED 0x886A
|
||||||
|
#define GL_FRAGMENT_SHADER 0x8B30
|
||||||
|
#define GL_VERTEX_SHADER 0x8B31
|
||||||
|
#define GL_COMPILE_STATUS 0x8B81
|
||||||
|
#define GL_LINK_STATUS 0x8B82
|
||||||
|
#define GL_INFO_LOG_LENGTH 0x8B84
|
||||||
|
#define GL_CURRENT_PROGRAM 0x8B8D
|
||||||
|
#define GL_UPPER_LEFT 0x8CA2
|
||||||
|
typedef void (APIENTRYP PFNGLBLENDEQUATIONSEPARATEPROC) (GLenum modeRGB, GLenum modeAlpha);
|
||||||
|
typedef void (APIENTRYP PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
|
||||||
|
typedef void (APIENTRYP PFNGLCOMPILESHADERPROC) (GLuint shader);
|
||||||
|
typedef GLuint (APIENTRYP PFNGLCREATEPROGRAMPROC) (void);
|
||||||
|
typedef GLuint (APIENTRYP PFNGLCREATESHADERPROC) (GLenum type);
|
||||||
|
typedef void (APIENTRYP PFNGLDELETEPROGRAMPROC) (GLuint program);
|
||||||
|
typedef void (APIENTRYP PFNGLDELETESHADERPROC) (GLuint shader);
|
||||||
|
typedef void (APIENTRYP PFNGLDETACHSHADERPROC) (GLuint program, GLuint shader);
|
||||||
|
typedef void (APIENTRYP PFNGLDISABLEVERTEXATTRIBARRAYPROC) (GLuint index);
|
||||||
|
typedef void (APIENTRYP PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
|
||||||
|
typedef GLint (APIENTRYP PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const GLchar *name);
|
||||||
|
typedef void (APIENTRYP PFNGLGETPROGRAMIVPROC) (GLuint program, GLenum pname, GLint *params);
|
||||||
|
typedef void (APIENTRYP PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
|
||||||
|
typedef void (APIENTRYP PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
|
||||||
|
typedef void (APIENTRYP PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
|
||||||
|
typedef GLint (APIENTRYP PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const GLchar *name);
|
||||||
|
typedef void (APIENTRYP PFNGLGETVERTEXATTRIBIVPROC) (GLuint index, GLenum pname, GLint *params);
|
||||||
|
typedef void (APIENTRYP PFNGLGETVERTEXATTRIBPOINTERVPROC) (GLuint index, GLenum pname, void **pointer);
|
||||||
|
typedef GLboolean (APIENTRYP PFNGLISPROGRAMPROC) (GLuint program);
|
||||||
|
typedef void (APIENTRYP PFNGLLINKPROGRAMPROC) (GLuint program);
|
||||||
|
typedef void (APIENTRYP PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length);
|
||||||
|
typedef void (APIENTRYP PFNGLUSEPROGRAMPROC) (GLuint program);
|
||||||
|
typedef void (APIENTRYP PFNGLUNIFORM1IPROC) (GLint location, GLint v0);
|
||||||
|
typedef void (APIENTRYP PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
|
||||||
|
typedef void (APIENTRYP PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer);
|
||||||
|
#ifdef GL_GLEXT_PROTOTYPES
|
||||||
|
GLAPI void APIENTRY glBlendEquationSeparate (GLenum modeRGB, GLenum modeAlpha);
|
||||||
|
GLAPI void APIENTRY glAttachShader (GLuint program, GLuint shader);
|
||||||
|
GLAPI void APIENTRY glCompileShader (GLuint shader);
|
||||||
|
GLAPI GLuint APIENTRY glCreateProgram (void);
|
||||||
|
GLAPI GLuint APIENTRY glCreateShader (GLenum type);
|
||||||
|
GLAPI void APIENTRY glDeleteProgram (GLuint program);
|
||||||
|
GLAPI void APIENTRY glDeleteShader (GLuint shader);
|
||||||
|
GLAPI void APIENTRY glDetachShader (GLuint program, GLuint shader);
|
||||||
|
GLAPI void APIENTRY glDisableVertexAttribArray (GLuint index);
|
||||||
|
GLAPI void APIENTRY glEnableVertexAttribArray (GLuint index);
|
||||||
|
GLAPI GLint APIENTRY glGetAttribLocation (GLuint program, const GLchar *name);
|
||||||
|
GLAPI void APIENTRY glGetProgramiv (GLuint program, GLenum pname, GLint *params);
|
||||||
|
GLAPI void APIENTRY glGetProgramInfoLog (GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
|
||||||
|
GLAPI void APIENTRY glGetShaderiv (GLuint shader, GLenum pname, GLint *params);
|
||||||
|
GLAPI void APIENTRY glGetShaderInfoLog (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
|
||||||
|
GLAPI GLint APIENTRY glGetUniformLocation (GLuint program, const GLchar *name);
|
||||||
|
GLAPI void APIENTRY glGetVertexAttribiv (GLuint index, GLenum pname, GLint *params);
|
||||||
|
GLAPI void APIENTRY glGetVertexAttribPointerv (GLuint index, GLenum pname, void **pointer);
|
||||||
|
GLAPI GLboolean APIENTRY glIsProgram (GLuint program);
|
||||||
|
GLAPI void APIENTRY glLinkProgram (GLuint program);
|
||||||
|
GLAPI void APIENTRY glShaderSource (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length);
|
||||||
|
GLAPI void APIENTRY glUseProgram (GLuint program);
|
||||||
|
GLAPI void APIENTRY glUniform1i (GLint location, GLint v0);
|
||||||
|
GLAPI void APIENTRY glUniformMatrix4fv (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
|
||||||
|
GLAPI void APIENTRY glVertexAttribPointer (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer);
|
||||||
|
#endif
|
||||||
|
#endif /* GL_VERSION_2_0 */
|
||||||
|
#ifndef GL_VERSION_2_1
|
||||||
|
#define GL_PIXEL_UNPACK_BUFFER 0x88EC
|
||||||
|
#define GL_PIXEL_UNPACK_BUFFER_BINDING 0x88EF
|
||||||
|
#endif /* GL_VERSION_2_1 */
|
||||||
|
#ifndef GL_VERSION_3_0
|
||||||
|
typedef khronos_uint16_t GLhalf;
|
||||||
|
#define GL_MAJOR_VERSION 0x821B
|
||||||
|
#define GL_MINOR_VERSION 0x821C
|
||||||
|
#define GL_NUM_EXTENSIONS 0x821D
|
||||||
|
#define GL_FRAMEBUFFER_SRGB 0x8DB9
|
||||||
|
#define GL_VERTEX_ARRAY_BINDING 0x85B5
|
||||||
|
typedef void (APIENTRYP PFNGLGETBOOLEANI_VPROC) (GLenum target, GLuint index, GLboolean *data);
|
||||||
|
typedef void (APIENTRYP PFNGLGETINTEGERI_VPROC) (GLenum target, GLuint index, GLint *data);
|
||||||
|
typedef const GLubyte *(APIENTRYP PFNGLGETSTRINGIPROC) (GLenum name, GLuint index);
|
||||||
|
typedef void (APIENTRYP PFNGLBINDVERTEXARRAYPROC) (GLuint array);
|
||||||
|
typedef void (APIENTRYP PFNGLDELETEVERTEXARRAYSPROC) (GLsizei n, const GLuint *arrays);
|
||||||
|
typedef void (APIENTRYP PFNGLGENVERTEXARRAYSPROC) (GLsizei n, GLuint *arrays);
|
||||||
|
#ifdef GL_GLEXT_PROTOTYPES
|
||||||
|
GLAPI const GLubyte *APIENTRY glGetStringi (GLenum name, GLuint index);
|
||||||
|
GLAPI void APIENTRY glBindVertexArray (GLuint array);
|
||||||
|
GLAPI void APIENTRY glDeleteVertexArrays (GLsizei n, const GLuint *arrays);
|
||||||
|
GLAPI void APIENTRY glGenVertexArrays (GLsizei n, GLuint *arrays);
|
||||||
|
#endif
|
||||||
|
#endif /* GL_VERSION_3_0 */
|
||||||
|
#ifndef GL_VERSION_3_1
|
||||||
|
#define GL_VERSION_3_1 1
|
||||||
|
#define GL_PRIMITIVE_RESTART 0x8F9D
|
||||||
|
#endif /* GL_VERSION_3_1 */
|
||||||
|
#ifndef GL_VERSION_3_2
|
||||||
|
#define GL_VERSION_3_2 1
|
||||||
|
typedef struct __GLsync *GLsync;
|
||||||
|
typedef khronos_uint64_t GLuint64;
|
||||||
|
typedef khronos_int64_t GLint64;
|
||||||
|
#define GL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
|
||||||
|
#define GL_CONTEXT_PROFILE_MASK 0x9126
|
||||||
|
typedef void (APIENTRYP PFNGLDRAWELEMENTSBASEVERTEXPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices, GLint basevertex);
|
||||||
|
typedef void (APIENTRYP PFNGLGETINTEGER64I_VPROC) (GLenum target, GLuint index, GLint64 *data);
|
||||||
|
#ifdef GL_GLEXT_PROTOTYPES
|
||||||
|
GLAPI void APIENTRY glDrawElementsBaseVertex (GLenum mode, GLsizei count, GLenum type, const void *indices, GLint basevertex);
|
||||||
|
#endif
|
||||||
|
#endif /* GL_VERSION_3_2 */
|
||||||
|
#ifndef GL_VERSION_3_3
|
||||||
|
#define GL_VERSION_3_3 1
|
||||||
|
#define GL_SAMPLER_BINDING 0x8919
|
||||||
|
typedef void (APIENTRYP PFNGLBINDSAMPLERPROC) (GLuint unit, GLuint sampler);
|
||||||
|
#ifdef GL_GLEXT_PROTOTYPES
|
||||||
|
GLAPI void APIENTRY glBindSampler (GLuint unit, GLuint sampler);
|
||||||
|
#endif
|
||||||
|
#endif /* GL_VERSION_3_3 */
|
||||||
|
#ifndef GL_VERSION_4_1
|
||||||
|
typedef void (APIENTRYP PFNGLGETFLOATI_VPROC) (GLenum target, GLuint index, GLfloat *data);
|
||||||
|
typedef void (APIENTRYP PFNGLGETDOUBLEI_VPROC) (GLenum target, GLuint index, GLdouble *data);
|
||||||
|
#endif /* GL_VERSION_4_1 */
|
||||||
|
#ifndef GL_VERSION_4_3
|
||||||
|
typedef void (APIENTRY *GLDEBUGPROC)(GLenum source,GLenum type,GLuint id,GLenum severity,GLsizei length,const GLchar *message,const void *userParam);
|
||||||
|
#endif /* GL_VERSION_4_3 */
|
||||||
|
#ifndef GL_VERSION_4_5
|
||||||
|
#define GL_CLIP_ORIGIN 0x935C
|
||||||
|
typedef void (APIENTRYP PFNGLGETTRANSFORMFEEDBACKI_VPROC) (GLuint xfb, GLenum pname, GLuint index, GLint *param);
|
||||||
|
typedef void (APIENTRYP PFNGLGETTRANSFORMFEEDBACKI64_VPROC) (GLuint xfb, GLenum pname, GLuint index, GLint64 *param);
|
||||||
|
#endif /* GL_VERSION_4_5 */
|
||||||
|
#ifndef GL_ARB_bindless_texture
|
||||||
|
typedef khronos_uint64_t GLuint64EXT;
|
||||||
|
#endif /* GL_ARB_bindless_texture */
|
||||||
|
#ifndef GL_ARB_cl_event
|
||||||
|
struct _cl_context;
|
||||||
|
struct _cl_event;
|
||||||
|
#endif /* GL_ARB_cl_event */
|
||||||
|
#ifndef GL_ARB_clip_control
|
||||||
|
#define GL_ARB_clip_control 1
|
||||||
|
#endif /* GL_ARB_clip_control */
|
||||||
|
#ifndef GL_ARB_debug_output
|
||||||
|
typedef void (APIENTRY *GLDEBUGPROCARB)(GLenum source,GLenum type,GLuint id,GLenum severity,GLsizei length,const GLchar *message,const void *userParam);
|
||||||
|
#endif /* GL_ARB_debug_output */
|
||||||
|
#ifndef GL_EXT_EGL_image_storage
|
||||||
|
typedef void *GLeglImageOES;
|
||||||
|
#endif /* GL_EXT_EGL_image_storage */
|
||||||
|
#ifndef GL_EXT_direct_state_access
|
||||||
|
typedef void (APIENTRYP PFNGLGETFLOATI_VEXTPROC) (GLenum pname, GLuint index, GLfloat *params);
|
||||||
|
typedef void (APIENTRYP PFNGLGETDOUBLEI_VEXTPROC) (GLenum pname, GLuint index, GLdouble *params);
|
||||||
|
typedef void (APIENTRYP PFNGLGETPOINTERI_VEXTPROC) (GLenum pname, GLuint index, void **params);
|
||||||
|
typedef void (APIENTRYP PFNGLGETVERTEXARRAYINTEGERI_VEXTPROC) (GLuint vaobj, GLuint index, GLenum pname, GLint *param);
|
||||||
|
typedef void (APIENTRYP PFNGLGETVERTEXARRAYPOINTERI_VEXTPROC) (GLuint vaobj, GLuint index, GLenum pname, void **param);
|
||||||
|
#endif /* GL_EXT_direct_state_access */
|
||||||
|
#ifndef GL_NV_draw_vulkan_image
|
||||||
|
typedef void (APIENTRY *GLVULKANPROCNV)(void);
|
||||||
|
#endif /* GL_NV_draw_vulkan_image */
|
||||||
|
#ifndef GL_NV_gpu_shader5
|
||||||
|
typedef khronos_int64_t GLint64EXT;
|
||||||
|
#endif /* GL_NV_gpu_shader5 */
|
||||||
|
#ifndef GL_NV_vertex_buffer_unified_memory
|
||||||
|
typedef void (APIENTRYP PFNGLGETINTEGERUI64I_VNVPROC) (GLenum value, GLuint index, GLuint64EXT *result);
|
||||||
|
#endif /* GL_NV_vertex_buffer_unified_memory */
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef GL3W_API
|
||||||
|
#define GL3W_API
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __gl_h_
|
||||||
|
#define __gl_h_
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define GL3W_OK 0
|
||||||
|
#define GL3W_ERROR_INIT -1
|
||||||
|
#define GL3W_ERROR_LIBRARY_OPEN -2
|
||||||
|
#define GL3W_ERROR_OPENGL_VERSION -3
|
||||||
|
|
||||||
|
typedef void (*GL3WglProc)(void);
|
||||||
|
typedef GL3WglProc (*GL3WGetProcAddressProc)(const char *proc);
|
||||||
|
|
||||||
|
/* gl3w api */
|
||||||
|
GL3W_API int imgl3wInit(void);
|
||||||
|
GL3W_API int imgl3wInit2(GL3WGetProcAddressProc proc);
|
||||||
|
GL3W_API int imgl3wIsSupported(int major, int minor);
|
||||||
|
GL3W_API GL3WglProc imgl3wGetProcAddress(const char *proc);
|
||||||
|
|
||||||
|
/* gl3w internal state */
|
||||||
|
union ImGL3WProcs {
|
||||||
|
GL3WglProc ptr[60];
|
||||||
|
struct {
|
||||||
|
PFNGLACTIVETEXTUREPROC ActiveTexture;
|
||||||
|
PFNGLATTACHSHADERPROC AttachShader;
|
||||||
|
PFNGLBINDBUFFERPROC BindBuffer;
|
||||||
|
PFNGLBINDSAMPLERPROC BindSampler;
|
||||||
|
PFNGLBINDTEXTUREPROC BindTexture;
|
||||||
|
PFNGLBINDVERTEXARRAYPROC BindVertexArray;
|
||||||
|
PFNGLBLENDEQUATIONPROC BlendEquation;
|
||||||
|
PFNGLBLENDEQUATIONSEPARATEPROC BlendEquationSeparate;
|
||||||
|
PFNGLBLENDFUNCSEPARATEPROC BlendFuncSeparate;
|
||||||
|
PFNGLBUFFERDATAPROC BufferData;
|
||||||
|
PFNGLBUFFERSUBDATAPROC BufferSubData;
|
||||||
|
PFNGLCLEARPROC Clear;
|
||||||
|
PFNGLCLEARCOLORPROC ClearColor;
|
||||||
|
PFNGLCOMPILESHADERPROC CompileShader;
|
||||||
|
PFNGLCREATEPROGRAMPROC CreateProgram;
|
||||||
|
PFNGLCREATESHADERPROC CreateShader;
|
||||||
|
PFNGLDELETEBUFFERSPROC DeleteBuffers;
|
||||||
|
PFNGLDELETEPROGRAMPROC DeleteProgram;
|
||||||
|
PFNGLDELETESHADERPROC DeleteShader;
|
||||||
|
PFNGLDELETETEXTURESPROC DeleteTextures;
|
||||||
|
PFNGLDELETEVERTEXARRAYSPROC DeleteVertexArrays;
|
||||||
|
PFNGLDETACHSHADERPROC DetachShader;
|
||||||
|
PFNGLDISABLEPROC Disable;
|
||||||
|
PFNGLDISABLEVERTEXATTRIBARRAYPROC DisableVertexAttribArray;
|
||||||
|
PFNGLDRAWELEMENTSPROC DrawElements;
|
||||||
|
PFNGLDRAWELEMENTSBASEVERTEXPROC DrawElementsBaseVertex;
|
||||||
|
PFNGLENABLEPROC Enable;
|
||||||
|
PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray;
|
||||||
|
PFNGLFLUSHPROC Flush;
|
||||||
|
PFNGLGENBUFFERSPROC GenBuffers;
|
||||||
|
PFNGLGENTEXTURESPROC GenTextures;
|
||||||
|
PFNGLGENVERTEXARRAYSPROC GenVertexArrays;
|
||||||
|
PFNGLGETATTRIBLOCATIONPROC GetAttribLocation;
|
||||||
|
PFNGLGETERRORPROC GetError;
|
||||||
|
PFNGLGETINTEGERVPROC GetIntegerv;
|
||||||
|
PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog;
|
||||||
|
PFNGLGETPROGRAMIVPROC GetProgramiv;
|
||||||
|
PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog;
|
||||||
|
PFNGLGETSHADERIVPROC GetShaderiv;
|
||||||
|
PFNGLGETSTRINGPROC GetString;
|
||||||
|
PFNGLGETSTRINGIPROC GetStringi;
|
||||||
|
PFNGLGETUNIFORMLOCATIONPROC GetUniformLocation;
|
||||||
|
PFNGLGETVERTEXATTRIBPOINTERVPROC GetVertexAttribPointerv;
|
||||||
|
PFNGLGETVERTEXATTRIBIVPROC GetVertexAttribiv;
|
||||||
|
PFNGLISENABLEDPROC IsEnabled;
|
||||||
|
PFNGLISPROGRAMPROC IsProgram;
|
||||||
|
PFNGLLINKPROGRAMPROC LinkProgram;
|
||||||
|
PFNGLPIXELSTOREIPROC PixelStorei;
|
||||||
|
PFNGLPOLYGONMODEPROC PolygonMode;
|
||||||
|
PFNGLREADPIXELSPROC ReadPixels;
|
||||||
|
PFNGLSCISSORPROC Scissor;
|
||||||
|
PFNGLSHADERSOURCEPROC ShaderSource;
|
||||||
|
PFNGLTEXIMAGE2DPROC TexImage2D;
|
||||||
|
PFNGLTEXPARAMETERIPROC TexParameteri;
|
||||||
|
PFNGLTEXSUBIMAGE2DPROC TexSubImage2D;
|
||||||
|
PFNGLUNIFORM1IPROC Uniform1i;
|
||||||
|
PFNGLUNIFORMMATRIX4FVPROC UniformMatrix4fv;
|
||||||
|
PFNGLUSEPROGRAMPROC UseProgram;
|
||||||
|
PFNGLVERTEXATTRIBPOINTERPROC VertexAttribPointer;
|
||||||
|
PFNGLVIEWPORTPROC Viewport;
|
||||||
|
} gl;
|
||||||
|
};
|
||||||
|
|
||||||
|
GL3W_API extern union ImGL3WProcs imgl3wProcs;
|
||||||
|
|
||||||
|
/* OpenGL functions */
|
||||||
|
#define glActiveTexture imgl3wProcs.gl.ActiveTexture
|
||||||
|
#define glAttachShader imgl3wProcs.gl.AttachShader
|
||||||
|
#define glBindBuffer imgl3wProcs.gl.BindBuffer
|
||||||
|
#define glBindSampler imgl3wProcs.gl.BindSampler
|
||||||
|
#define glBindTexture imgl3wProcs.gl.BindTexture
|
||||||
|
#define glBindVertexArray imgl3wProcs.gl.BindVertexArray
|
||||||
|
#define glBlendEquation imgl3wProcs.gl.BlendEquation
|
||||||
|
#define glBlendEquationSeparate imgl3wProcs.gl.BlendEquationSeparate
|
||||||
|
#define glBlendFuncSeparate imgl3wProcs.gl.BlendFuncSeparate
|
||||||
|
#define glBufferData imgl3wProcs.gl.BufferData
|
||||||
|
#define glBufferSubData imgl3wProcs.gl.BufferSubData
|
||||||
|
#define glClear imgl3wProcs.gl.Clear
|
||||||
|
#define glClearColor imgl3wProcs.gl.ClearColor
|
||||||
|
#define glCompileShader imgl3wProcs.gl.CompileShader
|
||||||
|
#define glCreateProgram imgl3wProcs.gl.CreateProgram
|
||||||
|
#define glCreateShader imgl3wProcs.gl.CreateShader
|
||||||
|
#define glDeleteBuffers imgl3wProcs.gl.DeleteBuffers
|
||||||
|
#define glDeleteProgram imgl3wProcs.gl.DeleteProgram
|
||||||
|
#define glDeleteShader imgl3wProcs.gl.DeleteShader
|
||||||
|
#define glDeleteTextures imgl3wProcs.gl.DeleteTextures
|
||||||
|
#define glDeleteVertexArrays imgl3wProcs.gl.DeleteVertexArrays
|
||||||
|
#define glDetachShader imgl3wProcs.gl.DetachShader
|
||||||
|
#define glDisable imgl3wProcs.gl.Disable
|
||||||
|
#define glDisableVertexAttribArray imgl3wProcs.gl.DisableVertexAttribArray
|
||||||
|
#define glDrawElements imgl3wProcs.gl.DrawElements
|
||||||
|
#define glDrawElementsBaseVertex imgl3wProcs.gl.DrawElementsBaseVertex
|
||||||
|
#define glEnable imgl3wProcs.gl.Enable
|
||||||
|
#define glEnableVertexAttribArray imgl3wProcs.gl.EnableVertexAttribArray
|
||||||
|
#define glFlush imgl3wProcs.gl.Flush
|
||||||
|
#define glGenBuffers imgl3wProcs.gl.GenBuffers
|
||||||
|
#define glGenTextures imgl3wProcs.gl.GenTextures
|
||||||
|
#define glGenVertexArrays imgl3wProcs.gl.GenVertexArrays
|
||||||
|
#define glGetAttribLocation imgl3wProcs.gl.GetAttribLocation
|
||||||
|
#define glGetError imgl3wProcs.gl.GetError
|
||||||
|
#define glGetIntegerv imgl3wProcs.gl.GetIntegerv
|
||||||
|
#define glGetProgramInfoLog imgl3wProcs.gl.GetProgramInfoLog
|
||||||
|
#define glGetProgramiv imgl3wProcs.gl.GetProgramiv
|
||||||
|
#define glGetShaderInfoLog imgl3wProcs.gl.GetShaderInfoLog
|
||||||
|
#define glGetShaderiv imgl3wProcs.gl.GetShaderiv
|
||||||
|
#define glGetString imgl3wProcs.gl.GetString
|
||||||
|
#define glGetStringi imgl3wProcs.gl.GetStringi
|
||||||
|
#define glGetUniformLocation imgl3wProcs.gl.GetUniformLocation
|
||||||
|
#define glGetVertexAttribPointerv imgl3wProcs.gl.GetVertexAttribPointerv
|
||||||
|
#define glGetVertexAttribiv imgl3wProcs.gl.GetVertexAttribiv
|
||||||
|
#define glIsEnabled imgl3wProcs.gl.IsEnabled
|
||||||
|
#define glIsProgram imgl3wProcs.gl.IsProgram
|
||||||
|
#define glLinkProgram imgl3wProcs.gl.LinkProgram
|
||||||
|
#define glPixelStorei imgl3wProcs.gl.PixelStorei
|
||||||
|
#define glPolygonMode imgl3wProcs.gl.PolygonMode
|
||||||
|
#define glReadPixels imgl3wProcs.gl.ReadPixels
|
||||||
|
#define glScissor imgl3wProcs.gl.Scissor
|
||||||
|
#define glShaderSource imgl3wProcs.gl.ShaderSource
|
||||||
|
#define glTexImage2D imgl3wProcs.gl.TexImage2D
|
||||||
|
#define glTexParameteri imgl3wProcs.gl.TexParameteri
|
||||||
|
#define glTexSubImage2D imgl3wProcs.gl.TexSubImage2D
|
||||||
|
#define glUniform1i imgl3wProcs.gl.Uniform1i
|
||||||
|
#define glUniformMatrix4fv imgl3wProcs.gl.UniformMatrix4fv
|
||||||
|
#define glUseProgram imgl3wProcs.gl.UseProgram
|
||||||
|
#define glVertexAttribPointer imgl3wProcs.gl.VertexAttribPointer
|
||||||
|
#define glViewport imgl3wProcs.gl.Viewport
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef IMGL3W_IMPL
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define GL3W_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN 1
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
static HMODULE libgl;
|
||||||
|
typedef PROC(__stdcall* GL3WglGetProcAddr)(LPCSTR);
|
||||||
|
static GL3WglGetProcAddr wgl_get_proc_address;
|
||||||
|
|
||||||
|
static int open_libgl(void)
|
||||||
|
{
|
||||||
|
libgl = LoadLibraryA("opengl32.dll");
|
||||||
|
if (!libgl)
|
||||||
|
return GL3W_ERROR_LIBRARY_OPEN;
|
||||||
|
wgl_get_proc_address = (GL3WglGetProcAddr)GetProcAddress(libgl, "wglGetProcAddress");
|
||||||
|
return GL3W_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void close_libgl(void) { FreeLibrary(libgl); }
|
||||||
|
static GL3WglProc get_proc(const char *proc)
|
||||||
|
{
|
||||||
|
GL3WglProc res;
|
||||||
|
res = (GL3WglProc)wgl_get_proc_address(proc);
|
||||||
|
if (!res)
|
||||||
|
res = (GL3WglProc)GetProcAddress(libgl, proc);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
static void *libgl;
|
||||||
|
static int open_libgl(void)
|
||||||
|
{
|
||||||
|
libgl = dlopen("/System/Library/Frameworks/OpenGL.framework/OpenGL", RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
if (!libgl)
|
||||||
|
return GL3W_ERROR_LIBRARY_OPEN;
|
||||||
|
return GL3W_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void close_libgl(void) { dlclose(libgl); }
|
||||||
|
|
||||||
|
static GL3WglProc get_proc(const char *proc)
|
||||||
|
{
|
||||||
|
GL3WglProc res;
|
||||||
|
*(void **)(&res) = dlsym(libgl, proc);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
static void* libgl; // OpenGL library
|
||||||
|
static void* libglx; // GLX library
|
||||||
|
static void* libegl; // EGL library
|
||||||
|
static GL3WGetProcAddressProc gl_get_proc_address;
|
||||||
|
|
||||||
|
static void close_libgl(void)
|
||||||
|
{
|
||||||
|
if (libgl) {
|
||||||
|
dlclose(libgl);
|
||||||
|
libgl = NULL;
|
||||||
|
}
|
||||||
|
if (libegl) {
|
||||||
|
dlclose(libegl);
|
||||||
|
libegl = NULL;
|
||||||
|
}
|
||||||
|
if (libglx) {
|
||||||
|
dlclose(libglx);
|
||||||
|
libglx = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int is_library_loaded(const char* name, void** lib)
|
||||||
|
{
|
||||||
|
*lib = dlopen(name, RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
|
||||||
|
return *lib != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int open_libs(void)
|
||||||
|
{
|
||||||
|
// On Linux we have two APIs to get process addresses: EGL and GLX.
|
||||||
|
// EGL is supported under both X11 and Wayland, whereas GLX is X11-specific.
|
||||||
|
|
||||||
|
libgl = NULL;
|
||||||
|
libegl = NULL;
|
||||||
|
libglx = NULL;
|
||||||
|
|
||||||
|
// First check what's already loaded, the windowing library might have
|
||||||
|
// already loaded either EGL or GLX and we want to use the same one.
|
||||||
|
|
||||||
|
if (is_library_loaded("libEGL.so.1", &libegl) ||
|
||||||
|
is_library_loaded("libGLX.so.0", &libglx)) {
|
||||||
|
libgl = dlopen("libOpenGL.so.0", RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
if (libgl)
|
||||||
|
return GL3W_OK;
|
||||||
|
else
|
||||||
|
close_libgl();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_library_loaded("libGL.so", &libgl))
|
||||||
|
return GL3W_OK;
|
||||||
|
if (is_library_loaded("libGL.so.1", &libgl))
|
||||||
|
return GL3W_OK;
|
||||||
|
if (is_library_loaded("libGL.so.3", &libgl))
|
||||||
|
return GL3W_OK;
|
||||||
|
|
||||||
|
// Neither is already loaded, so we have to load one. Try EGL first
|
||||||
|
// because it is supported under both X11 and Wayland.
|
||||||
|
|
||||||
|
// Load OpenGL + EGL
|
||||||
|
libgl = dlopen("libOpenGL.so.0", RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
libegl = dlopen("libEGL.so.1", RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
if (libgl && libegl)
|
||||||
|
return GL3W_OK;
|
||||||
|
else
|
||||||
|
close_libgl();
|
||||||
|
|
||||||
|
// Fall back to legacy libGL, which includes GLX
|
||||||
|
// While most systems use libGL.so.1, NetBSD seems to use that libGL.so.3. See https://github.com/ocornut/imgui/issues/6983
|
||||||
|
libgl = dlopen("libGL.so", RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
if (!libgl)
|
||||||
|
libgl = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
if (!libgl)
|
||||||
|
libgl = dlopen("libGL.so.3", RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
|
||||||
|
if (libgl)
|
||||||
|
return GL3W_OK;
|
||||||
|
|
||||||
|
return GL3W_ERROR_LIBRARY_OPEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int open_libgl(void)
|
||||||
|
{
|
||||||
|
int res = open_libs();
|
||||||
|
if (res)
|
||||||
|
return res;
|
||||||
|
|
||||||
|
if (libegl)
|
||||||
|
*(void**)(&gl_get_proc_address) = dlsym(libegl, "eglGetProcAddress");
|
||||||
|
else if (libglx)
|
||||||
|
*(void**)(&gl_get_proc_address) = dlsym(libglx, "glXGetProcAddressARB");
|
||||||
|
else
|
||||||
|
*(void**)(&gl_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB");
|
||||||
|
|
||||||
|
if (!gl_get_proc_address) {
|
||||||
|
close_libgl();
|
||||||
|
return GL3W_ERROR_LIBRARY_OPEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
return GL3W_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static GL3WglProc get_proc(const char* proc)
|
||||||
|
{
|
||||||
|
GL3WglProc res = NULL;
|
||||||
|
|
||||||
|
// Before EGL version 1.5, eglGetProcAddress doesn't support querying core
|
||||||
|
// functions and may return a dummy function if we try, so try to load the
|
||||||
|
// function from the GL library directly first.
|
||||||
|
if (libegl)
|
||||||
|
*(void**)(&res) = dlsym(libgl, proc);
|
||||||
|
|
||||||
|
if (!res)
|
||||||
|
res = gl_get_proc_address(proc);
|
||||||
|
|
||||||
|
if (!libegl && !res)
|
||||||
|
*(void**)(&res) = dlsym(libgl, proc);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static struct { int major, minor; } version;
|
||||||
|
|
||||||
|
static int parse_version(void)
|
||||||
|
{
|
||||||
|
if (!glGetIntegerv)
|
||||||
|
return GL3W_ERROR_INIT;
|
||||||
|
glGetIntegerv(GL_MAJOR_VERSION, &version.major);
|
||||||
|
glGetIntegerv(GL_MINOR_VERSION, &version.minor);
|
||||||
|
if (version.major == 0 && version.minor == 0)
|
||||||
|
{
|
||||||
|
// Query GL_VERSION in desktop GL 2.x, the string will start with "<major>.<minor>"
|
||||||
|
if (const char* gl_version = (const char*)glGetString(GL_VERSION))
|
||||||
|
sscanf(gl_version, "%d.%d", &version.major, &version.minor);
|
||||||
|
}
|
||||||
|
if (version.major < 2)
|
||||||
|
return GL3W_ERROR_OPENGL_VERSION;
|
||||||
|
return GL3W_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void load_procs(GL3WGetProcAddressProc proc);
|
||||||
|
|
||||||
|
int imgl3wInit(void)
|
||||||
|
{
|
||||||
|
int res = open_libgl();
|
||||||
|
if (res)
|
||||||
|
return res;
|
||||||
|
atexit(close_libgl);
|
||||||
|
return imgl3wInit2(get_proc);
|
||||||
|
}
|
||||||
|
|
||||||
|
int imgl3wInit2(GL3WGetProcAddressProc proc)
|
||||||
|
{
|
||||||
|
load_procs(proc);
|
||||||
|
return parse_version();
|
||||||
|
}
|
||||||
|
|
||||||
|
int imgl3wIsSupported(int major, int minor)
|
||||||
|
{
|
||||||
|
if (major < 2)
|
||||||
|
return 0;
|
||||||
|
if (version.major == major)
|
||||||
|
return version.minor >= minor;
|
||||||
|
return version.major >= major;
|
||||||
|
}
|
||||||
|
|
||||||
|
GL3WglProc imgl3wGetProcAddress(const char *proc) { return get_proc(proc); }
|
||||||
|
|
||||||
|
static const char *proc_names[] = {
|
||||||
|
"glActiveTexture",
|
||||||
|
"glAttachShader",
|
||||||
|
"glBindBuffer",
|
||||||
|
"glBindSampler",
|
||||||
|
"glBindTexture",
|
||||||
|
"glBindVertexArray",
|
||||||
|
"glBlendEquation",
|
||||||
|
"glBlendEquationSeparate",
|
||||||
|
"glBlendFuncSeparate",
|
||||||
|
"glBufferData",
|
||||||
|
"glBufferSubData",
|
||||||
|
"glClear",
|
||||||
|
"glClearColor",
|
||||||
|
"glCompileShader",
|
||||||
|
"glCreateProgram",
|
||||||
|
"glCreateShader",
|
||||||
|
"glDeleteBuffers",
|
||||||
|
"glDeleteProgram",
|
||||||
|
"glDeleteShader",
|
||||||
|
"glDeleteTextures",
|
||||||
|
"glDeleteVertexArrays",
|
||||||
|
"glDetachShader",
|
||||||
|
"glDisable",
|
||||||
|
"glDisableVertexAttribArray",
|
||||||
|
"glDrawElements",
|
||||||
|
"glDrawElementsBaseVertex",
|
||||||
|
"glEnable",
|
||||||
|
"glEnableVertexAttribArray",
|
||||||
|
"glFlush",
|
||||||
|
"glGenBuffers",
|
||||||
|
"glGenTextures",
|
||||||
|
"glGenVertexArrays",
|
||||||
|
"glGetAttribLocation",
|
||||||
|
"glGetError",
|
||||||
|
"glGetIntegerv",
|
||||||
|
"glGetProgramInfoLog",
|
||||||
|
"glGetProgramiv",
|
||||||
|
"glGetShaderInfoLog",
|
||||||
|
"glGetShaderiv",
|
||||||
|
"glGetString",
|
||||||
|
"glGetStringi",
|
||||||
|
"glGetUniformLocation",
|
||||||
|
"glGetVertexAttribPointerv",
|
||||||
|
"glGetVertexAttribiv",
|
||||||
|
"glIsEnabled",
|
||||||
|
"glIsProgram",
|
||||||
|
"glLinkProgram",
|
||||||
|
"glPixelStorei",
|
||||||
|
"glPolygonMode",
|
||||||
|
"glReadPixels",
|
||||||
|
"glScissor",
|
||||||
|
"glShaderSource",
|
||||||
|
"glTexImage2D",
|
||||||
|
"glTexParameteri",
|
||||||
|
"glTexSubImage2D",
|
||||||
|
"glUniform1i",
|
||||||
|
"glUniformMatrix4fv",
|
||||||
|
"glUseProgram",
|
||||||
|
"glVertexAttribPointer",
|
||||||
|
"glViewport",
|
||||||
|
};
|
||||||
|
|
||||||
|
GL3W_API union ImGL3WProcs imgl3wProcs;
|
||||||
|
|
||||||
|
static void load_procs(GL3WGetProcAddressProc proc)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < GL3W_ARRAY_SIZE(proc_names); i++)
|
||||||
|
imgl3wProcs.ptr[i] = proc(proc_names[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
54
imgui/backends/imgui_impl_osx.h
Normal file
54
imgui/backends/imgui_impl_osx.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
// dear imgui: Platform Backend for OSX / Cocoa
|
||||||
|
// This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan, Metal..)
|
||||||
|
// - Not well tested. If you want a portable application, prefer using the GLFW or SDL platform Backends on Mac.
|
||||||
|
// - Requires linking with the GameController framework ("-framework GameController").
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Clipboard support is part of core Dear ImGui (no specific code in this backend).
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/Pen.
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy kVK_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Gamepad support.
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
// [X] Platform: IME support.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
#ifdef __OBJC__
|
||||||
|
|
||||||
|
@class NSEvent;
|
||||||
|
@class NSView;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplOSX_Init(NSView* _Nonnull view);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOSX_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOSX_NewFrame(NSView* _Nullable view);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// C++ API
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifdef IMGUI_IMPL_METAL_CPP_EXTENSIONS
|
||||||
|
// #include <AppKit/AppKit.hpp>
|
||||||
|
#ifndef __OBJC__
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplOSX_Init(void* _Nonnull view);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOSX_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOSX_NewFrame(void* _Nullable view);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
832
imgui/backends/imgui_impl_osx.mm
Normal file
832
imgui/backends/imgui_impl_osx.mm
Normal file
@@ -0,0 +1,832 @@
|
|||||||
|
// dear imgui: Platform Backend for OSX / Cocoa
|
||||||
|
// This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan, Metal..)
|
||||||
|
// - Not well tested. If you want a portable application, prefer using the GLFW or SDL platform Backends on Mac.
|
||||||
|
// - Requires linking with the GameController framework ("-framework GameController").
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Clipboard support is part of core Dear ImGui (no specific code in this backend).
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/Pen.
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy kVK_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Gamepad support.
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
// [X] Platform: IME support.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#import "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#import "imgui_impl_osx.h"
|
||||||
|
#import <Cocoa/Cocoa.h>
|
||||||
|
#import <Carbon/Carbon.h>
|
||||||
|
#import <GameController/GameController.h>
|
||||||
|
#import <time.h>
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-06-27: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support.
|
||||||
|
// 2025-06-12: ImGui_ImplOSX_HandleEvent() only process event for window containing our view. (#8644)
|
||||||
|
// 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set.
|
||||||
|
// 2025-01-20: Removed notification observer when shutting down. (#8331)
|
||||||
|
// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
|
||||||
|
// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
|
||||||
|
// - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
|
||||||
|
// - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn
|
||||||
|
// 2024-07-02: Update for io.SetPlatformImeDataFn() -> io.PlatformSetImeDataFn() renaming in main library.
|
||||||
|
// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F20 function keys. Stopped mapping F13 into PrintScreen.
|
||||||
|
// 2023-04-09: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_Pen.
|
||||||
|
// 2023-02-01: Fixed scroll wheel scaling for devices emitting events with hasPreciseScrollingDeltas==false (e.g. non-Apple mice).
|
||||||
|
// 2022-11-02: Fixed mouse coordinates before clicking the host window.
|
||||||
|
// 2022-10-06: Fixed mouse inputs on flipped views.
|
||||||
|
// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
|
||||||
|
// 2022-05-03: Inputs: Removed ImGui_ImplOSX_HandleEvent() from backend API in favor of backend automatically handling event capture.
|
||||||
|
// 2022-04-27: Misc: Store backend data in a per-context struct, allowing to use this backend with multiple contexts.
|
||||||
|
// 2022-03-22: Inputs: Monitor NSKeyUp events to catch missing keyUp for key when user press Cmd + key
|
||||||
|
// 2022-02-07: Inputs: Forward keyDown/keyUp events to OS when unused by dear imgui.
|
||||||
|
// 2022-01-31: Fixed building with old Xcode versions that are missing gamepad features.
|
||||||
|
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
|
||||||
|
// 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
|
||||||
|
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
|
||||||
|
// 2022-01-12: Inputs: Added basic Platform IME support, hooking the io.SetPlatformImeDataFn() function.
|
||||||
|
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
|
||||||
|
// 2021-12-13: *BREAKING CHANGE* Add NSView parameter to ImGui_ImplOSX_Init(). Generally fix keyboard support. Using kVK_* codes for keyboard keys.
|
||||||
|
// 2021-12-13: Add game controller support.
|
||||||
|
// 2021-09-21: Use mach_absolute_time as CFAbsoluteTimeGetCurrent can jump backwards.
|
||||||
|
// 2021-08-17: Calling io.AddFocusEvent() on NSApplicationDidBecomeActiveNotification/NSApplicationDidResignActiveNotification events.
|
||||||
|
// 2021-06-23: Inputs: Added a fix for shortcuts using CTRL key instead of CMD key.
|
||||||
|
// 2021-04-19: Inputs: Added a fix for keys remaining stuck in pressed state when CMD-tabbing into different application.
|
||||||
|
// 2021-01-27: Inputs: Added a fix for mouse position not being reported when mouse buttons other than left one are down.
|
||||||
|
// 2020-10-28: Inputs: Added a fix for handling keypad-enter key.
|
||||||
|
// 2020-05-25: Inputs: Added a fix for missing trackpad clicks when done with "soft tap".
|
||||||
|
// 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
|
||||||
|
// 2019-10-11: Inputs: Fix using Backspace key.
|
||||||
|
// 2019-07-21: Re-added clipboard handlers as they are not enabled by default in core imgui.cpp (reverted 2019-05-18 change).
|
||||||
|
// 2019-05-28: Inputs: Added mouse cursor shape and visibility support.
|
||||||
|
// 2019-05-18: Misc: Removed clipboard handlers as they are now supported by core imgui.cpp.
|
||||||
|
// 2019-05-11: Inputs: Don't filter character values before calling AddInputCharacter() apart from 0xF700..0xFFFF range.
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
|
||||||
|
// 2018-07-07: Initial version.
|
||||||
|
|
||||||
|
#define APPLE_HAS_BUTTON_OPTIONS (__IPHONE_OS_VERSION_MIN_REQUIRED >= 130000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500 || __TV_OS_VERSION_MIN_REQUIRED >= 130000)
|
||||||
|
#define APPLE_HAS_CONTROLLER (__IPHONE_OS_VERSION_MIN_REQUIRED >= 140000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 110000 || __TV_OS_VERSION_MIN_REQUIRED >= 140000)
|
||||||
|
#define APPLE_HAS_THUMBSTICKS (__IPHONE_OS_VERSION_MIN_REQUIRED >= 120100 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101401 || __TV_OS_VERSION_MIN_REQUIRED >= 120100)
|
||||||
|
|
||||||
|
@class ImGuiObserver;
|
||||||
|
@class KeyEventResponder;
|
||||||
|
|
||||||
|
// Data
|
||||||
|
struct ImGui_ImplOSX_Data
|
||||||
|
{
|
||||||
|
CFTimeInterval Time;
|
||||||
|
NSCursor* MouseCursors[ImGuiMouseCursor_COUNT];
|
||||||
|
bool MouseCursorHidden;
|
||||||
|
ImGuiObserver* Observer;
|
||||||
|
KeyEventResponder* KeyEventResponder;
|
||||||
|
NSTextInputContext* InputContext;
|
||||||
|
id Monitor;
|
||||||
|
NSWindow* Window;
|
||||||
|
|
||||||
|
ImGui_ImplOSX_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
static ImGui_ImplOSX_Data* ImGui_ImplOSX_GetBackendData() { return (ImGui_ImplOSX_Data*)ImGui::GetIO().BackendPlatformUserData; }
|
||||||
|
static void ImGui_ImplOSX_DestroyBackendData() { IM_DELETE(ImGui_ImplOSX_GetBackendData()); }
|
||||||
|
|
||||||
|
static inline CFTimeInterval GetMachAbsoluteTimeInSeconds() { return (CFTimeInterval)(double)(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1e9); }
|
||||||
|
|
||||||
|
// Forward Declarations
|
||||||
|
static void ImGui_ImplOSX_AddTrackingArea(NSView* _Nonnull view);
|
||||||
|
static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view);
|
||||||
|
|
||||||
|
// Undocumented methods for creating cursors.
|
||||||
|
@interface NSCursor()
|
||||||
|
+ (id)_windowResizeNorthWestSouthEastCursor;
|
||||||
|
+ (id)_windowResizeNorthEastSouthWestCursor;
|
||||||
|
+ (id)_windowResizeNorthSouthCursor;
|
||||||
|
+ (id)_windowResizeEastWestCursor;
|
||||||
|
+ (id)busyButClickableCursor;
|
||||||
|
@end
|
||||||
|
|
||||||
|
/**
|
||||||
|
KeyEventResponder implements the NSTextInputClient protocol as is required by the macOS text input manager.
|
||||||
|
|
||||||
|
The macOS text input manager is invoked by calling the interpretKeyEvents method from the keyDown method.
|
||||||
|
Keyboard events are then evaluated by the macOS input manager and valid text input is passed back via the
|
||||||
|
insertText:replacementRange method.
|
||||||
|
|
||||||
|
This is the same approach employed by other cross-platform libraries such as SDL2:
|
||||||
|
https://github.com/spurious/SDL-mirror/blob/e17aacbd09e65a4fd1e166621e011e581fb017a8/src/video/cocoa/SDL_cocoakeyboard.m#L53
|
||||||
|
and GLFW:
|
||||||
|
https://github.com/glfw/glfw/blob/b55a517ae0c7b5127dffa79a64f5406021bf9076/src/cocoa_window.m#L722-L723
|
||||||
|
*/
|
||||||
|
@interface KeyEventResponder: NSView<NSTextInputClient>
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation KeyEventResponder
|
||||||
|
{
|
||||||
|
float _posX;
|
||||||
|
float _posY;
|
||||||
|
NSRect _imeRect;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - Public
|
||||||
|
|
||||||
|
- (void)setImePosX:(float)posX imePosY:(float)posY
|
||||||
|
{
|
||||||
|
_posX = posX;
|
||||||
|
_posY = posY;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)updateImePosWithView:(NSView *)view
|
||||||
|
{
|
||||||
|
NSWindow* window = view.window;
|
||||||
|
if (!window)
|
||||||
|
return;
|
||||||
|
NSRect contentRect = [window contentRectForFrameRect:window.frame];
|
||||||
|
NSRect rect = NSMakeRect(_posX, contentRect.size.height - _posY, 0, 0);
|
||||||
|
_imeRect = [window convertRectToScreen:rect];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)viewDidMoveToWindow
|
||||||
|
{
|
||||||
|
// Ensure self is a first responder to receive the input events.
|
||||||
|
[self.window makeFirstResponder:self];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)keyDown:(NSEvent*)event
|
||||||
|
{
|
||||||
|
if (!ImGui_ImplOSX_HandleEvent(event, self))
|
||||||
|
[super keyDown:event];
|
||||||
|
|
||||||
|
// Call to the macOS input manager system.
|
||||||
|
[self interpretKeyEvents:@[event]];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)keyUp:(NSEvent*)event
|
||||||
|
{
|
||||||
|
if (!ImGui_ImplOSX_HandleEvent(event, self))
|
||||||
|
[super keyUp:event];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)insertText:(id)aString replacementRange:(NSRange)replacementRange
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
NSString* characters;
|
||||||
|
if ([aString isKindOfClass:[NSAttributedString class]])
|
||||||
|
characters = [aString string];
|
||||||
|
else
|
||||||
|
characters = (NSString*)aString;
|
||||||
|
|
||||||
|
io.AddInputCharactersUTF8(characters.UTF8String);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)acceptsFirstResponder
|
||||||
|
{
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)doCommandBySelector:(SEL)myselector
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
- (nullable NSAttributedString*)attributedSubstringForProposedRange:(NSRange)range actualRange:(nullable NSRangePointer)actualRange
|
||||||
|
{
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSUInteger)characterIndexForPoint:(NSPoint)point
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(nullable NSRangePointer)actualRange
|
||||||
|
{
|
||||||
|
return _imeRect;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)hasMarkedText
|
||||||
|
{
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSRange)markedRange
|
||||||
|
{
|
||||||
|
return NSMakeRange(NSNotFound, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSRange)selectedRange
|
||||||
|
{
|
||||||
|
return NSMakeRange(NSNotFound, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)setMarkedText:(nonnull id)string selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)unmarkText
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
- (nonnull NSArray<NSAttributedStringKey>*)validAttributesForMarkedText
|
||||||
|
{
|
||||||
|
return @[];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@interface ImGuiObserver : NSObject
|
||||||
|
|
||||||
|
- (void)onApplicationBecomeActive:(NSNotification*)aNotification;
|
||||||
|
- (void)onApplicationBecomeInactive:(NSNotification*)aNotification;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation ImGuiObserver
|
||||||
|
|
||||||
|
- (void)onApplicationBecomeActive:(NSNotification*)aNotification
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.AddFocusEvent(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)onApplicationBecomeInactive:(NSNotification*)aNotification
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.AddFocusEvent(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
// Not static to allow third-party code to use that if they want to (but undocumented)
|
||||||
|
ImGuiKey ImGui_ImplOSX_KeyCodeToImGuiKey(int key_code);
|
||||||
|
ImGuiKey ImGui_ImplOSX_KeyCodeToImGuiKey(int key_code)
|
||||||
|
{
|
||||||
|
switch (key_code)
|
||||||
|
{
|
||||||
|
case kVK_ANSI_A: return ImGuiKey_A;
|
||||||
|
case kVK_ANSI_S: return ImGuiKey_S;
|
||||||
|
case kVK_ANSI_D: return ImGuiKey_D;
|
||||||
|
case kVK_ANSI_F: return ImGuiKey_F;
|
||||||
|
case kVK_ANSI_H: return ImGuiKey_H;
|
||||||
|
case kVK_ANSI_G: return ImGuiKey_G;
|
||||||
|
case kVK_ANSI_Z: return ImGuiKey_Z;
|
||||||
|
case kVK_ANSI_X: return ImGuiKey_X;
|
||||||
|
case kVK_ANSI_C: return ImGuiKey_C;
|
||||||
|
case kVK_ANSI_V: return ImGuiKey_V;
|
||||||
|
case kVK_ANSI_B: return ImGuiKey_B;
|
||||||
|
case kVK_ANSI_Q: return ImGuiKey_Q;
|
||||||
|
case kVK_ANSI_W: return ImGuiKey_W;
|
||||||
|
case kVK_ANSI_E: return ImGuiKey_E;
|
||||||
|
case kVK_ANSI_R: return ImGuiKey_R;
|
||||||
|
case kVK_ANSI_Y: return ImGuiKey_Y;
|
||||||
|
case kVK_ANSI_T: return ImGuiKey_T;
|
||||||
|
case kVK_ANSI_1: return ImGuiKey_1;
|
||||||
|
case kVK_ANSI_2: return ImGuiKey_2;
|
||||||
|
case kVK_ANSI_3: return ImGuiKey_3;
|
||||||
|
case kVK_ANSI_4: return ImGuiKey_4;
|
||||||
|
case kVK_ANSI_6: return ImGuiKey_6;
|
||||||
|
case kVK_ANSI_5: return ImGuiKey_5;
|
||||||
|
case kVK_ANSI_Equal: return ImGuiKey_Equal;
|
||||||
|
case kVK_ANSI_9: return ImGuiKey_9;
|
||||||
|
case kVK_ANSI_7: return ImGuiKey_7;
|
||||||
|
case kVK_ANSI_Minus: return ImGuiKey_Minus;
|
||||||
|
case kVK_ANSI_8: return ImGuiKey_8;
|
||||||
|
case kVK_ANSI_0: return ImGuiKey_0;
|
||||||
|
case kVK_ANSI_RightBracket: return ImGuiKey_RightBracket;
|
||||||
|
case kVK_ANSI_O: return ImGuiKey_O;
|
||||||
|
case kVK_ANSI_U: return ImGuiKey_U;
|
||||||
|
case kVK_ANSI_LeftBracket: return ImGuiKey_LeftBracket;
|
||||||
|
case kVK_ANSI_I: return ImGuiKey_I;
|
||||||
|
case kVK_ANSI_P: return ImGuiKey_P;
|
||||||
|
case kVK_ANSI_L: return ImGuiKey_L;
|
||||||
|
case kVK_ANSI_J: return ImGuiKey_J;
|
||||||
|
case kVK_ANSI_Quote: return ImGuiKey_Apostrophe;
|
||||||
|
case kVK_ANSI_K: return ImGuiKey_K;
|
||||||
|
case kVK_ANSI_Semicolon: return ImGuiKey_Semicolon;
|
||||||
|
case kVK_ANSI_Backslash: return ImGuiKey_Backslash;
|
||||||
|
case kVK_ANSI_Comma: return ImGuiKey_Comma;
|
||||||
|
case kVK_ANSI_Slash: return ImGuiKey_Slash;
|
||||||
|
case kVK_ANSI_N: return ImGuiKey_N;
|
||||||
|
case kVK_ANSI_M: return ImGuiKey_M;
|
||||||
|
case kVK_ANSI_Period: return ImGuiKey_Period;
|
||||||
|
case kVK_ANSI_Grave: return ImGuiKey_GraveAccent;
|
||||||
|
case kVK_ANSI_KeypadDecimal: return ImGuiKey_KeypadDecimal;
|
||||||
|
case kVK_ANSI_KeypadMultiply: return ImGuiKey_KeypadMultiply;
|
||||||
|
case kVK_ANSI_KeypadPlus: return ImGuiKey_KeypadAdd;
|
||||||
|
case kVK_ANSI_KeypadClear: return ImGuiKey_NumLock;
|
||||||
|
case kVK_ANSI_KeypadDivide: return ImGuiKey_KeypadDivide;
|
||||||
|
case kVK_ANSI_KeypadEnter: return ImGuiKey_KeypadEnter;
|
||||||
|
case kVK_ANSI_KeypadMinus: return ImGuiKey_KeypadSubtract;
|
||||||
|
case kVK_ANSI_KeypadEquals: return ImGuiKey_KeypadEqual;
|
||||||
|
case kVK_ANSI_Keypad0: return ImGuiKey_Keypad0;
|
||||||
|
case kVK_ANSI_Keypad1: return ImGuiKey_Keypad1;
|
||||||
|
case kVK_ANSI_Keypad2: return ImGuiKey_Keypad2;
|
||||||
|
case kVK_ANSI_Keypad3: return ImGuiKey_Keypad3;
|
||||||
|
case kVK_ANSI_Keypad4: return ImGuiKey_Keypad4;
|
||||||
|
case kVK_ANSI_Keypad5: return ImGuiKey_Keypad5;
|
||||||
|
case kVK_ANSI_Keypad6: return ImGuiKey_Keypad6;
|
||||||
|
case kVK_ANSI_Keypad7: return ImGuiKey_Keypad7;
|
||||||
|
case kVK_ANSI_Keypad8: return ImGuiKey_Keypad8;
|
||||||
|
case kVK_ANSI_Keypad9: return ImGuiKey_Keypad9;
|
||||||
|
case kVK_Return: return ImGuiKey_Enter;
|
||||||
|
case kVK_Tab: return ImGuiKey_Tab;
|
||||||
|
case kVK_Space: return ImGuiKey_Space;
|
||||||
|
case kVK_Delete: return ImGuiKey_Backspace;
|
||||||
|
case kVK_Escape: return ImGuiKey_Escape;
|
||||||
|
case kVK_CapsLock: return ImGuiKey_CapsLock;
|
||||||
|
case kVK_Control: return ImGuiKey_LeftCtrl;
|
||||||
|
case kVK_Shift: return ImGuiKey_LeftShift;
|
||||||
|
case kVK_Option: return ImGuiKey_LeftAlt;
|
||||||
|
case kVK_Command: return ImGuiKey_LeftSuper;
|
||||||
|
case kVK_RightControl: return ImGuiKey_RightCtrl;
|
||||||
|
case kVK_RightShift: return ImGuiKey_RightShift;
|
||||||
|
case kVK_RightOption: return ImGuiKey_RightAlt;
|
||||||
|
case kVK_RightCommand: return ImGuiKey_RightSuper;
|
||||||
|
// case kVK_Function: return ImGuiKey_;
|
||||||
|
// case kVK_VolumeUp: return ImGuiKey_;
|
||||||
|
// case kVK_VolumeDown: return ImGuiKey_;
|
||||||
|
// case kVK_Mute: return ImGuiKey_;
|
||||||
|
case kVK_F1: return ImGuiKey_F1;
|
||||||
|
case kVK_F2: return ImGuiKey_F2;
|
||||||
|
case kVK_F3: return ImGuiKey_F3;
|
||||||
|
case kVK_F4: return ImGuiKey_F4;
|
||||||
|
case kVK_F5: return ImGuiKey_F5;
|
||||||
|
case kVK_F6: return ImGuiKey_F6;
|
||||||
|
case kVK_F7: return ImGuiKey_F7;
|
||||||
|
case kVK_F8: return ImGuiKey_F8;
|
||||||
|
case kVK_F9: return ImGuiKey_F9;
|
||||||
|
case kVK_F10: return ImGuiKey_F10;
|
||||||
|
case kVK_F11: return ImGuiKey_F11;
|
||||||
|
case kVK_F12: return ImGuiKey_F12;
|
||||||
|
case kVK_F13: return ImGuiKey_F13;
|
||||||
|
case kVK_F14: return ImGuiKey_F14;
|
||||||
|
case kVK_F15: return ImGuiKey_F15;
|
||||||
|
case kVK_F16: return ImGuiKey_F16;
|
||||||
|
case kVK_F17: return ImGuiKey_F17;
|
||||||
|
case kVK_F18: return ImGuiKey_F18;
|
||||||
|
case kVK_F19: return ImGuiKey_F19;
|
||||||
|
case kVK_F20: return ImGuiKey_F20;
|
||||||
|
case 0x6E: return ImGuiKey_Menu;
|
||||||
|
case kVK_Help: return ImGuiKey_Insert;
|
||||||
|
case kVK_Home: return ImGuiKey_Home;
|
||||||
|
case kVK_PageUp: return ImGuiKey_PageUp;
|
||||||
|
case kVK_ForwardDelete: return ImGuiKey_Delete;
|
||||||
|
case kVK_End: return ImGuiKey_End;
|
||||||
|
case kVK_PageDown: return ImGuiKey_PageDown;
|
||||||
|
case kVK_LeftArrow: return ImGuiKey_LeftArrow;
|
||||||
|
case kVK_RightArrow: return ImGuiKey_RightArrow;
|
||||||
|
case kVK_DownArrow: return ImGuiKey_DownArrow;
|
||||||
|
case kVK_UpArrow: return ImGuiKey_UpArrow;
|
||||||
|
default: return ImGuiKey_None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef IMGUI_IMPL_METAL_CPP_EXTENSIONS
|
||||||
|
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplOSX_Init(void* _Nonnull view) {
|
||||||
|
return ImGui_ImplOSX_Init((__bridge NSView*)(view));
|
||||||
|
}
|
||||||
|
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplOSX_NewFrame(void* _Nullable view) {
|
||||||
|
return ImGui_ImplOSX_NewFrame((__bridge NSView*)(view));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
bool ImGui_ImplOSX_Init(NSView* view)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplOSX_Data* bd = IM_NEW(ImGui_ImplOSX_Data)();
|
||||||
|
io.BackendPlatformUserData = (void*)bd;
|
||||||
|
io.BackendPlatformName = "imgui_impl_osx";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
||||||
|
//io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
|
||||||
|
|
||||||
|
bd->Observer = [ImGuiObserver new];
|
||||||
|
bd->Window = view.window ?: NSApp.orderedWindows.firstObject;
|
||||||
|
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
|
||||||
|
main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (__bridge_retained void*)bd->Window;
|
||||||
|
|
||||||
|
// Load cursors. Some of them are undocumented.
|
||||||
|
bd->MouseCursorHidden = false;
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Arrow] = [NSCursor arrowCursor];
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_TextInput] = [NSCursor IBeamCursor];
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = [NSCursor closedHandCursor];
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = [NSCursor respondsToSelector:@selector(_windowResizeNorthSouthCursor)] ? [NSCursor _windowResizeNorthSouthCursor] : [NSCursor resizeUpDownCursor];
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = [NSCursor respondsToSelector:@selector(_windowResizeEastWestCursor)] ? [NSCursor _windowResizeEastWestCursor] : [NSCursor resizeLeftRightCursor];
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = [NSCursor respondsToSelector:@selector(_windowResizeNorthEastSouthWestCursor)] ? [NSCursor _windowResizeNorthEastSouthWestCursor] : [NSCursor closedHandCursor];
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = [NSCursor respondsToSelector:@selector(_windowResizeNorthWestSouthEastCursor)] ? [NSCursor _windowResizeNorthWestSouthEastCursor] : [NSCursor closedHandCursor];
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Hand] = [NSCursor pointingHandCursor];
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Wait] = bd->MouseCursors[ImGuiMouseCursor_Progress] = [NSCursor respondsToSelector:@selector(busyButClickableCursor)] ? [NSCursor busyButClickableCursor] : [NSCursor arrowCursor];
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = [NSCursor operationNotAllowedCursor];
|
||||||
|
|
||||||
|
// Note that imgui.cpp also include default OSX clipboard handlers which can be enabled
|
||||||
|
// by adding '#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS' in imconfig.h and adding '-framework ApplicationServices' to your linker command-line.
|
||||||
|
// Since we are already in ObjC land here, it is easy for us to add a clipboard handler using the NSPasteboard api.
|
||||||
|
platform_io.Platform_SetClipboardTextFn = [](ImGuiContext*, const char* str) -> void
|
||||||
|
{
|
||||||
|
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
|
||||||
|
[pasteboard declareTypes:[NSArray arrayWithObject:NSPasteboardTypeString] owner:nil];
|
||||||
|
[pasteboard setString:[NSString stringWithUTF8String:str] forType:NSPasteboardTypeString];
|
||||||
|
};
|
||||||
|
|
||||||
|
platform_io.Platform_GetClipboardTextFn = [](ImGuiContext*) -> const char*
|
||||||
|
{
|
||||||
|
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
|
||||||
|
NSString* available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:NSPasteboardTypeString]];
|
||||||
|
if (![available isEqualToString:NSPasteboardTypeString])
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
NSString* string = [pasteboard stringForType:NSPasteboardTypeString];
|
||||||
|
if (string == nil)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
const char* string_c = (const char*)[string UTF8String];
|
||||||
|
size_t string_len = strlen(string_c);
|
||||||
|
static ImVector<char> s_clipboard;
|
||||||
|
s_clipboard.resize((int)string_len + 1);
|
||||||
|
strcpy(s_clipboard.Data, string_c);
|
||||||
|
return s_clipboard.Data;
|
||||||
|
};
|
||||||
|
|
||||||
|
[[NSNotificationCenter defaultCenter] addObserver:bd->Observer
|
||||||
|
selector:@selector(onApplicationBecomeActive:)
|
||||||
|
name:NSApplicationDidBecomeActiveNotification
|
||||||
|
object:nil];
|
||||||
|
[[NSNotificationCenter defaultCenter] addObserver:bd->Observer
|
||||||
|
selector:@selector(onApplicationBecomeInactive:)
|
||||||
|
name:NSApplicationDidResignActiveNotification
|
||||||
|
object:nil];
|
||||||
|
|
||||||
|
// Add the NSTextInputClient to the view hierarchy,
|
||||||
|
// to receive keyboard events and translate them to input text.
|
||||||
|
bd->KeyEventResponder = [[KeyEventResponder alloc] initWithFrame:NSZeroRect];
|
||||||
|
bd->InputContext = [[NSTextInputContext alloc] initWithClient:bd->KeyEventResponder];
|
||||||
|
[view addSubview:bd->KeyEventResponder];
|
||||||
|
ImGui_ImplOSX_AddTrackingArea(view);
|
||||||
|
|
||||||
|
platform_io.Platform_SetImeDataFn = [](ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData* data) -> void
|
||||||
|
{
|
||||||
|
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
|
||||||
|
if (data->WantVisible)
|
||||||
|
{
|
||||||
|
[bd->InputContext activate];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
[bd->InputContext discardMarkedText];
|
||||||
|
[bd->InputContext invalidateCharacterCoordinates];
|
||||||
|
[bd->InputContext deactivate];
|
||||||
|
}
|
||||||
|
[bd->KeyEventResponder setImePosX:data->InputPos.x imePosY:data->InputPos.y + data->InputLineHeight];
|
||||||
|
};
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplOSX_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
|
||||||
|
|
||||||
|
[[NSNotificationCenter defaultCenter] removeObserver:bd->Observer];
|
||||||
|
bd->Observer = nullptr;
|
||||||
|
if (bd->Monitor != nullptr)
|
||||||
|
{
|
||||||
|
[NSEvent removeMonitor:bd->Monitor];
|
||||||
|
bd->Monitor = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui_ImplOSX_DestroyBackendData();
|
||||||
|
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.BackendPlatformName = nullptr;
|
||||||
|
io.BackendPlatformUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasGamepad);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplOSX_UpdateMouseCursor()
|
||||||
|
{
|
||||||
|
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
|
||||||
|
if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
|
||||||
|
{
|
||||||
|
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
|
||||||
|
if (!bd->MouseCursorHidden)
|
||||||
|
{
|
||||||
|
bd->MouseCursorHidden = true;
|
||||||
|
[NSCursor hide];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NSCursor* desired = bd->MouseCursors[imgui_cursor] ?: bd->MouseCursors[ImGuiMouseCursor_Arrow];
|
||||||
|
// -[NSCursor set] generates measurable overhead if called unconditionally.
|
||||||
|
if (desired != NSCursor.currentCursor)
|
||||||
|
{
|
||||||
|
[desired set];
|
||||||
|
}
|
||||||
|
if (bd->MouseCursorHidden)
|
||||||
|
{
|
||||||
|
bd->MouseCursorHidden = false;
|
||||||
|
[NSCursor unhide];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplOSX_UpdateGamepads()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
#if APPLE_HAS_CONTROLLER
|
||||||
|
GCController* controller = GCController.current;
|
||||||
|
#else
|
||||||
|
GCController* controller = GCController.controllers.firstObject;
|
||||||
|
#endif
|
||||||
|
if (controller == nil || controller.extendedGamepad == nil)
|
||||||
|
{
|
||||||
|
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
GCExtendedGamepad* gp = controller.extendedGamepad;
|
||||||
|
|
||||||
|
// Update gamepad inputs
|
||||||
|
#define IM_SATURATE(V) (V < 0.0f ? 0.0f : V > 1.0f ? 1.0f : V)
|
||||||
|
#define MAP_BUTTON(KEY_NO, BUTTON_NAME) { io.AddKeyEvent(KEY_NO, gp.BUTTON_NAME.isPressed); }
|
||||||
|
#define MAP_ANALOG(KEY_NO, AXIS_NAME, V0, V1) { float vn = (float)(gp.AXIS_NAME.value - V0) / (float)(V1 - V0); vn = IM_SATURATE(vn); io.AddKeyAnalogEvent(KEY_NO, vn > 0.1f, vn); }
|
||||||
|
const float thumb_dead_zone = 0.0f;
|
||||||
|
|
||||||
|
#if APPLE_HAS_BUTTON_OPTIONS
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadBack, buttonOptions);
|
||||||
|
#endif
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadFaceLeft, buttonX); // Xbox X, PS Square
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadFaceRight, buttonB); // Xbox B, PS Circle
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadFaceUp, buttonY); // Xbox Y, PS Triangle
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadFaceDown, buttonA); // Xbox A, PS Cross
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadDpadLeft, dpad.left);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadDpadRight, dpad.right);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadDpadUp, dpad.up);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadDpadDown, dpad.down);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadL1, leftShoulder, 0.0f, 1.0f);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadR1, rightShoulder, 0.0f, 1.0f);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadL2, leftTrigger, 0.0f, 1.0f);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadR2, rightTrigger, 0.0f, 1.0f);
|
||||||
|
#if APPLE_HAS_THUMBSTICKS
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadL3, leftThumbstickButton);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadR3, rightThumbstickButton);
|
||||||
|
#endif
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadLStickLeft, leftThumbstick.xAxis, -thumb_dead_zone, -1.0f);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadLStickRight, leftThumbstick.xAxis, +thumb_dead_zone, +1.0f);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadLStickUp, leftThumbstick.yAxis, +thumb_dead_zone, +1.0f);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadLStickDown, leftThumbstick.yAxis, -thumb_dead_zone, -1.0f);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadRStickLeft, rightThumbstick.xAxis, -thumb_dead_zone, -1.0f);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadRStickRight, rightThumbstick.xAxis, +thumb_dead_zone, +1.0f);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadRStickUp, rightThumbstick.yAxis, +thumb_dead_zone, +1.0f);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadRStickDown, rightThumbstick.yAxis, -thumb_dead_zone, -1.0f);
|
||||||
|
#undef MAP_BUTTON
|
||||||
|
#undef MAP_ANALOG
|
||||||
|
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplOSX_UpdateImePosWithView(NSView* view)
|
||||||
|
{
|
||||||
|
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
if (io.WantTextInput)
|
||||||
|
[bd->KeyEventResponder updateImePosWithView:view];
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplOSX_NewFrame(NSView* view)
|
||||||
|
{
|
||||||
|
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplOSX_Init()?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Setup display size
|
||||||
|
if (view)
|
||||||
|
{
|
||||||
|
const float dpi = (float)[view.window backingScaleFactor];
|
||||||
|
io.DisplaySize = ImVec2((float)view.bounds.size.width, (float)view.bounds.size.height);
|
||||||
|
io.DisplayFramebufferScale = ImVec2(dpi, dpi);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup time step
|
||||||
|
if (bd->Time == 0.0)
|
||||||
|
bd->Time = GetMachAbsoluteTimeInSeconds();
|
||||||
|
|
||||||
|
double current_time = GetMachAbsoluteTimeInSeconds();
|
||||||
|
io.DeltaTime = (float)(current_time - bd->Time);
|
||||||
|
bd->Time = current_time;
|
||||||
|
|
||||||
|
ImGui_ImplOSX_UpdateMouseCursor();
|
||||||
|
ImGui_ImplOSX_UpdateGamepads();
|
||||||
|
ImGui_ImplOSX_UpdateImePosWithView(view);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Must only be called for a mouse event, otherwise an exception occurs
|
||||||
|
// (Note that NSEventTypeScrollWheel is considered "other input". Oddly enough an exception does not occur with it, but the value will sometimes be wrong!)
|
||||||
|
static ImGuiMouseSource GetMouseSource(NSEvent* event)
|
||||||
|
{
|
||||||
|
switch (event.subtype)
|
||||||
|
{
|
||||||
|
case NSEventSubtypeTabletPoint:
|
||||||
|
return ImGuiMouseSource_Pen;
|
||||||
|
// macOS considers input from relative touch devices (like the trackpad or Apple Magic Mouse) to be touch input.
|
||||||
|
// This doesn't really make sense for Dear ImGui, which expects absolute touch devices only.
|
||||||
|
// There does not seem to be a simple way to disambiguate things here so we consider NSEventSubtypeTouch events to always come from mice.
|
||||||
|
// See https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html#//apple_ref/doc/uid/10000060i-CH13-SW24
|
||||||
|
//case NSEventSubtypeTouch:
|
||||||
|
// return ImGuiMouseSource_TouchScreen;
|
||||||
|
case NSEventSubtypeMouseEvent:
|
||||||
|
default:
|
||||||
|
return ImGuiMouseSource_Mouse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
|
||||||
|
{
|
||||||
|
// Only process events from the window containing ImGui view
|
||||||
|
if (event.window != view.window)
|
||||||
|
return false;
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
if (event.type == NSEventTypeLeftMouseDown || event.type == NSEventTypeRightMouseDown || event.type == NSEventTypeOtherMouseDown)
|
||||||
|
{
|
||||||
|
int button = (int)[event buttonNumber];
|
||||||
|
if (button >= 0 && button < ImGuiMouseButton_COUNT)
|
||||||
|
{
|
||||||
|
io.AddMouseSourceEvent(GetMouseSource(event));
|
||||||
|
io.AddMouseButtonEvent(button, true);
|
||||||
|
}
|
||||||
|
return io.WantCaptureMouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.type == NSEventTypeLeftMouseUp || event.type == NSEventTypeRightMouseUp || event.type == NSEventTypeOtherMouseUp)
|
||||||
|
{
|
||||||
|
int button = (int)[event buttonNumber];
|
||||||
|
if (button >= 0 && button < ImGuiMouseButton_COUNT)
|
||||||
|
{
|
||||||
|
io.AddMouseSourceEvent(GetMouseSource(event));
|
||||||
|
io.AddMouseButtonEvent(button, false);
|
||||||
|
}
|
||||||
|
return io.WantCaptureMouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.type == NSEventTypeMouseMoved || event.type == NSEventTypeLeftMouseDragged || event.type == NSEventTypeRightMouseDragged || event.type == NSEventTypeOtherMouseDragged)
|
||||||
|
{
|
||||||
|
NSPoint mousePoint = event.locationInWindow;
|
||||||
|
if (event.window == nil)
|
||||||
|
mousePoint = [[view window] convertPointFromScreen:mousePoint];
|
||||||
|
mousePoint = [view convertPoint:mousePoint fromView:nil];
|
||||||
|
if ([view isFlipped])
|
||||||
|
mousePoint = NSMakePoint(mousePoint.x, mousePoint.y);
|
||||||
|
else
|
||||||
|
mousePoint = NSMakePoint(mousePoint.x, view.bounds.size.height - mousePoint.y);
|
||||||
|
io.AddMouseSourceEvent(GetMouseSource(event));
|
||||||
|
io.AddMousePosEvent((float)mousePoint.x, (float)mousePoint.y);
|
||||||
|
return io.WantCaptureMouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.type == NSEventTypeScrollWheel)
|
||||||
|
{
|
||||||
|
// Ignore canceled events.
|
||||||
|
//
|
||||||
|
// From macOS 12.1, scrolling with two fingers and then decelerating
|
||||||
|
// by tapping two fingers results in two events appearing:
|
||||||
|
//
|
||||||
|
// 1. A scroll wheel NSEvent, with a phase == NSEventPhaseMayBegin, when the user taps
|
||||||
|
// two fingers to decelerate or stop the scroll events.
|
||||||
|
//
|
||||||
|
// 2. A scroll wheel NSEvent, with a phase == NSEventPhaseCancelled, when the user releases the
|
||||||
|
// two-finger tap. It is this event that sometimes contains large values for scrollingDeltaX and
|
||||||
|
// scrollingDeltaY. When these are added to the current x and y positions of the scrolling view,
|
||||||
|
// it appears to jump up or down. It can be observed in Preview, various JetBrains IDEs and here.
|
||||||
|
if (event.phase == NSEventPhaseCancelled)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
double wheel_dx = 0.0;
|
||||||
|
double wheel_dy = 0.0;
|
||||||
|
|
||||||
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
|
||||||
|
if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
|
||||||
|
{
|
||||||
|
wheel_dx = [event scrollingDeltaX];
|
||||||
|
wheel_dy = [event scrollingDeltaY];
|
||||||
|
if ([event hasPreciseScrollingDeltas])
|
||||||
|
{
|
||||||
|
wheel_dx *= 0.01;
|
||||||
|
wheel_dy *= 0.01;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
#endif // MAC_OS_X_VERSION_MAX_ALLOWED
|
||||||
|
{
|
||||||
|
wheel_dx = [event deltaX] * 0.1;
|
||||||
|
wheel_dy = [event deltaY] * 0.1;
|
||||||
|
}
|
||||||
|
if (wheel_dx != 0.0 || wheel_dy != 0.0)
|
||||||
|
io.AddMouseWheelEvent((float)wheel_dx, (float)wheel_dy);
|
||||||
|
|
||||||
|
return io.WantCaptureMouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.type == NSEventTypeKeyDown || event.type == NSEventTypeKeyUp)
|
||||||
|
{
|
||||||
|
if ([event isARepeat])
|
||||||
|
return io.WantCaptureKeyboard;
|
||||||
|
|
||||||
|
int key_code = (int)[event keyCode];
|
||||||
|
ImGuiKey key = ImGui_ImplOSX_KeyCodeToImGuiKey(key_code);
|
||||||
|
io.AddKeyEvent(key, event.type == NSEventTypeKeyDown);
|
||||||
|
io.SetKeyEventNativeData(key, key_code, -1); // To support legacy indexing (<1.87 user code)
|
||||||
|
|
||||||
|
return io.WantCaptureKeyboard;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.type == NSEventTypeFlagsChanged)
|
||||||
|
{
|
||||||
|
unsigned short key_code = [event keyCode];
|
||||||
|
NSEventModifierFlags modifier_flags = [event modifierFlags];
|
||||||
|
|
||||||
|
io.AddKeyEvent(ImGuiMod_Shift, (modifier_flags & NSEventModifierFlagShift) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Ctrl, (modifier_flags & NSEventModifierFlagControl) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Alt, (modifier_flags & NSEventModifierFlagOption) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Super, (modifier_flags & NSEventModifierFlagCommand) != 0);
|
||||||
|
|
||||||
|
ImGuiKey key = ImGui_ImplOSX_KeyCodeToImGuiKey(key_code);
|
||||||
|
if (key != ImGuiKey_None)
|
||||||
|
{
|
||||||
|
// macOS does not generate down/up event for modifiers. We're trying
|
||||||
|
// to use hardware dependent masks to extract that information.
|
||||||
|
// 'imgui_mask' is left as a fallback.
|
||||||
|
NSEventModifierFlags mask = 0;
|
||||||
|
switch (key)
|
||||||
|
{
|
||||||
|
case ImGuiKey_LeftCtrl: mask = 0x0001; break;
|
||||||
|
case ImGuiKey_RightCtrl: mask = 0x2000; break;
|
||||||
|
case ImGuiKey_LeftShift: mask = 0x0002; break;
|
||||||
|
case ImGuiKey_RightShift: mask = 0x0004; break;
|
||||||
|
case ImGuiKey_LeftSuper: mask = 0x0008; break;
|
||||||
|
case ImGuiKey_RightSuper: mask = 0x0010; break;
|
||||||
|
case ImGuiKey_LeftAlt: mask = 0x0020; break;
|
||||||
|
case ImGuiKey_RightAlt: mask = 0x0040; break;
|
||||||
|
default:
|
||||||
|
return io.WantCaptureKeyboard;
|
||||||
|
}
|
||||||
|
io.AddKeyEvent(key, (modifier_flags & mask) != 0);
|
||||||
|
io.SetKeyEventNativeData(key, key_code, -1); // To support legacy indexing (<1.87 user code)
|
||||||
|
}
|
||||||
|
|
||||||
|
return io.WantCaptureKeyboard;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplOSX_AddTrackingArea(NSView* _Nonnull view)
|
||||||
|
{
|
||||||
|
// If we want to receive key events, we either need to be in the responder chain of the key view,
|
||||||
|
// or else we can install a local monitor. The consequence of this heavy-handed approach is that
|
||||||
|
// we receive events for all controls, not just Dear ImGui widgets. If we had native controls in our
|
||||||
|
// window, we'd want to be much more careful than just ingesting the complete event stream.
|
||||||
|
// To match the behavior of other backends, we pass every event down to the OS.
|
||||||
|
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
|
||||||
|
if (bd->Monitor)
|
||||||
|
return;
|
||||||
|
NSEventMask eventMask = 0;
|
||||||
|
eventMask |= NSEventMaskMouseMoved | NSEventMaskScrollWheel;
|
||||||
|
eventMask |= NSEventMaskLeftMouseDown | NSEventMaskLeftMouseUp | NSEventMaskLeftMouseDragged;
|
||||||
|
eventMask |= NSEventMaskRightMouseDown | NSEventMaskRightMouseUp | NSEventMaskRightMouseDragged;
|
||||||
|
eventMask |= NSEventMaskOtherMouseDown | NSEventMaskOtherMouseUp | NSEventMaskOtherMouseDragged;
|
||||||
|
eventMask |= NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged;
|
||||||
|
bd->Monitor = [NSEvent addLocalMonitorForEventsMatchingMask:eventMask
|
||||||
|
handler:^NSEvent* _Nullable(NSEvent* event)
|
||||||
|
{
|
||||||
|
ImGui_ImplOSX_HandleEvent(event, view);
|
||||||
|
return event;
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
893
imgui/backends/imgui_impl_sdl2.cpp
Normal file
893
imgui/backends/imgui_impl_sdl2.cpp
Normal file
@@ -0,0 +1,893 @@
|
|||||||
|
// dear imgui: Platform Backend for SDL2
|
||||||
|
// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
|
||||||
|
// (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
|
||||||
|
// (Prefer SDL 2.0.5+ for full feature support.)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Clipboard support.
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen.
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Gamepad support.
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
// [X] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-06-11: Added ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window) and ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index) helper to facilitate making DPI-aware apps.
|
||||||
|
// 2025-04-09: Don't attempt to call SDL_CaptureMouse() on drivers where we don't call SDL_GetGlobalMouseState(). (#8561)
|
||||||
|
// 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set.
|
||||||
|
// 2025-03-10: When dealing with OEM keys, use scancodes instead of translated keycodes to choose ImGuiKey values. (#7136, #7201, #7206, #7306, #7670, #7672, #8468)
|
||||||
|
// 2025-02-26: Only start SDL_CaptureMouse() when mouse is being dragged, to mitigate issues with e.g.Linux debuggers not claiming capture back. (#6410, #3650)
|
||||||
|
// 2025-02-24: Avoid calling SDL_GetGlobalMouseState() when mouse is in relative mode.
|
||||||
|
// 2025-02-18: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support.
|
||||||
|
// 2025-02-10: Using SDL_OpenURL() in platform_io.Platform_OpenInShellFn handler.
|
||||||
|
// 2025-01-20: Made ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode_Manual) accept an empty array.
|
||||||
|
// 2024-10-24: Emscripten: from SDL 2.30.9, SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f.
|
||||||
|
// 2024-09-09: use SDL_Vulkan_GetDrawableSize() when available. (#7967, #3190)
|
||||||
|
// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
|
||||||
|
// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
|
||||||
|
// - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
|
||||||
|
// - io.PlatformOpenInShellFn -> platform_io.Platform_OpenInShellFn
|
||||||
|
// - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn
|
||||||
|
// 2024-08-19: Storing SDL's Uint32 WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*.
|
||||||
|
// 2024-08-19: ImGui_ImplSDL2_ProcessEvent() now ignores events intended for other SDL windows. (#7853)
|
||||||
|
// 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions.
|
||||||
|
// 2024-07-02: Update for io.SetPlatformImeDataFn() -> io.PlatformSetImeDataFn() renaming in main library.
|
||||||
|
// 2024-02-14: Inputs: Handle gamepad disconnection. Added ImGui_ImplSDL2_SetGamepadMode().
|
||||||
|
// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
|
||||||
|
// 2023-04-06: Inputs: Avoid calling SDL_StartTextInput()/SDL_StopTextInput() as they don't only pertain to IME. It's unclear exactly what their relation is to IME. (#6306)
|
||||||
|
// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702)
|
||||||
|
// 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644)
|
||||||
|
// 2023-02-07: Implement IME handler (io.SetPlatformImeDataFn will call SDL_SetTextInputRect()/SDL_StartTextInput()).
|
||||||
|
// 2023-02-07: *BREAKING CHANGE* Renamed this backend file from imgui_impl_sdl.cpp/.h to imgui_impl_sdl2.cpp/.h in prevision for the future release of SDL3.
|
||||||
|
// 2023-02-02: Avoid calling SDL_SetCursor() when cursor has not changed, as the function is surprisingly costly on Mac with latest SDL (may be fixed in next SDL version).
|
||||||
|
// 2023-02-02: Added support for SDL 2.0.18+ preciseX/preciseY mouse wheel data for smooth scrolling + Scaling X value on Emscripten (bug?). (#4019, #6096)
|
||||||
|
// 2023-02-02: Removed SDL_MOUSEWHEEL value clamping, as values seem correct in latest Emscripten. (#4019)
|
||||||
|
// 2023-02-01: Flipping SDL_MOUSEWHEEL 'wheel.x' value to match other backends and offer consistent horizontal scrolling direction. (#4019, #6096, #1463)
|
||||||
|
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
|
||||||
|
// 2022-09-26: Inputs: Disable SDL 2.0.22 new "auto capture" (SDL_HINT_MOUSE_AUTO_CAPTURE) which prevents drag and drop across windows for multi-viewport support + don't capture when drag and dropping. (#5710)
|
||||||
|
// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
|
||||||
|
// 2022-03-22: Inputs: Fix mouse position issues when dragging outside of boundaries. SDL_CaptureMouse() erroneously still gives out LEAVE events when hovering OS decorations.
|
||||||
|
// 2022-03-22: Inputs: Added support for extra mouse buttons (SDL_BUTTON_X1/SDL_BUTTON_X2).
|
||||||
|
// 2022-02-04: Added SDL_Renderer* parameter to ImGui_ImplSDL2_InitForSDLRenderer(), so we can use SDL_GetRendererOutputSize() instead of SDL_GL_GetDrawableSize() when bound to a SDL_Renderer.
|
||||||
|
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
|
||||||
|
// 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
|
||||||
|
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
|
||||||
|
// 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates.
|
||||||
|
// 2022-01-12: Update mouse inputs using SDL_MOUSEMOTION/SDL_WINDOWEVENT_LEAVE + fallback to provide it when focused but not hovered/captured. More standard and will allow us to pass it to future input queue API.
|
||||||
|
// 2022-01-12: Maintain our own copy of MouseButtonsDown mask instead of using ImGui::IsAnyMouseDown() which will be obsoleted.
|
||||||
|
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
|
||||||
|
// 2021-08-17: Calling io.AddFocusEvent() on SDL_WINDOWEVENT_FOCUS_GAINED/SDL_WINDOWEVENT_FOCUS_LOST.
|
||||||
|
// 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using SDL_GetMouseFocus() + SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, requires SDL 2.0.5+)
|
||||||
|
// 2021-06-29: *BREAKING CHANGE* Removed 'SDL_Window* window' parameter to ImGui_ImplSDL2_NewFrame() which was unnecessary.
|
||||||
|
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
|
||||||
|
// 2021-03-22: Rework global mouse pos availability check listing supported platforms explicitly, effectively fixing mouse access on Raspberry Pi. (#2837, #3950)
|
||||||
|
// 2020-05-25: Misc: Report a zero display-size when window is minimized, to be consistent with other backends.
|
||||||
|
// 2020-02-20: Inputs: Fixed mapping for ImGuiKey_KeyPadEnter (using SDL_SCANCODE_KP_ENTER instead of SDL_SCANCODE_RETURN2).
|
||||||
|
// 2019-12-17: Inputs: On Wayland, use SDL_GetMouseState (because there is no global mouse state).
|
||||||
|
// 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
|
||||||
|
// 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
|
||||||
|
// 2019-04-23: Inputs: Added support for SDL_GameController (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
|
||||||
|
// 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized.
|
||||||
|
// 2018-12-21: Inputs: Workaround for Android/iOS which don't seem to handle focus related calls.
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
|
||||||
|
// 2018-11-14: Changed the signature of ImGui_ImplSDL2_ProcessEvent() to take a 'const SDL_Event*'.
|
||||||
|
// 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls.
|
||||||
|
// 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
|
||||||
|
// 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples.
|
||||||
|
// 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter.
|
||||||
|
// 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText).
|
||||||
|
// 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
|
||||||
|
// 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
|
||||||
|
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
||||||
|
// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
|
||||||
|
// 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
|
||||||
|
// 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
|
||||||
|
// 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
|
||||||
|
// 2018-01-19: Inputs: When available (SDL 2.0.4+) using SDL_CaptureMouse() to retrieve coordinates outside of client area when dragging. Otherwise (SDL 2.0.3 and before) testing for SDL_WINDOW_INPUT_FOCUS instead of SDL_WINDOW_MOUSE_FOCUS.
|
||||||
|
// 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
|
||||||
|
// 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
|
||||||
|
// 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_sdl2.h"
|
||||||
|
|
||||||
|
// Clang warnings with -Weverything
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
|
||||||
|
#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// SDL
|
||||||
|
#include <SDL.h>
|
||||||
|
#include <SDL_syswm.h>
|
||||||
|
#include <stdio.h> // for snprintf()
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#include <TargetConditionals.h>
|
||||||
|
#endif
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
#include <emscripten/em_js.h>
|
||||||
|
#endif
|
||||||
|
#undef Status // X11 headers are leaking this.
|
||||||
|
|
||||||
|
#if SDL_VERSION_ATLEAST(2,0,4) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
|
||||||
|
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
|
||||||
|
#else
|
||||||
|
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
|
||||||
|
#endif
|
||||||
|
#define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4)
|
||||||
|
#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
|
||||||
|
#define SDL_HAS_OPEN_URL SDL_VERSION_ATLEAST(2,0,14)
|
||||||
|
#if SDL_HAS_VULKAN
|
||||||
|
#include <SDL_vulkan.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// SDL Data
|
||||||
|
struct ImGui_ImplSDL2_Data
|
||||||
|
{
|
||||||
|
SDL_Window* Window;
|
||||||
|
Uint32 WindowID;
|
||||||
|
SDL_Renderer* Renderer;
|
||||||
|
Uint64 Time;
|
||||||
|
char* ClipboardTextData;
|
||||||
|
char BackendPlatformName[48];
|
||||||
|
|
||||||
|
// Mouse handling
|
||||||
|
Uint32 MouseWindowID;
|
||||||
|
int MouseButtonsDown;
|
||||||
|
SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
|
||||||
|
SDL_Cursor* MouseLastCursor;
|
||||||
|
int MouseLastLeaveFrame;
|
||||||
|
bool MouseCanUseGlobalState;
|
||||||
|
bool MouseCanUseCapture;
|
||||||
|
|
||||||
|
// Gamepad handling
|
||||||
|
ImVector<SDL_GameController*> Gamepads;
|
||||||
|
ImGui_ImplSDL2_GamepadMode GamepadMode;
|
||||||
|
bool WantUpdateGamepadsList;
|
||||||
|
|
||||||
|
ImGui_ImplSDL2_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
// FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
|
||||||
|
// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
|
||||||
|
static ImGui_ImplSDL2_Data* ImGui_ImplSDL2_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplSDL2_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
static const char* ImGui_ImplSDL2_GetClipboardText(ImGuiContext*)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||||
|
if (bd->ClipboardTextData)
|
||||||
|
SDL_free(bd->ClipboardTextData);
|
||||||
|
bd->ClipboardTextData = SDL_GetClipboardText();
|
||||||
|
return bd->ClipboardTextData;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL2_SetClipboardText(ImGuiContext*, const char* text)
|
||||||
|
{
|
||||||
|
SDL_SetClipboardText(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: native IME will only display if user calls SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1") _before_ SDL_CreateWindow().
|
||||||
|
static void ImGui_ImplSDL2_PlatformSetImeData(ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData* data)
|
||||||
|
{
|
||||||
|
if (data->WantVisible)
|
||||||
|
{
|
||||||
|
SDL_Rect r;
|
||||||
|
r.x = (int)data->InputPos.x;
|
||||||
|
r.y = (int)data->InputPos.y;
|
||||||
|
r.w = 1;
|
||||||
|
r.h = (int)data->InputLineHeight;
|
||||||
|
SDL_SetTextInputRect(&r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not static to allow third-party code to use that if they want to (but undocumented)
|
||||||
|
ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode);
|
||||||
|
ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
|
||||||
|
{
|
||||||
|
switch (keycode)
|
||||||
|
{
|
||||||
|
case SDLK_TAB: return ImGuiKey_Tab;
|
||||||
|
case SDLK_LEFT: return ImGuiKey_LeftArrow;
|
||||||
|
case SDLK_RIGHT: return ImGuiKey_RightArrow;
|
||||||
|
case SDLK_UP: return ImGuiKey_UpArrow;
|
||||||
|
case SDLK_DOWN: return ImGuiKey_DownArrow;
|
||||||
|
case SDLK_PAGEUP: return ImGuiKey_PageUp;
|
||||||
|
case SDLK_PAGEDOWN: return ImGuiKey_PageDown;
|
||||||
|
case SDLK_HOME: return ImGuiKey_Home;
|
||||||
|
case SDLK_END: return ImGuiKey_End;
|
||||||
|
case SDLK_INSERT: return ImGuiKey_Insert;
|
||||||
|
case SDLK_DELETE: return ImGuiKey_Delete;
|
||||||
|
case SDLK_BACKSPACE: return ImGuiKey_Backspace;
|
||||||
|
case SDLK_SPACE: return ImGuiKey_Space;
|
||||||
|
case SDLK_RETURN: return ImGuiKey_Enter;
|
||||||
|
case SDLK_ESCAPE: return ImGuiKey_Escape;
|
||||||
|
//case SDLK_QUOTE: return ImGuiKey_Apostrophe;
|
||||||
|
case SDLK_COMMA: return ImGuiKey_Comma;
|
||||||
|
//case SDLK_MINUS: return ImGuiKey_Minus;
|
||||||
|
case SDLK_PERIOD: return ImGuiKey_Period;
|
||||||
|
//case SDLK_SLASH: return ImGuiKey_Slash;
|
||||||
|
case SDLK_SEMICOLON: return ImGuiKey_Semicolon;
|
||||||
|
//case SDLK_EQUALS: return ImGuiKey_Equal;
|
||||||
|
//case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket;
|
||||||
|
//case SDLK_BACKSLASH: return ImGuiKey_Backslash;
|
||||||
|
//case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket;
|
||||||
|
//case SDLK_BACKQUOTE: return ImGuiKey_GraveAccent;
|
||||||
|
case SDLK_CAPSLOCK: return ImGuiKey_CapsLock;
|
||||||
|
case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock;
|
||||||
|
case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock;
|
||||||
|
case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen;
|
||||||
|
case SDLK_PAUSE: return ImGuiKey_Pause;
|
||||||
|
case SDLK_KP_0: return ImGuiKey_Keypad0;
|
||||||
|
case SDLK_KP_1: return ImGuiKey_Keypad1;
|
||||||
|
case SDLK_KP_2: return ImGuiKey_Keypad2;
|
||||||
|
case SDLK_KP_3: return ImGuiKey_Keypad3;
|
||||||
|
case SDLK_KP_4: return ImGuiKey_Keypad4;
|
||||||
|
case SDLK_KP_5: return ImGuiKey_Keypad5;
|
||||||
|
case SDLK_KP_6: return ImGuiKey_Keypad6;
|
||||||
|
case SDLK_KP_7: return ImGuiKey_Keypad7;
|
||||||
|
case SDLK_KP_8: return ImGuiKey_Keypad8;
|
||||||
|
case SDLK_KP_9: return ImGuiKey_Keypad9;
|
||||||
|
case SDLK_KP_PERIOD: return ImGuiKey_KeypadDecimal;
|
||||||
|
case SDLK_KP_DIVIDE: return ImGuiKey_KeypadDivide;
|
||||||
|
case SDLK_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
|
||||||
|
case SDLK_KP_MINUS: return ImGuiKey_KeypadSubtract;
|
||||||
|
case SDLK_KP_PLUS: return ImGuiKey_KeypadAdd;
|
||||||
|
case SDLK_KP_ENTER: return ImGuiKey_KeypadEnter;
|
||||||
|
case SDLK_KP_EQUALS: return ImGuiKey_KeypadEqual;
|
||||||
|
case SDLK_LCTRL: return ImGuiKey_LeftCtrl;
|
||||||
|
case SDLK_LSHIFT: return ImGuiKey_LeftShift;
|
||||||
|
case SDLK_LALT: return ImGuiKey_LeftAlt;
|
||||||
|
case SDLK_LGUI: return ImGuiKey_LeftSuper;
|
||||||
|
case SDLK_RCTRL: return ImGuiKey_RightCtrl;
|
||||||
|
case SDLK_RSHIFT: return ImGuiKey_RightShift;
|
||||||
|
case SDLK_RALT: return ImGuiKey_RightAlt;
|
||||||
|
case SDLK_RGUI: return ImGuiKey_RightSuper;
|
||||||
|
case SDLK_APPLICATION: return ImGuiKey_Menu;
|
||||||
|
case SDLK_0: return ImGuiKey_0;
|
||||||
|
case SDLK_1: return ImGuiKey_1;
|
||||||
|
case SDLK_2: return ImGuiKey_2;
|
||||||
|
case SDLK_3: return ImGuiKey_3;
|
||||||
|
case SDLK_4: return ImGuiKey_4;
|
||||||
|
case SDLK_5: return ImGuiKey_5;
|
||||||
|
case SDLK_6: return ImGuiKey_6;
|
||||||
|
case SDLK_7: return ImGuiKey_7;
|
||||||
|
case SDLK_8: return ImGuiKey_8;
|
||||||
|
case SDLK_9: return ImGuiKey_9;
|
||||||
|
case SDLK_a: return ImGuiKey_A;
|
||||||
|
case SDLK_b: return ImGuiKey_B;
|
||||||
|
case SDLK_c: return ImGuiKey_C;
|
||||||
|
case SDLK_d: return ImGuiKey_D;
|
||||||
|
case SDLK_e: return ImGuiKey_E;
|
||||||
|
case SDLK_f: return ImGuiKey_F;
|
||||||
|
case SDLK_g: return ImGuiKey_G;
|
||||||
|
case SDLK_h: return ImGuiKey_H;
|
||||||
|
case SDLK_i: return ImGuiKey_I;
|
||||||
|
case SDLK_j: return ImGuiKey_J;
|
||||||
|
case SDLK_k: return ImGuiKey_K;
|
||||||
|
case SDLK_l: return ImGuiKey_L;
|
||||||
|
case SDLK_m: return ImGuiKey_M;
|
||||||
|
case SDLK_n: return ImGuiKey_N;
|
||||||
|
case SDLK_o: return ImGuiKey_O;
|
||||||
|
case SDLK_p: return ImGuiKey_P;
|
||||||
|
case SDLK_q: return ImGuiKey_Q;
|
||||||
|
case SDLK_r: return ImGuiKey_R;
|
||||||
|
case SDLK_s: return ImGuiKey_S;
|
||||||
|
case SDLK_t: return ImGuiKey_T;
|
||||||
|
case SDLK_u: return ImGuiKey_U;
|
||||||
|
case SDLK_v: return ImGuiKey_V;
|
||||||
|
case SDLK_w: return ImGuiKey_W;
|
||||||
|
case SDLK_x: return ImGuiKey_X;
|
||||||
|
case SDLK_y: return ImGuiKey_Y;
|
||||||
|
case SDLK_z: return ImGuiKey_Z;
|
||||||
|
case SDLK_F1: return ImGuiKey_F1;
|
||||||
|
case SDLK_F2: return ImGuiKey_F2;
|
||||||
|
case SDLK_F3: return ImGuiKey_F3;
|
||||||
|
case SDLK_F4: return ImGuiKey_F4;
|
||||||
|
case SDLK_F5: return ImGuiKey_F5;
|
||||||
|
case SDLK_F6: return ImGuiKey_F6;
|
||||||
|
case SDLK_F7: return ImGuiKey_F7;
|
||||||
|
case SDLK_F8: return ImGuiKey_F8;
|
||||||
|
case SDLK_F9: return ImGuiKey_F9;
|
||||||
|
case SDLK_F10: return ImGuiKey_F10;
|
||||||
|
case SDLK_F11: return ImGuiKey_F11;
|
||||||
|
case SDLK_F12: return ImGuiKey_F12;
|
||||||
|
case SDLK_F13: return ImGuiKey_F13;
|
||||||
|
case SDLK_F14: return ImGuiKey_F14;
|
||||||
|
case SDLK_F15: return ImGuiKey_F15;
|
||||||
|
case SDLK_F16: return ImGuiKey_F16;
|
||||||
|
case SDLK_F17: return ImGuiKey_F17;
|
||||||
|
case SDLK_F18: return ImGuiKey_F18;
|
||||||
|
case SDLK_F19: return ImGuiKey_F19;
|
||||||
|
case SDLK_F20: return ImGuiKey_F20;
|
||||||
|
case SDLK_F21: return ImGuiKey_F21;
|
||||||
|
case SDLK_F22: return ImGuiKey_F22;
|
||||||
|
case SDLK_F23: return ImGuiKey_F23;
|
||||||
|
case SDLK_F24: return ImGuiKey_F24;
|
||||||
|
case SDLK_AC_BACK: return ImGuiKey_AppBack;
|
||||||
|
case SDLK_AC_FORWARD: return ImGuiKey_AppForward;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to scancode
|
||||||
|
switch (scancode)
|
||||||
|
{
|
||||||
|
case SDL_SCANCODE_GRAVE: return ImGuiKey_GraveAccent;
|
||||||
|
case SDL_SCANCODE_MINUS: return ImGuiKey_Minus;
|
||||||
|
case SDL_SCANCODE_EQUALS: return ImGuiKey_Equal;
|
||||||
|
case SDL_SCANCODE_LEFTBRACKET: return ImGuiKey_LeftBracket;
|
||||||
|
case SDL_SCANCODE_RIGHTBRACKET: return ImGuiKey_RightBracket;
|
||||||
|
case SDL_SCANCODE_NONUSBACKSLASH: return ImGuiKey_Oem102;
|
||||||
|
case SDL_SCANCODE_BACKSLASH: return ImGuiKey_Backslash;
|
||||||
|
case SDL_SCANCODE_SEMICOLON: return ImGuiKey_Semicolon;
|
||||||
|
case SDL_SCANCODE_APOSTROPHE: return ImGuiKey_Apostrophe;
|
||||||
|
case SDL_SCANCODE_COMMA: return ImGuiKey_Comma;
|
||||||
|
case SDL_SCANCODE_PERIOD: return ImGuiKey_Period;
|
||||||
|
case SDL_SCANCODE_SLASH: return ImGuiKey_Slash;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
return ImGuiKey_None;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL2_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & KMOD_CTRL) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & KMOD_SHIFT) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & KMOD_ALT) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & KMOD_GUI) != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ImGuiViewport* ImGui_ImplSDL2_GetViewportForWindowID(Uint32 window_id)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||||
|
return (window_id == bd->WindowID) ? ImGui::GetMainViewport() : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
||||||
|
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
|
||||||
|
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
|
||||||
|
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
||||||
|
// If you have multiple SDL events and some of them are not meant to be used by dear imgui, you may need to filter events based on their windowID field.
|
||||||
|
bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
switch (event->type)
|
||||||
|
{
|
||||||
|
case SDL_MOUSEMOTION:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->motion.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
|
||||||
|
io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
|
||||||
|
io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_MOUSEWHEEL:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->wheel.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
//IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
|
||||||
|
#if SDL_VERSION_ATLEAST(2,0,18) // If this fails to compile on Emscripten: update to latest Emscripten!
|
||||||
|
float wheel_x = -event->wheel.preciseX;
|
||||||
|
float wheel_y = event->wheel.preciseY;
|
||||||
|
#else
|
||||||
|
float wheel_x = -(float)event->wheel.x;
|
||||||
|
float wheel_y = (float)event->wheel.y;
|
||||||
|
#endif
|
||||||
|
#if defined(__EMSCRIPTEN__) && !SDL_VERSION_ATLEAST(2,31,0)
|
||||||
|
wheel_x /= 100.0f;
|
||||||
|
#endif
|
||||||
|
io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
|
||||||
|
io.AddMouseWheelEvent(wheel_x, wheel_y);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
|
case SDL_MOUSEBUTTONUP:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->button.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
int mouse_button = -1;
|
||||||
|
if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
|
||||||
|
if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
|
||||||
|
if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; }
|
||||||
|
if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; }
|
||||||
|
if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; }
|
||||||
|
if (mouse_button == -1)
|
||||||
|
break;
|
||||||
|
io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
|
||||||
|
io.AddMouseButtonEvent(mouse_button, (event->type == SDL_MOUSEBUTTONDOWN));
|
||||||
|
bd->MouseButtonsDown = (event->type == SDL_MOUSEBUTTONDOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_TEXTINPUT:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->text.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
io.AddInputCharactersUTF8(event->text.text);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_KEYDOWN:
|
||||||
|
case SDL_KEYUP:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->key.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->key.keysym.mod);
|
||||||
|
//IMGUI_DEBUG_LOG("SDL_KEY_%s : key=%d ('%s'), scancode=%d ('%s'), mod=%X\n",
|
||||||
|
// (event->type == SDL_KEYDOWN) ? "DOWN" : "UP ", event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym), event->key.keysym.scancode, SDL_GetScancodeName(event->key.keysym.scancode), event->key.keysym.mod);
|
||||||
|
ImGuiKey key = ImGui_ImplSDL2_KeyEventToImGuiKey(event->key.keysym.sym, event->key.keysym.scancode);
|
||||||
|
io.AddKeyEvent(key, (event->type == SDL_KEYDOWN));
|
||||||
|
io.SetKeyEventNativeData(key, (int)event->key.keysym.sym, (int)event->key.keysym.scancode, (int)event->key.keysym.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_WINDOWEVENT:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->window.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
// - When capturing mouse, SDL will send a bunch of conflicting LEAVE/ENTER event on every mouse move, but the final ENTER tends to be right.
|
||||||
|
// - However we won't get a correct LEAVE event for a captured window.
|
||||||
|
// - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
|
||||||
|
// causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
|
||||||
|
// we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
|
||||||
|
Uint8 window_event = event->window.event;
|
||||||
|
if (window_event == SDL_WINDOWEVENT_ENTER)
|
||||||
|
{
|
||||||
|
bd->MouseWindowID = event->window.windowID;
|
||||||
|
bd->MouseLastLeaveFrame = 0;
|
||||||
|
}
|
||||||
|
if (window_event == SDL_WINDOWEVENT_LEAVE)
|
||||||
|
bd->MouseLastLeaveFrame = ImGui::GetFrameCount() + 1;
|
||||||
|
if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED)
|
||||||
|
io.AddFocusEvent(true);
|
||||||
|
else if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST)
|
||||||
|
io.AddFocusEvent(false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_CONTROLLERDEVICEADDED:
|
||||||
|
case SDL_CONTROLLERDEVICEREMOVED:
|
||||||
|
{
|
||||||
|
bd->WantUpdateGamepadsList = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
EM_JS(void, ImGui_ImplSDL2_EmscriptenOpenURL, (char const* url), { url = url ? UTF8ToString(url) : null; if (url) window.open(url, '_blank'); });
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
|
||||||
|
|
||||||
|
// Obtain compiled and runtime versions
|
||||||
|
SDL_version ver_compiled;
|
||||||
|
SDL_version ver_runtime;
|
||||||
|
SDL_VERSION(&ver_compiled);
|
||||||
|
SDL_GetVersion(&ver_runtime);
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplSDL2_Data* bd = IM_NEW(ImGui_ImplSDL2_Data)();
|
||||||
|
snprintf(bd->BackendPlatformName, sizeof(bd->BackendPlatformName), "imgui_impl_sdl2 (%u.%u.%u, %u.%u.%u)",
|
||||||
|
ver_compiled.major, ver_compiled.minor, ver_compiled.patch, ver_runtime.major, ver_runtime.minor, ver_runtime.patch);
|
||||||
|
io.BackendPlatformUserData = (void*)bd;
|
||||||
|
io.BackendPlatformName = bd->BackendPlatformName;
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
|
||||||
|
|
||||||
|
bd->Window = window;
|
||||||
|
bd->WindowID = SDL_GetWindowID(window);
|
||||||
|
bd->Renderer = renderer;
|
||||||
|
|
||||||
|
// Check and store if we are on a SDL backend that supports SDL_GetGlobalMouseState() and SDL_CaptureMouse()
|
||||||
|
// ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
|
||||||
|
bd->MouseCanUseGlobalState = false;
|
||||||
|
bd->MouseCanUseCapture = false;
|
||||||
|
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
|
||||||
|
const char* sdl_backend = SDL_GetCurrentVideoDriver();
|
||||||
|
const char* capture_and_global_state_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
|
||||||
|
for (const char* item : capture_and_global_state_whitelist)
|
||||||
|
if (strncmp(sdl_backend, item, strlen(item)) == 0)
|
||||||
|
bd->MouseCanUseGlobalState = bd->MouseCanUseCapture = true;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
|
||||||
|
platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
|
||||||
|
platform_io.Platform_ClipboardUserData = nullptr;
|
||||||
|
platform_io.Platform_SetImeDataFn = ImGui_ImplSDL2_PlatformSetImeData;
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { ImGui_ImplSDL2_EmscriptenOpenURL(url); return true; };
|
||||||
|
#elif SDL_HAS_OPEN_URL
|
||||||
|
platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { return SDL_OpenURL(url) == 0; };
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Gamepad handling
|
||||||
|
bd->GamepadMode = ImGui_ImplSDL2_GamepadMode_AutoFirst;
|
||||||
|
bd->WantUpdateGamepadsList = true;
|
||||||
|
|
||||||
|
// Load mouse cursors
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Wait] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Progress] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAITARROW);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO);
|
||||||
|
|
||||||
|
// Set platform dependent data in viewport
|
||||||
|
// Our mouse update function expect PlatformHandle to be filled for the main viewport
|
||||||
|
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
|
||||||
|
main_viewport->PlatformHandle = (void*)(intptr_t)bd->WindowID;
|
||||||
|
main_viewport->PlatformHandleRaw = nullptr;
|
||||||
|
SDL_SysWMinfo info;
|
||||||
|
SDL_VERSION(&info.version);
|
||||||
|
if (SDL_GetWindowWMInfo(window, &info))
|
||||||
|
{
|
||||||
|
#if defined(SDL_VIDEO_DRIVER_WINDOWS)
|
||||||
|
main_viewport->PlatformHandleRaw = (void*)info.info.win.window;
|
||||||
|
#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
|
||||||
|
main_viewport->PlatformHandleRaw = (void*)info.info.cocoa.window;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event.
|
||||||
|
// Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
|
||||||
|
// (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
|
||||||
|
// It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
|
||||||
|
// you can ignore SDL_MOUSEBUTTONDOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
|
||||||
|
#ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH
|
||||||
|
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// From 2.0.18: Enable native IME.
|
||||||
|
// IMPORTANT: This is used at the time of SDL_CreateWindow() so this will only affects secondary windows, if any.
|
||||||
|
// For the main window to be affected, your application needs to call this manually before calling SDL_CreateWindow().
|
||||||
|
#ifdef SDL_HINT_IME_SHOW_UI
|
||||||
|
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710)
|
||||||
|
#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
|
||||||
|
SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
(void)sdl_gl_context; // Unused in 'master' branch.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
|
||||||
|
{
|
||||||
|
return ImGui_ImplSDL2_Init(window, nullptr, sdl_gl_context);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window)
|
||||||
|
{
|
||||||
|
#if !SDL_HAS_VULKAN
|
||||||
|
IM_ASSERT(0 && "Unsupported");
|
||||||
|
#endif
|
||||||
|
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window)
|
||||||
|
{
|
||||||
|
#if !defined(_WIN32)
|
||||||
|
IM_ASSERT(0 && "Unsupported");
|
||||||
|
#endif
|
||||||
|
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window)
|
||||||
|
{
|
||||||
|
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
|
||||||
|
{
|
||||||
|
return ImGui_ImplSDL2_Init(window, renderer, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL2_InitForOther(SDL_Window* window)
|
||||||
|
{
|
||||||
|
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL2_CloseGamepads();
|
||||||
|
|
||||||
|
void ImGui_ImplSDL2_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
if (bd->ClipboardTextData)
|
||||||
|
SDL_free(bd->ClipboardTextData);
|
||||||
|
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
|
||||||
|
SDL_FreeCursor(bd->MouseCursors[cursor_n]);
|
||||||
|
ImGui_ImplSDL2_CloseGamepads();
|
||||||
|
|
||||||
|
io.BackendPlatformName = nullptr;
|
||||||
|
io.BackendPlatformUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL2_UpdateMouseData()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// We forward mouse input when hovered or captured (via SDL_MOUSEMOTION) or when focused (below)
|
||||||
|
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
|
||||||
|
// - SDL_CaptureMouse() let the OS know e.g. that our drags can extend outside of parent boundaries (we want updated position) and shouldn't trigger other operations outside.
|
||||||
|
// - Debuggers under Linux tends to leave captured mouse on break, which may be very inconvenient, so to mitigate the issue we wait until mouse has moved to begin capture.
|
||||||
|
if (bd->MouseCanUseCapture)
|
||||||
|
{
|
||||||
|
bool want_capture = false;
|
||||||
|
for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++)
|
||||||
|
if (ImGui::IsMouseDragging(button_n, 1.0f))
|
||||||
|
want_capture = true;
|
||||||
|
SDL_CaptureMouse(want_capture ? SDL_TRUE : SDL_FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Window* focused_window = SDL_GetKeyboardFocus();
|
||||||
|
const bool is_app_focused = (bd->Window == focused_window);
|
||||||
|
#else
|
||||||
|
const bool is_app_focused = (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0; // SDL 2.0.3 and non-windowed systems: single-viewport only
|
||||||
|
#endif
|
||||||
|
if (is_app_focused)
|
||||||
|
{
|
||||||
|
// (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
|
||||||
|
if (io.WantSetMousePos)
|
||||||
|
SDL_WarpMouseInWindow(bd->Window, (int)io.MousePos.x, (int)io.MousePos.y);
|
||||||
|
|
||||||
|
// (Optional) Fallback to provide mouse position when focused (SDL_MOUSEMOTION already provides this when hovered or captured)
|
||||||
|
const bool is_relative_mouse_mode = SDL_GetRelativeMouseMode() != 0;
|
||||||
|
if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode)
|
||||||
|
{
|
||||||
|
// Single-viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
|
||||||
|
int window_x, window_y, mouse_x_global, mouse_y_global;
|
||||||
|
SDL_GetGlobalMouseState(&mouse_x_global, &mouse_y_global);
|
||||||
|
SDL_GetWindowPosition(bd->Window, &window_x, &window_y);
|
||||||
|
io.AddMousePosEvent((float)(mouse_x_global - window_x), (float)(mouse_y_global - window_y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL2_UpdateMouseCursor()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
|
||||||
|
return;
|
||||||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||||
|
|
||||||
|
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
|
||||||
|
if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
|
||||||
|
{
|
||||||
|
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
|
||||||
|
SDL_ShowCursor(SDL_FALSE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Show OS mouse cursor
|
||||||
|
SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow];
|
||||||
|
if (bd->MouseLastCursor != expected_cursor)
|
||||||
|
{
|
||||||
|
SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113)
|
||||||
|
bd->MouseLastCursor = expected_cursor;
|
||||||
|
}
|
||||||
|
SDL_ShowCursor(SDL_TRUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// - On Windows the process needs to be marked DPI-aware!! SDL2 doesn't do it by default. You can call ::SetProcessDPIAware() or call ImGui_ImplWin32_EnableDpiAwareness() from Win32 backend.
|
||||||
|
// - Apple platforms use FramebufferScale so we always return 1.0f.
|
||||||
|
// - Some accessibility applications are declaring virtual monitors with a DPI of 0.0f, see #7902. We preserve this value for caller to handle.
|
||||||
|
float ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window)
|
||||||
|
{
|
||||||
|
return ImGui_ImplSDL2_GetContentScaleForDisplay(SDL_GetWindowDisplayIndex(window));
|
||||||
|
}
|
||||||
|
|
||||||
|
float ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index)
|
||||||
|
{
|
||||||
|
#if SDL_HAS_PER_MONITOR_DPI
|
||||||
|
#ifndef __APPLE__
|
||||||
|
float dpi = 0.0f;
|
||||||
|
if (SDL_GetDisplayDPI(display_index, &dpi, nullptr, nullptr) == 0)
|
||||||
|
return dpi / 96.0f;
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
IM_UNUSED(display_index);
|
||||||
|
return 1.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL2_CloseGamepads()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||||
|
if (bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual)
|
||||||
|
for (SDL_GameController* gamepad : bd->Gamepads)
|
||||||
|
SDL_GameControllerClose(gamepad);
|
||||||
|
bd->Gamepads.resize(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController** manual_gamepads_array, int manual_gamepads_count)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||||
|
ImGui_ImplSDL2_CloseGamepads();
|
||||||
|
if (mode == ImGui_ImplSDL2_GamepadMode_Manual)
|
||||||
|
{
|
||||||
|
IM_ASSERT(manual_gamepads_array != nullptr || manual_gamepads_count <= 0);
|
||||||
|
for (int n = 0; n < manual_gamepads_count; n++)
|
||||||
|
bd->Gamepads.push_back(manual_gamepads_array[n]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0);
|
||||||
|
bd->WantUpdateGamepadsList = true;
|
||||||
|
}
|
||||||
|
bd->GamepadMode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL2_UpdateGamepadButton(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerButton button_no)
|
||||||
|
{
|
||||||
|
bool merged_value = false;
|
||||||
|
for (SDL_GameController* gamepad : bd->Gamepads)
|
||||||
|
merged_value |= SDL_GameControllerGetButton(gamepad, button_no) != 0;
|
||||||
|
io.AddKeyEvent(key, merged_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
|
||||||
|
static void ImGui_ImplSDL2_UpdateGamepadAnalog(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerAxis axis_no, float v0, float v1)
|
||||||
|
{
|
||||||
|
float merged_value = 0.0f;
|
||||||
|
for (SDL_GameController* gamepad : bd->Gamepads)
|
||||||
|
{
|
||||||
|
float vn = Saturate((float)(SDL_GameControllerGetAxis(gamepad, axis_no) - v0) / (float)(v1 - v0));
|
||||||
|
if (merged_value < vn)
|
||||||
|
merged_value = vn;
|
||||||
|
}
|
||||||
|
io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL2_UpdateGamepads()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Update list of controller(s) to use
|
||||||
|
if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_CloseGamepads();
|
||||||
|
int joystick_count = SDL_NumJoysticks();
|
||||||
|
for (int n = 0; n < joystick_count; n++)
|
||||||
|
if (SDL_IsGameController(n))
|
||||||
|
if (SDL_GameController* gamepad = SDL_GameControllerOpen(n))
|
||||||
|
{
|
||||||
|
bd->Gamepads.push_back(gamepad);
|
||||||
|
if (bd->GamepadMode == ImGui_ImplSDL2_GamepadMode_AutoFirst)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bd->WantUpdateGamepadsList = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
|
||||||
|
if (bd->Gamepads.Size == 0)
|
||||||
|
return;
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
|
||||||
|
|
||||||
|
// Update gamepad inputs
|
||||||
|
const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value.
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_CONTROLLER_BUTTON_START);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_CONTROLLER_BUTTON_BACK);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_CONTROLLER_BUTTON_X); // Xbox X, PS Square
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_CONTROLLER_BUTTON_B); // Xbox B, PS Circle
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_CONTROLLER_BUTTON_Y); // Xbox Y, PS Triangle
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_CONTROLLER_BUTTON_A); // Xbox A, PS Cross
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0.0f, 32767);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0.0f, 32767);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_CONTROLLER_BUTTON_LEFTSTICK);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_CONTROLLER_BUTTON_RIGHTSTICK);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32768);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_CONTROLLER_AXIS_RIGHTX, -thumb_dead_zone, -32768);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_CONTROLLER_AXIS_RIGHTX, +thumb_dead_zone, +32767);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_CONTROLLER_AXIS_RIGHTY, -thumb_dead_zone, -32768);
|
||||||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_CONTROLLER_AXIS_RIGHTY, +thumb_dead_zone, +32767);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL2_GetWindowSizeAndFramebufferScale(SDL_Window* window, SDL_Renderer* renderer, ImVec2* out_size, ImVec2* out_framebuffer_scale)
|
||||||
|
{
|
||||||
|
int w, h;
|
||||||
|
int display_w, display_h;
|
||||||
|
SDL_GetWindowSize(window, &w, &h);
|
||||||
|
if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
|
||||||
|
w = h = 0;
|
||||||
|
if (renderer != nullptr)
|
||||||
|
SDL_GetRendererOutputSize(renderer, &display_w, &display_h);
|
||||||
|
#if SDL_HAS_VULKAN
|
||||||
|
else if (SDL_GetWindowFlags(window) & SDL_WINDOW_VULKAN)
|
||||||
|
SDL_Vulkan_GetDrawableSize(window, &display_w, &display_h);
|
||||||
|
#endif
|
||||||
|
else
|
||||||
|
SDL_GL_GetDrawableSize(window, &display_w, &display_h);
|
||||||
|
if (out_size != nullptr)
|
||||||
|
*out_size = ImVec2((float)w, (float)h);
|
||||||
|
if (out_framebuffer_scale != nullptr)
|
||||||
|
*out_framebuffer_scale = (w > 0 && h > 0) ? ImVec2((float)display_w / w, (float)display_h / h) : ImVec2(1.0f, 1.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDL2_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Setup main viewport size (every frame to accommodate for window resizing)
|
||||||
|
ImGui_ImplSDL2_GetWindowSizeAndFramebufferScale(bd->Window, bd->Renderer, &io.DisplaySize, &io.DisplayFramebufferScale);
|
||||||
|
|
||||||
|
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
|
||||||
|
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
|
||||||
|
static Uint64 frequency = SDL_GetPerformanceFrequency();
|
||||||
|
Uint64 current_time = SDL_GetPerformanceCounter();
|
||||||
|
if (current_time <= bd->Time)
|
||||||
|
current_time = bd->Time + 1;
|
||||||
|
io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
|
||||||
|
bd->Time = current_time;
|
||||||
|
|
||||||
|
if (bd->MouseLastLeaveFrame && bd->MouseLastLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
|
||||||
|
{
|
||||||
|
bd->MouseWindowID = 0;
|
||||||
|
bd->MouseLastLeaveFrame = 0;
|
||||||
|
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui_ImplSDL2_UpdateMouseData();
|
||||||
|
ImGui_ImplSDL2_UpdateMouseCursor();
|
||||||
|
|
||||||
|
// Update game controllers (if enabled and available)
|
||||||
|
ImGui_ImplSDL2_UpdateGamepads();
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
50
imgui/backends/imgui_impl_sdl2.h
Normal file
50
imgui/backends/imgui_impl_sdl2.h
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// dear imgui: Platform Backend for SDL2
|
||||||
|
// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
|
||||||
|
// (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Clipboard support.
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen.
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Gamepad support.
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
// [X] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
struct SDL_Window;
|
||||||
|
struct SDL_Renderer;
|
||||||
|
struct _SDL_GameController;
|
||||||
|
typedef union SDL_Event SDL_Event;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForOther(SDL_Window* window);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDL2_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDL2_NewFrame();
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event);
|
||||||
|
|
||||||
|
// DPI-related helpers (optional)
|
||||||
|
IMGUI_IMPL_API float ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window);
|
||||||
|
IMGUI_IMPL_API float ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index);
|
||||||
|
|
||||||
|
// Gamepad selection automatically starts in AutoFirst mode, picking first available SDL_Gamepad. You may override this.
|
||||||
|
// When using manual mode, caller is responsible for opening/closing gamepad.
|
||||||
|
enum ImGui_ImplSDL2_GamepadMode { ImGui_ImplSDL2_GamepadMode_AutoFirst, ImGui_ImplSDL2_GamepadMode_AutoAll, ImGui_ImplSDL2_GamepadMode_Manual };
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController** manual_gamepads_array = nullptr, int manual_gamepads_count = -1);
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
830
imgui/backends/imgui_impl_sdl3.cpp
Normal file
830
imgui/backends/imgui_impl_sdl3.cpp
Normal file
@@ -0,0 +1,830 @@
|
|||||||
|
// dear imgui: Platform Backend for SDL3
|
||||||
|
// This needs to be used along with a Renderer (e.g. SDL_GPU, DirectX11, OpenGL3, Vulkan..)
|
||||||
|
// (Info: SDL3 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Clipboard support.
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen.
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Gamepad support.
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
// [X] Platform: IME support.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-06-27: IME: avoid calling SDL_StartTextInput() again if already active. (#8727)
|
||||||
|
// 2025-04-22: IME: honor ImGuiPlatformImeData->WantTextInput as an alternative way to call SDL_StartTextInput(), without IME being necessarily visible.
|
||||||
|
// 2025-04-09: Don't attempt to call SDL_CaptureMouse() on drivers where we don't call SDL_GetGlobalMouseState(). (#8561)
|
||||||
|
// 2025-03-30: Update for SDL3 api changes: Revert SDL_GetClipboardText() memory ownership change. (#8530, #7801)
|
||||||
|
// 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set.
|
||||||
|
// 2025-03-10: When dealing with OEM keys, use scancodes instead of translated keycodes to choose ImGuiKey values. (#7136, #7201, #7206, #7306, #7670, #7672, #8468)
|
||||||
|
// 2025-02-26: Only start SDL_CaptureMouse() when mouse is being dragged, to mitigate issues with e.g.Linux debuggers not claiming capture back. (#6410, #3650)
|
||||||
|
// 2025-02-24: Avoid calling SDL_GetGlobalMouseState() when mouse is in relative mode.
|
||||||
|
// 2025-02-18: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support.
|
||||||
|
// 2025-02-10: Using SDL_OpenURL() in platform_io.Platform_OpenInShellFn handler.
|
||||||
|
// 2025-01-20: Made ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode_Manual) accept an empty array.
|
||||||
|
// 2024-10-24: Emscripten: SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f on Emscripten.
|
||||||
|
// 2024-09-03: Update for SDL3 api changes: SDL_GetGamepads() memory ownership revert. (#7918, #7898, #7807)
|
||||||
|
// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
|
||||||
|
// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
|
||||||
|
// - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
|
||||||
|
// - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn
|
||||||
|
// 2024-08-19: Storing SDL_WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*.
|
||||||
|
// 2024-08-19: ImGui_ImplSDL3_ProcessEvent() now ignores events intended for other SDL windows. (#7853)
|
||||||
|
// 2024-07-22: Update for SDL3 api changes: SDL_GetGamepads() memory ownership change. (#7807)
|
||||||
|
// 2024-07-18: Update for SDL3 api changes: SDL_GetClipboardText() memory ownership change. (#7801)
|
||||||
|
// 2024-07-15: Update for SDL3 api changes: SDL_GetProperty() change to SDL_GetPointerProperty(). (#7794)
|
||||||
|
// 2024-07-02: Update for SDL3 api changes: SDLK_x renames and SDLK_KP_x removals (#7761, #7762).
|
||||||
|
// 2024-07-01: Update for SDL3 api changes: SDL_SetTextInputRect() changed to SDL_SetTextInputArea().
|
||||||
|
// 2024-06-26: Update for SDL3 api changes: SDL_StartTextInput()/SDL_StopTextInput()/SDL_SetTextInputRect() functions signatures.
|
||||||
|
// 2024-06-24: Update for SDL3 api changes: SDL_EVENT_KEY_DOWN/SDL_EVENT_KEY_UP contents.
|
||||||
|
// 2024-06-03; Update for SDL3 api changes: SDL_SYSTEM_CURSOR_ renames.
|
||||||
|
// 2024-05-15: Update for SDL3 api changes: SDLK_ renames.
|
||||||
|
// 2024-04-15: Inputs: Re-enable calling SDL_StartTextInput()/SDL_StopTextInput() as SDL3 no longer enables it by default and should play nicer with IME.
|
||||||
|
// 2024-02-13: Inputs: Fixed gamepad support. Handle gamepad disconnection. Added ImGui_ImplSDL3_SetGamepadMode().
|
||||||
|
// 2023-11-13: Updated for recent SDL3 API changes.
|
||||||
|
// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
|
||||||
|
// 2023-05-04: Fixed build on Emscripten/iOS/Android. (#6391)
|
||||||
|
// 2023-04-06: Inputs: Avoid calling SDL_StartTextInput()/SDL_StopTextInput() as they don't only pertain to IME. It's unclear exactly what their relation is to IME. (#6306)
|
||||||
|
// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702)
|
||||||
|
// 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644)
|
||||||
|
// 2023-02-07: Forked "imgui_impl_sdl2" into "imgui_impl_sdl3". Removed version checks for old feature. Refer to imgui_impl_sdl2.cpp for older changelog.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_sdl3.h"
|
||||||
|
|
||||||
|
// Clang warnings with -Weverything
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
|
||||||
|
#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// SDL
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <stdio.h> // for snprintf()
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
#include <TargetConditionals.h>
|
||||||
|
#endif
|
||||||
|
#ifdef _WIN32
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
|
||||||
|
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
|
||||||
|
#else
|
||||||
|
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// FIXME-LEGACY: remove when SDL 3.1.3 preview is released.
|
||||||
|
#ifndef SDLK_APOSTROPHE
|
||||||
|
#define SDLK_APOSTROPHE SDLK_QUOTE
|
||||||
|
#endif
|
||||||
|
#ifndef SDLK_GRAVE
|
||||||
|
#define SDLK_GRAVE SDLK_BACKQUOTE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// SDL Data
|
||||||
|
struct ImGui_ImplSDL3_Data
|
||||||
|
{
|
||||||
|
SDL_Window* Window;
|
||||||
|
SDL_WindowID WindowID;
|
||||||
|
SDL_Renderer* Renderer;
|
||||||
|
Uint64 Time;
|
||||||
|
char* ClipboardTextData;
|
||||||
|
char BackendPlatformName[48];
|
||||||
|
|
||||||
|
// IME handling
|
||||||
|
SDL_Window* ImeWindow;
|
||||||
|
|
||||||
|
// Mouse handling
|
||||||
|
Uint32 MouseWindowID;
|
||||||
|
int MouseButtonsDown;
|
||||||
|
SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
|
||||||
|
SDL_Cursor* MouseLastCursor;
|
||||||
|
int MousePendingLeaveFrame;
|
||||||
|
bool MouseCanUseGlobalState;
|
||||||
|
bool MouseCanUseCapture;
|
||||||
|
|
||||||
|
// Gamepad handling
|
||||||
|
ImVector<SDL_Gamepad*> Gamepads;
|
||||||
|
ImGui_ImplSDL3_GamepadMode GamepadMode;
|
||||||
|
bool WantUpdateGamepadsList;
|
||||||
|
|
||||||
|
ImGui_ImplSDL3_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
// FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
|
||||||
|
// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
|
||||||
|
static ImGui_ImplSDL3_Data* ImGui_ImplSDL3_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplSDL3_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
static const char* ImGui_ImplSDL3_GetClipboardText(ImGuiContext*)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
if (bd->ClipboardTextData)
|
||||||
|
SDL_free(bd->ClipboardTextData);
|
||||||
|
bd->ClipboardTextData = SDL_GetClipboardText();
|
||||||
|
return bd->ClipboardTextData;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_SetClipboardText(ImGuiContext*, const char* text)
|
||||||
|
{
|
||||||
|
SDL_SetClipboardText(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
|
||||||
|
SDL_Window* window = SDL_GetWindowFromID(window_id);
|
||||||
|
if ((!(data->WantVisible || data->WantTextInput) || bd->ImeWindow != window) && bd->ImeWindow != nullptr)
|
||||||
|
{
|
||||||
|
SDL_StopTextInput(bd->ImeWindow);
|
||||||
|
bd->ImeWindow = nullptr;
|
||||||
|
}
|
||||||
|
if (data->WantVisible)
|
||||||
|
{
|
||||||
|
SDL_Rect r;
|
||||||
|
r.x = (int)data->InputPos.x;
|
||||||
|
r.y = (int)data->InputPos.y;
|
||||||
|
r.w = 1;
|
||||||
|
r.h = (int)data->InputLineHeight;
|
||||||
|
SDL_SetTextInputArea(window, &r, 0);
|
||||||
|
bd->ImeWindow = window;
|
||||||
|
}
|
||||||
|
if (!SDL_TextInputActive(window) && (data->WantVisible || data->WantTextInput))
|
||||||
|
SDL_StartTextInput(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not static to allow third-party code to use that if they want to (but undocumented)
|
||||||
|
ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode);
|
||||||
|
ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
|
||||||
|
{
|
||||||
|
// Keypad doesn't have individual key values in SDL3
|
||||||
|
switch (scancode)
|
||||||
|
{
|
||||||
|
case SDL_SCANCODE_KP_0: return ImGuiKey_Keypad0;
|
||||||
|
case SDL_SCANCODE_KP_1: return ImGuiKey_Keypad1;
|
||||||
|
case SDL_SCANCODE_KP_2: return ImGuiKey_Keypad2;
|
||||||
|
case SDL_SCANCODE_KP_3: return ImGuiKey_Keypad3;
|
||||||
|
case SDL_SCANCODE_KP_4: return ImGuiKey_Keypad4;
|
||||||
|
case SDL_SCANCODE_KP_5: return ImGuiKey_Keypad5;
|
||||||
|
case SDL_SCANCODE_KP_6: return ImGuiKey_Keypad6;
|
||||||
|
case SDL_SCANCODE_KP_7: return ImGuiKey_Keypad7;
|
||||||
|
case SDL_SCANCODE_KP_8: return ImGuiKey_Keypad8;
|
||||||
|
case SDL_SCANCODE_KP_9: return ImGuiKey_Keypad9;
|
||||||
|
case SDL_SCANCODE_KP_PERIOD: return ImGuiKey_KeypadDecimal;
|
||||||
|
case SDL_SCANCODE_KP_DIVIDE: return ImGuiKey_KeypadDivide;
|
||||||
|
case SDL_SCANCODE_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
|
||||||
|
case SDL_SCANCODE_KP_MINUS: return ImGuiKey_KeypadSubtract;
|
||||||
|
case SDL_SCANCODE_KP_PLUS: return ImGuiKey_KeypadAdd;
|
||||||
|
case SDL_SCANCODE_KP_ENTER: return ImGuiKey_KeypadEnter;
|
||||||
|
case SDL_SCANCODE_KP_EQUALS: return ImGuiKey_KeypadEqual;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
switch (keycode)
|
||||||
|
{
|
||||||
|
case SDLK_TAB: return ImGuiKey_Tab;
|
||||||
|
case SDLK_LEFT: return ImGuiKey_LeftArrow;
|
||||||
|
case SDLK_RIGHT: return ImGuiKey_RightArrow;
|
||||||
|
case SDLK_UP: return ImGuiKey_UpArrow;
|
||||||
|
case SDLK_DOWN: return ImGuiKey_DownArrow;
|
||||||
|
case SDLK_PAGEUP: return ImGuiKey_PageUp;
|
||||||
|
case SDLK_PAGEDOWN: return ImGuiKey_PageDown;
|
||||||
|
case SDLK_HOME: return ImGuiKey_Home;
|
||||||
|
case SDLK_END: return ImGuiKey_End;
|
||||||
|
case SDLK_INSERT: return ImGuiKey_Insert;
|
||||||
|
case SDLK_DELETE: return ImGuiKey_Delete;
|
||||||
|
case SDLK_BACKSPACE: return ImGuiKey_Backspace;
|
||||||
|
case SDLK_SPACE: return ImGuiKey_Space;
|
||||||
|
case SDLK_RETURN: return ImGuiKey_Enter;
|
||||||
|
case SDLK_ESCAPE: return ImGuiKey_Escape;
|
||||||
|
//case SDLK_APOSTROPHE: return ImGuiKey_Apostrophe;
|
||||||
|
case SDLK_COMMA: return ImGuiKey_Comma;
|
||||||
|
//case SDLK_MINUS: return ImGuiKey_Minus;
|
||||||
|
case SDLK_PERIOD: return ImGuiKey_Period;
|
||||||
|
//case SDLK_SLASH: return ImGuiKey_Slash;
|
||||||
|
case SDLK_SEMICOLON: return ImGuiKey_Semicolon;
|
||||||
|
//case SDLK_EQUALS: return ImGuiKey_Equal;
|
||||||
|
//case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket;
|
||||||
|
//case SDLK_BACKSLASH: return ImGuiKey_Backslash;
|
||||||
|
//case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket;
|
||||||
|
//case SDLK_GRAVE: return ImGuiKey_GraveAccent;
|
||||||
|
case SDLK_CAPSLOCK: return ImGuiKey_CapsLock;
|
||||||
|
case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock;
|
||||||
|
case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock;
|
||||||
|
case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen;
|
||||||
|
case SDLK_PAUSE: return ImGuiKey_Pause;
|
||||||
|
case SDLK_LCTRL: return ImGuiKey_LeftCtrl;
|
||||||
|
case SDLK_LSHIFT: return ImGuiKey_LeftShift;
|
||||||
|
case SDLK_LALT: return ImGuiKey_LeftAlt;
|
||||||
|
case SDLK_LGUI: return ImGuiKey_LeftSuper;
|
||||||
|
case SDLK_RCTRL: return ImGuiKey_RightCtrl;
|
||||||
|
case SDLK_RSHIFT: return ImGuiKey_RightShift;
|
||||||
|
case SDLK_RALT: return ImGuiKey_RightAlt;
|
||||||
|
case SDLK_RGUI: return ImGuiKey_RightSuper;
|
||||||
|
case SDLK_APPLICATION: return ImGuiKey_Menu;
|
||||||
|
case SDLK_0: return ImGuiKey_0;
|
||||||
|
case SDLK_1: return ImGuiKey_1;
|
||||||
|
case SDLK_2: return ImGuiKey_2;
|
||||||
|
case SDLK_3: return ImGuiKey_3;
|
||||||
|
case SDLK_4: return ImGuiKey_4;
|
||||||
|
case SDLK_5: return ImGuiKey_5;
|
||||||
|
case SDLK_6: return ImGuiKey_6;
|
||||||
|
case SDLK_7: return ImGuiKey_7;
|
||||||
|
case SDLK_8: return ImGuiKey_8;
|
||||||
|
case SDLK_9: return ImGuiKey_9;
|
||||||
|
case SDLK_A: return ImGuiKey_A;
|
||||||
|
case SDLK_B: return ImGuiKey_B;
|
||||||
|
case SDLK_C: return ImGuiKey_C;
|
||||||
|
case SDLK_D: return ImGuiKey_D;
|
||||||
|
case SDLK_E: return ImGuiKey_E;
|
||||||
|
case SDLK_F: return ImGuiKey_F;
|
||||||
|
case SDLK_G: return ImGuiKey_G;
|
||||||
|
case SDLK_H: return ImGuiKey_H;
|
||||||
|
case SDLK_I: return ImGuiKey_I;
|
||||||
|
case SDLK_J: return ImGuiKey_J;
|
||||||
|
case SDLK_K: return ImGuiKey_K;
|
||||||
|
case SDLK_L: return ImGuiKey_L;
|
||||||
|
case SDLK_M: return ImGuiKey_M;
|
||||||
|
case SDLK_N: return ImGuiKey_N;
|
||||||
|
case SDLK_O: return ImGuiKey_O;
|
||||||
|
case SDLK_P: return ImGuiKey_P;
|
||||||
|
case SDLK_Q: return ImGuiKey_Q;
|
||||||
|
case SDLK_R: return ImGuiKey_R;
|
||||||
|
case SDLK_S: return ImGuiKey_S;
|
||||||
|
case SDLK_T: return ImGuiKey_T;
|
||||||
|
case SDLK_U: return ImGuiKey_U;
|
||||||
|
case SDLK_V: return ImGuiKey_V;
|
||||||
|
case SDLK_W: return ImGuiKey_W;
|
||||||
|
case SDLK_X: return ImGuiKey_X;
|
||||||
|
case SDLK_Y: return ImGuiKey_Y;
|
||||||
|
case SDLK_Z: return ImGuiKey_Z;
|
||||||
|
case SDLK_F1: return ImGuiKey_F1;
|
||||||
|
case SDLK_F2: return ImGuiKey_F2;
|
||||||
|
case SDLK_F3: return ImGuiKey_F3;
|
||||||
|
case SDLK_F4: return ImGuiKey_F4;
|
||||||
|
case SDLK_F5: return ImGuiKey_F5;
|
||||||
|
case SDLK_F6: return ImGuiKey_F6;
|
||||||
|
case SDLK_F7: return ImGuiKey_F7;
|
||||||
|
case SDLK_F8: return ImGuiKey_F8;
|
||||||
|
case SDLK_F9: return ImGuiKey_F9;
|
||||||
|
case SDLK_F10: return ImGuiKey_F10;
|
||||||
|
case SDLK_F11: return ImGuiKey_F11;
|
||||||
|
case SDLK_F12: return ImGuiKey_F12;
|
||||||
|
case SDLK_F13: return ImGuiKey_F13;
|
||||||
|
case SDLK_F14: return ImGuiKey_F14;
|
||||||
|
case SDLK_F15: return ImGuiKey_F15;
|
||||||
|
case SDLK_F16: return ImGuiKey_F16;
|
||||||
|
case SDLK_F17: return ImGuiKey_F17;
|
||||||
|
case SDLK_F18: return ImGuiKey_F18;
|
||||||
|
case SDLK_F19: return ImGuiKey_F19;
|
||||||
|
case SDLK_F20: return ImGuiKey_F20;
|
||||||
|
case SDLK_F21: return ImGuiKey_F21;
|
||||||
|
case SDLK_F22: return ImGuiKey_F22;
|
||||||
|
case SDLK_F23: return ImGuiKey_F23;
|
||||||
|
case SDLK_F24: return ImGuiKey_F24;
|
||||||
|
case SDLK_AC_BACK: return ImGuiKey_AppBack;
|
||||||
|
case SDLK_AC_FORWARD: return ImGuiKey_AppForward;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to scancode
|
||||||
|
switch (scancode)
|
||||||
|
{
|
||||||
|
case SDL_SCANCODE_GRAVE: return ImGuiKey_GraveAccent;
|
||||||
|
case SDL_SCANCODE_MINUS: return ImGuiKey_Minus;
|
||||||
|
case SDL_SCANCODE_EQUALS: return ImGuiKey_Equal;
|
||||||
|
case SDL_SCANCODE_LEFTBRACKET: return ImGuiKey_LeftBracket;
|
||||||
|
case SDL_SCANCODE_RIGHTBRACKET: return ImGuiKey_RightBracket;
|
||||||
|
case SDL_SCANCODE_NONUSBACKSLASH: return ImGuiKey_Oem102;
|
||||||
|
case SDL_SCANCODE_BACKSLASH: return ImGuiKey_Backslash;
|
||||||
|
case SDL_SCANCODE_SEMICOLON: return ImGuiKey_Semicolon;
|
||||||
|
case SDL_SCANCODE_APOSTROPHE: return ImGuiKey_Apostrophe;
|
||||||
|
case SDL_SCANCODE_COMMA: return ImGuiKey_Comma;
|
||||||
|
case SDL_SCANCODE_PERIOD: return ImGuiKey_Period;
|
||||||
|
case SDL_SCANCODE_SLASH: return ImGuiKey_Slash;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
return ImGuiKey_None;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & SDL_KMOD_CTRL) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & SDL_KMOD_SHIFT) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & SDL_KMOD_ALT) != 0);
|
||||||
|
io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & SDL_KMOD_GUI) != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static ImGuiViewport* ImGui_ImplSDL3_GetViewportForWindowID(SDL_WindowID window_id)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
return (window_id == bd->WindowID) ? ImGui::GetMainViewport() : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
||||||
|
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
|
||||||
|
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
|
||||||
|
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
||||||
|
// If you have multiple SDL events and some of them are not meant to be used by dear imgui, you may need to filter events based on their windowID field.
|
||||||
|
bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
switch (event->type)
|
||||||
|
{
|
||||||
|
case SDL_EVENT_MOUSE_MOTION:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL3_GetViewportForWindowID(event->motion.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
|
||||||
|
io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
|
||||||
|
io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_EVENT_MOUSE_WHEEL:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL3_GetViewportForWindowID(event->wheel.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
//IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
|
||||||
|
float wheel_x = -event->wheel.x;
|
||||||
|
float wheel_y = event->wheel.y;
|
||||||
|
io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
|
||||||
|
io.AddMouseWheelEvent(wheel_x, wheel_y);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
||||||
|
case SDL_EVENT_MOUSE_BUTTON_UP:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL3_GetViewportForWindowID(event->button.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
int mouse_button = -1;
|
||||||
|
if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
|
||||||
|
if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
|
||||||
|
if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; }
|
||||||
|
if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; }
|
||||||
|
if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; }
|
||||||
|
if (mouse_button == -1)
|
||||||
|
break;
|
||||||
|
io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
|
||||||
|
io.AddMouseButtonEvent(mouse_button, (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN));
|
||||||
|
bd->MouseButtonsDown = (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_EVENT_TEXT_INPUT:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL3_GetViewportForWindowID(event->text.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
io.AddInputCharactersUTF8(event->text.text);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_EVENT_KEY_DOWN:
|
||||||
|
case SDL_EVENT_KEY_UP:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL3_GetViewportForWindowID(event->key.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.mod);
|
||||||
|
//IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%s : key=%d ('%s'), scancode=%d ('%s'), mod=%X\n",
|
||||||
|
// (event->type == SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP ", event->key.key, SDL_GetKeyName(event->key.key), event->key.scancode, SDL_GetScancodeName(event->key.scancode), event->key.mod);
|
||||||
|
ImGuiKey key = ImGui_ImplSDL3_KeyEventToImGuiKey(event->key.key, event->key.scancode);
|
||||||
|
io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN));
|
||||||
|
io.SetKeyEventNativeData(key, (int)event->key.key, (int)event->key.scancode, (int)event->key.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_EVENT_WINDOW_MOUSE_ENTER:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
bd->MouseWindowID = event->window.windowID;
|
||||||
|
bd->MousePendingLeaveFrame = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
|
||||||
|
// causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
|
||||||
|
// we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
|
||||||
|
// FIXME: Unconfirmed whether this is still needed with SDL3.
|
||||||
|
case SDL_EVENT_WINDOW_MOUSE_LEAVE:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
bd->MousePendingLeaveFrame = ImGui::GetFrameCount() + 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_EVENT_WINDOW_FOCUS_GAINED:
|
||||||
|
case SDL_EVENT_WINDOW_FOCUS_LOST:
|
||||||
|
{
|
||||||
|
if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr)
|
||||||
|
return false;
|
||||||
|
io.AddFocusEvent(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_EVENT_GAMEPAD_ADDED:
|
||||||
|
case SDL_EVENT_GAMEPAD_REMOVED:
|
||||||
|
{
|
||||||
|
bd->WantUpdateGamepadsList = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window)
|
||||||
|
{
|
||||||
|
viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window);
|
||||||
|
viewport->PlatformHandleRaw = nullptr;
|
||||||
|
#if defined(_WIN32) && !defined(__WINRT__)
|
||||||
|
viewport->PlatformHandleRaw = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
viewport->PlatformHandleRaw = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
|
||||||
|
IM_UNUSED(sdl_gl_context); // Unused in this branch
|
||||||
|
|
||||||
|
const int ver_linked = SDL_GetVersion();
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplSDL3_Data* bd = IM_NEW(ImGui_ImplSDL3_Data)();
|
||||||
|
snprintf(bd->BackendPlatformName, sizeof(bd->BackendPlatformName), "imgui_impl_sdl3 (%d.%d.%d; %d.%d.%d)",
|
||||||
|
SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_MICRO_VERSION, SDL_VERSIONNUM_MAJOR(ver_linked), SDL_VERSIONNUM_MINOR(ver_linked), SDL_VERSIONNUM_MICRO(ver_linked));
|
||||||
|
io.BackendPlatformUserData = (void*)bd;
|
||||||
|
io.BackendPlatformName = bd->BackendPlatformName;
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
|
||||||
|
|
||||||
|
bd->Window = window;
|
||||||
|
bd->WindowID = SDL_GetWindowID(window);
|
||||||
|
bd->Renderer = renderer;
|
||||||
|
|
||||||
|
// Check and store if we are on a SDL backend that supports SDL_GetGlobalMouseState() and SDL_CaptureMouse()
|
||||||
|
// ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
|
||||||
|
bd->MouseCanUseGlobalState = false;
|
||||||
|
bd->MouseCanUseCapture = false;
|
||||||
|
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
|
||||||
|
const char* sdl_backend = SDL_GetCurrentVideoDriver();
|
||||||
|
const char* capture_and_global_state_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
|
||||||
|
for (const char* item : capture_and_global_state_whitelist)
|
||||||
|
if (strncmp(sdl_backend, item, strlen(item)) == 0)
|
||||||
|
bd->MouseCanUseGlobalState = bd->MouseCanUseCapture = true;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText;
|
||||||
|
platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText;
|
||||||
|
platform_io.Platform_SetImeDataFn = ImGui_ImplSDL3_PlatformSetImeData;
|
||||||
|
platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { return SDL_OpenURL(url) == 0; };
|
||||||
|
|
||||||
|
// Gamepad handling
|
||||||
|
bd->GamepadMode = ImGui_ImplSDL3_GamepadMode_AutoFirst;
|
||||||
|
bd->WantUpdateGamepadsList = true;
|
||||||
|
|
||||||
|
// Load mouse cursors
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_MOVE);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NS_RESIZE);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_EW_RESIZE);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NESW_RESIZE);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NWSE_RESIZE);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_POINTER);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Wait] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_Progress] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_PROGRESS);
|
||||||
|
bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NOT_ALLOWED);
|
||||||
|
|
||||||
|
// Set platform dependent data in viewport
|
||||||
|
// Our mouse update function expect PlatformHandle to be filled for the main viewport
|
||||||
|
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
|
||||||
|
ImGui_ImplSDL3_SetupPlatformHandles(main_viewport, window);
|
||||||
|
|
||||||
|
// From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event.
|
||||||
|
// Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
|
||||||
|
// (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
|
||||||
|
// It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
|
||||||
|
// you can ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
|
||||||
|
#ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH
|
||||||
|
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710)
|
||||||
|
#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
|
||||||
|
SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
|
||||||
|
{
|
||||||
|
IM_UNUSED(sdl_gl_context); // Viewport branch will need this.
|
||||||
|
return ImGui_ImplSDL3_Init(window, nullptr, sdl_gl_context);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window)
|
||||||
|
{
|
||||||
|
return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window)
|
||||||
|
{
|
||||||
|
#if !defined(_WIN32)
|
||||||
|
IM_ASSERT(0 && "Unsupported");
|
||||||
|
#endif
|
||||||
|
return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window)
|
||||||
|
{
|
||||||
|
return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
|
||||||
|
{
|
||||||
|
return ImGui_ImplSDL3_Init(window, renderer, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL3_InitForSDLGPU(SDL_Window* window)
|
||||||
|
{
|
||||||
|
return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDL3_InitForOther(SDL_Window* window)
|
||||||
|
{
|
||||||
|
return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_CloseGamepads();
|
||||||
|
|
||||||
|
void ImGui_ImplSDL3_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
if (bd->ClipboardTextData)
|
||||||
|
SDL_free(bd->ClipboardTextData);
|
||||||
|
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
|
||||||
|
SDL_DestroyCursor(bd->MouseCursors[cursor_n]);
|
||||||
|
ImGui_ImplSDL3_CloseGamepads();
|
||||||
|
|
||||||
|
io.BackendPlatformName = nullptr;
|
||||||
|
io.BackendPlatformUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_UpdateMouseData()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or when focused (below)
|
||||||
|
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
|
||||||
|
// - SDL_CaptureMouse() let the OS know e.g. that our drags can extend outside of parent boundaries (we want updated position) and shouldn't trigger other operations outside.
|
||||||
|
// - Debuggers under Linux tends to leave captured mouse on break, which may be very inconvenient, so to mitigate the issue we wait until mouse has moved to begin capture.
|
||||||
|
if (bd->MouseCanUseCapture)
|
||||||
|
{
|
||||||
|
bool want_capture = false;
|
||||||
|
for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++)
|
||||||
|
if (ImGui::IsMouseDragging(button_n, 1.0f))
|
||||||
|
want_capture = true;
|
||||||
|
SDL_CaptureMouse(want_capture);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Window* focused_window = SDL_GetKeyboardFocus();
|
||||||
|
const bool is_app_focused = (bd->Window == focused_window);
|
||||||
|
#else
|
||||||
|
SDL_Window* focused_window = bd->Window;
|
||||||
|
const bool is_app_focused = (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0; // SDL 2.0.3 and non-windowed systems: single-viewport only
|
||||||
|
#endif
|
||||||
|
if (is_app_focused)
|
||||||
|
{
|
||||||
|
// (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
|
||||||
|
if (io.WantSetMousePos)
|
||||||
|
SDL_WarpMouseInWindow(bd->Window, io.MousePos.x, io.MousePos.y);
|
||||||
|
|
||||||
|
// (Optional) Fallback to provide mouse position when focused (SDL_EVENT_MOUSE_MOTION already provides this when hovered or captured)
|
||||||
|
const bool is_relative_mouse_mode = SDL_GetWindowRelativeMouseMode(bd->Window);
|
||||||
|
if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode)
|
||||||
|
{
|
||||||
|
// Single-viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
|
||||||
|
float mouse_x_global, mouse_y_global;
|
||||||
|
int window_x, window_y;
|
||||||
|
SDL_GetGlobalMouseState(&mouse_x_global, &mouse_y_global);
|
||||||
|
SDL_GetWindowPosition(focused_window, &window_x, &window_y);
|
||||||
|
io.AddMousePosEvent(mouse_x_global - window_x, mouse_y_global - window_y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_UpdateMouseCursor()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
|
||||||
|
return;
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
|
||||||
|
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
|
||||||
|
if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
|
||||||
|
{
|
||||||
|
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
|
||||||
|
SDL_HideCursor();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Show OS mouse cursor
|
||||||
|
SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow];
|
||||||
|
if (bd->MouseLastCursor != expected_cursor)
|
||||||
|
{
|
||||||
|
SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113)
|
||||||
|
bd->MouseLastCursor = expected_cursor;
|
||||||
|
}
|
||||||
|
SDL_ShowCursor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_CloseGamepads()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
if (bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual)
|
||||||
|
for (SDL_Gamepad* gamepad : bd->Gamepads)
|
||||||
|
SDL_CloseGamepad(gamepad);
|
||||||
|
bd->Gamepads.resize(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, SDL_Gamepad** manual_gamepads_array, int manual_gamepads_count)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
ImGui_ImplSDL3_CloseGamepads();
|
||||||
|
if (mode == ImGui_ImplSDL3_GamepadMode_Manual)
|
||||||
|
{
|
||||||
|
IM_ASSERT(manual_gamepads_array != nullptr || manual_gamepads_count <= 0);
|
||||||
|
for (int n = 0; n < manual_gamepads_count; n++)
|
||||||
|
bd->Gamepads.push_back(manual_gamepads_array[n]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0);
|
||||||
|
bd->WantUpdateGamepadsList = true;
|
||||||
|
}
|
||||||
|
bd->GamepadMode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_UpdateGamepadButton(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadButton button_no)
|
||||||
|
{
|
||||||
|
bool merged_value = false;
|
||||||
|
for (SDL_Gamepad* gamepad : bd->Gamepads)
|
||||||
|
merged_value |= SDL_GetGamepadButton(gamepad, button_no) != 0;
|
||||||
|
io.AddKeyEvent(key, merged_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
|
||||||
|
static void ImGui_ImplSDL3_UpdateGamepadAnalog(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadAxis axis_no, float v0, float v1)
|
||||||
|
{
|
||||||
|
float merged_value = 0.0f;
|
||||||
|
for (SDL_Gamepad* gamepad : bd->Gamepads)
|
||||||
|
{
|
||||||
|
float vn = Saturate((float)(SDL_GetGamepadAxis(gamepad, axis_no) - v0) / (float)(v1 - v0));
|
||||||
|
if (merged_value < vn)
|
||||||
|
merged_value = vn;
|
||||||
|
}
|
||||||
|
io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_UpdateGamepads()
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
|
||||||
|
// Update list of gamepads to use
|
||||||
|
if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL3_CloseGamepads();
|
||||||
|
int sdl_gamepads_count = 0;
|
||||||
|
SDL_JoystickID* sdl_gamepads = SDL_GetGamepads(&sdl_gamepads_count);
|
||||||
|
for (int n = 0; n < sdl_gamepads_count; n++)
|
||||||
|
if (SDL_Gamepad* gamepad = SDL_OpenGamepad(sdl_gamepads[n]))
|
||||||
|
{
|
||||||
|
bd->Gamepads.push_back(gamepad);
|
||||||
|
if (bd->GamepadMode == ImGui_ImplSDL3_GamepadMode_AutoFirst)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bd->WantUpdateGamepadsList = false;
|
||||||
|
SDL_free(sdl_gamepads);
|
||||||
|
}
|
||||||
|
|
||||||
|
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
|
||||||
|
if (bd->Gamepads.Size == 0)
|
||||||
|
return;
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
|
||||||
|
|
||||||
|
// Update gamepad inputs
|
||||||
|
const int thumb_dead_zone = 8000; // SDL_gamepad.h suggests using this value.
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_GAMEPAD_BUTTON_START);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_GAMEPAD_BUTTON_BACK);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_GAMEPAD_BUTTON_WEST); // Xbox X, PS Square
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_GAMEPAD_BUTTON_EAST); // Xbox B, PS Circle
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_GAMEPAD_BUTTON_NORTH); // Xbox Y, PS Triangle
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_GAMEPAD_BUTTON_SOUTH); // Xbox A, PS Cross
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_GAMEPAD_BUTTON_DPAD_LEFT);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_GAMEPAD_BUTTON_DPAD_UP);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_GAMEPAD_BUTTON_DPAD_DOWN);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0.0f, 32767);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0.0f, 32767);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_GAMEPAD_BUTTON_LEFT_STICK);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_GAMEPAD_BUTTON_RIGHT_STICK);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_GAMEPAD_AXIS_LEFTX, -thumb_dead_zone, -32768);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_GAMEPAD_AXIS_LEFTX, +thumb_dead_zone, +32767);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_GAMEPAD_AXIS_LEFTY, -thumb_dead_zone, -32768);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_GAMEPAD_AXIS_LEFTY, +thumb_dead_zone, +32767);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_GAMEPAD_AXIS_RIGHTX, -thumb_dead_zone, -32768);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_GAMEPAD_AXIS_RIGHTX, +thumb_dead_zone, +32767);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_GAMEPAD_AXIS_RIGHTY, -thumb_dead_zone, -32768);
|
||||||
|
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_GAMEPAD_AXIS_RIGHTY, +thumb_dead_zone, +32767);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(SDL_Window* window, ImVec2* out_size, ImVec2* out_framebuffer_scale)
|
||||||
|
{
|
||||||
|
int w, h;
|
||||||
|
int display_w, display_h;
|
||||||
|
SDL_GetWindowSize(window, &w, &h);
|
||||||
|
if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
|
||||||
|
w = h = 0;
|
||||||
|
SDL_GetWindowSizeInPixels(window, &display_w, &display_h);
|
||||||
|
if (out_size != nullptr)
|
||||||
|
*out_size = ImVec2((float)w, (float)h);
|
||||||
|
if (out_framebuffer_scale != nullptr)
|
||||||
|
*out_framebuffer_scale = (w > 0 && h > 0) ? ImVec2((float)display_w / w, (float)display_h / h) : ImVec2(1.0f, 1.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDL3_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Setup main viewport size (every frame to accommodate for window resizing)
|
||||||
|
ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(bd->Window, &io.DisplaySize, &io.DisplayFramebufferScale);
|
||||||
|
|
||||||
|
// Setup time step (we could also use SDL_GetTicksNS() available since SDL3)
|
||||||
|
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
|
||||||
|
static Uint64 frequency = SDL_GetPerformanceFrequency();
|
||||||
|
Uint64 current_time = SDL_GetPerformanceCounter();
|
||||||
|
if (current_time <= bd->Time)
|
||||||
|
current_time = bd->Time + 1;
|
||||||
|
io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
|
||||||
|
bd->Time = current_time;
|
||||||
|
|
||||||
|
if (bd->MousePendingLeaveFrame && bd->MousePendingLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
|
||||||
|
{
|
||||||
|
bd->MouseWindowID = 0;
|
||||||
|
bd->MousePendingLeaveFrame = 0;
|
||||||
|
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui_ImplSDL3_UpdateMouseData();
|
||||||
|
ImGui_ImplSDL3_UpdateMouseCursor();
|
||||||
|
|
||||||
|
// Update game controllers (if enabled and available)
|
||||||
|
ImGui_ImplSDL3_UpdateGamepads();
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
47
imgui/backends/imgui_impl_sdl3.h
Normal file
47
imgui/backends/imgui_impl_sdl3.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
// dear imgui: Platform Backend for SDL3
|
||||||
|
// This needs to be used along with a Renderer (e.g. SDL_GPU, DirectX11, OpenGL3, Vulkan..)
|
||||||
|
// (Info: SDL3 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Clipboard support.
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen.
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Gamepad support.
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
// [X] Platform: IME support.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
struct SDL_Window;
|
||||||
|
struct SDL_Renderer;
|
||||||
|
struct SDL_Gamepad;
|
||||||
|
typedef union SDL_Event SDL_Event;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForSDLGPU(SDL_Window* window);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForOther(SDL_Window* window);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDL3_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDL3_NewFrame();
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event);
|
||||||
|
|
||||||
|
// Gamepad selection automatically starts in AutoFirst mode, picking first available SDL_Gamepad. You may override this.
|
||||||
|
// When using manual mode, caller is responsible for opening/closing gamepad.
|
||||||
|
enum ImGui_ImplSDL3_GamepadMode { ImGui_ImplSDL3_GamepadMode_AutoFirst, ImGui_ImplSDL3_GamepadMode_AutoAll, ImGui_ImplSDL3_GamepadMode_Manual };
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, SDL_Gamepad** manual_gamepads_array = nullptr, int manual_gamepads_count = -1);
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
661
imgui/backends/imgui_impl_sdlgpu3.cpp
Normal file
661
imgui/backends/imgui_impl_sdlgpu3.cpp
Normal file
@@ -0,0 +1,661 @@
|
|||||||
|
// dear imgui: Renderer Backend for SDL_GPU
|
||||||
|
// This needs to be used along with the SDL3 Platform Backend
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use simply cast a reference to your SDL_GPUTextureSamplerBinding to ImTextureID.
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
|
||||||
|
// The aim of imgui_impl_sdlgpu3.h/.cpp is to be usable in your engine without any modification.
|
||||||
|
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// Important note to the reader who wish to integrate imgui_impl_sdlgpu3.cpp/.h in their own engine/app.
|
||||||
|
// - Unlike other backends, the user must call the function ImGui_ImplSDLGPU3_PrepareDrawData() BEFORE issuing a SDL_GPURenderPass containing ImGui_ImplSDLGPU3_RenderDrawData.
|
||||||
|
// Calling the function is MANDATORY, otherwise the ImGui will not upload neither the vertex nor the index buffer for the GPU. See imgui_impl_sdlgpu3.cpp for more info.
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// 2025-06-25: Mapping transfer buffer for texture update use cycle=true. Fixes artifacts e.g. on Metal backend.
|
||||||
|
// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplSDLGPU3_CreateFontsTexture() and ImGui_ImplSDLGPU3_DestroyFontsTexture().
|
||||||
|
// 2025-04-28: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
|
||||||
|
// 2025-03-30: Made ImGui_ImplSDLGPU3_PrepareDrawData() reuse GPU Transfer Buffers which were unusually slow to recreate every frame. Much faster now.
|
||||||
|
// 2025-03-21: Fixed typo in function name Imgui_ImplSDLGPU3_PrepareDrawData() -> ImGui_ImplSDLGPU3_PrepareDrawData().
|
||||||
|
// 2025-01-16: Renamed ImGui_ImplSDLGPU3_InitInfo::GpuDevice to Device.
|
||||||
|
// 2025-01-09: SDL_GPU: Added the SDL_GPU3 backend.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_sdlgpu3.h"
|
||||||
|
#include "imgui_impl_sdlgpu3_shaders.h"
|
||||||
|
|
||||||
|
// SDL_GPU Data
|
||||||
|
struct ImGui_ImplSDLGPU3_Texture
|
||||||
|
{
|
||||||
|
SDL_GPUTexture* Texture = nullptr;
|
||||||
|
SDL_GPUTextureSamplerBinding TextureSamplerBinding = { nullptr, nullptr };
|
||||||
|
};
|
||||||
|
|
||||||
|
// Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplSDLGPU3_RenderDrawData()
|
||||||
|
struct ImGui_ImplSDLGPU3_FrameData
|
||||||
|
{
|
||||||
|
SDL_GPUBuffer* VertexBuffer = nullptr;
|
||||||
|
SDL_GPUTransferBuffer* VertexTransferBuffer = nullptr;
|
||||||
|
uint32_t VertexBufferSize = 0;
|
||||||
|
SDL_GPUBuffer* IndexBuffer = nullptr;
|
||||||
|
SDL_GPUTransferBuffer* IndexTransferBuffer = nullptr;
|
||||||
|
uint32_t IndexBufferSize = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImGui_ImplSDLGPU3_Data
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_InitInfo InitInfo;
|
||||||
|
|
||||||
|
// Graphics pipeline & shaders
|
||||||
|
SDL_GPUShader* VertexShader = nullptr;
|
||||||
|
SDL_GPUShader* FragmentShader = nullptr;
|
||||||
|
SDL_GPUGraphicsPipeline* Pipeline = nullptr;
|
||||||
|
SDL_GPUSampler* TexSampler = nullptr;
|
||||||
|
SDL_GPUTransferBuffer* TexTransferBuffer = nullptr;
|
||||||
|
uint32_t TexTransferBufferSize = 0;
|
||||||
|
|
||||||
|
// Frame data for main window
|
||||||
|
ImGui_ImplSDLGPU3_FrameData MainWindowFrameData;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Forward Declarations
|
||||||
|
static void ImGui_ImplSDLGPU3_DestroyFrameData();
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// FUNCTIONS
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
// FIXME: multi-context support has never been tested.
|
||||||
|
static ImGui_ImplSDLGPU3_Data* ImGui_ImplSDLGPU3_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplSDLGPU3_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDLGPU3_SetupRenderState(ImDrawData* draw_data, SDL_GPUGraphicsPipeline* pipeline, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass * render_pass, ImGui_ImplSDLGPU3_FrameData* fd, uint32_t fb_width, uint32_t fb_height)
|
||||||
|
{
|
||||||
|
//ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
|
||||||
|
// Bind graphics pipeline
|
||||||
|
SDL_BindGPUGraphicsPipeline(render_pass,pipeline);
|
||||||
|
|
||||||
|
// Bind Vertex And Index Buffers
|
||||||
|
if (draw_data->TotalVtxCount > 0)
|
||||||
|
{
|
||||||
|
SDL_GPUBufferBinding vertex_buffer_binding = {};
|
||||||
|
vertex_buffer_binding.buffer = fd->VertexBuffer;
|
||||||
|
vertex_buffer_binding.offset = 0;
|
||||||
|
SDL_GPUBufferBinding index_buffer_binding = {};
|
||||||
|
index_buffer_binding.buffer = fd->IndexBuffer;
|
||||||
|
index_buffer_binding.offset = 0;
|
||||||
|
SDL_BindGPUVertexBuffers(render_pass,0, &vertex_buffer_binding, 1);
|
||||||
|
SDL_BindGPUIndexBuffer(render_pass, &index_buffer_binding, sizeof(ImDrawIdx) == 2 ? SDL_GPU_INDEXELEMENTSIZE_16BIT : SDL_GPU_INDEXELEMENTSIZE_32BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup viewport
|
||||||
|
SDL_GPUViewport viewport = {};
|
||||||
|
viewport.x = 0;
|
||||||
|
viewport.y = 0;
|
||||||
|
viewport.w = (float)fb_width;
|
||||||
|
viewport.h = (float)fb_height;
|
||||||
|
viewport.min_depth = 0.0f;
|
||||||
|
viewport.max_depth = 1.0f;
|
||||||
|
SDL_SetGPUViewport(render_pass, &viewport);
|
||||||
|
|
||||||
|
// Setup scale and translation
|
||||||
|
// Our visible imgui space lies from draw_data->DisplayPps (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
|
||||||
|
struct UBO { float scale[2]; float translation[2]; } ubo;
|
||||||
|
ubo.scale[0] = 2.0f / draw_data->DisplaySize.x;
|
||||||
|
ubo.scale[1] = 2.0f / draw_data->DisplaySize.y;
|
||||||
|
ubo.translation[0] = -1.0f - draw_data->DisplayPos.x * ubo.scale[0];
|
||||||
|
ubo.translation[1] = -1.0f - draw_data->DisplayPos.y * ubo.scale[1];
|
||||||
|
SDL_PushGPUVertexUniformData(command_buffer, 0, &ubo, sizeof(UBO));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void CreateOrResizeBuffers(SDL_GPUBuffer** buffer, SDL_GPUTransferBuffer** transferbuffer, uint32_t* old_size, uint32_t new_size, SDL_GPUBufferUsageFlags usage)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
|
||||||
|
|
||||||
|
// FIXME-OPT: Not optimal, but this is fairly rarely called.
|
||||||
|
SDL_WaitForGPUIdle(v->Device);
|
||||||
|
SDL_ReleaseGPUBuffer(v->Device, *buffer);
|
||||||
|
SDL_ReleaseGPUTransferBuffer(v->Device, *transferbuffer);
|
||||||
|
|
||||||
|
SDL_GPUBufferCreateInfo buffer_info = {};
|
||||||
|
buffer_info.usage = usage;
|
||||||
|
buffer_info.size = new_size;
|
||||||
|
buffer_info.props = 0;
|
||||||
|
*buffer = SDL_CreateGPUBuffer(v->Device, &buffer_info);
|
||||||
|
*old_size = new_size;
|
||||||
|
IM_ASSERT(*buffer != nullptr && "Failed to create GPU Buffer, call SDL_GetError() for more information");
|
||||||
|
|
||||||
|
SDL_GPUTransferBufferCreateInfo transferbuffer_info = {};
|
||||||
|
transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
|
||||||
|
transferbuffer_info.size = new_size;
|
||||||
|
*transferbuffer = SDL_CreateGPUTransferBuffer(v->Device, &transferbuffer_info);
|
||||||
|
IM_ASSERT(*transferbuffer != nullptr && "Failed to create GPU Transfer Buffer, call SDL_GetError() for more information");
|
||||||
|
}
|
||||||
|
|
||||||
|
// SDL_GPU doesn't allow copy passes to occur while a render or compute pass is bound!
|
||||||
|
// The only way to allow a user to supply their own RenderPass (to render to a texture instead of the window for example),
|
||||||
|
// is to split the upload part of ImGui_ImplSDLGPU3_RenderDrawData() to another function that needs to be called by the user before rendering.
|
||||||
|
void ImGui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer)
|
||||||
|
{
|
||||||
|
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
|
||||||
|
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
|
||||||
|
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
|
||||||
|
if (fb_width <= 0 || fb_height <= 0 || draw_data->TotalVtxCount <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplSDLGPU3_UpdateTexture(tex);
|
||||||
|
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
|
||||||
|
ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData;
|
||||||
|
|
||||||
|
uint32_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
|
||||||
|
uint32_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
|
||||||
|
if (fd->VertexBuffer == nullptr || fd->VertexBufferSize < vertex_size)
|
||||||
|
CreateOrResizeBuffers(&fd->VertexBuffer, &fd->VertexTransferBuffer, &fd->VertexBufferSize, vertex_size, SDL_GPU_BUFFERUSAGE_VERTEX);
|
||||||
|
if (fd->IndexBuffer == nullptr || fd->IndexBufferSize < index_size)
|
||||||
|
CreateOrResizeBuffers(&fd->IndexBuffer, &fd->IndexTransferBuffer, &fd->IndexBufferSize, index_size, SDL_GPU_BUFFERUSAGE_INDEX);
|
||||||
|
|
||||||
|
ImDrawVert* vtx_dst = (ImDrawVert*)SDL_MapGPUTransferBuffer(v->Device, fd->VertexTransferBuffer, true);
|
||||||
|
ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer(v->Device, fd->IndexTransferBuffer, true);
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
|
||||||
|
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
|
||||||
|
vtx_dst += draw_list->VtxBuffer.Size;
|
||||||
|
idx_dst += draw_list->IdxBuffer.Size;
|
||||||
|
}
|
||||||
|
SDL_UnmapGPUTransferBuffer(v->Device, fd->VertexTransferBuffer);
|
||||||
|
SDL_UnmapGPUTransferBuffer(v->Device, fd->IndexTransferBuffer);
|
||||||
|
|
||||||
|
SDL_GPUTransferBufferLocation vertex_buffer_location = {};
|
||||||
|
vertex_buffer_location.offset = 0;
|
||||||
|
vertex_buffer_location.transfer_buffer = fd->VertexTransferBuffer;
|
||||||
|
SDL_GPUTransferBufferLocation index_buffer_location = {};
|
||||||
|
index_buffer_location.offset = 0;
|
||||||
|
index_buffer_location.transfer_buffer = fd->IndexTransferBuffer;
|
||||||
|
|
||||||
|
SDL_GPUBufferRegion vertex_buffer_region = {};
|
||||||
|
vertex_buffer_region.buffer = fd->VertexBuffer;
|
||||||
|
vertex_buffer_region.offset = 0;
|
||||||
|
vertex_buffer_region.size = vertex_size;
|
||||||
|
|
||||||
|
SDL_GPUBufferRegion index_buffer_region = {};
|
||||||
|
index_buffer_region.buffer = fd->IndexBuffer;
|
||||||
|
index_buffer_region.offset = 0;
|
||||||
|
index_buffer_region.size = index_size;
|
||||||
|
|
||||||
|
SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(command_buffer);
|
||||||
|
SDL_UploadToGPUBuffer(copy_pass, &vertex_buffer_location, &vertex_buffer_region, true);
|
||||||
|
SDL_UploadToGPUBuffer(copy_pass, &index_buffer_location, &index_buffer_region, true);
|
||||||
|
SDL_EndGPUCopyPass(copy_pass);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline)
|
||||||
|
{
|
||||||
|
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
|
||||||
|
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
|
||||||
|
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
|
||||||
|
if (fb_width <= 0 || fb_height <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData;
|
||||||
|
|
||||||
|
if (pipeline == nullptr)
|
||||||
|
pipeline = bd->Pipeline;
|
||||||
|
|
||||||
|
ImGui_ImplSDLGPU3_SetupRenderState(draw_data, pipeline, command_buffer, render_pass, fd, fb_width, fb_height);
|
||||||
|
|
||||||
|
// Will project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
|
||||||
|
ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
// (Because we merged all buffers into a single one, we maintain our own offset into them)
|
||||||
|
int global_vtx_offset = 0;
|
||||||
|
int global_idx_offset = 0;
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback != nullptr)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplSDLGPU3_SetupRenderState(draw_data, pipeline, command_buffer, render_pass, fd, fb_width, fb_height);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
|
||||||
|
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
|
||||||
|
|
||||||
|
// Clamp to viewport as SDL_SetGPUScissor() won't accept values that are off bounds
|
||||||
|
if (clip_min.x < 0.0f) { clip_min.x = 0.0f; }
|
||||||
|
if (clip_min.y < 0.0f) { clip_min.y = 0.0f; }
|
||||||
|
if (clip_max.x > fb_width) { clip_max.x = (float)fb_width; }
|
||||||
|
if (clip_max.y > fb_height) { clip_max.y = (float)fb_height; }
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Apply scissor/clipping rectangle
|
||||||
|
SDL_Rect scissor_rect = {};
|
||||||
|
scissor_rect.x = (int)clip_min.x;
|
||||||
|
scissor_rect.y = (int)clip_min.y;
|
||||||
|
scissor_rect.w = (int)(clip_max.x - clip_min.x);
|
||||||
|
scissor_rect.h = (int)(clip_max.y - clip_min.y);
|
||||||
|
SDL_SetGPUScissor(render_pass,&scissor_rect);
|
||||||
|
|
||||||
|
// Bind DescriptorSet with font or user texture
|
||||||
|
SDL_BindGPUFragmentSamplers(render_pass, 0, (SDL_GPUTextureSamplerBinding*)pcmd->GetTexID(), 1);
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
SDL_DrawGPUIndexedPrimitives(render_pass, pcmd->ElemCount, 1, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
global_idx_offset += draw_list->IdxBuffer.Size;
|
||||||
|
global_vtx_offset += draw_list->VtxBuffer.Size;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: at this point both SDL_SetGPUViewport() and SDL_SetGPUScissor() have been called.
|
||||||
|
// Our last values will leak into user/application rendering if you forgot to call SDL_SetGPUViewport() and SDL_SetGPUScissor() yourself to explicitly set that state
|
||||||
|
// In theory we should aim to backup/restore those values but I am not sure this is possible.
|
||||||
|
// We perform a call to SDL_SetGPUScissor() to set back a full viewport which is likely to fix things for 99% users but technically this is not perfect. (See github #4644)
|
||||||
|
SDL_Rect scissor_rect { 0, 0, fb_width, fb_height };
|
||||||
|
SDL_SetGPUScissor(render_pass, &scissor_rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDLGPU3_DestroyTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
ImGui_ImplSDLGPU3_Texture* backend_tex = (ImGui_ImplSDLGPU3_Texture*)tex->BackendUserData;
|
||||||
|
if (backend_tex == nullptr)
|
||||||
|
return;
|
||||||
|
SDL_GPUTextureSamplerBinding* binding = (SDL_GPUTextureSamplerBinding*)(intptr_t)tex->BackendUserData;
|
||||||
|
IM_ASSERT(backend_tex->Texture == binding->texture);
|
||||||
|
SDL_ReleaseGPUTexture(bd->InitInfo.Device, backend_tex->Texture);
|
||||||
|
IM_DELETE(backend_tex);
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
tex->BackendUserData = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLGPU3_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
|
||||||
|
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
ImGui_ImplSDLGPU3_Texture* backend_tex = IM_NEW(ImGui_ImplSDLGPU3_Texture)();
|
||||||
|
|
||||||
|
// Create texture
|
||||||
|
SDL_GPUTextureCreateInfo texture_info = {};
|
||||||
|
texture_info.type = SDL_GPU_TEXTURETYPE_2D;
|
||||||
|
texture_info.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
|
||||||
|
texture_info.usage = SDL_GPU_TEXTUREUSAGE_SAMPLER;
|
||||||
|
texture_info.width = tex->Width;
|
||||||
|
texture_info.height = tex->Height;
|
||||||
|
texture_info.layer_count_or_depth = 1;
|
||||||
|
texture_info.num_levels = 1;
|
||||||
|
texture_info.sample_count = SDL_GPU_SAMPLECOUNT_1;
|
||||||
|
|
||||||
|
backend_tex->Texture = SDL_CreateGPUTexture(v->Device, &texture_info);
|
||||||
|
backend_tex->TextureSamplerBinding.texture = backend_tex->Texture;
|
||||||
|
backend_tex->TextureSamplerBinding.sampler = bd->TexSampler;
|
||||||
|
IM_ASSERT(backend_tex->Texture && "Failed to create font texture, call SDL_GetError() for more info");
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)(intptr_t)&backend_tex->TextureSamplerBinding);
|
||||||
|
tex->BackendUserData = backend_tex;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_Texture* backend_tex = (ImGui_ImplSDLGPU3_Texture*)tex->BackendUserData;
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
|
||||||
|
// Update full texture or selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->UpdateRect but you can use tex->Updates[] to upload individual regions.
|
||||||
|
// We could use the smaller rect on _WantCreate but using the full rect allows us to clear the texture.
|
||||||
|
const int upload_x = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.x;
|
||||||
|
const int upload_y = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.y;
|
||||||
|
const int upload_w = (tex->Status == ImTextureStatus_WantCreate) ? tex->Width : tex->UpdateRect.w;
|
||||||
|
const int upload_h = (tex->Status == ImTextureStatus_WantCreate) ? tex->Height : tex->UpdateRect.h;
|
||||||
|
uint32_t upload_pitch = upload_w * tex->BytesPerPixel;
|
||||||
|
uint32_t upload_size = upload_w * upload_h * tex->BytesPerPixel;
|
||||||
|
|
||||||
|
// Create transfer buffer
|
||||||
|
if (bd->TexTransferBufferSize < upload_size)
|
||||||
|
{
|
||||||
|
SDL_ReleaseGPUTransferBuffer(v->Device, bd->TexTransferBuffer);
|
||||||
|
SDL_GPUTransferBufferCreateInfo transferbuffer_info = {};
|
||||||
|
transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
|
||||||
|
transferbuffer_info.size = upload_size + 1024;
|
||||||
|
bd->TexTransferBufferSize = upload_size + 1024;
|
||||||
|
bd->TexTransferBuffer = SDL_CreateGPUTransferBuffer(v->Device, &transferbuffer_info);
|
||||||
|
IM_ASSERT(bd->TexTransferBuffer != nullptr && "Failed to create font transfer buffer, call SDL_GetError() for more information");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy to transfer buffer
|
||||||
|
{
|
||||||
|
void* texture_ptr = SDL_MapGPUTransferBuffer(v->Device, bd->TexTransferBuffer, true);
|
||||||
|
for (int y = 0; y < upload_h; y++)
|
||||||
|
memcpy((void*)((uintptr_t)texture_ptr + y * upload_pitch), tex->GetPixelsAt(upload_x, upload_y + y), upload_pitch);
|
||||||
|
SDL_UnmapGPUTransferBuffer(v->Device, bd->TexTransferBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GPUTextureTransferInfo transfer_info = {};
|
||||||
|
transfer_info.offset = 0;
|
||||||
|
transfer_info.transfer_buffer = bd->TexTransferBuffer;
|
||||||
|
|
||||||
|
SDL_GPUTextureRegion texture_region = {};
|
||||||
|
texture_region.texture = backend_tex->Texture;
|
||||||
|
texture_region.x = (Uint32)upload_x;
|
||||||
|
texture_region.y = (Uint32)upload_y;
|
||||||
|
texture_region.w = (Uint32)upload_w;
|
||||||
|
texture_region.h = (Uint32)upload_h;
|
||||||
|
texture_region.d = 1;
|
||||||
|
|
||||||
|
// Upload
|
||||||
|
{
|
||||||
|
SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(v->Device);
|
||||||
|
SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(cmd);
|
||||||
|
SDL_UploadToGPUTexture(copy_pass, &transfer_info, &texture_region, false);
|
||||||
|
SDL_EndGPUCopyPass(copy_pass);
|
||||||
|
SDL_SubmitGPUCommandBuffer(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
|
||||||
|
ImGui_ImplSDLGPU3_DestroyTexture(tex);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDLGPU3_CreateShaders()
|
||||||
|
{
|
||||||
|
// Create the shader modules
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
|
||||||
|
|
||||||
|
const char* driver = SDL_GetGPUDeviceDriver(v->Device);
|
||||||
|
|
||||||
|
SDL_GPUShaderCreateInfo vertex_shader_info = {};
|
||||||
|
vertex_shader_info.entrypoint = "main";
|
||||||
|
vertex_shader_info.stage = SDL_GPU_SHADERSTAGE_VERTEX;
|
||||||
|
vertex_shader_info.num_uniform_buffers = 1;
|
||||||
|
vertex_shader_info.num_storage_buffers = 0;
|
||||||
|
vertex_shader_info.num_storage_textures = 0;
|
||||||
|
vertex_shader_info.num_samplers = 0;
|
||||||
|
|
||||||
|
SDL_GPUShaderCreateInfo fragment_shader_info = {};
|
||||||
|
fragment_shader_info.entrypoint = "main";
|
||||||
|
fragment_shader_info.stage = SDL_GPU_SHADERSTAGE_FRAGMENT;
|
||||||
|
fragment_shader_info.num_samplers = 1;
|
||||||
|
fragment_shader_info.num_storage_buffers = 0;
|
||||||
|
fragment_shader_info.num_storage_textures = 0;
|
||||||
|
fragment_shader_info.num_uniform_buffers = 0;
|
||||||
|
|
||||||
|
if (strcmp(driver, "vulkan") == 0)
|
||||||
|
{
|
||||||
|
vertex_shader_info.format = SDL_GPU_SHADERFORMAT_SPIRV;
|
||||||
|
vertex_shader_info.code = spirv_vertex;
|
||||||
|
vertex_shader_info.code_size = sizeof(spirv_vertex);
|
||||||
|
fragment_shader_info.format = SDL_GPU_SHADERFORMAT_SPIRV;
|
||||||
|
fragment_shader_info.code = spirv_fragment;
|
||||||
|
fragment_shader_info.code_size = sizeof(spirv_fragment);
|
||||||
|
}
|
||||||
|
else if (strcmp(driver, "direct3d12") == 0)
|
||||||
|
{
|
||||||
|
vertex_shader_info.format = SDL_GPU_SHADERFORMAT_DXBC;
|
||||||
|
vertex_shader_info.code = dxbc_vertex;
|
||||||
|
vertex_shader_info.code_size = sizeof(dxbc_vertex);
|
||||||
|
fragment_shader_info.format = SDL_GPU_SHADERFORMAT_DXBC;
|
||||||
|
fragment_shader_info.code = dxbc_fragment;
|
||||||
|
fragment_shader_info.code_size = sizeof(dxbc_fragment);
|
||||||
|
}
|
||||||
|
#ifdef __APPLE__
|
||||||
|
else
|
||||||
|
{
|
||||||
|
vertex_shader_info.entrypoint = "main0";
|
||||||
|
vertex_shader_info.format = SDL_GPU_SHADERFORMAT_METALLIB;
|
||||||
|
vertex_shader_info.code = metallib_vertex;
|
||||||
|
vertex_shader_info.code_size = sizeof(metallib_vertex);
|
||||||
|
fragment_shader_info.entrypoint = "main0";
|
||||||
|
fragment_shader_info.format = SDL_GPU_SHADERFORMAT_METALLIB;
|
||||||
|
fragment_shader_info.code = metallib_fragment;
|
||||||
|
fragment_shader_info.code_size = sizeof(metallib_fragment);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
bd->VertexShader = SDL_CreateGPUShader(v->Device, &vertex_shader_info);
|
||||||
|
bd->FragmentShader = SDL_CreateGPUShader(v->Device, &fragment_shader_info);
|
||||||
|
IM_ASSERT(bd->VertexShader != nullptr && "Failed to create vertex shader, call SDL_GetError() for more information");
|
||||||
|
IM_ASSERT(bd->FragmentShader != nullptr && "Failed to create fragment shader, call SDL_GetError() for more information");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDLGPU3_CreateGraphicsPipeline()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
|
||||||
|
ImGui_ImplSDLGPU3_CreateShaders();
|
||||||
|
|
||||||
|
SDL_GPUVertexBufferDescription vertex_buffer_desc[1];
|
||||||
|
vertex_buffer_desc[0].slot = 0;
|
||||||
|
vertex_buffer_desc[0].input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX;
|
||||||
|
vertex_buffer_desc[0].instance_step_rate = 0;
|
||||||
|
vertex_buffer_desc[0].pitch = sizeof(ImDrawVert);
|
||||||
|
|
||||||
|
SDL_GPUVertexAttribute vertex_attributes[3];
|
||||||
|
vertex_attributes[0].buffer_slot = 0;
|
||||||
|
vertex_attributes[0].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
|
||||||
|
vertex_attributes[0].location = 0;
|
||||||
|
vertex_attributes[0].offset = offsetof(ImDrawVert,pos);
|
||||||
|
|
||||||
|
vertex_attributes[1].buffer_slot = 0;
|
||||||
|
vertex_attributes[1].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
|
||||||
|
vertex_attributes[1].location = 1;
|
||||||
|
vertex_attributes[1].offset = offsetof(ImDrawVert, uv);
|
||||||
|
|
||||||
|
vertex_attributes[2].buffer_slot = 0;
|
||||||
|
vertex_attributes[2].format = SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4_NORM;
|
||||||
|
vertex_attributes[2].location = 2;
|
||||||
|
vertex_attributes[2].offset = offsetof(ImDrawVert, col);
|
||||||
|
|
||||||
|
SDL_GPUVertexInputState vertex_input_state = {};
|
||||||
|
vertex_input_state.num_vertex_attributes = 3;
|
||||||
|
vertex_input_state.vertex_attributes = vertex_attributes;
|
||||||
|
vertex_input_state.num_vertex_buffers = 1;
|
||||||
|
vertex_input_state.vertex_buffer_descriptions = vertex_buffer_desc;
|
||||||
|
|
||||||
|
SDL_GPURasterizerState rasterizer_state = {};
|
||||||
|
rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
|
||||||
|
rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;
|
||||||
|
rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
|
||||||
|
rasterizer_state.enable_depth_bias = false;
|
||||||
|
rasterizer_state.enable_depth_clip = false;
|
||||||
|
|
||||||
|
SDL_GPUMultisampleState multisample_state = {};
|
||||||
|
multisample_state.sample_count = v->MSAASamples;
|
||||||
|
multisample_state.enable_mask = false;
|
||||||
|
|
||||||
|
SDL_GPUDepthStencilState depth_stencil_state = {};
|
||||||
|
depth_stencil_state.enable_depth_test = false;
|
||||||
|
depth_stencil_state.enable_depth_write = false;
|
||||||
|
depth_stencil_state.enable_stencil_test = false;
|
||||||
|
|
||||||
|
SDL_GPUColorTargetBlendState blend_state = {};
|
||||||
|
blend_state.enable_blend = true;
|
||||||
|
blend_state.src_color_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA;
|
||||||
|
blend_state.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA;
|
||||||
|
blend_state.color_blend_op = SDL_GPU_BLENDOP_ADD;
|
||||||
|
blend_state.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE;
|
||||||
|
blend_state.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA;
|
||||||
|
blend_state.alpha_blend_op = SDL_GPU_BLENDOP_ADD;
|
||||||
|
blend_state.color_write_mask = SDL_GPU_COLORCOMPONENT_R | SDL_GPU_COLORCOMPONENT_G | SDL_GPU_COLORCOMPONENT_B | SDL_GPU_COLORCOMPONENT_A;
|
||||||
|
|
||||||
|
SDL_GPUColorTargetDescription color_target_desc[1];
|
||||||
|
color_target_desc[0].format = v->ColorTargetFormat;
|
||||||
|
color_target_desc[0].blend_state = blend_state;
|
||||||
|
|
||||||
|
SDL_GPUGraphicsPipelineTargetInfo target_info = {};
|
||||||
|
target_info.num_color_targets = 1;
|
||||||
|
target_info.color_target_descriptions = color_target_desc;
|
||||||
|
target_info.has_depth_stencil_target = false;
|
||||||
|
|
||||||
|
SDL_GPUGraphicsPipelineCreateInfo pipeline_info = {};
|
||||||
|
pipeline_info.vertex_shader = bd->VertexShader;
|
||||||
|
pipeline_info.fragment_shader = bd->FragmentShader;
|
||||||
|
pipeline_info.vertex_input_state = vertex_input_state;
|
||||||
|
pipeline_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
|
||||||
|
pipeline_info.rasterizer_state = rasterizer_state;
|
||||||
|
pipeline_info.multisample_state = multisample_state;
|
||||||
|
pipeline_info.depth_stencil_state = depth_stencil_state;
|
||||||
|
pipeline_info.target_info = target_info;
|
||||||
|
|
||||||
|
bd->Pipeline = SDL_CreateGPUGraphicsPipeline(v->Device, &pipeline_info);
|
||||||
|
IM_ASSERT(bd->Pipeline != nullptr && "Failed to create graphics pipeline, call SDL_GetError() for more information");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLGPU3_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
|
||||||
|
|
||||||
|
ImGui_ImplSDLGPU3_DestroyDeviceObjects();
|
||||||
|
|
||||||
|
if (bd->TexSampler == nullptr)
|
||||||
|
{
|
||||||
|
// Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling.
|
||||||
|
SDL_GPUSamplerCreateInfo sampler_info = {};
|
||||||
|
sampler_info.min_filter = SDL_GPU_FILTER_LINEAR;
|
||||||
|
sampler_info.mag_filter = SDL_GPU_FILTER_LINEAR;
|
||||||
|
sampler_info.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_LINEAR;
|
||||||
|
sampler_info.address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
|
||||||
|
sampler_info.address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
|
||||||
|
sampler_info.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
|
||||||
|
sampler_info.mip_lod_bias = 0.0f;
|
||||||
|
sampler_info.min_lod = -1000.0f;
|
||||||
|
sampler_info.max_lod = 1000.0f;
|
||||||
|
sampler_info.enable_anisotropy = false;
|
||||||
|
sampler_info.max_anisotropy = 1.0f;
|
||||||
|
sampler_info.enable_compare = false;
|
||||||
|
|
||||||
|
bd->TexSampler = SDL_CreateGPUSampler(v->Device, &sampler_info);
|
||||||
|
IM_ASSERT(bd->TexSampler != nullptr && "Failed to create font sampler, call SDL_GetError() for more information");
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui_ImplSDLGPU3_CreateGraphicsPipeline();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLGPU3_DestroyFrameData()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
|
||||||
|
|
||||||
|
ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData;
|
||||||
|
SDL_ReleaseGPUBuffer(v->Device, fd->VertexBuffer);
|
||||||
|
SDL_ReleaseGPUBuffer(v->Device, fd->IndexBuffer);
|
||||||
|
SDL_ReleaseGPUTransferBuffer(v->Device, fd->VertexTransferBuffer);
|
||||||
|
SDL_ReleaseGPUTransferBuffer(v->Device, fd->IndexTransferBuffer);
|
||||||
|
fd->VertexBuffer = fd->IndexBuffer = nullptr;
|
||||||
|
fd->VertexTransferBuffer = fd->IndexTransferBuffer = nullptr;
|
||||||
|
fd->VertexBufferSize = fd->IndexBufferSize = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLGPU3_DestroyDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
|
||||||
|
|
||||||
|
ImGui_ImplSDLGPU3_DestroyFrameData();
|
||||||
|
|
||||||
|
// Destroy all textures
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
ImGui_ImplSDLGPU3_DestroyTexture(tex);
|
||||||
|
if (bd->TexTransferBuffer) { SDL_ReleaseGPUTransferBuffer(v->Device, bd->TexTransferBuffer); bd->TexTransferBuffer = nullptr; }
|
||||||
|
if (bd->VertexShader) { SDL_ReleaseGPUShader(v->Device, bd->VertexShader); bd->VertexShader = nullptr; }
|
||||||
|
if (bd->FragmentShader) { SDL_ReleaseGPUShader(v->Device, bd->FragmentShader); bd->FragmentShader = nullptr; }
|
||||||
|
if (bd->TexSampler) { SDL_ReleaseGPUSampler(v->Device, bd->TexSampler); bd->TexSampler = nullptr; }
|
||||||
|
if (bd->Pipeline) { SDL_ReleaseGPUGraphicsPipeline(v->Device, bd->Pipeline); bd->Pipeline = nullptr; }
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSDLGPU3_Init(ImGui_ImplSDLGPU3_InitInfo* info)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = IM_NEW(ImGui_ImplSDLGPU3_Data)();
|
||||||
|
io.BackendRendererUserData = (void*)bd;
|
||||||
|
io.BackendRendererName = "imgui_impl_sdlgpu3";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
IM_ASSERT(info->Device != nullptr);
|
||||||
|
IM_ASSERT(info->ColorTargetFormat != SDL_GPU_TEXTUREFORMAT_INVALID);
|
||||||
|
|
||||||
|
bd->InitInfo = *info;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLGPU3_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
ImGui_ImplSDLGPU3_DestroyDeviceObjects();
|
||||||
|
io.BackendRendererName = nullptr;
|
||||||
|
io.BackendRendererUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLGPU3_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLGPU3_Init()?");
|
||||||
|
|
||||||
|
if (!bd->TexSampler)
|
||||||
|
ImGui_ImplSDLGPU3_CreateDeviceObjects();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
52
imgui/backends/imgui_impl_sdlgpu3.h
Normal file
52
imgui/backends/imgui_impl_sdlgpu3.h
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
// dear imgui: Renderer Backend for SDL_GPU
|
||||||
|
// This needs to be used along with the SDL3 Platform Backend
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use simply cast a reference to your SDL_GPUTextureSamplerBinding to ImTextureID.
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
|
||||||
|
// The aim of imgui_impl_sdlgpu3.h/.cpp is to be usable in your engine without any modification.
|
||||||
|
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// Important note to the reader who wish to integrate imgui_impl_sdlgpu3.cpp/.h in their own engine/app.
|
||||||
|
// - Unlike other backends, the user must call the function ImGui_ImplSDLGPU_PrepareDrawData BEFORE issuing a SDL_GPURenderPass containing ImGui_ImplSDLGPU_RenderDrawData.
|
||||||
|
// Calling the function is MANDATORY, otherwise the ImGui will not upload neither the vertex nor the index buffer for the GPU. See imgui_impl_sdlgpu3.cpp for more info.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include <SDL3/SDL_gpu.h>
|
||||||
|
|
||||||
|
// Initialization data, for ImGui_ImplSDLGPU_Init()
|
||||||
|
// - Remember to set ColorTargetFormat to the correct format. If you're rendering to the swapchain, call SDL_GetGPUSwapchainTextureFormat to query the right value
|
||||||
|
struct ImGui_ImplSDLGPU3_InitInfo
|
||||||
|
{
|
||||||
|
SDL_GPUDevice* Device = nullptr;
|
||||||
|
SDL_GPUTextureFormat ColorTargetFormat = SDL_GPU_TEXTUREFORMAT_INVALID;
|
||||||
|
SDL_GPUSampleCount MSAASamples = SDL_GPU_SAMPLECOUNT_1;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDLGPU3_Init(ImGui_ImplSDLGPU3_InitInfo* info);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline = nullptr);
|
||||||
|
|
||||||
|
// Use if you want to reset your rendering device without losing Dear ImGui state.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_DestroyDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
372
imgui/backends/imgui_impl_sdlgpu3_shaders.h
Normal file
372
imgui/backends/imgui_impl_sdlgpu3_shaders.h
Normal file
@@ -0,0 +1,372 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// Data exported using
|
||||||
|
// misc/fonts/binary_to_compressed_c.exe -u8 -nocompress filename symbolname >filename.h
|
||||||
|
// With some manual pasting.
|
||||||
|
|
||||||
|
// Check sdlgpu3/ folder for the shaders' source code and instruction on how to build them
|
||||||
|
const uint8_t spirv_vertex[1732] = {
|
||||||
|
3,2,35,7,0,0,1,0,11,0,13,0,55,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,11,0,6,0,1,0,0,0,71,76,83,76,46,115,116,100,46,52,53,48,0,0,0,0,14,0,3,0,0,0,0,0,1,0,0,0,15,0,10,0,0,0,0,0,4,0,0,0,109,
|
||||||
|
97,105,110,0,0,0,0,11,0,0,0,15,0,0,0,21,0,0,0,30,0,0,0,31,0,0,0,3,0,3,0,2,0,0,0,194,1,0,0,4,0,10,0,71,76,95,71,79,79,71,76,69,95,99,112,112,95,115,116,121,108,101,95,108,105,110,101,
|
||||||
|
95,100,105,114,101,99,116,105,118,101,0,0,4,0,8,0,71,76,95,71,79,79,71,76,69,95,105,110,99,108,117,100,101,95,100,105,114,101,99,116,105,118,101,0,5,0,4,0,4,0,0,0,109,97,105,110,0,
|
||||||
|
0,0,0,5,0,3,0,9,0,0,0,0,0,0,0,6,0,5,0,9,0,0,0,0,0,0,0,67,111,108,111,114,0,0,0,6,0,4,0,9,0,0,0,1,0,0,0,85,86,0,0,5,0,3,0,11,0,0,0,79,117,116,0,5,0,4,0,15,0,0,0,97,67,111,108,111,114,
|
||||||
|
0,0,5,0,3,0,21,0,0,0,97,85,86,0,5,0,6,0,28,0,0,0,103,108,95,80,101,114,86,101,114,116,101,120,0,0,0,0,6,0,6,0,28,0,0,0,0,0,0,0,103,108,95,80,111,115,105,116,105,111,110,0,6,0,7,0,28,
|
||||||
|
0,0,0,1,0,0,0,103,108,95,80,111,105,110,116,83,105,122,101,0,0,0,0,6,0,7,0,28,0,0,0,2,0,0,0,103,108,95,67,108,105,112,68,105,115,116,97,110,99,101,0,6,0,7,0,28,0,0,0,3,0,0,0,103,108,
|
||||||
|
95,67,117,108,108,68,105,115,116,97,110,99,101,0,5,0,3,0,30,0,0,0,0,0,0,0,5,0,4,0,31,0,0,0,97,80,111,115,0,0,0,0,5,0,6,0,33,0,0,0,117,80,117,115,104,67,111,110,115,116,97,110,116,0,
|
||||||
|
0,0,6,0,5,0,33,0,0,0,0,0,0,0,117,83,99,97,108,101,0,0,6,0,6,0,33,0,0,0,1,0,0,0,117,84,114,97,110,115,108,97,116,101,0,0,5,0,3,0,35,0,0,0,112,99,0,0,71,0,4,0,11,0,0,0,30,0,0,0,0,0,0,
|
||||||
|
0,71,0,4,0,15,0,0,0,30,0,0,0,2,0,0,0,71,0,4,0,21,0,0,0,30,0,0,0,1,0,0,0,71,0,3,0,28,0,0,0,2,0,0,0,72,0,5,0,28,0,0,0,0,0,0,0,11,0,0,0,0,0,0,0,72,0,5,0,28,0,0,0,1,0,0,0,11,0,0,0,1,0,
|
||||||
|
0,0,72,0,5,0,28,0,0,0,2,0,0,0,11,0,0,0,3,0,0,0,72,0,5,0,28,0,0,0,3,0,0,0,11,0,0,0,4,0,0,0,71,0,4,0,31,0,0,0,30,0,0,0,0,0,0,0,71,0,3,0,33,0,0,0,2,0,0,0,72,0,5,0,33,0,0,0,0,0,0,0,35,
|
||||||
|
0,0,0,0,0,0,0,72,0,5,0,33,0,0,0,1,0,0,0,35,0,0,0,8,0,0,0,71,0,4,0,35,0,0,0,33,0,0,0,0,0,0,0,71,0,4,0,35,0,0,0,34,0,0,0,1,0,0,0,19,0,2,0,2,0,0,0,33,0,3,0,3,0,0,0,2,0,0,0,22,0,3,0,6,
|
||||||
|
0,0,0,32,0,0,0,23,0,4,0,7,0,0,0,6,0,0,0,4,0,0,0,23,0,4,0,8,0,0,0,6,0,0,0,2,0,0,0,30,0,4,0,9,0,0,0,7,0,0,0,8,0,0,0,32,0,4,0,10,0,0,0,3,0,0,0,9,0,0,0,59,0,4,0,10,0,0,0,11,0,0,0,3,0,0,
|
||||||
|
0,21,0,4,0,12,0,0,0,32,0,0,0,1,0,0,0,43,0,4,0,12,0,0,0,13,0,0,0,0,0,0,0,32,0,4,0,14,0,0,0,1,0,0,0,7,0,0,0,59,0,4,0,14,0,0,0,15,0,0,0,1,0,0,0,32,0,4,0,17,0,0,0,3,0,0,0,7,0,0,0,43,0,
|
||||||
|
4,0,12,0,0,0,19,0,0,0,1,0,0,0,32,0,4,0,20,0,0,0,1,0,0,0,8,0,0,0,59,0,4,0,20,0,0,0,21,0,0,0,1,0,0,0,32,0,4,0,23,0,0,0,3,0,0,0,8,0,0,0,21,0,4,0,25,0,0,0,32,0,0,0,0,0,0,0,43,0,4,0,25,
|
||||||
|
0,0,0,26,0,0,0,1,0,0,0,28,0,4,0,27,0,0,0,6,0,0,0,26,0,0,0,30,0,6,0,28,0,0,0,7,0,0,0,6,0,0,0,27,0,0,0,27,0,0,0,32,0,4,0,29,0,0,0,3,0,0,0,28,0,0,0,59,0,4,0,29,0,0,0,30,0,0,0,3,0,0,0,
|
||||||
|
59,0,4,0,20,0,0,0,31,0,0,0,1,0,0,0,30,0,4,0,33,0,0,0,8,0,0,0,8,0,0,0,32,0,4,0,34,0,0,0,2,0,0,0,33,0,0,0,59,0,4,0,34,0,0,0,35,0,0,0,2,0,0,0,32,0,4,0,36,0,0,0,2,0,0,0,8,0,0,0,43,0,4,
|
||||||
|
0,6,0,0,0,43,0,0,0,0,0,0,0,43,0,4,0,6,0,0,0,44,0,0,0,0,0,128,63,43,0,4,0,6,0,0,0,49,0,0,0,0,0,128,191,32,0,4,0,50,0,0,0,3,0,0,0,6,0,0,0,54,0,5,0,2,0,0,0,4,0,0,0,0,0,0,0,3,0,0,0,248,
|
||||||
|
0,2,0,5,0,0,0,61,0,4,0,7,0,0,0,16,0,0,0,15,0,0,0,65,0,5,0,17,0,0,0,18,0,0,0,11,0,0,0,13,0,0,0,62,0,3,0,18,0,0,0,16,0,0,0,61,0,4,0,8,0,0,0,22,0,0,0,21,0,0,0,65,0,5,0,23,0,0,0,24,0,0,
|
||||||
|
0,11,0,0,0,19,0,0,0,62,0,3,0,24,0,0,0,22,0,0,0,61,0,4,0,8,0,0,0,32,0,0,0,31,0,0,0,65,0,5,0,36,0,0,0,37,0,0,0,35,0,0,0,13,0,0,0,61,0,4,0,8,0,0,0,38,0,0,0,37,0,0,0,133,0,5,0,8,0,0,0,
|
||||||
|
39,0,0,0,32,0,0,0,38,0,0,0,65,0,5,0,36,0,0,0,40,0,0,0,35,0,0,0,19,0,0,0,61,0,4,0,8,0,0,0,41,0,0,0,40,0,0,0,129,0,5,0,8,0,0,0,42,0,0,0,39,0,0,0,41,0,0,0,81,0,5,0,6,0,0,0,45,0,0,0,42,
|
||||||
|
0,0,0,0,0,0,0,81,0,5,0,6,0,0,0,46,0,0,0,42,0,0,0,1,0,0,0,80,0,7,0,7,0,0,0,47,0,0,0,45,0,0,0,46,0,0,0,43,0,0,0,44,0,0,0,65,0,5,0,17,0,0,0,48,0,0,0,30,0,0,0,13,0,0,0,62,0,3,0,48,0,0,
|
||||||
|
0,47,0,0,0,65,0,6,0,50,0,0,0,51,0,0,0,30,0,0,0,13,0,0,0,26,0,0,0,61,0,4,0,6,0,0,0,52,0,0,0,51,0,0,0,133,0,5,0,6,0,0,0,53,0,0,0,52,0,0,0,49,0,0,0,65,0,6,0,50,0,0,0,54,0,0,0,30,0,0,0,
|
||||||
|
13,0,0,0,26,0,0,0,62,0,3,0,54,0,0,0,53,0,0,0,253,0,1,0,56,0,1,0,
|
||||||
|
};
|
||||||
|
const uint8_t spirv_fragment[844] = {
|
||||||
|
3,2,35,7,0,0,1,0,11,0,13,0,30,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,11,0,6,0,1,0,0,0,71,76,83,76,46,115,116,100,46,52,53,48,0,0,0,0,14,0,3,0,0,0,0,0,1,0,0,0,15,0,7,0,4,0,0,0,4,0,0,0,109,97,
|
||||||
|
105,110,0,0,0,0,9,0,0,0,13,0,0,0,16,0,3,0,4,0,0,0,7,0,0,0,3,0,3,0,2,0,0,0,194,1,0,0,4,0,10,0,71,76,95,71,79,79,71,76,69,95,99,112,112,95,115,116,121,108,101,95,108,105,110,101,95,100,
|
||||||
|
105,114,101,99,116,105,118,101,0,0,4,0,8,0,71,76,95,71,79,79,71,76,69,95,105,110,99,108,117,100,101,95,100,105,114,101,99,116,105,118,101,0,5,0,4,0,4,0,0,0,109,97,105,110,0,0,0,0,5,
|
||||||
|
0,4,0,9,0,0,0,102,67,111,108,111,114,0,0,5,0,3,0,11,0,0,0,0,0,0,0,6,0,5,0,11,0,0,0,0,0,0,0,67,111,108,111,114,0,0,0,6,0,4,0,11,0,0,0,1,0,0,0,85,86,0,0,5,0,3,0,13,0,0,0,73,110,0,0,5,
|
||||||
|
0,5,0,22,0,0,0,115,84,101,120,116,117,114,101,0,0,0,0,71,0,4,0,9,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,13,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,22,0,0,0,33,0,0,0,0,0,0,0,71,0,4,0,22,0,0,0,34,0,
|
||||||
|
0,0,2,0,0,0,19,0,2,0,2,0,0,0,33,0,3,0,3,0,0,0,2,0,0,0,22,0,3,0,6,0,0,0,32,0,0,0,23,0,4,0,7,0,0,0,6,0,0,0,4,0,0,0,32,0,4,0,8,0,0,0,3,0,0,0,7,0,0,0,59,0,4,0,8,0,0,0,9,0,0,0,3,0,0,0,23,
|
||||||
|
0,4,0,10,0,0,0,6,0,0,0,2,0,0,0,30,0,4,0,11,0,0,0,7,0,0,0,10,0,0,0,32,0,4,0,12,0,0,0,1,0,0,0,11,0,0,0,59,0,4,0,12,0,0,0,13,0,0,0,1,0,0,0,21,0,4,0,14,0,0,0,32,0,0,0,1,0,0,0,43,0,4,0,
|
||||||
|
14,0,0,0,15,0,0,0,0,0,0,0,32,0,4,0,16,0,0,0,1,0,0,0,7,0,0,0,25,0,9,0,19,0,0,0,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,27,0,3,0,20,0,0,0,19,0,0,0,32,0,4,0,21,0,0,0,0,
|
||||||
|
0,0,0,20,0,0,0,59,0,4,0,21,0,0,0,22,0,0,0,0,0,0,0,43,0,4,0,14,0,0,0,24,0,0,0,1,0,0,0,32,0,4,0,25,0,0,0,1,0,0,0,10,0,0,0,54,0,5,0,2,0,0,0,4,0,0,0,0,0,0,0,3,0,0,0,248,0,2,0,5,0,0,0,65,
|
||||||
|
0,5,0,16,0,0,0,17,0,0,0,13,0,0,0,15,0,0,0,61,0,4,0,7,0,0,0,18,0,0,0,17,0,0,0,61,0,4,0,20,0,0,0,23,0,0,0,22,0,0,0,65,0,5,0,25,0,0,0,26,0,0,0,13,0,0,0,24,0,0,0,61,0,4,0,10,0,0,0,27,0,
|
||||||
|
0,0,26,0,0,0,87,0,5,0,7,0,0,0,28,0,0,0,23,0,0,0,27,0,0,0,133,0,5,0,7,0,0,0,29,0,0,0,18,0,0,0,28,0,0,0,62,0,3,0,9,0,0,0,29,0,0,0,253,0,1,0,56,0,1,0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint8_t dxbc_vertex[1064] = {
|
||||||
|
68,88,66,67,32,50,127,204,241,196,165,104,216,114,216,116,220,164,29,45,1,0,0,0,40,4,0,0,5,0,0,0,52,0,0,0,136,1,0,0,236,1,0,0,92,2,0,0,140,3,0,0,82,68,69,70,76,1,0,0,1,0,0,0,116,0,
|
||||||
|
0,0,1,0,0,0,60,0,0,0,1,5,254,255,0,5,0,0,34,1,0,0,19,19,68,37,60,0,0,0,24,0,0,0,40,0,0,0,40,0,0,0,36,0,0,0,12,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
|
||||||
|
0,1,0,0,0,1,0,0,0,0,0,0,0,117,80,117,115,104,67,111,110,115,116,97,110,116,0,171,171,100,0,0,0,2,0,0,0,140,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,220,0,0,0,0,0,0,0,8,0,0,0,2,0,0,0,240,0,0,
|
||||||
|
0,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0,20,1,0,0,8,0,0,0,8,0,0,0,2,0,0,0,240,0,0,0,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0,112,99,95,117,83,99,97,
|
||||||
|
108,101,0,102,108,111,97,116,50,0,171,171,171,1,0,3,0,1,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,230,0,0,0,112,99,95,117,84,114,97,110,115,108,97,116,101,0,77,105,99,114,
|
||||||
|
111,115,111,102,116,32,40,82,41,32,72,76,83,76,32,83,104,97,100,101,114,32,67,111,109,112,105,108,101,114,32,49,48,46,49,0,171,171,73,83,71,78,92,0,0,0,3,0,0,0,8,0,0,0,80,0,0,0,0,0,
|
||||||
|
0,0,0,0,0,0,3,0,0,0,0,0,0,0,3,3,0,0,80,0,0,0,1,0,0,0,0,0,0,0,3,0,0,0,1,0,0,0,3,3,0,0,80,0,0,0,2,0,0,0,0,0,0,0,3,0,0,0,2,0,0,0,15,15,0,0,84,69,88,67,79,79,82,68,0,171,171,171,79,83,
|
||||||
|
71,78,104,0,0,0,3,0,0,0,8,0,0,0,80,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,15,0,0,0,80,0,0,0,1,0,0,0,0,0,0,0,3,0,0,0,1,0,0,0,3,12,0,0,89,0,0,0,0,0,0,0,1,0,0,0,3,0,0,0,2,0,0,0,15,0,0,
|
||||||
|
0,84,69,88,67,79,79,82,68,0,83,86,95,80,111,115,105,116,105,111,110,0,171,171,171,83,72,69,88,40,1,0,0,81,0,1,0,74,0,0,0,106,8,0,1,89,0,0,7,70,142,48,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,
|
||||||
|
0,0,1,0,0,0,95,0,0,3,50,16,16,0,0,0,0,0,95,0,0,3,50,16,16,0,1,0,0,0,95,0,0,3,242,16,16,0,2,0,0,0,101,0,0,3,242,32,16,0,0,0,0,0,101,0,0,3,50,32,16,0,1,0,0,0,103,0,0,4,242,32,16,0,2,
|
||||||
|
0,0,0,1,0,0,0,104,0,0,2,1,0,0,0,50,0,0,13,50,0,16,0,0,0,0,0,70,16,16,0,0,0,0,0,70,128,48,0,0,0,0,0,0,0,0,0,0,0,0,0,230,138,48,0,0,0,0,0,0,0,0,0,0,0,0,0,54,0,0,6,34,32,16,0,2,0,0,0,
|
||||||
|
26,0,16,128,65,0,0,0,0,0,0,0,54,0,0,5,242,32,16,0,0,0,0,0,70,30,16,0,2,0,0,0,54,0,0,5,18,32,16,0,2,0,0,0,10,0,16,0,0,0,0,0,54,0,0,8,194,32,16,0,2,0,0,0,2,64,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,128,63,54,0,0,5,50,32,16,0,1,0,0,0,70,16,16,0,1,0,0,0,62,0,0,1,83,84,65,84,148,0,0,0,7,0,0,0,1,0,0,0,0,0,0,0,6,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
};
|
||||||
|
const uint8_t dxbc_fragment[744] = {
|
||||||
|
68,88,66,67,235,219,43,109,38,151,39,223,98,41,193,28,215,98,67,110,1,0,0,0,232,2,0,0,5,0,0,0,52,0,0,0,12,1,0,0,88,1,0,0,140,1,0,0,76,2,0,0,82,68,69,70,208,0,0,0,0,0,0,0,0,0,0,0,2,
|
||||||
|
0,0,0,60,0,0,0,1,5,255,255,0,5,0,0,167,0,0,0,19,19,68,37,60,0,0,0,24,0,0,0,40,0,0,0,40,0,0,0,36,0,0,0,12,0,0,0,0,0,0,0,140,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,
|
||||||
|
0,0,2,0,0,0,0,0,0,0,158,0,0,0,2,0,0,0,5,0,0,0,4,0,0,0,255,255,255,255,0,0,0,0,1,0,0,0,12,0,0,0,2,0,0,0,0,0,0,0,95,115,84,101,120,116,117,114,101,95,115,97,109,112,108,101,114,0,115,
|
||||||
|
84,101,120,116,117,114,101,0,77,105,99,114,111,115,111,102,116,32,40,82,41,32,72,76,83,76,32,83,104,97,100,101,114,32,67,111,109,112,105,108,101,114,32,49,48,46,49,0,171,73,83,71,78,
|
||||||
|
68,0,0,0,2,0,0,0,8,0,0,0,56,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,15,15,0,0,56,0,0,0,1,0,0,0,0,0,0,0,3,0,0,0,1,0,0,0,3,3,0,0,84,69,88,67,79,79,82,68,0,171,171,171,79,83,71,78,44,0,
|
||||||
|
0,0,1,0,0,0,8,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,15,0,0,0,83,86,95,84,97,114,103,101,116,0,171,171,83,72,69,88,184,0,0,0,81,0,0,0,46,0,0,0,106,8,0,1,90,0,0,6,70,110,48,
|
||||||
|
0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,88,24,0,7,70,126,48,0,0,0,0,0,0,0,0,0,0,0,0,0,85,85,0,0,2,0,0,0,98,16,0,3,242,16,16,0,0,0,0,0,98,16,0,3,50,16,16,0,1,0,0,0,101,0,0,3,242,32,16,0,0,
|
||||||
|
0,0,0,104,0,0,2,1,0,0,0,69,0,0,11,242,0,16,0,0,0,0,0,70,16,16,0,1,0,0,0,70,126,32,0,0,0,0,0,0,0,0,0,0,96,32,0,0,0,0,0,0,0,0,0,56,0,0,7,242,32,16,0,0,0,0,0,70,14,16,0,0,0,0,0,70,30,
|
||||||
|
16,0,0,0,0,0,62,0,0,1,83,84,65,84,148,0,0,0,3,0,0,0,1,0,0,0,0,0,0,0,3,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#include <TargetConditionals.h>
|
||||||
|
#if TARGET_OS_MAC
|
||||||
|
const uint8_t metallib_vertex[3892] = {
|
||||||
|
77,84,76,66,1,128,2,0,7,0,0,129,14,0,0,0,52,15,0,0,0,0,0,0,88,0,0,0,0,0,0,0,123,0,0,0,0,0,0,0,219,0,0,0,0,0,0,0,49,0,0,0,0,0,0,0,12,1,0,0,0,0,0,0,8,0,0,0,0,0,0,0,20,1,0,0,0,0,0,0,32,
|
||||||
|
14,0,0,0,0,0,0,1,0,0,0,123,0,0,0,78,65,77,69,6,0,109,97,105,110,48,0,84,89,80,69,1,0,0,72,65,83,72,32,0,62,81,190,157,8,240,236,158,164,213,65,62,170,226,96,136,231,243,238,160,100,
|
||||||
|
26,13,254,254,64,19,129,180,3,149,75,77,68,83,90,8,0,32,14,0,0,0,0,0,0,79,70,70,84,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,86,69,82,83,8,0,2,0,6,0,3,0,1,0,69,78,68,84,
|
||||||
|
69,78,68,84,45,0,0,0,86,65,84,84,24,0,3,0,97,80,111,115,0,0,128,97,85,86,0,1,128,97,67,111,108,111,114,0,2,128,86,65,84,89,5,0,3,0,4,4,6,69,78,68,84,4,0,0,0,69,78,68,84,222,192,23,
|
||||||
|
11,0,0,0,0,20,0,0,0,8,14,0,0,255,255,255,255,66,67,192,222,53,20,0,0,3,0,0,0,98,12,48,36,128,16,5,200,20,0,0,0,33,12,0,0,74,3,0,0,11,2,33,0,2,0,0,0,22,0,0,0,7,129,35,145,65,200,4,73,
|
||||||
|
6,16,50,57,146,1,132,12,37,5,8,25,30,4,139,98,128,16,69,2,66,146,11,66,132,16,50,20,56,8,24,75,10,50,66,136,72,112,196,33,35,68,18,135,140,16,65,146,2,100,200,8,177,20,32,67,70,136,
|
||||||
|
32,201,1,50,66,132,24,42,40,42,144,49,124,176,92,145,32,196,200,0,0,0,137,32,0,0,24,0,0,0,50,34,8,9,32,98,70,0,33,43,36,152,16,33,37,36,152,16,25,39,12,133,164,144,96,66,100,92,32,
|
||||||
|
36,100,130,224,153,1,24,70,32,128,97,4,1,184,67,72,32,37,77,17,37,76,62,149,82,210,193,57,141,52,1,205,148,4,145,65,4,66,48,197,136,68,145,13,4,204,17,128,129,10,228,28,1,40,12,34,
|
||||||
|
8,194,48,2,145,140,0,0,0,0,0,81,24,0,0,100,0,0,0,27,246,35,248,255,255,255,255,1,48,5,192,15,0,56,0,254,0,144,0,10,232,3,34,28,224,1,30,228,225,29,240,161,13,204,161,30,220,97,28,218,
|
||||||
|
192,28,224,161,13,218,33,28,232,1,29,0,122,144,135,122,40,7,128,48,7,121,8,135,118,40,135,54,128,135,119,72,7,119,160,135,114,144,7,32,28,216,129,29,0,162,29,210,193,29,218,128,29,
|
||||||
|
202,225,28,194,129,29,218,192,30,202,97,28,232,225,29,228,161,13,238,33,29,200,129,30,208,1,136,3,57,192,3,96,112,135,119,104,3,113,168,135,116,96,7,122,72,7,119,152,7,128,112,135,
|
||||||
|
119,104,131,116,112,7,115,152,135,54,48,7,120,104,131,118,8,7,122,64,7,128,30,228,161,30,202,1,32,220,225,29,218,192,29,194,193,29,230,161,13,204,1,30,218,160,29,194,129,30,208,1,160,
|
||||||
|
7,121,168,135,114,0,8,119,120,135,54,152,135,116,56,7,119,40,7,114,104,3,125,40,7,121,120,135,121,104,3,115,128,135,54,104,135,112,160,7,116,0,232,65,30,234,161,28,0,194,29,222,161,
|
||||||
|
13,232,65,30,194,1,30,224,33,29,220,225,28,218,160,29,194,129,30,208,1,160,7,121,168,135,114,0,136,121,160,135,112,24,135,117,104,3,120,144,135,119,160,135,114,24,7,122,120,7,121,104,
|
||||||
|
3,113,168,7,115,48,135,114,144,135,54,152,135,116,208,135,114,0,240,0,32,234,193,29,230,33,28,204,161,28,218,192,28,224,161,13,218,33,28,232,1,29,0,122,144,135,122,40,7,96,195,24,8,
|
||||||
|
4,176,0,164,0,84,65,128,4,105,0,13,225,144,14,242,208,6,226,80,15,230,96,14,229,32,15,109,224,14,239,208,6,225,192,14,233,16,14,243,0,0,0,73,24,0,0,1,0,0,0,19,132,64,0,19,176,112,72,
|
||||||
|
7,121,176,3,58,104,131,112,128,7,120,96,135,114,104,131,118,8,135,113,120,135,121,192,135,56,160,3,55,128,3,55,128,131,13,183,81,14,109,0,15,122,96,7,116,160,7,118,64,7,122,96,7,116,
|
||||||
|
208,6,233,16,7,122,128,7,122,128,7,109,144,14,120,160,7,120,160,7,120,208,6,233,16,7,118,160,7,113,96,7,122,16,7,118,208,6,233,48,7,114,160,7,115,32,7,122,48,7,114,208,6,233,96,7,116,
|
||||||
|
160,7,118,64,7,122,96,7,116,208,6,230,48,7,114,160,7,115,32,7,122,48,7,114,208,6,230,96,7,116,160,7,118,64,7,122,96,7,116,208,6,246,16,7,118,160,7,113,96,7,122,16,7,118,208,6,246,32,
|
||||||
|
7,116,160,7,115,32,7,122,48,7,114,208,6,246,48,7,114,160,7,115,32,7,122,48,7,114,208,6,246,64,7,120,160,7,118,64,7,122,96,7,116,208,6,246,96,7,116,160,7,118,64,7,122,96,7,116,208,6,
|
||||||
|
246,144,7,118,160,7,113,32,7,120,160,7,113,32,7,120,208,6,246,16,7,114,128,7,122,16,7,114,128,7,122,16,7,114,128,7,109,96,15,113,144,7,114,160,7,114,80,7,118,160,7,114,80,7,118,208,
|
||||||
|
6,246,32,7,117,96,7,122,32,7,117,96,7,122,32,7,117,96,7,109,96,15,117,16,7,114,160,7,117,16,7,114,160,7,117,16,7,114,208,6,246,16,7,112,32,7,116,160,7,113,0,7,114,64,7,122,16,7,112,
|
||||||
|
32,7,116,208,6,238,128,7,122,16,7,118,160,7,115,32,7,26,33,12,89,48,0,210,208,67,42,160,48,0,0,8,0,0,0,4,0,0,0,0,0,10,64,98,131,64,81,166,1,0,128,44,16,0,11,0,0,0,50,30,152,16,25,17,
|
||||||
|
76,144,140,9,38,71,198,4,67,202,34,40,129,66,40,135,242,41,64,129,130,40,144,98,24,1,40,3,218,17,0,210,177,132,39,0,0,0,177,24,0,0,165,0,0,0,51,8,128,28,196,225,28,102,20,1,61,136,
|
||||||
|
67,56,132,195,140,66,128,7,121,120,7,115,152,113,12,230,0,15,237,16,14,244,128,14,51,12,66,30,194,193,29,206,161,28,102,48,5,61,136,67,56,132,131,27,204,3,61,200,67,61,140,3,61,204,
|
||||||
|
120,140,116,112,7,123,8,7,121,72,135,112,112,7,122,112,3,118,120,135,112,32,135,25,204,17,14,236,144,14,225,48,15,110,48,15,227,240,14,240,80,14,51,16,196,29,222,33,28,216,33,29,194,
|
||||||
|
97,30,102,48,137,59,188,131,59,208,67,57,180,3,60,188,131,60,132,3,59,204,240,20,118,96,7,123,104,7,55,104,135,114,104,7,55,128,135,112,144,135,112,96,7,118,40,7,118,248,5,118,120,
|
||||||
|
135,119,128,135,95,8,135,113,24,135,114,152,135,121,152,129,44,238,240,14,238,224,14,245,192,14,236,48,3,98,200,161,28,228,161,28,204,161,28,228,161,28,220,97,28,202,33,28,196,129,
|
||||||
|
29,202,97,6,214,144,67,57,200,67,57,152,67,57,200,67,57,184,195,56,148,67,56,136,3,59,148,195,47,188,131,60,252,130,59,212,3,59,176,195,12,199,105,135,112,88,135,114,112,131,116,104,
|
||||||
|
7,120,96,135,116,24,135,116,160,135,25,206,83,15,238,0,15,242,80,14,228,144,14,227,64,15,225,32,14,236,80,14,51,32,40,29,220,193,30,194,65,30,210,33,28,220,129,30,220,224,28,228,225,
|
||||||
|
29,234,1,30,102,24,81,56,176,67,58,156,131,59,204,80,36,118,96,7,123,104,7,55,96,135,119,120,7,120,152,81,76,244,144,15,240,80,14,51,30,106,30,202,97,28,232,33,29,222,193,29,126,1,
|
||||||
|
30,228,161,28,204,33,29,240,97,6,84,133,131,56,204,195,59,176,67,61,208,67,57,252,194,60,228,67,59,136,195,59,176,195,140,197,10,135,121,152,135,119,24,135,116,8,7,122,40,7,114,152,
|
||||||
|
129,92,227,16,14,236,192,14,229,80,14,243,48,35,193,210,65,30,228,225,23,216,225,29,222,1,30,102,72,25,59,176,131,61,180,131,27,132,195,56,140,67,57,204,195,60,184,193,57,200,195,59,
|
||||||
|
212,3,60,204,72,180,113,8,7,118,96,7,113,8,135,113,88,135,25,219,198,14,236,96,15,237,224,6,240,32,15,229,48,15,229,32,15,246,80,14,110,16,14,227,48,14,229,48,15,243,224,6,233,224,
|
||||||
|
14,228,80,14,248,48,35,226,236,97,28,194,129,29,216,225,23,236,33,29,230,33,29,196,33,29,216,33,29,232,33,31,102,32,157,59,188,67,61,184,3,57,148,131,57,204,88,188,112,112,7,119,120,
|
||||||
|
7,122,8,7,122,72,135,119,112,135,25,206,135,14,229,16,14,240,16,14,236,192,14,239,48,14,243,144,14,244,80,14,51,40,48,8,135,116,144,7,55,48,135,122,112,135,113,160,135,116,120,7,119,
|
||||||
|
248,133,115,144,135,119,168,7,120,152,7,0,0,0,0,121,32,0,0,26,1,0,0,114,30,72,32,67,136,12,25,9,114,50,72,32,35,129,140,145,145,209,68,160,16,40,100,60,49,50,66,142,144,33,163,152,
|
||||||
|
6,100,208,82,0,0,0,139,210,88,216,6,109,80,28,20,27,71,6,209,18,25,76,178,24,6,179,64,18,49,24,202,131,68,148,161,68,87,35,0,0,0,0,83,68,75,32,86,101,114,115,105,111,110,119,99,104,
|
||||||
|
97,114,95,115,105,122,101,102,114,97,109,101,45,112,111,105,110,116,101,114,97,105,114,46,109,97,120,95,100,101,118,105,99,101,95,98,117,102,102,101,114,115,97,105,114,46,109,97,120,
|
||||||
|
95,99,111,110,115,116,97,110,116,95,98,117,102,102,101,114,115,97,105,114,46,109,97,120,95,116,104,114,101,97,100,103,114,111,117,112,95,98,117,102,102,101,114,115,97,105,114,46,109,
|
||||||
|
97,120,95,116,101,120,116,117,114,101,115,97,105,114,46,109,97,120,95,114,101,97,100,95,119,114,105,116,101,95,116,101,120,116,117,114,101,115,97,105,114,46,109,97,120,95,115,97,109,
|
||||||
|
112,108,101,114,115,65,112,112,108,101,32,109,101,116,97,108,32,118,101,114,115,105,111,110,32,51,50,48,50,51,46,51,54,56,32,40,109,101,116,97,108,102,101,45,51,50,48,50,51,46,51,54,
|
||||||
|
56,41,77,101,116,97,108,97,105,114,46,99,111,109,112,105,108,101,46,100,101,110,111,114,109,115,95,100,105,115,97,98,108,101,97,105,114,46,99,111,109,112,105,108,101,46,102,97,115,
|
||||||
|
116,95,109,97,116,104,95,101,110,97,98,108,101,97,105,114,46,99,111,109,112,105,108,101,46,102,114,97,109,101,98,117,102,102,101,114,95,102,101,116,99,104,95,101,110,97,98,108,101,
|
||||||
|
97,105,114,46,118,101,114,116,101,120,95,111,117,116,112,117,116,117,115,101,114,40,108,111,99,110,48,41,97,105,114,46,97,114,103,95,116,121,112,101,95,110,97,109,101,102,108,111,97,
|
||||||
|
116,52,97,105,114,46,97,114,103,95,110,97,109,101,79,117,116,95,67,111,108,111,114,117,115,101,114,40,108,111,99,110,49,41,102,108,111,97,116,50,79,117,116,95,85,86,97,105,114,46,112,
|
||||||
|
111,115,105,116,105,111,110,103,108,95,80,111,115,105,116,105,111,110,97,105,114,46,118,101,114,116,101,120,95,105,110,112,117,116,97,105,114,46,108,111,99,97,116,105,111,110,95,105,
|
||||||
|
110,100,101,120,97,80,111,115,97,85,86,97,67,111,108,111,114,97,105,114,46,98,117,102,102,101,114,97,105,114,46,98,117,102,102,101,114,95,115,105,122,101,97,105,114,46,114,101,97,100,
|
||||||
|
97,105,114,46,97,100,100,114,101,115,115,95,115,112,97,99,101,97,105,114,46,115,116,114,117,99,116,95,116,121,112,101,95,105,110,102,111,117,83,99,97,108,101,117,84,114,97,110,115,
|
||||||
|
108,97,116,101,97,105,114,46,97,114,103,95,116,121,112,101,95,115,105,122,101,97,105,114,46,97,114,103,95,116,121,112,101,95,97,108,105,103,110,95,115,105,122,101,117,80,117,115,104,
|
||||||
|
67,111,110,115,116,97,110,116,112,99,0,0,0,166,119,0,0,0,0,0,0,48,130,144,4,35,8,74,51,130,144,8,35,8,201,48,130,144,16,35,8,73,49,130,144,24,35,8,201,49,130,144,32,35,8,73,50,130,
|
||||||
|
144,40,35,8,201,50,130,112,0,51,12,106,16,172,193,12,3,27,8,109,48,195,224,6,131,26,204,48,184,1,241,6,51,12,110,80,188,193,12,131,27,24,111,48,195,224,6,7,28,204,48,184,1,18,7,51,
|
||||||
|
12,110,144,200,193,12,129,50,195,160,6,115,64,7,51,16,75,29,176,1,29,204,16,48,51,4,205,12,129,51,131,241,64,145,52,81,51,24,79,21,89,211,53,67,129,69,210,148,205,48,152,194,41,160,
|
||||||
|
194,12,9,29,104,27,29,176,65,100,77,220,12,9,27,104,27,27,176,65,100,77,221,12,137,26,104,155,26,176,65,36,77,222,12,10,29,196,1,29,88,100,16,7,113,64,7,86,25,204,64,213,193,7,6,114,
|
||||||
|
176,209,1,27,132,129,24,168,193,24,180,130,25,200,193,25,196,65,132,6,83,26,204,64,168,194,42,176,130,43,204,48,216,65,42,188,194,157,1,192,113,28,199,113,28,199,113,28,199,185,129,
|
||||||
|
27,184,129,27,184,129,27,184,129,27,184,129,69,7,122,96,89,22,29,208,129,27,208,1,46,224,2,46,240,3,122,128,130,140,4,38,40,35,54,54,187,54,151,182,55,178,58,182,50,23,51,182,176,179,
|
||||||
|
185,81,18,59,184,3,60,200,3,61,216,3,62,232,3,63,72,133,141,205,174,205,37,141,172,204,141,110,148,224,15,114,9,75,147,115,177,43,147,155,75,123,115,27,37,0,133,164,194,210,228,92,
|
||||||
|
216,194,220,206,234,194,206,202,190,236,202,228,230,210,222,220,70,9,66,33,167,176,52,57,151,177,183,54,184,52,182,178,175,55,56,186,180,55,183,185,81,6,81,24,5,82,72,37,44,77,206,
|
||||||
|
197,174,76,142,174,12,111,148,224,21,0,0,0,169,24,0,0,37,0,0,0,11,10,114,40,135,119,128,7,122,88,112,152,67,61,184,195,56,176,67,57,208,195,130,230,28,198,161,13,232,65,30,194,193,
|
||||||
|
29,230,33,29,232,33,29,222,193,29,22,52,227,96,14,231,80,15,225,32,15,228,64,15,225,32,15,231,80,14,244,176,128,129,7,121,40,135,112,96,7,118,120,135,113,8,7,122,40,7,114,88,112,156,
|
||||||
|
195,56,180,1,59,164,131,61,148,195,2,107,28,216,33,28,220,225,28,220,32,28,228,97,28,220,32,28,232,129,30,194,97,28,208,161,28,200,97,28,194,129,29,216,97,193,1,15,244,32,15,225,80,
|
||||||
|
15,244,128,14,0,0,0,0,209,16,0,0,6,0,0,0,7,204,60,164,131,59,156,3,59,148,3,61,160,131,60,148,67,56,144,195,1,0,0,0,97,32,0,0,68,0,0,0,19,4,65,44,16,0,0,0,11,0,0,0,148,51,0,180,37,
|
||||||
|
64,61,7,161,72,146,52,7,161,72,9,65,48,26,48,2,48,70,0,130,32,136,127,20,115,16,150,117,97,36,163,1,52,51,0,0,0,0,0,241,48,0,0,32,0,0,0,34,71,200,144,81,18,196,43,0,0,0,0,207,115,89,
|
||||||
|
0,111,109,110,105,112,111,116,101,110,116,32,99,104,97,114,83,105,109,112,108,101,32,67,43,43,32,84,66,65,65,97,105,114,45,97,108,105,97,115,45,115,99,111,112,101,115,40,109,97,105,
|
||||||
|
110,48,41,97,105,114,45,97,108,105,97,115,45,115,99,111,112,101,45,97,114,103,40,51,41,0,19,132,133,89,33,216,194,44,172,24,110,193,22,104,97,67,32,11,27,134,88,192,133,90,216,48,228,
|
||||||
|
66,46,212,194,134,224,22,0,0,157,134,5,146,40,16,130,1,36,254,157,6,103,234,40,16,130,67,0,254,131,12,1,226,12,50,4,138,51,134,48,68,22,128,255,28,195,16,76,179,13,204,5,204,54,4,89,
|
||||||
|
48,219,16,12,194,6,1,49,0,4,0,0,0,91,138,32,200,133,67,23,182,20,68,144,11,135,46,0,0,0,0,0,0,113,32,0,0,3,0,0,0,50,14,16,34,132,0,134,6,0,0,0,0,0,0,0,0,101,12,0,0,31,0,0,0,18,3,148,
|
||||||
|
240,0,0,0,0,3,0,0,0,5,0,0,0,9,0,0,0,76,0,0,0,1,0,0,0,88,0,0,0,0,0,0,0,88,0,0,0,1,0,0,0,112,0,0,0,0,0,0,0,14,0,0,0,24,0,0,0,0,0,0,0,5,0,0,0,5,0,0,0,0,0,0,0,112,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
1,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,5,0,0,0,255,255,255,255,0,36,0,0,0,0,0,0,93,12,0,0,13,0,0,0,18,3,148,102,0,0,0,0,109,97,105,110,48,51,50,48,50,51,46,51,54,56,97,105,114,54,
|
||||||
|
52,45,97,112,112,108,101,45,109,97,99,111,115,120,49,52,46,48,46,48,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
};
|
||||||
|
const uint8_t metallib_fragment[3787] = {
|
||||||
|
77,84,76,66,1,128,2,0,7,0,0,129,14,0,0,0,203,14,0,0,0,0,0,0,88,0,0,0,0,0,0,0,123,0,0,0,0,0,0,0,219,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,227,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,235,0,0,0,0,0,0,0,
|
||||||
|
224,13,0,0,0,0,0,0,1,0,0,0,123,0,0,0,78,65,77,69,6,0,109,97,105,110,48,0,84,89,80,69,1,0,1,72,65,83,72,32,0,201,103,233,140,10,95,185,107,79,93,85,82,78,218,248,8,95,184,8,139,191,
|
||||||
|
155,174,56,51,95,203,135,255,117,44,62,77,68,83,90,8,0,224,13,0,0,0,0,0,0,79,70,70,84,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,86,69,82,83,8,0,2,0,6,0,3,0,1,0,69,78,68,
|
||||||
|
84,69,78,68,84,4,0,0,0,69,78,68,84,4,0,0,0,69,78,68,84,222,192,23,11,0,0,0,0,20,0,0,0,196,13,0,0,255,255,255,255,66,67,192,222,53,20,0,0,3,0,0,0,98,12,48,36,128,16,5,200,20,0,0,0,33,
|
||||||
|
12,0,0,44,3,0,0,11,2,33,0,2,0,0,0,22,0,0,0,7,129,35,145,65,200,4,73,6,16,50,57,146,1,132,12,37,5,8,25,30,4,139,98,128,20,69,2,66,146,11,66,164,16,50,20,56,8,24,75,10,50,82,136,72,112,
|
||||||
|
196,33,35,68,18,135,140,16,65,146,2,100,200,8,177,20,32,67,70,136,32,201,1,50,82,132,24,42,40,42,144,49,124,176,92,145,32,197,200,0,0,0,137,32,0,0,31,0,0,0,50,34,72,9,32,98,70,0,33,
|
||||||
|
43,36,152,20,33,37,36,152,20,25,39,12,133,164,144,96,82,100,92,32,36,101,130,128,154,1,24,70,32,128,27,132,97,4,1,64,74,154,34,74,152,252,127,34,174,137,138,136,223,30,254,105,140,
|
||||||
|
0,24,68,32,2,140,164,41,162,132,201,255,37,128,121,22,34,250,167,49,2,96,16,193,16,76,33,194,40,135,208,28,1,114,132,160,230,8,130,57,2,48,24,70,16,26,163,172,114,6,115,12,128,70,111,
|
||||||
|
32,64,5,218,8,0,0,81,24,0,0,105,0,0,0,27,246,35,248,255,255,255,255,1,104,3,96,13,0,83,0,252,0,144,128,10,232,3,34,28,224,1,30,228,225,29,240,161,13,204,161,30,220,97,28,218,192,28,
|
||||||
|
224,161,13,218,33,28,232,1,29,0,122,144,135,122,40,7,128,48,7,121,8,135,118,40,135,54,128,135,119,72,7,119,160,135,114,144,7,32,28,216,129,29,0,162,29,210,193,29,218,128,29,202,225,
|
||||||
|
28,194,129,29,218,192,30,202,97,28,232,225,29,228,161,13,238,33,29,200,129,30,208,1,136,3,57,192,3,96,112,135,119,104,3,113,168,135,116,96,7,122,72,7,119,152,7,128,112,135,119,104,
|
||||||
|
131,116,112,7,115,152,135,54,48,7,120,104,131,118,8,7,122,64,7,128,30,228,161,30,202,1,32,220,225,29,218,192,29,194,193,29,230,161,13,204,1,30,218,160,29,194,129,30,208,1,160,7,121,
|
||||||
|
168,135,114,0,8,119,120,135,54,152,135,116,56,7,119,40,7,114,104,3,125,40,7,121,120,135,121,104,3,115,128,135,54,104,135,112,160,7,116,0,232,65,30,234,161,28,0,194,29,222,161,13,232,
|
||||||
|
65,30,194,1,30,224,33,29,220,225,28,218,160,29,194,129,30,208,1,160,7,121,168,135,114,0,136,121,160,135,112,24,135,117,104,3,120,144,135,119,160,135,114,24,7,122,120,7,121,104,3,113,
|
||||||
|
168,7,115,48,135,114,144,135,54,152,135,116,208,135,114,0,240,0,32,234,193,29,230,33,28,204,161,28,218,192,28,224,161,13,218,33,28,232,1,29,0,122,144,135,122,40,7,96,131,33,12,192,
|
||||||
|
2,84,27,140,129,0,22,160,218,0,17,255,255,255,255,63,0,109,0,172,1,96,10,128,31,0,18,80,1,125,176,193,40,2,96,1,170,13,134,33,0,11,80,109,96,142,255,255,255,255,31,128,54,0,214,0,144,
|
||||||
|
128,10,232,3,0,73,24,0,0,4,0,0,0,19,134,64,24,38,12,68,97,76,24,142,194,0,0,0,0,19,176,112,72,7,121,176,3,58,104,131,112,128,7,120,96,135,114,104,131,118,8,135,113,120,135,121,192,
|
||||||
|
135,56,160,3,55,128,3,55,128,131,13,183,81,14,109,0,15,122,96,7,116,160,7,118,64,7,122,96,7,116,208,6,233,16,7,122,128,7,122,128,7,109,144,14,120,160,7,120,160,7,120,208,6,233,16,7,
|
||||||
|
118,160,7,113,96,7,122,16,7,118,208,6,233,48,7,114,160,7,115,32,7,122,48,7,114,208,6,233,96,7,116,160,7,118,64,7,122,96,7,116,208,6,230,48,7,114,160,7,115,32,7,122,48,7,114,208,6,230,
|
||||||
|
96,7,116,160,7,118,64,7,122,96,7,116,208,6,246,16,7,118,160,7,113,96,7,122,16,7,118,208,6,246,32,7,116,160,7,115,32,7,122,48,7,114,208,6,246,48,7,114,160,7,115,32,7,122,48,7,114,208,
|
||||||
|
6,246,64,7,120,160,7,118,64,7,122,96,7,116,208,6,246,96,7,116,160,7,118,64,7,122,96,7,116,208,6,246,144,7,118,160,7,113,32,7,120,160,7,113,32,7,120,208,6,246,16,7,114,128,7,122,16,
|
||||||
|
7,114,128,7,122,16,7,114,128,7,109,96,15,113,144,7,114,160,7,114,80,7,118,160,7,114,80,7,118,208,6,246,32,7,117,96,7,122,32,7,117,96,7,122,32,7,117,96,7,109,96,15,117,16,7,114,160,
|
||||||
|
7,117,16,7,114,160,7,117,16,7,114,208,6,246,16,7,112,32,7,116,160,7,113,0,7,114,64,7,122,16,7,112,32,7,116,208,6,238,128,7,122,16,7,118,160,7,115,32,7,26,33,12,89,48,0,210,208,67,42,
|
||||||
|
160,64,0,0,8,0,0,0,4,0,0,0,0,0,10,96,72,85,108,15,16,0,2,0,0,128,0,0,0,0,0,64,1,72,108,16,40,234,50,0,0,144,5,2,0,0,0,10,0,0,0,50,30,152,16,25,17,76,144,140,9,38,71,198,4,67,106,69,
|
||||||
|
80,2,133,80,14,229,83,128,2,5,81,32,197,48,2,80,6,36,199,18,158,0,0,177,24,0,0,165,0,0,0,51,8,128,28,196,225,28,102,20,1,61,136,67,56,132,195,140,66,128,7,121,120,7,115,152,113,12,
|
||||||
|
230,0,15,237,16,14,244,128,14,51,12,66,30,194,193,29,206,161,28,102,48,5,61,136,67,56,132,131,27,204,3,61,200,67,61,140,3,61,204,120,140,116,112,7,123,8,7,121,72,135,112,112,7,122,
|
||||||
|
112,3,118,120,135,112,32,135,25,204,17,14,236,144,14,225,48,15,110,48,15,227,240,14,240,80,14,51,16,196,29,222,33,28,216,33,29,194,97,30,102,48,137,59,188,131,59,208,67,57,180,3,60,
|
||||||
|
188,131,60,132,3,59,204,240,20,118,96,7,123,104,7,55,104,135,114,104,7,55,128,135,112,144,135,112,96,7,118,40,7,118,248,5,118,120,135,119,128,135,95,8,135,113,24,135,114,152,135,121,
|
||||||
|
152,129,44,238,240,14,238,224,14,245,192,14,236,48,3,98,200,161,28,228,161,28,204,161,28,228,161,28,220,97,28,202,33,28,196,129,29,202,97,6,214,144,67,57,200,67,57,152,67,57,200,67,
|
||||||
|
57,184,195,56,148,67,56,136,3,59,148,195,47,188,131,60,252,130,59,212,3,59,176,195,12,199,105,135,112,88,135,114,112,131,116,104,7,120,96,135,116,24,135,116,160,135,25,206,83,15,238,
|
||||||
|
0,15,242,80,14,228,144,14,227,64,15,225,32,14,236,80,14,51,32,40,29,220,193,30,194,65,30,210,33,28,220,129,30,220,224,28,228,225,29,234,1,30,102,24,81,56,176,67,58,156,131,59,204,80,
|
||||||
|
36,118,96,7,123,104,7,55,96,135,119,120,7,120,152,81,76,244,144,15,240,80,14,51,30,106,30,202,97,28,232,33,29,222,193,29,126,1,30,228,161,28,204,33,29,240,97,6,84,133,131,56,204,195,
|
||||||
|
59,176,67,61,208,67,57,252,194,60,228,67,59,136,195,59,176,195,140,197,10,135,121,152,135,119,24,135,116,8,7,122,40,7,114,152,129,92,227,16,14,236,192,14,229,80,14,243,48,35,193,210,
|
||||||
|
65,30,228,225,23,216,225,29,222,1,30,102,72,25,59,176,131,61,180,131,27,132,195,56,140,67,57,204,195,60,184,193,57,200,195,59,212,3,60,204,72,180,113,8,7,118,96,7,113,8,135,113,88,
|
||||||
|
135,25,219,198,14,236,96,15,237,224,6,240,32,15,229,48,15,229,32,15,246,80,14,110,16,14,227,48,14,229,48,15,243,224,6,233,224,14,228,80,14,248,48,35,226,236,97,28,194,129,29,216,225,
|
||||||
|
23,236,33,29,230,33,29,196,33,29,216,33,29,232,33,31,102,32,157,59,188,67,61,184,3,57,148,131,57,204,88,188,112,112,7,119,120,7,122,8,7,122,72,135,119,112,135,25,206,135,14,229,16,
|
||||||
|
14,240,16,14,236,192,14,239,48,14,243,144,14,244,80,14,51,40,48,8,135,116,144,7,55,48,135,122,112,135,113,160,135,116,120,7,119,248,133,115,144,135,119,168,7,120,152,7,0,0,0,0,121,
|
||||||
|
32,0,0,252,0,0,0,114,30,72,32,67,136,12,25,9,114,50,72,32,35,129,140,145,145,209,68,160,16,40,100,60,49,50,66,142,144,33,163,56,6,220,41,1,0,0,0,139,210,88,216,6,109,80,28,20,27,71,
|
||||||
|
6,81,100,48,134,180,40,15,178,24,197,34,41,24,178,28,13,83,68,75,32,86,101,114,115,105,111,110,119,99,104,97,114,95,115,105,122,101,102,114,97,109,101,45,112,111,105,110,116,101,114,
|
||||||
|
97,105,114,46,109,97,120,95,100,101,118,105,99,101,95,98,117,102,102,101,114,115,97,105,114,46,109,97,120,95,99,111,110,115,116,97,110,116,95,98,117,102,102,101,114,115,97,105,114,
|
||||||
|
46,109,97,120,95,116,104,114,101,97,100,103,114,111,117,112,95,98,117,102,102,101,114,115,97,105,114,46,109,97,120,95,116,101,120,116,117,114,101,115,97,105,114,46,109,97,120,95,114,
|
||||||
|
101,97,100,95,119,114,105,116,101,95,116,101,120,116,117,114,101,115,97,105,114,46,109,97,120,95,115,97,109,112,108,101,114,115,65,112,112,108,101,32,109,101,116,97,108,32,118,101,
|
||||||
|
114,115,105,111,110,32,51,50,48,50,51,46,51,54,56,32,40,109,101,116,97,108,102,101,45,51,50,48,50,51,46,51,54,56,41,77,101,116,97,108,97,105,114,46,99,111,109,112,105,108,101,46,100,
|
||||||
|
101,110,111,114,109,115,95,100,105,115,97,98,108,101,97,105,114,46,99,111,109,112,105,108,101,46,102,97,115,116,95,109,97,116,104,95,101,110,97,98,108,101,97,105,114,46,99,111,109,
|
||||||
|
112,105,108,101,46,102,114,97,109,101,98,117,102,102,101,114,95,102,101,116,99,104,95,101,110,97,98,108,101,97,105,114,46,114,101,110,100,101,114,95,116,97,114,103,101,116,97,105,114,
|
||||||
|
46,97,114,103,95,116,121,112,101,95,110,97,109,101,102,108,111,97,116,52,97,105,114,46,97,114,103,95,110,97,109,101,102,67,111,108,111,114,97,105,114,46,102,114,97,103,109,101,110,
|
||||||
|
116,95,105,110,112,117,116,117,115,101,114,40,108,111,99,110,48,41,97,105,114,46,99,101,110,116,101,114,97,105,114,46,112,101,114,115,112,101,99,116,105,118,101,73,110,95,67,111,108,
|
||||||
|
111,114,117,115,101,114,40,108,111,99,110,49,41,102,108,111,97,116,50,73,110,95,85,86,97,105,114,46,116,101,120,116,117,114,101,97,105,114,46,108,111,99,97,116,105,111,110,95,105,110,
|
||||||
|
100,101,120,97,105,114,46,115,97,109,112,108,101,116,101,120,116,117,114,101,50,100,60,102,108,111,97,116,44,32,115,97,109,112,108,101,62,115,84,101,120,116,117,114,101,97,105,114,
|
||||||
|
46,115,97,109,112,108,101,114,115,97,109,112,108,101,114,115,84,101,120,116,117,114,101,83,109,112,108,114,0,198,96,0,0,0,0,0,0,48,130,208,8,35,8,82,51,130,208,12,35,8,13,49,130,208,
|
||||||
|
20,35,8,141,49,130,208,28,35,8,13,50,130,208,36,35,8,141,50,130,208,44,35,8,13,51,130,144,0,51,12,100,16,148,193,12,131,25,8,103,48,195,128,6,3,25,204,48,160,1,145,6,51,12,104,80,164,
|
||||||
|
193,12,3,26,24,105,48,195,128,6,135,26,204,48,160,1,178,6,51,12,104,144,176,193,12,129,50,195,64,6,109,224,6,51,16,203,27,152,129,27,204,16,48,51,4,205,12,129,51,195,241,184,129,27,
|
||||||
|
64,145,52,205,16,128,194,12,137,27,80,149,117,65,145,132,205,144,152,1,149,89,23,164,73,219,12,10,25,112,157,27,152,129,7,125,18,24,204,144,188,65,24,116,110,96,6,144,24,72,99,48,3,
|
||||||
|
33,10,163,64,10,165,48,195,0,7,161,96,10,71,6,0,199,113,28,199,113,28,199,113,28,231,6,110,224,6,110,224,6,110,224,6,110,224,6,22,29,232,129,101,89,166,192,177,2,43,144,131,58,128,
|
||||||
|
130,140,4,38,40,35,54,54,187,54,151,182,55,178,58,182,50,23,51,182,176,179,185,81,18,56,136,3,57,152,3,58,168,3,59,184,3,60,72,133,141,205,174,205,37,141,172,204,141,110,148,32,15,
|
||||||
|
114,9,75,147,115,177,43,147,155,75,123,115,27,37,208,131,164,194,210,228,92,216,194,220,206,234,194,206,202,190,236,202,228,230,210,222,220,70,9,246,32,167,176,52,57,151,177,183,54,
|
||||||
|
184,52,182,178,175,55,56,186,180,55,183,185,81,6,62,232,3,63,72,38,44,77,206,197,76,46,236,172,173,204,141,110,148,192,20,0,0,0,0,169,24,0,0,37,0,0,0,11,10,114,40,135,119,128,7,122,
|
||||||
|
88,112,152,67,61,184,195,56,176,67,57,208,195,130,230,28,198,161,13,232,65,30,194,193,29,230,33,29,232,33,29,222,193,29,22,52,227,96,14,231,80,15,225,32,15,228,64,15,225,32,15,231,
|
||||||
|
80,14,244,176,128,129,7,121,40,135,112,96,7,118,120,135,113,8,7,122,40,7,114,88,112,156,195,56,180,1,59,164,131,61,148,195,2,107,28,216,33,28,220,225,28,220,32,28,228,97,28,220,32,
|
||||||
|
28,232,129,30,194,97,28,208,161,28,200,97,28,194,129,29,216,97,193,1,15,244,32,15,225,80,15,244,128,14,0,0,0,0,209,16,0,0,6,0,0,0,7,204,60,164,131,59,156,3,59,148,3,61,160,131,60,148,
|
||||||
|
67,56,144,195,1,0,0,0,97,32,0,0,49,0,0,0,19,4,65,44,16,0,0,0,4,0,0,0,196,106,96,4,128,220,8,0,129,17,0,18,51,0,0,0,241,48,0,0,28,0,0,0,34,71,200,144,81,14,196,42,0,0,0,0,23,134,1,0,
|
||||||
|
97,105,114,45,97,108,105,97,115,45,115,99,111,112,101,115,40,109,97,105,110,48,41,97,105,114,45,97,108,105,97,115,45,115,99,111,112,101,45,115,97,109,112,108,101,114,115,97,105,114,
|
||||||
|
45,97,108,105,97,115,45,115,99,111,112,101,45,116,101,120,116,117,114,101,115,0,43,132,85,64,133,21,3,43,172,66,42,172,24,90,97,21,84,97,131,208,10,172,0,0,35,6,205,16,130,96,240,88,
|
||||||
|
135,129,20,3,33,8,204,104,66,0,96,176,136,255,108,3,17,0,27,4,196,0,0,0,2,0,0,0,91,6,224,104,5,0,0,0,0,0,0,0,113,32,0,0,3,0,0,0,50,14,16,34,132,0,251,5,0,0,0,0,0,0,0,0,101,12,0,0,37,
|
||||||
|
0,0,0,18,3,148,40,1,0,0,0,3,0,0,0,32,0,0,0,9,0,0,0,76,0,0,0,1,0,0,0,88,0,0,0,0,0,0,0,88,0,0,0,2,0,0,0,136,0,0,0,0,0,0,0,41,0,0,0,24,0,0,0,0,0,0,0,5,0,0,0,5,0,0,0,0,0,0,0,136,0,0,0,
|
||||||
|
0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,5,0,0,0,255,255,255,255,0,36,0,0,5,0,0,0,27,0,0,0,5,0,0,0,27,0,0,0,255,255,255,255,8,36,0,0,0,0,0,0,93,12,0,0,20,0,0,0,18,3,
|
||||||
|
148,161,0,0,0,0,109,97,105,110,48,97,105,114,46,115,97,109,112,108,101,95,116,101,120,116,117,114,101,95,50,100,46,118,52,102,51,50,51,50,48,50,51,46,51,54,56,97,105,114,54,52,45,97,
|
||||||
|
112,112,108,101,45,109,97,99,111,115,120,49,52,46,48,46,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
};
|
||||||
|
#elif TARGET_OS_IPHONE
|
||||||
|
const uint8_t metallib_vertex[3876] = {
|
||||||
|
77,84,76,66,1,0,2,0,7,0,0,130,18,0,1,0,36,15,0,0,0,0,0,0,88,0,0,0,0,0,0,0,123,0,0,0,0,0,0,0,219,0,0,0,0,0,0,0,49,0,0,0,0,0,0,0,12,1,0,0,0,0,0,0,8,0,0,0,0,0,0,0,20,1,0,0,0,0,0,0,16,
|
||||||
|
14,0,0,0,0,0,0,1,0,0,0,123,0,0,0,78,65,77,69,6,0,109,97,105,110,48,0,84,89,80,69,1,0,0,72,65,83,72,32,0,240,54,230,217,232,66,102,78,35,5,77,235,101,252,229,192,148,96,126,162,111,
|
||||||
|
77,253,247,211,52,17,198,182,137,68,244,77,68,83,90,8,0,16,14,0,0,0,0,0,0,79,70,70,84,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,86,69,82,83,8,0,2,0,7,0,3,0,2,0,69,78,68,
|
||||||
|
84,69,78,68,84,45,0,0,0,86,65,84,84,24,0,3,0,97,80,111,115,0,0,128,97,85,86,0,1,128,97,67,111,108,111,114,0,2,128,86,65,84,89,5,0,3,0,4,4,6,69,78,68,84,4,0,0,0,69,78,68,84,222,192,
|
||||||
|
23,11,0,0,0,0,20,0,0,0,252,13,0,0,255,255,255,255,66,67,192,222,53,20,0,0,3,0,0,0,98,12,48,36,128,16,5,200,20,0,0,0,33,12,0,0,72,3,0,0,11,2,33,0,2,0,0,0,22,0,0,0,7,129,35,145,65,200,
|
||||||
|
4,73,6,16,50,57,146,1,132,12,37,5,8,25,30,4,139,98,128,16,69,2,66,146,11,66,132,16,50,20,56,8,24,75,10,50,66,136,72,112,196,33,35,68,18,135,140,16,65,146,2,100,200,8,177,20,32,67,70,
|
||||||
|
136,32,201,1,50,66,132,24,42,40,42,144,49,124,176,92,145,32,196,200,0,0,0,137,32,0,0,24,0,0,0,50,34,8,9,32,98,70,0,33,43,36,152,16,33,37,36,152,16,25,39,12,133,164,144,96,66,100,92,
|
||||||
|
32,36,100,130,224,153,1,24,70,32,128,97,4,1,184,67,72,32,37,77,17,37,76,62,149,82,210,193,57,141,52,1,205,148,4,145,65,4,66,48,197,136,68,145,13,4,204,17,128,129,10,228,28,1,40,12,
|
||||||
|
34,8,194,48,2,145,140,0,0,0,0,0,81,24,0,0,100,0,0,0,27,246,35,248,255,255,255,255,1,48,5,192,15,0,56,0,254,0,144,0,10,232,3,34,28,224,1,30,228,225,29,240,161,13,204,161,30,220,97,28,
|
||||||
|
218,192,28,224,161,13,218,33,28,232,1,29,0,122,144,135,122,40,7,128,48,7,121,8,135,118,40,135,54,128,135,119,72,7,119,160,135,114,144,7,32,28,216,129,29,0,162,29,210,193,29,218,128,
|
||||||
|
29,202,225,28,194,129,29,218,192,30,202,97,28,232,225,29,228,161,13,238,33,29,200,129,30,208,1,136,3,57,192,3,96,112,135,119,104,3,113,168,135,116,96,7,122,72,7,119,152,7,128,112,135,
|
||||||
|
119,104,131,116,112,7,115,152,135,54,48,7,120,104,131,118,8,7,122,64,7,128,30,228,161,30,202,1,32,220,225,29,218,192,29,194,193,29,230,161,13,204,1,30,218,160,29,194,129,30,208,1,160,
|
||||||
|
7,121,168,135,114,0,8,119,120,135,54,152,135,116,56,7,119,40,7,114,104,3,125,40,7,121,120,135,121,104,3,115,128,135,54,104,135,112,160,7,116,0,232,65,30,234,161,28,0,194,29,222,161,
|
||||||
|
13,232,65,30,194,1,30,224,33,29,220,225,28,218,160,29,194,129,30,208,1,160,7,121,168,135,114,0,136,121,160,135,112,24,135,117,104,3,120,144,135,119,160,135,114,24,7,122,120,7,121,104,
|
||||||
|
3,113,168,7,115,48,135,114,144,135,54,152,135,116,208,135,114,0,240,0,32,234,193,29,230,33,28,204,161,28,218,192,28,224,161,13,218,33,28,232,1,29,0,122,144,135,122,40,7,96,195,24,8,
|
||||||
|
4,176,0,164,0,84,65,128,4,105,0,13,225,144,14,242,208,6,226,80,15,230,96,14,229,32,15,109,224,14,239,208,6,225,192,14,233,16,14,243,0,0,0,73,24,0,0,1,0,0,0,19,132,64,0,19,170,112,72,
|
||||||
|
7,121,176,3,58,104,131,112,128,7,120,96,135,114,104,131,116,120,135,121,136,3,60,112,131,56,112,3,56,216,112,27,229,208,6,240,160,7,118,64,7,122,96,7,116,160,7,118,64,7,109,144,14,
|
||||||
|
113,160,7,120,160,7,120,208,6,233,128,7,122,128,7,122,128,7,109,144,14,113,96,7,122,16,7,118,160,7,113,96,7,109,144,14,115,32,7,122,48,7,114,160,7,115,32,7,109,144,14,118,64,7,122,
|
||||||
|
96,7,116,160,7,118,64,7,109,96,14,115,32,7,122,48,7,114,160,7,115,32,7,109,96,14,118,64,7,122,96,7,116,160,7,118,64,7,109,96,15,113,96,7,122,16,7,118,160,7,113,96,7,109,96,15,114,64,
|
||||||
|
7,122,48,7,114,160,7,115,32,7,109,96,15,115,32,7,122,48,7,114,160,7,115,32,7,109,96,15,116,128,7,122,96,7,116,160,7,118,64,7,109,96,15,118,64,7,122,96,7,116,160,7,118,64,7,109,96,15,
|
||||||
|
121,96,7,122,16,7,114,128,7,122,16,7,114,128,7,109,96,15,113,32,7,120,160,7,113,32,7,120,160,7,113,32,7,120,208,6,246,16,7,121,32,7,122,32,7,117,96,7,122,32,7,117,96,7,109,96,15,114,
|
||||||
|
80,7,118,160,7,114,80,7,118,160,7,114,80,7,118,208,6,246,80,7,113,32,7,122,80,7,113,32,7,122,80,7,113,32,7,109,96,15,113,0,7,114,64,7,122,16,7,112,32,7,116,160,7,113,0,7,114,64,7,109,
|
||||||
|
224,14,120,160,7,113,96,7,122,48,7,114,160,17,194,144,5,3,32,13,61,164,2,10,3,0,128,0,0,0,64,0,0,0,0,0,160,0,36,54,8,20,85,26,0,0,200,2,1,0,11,0,0,0,50,30,152,16,25,17,76,144,140,9,
|
||||||
|
38,71,198,4,67,202,34,40,129,66,40,135,242,41,64,129,130,40,144,17,128,50,160,29,1,32,29,75,144,2,0,0,0,0,177,24,0,0,165,0,0,0,51,8,128,28,196,225,28,102,20,1,61,136,67,56,132,195,
|
||||||
|
140,66,128,7,121,120,7,115,152,113,12,230,0,15,237,16,14,244,128,14,51,12,66,30,194,193,29,206,161,28,102,48,5,61,136,67,56,132,131,27,204,3,61,200,67,61,140,3,61,204,120,140,116,112,
|
||||||
|
7,123,8,7,121,72,135,112,112,7,122,112,3,118,120,135,112,32,135,25,204,17,14,236,144,14,225,48,15,110,48,15,227,240,14,240,80,14,51,16,196,29,222,33,28,216,33,29,194,97,30,102,48,137,
|
||||||
|
59,188,131,59,208,67,57,180,3,60,188,131,60,132,3,59,204,240,20,118,96,7,123,104,7,55,104,135,114,104,7,55,128,135,112,144,135,112,96,7,118,40,7,118,248,5,118,120,135,119,128,135,95,
|
||||||
|
8,135,113,24,135,114,152,135,121,152,129,44,238,240,14,238,224,14,245,192,14,236,48,3,98,200,161,28,228,161,28,204,161,28,228,161,28,220,97,28,202,33,28,196,129,29,202,97,6,214,144,
|
||||||
|
67,57,200,67,57,152,67,57,200,67,57,184,195,56,148,67,56,136,3,59,148,195,47,188,131,60,252,130,59,212,3,59,176,195,12,199,105,135,112,88,135,114,112,131,116,104,7,120,96,135,116,24,
|
||||||
|
135,116,160,135,25,206,83,15,238,0,15,242,80,14,228,144,14,227,64,15,225,32,14,236,80,14,51,32,40,29,220,193,30,194,65,30,210,33,28,220,129,30,220,224,28,228,225,29,234,1,30,102,24,
|
||||||
|
81,56,176,67,58,156,131,59,204,80,36,118,96,7,123,104,7,55,96,135,119,120,7,120,152,81,76,244,144,15,240,80,14,51,30,106,30,202,97,28,232,33,29,222,193,29,126,1,30,228,161,28,204,33,
|
||||||
|
29,240,97,6,84,133,131,56,204,195,59,176,67,61,208,67,57,252,194,60,228,67,59,136,195,59,176,195,140,197,10,135,121,152,135,119,24,135,116,8,7,122,40,7,114,152,129,92,227,16,14,236,
|
||||||
|
192,14,229,80,14,243,48,35,193,210,65,30,228,225,23,216,225,29,222,1,30,102,72,25,59,176,131,61,180,131,27,132,195,56,140,67,57,204,195,60,184,193,57,200,195,59,212,3,60,204,72,180,
|
||||||
|
113,8,7,118,96,7,113,8,135,113,88,135,25,219,198,14,236,96,15,237,224,6,240,32,15,229,48,15,229,32,15,246,80,14,110,16,14,227,48,14,229,48,15,243,224,6,233,224,14,228,80,14,248,48,
|
||||||
|
35,226,236,97,28,194,129,29,216,225,23,236,33,29,230,33,29,196,33,29,216,33,29,232,33,31,102,32,157,59,188,67,61,184,3,57,148,131,57,204,88,188,112,112,7,119,120,7,122,8,7,122,72,135,
|
||||||
|
119,112,135,25,206,135,14,229,16,14,240,16,14,236,192,14,239,48,14,243,144,14,244,80,14,51,40,48,8,135,116,144,7,55,48,135,122,112,135,113,160,135,116,120,7,119,248,133,115,144,135,
|
||||||
|
119,168,7,120,152,7,0,0,0,0,121,32,0,0,25,1,0,0,114,30,72,32,67,136,12,25,9,114,50,72,32,35,129,140,145,145,209,68,160,16,40,100,60,49,50,66,142,144,33,163,152,6,100,208,82,0,0,0,139,
|
||||||
|
210,88,216,6,109,80,28,20,27,71,6,209,18,25,76,178,24,6,179,64,18,49,24,202,131,68,148,161,68,87,35,0,0,0,0,83,68,75,32,86,101,114,115,105,111,110,119,99,104,97,114,95,115,105,122,
|
||||||
|
101,102,114,97,109,101,45,112,111,105,110,116,101,114,97,105,114,46,109,97,120,95,100,101,118,105,99,101,95,98,117,102,102,101,114,115,97,105,114,46,109,97,120,95,99,111,110,115,116,
|
||||||
|
97,110,116,95,98,117,102,102,101,114,115,97,105,114,46,109,97,120,95,116,104,114,101,97,100,103,114,111,117,112,95,98,117,102,102,101,114,115,97,105,114,46,109,97,120,95,116,101,120,
|
||||||
|
116,117,114,101,115,97,105,114,46,109,97,120,95,114,101,97,100,95,119,114,105,116,101,95,116,101,120,116,117,114,101,115,97,105,114,46,109,97,120,95,115,97,109,112,108,101,114,115,
|
||||||
|
65,112,112,108,101,32,109,101,116,97,108,32,118,101,114,115,105,111,110,32,51,50,48,50,51,46,51,54,56,32,40,109,101,116,97,108,102,101,45,51,50,48,50,51,46,51,54,56,41,77,101,116,97,
|
||||||
|
108,97,105,114,46,99,111,109,112,105,108,101,46,100,101,110,111,114,109,115,95,100,105,115,97,98,108,101,97,105,114,46,99,111,109,112,105,108,101,46,102,97,115,116,95,109,97,116,104,
|
||||||
|
95,101,110,97,98,108,101,97,105,114,46,99,111,109,112,105,108,101,46,102,114,97,109,101,98,117,102,102,101,114,95,102,101,116,99,104,95,101,110,97,98,108,101,97,105,114,46,118,101,
|
||||||
|
114,116,101,120,95,111,117,116,112,117,116,117,115,101,114,40,108,111,99,110,48,41,97,105,114,46,97,114,103,95,116,121,112,101,95,110,97,109,101,102,108,111,97,116,52,97,105,114,46,
|
||||||
|
97,114,103,95,110,97,109,101,79,117,116,95,67,111,108,111,114,117,115,101,114,40,108,111,99,110,49,41,102,108,111,97,116,50,79,117,116,95,85,86,97,105,114,46,112,111,115,105,116,105,
|
||||||
|
111,110,103,108,95,80,111,115,105,116,105,111,110,97,105,114,46,118,101,114,116,101,120,95,105,110,112,117,116,97,105,114,46,108,111,99,97,116,105,111,110,95,105,110,100,101,120,97,
|
||||||
|
80,111,115,97,85,86,97,67,111,108,111,114,97,105,114,46,98,117,102,102,101,114,97,105,114,46,98,117,102,102,101,114,95,115,105,122,101,97,105,114,46,114,101,97,100,97,105,114,46,97,
|
||||||
|
100,100,114,101,115,115,95,115,112,97,99,101,97,105,114,46,115,116,114,117,99,116,95,116,121,112,101,95,105,110,102,111,117,83,99,97,108,101,117,84,114,97,110,115,108,97,116,101,97,
|
||||||
|
105,114,46,97,114,103,95,116,121,112,101,95,115,105,122,101,97,105,114,46,97,114,103,95,116,121,112,101,95,97,108,105,103,110,95,115,105,122,101,117,80,117,115,104,67,111,110,115,116,
|
||||||
|
97,110,116,112,99,0,0,0,230,117,0,0,0,0,0,0,48,130,144,4,35,8,10,51,130,144,8,35,8,201,48,130,144,16,35,8,73,49,130,144,24,35,8,201,49,130,144,32,35,8,73,50,130,144,40,35,8,7,48,195,
|
||||||
|
160,6,193,26,204,48,176,129,208,6,51,12,110,48,168,193,12,131,27,16,111,48,195,224,6,197,27,204,48,184,129,241,6,51,12,110,112,192,193,12,131,27,32,113,48,195,224,6,137,28,204,16,40,
|
||||||
|
51,12,106,224,6,115,48,3,177,208,129,26,204,193,12,1,51,67,208,204,16,56,51,24,15,20,73,19,53,131,241,84,145,53,93,51,20,88,36,77,217,12,67,41,152,194,41,204,144,204,129,182,205,1,
|
||||||
|
27,68,214,196,205,144,176,129,182,177,1,27,68,214,212,205,144,168,129,182,169,1,27,68,210,228,205,160,204,65,28,204,129,69,6,113,16,7,115,96,149,193,12,20,29,124,96,32,7,219,28,176,
|
||||||
|
65,24,136,129,26,140,1,43,152,129,28,156,65,28,68,104,48,165,193,12,68,42,168,194,42,180,194,12,67,29,160,130,43,156,25,0,28,199,113,28,199,113,28,199,113,110,224,6,110,224,6,110,224,
|
||||||
|
6,110,224,6,110,96,209,129,30,88,150,69,7,116,224,6,116,128,11,184,128,11,252,128,30,160,32,35,129,9,202,136,141,205,174,205,165,237,141,172,142,173,204,197,140,45,236,108,110,148,
|
||||||
|
164,14,236,224,14,240,32,15,244,96,15,248,160,15,82,97,99,179,107,115,73,35,43,115,163,27,37,240,131,92,194,210,228,92,236,202,228,230,210,222,220,70,9,254,32,169,176,52,57,23,182,
|
||||||
|
48,183,179,186,176,179,178,47,187,50,185,185,180,55,183,81,2,80,200,41,44,77,206,101,236,173,13,46,141,173,236,235,13,142,46,237,205,109,110,148,33,20,68,97,20,82,9,75,147,115,177,
|
||||||
|
43,147,163,43,195,27,37,112,5,0,0,0,169,24,0,0,37,0,0,0,11,10,114,40,135,119,128,7,122,88,112,152,67,61,184,195,56,176,67,57,208,195,130,230,28,198,161,13,232,65,30,194,193,29,230,
|
||||||
|
33,29,232,33,29,222,193,29,22,52,227,96,14,231,80,15,225,32,15,228,64,15,225,32,15,231,80,14,244,176,128,129,7,121,40,135,112,96,7,118,120,135,113,8,7,122,40,7,114,88,112,156,195,56,
|
||||||
|
180,1,59,164,131,61,148,195,2,107,28,216,33,28,220,225,28,220,32,28,228,97,28,220,32,28,232,129,30,194,97,28,208,161,28,200,97,28,194,129,29,216,97,193,1,15,244,32,15,225,80,15,244,
|
||||||
|
128,14,0,0,0,0,209,16,0,0,6,0,0,0,7,204,60,164,131,59,156,3,59,148,3,61,160,131,60,148,67,56,144,195,1,0,0,0,97,32,0,0,68,0,0,0,19,4,65,44,16,0,0,0,11,0,0,0,148,51,0,197,64,91,2,212,
|
||||||
|
115,16,73,20,69,115,16,73,36,17,4,163,1,35,0,99,4,32,8,130,248,71,49,7,97,89,23,70,50,26,64,51,3,0,0,0,241,48,0,0,32,0,0,0,34,71,200,144,81,18,196,43,0,0,0,0,207,115,89,0,111,109,110,
|
||||||
|
105,112,111,116,101,110,116,32,99,104,97,114,83,105,109,112,108,101,32,67,43,43,32,84,66,65,65,97,105,114,45,97,108,105,97,115,45,115,99,111,112,101,115,40,109,97,105,110,48,41,97,
|
||||||
|
105,114,45,97,108,105,97,115,45,115,99,111,112,101,45,97,114,103,40,51,41,0,19,132,101,89,33,212,130,44,172,24,108,161,22,102,97,67,16,11,27,6,88,184,5,90,216,48,224,2,46,208,194,134,
|
||||||
|
192,22,0,0,157,6,38,154,40,16,130,65,36,254,157,134,135,234,40,16,130,67,0,254,131,12,1,226,12,50,4,138,51,134,48,68,22,128,255,28,195,16,76,179,13,12,6,204,54,4,90,48,219,16,12,194,
|
||||||
|
6,1,49,0,4,0,0,0,91,138,32,192,133,35,23,182,20,68,128,11,71,46,0,0,0,0,0,0,113,32,0,0,3,0,0,0,50,14,16,34,132,0,132,6,0,0,0,0,0,0,0,0,101,12,0,0,31,0,0,0,18,3,148,240,0,0,0,0,3,0,
|
||||||
|
0,0,5,0,0,0,9,0,0,0,76,0,0,0,1,0,0,0,88,0,0,0,0,0,0,0,88,0,0,0,1,0,0,0,112,0,0,0,0,0,0,0,14,0,0,0,21,0,0,0,0,0,0,0,5,0,0,0,5,0,0,0,0,0,0,0,112,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,5,0,0,0,0,0,0,0,5,0,0,0,255,255,255,255,0,36,0,0,0,0,0,0,93,12,0,0,12,0,0,0,18,3,148,99,0,0,0,0,109,97,105,110,48,51,50,48,50,51,46,51,54,56,97,105,114,54,52,45,97,112,112,
|
||||||
|
108,101,45,105,111,115,49,56,46,49,46,48,0,0,0,0,0,
|
||||||
|
};
|
||||||
|
const uint8_t metallib_fragment[3771] = {
|
||||||
|
77,84,76,66,1,0,2,0,7,0,0,130,18,0,1,0,187,14,0,0,0,0,0,0,88,0,0,0,0,0,0,0,123,0,0,0,0,0,0,0,219,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,227,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,235,0,0,0,0,0,0,0,208,
|
||||||
|
13,0,0,0,0,0,0,1,0,0,0,123,0,0,0,78,65,77,69,6,0,109,97,105,110,48,0,84,89,80,69,1,0,1,72,65,83,72,32,0,167,26,51,31,140,153,203,226,66,149,243,47,185,58,96,202,28,176,71,121,86,159,
|
||||||
|
244,234,235,69,155,58,121,67,241,212,77,68,83,90,8,0,208,13,0,0,0,0,0,0,79,70,70,84,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,86,69,82,83,8,0,2,0,7,0,3,0,2,0,69,78,68,84,
|
||||||
|
69,78,68,84,4,0,0,0,69,78,68,84,4,0,0,0,69,78,68,84,222,192,23,11,0,0,0,0,20,0,0,0,180,13,0,0,255,255,255,255,66,67,192,222,53,20,0,0,3,0,0,0,98,12,48,36,128,16,5,200,20,0,0,0,33,12,
|
||||||
|
0,0,41,3,0,0,11,2,33,0,2,0,0,0,22,0,0,0,7,129,35,145,65,200,4,73,6,16,50,57,146,1,132,12,37,5,8,25,30,4,139,98,128,20,69,2,66,146,11,66,164,16,50,20,56,8,24,75,10,50,82,136,72,112,
|
||||||
|
196,33,35,68,18,135,140,16,65,146,2,100,200,8,177,20,32,67,70,136,32,201,1,50,82,132,24,42,40,42,144,49,124,176,92,145,32,197,200,0,0,0,137,32,0,0,31,0,0,0,50,34,72,9,32,98,70,0,33,
|
||||||
|
43,36,152,20,33,37,36,152,20,25,39,12,133,164,144,96,82,100,92,32,36,101,130,128,154,1,24,70,32,128,27,132,97,4,1,64,74,154,34,74,152,252,127,34,174,137,138,136,223,30,254,105,140,
|
||||||
|
0,24,68,32,2,140,164,41,162,132,201,255,37,128,121,22,34,250,167,49,2,96,16,193,16,76,33,194,40,135,208,28,1,114,132,160,230,8,130,57,2,48,24,70,16,26,163,172,114,6,115,12,128,70,111,
|
||||||
|
32,64,5,218,8,0,0,81,24,0,0,105,0,0,0,27,246,35,248,255,255,255,255,1,104,3,96,13,0,83,0,252,0,144,128,10,232,3,34,28,224,1,30,228,225,29,240,161,13,204,161,30,220,97,28,218,192,28,
|
||||||
|
224,161,13,218,33,28,232,1,29,0,122,144,135,122,40,7,128,48,7,121,8,135,118,40,135,54,128,135,119,72,7,119,160,135,114,144,7,32,28,216,129,29,0,162,29,210,193,29,218,128,29,202,225,
|
||||||
|
28,194,129,29,218,192,30,202,97,28,232,225,29,228,161,13,238,33,29,200,129,30,208,1,136,3,57,192,3,96,112,135,119,104,3,113,168,135,116,96,7,122,72,7,119,152,7,128,112,135,119,104,
|
||||||
|
131,116,112,7,115,152,135,54,48,7,120,104,131,118,8,7,122,64,7,128,30,228,161,30,202,1,32,220,225,29,218,192,29,194,193,29,230,161,13,204,1,30,218,160,29,194,129,30,208,1,160,7,121,
|
||||||
|
168,135,114,0,8,119,120,135,54,152,135,116,56,7,119,40,7,114,104,3,125,40,7,121,120,135,121,104,3,115,128,135,54,104,135,112,160,7,116,0,232,65,30,234,161,28,0,194,29,222,161,13,232,
|
||||||
|
65,30,194,1,30,224,33,29,220,225,28,218,160,29,194,129,30,208,1,160,7,121,168,135,114,0,136,121,160,135,112,24,135,117,104,3,120,144,135,119,160,135,114,24,7,122,120,7,121,104,3,113,
|
||||||
|
168,7,115,48,135,114,144,135,54,152,135,116,208,135,114,0,240,0,32,234,193,29,230,33,28,204,161,28,218,192,28,224,161,13,218,33,28,232,1,29,0,122,144,135,122,40,7,96,131,33,12,192,
|
||||||
|
2,84,27,140,129,0,22,160,218,0,17,255,255,255,255,63,0,109,0,172,1,96,10,128,31,0,18,80,1,125,176,193,40,2,96,1,170,13,134,33,0,11,80,109,96,142,255,255,255,255,31,128,54,0,214,0,144,
|
||||||
|
128,10,232,3,0,73,24,0,0,4,0,0,0,19,134,64,24,38,12,68,97,76,24,142,194,0,0,0,0,19,170,112,72,7,121,176,3,58,104,131,112,128,7,120,96,135,114,104,131,116,120,135,121,136,3,60,112,131,
|
||||||
|
56,112,3,56,216,112,27,229,208,6,240,160,7,118,64,7,122,96,7,116,160,7,118,64,7,109,144,14,113,160,7,120,160,7,120,208,6,233,128,7,122,128,7,122,128,7,109,144,14,113,96,7,122,16,7,
|
||||||
|
118,160,7,113,96,7,109,144,14,115,32,7,122,48,7,114,160,7,115,32,7,109,144,14,118,64,7,122,96,7,116,160,7,118,64,7,109,96,14,115,32,7,122,48,7,114,160,7,115,32,7,109,96,14,118,64,7,
|
||||||
|
122,96,7,116,160,7,118,64,7,109,96,15,113,96,7,122,16,7,118,160,7,113,96,7,109,96,15,114,64,7,122,48,7,114,160,7,115,32,7,109,96,15,115,32,7,122,48,7,114,160,7,115,32,7,109,96,15,116,
|
||||||
|
128,7,122,96,7,116,160,7,118,64,7,109,96,15,118,64,7,122,96,7,116,160,7,118,64,7,109,96,15,121,96,7,122,16,7,114,128,7,122,16,7,114,128,7,109,96,15,113,32,7,120,160,7,113,32,7,120,
|
||||||
|
160,7,113,32,7,120,208,6,246,16,7,121,32,7,122,32,7,117,96,7,122,32,7,117,96,7,109,96,15,114,80,7,118,160,7,114,80,7,118,160,7,114,80,7,118,208,6,246,80,7,113,32,7,122,80,7,113,32,
|
||||||
|
7,122,80,7,113,32,7,109,96,15,113,0,7,114,64,7,122,16,7,112,32,7,116,160,7,113,0,7,114,64,7,109,224,14,120,160,7,113,96,7,122,48,7,114,160,17,194,144,5,3,32,13,61,164,2,10,4,0,128,
|
||||||
|
0,0,0,64,0,0,0,0,0,160,0,134,84,197,246,0,1,32,0,0,0,8,0,0,0,0,0,20,128,196,6,129,162,43,3,0,0,89,32,10,0,0,0,50,30,152,16,25,17,76,144,140,9,38,71,198,4,67,106,69,80,2,133,80,14,229,
|
||||||
|
83,128,2,5,81,32,35,0,101,64,114,44,65,10,0,0,0,177,24,0,0,165,0,0,0,51,8,128,28,196,225,28,102,20,1,61,136,67,56,132,195,140,66,128,7,121,120,7,115,152,113,12,230,0,15,237,16,14,244,
|
||||||
|
128,14,51,12,66,30,194,193,29,206,161,28,102,48,5,61,136,67,56,132,131,27,204,3,61,200,67,61,140,3,61,204,120,140,116,112,7,123,8,7,121,72,135,112,112,7,122,112,3,118,120,135,112,32,
|
||||||
|
135,25,204,17,14,236,144,14,225,48,15,110,48,15,227,240,14,240,80,14,51,16,196,29,222,33,28,216,33,29,194,97,30,102,48,137,59,188,131,59,208,67,57,180,3,60,188,131,60,132,3,59,204,
|
||||||
|
240,20,118,96,7,123,104,7,55,104,135,114,104,7,55,128,135,112,144,135,112,96,7,118,40,7,118,248,5,118,120,135,119,128,135,95,8,135,113,24,135,114,152,135,121,152,129,44,238,240,14,
|
||||||
|
238,224,14,245,192,14,236,48,3,98,200,161,28,228,161,28,204,161,28,228,161,28,220,97,28,202,33,28,196,129,29,202,97,6,214,144,67,57,200,67,57,152,67,57,200,67,57,184,195,56,148,67,
|
||||||
|
56,136,3,59,148,195,47,188,131,60,252,130,59,212,3,59,176,195,12,199,105,135,112,88,135,114,112,131,116,104,7,120,96,135,116,24,135,116,160,135,25,206,83,15,238,0,15,242,80,14,228,
|
||||||
|
144,14,227,64,15,225,32,14,236,80,14,51,32,40,29,220,193,30,194,65,30,210,33,28,220,129,30,220,224,28,228,225,29,234,1,30,102,24,81,56,176,67,58,156,131,59,204,80,36,118,96,7,123,104,
|
||||||
|
7,55,96,135,119,120,7,120,152,81,76,244,144,15,240,80,14,51,30,106,30,202,97,28,232,33,29,222,193,29,126,1,30,228,161,28,204,33,29,240,97,6,84,133,131,56,204,195,59,176,67,61,208,67,
|
||||||
|
57,252,194,60,228,67,59,136,195,59,176,195,140,197,10,135,121,152,135,119,24,135,116,8,7,122,40,7,114,152,129,92,227,16,14,236,192,14,229,80,14,243,48,35,193,210,65,30,228,225,23,216,
|
||||||
|
225,29,222,1,30,102,72,25,59,176,131,61,180,131,27,132,195,56,140,67,57,204,195,60,184,193,57,200,195,59,212,3,60,204,72,180,113,8,7,118,96,7,113,8,135,113,88,135,25,219,198,14,236,
|
||||||
|
96,15,237,224,6,240,32,15,229,48,15,229,32,15,246,80,14,110,16,14,227,48,14,229,48,15,243,224,6,233,224,14,228,80,14,248,48,35,226,236,97,28,194,129,29,216,225,23,236,33,29,230,33,
|
||||||
|
29,196,33,29,216,33,29,232,33,31,102,32,157,59,188,67,61,184,3,57,148,131,57,204,88,188,112,112,7,119,120,7,122,8,7,122,72,135,119,112,135,25,206,135,14,229,16,14,240,16,14,236,192,
|
||||||
|
14,239,48,14,243,144,14,244,80,14,51,40,48,8,135,116,144,7,55,48,135,122,112,135,113,160,135,116,120,7,119,248,133,115,144,135,119,168,7,120,152,7,0,0,0,0,121,32,0,0,251,0,0,0,114,
|
||||||
|
30,72,32,67,136,12,25,9,114,50,72,32,35,129,140,145,145,209,68,160,16,40,100,60,49,50,66,142,144,33,163,56,6,220,41,1,0,0,0,139,210,88,216,6,109,80,28,20,27,71,6,81,100,48,134,180,
|
||||||
|
40,15,178,24,197,34,41,24,178,28,13,83,68,75,32,86,101,114,115,105,111,110,119,99,104,97,114,95,115,105,122,101,102,114,97,109,101,45,112,111,105,110,116,101,114,97,105,114,46,109,
|
||||||
|
97,120,95,100,101,118,105,99,101,95,98,117,102,102,101,114,115,97,105,114,46,109,97,120,95,99,111,110,115,116,97,110,116,95,98,117,102,102,101,114,115,97,105,114,46,109,97,120,95,116,
|
||||||
|
104,114,101,97,100,103,114,111,117,112,95,98,117,102,102,101,114,115,97,105,114,46,109,97,120,95,116,101,120,116,117,114,101,115,97,105,114,46,109,97,120,95,114,101,97,100,95,119,114,
|
||||||
|
105,116,101,95,116,101,120,116,117,114,101,115,97,105,114,46,109,97,120,95,115,97,109,112,108,101,114,115,65,112,112,108,101,32,109,101,116,97,108,32,118,101,114,115,105,111,110,32,
|
||||||
|
51,50,48,50,51,46,51,54,56,32,40,109,101,116,97,108,102,101,45,51,50,48,50,51,46,51,54,56,41,77,101,116,97,108,97,105,114,46,99,111,109,112,105,108,101,46,100,101,110,111,114,109,115,
|
||||||
|
95,100,105,115,97,98,108,101,97,105,114,46,99,111,109,112,105,108,101,46,102,97,115,116,95,109,97,116,104,95,101,110,97,98,108,101,97,105,114,46,99,111,109,112,105,108,101,46,102,114,
|
||||||
|
97,109,101,98,117,102,102,101,114,95,102,101,116,99,104,95,101,110,97,98,108,101,97,105,114,46,114,101,110,100,101,114,95,116,97,114,103,101,116,97,105,114,46,97,114,103,95,116,121,
|
||||||
|
112,101,95,110,97,109,101,102,108,111,97,116,52,97,105,114,46,97,114,103,95,110,97,109,101,102,67,111,108,111,114,97,105,114,46,102,114,97,103,109,101,110,116,95,105,110,112,117,116,
|
||||||
|
117,115,101,114,40,108,111,99,110,48,41,97,105,114,46,99,101,110,116,101,114,97,105,114,46,112,101,114,115,112,101,99,116,105,118,101,73,110,95,67,111,108,111,114,117,115,101,114,40,
|
||||||
|
108,111,99,110,49,41,102,108,111,97,116,50,73,110,95,85,86,97,105,114,46,116,101,120,116,117,114,101,97,105,114,46,108,111,99,97,116,105,111,110,95,105,110,100,101,120,97,105,114,46,
|
||||||
|
115,97,109,112,108,101,116,101,120,116,117,114,101,50,100,60,102,108,111,97,116,44,32,115,97,109,112,108,101,62,115,84,101,120,116,117,114,101,97,105,114,46,115,97,109,112,108,101,
|
||||||
|
114,115,97,109,112,108,101,114,115,84,101,120,116,117,114,101,83,109,112,108,114,0,6,95,0,0,0,0,0,0,48,130,208,8,35,8,18,51,130,208,12,35,8,13,49,130,208,20,35,8,141,49,130,208,28,
|
||||||
|
35,8,13,50,130,208,36,35,8,141,50,130,208,44,35,8,9,48,195,64,6,65,25,204,48,152,129,112,6,51,12,104,48,144,193,12,3,26,16,105,48,195,128,6,69,26,204,48,160,129,145,6,51,12,104,112,
|
||||||
|
168,193,12,3,26,32,107,48,195,128,6,9,27,204,16,40,51,12,100,128,6,109,48,3,177,184,1,25,180,193,12,1,51,67,208,204,16,56,51,28,79,27,180,1,20,73,211,12,193,31,204,144,180,1,85,89,
|
||||||
|
23,20,73,216,12,137,25,80,153,117,65,154,180,205,160,144,1,215,181,129,25,120,208,39,129,193,12,137,27,132,65,215,6,102,0,137,129,52,6,51,16,161,32,10,163,64,10,51,12,111,0,10,165,
|
||||||
|
112,99,0,112,28,199,113,28,199,113,28,199,185,129,27,184,129,27,184,129,27,184,129,27,184,129,69,7,122,96,89,150,41,112,172,192,10,228,160,14,160,32,35,129,9,202,136,141,205,174,205,
|
||||||
|
165,237,141,172,142,173,204,197,140,45,236,108,110,148,228,13,224,32,14,228,96,14,232,160,14,236,224,14,82,97,99,179,107,115,73,35,43,115,163,27,37,192,131,92,194,210,228,92,236,202,
|
||||||
|
228,230,210,222,220,70,9,242,32,169,176,52,57,23,182,48,183,179,186,176,179,178,47,187,50,185,185,180,55,183,81,2,61,200,41,44,77,206,101,236,173,13,46,141,173,236,235,13,142,46,237,
|
||||||
|
205,109,110,148,97,15,248,160,15,146,9,75,147,115,49,147,11,59,107,43,115,163,27,37,40,5,0,0,0,0,169,24,0,0,37,0,0,0,11,10,114,40,135,119,128,7,122,88,112,152,67,61,184,195,56,176,
|
||||||
|
67,57,208,195,130,230,28,198,161,13,232,65,30,194,193,29,230,33,29,232,33,29,222,193,29,22,52,227,96,14,231,80,15,225,32,15,228,64,15,225,32,15,231,80,14,244,176,128,129,7,121,40,135,
|
||||||
|
112,96,7,118,120,135,113,8,7,122,40,7,114,88,112,156,195,56,180,1,59,164,131,61,148,195,2,107,28,216,33,28,220,225,28,220,32,28,228,97,28,220,32,28,232,129,30,194,97,28,208,161,28,
|
||||||
|
200,97,28,194,129,29,216,97,193,1,15,244,32,15,225,80,15,244,128,14,0,0,0,0,209,16,0,0,6,0,0,0,7,204,60,164,131,59,156,3,59,148,3,61,160,131,60,148,67,56,144,195,1,0,0,0,97,32,0,0,
|
||||||
|
49,0,0,0,19,4,65,44,16,0,0,0,4,0,0,0,196,106,96,4,128,220,8,0,129,17,0,18,51,0,0,0,241,48,0,0,28,0,0,0,34,71,200,144,81,14,196,42,0,0,0,0,23,134,1,0,97,105,114,45,97,108,105,97,115,
|
||||||
|
45,115,99,111,112,101,115,40,109,97,105,110,48,41,97,105,114,45,97,108,105,97,115,45,115,99,111,112,101,45,115,97,109,112,108,101,114,115,97,105,114,45,97,108,105,97,115,45,115,99,
|
||||||
|
111,112,101,45,116,101,120,116,117,114,101,115,0,43,4,85,56,133,21,195,42,168,2,42,172,24,88,65,21,82,97,131,192,10,171,0,0,35,6,205,16,130,96,240,84,135,129,20,3,33,8,204,104,66,0,
|
||||||
|
96,176,136,255,108,3,17,0,27,4,196,0,0,0,2,0,0,0,91,6,224,96,5,0,0,0,0,0,0,0,113,32,0,0,3,0,0,0,50,14,16,34,132,0,248,5,0,0,0,0,0,0,0,0,101,12,0,0,37,0,0,0,18,3,148,40,1,0,0,0,3,0,
|
||||||
|
0,0,32,0,0,0,9,0,0,0,76,0,0,0,1,0,0,0,88,0,0,0,0,0,0,0,88,0,0,0,2,0,0,0,136,0,0,0,0,0,0,0,41,0,0,0,21,0,0,0,0,0,0,0,5,0,0,0,5,0,0,0,0,0,0,0,136,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,0,5,0,0,0,0,0,0,0,5,0,0,0,255,255,255,255,0,36,0,0,5,0,0,0,27,0,0,0,5,0,0,0,27,0,0,0,255,255,255,255,8,36,0,0,0,0,0,0,93,12,0,0,19,0,0,0,18,3,148,126,0,0,0,0,109,97,105,110,
|
||||||
|
48,97,105,114,46,115,97,109,112,108,101,95,116,101,120,116,117,114,101,95,50,100,46,118,52,102,51,50,51,50,48,50,51,46,51,54,56,97,105,114,54,52,45,97,112,112,108,101,45,105,111,115,
|
||||||
|
49,56,46,49,46,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
};
|
||||||
|
#elif TARGET_IPHONE_SIMULATOR
|
||||||
|
#error "SDL_GPU does not support the iphone simulator"
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
300
imgui/backends/imgui_impl_sdlrenderer2.cpp
Normal file
300
imgui/backends/imgui_impl_sdlrenderer2.cpp
Normal file
@@ -0,0 +1,300 @@
|
|||||||
|
// dear imgui: Renderer Backend for SDL_Renderer for SDL2
|
||||||
|
// (Requires: SDL 2.0.17+)
|
||||||
|
|
||||||
|
// Note that SDL_Renderer is an _optional_ component of SDL2, which IMHO is now largely obsolete.
|
||||||
|
// For a multi-platform app consider using other technologies:
|
||||||
|
// - SDL3+SDL_GPU: SDL_GPU is SDL3 new graphics abstraction API. You will need to update to SDL3.
|
||||||
|
// - SDL2+DirectX, SDL2+OpenGL, SDL2+Vulkan: combine SDL with dedicated renderers.
|
||||||
|
// If your application wants to render any non trivial amount of graphics other than UI,
|
||||||
|
// please be aware that SDL_Renderer currently offers a limited graphic API to the end-user
|
||||||
|
// and it might be difficult to step out of those boundaries.
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
|
||||||
|
// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplSDLRenderer2_CreateFontsTexture() and ImGui_ImplSDLRenderer2_DestroyFontsTexture().
|
||||||
|
// 2025-01-18: Use endian-dependent RGBA32 texture format, to match SDL_Color.
|
||||||
|
// 2024-10-09: Expose selected render state in ImGui_ImplSDLRenderer2_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
|
||||||
|
// 2024-05-14: *BREAKING CHANGE* ImGui_ImplSDLRenderer3_RenderDrawData() requires SDL_Renderer* passed as parameter.
|
||||||
|
// 2023-05-30: Renamed imgui_impl_sdlrenderer.h/.cpp to imgui_impl_sdlrenderer2.h/.cpp to accommodate for upcoming SDL3.
|
||||||
|
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
|
||||||
|
// 2021-12-21: Update SDL_RenderGeometryRaw() format to work with SDL 2.0.19.
|
||||||
|
// 2021-12-03: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
|
||||||
|
// 2021-10-06: Backup and restore modified ClipRect/Viewport.
|
||||||
|
// 2021-09-21: Initial version.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_sdlrenderer2.h"
|
||||||
|
#include <stdint.h> // intptr_t
|
||||||
|
|
||||||
|
// Clang warnings with -Weverything
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
#pragma GCC diagnostic ignored "-Wfloat-equal" // warning: comparing floating-point with '==' or '!=' is unsafe
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// SDL
|
||||||
|
#include <SDL.h>
|
||||||
|
#if !SDL_VERSION_ATLEAST(2,0,17)
|
||||||
|
#error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// SDL_Renderer data
|
||||||
|
struct ImGui_ImplSDLRenderer2_Data
|
||||||
|
{
|
||||||
|
SDL_Renderer* Renderer; // Main viewport's renderer
|
||||||
|
|
||||||
|
ImGui_ImplSDLRenderer2_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
static ImGui_ImplSDLRenderer2_Data* ImGui_ImplSDLRenderer2_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplSDLRenderer2_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
bool ImGui_ImplSDLRenderer2_Init(SDL_Renderer* renderer)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
||||||
|
IM_ASSERT(renderer != nullptr && "SDL_Renderer not initialized!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplSDLRenderer2_Data* bd = IM_NEW(ImGui_ImplSDLRenderer2_Data)();
|
||||||
|
io.BackendRendererUserData = (void*)bd;
|
||||||
|
io.BackendRendererName = "imgui_impl_sdlrenderer2";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
bd->Renderer = renderer;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer2_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
ImGui_ImplSDLRenderer2_DestroyDeviceObjects();
|
||||||
|
|
||||||
|
io.BackendRendererName = nullptr;
|
||||||
|
io.BackendRendererUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDLRenderer2_SetupRenderState(SDL_Renderer* renderer)
|
||||||
|
{
|
||||||
|
// Clear out any viewports and cliprect set by the user
|
||||||
|
// FIXME: Technically speaking there are lots of other things we could backup/setup/restore during our render process.
|
||||||
|
SDL_RenderSetViewport(renderer, nullptr);
|
||||||
|
SDL_RenderSetClipRect(renderer, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer2_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLRenderer2_Init()?");
|
||||||
|
IM_UNUSED(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer)
|
||||||
|
{
|
||||||
|
// If there's a scale factor set by the user, use that instead
|
||||||
|
// If the user has specified a scale factor to SDL_Renderer already via SDL_RenderSetScale(), SDL will scale whatever we pass
|
||||||
|
// to SDL_RenderGeometryRaw() by that scale factor. In that case we don't want to be also scaling it ourselves here.
|
||||||
|
float rsx = 1.0f;
|
||||||
|
float rsy = 1.0f;
|
||||||
|
SDL_RenderGetScale(renderer, &rsx, &rsy);
|
||||||
|
ImVec2 render_scale;
|
||||||
|
render_scale.x = (rsx == 1.0f) ? draw_data->FramebufferScale.x : 1.0f;
|
||||||
|
render_scale.y = (rsy == 1.0f) ? draw_data->FramebufferScale.y : 1.0f;
|
||||||
|
|
||||||
|
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
|
||||||
|
int fb_width = (int)(draw_data->DisplaySize.x * render_scale.x);
|
||||||
|
int fb_height = (int)(draw_data->DisplaySize.y * render_scale.y);
|
||||||
|
if (fb_width == 0 || fb_height == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplSDLRenderer2_UpdateTexture(tex);
|
||||||
|
|
||||||
|
// Backup SDL_Renderer state that will be modified to restore it afterwards
|
||||||
|
struct BackupSDLRendererState
|
||||||
|
{
|
||||||
|
SDL_Rect Viewport;
|
||||||
|
bool ClipEnabled;
|
||||||
|
SDL_Rect ClipRect;
|
||||||
|
};
|
||||||
|
BackupSDLRendererState old = {};
|
||||||
|
old.ClipEnabled = SDL_RenderIsClipEnabled(renderer) == SDL_TRUE;
|
||||||
|
SDL_RenderGetViewport(renderer, &old.Viewport);
|
||||||
|
SDL_RenderGetClipRect(renderer, &old.ClipRect);
|
||||||
|
|
||||||
|
// Setup desired state
|
||||||
|
ImGui_ImplSDLRenderer2_SetupRenderState(renderer);
|
||||||
|
|
||||||
|
// Setup render state structure (for callbacks and custom texture bindings)
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
ImGui_ImplSDLRenderer2_RenderState render_state;
|
||||||
|
render_state.Renderer = renderer;
|
||||||
|
platform_io.Renderer_RenderState = &render_state;
|
||||||
|
|
||||||
|
// Will project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
|
||||||
|
ImVec2 clip_scale = render_scale;
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data;
|
||||||
|
const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data;
|
||||||
|
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplSDLRenderer2_SetupRenderState(renderer);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
|
||||||
|
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
|
||||||
|
if (clip_min.x < 0.0f) { clip_min.x = 0.0f; }
|
||||||
|
if (clip_min.y < 0.0f) { clip_min.y = 0.0f; }
|
||||||
|
if (clip_max.x > (float)fb_width) { clip_max.x = (float)fb_width; }
|
||||||
|
if (clip_max.y > (float)fb_height) { clip_max.y = (float)fb_height; }
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
SDL_Rect r = { (int)(clip_min.x), (int)(clip_min.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y) };
|
||||||
|
SDL_RenderSetClipRect(renderer, &r);
|
||||||
|
|
||||||
|
const float* xy = (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, pos));
|
||||||
|
const float* uv = (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, uv));
|
||||||
|
#if SDL_VERSION_ATLEAST(2,0,19)
|
||||||
|
const SDL_Color* color = (const SDL_Color*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, col)); // SDL 2.0.19+
|
||||||
|
#else
|
||||||
|
const int* color = (const int*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, col)); // SDL 2.0.17 and 2.0.18
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Bind texture, Draw
|
||||||
|
SDL_Texture* tex = (SDL_Texture*)pcmd->GetTexID();
|
||||||
|
SDL_RenderGeometryRaw(renderer, tex,
|
||||||
|
xy, (int)sizeof(ImDrawVert),
|
||||||
|
color, (int)sizeof(ImDrawVert),
|
||||||
|
uv, (int)sizeof(ImDrawVert),
|
||||||
|
draw_list->VtxBuffer.Size - pcmd->VtxOffset,
|
||||||
|
idx_buffer + pcmd->IdxOffset, pcmd->ElemCount, sizeof(ImDrawIdx));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
platform_io.Renderer_RenderState = nullptr;
|
||||||
|
|
||||||
|
// Restore modified SDL_Renderer state
|
||||||
|
SDL_RenderSetViewport(renderer, &old.Viewport);
|
||||||
|
SDL_RenderSetClipRect(renderer, old.ClipEnabled ? &old.ClipRect : nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer2_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
|
||||||
|
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
|
||||||
|
// Create texture
|
||||||
|
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
|
||||||
|
SDL_Texture* sdl_texture = SDL_CreateTexture(bd->Renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(sdl_texture != nullptr && "Backend failed to create texture!");
|
||||||
|
SDL_UpdateTexture(sdl_texture, nullptr, tex->GetPixels(), tex->GetPitch());
|
||||||
|
SDL_SetTextureBlendMode(sdl_texture, SDL_BLENDMODE_BLEND);
|
||||||
|
SDL_SetTextureScaleMode(sdl_texture, SDL_ScaleModeLinear);
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)(intptr_t)sdl_texture);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
// Update selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
|
||||||
|
SDL_Texture* sdl_texture = (SDL_Texture*)(intptr_t)tex->TexID;
|
||||||
|
for (ImTextureRect& r : tex->Updates)
|
||||||
|
{
|
||||||
|
SDL_Rect sdl_r = { r.x, r.y, r.w, r.h };
|
||||||
|
SDL_UpdateTexture(sdl_texture, &sdl_r, tex->GetPixelsAt(r.x, r.y), tex->GetPitch());
|
||||||
|
}
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantDestroy)
|
||||||
|
{
|
||||||
|
SDL_Texture* sdl_texture = (SDL_Texture*)(intptr_t)tex->TexID;
|
||||||
|
if (sdl_texture == nullptr)
|
||||||
|
return;
|
||||||
|
SDL_DestroyTexture(sdl_texture);
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer2_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer2_DestroyDeviceObjects()
|
||||||
|
{
|
||||||
|
// Destroy all textures
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
{
|
||||||
|
tex->SetStatus(ImTextureStatus_WantDestroy);
|
||||||
|
ImGui_ImplSDLRenderer2_UpdateTexture(tex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
53
imgui/backends/imgui_impl_sdlrenderer2.h
Normal file
53
imgui/backends/imgui_impl_sdlrenderer2.h
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
// dear imgui: Renderer Backend for SDL_Renderer for SDL2
|
||||||
|
// (Requires: SDL 2.0.17+)
|
||||||
|
|
||||||
|
// Note that SDL_Renderer is an _optional_ component of SDL2, which IMHO is now largely obsolete.
|
||||||
|
// For a multi-platform app consider using other technologies:
|
||||||
|
// - SDL3+SDL_GPU: SDL_GPU is SDL3 new graphics abstraction API. You will need to update to SDL3.
|
||||||
|
// - SDL2+DirectX, SDL2+OpenGL, SDL2+Vulkan: combine SDL with dedicated renderers.
|
||||||
|
// If your application wants to render any non trivial amount of graphics other than UI,
|
||||||
|
// please be aware that SDL_Renderer currently offers a limited graphic API to the end-user
|
||||||
|
// and it might be difficult to step out of those boundaries.
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
|
||||||
|
struct SDL_Renderer;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDLRenderer2_Init(SDL_Renderer* renderer);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer);
|
||||||
|
|
||||||
|
// Called by Init/NewFrame/Shutdown
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_DestroyDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
// [BETA] Selected render state data shared with callbacks.
|
||||||
|
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplSDLRenderer2_RenderDrawData() call.
|
||||||
|
// (Please open an issue if you feel you need access to more data)
|
||||||
|
struct ImGui_ImplSDLRenderer2_RenderState
|
||||||
|
{
|
||||||
|
SDL_Renderer* Renderer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
316
imgui/backends/imgui_impl_sdlrenderer3.cpp
Normal file
316
imgui/backends/imgui_impl_sdlrenderer3.cpp
Normal file
@@ -0,0 +1,316 @@
|
|||||||
|
// dear imgui: Renderer Backend for SDL_Renderer for SDL3
|
||||||
|
// (Requires: SDL 3.1.8+)
|
||||||
|
|
||||||
|
// Note that SDL_Renderer is an _optional_ component of SDL3, which IMHO is now largely obsolete.
|
||||||
|
// For a multi-platform app consider using other technologies:
|
||||||
|
// - SDL3+SDL_GPU: SDL_GPU is SDL3 new graphics abstraction API.
|
||||||
|
// - SDL3+DirectX, SDL3+OpenGL, SDL3+Vulkan: combine SDL with dedicated renderers.
|
||||||
|
// If your application wants to render any non trivial amount of graphics other than UI,
|
||||||
|
// please be aware that SDL_Renderer currently offers a limited graphic API to the end-user
|
||||||
|
// and it might be difficult to step out of those boundaries.
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
|
||||||
|
// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplSDLRenderer3_CreateFontsTexture() and ImGui_ImplSDLRenderer3_DestroyFontsTexture().
|
||||||
|
// 2025-01-18: Use endian-dependent RGBA32 texture format, to match SDL_Color.
|
||||||
|
// 2024-10-09: Expose selected render state in ImGui_ImplSDLRenderer3_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
|
||||||
|
// 2024-07-01: Update for SDL3 api changes: SDL_RenderGeometryRaw() uint32 version was removed (SDL#9009).
|
||||||
|
// 2024-05-14: *BREAKING CHANGE* ImGui_ImplSDLRenderer3_RenderDrawData() requires SDL_Renderer* passed as parameter.
|
||||||
|
// 2024-02-12: Amend to query SDL_RenderViewportSet() and restore viewport accordingly.
|
||||||
|
// 2023-05-30: Initial version.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_sdlrenderer3.h"
|
||||||
|
#include <stdint.h> // intptr_t
|
||||||
|
|
||||||
|
// Clang warnings with -Weverything
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
#pragma GCC diagnostic ignored "-Wfloat-equal" // warning: comparing floating-point with '==' or '!=' is unsafe
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// SDL
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#if !SDL_VERSION_ATLEAST(3,0,0)
|
||||||
|
#error This backend requires SDL 3.0.0+
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// SDL_Renderer data
|
||||||
|
struct ImGui_ImplSDLRenderer3_Data
|
||||||
|
{
|
||||||
|
SDL_Renderer* Renderer; // Main viewport's renderer
|
||||||
|
ImVector<SDL_FColor> ColorBuffer;
|
||||||
|
|
||||||
|
ImGui_ImplSDLRenderer3_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
static ImGui_ImplSDLRenderer3_Data* ImGui_ImplSDLRenderer3_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplSDLRenderer3_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
bool ImGui_ImplSDLRenderer3_Init(SDL_Renderer* renderer)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
||||||
|
IM_ASSERT(renderer != nullptr && "SDL_Renderer not initialized!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplSDLRenderer3_Data* bd = IM_NEW(ImGui_ImplSDLRenderer3_Data)();
|
||||||
|
io.BackendRendererUserData = (void*)bd;
|
||||||
|
io.BackendRendererName = "imgui_impl_sdlrenderer3";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
bd->Renderer = renderer;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer3_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
ImGui_ImplSDLRenderer3_DestroyDeviceObjects();
|
||||||
|
|
||||||
|
io.BackendRendererName = nullptr;
|
||||||
|
io.BackendRendererUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSDLRenderer3_SetupRenderState(SDL_Renderer* renderer)
|
||||||
|
{
|
||||||
|
// Clear out any viewports and cliprect set by the user
|
||||||
|
// FIXME: Technically speaking there are lots of other things we could backup/setup/restore during our render process.
|
||||||
|
SDL_SetRenderViewport(renderer, nullptr);
|
||||||
|
SDL_SetRenderClipRect(renderer, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer3_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLRenderer3_Init()?");
|
||||||
|
IM_UNUSED(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://github.com/libsdl-org/SDL/issues/9009
|
||||||
|
static int SDL_RenderGeometryRaw8BitColor(SDL_Renderer* renderer, ImVector<SDL_FColor>& colors_out, SDL_Texture* texture, const float* xy, int xy_stride, const SDL_Color* color, int color_stride, const float* uv, int uv_stride, int num_vertices, const void* indices, int num_indices, int size_indices)
|
||||||
|
{
|
||||||
|
const Uint8* color2 = (const Uint8*)color;
|
||||||
|
colors_out.resize(num_vertices);
|
||||||
|
SDL_FColor* color3 = colors_out.Data;
|
||||||
|
for (int i = 0; i < num_vertices; i++)
|
||||||
|
{
|
||||||
|
color3[i].r = color->r / 255.0f;
|
||||||
|
color3[i].g = color->g / 255.0f;
|
||||||
|
color3[i].b = color->b / 255.0f;
|
||||||
|
color3[i].a = color->a / 255.0f;
|
||||||
|
color2 += color_stride;
|
||||||
|
color = (const SDL_Color*)color2;
|
||||||
|
}
|
||||||
|
return SDL_RenderGeometryRaw(renderer, texture, xy, xy_stride, color3, sizeof(*color3), uv, uv_stride, num_vertices, indices, num_indices, size_indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer3_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
|
||||||
|
|
||||||
|
// If there's a scale factor set by the user, use that instead
|
||||||
|
// If the user has specified a scale factor to SDL_Renderer already via SDL_RenderSetScale(), SDL will scale whatever we pass
|
||||||
|
// to SDL_RenderGeometryRaw() by that scale factor. In that case we don't want to be also scaling it ourselves here.
|
||||||
|
float rsx = 1.0f;
|
||||||
|
float rsy = 1.0f;
|
||||||
|
SDL_GetRenderScale(renderer, &rsx, &rsy);
|
||||||
|
ImVec2 render_scale;
|
||||||
|
render_scale.x = (rsx == 1.0f) ? draw_data->FramebufferScale.x : 1.0f;
|
||||||
|
render_scale.y = (rsy == 1.0f) ? draw_data->FramebufferScale.y : 1.0f;
|
||||||
|
|
||||||
|
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
|
||||||
|
int fb_width = (int)(draw_data->DisplaySize.x * render_scale.x);
|
||||||
|
int fb_height = (int)(draw_data->DisplaySize.y * render_scale.y);
|
||||||
|
if (fb_width == 0 || fb_height == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplSDLRenderer3_UpdateTexture(tex);
|
||||||
|
|
||||||
|
// Backup SDL_Renderer state that will be modified to restore it afterwards
|
||||||
|
struct BackupSDLRendererState
|
||||||
|
{
|
||||||
|
SDL_Rect Viewport;
|
||||||
|
bool ViewportEnabled;
|
||||||
|
bool ClipEnabled;
|
||||||
|
SDL_Rect ClipRect;
|
||||||
|
};
|
||||||
|
BackupSDLRendererState old = {};
|
||||||
|
old.ViewportEnabled = SDL_RenderViewportSet(renderer);
|
||||||
|
old.ClipEnabled = SDL_RenderClipEnabled(renderer);
|
||||||
|
SDL_GetRenderViewport(renderer, &old.Viewport);
|
||||||
|
SDL_GetRenderClipRect(renderer, &old.ClipRect);
|
||||||
|
|
||||||
|
// Setup desired state
|
||||||
|
ImGui_ImplSDLRenderer3_SetupRenderState(renderer);
|
||||||
|
|
||||||
|
// Setup render state structure (for callbacks and custom texture bindings)
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
ImGui_ImplSDLRenderer3_RenderState render_state;
|
||||||
|
render_state.Renderer = renderer;
|
||||||
|
platform_io.Renderer_RenderState = &render_state;
|
||||||
|
|
||||||
|
// Will project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
|
||||||
|
ImVec2 clip_scale = render_scale;
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data;
|
||||||
|
const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data;
|
||||||
|
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplSDLRenderer3_SetupRenderState(renderer);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
|
||||||
|
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
|
||||||
|
if (clip_min.x < 0.0f) { clip_min.x = 0.0f; }
|
||||||
|
if (clip_min.y < 0.0f) { clip_min.y = 0.0f; }
|
||||||
|
if (clip_max.x > (float)fb_width) { clip_max.x = (float)fb_width; }
|
||||||
|
if (clip_max.y > (float)fb_height) { clip_max.y = (float)fb_height; }
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
SDL_Rect r = { (int)(clip_min.x), (int)(clip_min.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y) };
|
||||||
|
SDL_SetRenderClipRect(renderer, &r);
|
||||||
|
|
||||||
|
const float* xy = (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, pos));
|
||||||
|
const float* uv = (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, uv));
|
||||||
|
const SDL_Color* color = (const SDL_Color*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, col)); // SDL 2.0.19+
|
||||||
|
|
||||||
|
// Bind texture, Draw
|
||||||
|
SDL_Texture* tex = (SDL_Texture*)pcmd->GetTexID();
|
||||||
|
SDL_RenderGeometryRaw8BitColor(renderer, bd->ColorBuffer, tex,
|
||||||
|
xy, (int)sizeof(ImDrawVert),
|
||||||
|
color, (int)sizeof(ImDrawVert),
|
||||||
|
uv, (int)sizeof(ImDrawVert),
|
||||||
|
draw_list->VtxBuffer.Size - pcmd->VtxOffset,
|
||||||
|
idx_buffer + pcmd->IdxOffset, pcmd->ElemCount, sizeof(ImDrawIdx));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
platform_io.Renderer_RenderState = nullptr;
|
||||||
|
|
||||||
|
// Restore modified SDL_Renderer state
|
||||||
|
SDL_SetRenderViewport(renderer, old.ViewportEnabled ? &old.Viewport : nullptr);
|
||||||
|
SDL_SetRenderClipRect(renderer, old.ClipEnabled ? &old.ClipRect : nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer3_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
|
||||||
|
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == 0 && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
|
||||||
|
// Create texture
|
||||||
|
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
|
||||||
|
SDL_Texture* sdl_texture = SDL_CreateTexture(bd->Renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(sdl_texture != nullptr && "Backend failed to create texture!");
|
||||||
|
SDL_UpdateTexture(sdl_texture, nullptr, tex->GetPixels(), tex->GetPitch());
|
||||||
|
SDL_SetTextureBlendMode(sdl_texture, SDL_BLENDMODE_BLEND);
|
||||||
|
SDL_SetTextureScaleMode(sdl_texture, SDL_SCALEMODE_LINEAR);
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)(intptr_t)sdl_texture);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
// Update selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
|
||||||
|
SDL_Texture* sdl_texture = (SDL_Texture*)(intptr_t)tex->TexID;
|
||||||
|
for (ImTextureRect& r : tex->Updates)
|
||||||
|
{
|
||||||
|
SDL_Rect sdl_r = { r.x, r.y, r.w, r.h };
|
||||||
|
SDL_UpdateTexture(sdl_texture, &sdl_r, tex->GetPixelsAt(r.x, r.y), tex->GetPitch());
|
||||||
|
}
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
else if (tex->Status == ImTextureStatus_WantDestroy)
|
||||||
|
{
|
||||||
|
SDL_Texture* sdl_texture = (SDL_Texture*)(intptr_t)tex->TexID;
|
||||||
|
if (sdl_texture == nullptr)
|
||||||
|
return;
|
||||||
|
SDL_DestroyTexture(sdl_texture);
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer3_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSDLRenderer3_DestroyDeviceObjects()
|
||||||
|
{
|
||||||
|
// Destroy all textures
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
{
|
||||||
|
tex->SetStatus(ImTextureStatus_WantDestroy);
|
||||||
|
ImGui_ImplSDLRenderer3_UpdateTexture(tex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
53
imgui/backends/imgui_impl_sdlrenderer3.h
Normal file
53
imgui/backends/imgui_impl_sdlrenderer3.h
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
// dear imgui: Renderer Backend for SDL_Renderer for SDL3
|
||||||
|
// (Requires: SDL 3.1.8+)
|
||||||
|
|
||||||
|
// Note that SDL_Renderer is an _optional_ component of SDL3, which IMHO is now largely obsolete.
|
||||||
|
// For a multi-platform app consider using other technologies:
|
||||||
|
// - SDL3+SDL_GPU: SDL_GPU is SDL3 new graphics abstraction API.
|
||||||
|
// - SDL3+DirectX, SDL3+OpenGL, SDL3+Vulkan: combine SDL with dedicated renderers.
|
||||||
|
// If your application wants to render any non trivial amount of graphics other than UI,
|
||||||
|
// please be aware that SDL_Renderer currently offers a limited graphic API to the end-user
|
||||||
|
// and it might be difficult to step out of those boundaries.
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
|
||||||
|
// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
struct SDL_Renderer;
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplSDLRenderer3_Init(SDL_Renderer* renderer);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer);
|
||||||
|
|
||||||
|
// Called by Init/NewFrame/Shutdown
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_DestroyDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
// [BETA] Selected render state data shared with callbacks.
|
||||||
|
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplSDLRenderer3_RenderDrawData() call.
|
||||||
|
// (Please open an issue if you feel you need access to more data)
|
||||||
|
struct ImGui_ImplSDLRenderer3_RenderState
|
||||||
|
{
|
||||||
|
SDL_Renderer* Renderer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
1849
imgui/backends/imgui_impl_vulkan.cpp
Normal file
1849
imgui/backends/imgui_impl_vulkan.cpp
Normal file
File diff suppressed because it is too large
Load Diff
223
imgui/backends/imgui_impl_vulkan.h
Normal file
223
imgui/backends/imgui_impl_vulkan.h
Normal file
@@ -0,0 +1,223 @@
|
|||||||
|
// dear imgui: Renderer Backend for Vulkan
|
||||||
|
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [!] Renderer: User texture binding. Use 'VkDescriptorSet' as texture identifier. Call ImGui_ImplVulkan_AddTexture() to register one. Read the FAQ about ImTextureID/ImTextureRef + https://github.com/ocornut/imgui/pull/914 for discussions.
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
|
||||||
|
// The aim of imgui_impl_vulkan.h/.cpp is to be usable in your engine without any modification.
|
||||||
|
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app.
|
||||||
|
// - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h.
|
||||||
|
// You will use those if you want to use this rendering backend in your engine/app.
|
||||||
|
// - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by
|
||||||
|
// the backend itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code.
|
||||||
|
// Read comments in imgui_impl_vulkan.h.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
|
||||||
|
// [Configuration] in order to use a custom Vulkan function loader:
|
||||||
|
// (1) You'll need to disable default Vulkan function prototypes.
|
||||||
|
// We provide a '#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES' convenience configuration flag.
|
||||||
|
// In order to make sure this is visible from the imgui_impl_vulkan.cpp compilation unit:
|
||||||
|
// - Add '#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES' in your imconfig.h file
|
||||||
|
// - Or as a compilation flag in your build system
|
||||||
|
// - Or uncomment here (not recommended because you'd be modifying imgui sources!)
|
||||||
|
// - Do not simply add it in a .cpp file!
|
||||||
|
// (2) Call ImGui_ImplVulkan_LoadFunctions() before ImGui_ImplVulkan_Init() with your custom function.
|
||||||
|
// If you have no idea what this is, leave it alone!
|
||||||
|
//#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES
|
||||||
|
|
||||||
|
// Convenience support for Volk
|
||||||
|
// (you can also technically use IMGUI_IMPL_VULKAN_NO_PROTOTYPES + wrap Volk via ImGui_ImplVulkan_LoadFunctions().)
|
||||||
|
//#define IMGUI_IMPL_VULKAN_USE_VOLK
|
||||||
|
|
||||||
|
#if defined(IMGUI_IMPL_VULKAN_NO_PROTOTYPES) && !defined(VK_NO_PROTOTYPES)
|
||||||
|
#define VK_NO_PROTOTYPES
|
||||||
|
#endif
|
||||||
|
#if defined(VK_USE_PLATFORM_WIN32_KHR) && !defined(NOMINMAX)
|
||||||
|
#define NOMINMAX
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Vulkan includes
|
||||||
|
#ifdef IMGUI_IMPL_VULKAN_USE_VOLK
|
||||||
|
#include <volk.h>
|
||||||
|
#else
|
||||||
|
#include <vulkan/vulkan.h>
|
||||||
|
#endif
|
||||||
|
#if defined(VK_VERSION_1_3) || defined(VK_KHR_dynamic_rendering)
|
||||||
|
#define IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Backend uses a small number of descriptors per font atlas + as many as additional calls done to ImGui_ImplVulkan_AddTexture().
|
||||||
|
#define IMGUI_IMPL_VULKAN_MINIMUM_IMAGE_SAMPLER_POOL_SIZE (8) // Minimum per atlas
|
||||||
|
|
||||||
|
// Initialization data, for ImGui_ImplVulkan_Init()
|
||||||
|
// [Please zero-clear before use!]
|
||||||
|
// - About descriptor pool:
|
||||||
|
// - A VkDescriptorPool should be created with VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
|
||||||
|
// and must contain a pool size large enough to hold a small number of VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER descriptors.
|
||||||
|
// - As an convenience, by setting DescriptorPoolSize > 0 the backend will create one for you.
|
||||||
|
// - About dynamic rendering:
|
||||||
|
// - When using dynamic rendering, set UseDynamicRendering=true and fill PipelineRenderingCreateInfo structure.
|
||||||
|
struct ImGui_ImplVulkan_InitInfo
|
||||||
|
{
|
||||||
|
uint32_t ApiVersion; // Fill with API version of Instance, e.g. VK_API_VERSION_1_3 or your value of VkApplicationInfo::apiVersion. May be lower than header version (VK_HEADER_VERSION_COMPLETE)
|
||||||
|
VkInstance Instance;
|
||||||
|
VkPhysicalDevice PhysicalDevice;
|
||||||
|
VkDevice Device;
|
||||||
|
uint32_t QueueFamily;
|
||||||
|
VkQueue Queue;
|
||||||
|
VkDescriptorPool DescriptorPool; // See requirements in note above; ignored if using DescriptorPoolSize > 0
|
||||||
|
VkRenderPass RenderPass; // Ignored if using dynamic rendering
|
||||||
|
uint32_t MinImageCount; // >= 2
|
||||||
|
uint32_t ImageCount; // >= MinImageCount
|
||||||
|
VkSampleCountFlagBits MSAASamples; // 0 defaults to VK_SAMPLE_COUNT_1_BIT
|
||||||
|
|
||||||
|
// (Optional)
|
||||||
|
VkPipelineCache PipelineCache;
|
||||||
|
uint32_t Subpass;
|
||||||
|
|
||||||
|
// (Optional) Set to create internal descriptor pool instead of using DescriptorPool
|
||||||
|
uint32_t DescriptorPoolSize;
|
||||||
|
|
||||||
|
// (Optional) Dynamic Rendering
|
||||||
|
// Need to explicitly enable VK_KHR_dynamic_rendering extension to use this, even for Vulkan 1.3.
|
||||||
|
bool UseDynamicRendering;
|
||||||
|
#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
|
||||||
|
VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// (Optional) Allocation, Debugging
|
||||||
|
const VkAllocationCallbacks* Allocator;
|
||||||
|
void (*CheckVkResultFn)(VkResult err);
|
||||||
|
VkDeviceSize MinAllocationSize; // Minimum allocation size. Set to 1024*1024 to satisfy zealous best practices validation layer and waste a little memory.
|
||||||
|
};
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, VkPipeline pipeline = VK_NULL_HANDLE);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated)
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplVulkan_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
// Register a texture (VkDescriptorSet == ImTextureID)
|
||||||
|
// FIXME: This is experimental in the sense that we are unsure how to best design/tackle this problem
|
||||||
|
// Please post to https://github.com/ocornut/imgui/pull/914 if you have suggestions.
|
||||||
|
IMGUI_IMPL_API VkDescriptorSet ImGui_ImplVulkan_AddTexture(VkSampler sampler, VkImageView image_view, VkImageLayout image_layout);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplVulkan_RemoveTexture(VkDescriptorSet descriptor_set);
|
||||||
|
|
||||||
|
// Optional: load Vulkan functions with a custom function loader
|
||||||
|
// This is only useful with IMGUI_IMPL_VULKAN_NO_PROTOTYPES / VK_NO_PROTOTYPES
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplVulkan_LoadFunctions(uint32_t api_version, PFN_vkVoidFunction(*loader_func)(const char* function_name, void* user_data), void* user_data = nullptr);
|
||||||
|
|
||||||
|
// [BETA] Selected render state data shared with callbacks.
|
||||||
|
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplVulkan_RenderDrawData() call.
|
||||||
|
// (Please open an issue if you feel you need access to more data)
|
||||||
|
struct ImGui_ImplVulkan_RenderState
|
||||||
|
{
|
||||||
|
VkCommandBuffer CommandBuffer;
|
||||||
|
VkPipeline Pipeline;
|
||||||
|
VkPipelineLayout PipelineLayout;
|
||||||
|
};
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
// Internal / Miscellaneous Vulkan Helpers
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
// Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own engine/app.
|
||||||
|
//
|
||||||
|
// You probably do NOT need to use or care about those functions.
|
||||||
|
// Those functions only exist because:
|
||||||
|
// 1) they facilitate the readability and maintenance of the multiple main.cpp examples files.
|
||||||
|
// 2) the multi-viewport / platform window implementation needs them internally.
|
||||||
|
// Generally we avoid exposing any kind of superfluous high-level helpers in the backends,
|
||||||
|
// but it is too much code to duplicate everywhere so we exceptionally expose them.
|
||||||
|
//
|
||||||
|
// Your engine/app will likely _already_ have code to setup all that stuff (swap chain,
|
||||||
|
// render pass, frame buffers, etc.). You may read this code if you are curious, but
|
||||||
|
// it is recommended you use you own custom tailored code to do equivalent work.
|
||||||
|
//
|
||||||
|
// We don't provide a strong guarantee that we won't change those functions API.
|
||||||
|
//
|
||||||
|
// The ImGui_ImplVulkanH_XXX functions should NOT interact with any of the state used
|
||||||
|
// by the regular ImGui_ImplVulkan_XXX functions).
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
struct ImGui_ImplVulkanH_Frame;
|
||||||
|
struct ImGui_ImplVulkanH_Window;
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateOrResizeWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wnd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wnd, const VkAllocationCallbacks* allocator);
|
||||||
|
IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space);
|
||||||
|
IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count);
|
||||||
|
IMGUI_IMPL_API VkPhysicalDevice ImGui_ImplVulkanH_SelectPhysicalDevice(VkInstance instance);
|
||||||
|
IMGUI_IMPL_API uint32_t ImGui_ImplVulkanH_SelectQueueFamilyIndex(VkPhysicalDevice physical_device);
|
||||||
|
IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode);
|
||||||
|
|
||||||
|
// Helper structure to hold the data needed by one rendering frame
|
||||||
|
// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
|
||||||
|
// [Please zero-clear before use!]
|
||||||
|
struct ImGui_ImplVulkanH_Frame
|
||||||
|
{
|
||||||
|
VkCommandPool CommandPool;
|
||||||
|
VkCommandBuffer CommandBuffer;
|
||||||
|
VkFence Fence;
|
||||||
|
VkImage Backbuffer;
|
||||||
|
VkImageView BackbufferView;
|
||||||
|
VkFramebuffer Framebuffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImGui_ImplVulkanH_FrameSemaphores
|
||||||
|
{
|
||||||
|
VkSemaphore ImageAcquiredSemaphore;
|
||||||
|
VkSemaphore RenderCompleteSemaphore;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Helper structure to hold the data needed by one rendering context into one OS window
|
||||||
|
// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
|
||||||
|
struct ImGui_ImplVulkanH_Window
|
||||||
|
{
|
||||||
|
int Width;
|
||||||
|
int Height;
|
||||||
|
VkSwapchainKHR Swapchain;
|
||||||
|
VkSurfaceKHR Surface;
|
||||||
|
VkSurfaceFormatKHR SurfaceFormat;
|
||||||
|
VkPresentModeKHR PresentMode;
|
||||||
|
VkRenderPass RenderPass;
|
||||||
|
VkPipeline Pipeline; // The window pipeline may uses a different VkRenderPass than the one passed in ImGui_ImplVulkan_InitInfo
|
||||||
|
bool UseDynamicRendering;
|
||||||
|
bool ClearEnable;
|
||||||
|
VkClearValue ClearValue;
|
||||||
|
uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount)
|
||||||
|
uint32_t ImageCount; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count)
|
||||||
|
uint32_t SemaphoreCount; // Number of simultaneous in-flight frames + 1, to be able to use it in vkAcquireNextImageKHR
|
||||||
|
uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data)
|
||||||
|
ImVector<ImGui_ImplVulkanH_Frame> Frames;
|
||||||
|
ImVector<ImGui_ImplVulkanH_FrameSemaphores> FrameSemaphores;
|
||||||
|
|
||||||
|
ImGui_ImplVulkanH_Window()
|
||||||
|
{
|
||||||
|
memset((void*)this, 0, sizeof(*this));
|
||||||
|
PresentMode = (VkPresentModeKHR)~0; // Ensure we get an error if user doesn't set this.
|
||||||
|
ClearEnable = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
910
imgui/backends/imgui_impl_wgpu.cpp
Normal file
910
imgui/backends/imgui_impl_wgpu.cpp
Normal file
@@ -0,0 +1,910 @@
|
|||||||
|
// dear imgui: Renderer for WebGPU
|
||||||
|
// This needs to be used along with a Platform Binding (e.g. GLFW)
|
||||||
|
// (Please note that WebGPU is currently experimental, will not run on non-beta browsers, and may break.)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'WGPUTextureView' as ImTextureID. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font system (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-06-12: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. (#8465)
|
||||||
|
// 2025-02-26: Recreate image bind groups during render. (#8426, #8046, #7765, #8027) + Update for latest webgpu-native changes.
|
||||||
|
// 2024-10-14: Update Dawn support for change of string usages. (#8082, #8083)
|
||||||
|
// 2024-10-07: Expose selected render state in ImGui_ImplWGPU_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
|
||||||
|
// 2024-10-07: Changed default texture sampler to Clamp instead of Repeat/Wrap.
|
||||||
|
// 2024-09-16: Added support for optional IMGUI_IMPL_WEBGPU_BACKEND_DAWN / IMGUI_IMPL_WEBGPU_BACKEND_WGPU define to handle ever-changing native implementations. (#7977)
|
||||||
|
// 2024-01-22: Added configurable PipelineMultisampleState struct. (#7240)
|
||||||
|
// 2024-01-22: (Breaking) ImGui_ImplWGPU_Init() now takes a ImGui_ImplWGPU_InitInfo structure instead of variety of parameters, allowing for easier further changes.
|
||||||
|
// 2024-01-22: Fixed pipeline layout leak. (#7245)
|
||||||
|
// 2024-01-17: Explicitly fill all of WGPUDepthStencilState since standard removed defaults.
|
||||||
|
// 2023-07-13: Use WGPUShaderModuleWGSLDescriptor's code instead of source. use WGPUMipmapFilterMode_Linear instead of WGPUFilterMode_Linear. (#6602)
|
||||||
|
// 2023-04-11: Align buffer sizes. Use WGSL shaders instead of precompiled SPIR-V.
|
||||||
|
// 2023-04-11: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
|
||||||
|
// 2023-01-25: Revert automatic pipeline layout generation (see https://github.com/gpuweb/gpuweb/issues/2470)
|
||||||
|
// 2022-11-24: Fixed validation error with default depth buffer settings.
|
||||||
|
// 2022-11-10: Fixed rendering when a depth buffer is enabled. Added 'WGPUTextureFormat depth_format' parameter to ImGui_ImplWGPU_Init().
|
||||||
|
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
|
||||||
|
// 2021-11-29: Passing explicit buffer sizes to wgpuRenderPassEncoderSetVertexBuffer()/wgpuRenderPassEncoderSetIndexBuffer().
|
||||||
|
// 2021-08-24: Fixed for latest specs.
|
||||||
|
// 2021-05-24: Add support for draw_data->FramebufferScale.
|
||||||
|
// 2021-05-19: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
|
||||||
|
// 2021-05-16: Update to latest WebGPU specs (compatible with Emscripten 2.0.20 and Chrome Canary 92).
|
||||||
|
// 2021-02-18: Change blending equation to preserve alpha in output buffer.
|
||||||
|
// 2021-01-28: Initial version.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
|
||||||
|
// When targeting native platforms (i.e. NOT emscripten), one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN
|
||||||
|
// or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided. See imgui_impl_wgpu.h for more details.
|
||||||
|
#ifndef __EMSCRIPTEN__
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) == defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
#error exactly one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be defined!
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
#error neither IMGUI_IMPL_WEBGPU_BACKEND_DAWN nor IMGUI_IMPL_WEBGPU_BACKEND_WGPU may be defined if targeting emscripten!
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_wgpu.h"
|
||||||
|
#include <limits.h>
|
||||||
|
#include <webgpu/webgpu.h>
|
||||||
|
|
||||||
|
#ifdef IMGUI_IMPL_WEBGPU_BACKEND_DAWN
|
||||||
|
// Dawn renamed WGPUProgrammableStageDescriptor to WGPUComputeState (see: https://github.com/webgpu-native/webgpu-headers/pull/413)
|
||||||
|
// Using type alias until WGPU adopts the same naming convention (#8369)
|
||||||
|
using WGPUProgrammableStageDescriptor = WGPUComputeState;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Dear ImGui prototypes from imgui_internal.h
|
||||||
|
extern ImGuiID ImHashData(const void* data_p, size_t data_size, ImU32 seed);
|
||||||
|
#define MEMALIGN(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1)) // Memory align (copied from IM_ALIGN() macro).
|
||||||
|
|
||||||
|
// WebGPU data
|
||||||
|
struct ImGui_ImplWGPU_Texture
|
||||||
|
{
|
||||||
|
WGPUTexture Texture = nullptr;
|
||||||
|
WGPUTextureView TextureView = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RenderResources
|
||||||
|
{
|
||||||
|
WGPUSampler Sampler = nullptr; // Sampler for textures
|
||||||
|
WGPUBuffer Uniforms = nullptr; // Shader uniforms
|
||||||
|
WGPUBindGroup CommonBindGroup = nullptr; // Resources bind-group to bind the common resources to pipeline
|
||||||
|
ImGuiStorage ImageBindGroups; // Resources bind-group to bind the font/image resources to pipeline (this is a key->value map)
|
||||||
|
WGPUBindGroupLayout ImageBindGroupLayout = nullptr; // Cache layout used for the image bind group. Avoids allocating unnecessary JS objects when working with WebASM
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FrameResources
|
||||||
|
{
|
||||||
|
WGPUBuffer IndexBuffer;
|
||||||
|
WGPUBuffer VertexBuffer;
|
||||||
|
ImDrawIdx* IndexBufferHost;
|
||||||
|
ImDrawVert* VertexBufferHost;
|
||||||
|
int IndexBufferSize;
|
||||||
|
int VertexBufferSize;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Uniforms
|
||||||
|
{
|
||||||
|
float MVP[4][4];
|
||||||
|
float Gamma;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImGui_ImplWGPU_Data
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_InitInfo initInfo;
|
||||||
|
WGPUDevice wgpuDevice = nullptr;
|
||||||
|
WGPUQueue defaultQueue = nullptr;
|
||||||
|
WGPUTextureFormat renderTargetFormat = WGPUTextureFormat_Undefined;
|
||||||
|
WGPUTextureFormat depthStencilFormat = WGPUTextureFormat_Undefined;
|
||||||
|
WGPURenderPipeline pipelineState = nullptr;
|
||||||
|
|
||||||
|
RenderResources renderResources;
|
||||||
|
FrameResources* pFrameResources = nullptr;
|
||||||
|
unsigned int numFramesInFlight = 0;
|
||||||
|
unsigned int frameIndex = UINT_MAX;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
static ImGui_ImplWGPU_Data* ImGui_ImplWGPU_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplWGPU_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// SHADERS
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static const char __shader_vert_wgsl[] = R"(
|
||||||
|
struct VertexInput {
|
||||||
|
@location(0) position: vec2<f32>,
|
||||||
|
@location(1) uv: vec2<f32>,
|
||||||
|
@location(2) color: vec4<f32>,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexOutput {
|
||||||
|
@builtin(position) position: vec4<f32>,
|
||||||
|
@location(0) color: vec4<f32>,
|
||||||
|
@location(1) uv: vec2<f32>,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Uniforms {
|
||||||
|
mvp: mat4x4<f32>,
|
||||||
|
gamma: f32,
|
||||||
|
};
|
||||||
|
|
||||||
|
@group(0) @binding(0) var<uniform> uniforms: Uniforms;
|
||||||
|
|
||||||
|
@vertex
|
||||||
|
fn main(in: VertexInput) -> VertexOutput {
|
||||||
|
var out: VertexOutput;
|
||||||
|
out.position = uniforms.mvp * vec4<f32>(in.position, 0.0, 1.0);
|
||||||
|
out.color = in.color;
|
||||||
|
out.uv = in.uv;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
static const char __shader_frag_wgsl[] = R"(
|
||||||
|
struct VertexOutput {
|
||||||
|
@builtin(position) position: vec4<f32>,
|
||||||
|
@location(0) color: vec4<f32>,
|
||||||
|
@location(1) uv: vec2<f32>,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Uniforms {
|
||||||
|
mvp: mat4x4<f32>,
|
||||||
|
gamma: f32,
|
||||||
|
};
|
||||||
|
|
||||||
|
@group(0) @binding(0) var<uniform> uniforms: Uniforms;
|
||||||
|
@group(0) @binding(1) var s: sampler;
|
||||||
|
@group(1) @binding(0) var t: texture_2d<f32>;
|
||||||
|
|
||||||
|
@fragment
|
||||||
|
fn main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||||
|
let color = in.color * textureSample(t, s, in.uv);
|
||||||
|
let corrected_color = pow(color.rgb, vec3<f32>(uniforms.gamma));
|
||||||
|
return vec4<f32>(corrected_color, color.a);
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
static void SafeRelease(ImDrawIdx*& res)
|
||||||
|
{
|
||||||
|
if (res)
|
||||||
|
delete[] res;
|
||||||
|
res = nullptr;
|
||||||
|
}
|
||||||
|
static void SafeRelease(ImDrawVert*& res)
|
||||||
|
{
|
||||||
|
if (res)
|
||||||
|
delete[] res;
|
||||||
|
res = nullptr;
|
||||||
|
}
|
||||||
|
static void SafeRelease(WGPUBindGroupLayout& res)
|
||||||
|
{
|
||||||
|
if (res)
|
||||||
|
wgpuBindGroupLayoutRelease(res);
|
||||||
|
res = nullptr;
|
||||||
|
}
|
||||||
|
static void SafeRelease(WGPUBindGroup& res)
|
||||||
|
{
|
||||||
|
if (res)
|
||||||
|
wgpuBindGroupRelease(res);
|
||||||
|
res = nullptr;
|
||||||
|
}
|
||||||
|
static void SafeRelease(WGPUBuffer& res)
|
||||||
|
{
|
||||||
|
if (res)
|
||||||
|
wgpuBufferRelease(res);
|
||||||
|
res = nullptr;
|
||||||
|
}
|
||||||
|
static void SafeRelease(WGPUPipelineLayout& res)
|
||||||
|
{
|
||||||
|
if (res)
|
||||||
|
wgpuPipelineLayoutRelease(res);
|
||||||
|
res = nullptr;
|
||||||
|
}
|
||||||
|
static void SafeRelease(WGPURenderPipeline& res)
|
||||||
|
{
|
||||||
|
if (res)
|
||||||
|
wgpuRenderPipelineRelease(res);
|
||||||
|
res = nullptr;
|
||||||
|
}
|
||||||
|
static void SafeRelease(WGPUSampler& res)
|
||||||
|
{
|
||||||
|
if (res)
|
||||||
|
wgpuSamplerRelease(res);
|
||||||
|
res = nullptr;
|
||||||
|
}
|
||||||
|
static void SafeRelease(WGPUShaderModule& res)
|
||||||
|
{
|
||||||
|
if (res)
|
||||||
|
wgpuShaderModuleRelease(res);
|
||||||
|
res = nullptr;
|
||||||
|
}
|
||||||
|
static void SafeRelease(RenderResources& res)
|
||||||
|
{
|
||||||
|
SafeRelease(res.Sampler);
|
||||||
|
SafeRelease(res.Uniforms);
|
||||||
|
SafeRelease(res.CommonBindGroup);
|
||||||
|
SafeRelease(res.ImageBindGroupLayout);
|
||||||
|
};
|
||||||
|
|
||||||
|
static void SafeRelease(FrameResources& res)
|
||||||
|
{
|
||||||
|
SafeRelease(res.IndexBuffer);
|
||||||
|
SafeRelease(res.VertexBuffer);
|
||||||
|
SafeRelease(res.IndexBufferHost);
|
||||||
|
SafeRelease(res.VertexBufferHost);
|
||||||
|
}
|
||||||
|
|
||||||
|
static WGPUProgrammableStageDescriptor ImGui_ImplWGPU_CreateShaderModule(const char* wgsl_source)
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
|
||||||
|
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
WGPUShaderSourceWGSL wgsl_desc = {};
|
||||||
|
wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
|
||||||
|
wgsl_desc.code = { wgsl_source, WGPU_STRLEN };
|
||||||
|
#else
|
||||||
|
WGPUShaderModuleWGSLDescriptor wgsl_desc = {};
|
||||||
|
wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
|
||||||
|
wgsl_desc.code = wgsl_source;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WGPUShaderModuleDescriptor desc = {};
|
||||||
|
desc.nextInChain = reinterpret_cast<WGPUChainedStruct*>(&wgsl_desc);
|
||||||
|
|
||||||
|
WGPUProgrammableStageDescriptor stage_desc = {};
|
||||||
|
stage_desc.module = wgpuDeviceCreateShaderModule(bd->wgpuDevice, &desc);
|
||||||
|
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
stage_desc.entryPoint = { "main", WGPU_STRLEN };
|
||||||
|
#else
|
||||||
|
stage_desc.entryPoint = "main";
|
||||||
|
#endif
|
||||||
|
return stage_desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
static WGPUBindGroup ImGui_ImplWGPU_CreateImageBindGroup(WGPUBindGroupLayout layout, WGPUTextureView texture)
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
|
||||||
|
WGPUBindGroupEntry image_bg_entries[] = { { nullptr, 0, 0, 0, 0, 0, texture } };
|
||||||
|
|
||||||
|
WGPUBindGroupDescriptor image_bg_descriptor = {};
|
||||||
|
image_bg_descriptor.layout = layout;
|
||||||
|
image_bg_descriptor.entryCount = sizeof(image_bg_entries) / sizeof(WGPUBindGroupEntry);
|
||||||
|
image_bg_descriptor.entries = image_bg_entries;
|
||||||
|
return wgpuDeviceCreateBindGroup(bd->wgpuDevice, &image_bg_descriptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplWGPU_SetupRenderState(ImDrawData* draw_data, WGPURenderPassEncoder ctx, FrameResources* fr)
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
|
||||||
|
|
||||||
|
// Setup orthographic projection matrix into our constant buffer
|
||||||
|
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
|
||||||
|
{
|
||||||
|
float L = draw_data->DisplayPos.x;
|
||||||
|
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
||||||
|
float T = draw_data->DisplayPos.y;
|
||||||
|
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
||||||
|
float mvp[4][4] =
|
||||||
|
{
|
||||||
|
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, 0.5f, 0.0f },
|
||||||
|
{ (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
|
||||||
|
};
|
||||||
|
wgpuQueueWriteBuffer(bd->defaultQueue, bd->renderResources.Uniforms, offsetof(Uniforms, MVP), mvp, sizeof(Uniforms::MVP));
|
||||||
|
float gamma;
|
||||||
|
switch (bd->renderTargetFormat)
|
||||||
|
{
|
||||||
|
case WGPUTextureFormat_ASTC10x10UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC10x5UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC10x6UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC10x8UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC12x10UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC12x12UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC4x4UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC5x5UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC6x5UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC6x6UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC8x5UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC8x6UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ASTC8x8UnormSrgb:
|
||||||
|
case WGPUTextureFormat_BC1RGBAUnormSrgb:
|
||||||
|
case WGPUTextureFormat_BC2RGBAUnormSrgb:
|
||||||
|
case WGPUTextureFormat_BC3RGBAUnormSrgb:
|
||||||
|
case WGPUTextureFormat_BC7RGBAUnormSrgb:
|
||||||
|
case WGPUTextureFormat_BGRA8UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ETC2RGB8A1UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ETC2RGB8UnormSrgb:
|
||||||
|
case WGPUTextureFormat_ETC2RGBA8UnormSrgb:
|
||||||
|
case WGPUTextureFormat_RGBA8UnormSrgb:
|
||||||
|
gamma = 2.2f;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
gamma = 1.0f;
|
||||||
|
}
|
||||||
|
wgpuQueueWriteBuffer(bd->defaultQueue, bd->renderResources.Uniforms, offsetof(Uniforms, Gamma), &gamma, sizeof(Uniforms::Gamma));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup viewport
|
||||||
|
wgpuRenderPassEncoderSetViewport(ctx, 0, 0, draw_data->FramebufferScale.x * draw_data->DisplaySize.x, draw_data->FramebufferScale.y * draw_data->DisplaySize.y, 0, 1);
|
||||||
|
|
||||||
|
// Bind shader and vertex buffers
|
||||||
|
wgpuRenderPassEncoderSetVertexBuffer(ctx, 0, fr->VertexBuffer, 0, fr->VertexBufferSize * sizeof(ImDrawVert));
|
||||||
|
wgpuRenderPassEncoderSetIndexBuffer(ctx, fr->IndexBuffer, sizeof(ImDrawIdx) == 2 ? WGPUIndexFormat_Uint16 : WGPUIndexFormat_Uint32, 0, fr->IndexBufferSize * sizeof(ImDrawIdx));
|
||||||
|
wgpuRenderPassEncoderSetPipeline(ctx, bd->pipelineState);
|
||||||
|
wgpuRenderPassEncoderSetBindGroup(ctx, 0, bd->renderResources.CommonBindGroup, 0, nullptr);
|
||||||
|
|
||||||
|
// Setup blend factor
|
||||||
|
WGPUColor blend_color = { 0.f, 0.f, 0.f, 0.f };
|
||||||
|
wgpuRenderPassEncoderSetBlendConstant(ctx, &blend_color);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render function
|
||||||
|
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
|
||||||
|
void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder pass_encoder)
|
||||||
|
{
|
||||||
|
// Avoid rendering when minimized
|
||||||
|
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
|
||||||
|
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
|
||||||
|
if (fb_width <= 0 || fb_height <= 0 || draw_data->CmdListsCount == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
|
||||||
|
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
|
||||||
|
if (draw_data->Textures != nullptr)
|
||||||
|
for (ImTextureData* tex : *draw_data->Textures)
|
||||||
|
if (tex->Status != ImTextureStatus_OK)
|
||||||
|
ImGui_ImplWGPU_UpdateTexture(tex);
|
||||||
|
|
||||||
|
// FIXME: Assuming that this only gets called once per frame!
|
||||||
|
// If not, we can't just re-allocate the IB or VB, we'll have to do a proper allocator.
|
||||||
|
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
|
||||||
|
bd->frameIndex = bd->frameIndex + 1;
|
||||||
|
FrameResources* fr = &bd->pFrameResources[bd->frameIndex % bd->numFramesInFlight];
|
||||||
|
|
||||||
|
// Create and grow vertex/index buffers if needed
|
||||||
|
if (fr->VertexBuffer == nullptr || fr->VertexBufferSize < draw_data->TotalVtxCount)
|
||||||
|
{
|
||||||
|
if (fr->VertexBuffer)
|
||||||
|
{
|
||||||
|
wgpuBufferDestroy(fr->VertexBuffer);
|
||||||
|
wgpuBufferRelease(fr->VertexBuffer);
|
||||||
|
}
|
||||||
|
SafeRelease(fr->VertexBufferHost);
|
||||||
|
fr->VertexBufferSize = draw_data->TotalVtxCount + 5000;
|
||||||
|
|
||||||
|
WGPUBufferDescriptor vb_desc =
|
||||||
|
{
|
||||||
|
nullptr,
|
||||||
|
"Dear ImGui Vertex buffer",
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
WGPU_STRLEN,
|
||||||
|
#endif
|
||||||
|
WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
|
||||||
|
MEMALIGN(fr->VertexBufferSize * sizeof(ImDrawVert), 4),
|
||||||
|
false
|
||||||
|
};
|
||||||
|
fr->VertexBuffer = wgpuDeviceCreateBuffer(bd->wgpuDevice, &vb_desc);
|
||||||
|
if (!fr->VertexBuffer)
|
||||||
|
return;
|
||||||
|
|
||||||
|
fr->VertexBufferHost = new ImDrawVert[fr->VertexBufferSize];
|
||||||
|
}
|
||||||
|
if (fr->IndexBuffer == nullptr || fr->IndexBufferSize < draw_data->TotalIdxCount)
|
||||||
|
{
|
||||||
|
if (fr->IndexBuffer)
|
||||||
|
{
|
||||||
|
wgpuBufferDestroy(fr->IndexBuffer);
|
||||||
|
wgpuBufferRelease(fr->IndexBuffer);
|
||||||
|
}
|
||||||
|
SafeRelease(fr->IndexBufferHost);
|
||||||
|
fr->IndexBufferSize = draw_data->TotalIdxCount + 10000;
|
||||||
|
|
||||||
|
WGPUBufferDescriptor ib_desc =
|
||||||
|
{
|
||||||
|
nullptr,
|
||||||
|
"Dear ImGui Index buffer",
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
WGPU_STRLEN,
|
||||||
|
#endif
|
||||||
|
WGPUBufferUsage_CopyDst | WGPUBufferUsage_Index,
|
||||||
|
MEMALIGN(fr->IndexBufferSize * sizeof(ImDrawIdx), 4),
|
||||||
|
false
|
||||||
|
};
|
||||||
|
fr->IndexBuffer = wgpuDeviceCreateBuffer(bd->wgpuDevice, &ib_desc);
|
||||||
|
if (!fr->IndexBuffer)
|
||||||
|
return;
|
||||||
|
|
||||||
|
fr->IndexBufferHost = new ImDrawIdx[fr->IndexBufferSize];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upload vertex/index data into a single contiguous GPU buffer
|
||||||
|
ImDrawVert* vtx_dst = (ImDrawVert*)fr->VertexBufferHost;
|
||||||
|
ImDrawIdx* idx_dst = (ImDrawIdx*)fr->IndexBufferHost;
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
|
||||||
|
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
|
||||||
|
vtx_dst += draw_list->VtxBuffer.Size;
|
||||||
|
idx_dst += draw_list->IdxBuffer.Size;
|
||||||
|
}
|
||||||
|
int64_t vb_write_size = MEMALIGN((char*)vtx_dst - (char*)fr->VertexBufferHost, 4);
|
||||||
|
int64_t ib_write_size = MEMALIGN((char*)idx_dst - (char*)fr->IndexBufferHost, 4);
|
||||||
|
wgpuQueueWriteBuffer(bd->defaultQueue, fr->VertexBuffer, 0, fr->VertexBufferHost, vb_write_size);
|
||||||
|
wgpuQueueWriteBuffer(bd->defaultQueue, fr->IndexBuffer, 0, fr->IndexBufferHost, ib_write_size);
|
||||||
|
|
||||||
|
// Setup desired render state
|
||||||
|
ImGui_ImplWGPU_SetupRenderState(draw_data, pass_encoder, fr);
|
||||||
|
|
||||||
|
// Setup render state structure (for callbacks and custom texture bindings)
|
||||||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||||||
|
ImGui_ImplWGPU_RenderState render_state;
|
||||||
|
render_state.Device = bd->wgpuDevice;
|
||||||
|
render_state.RenderPassEncoder = pass_encoder;
|
||||||
|
platform_io.Renderer_RenderState = &render_state;
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
// (Because we merged all buffers into a single one, we maintain our own offset into them)
|
||||||
|
int global_vtx_offset = 0;
|
||||||
|
int global_idx_offset = 0;
|
||||||
|
ImVec2 clip_scale = draw_data->FramebufferScale;
|
||||||
|
ImVec2 clip_off = draw_data->DisplayPos;
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||||
|
{
|
||||||
|
const ImDrawList* draw_list = draw_data->CmdLists[n];
|
||||||
|
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
|
||||||
|
{
|
||||||
|
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
|
||||||
|
if (pcmd->UserCallback != nullptr)
|
||||||
|
{
|
||||||
|
// User callback, registered via ImDrawList::AddCallback()
|
||||||
|
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||||
|
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||||
|
ImGui_ImplWGPU_SetupRenderState(draw_data, pass_encoder, fr);
|
||||||
|
else
|
||||||
|
pcmd->UserCallback(draw_list, pcmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Bind custom texture
|
||||||
|
ImTextureID tex_id = pcmd->GetTexID();
|
||||||
|
ImGuiID tex_id_hash = ImHashData(&tex_id, sizeof(tex_id), 0);
|
||||||
|
WGPUBindGroup bind_group = (WGPUBindGroup)bd->renderResources.ImageBindGroups.GetVoidPtr(tex_id_hash);
|
||||||
|
if (!bind_group)
|
||||||
|
{
|
||||||
|
bind_group = ImGui_ImplWGPU_CreateImageBindGroup(bd->renderResources.ImageBindGroupLayout, (WGPUTextureView)tex_id);
|
||||||
|
bd->renderResources.ImageBindGroups.SetVoidPtr(tex_id_hash, bind_group);
|
||||||
|
}
|
||||||
|
wgpuRenderPassEncoderSetBindGroup(pass_encoder, 1, (WGPUBindGroup)bind_group, 0, nullptr);
|
||||||
|
|
||||||
|
// Project scissor/clipping rectangles into framebuffer space
|
||||||
|
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
|
||||||
|
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
|
||||||
|
|
||||||
|
// Clamp to viewport as wgpuRenderPassEncoderSetScissorRect() won't accept values that are off bounds
|
||||||
|
if (clip_min.x < 0.0f) { clip_min.x = 0.0f; }
|
||||||
|
if (clip_min.y < 0.0f) { clip_min.y = 0.0f; }
|
||||||
|
if (clip_max.x > fb_width) { clip_max.x = (float)fb_width; }
|
||||||
|
if (clip_max.y > fb_height) { clip_max.y = (float)fb_height; }
|
||||||
|
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Apply scissor/clipping rectangle, Draw
|
||||||
|
wgpuRenderPassEncoderSetScissorRect(pass_encoder, (uint32_t)clip_min.x, (uint32_t)clip_min.y, (uint32_t)(clip_max.x - clip_min.x), (uint32_t)(clip_max.y - clip_min.y));
|
||||||
|
wgpuRenderPassEncoderDrawIndexed(pass_encoder, pcmd->ElemCount, 1, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
global_idx_offset += draw_list->IdxBuffer.Size;
|
||||||
|
global_vtx_offset += draw_list->VtxBuffer.Size;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove all ImageBindGroups
|
||||||
|
ImGuiStorage& image_bind_groups = bd->renderResources.ImageBindGroups;
|
||||||
|
for (int i = 0; i < image_bind_groups.Data.Size; i++)
|
||||||
|
{
|
||||||
|
WGPUBindGroup bind_group = (WGPUBindGroup)image_bind_groups.Data[i].val_p;
|
||||||
|
SafeRelease(bind_group);
|
||||||
|
}
|
||||||
|
image_bind_groups.Data.resize(0);
|
||||||
|
|
||||||
|
platform_io.Renderer_RenderState = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplWGPU_DestroyTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Texture* backend_tex = (ImGui_ImplWGPU_Texture*)tex->BackendUserData;
|
||||||
|
if (backend_tex == nullptr)
|
||||||
|
return;
|
||||||
|
|
||||||
|
IM_ASSERT(backend_tex->TextureView == (WGPUTextureView)(intptr_t)tex->TexID);
|
||||||
|
wgpuTextureViewRelease(backend_tex->TextureView);
|
||||||
|
wgpuTextureRelease(backend_tex->Texture);
|
||||||
|
IM_DELETE(backend_tex);
|
||||||
|
|
||||||
|
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
|
||||||
|
tex->SetTexID(ImTextureID_Invalid);
|
||||||
|
tex->SetStatus(ImTextureStatus_Destroyed);
|
||||||
|
tex->BackendUserData = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplWGPU_UpdateTexture(ImTextureData* tex)
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate)
|
||||||
|
{
|
||||||
|
// Create and upload new texture to graphics system
|
||||||
|
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
|
||||||
|
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
ImGui_ImplWGPU_Texture* backend_tex = IM_NEW(ImGui_ImplWGPU_Texture)();
|
||||||
|
|
||||||
|
// Create texture
|
||||||
|
WGPUTextureDescriptor tex_desc = {};
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
tex_desc.label = { "Dear ImGui Texture", WGPU_STRLEN };
|
||||||
|
#else
|
||||||
|
tex_desc.label = "Dear ImGui Texture";
|
||||||
|
#endif
|
||||||
|
tex_desc.dimension = WGPUTextureDimension_2D;
|
||||||
|
tex_desc.size.width = tex->Width;
|
||||||
|
tex_desc.size.height = tex->Height;
|
||||||
|
tex_desc.size.depthOrArrayLayers = 1;
|
||||||
|
tex_desc.sampleCount = 1;
|
||||||
|
tex_desc.format = WGPUTextureFormat_RGBA8Unorm;
|
||||||
|
tex_desc.mipLevelCount = 1;
|
||||||
|
tex_desc.usage = WGPUTextureUsage_CopyDst | WGPUTextureUsage_TextureBinding;
|
||||||
|
backend_tex->Texture = wgpuDeviceCreateTexture(bd->wgpuDevice, &tex_desc);
|
||||||
|
|
||||||
|
// Create texture view
|
||||||
|
WGPUTextureViewDescriptor tex_view_desc = {};
|
||||||
|
tex_view_desc.format = WGPUTextureFormat_RGBA8Unorm;
|
||||||
|
tex_view_desc.dimension = WGPUTextureViewDimension_2D;
|
||||||
|
tex_view_desc.baseMipLevel = 0;
|
||||||
|
tex_view_desc.mipLevelCount = 1;
|
||||||
|
tex_view_desc.baseArrayLayer = 0;
|
||||||
|
tex_view_desc.arrayLayerCount = 1;
|
||||||
|
tex_view_desc.aspect = WGPUTextureAspect_All;
|
||||||
|
backend_tex->TextureView = wgpuTextureCreateView(backend_tex->Texture, &tex_view_desc);
|
||||||
|
|
||||||
|
// Store identifiers
|
||||||
|
tex->SetTexID((ImTextureID)(intptr_t)backend_tex->TextureView);
|
||||||
|
tex->BackendUserData = backend_tex;
|
||||||
|
// We don't set tex->Status to ImTextureStatus_OK to let the code fallthrough below.
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates)
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Texture* backend_tex = (ImGui_ImplWGPU_Texture*)tex->BackendUserData;
|
||||||
|
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
|
||||||
|
|
||||||
|
// We could use the smaller rect on _WantCreate but using the full rect allows us to clear the texture.
|
||||||
|
const int upload_x = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.x;
|
||||||
|
const int upload_y = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.y;
|
||||||
|
const int upload_w = (tex->Status == ImTextureStatus_WantCreate) ? tex->Width : tex->UpdateRect.w;
|
||||||
|
const int upload_h = (tex->Status == ImTextureStatus_WantCreate) ? tex->Height : tex->UpdateRect.h;
|
||||||
|
|
||||||
|
// Update full texture or selected blocks. We only ever write to textures regions which have never been used before!
|
||||||
|
// This backend choose to use tex->UpdateRect but you can use tex->Updates[] to upload individual regions.
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
WGPUTexelCopyTextureInfo dst_view = {};
|
||||||
|
#else
|
||||||
|
WGPUImageCopyTexture dst_view = {};
|
||||||
|
#endif
|
||||||
|
dst_view.texture = backend_tex->Texture;
|
||||||
|
dst_view.mipLevel = 0;
|
||||||
|
dst_view.origin = { (uint32_t)upload_x, (uint32_t)upload_y, 0 };
|
||||||
|
dst_view.aspect = WGPUTextureAspect_All;
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
WGPUTexelCopyBufferLayout layout = {};
|
||||||
|
#else
|
||||||
|
WGPUTextureDataLayout layout = {};
|
||||||
|
#endif
|
||||||
|
layout.offset = 0;
|
||||||
|
layout.bytesPerRow = tex->Width * tex->BytesPerPixel;
|
||||||
|
layout.rowsPerImage = upload_h;
|
||||||
|
WGPUExtent3D write_size = { (uint32_t)upload_w, (uint32_t)upload_h, 1 };
|
||||||
|
wgpuQueueWriteTexture(bd->defaultQueue, &dst_view, tex->GetPixelsAt(upload_x, upload_y), (uint32_t)(tex->Width * upload_h * tex->BytesPerPixel), &layout, &write_size);
|
||||||
|
tex->SetStatus(ImTextureStatus_OK);
|
||||||
|
}
|
||||||
|
if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
|
||||||
|
ImGui_ImplWGPU_DestroyTexture(tex);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplWGPU_CreateUniformBuffer()
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
|
||||||
|
WGPUBufferDescriptor ub_desc =
|
||||||
|
{
|
||||||
|
nullptr,
|
||||||
|
"Dear ImGui Uniform buffer",
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
WGPU_STRLEN,
|
||||||
|
#endif
|
||||||
|
WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
|
||||||
|
MEMALIGN(sizeof(Uniforms), 16),
|
||||||
|
false
|
||||||
|
};
|
||||||
|
bd->renderResources.Uniforms = wgpuDeviceCreateBuffer(bd->wgpuDevice, &ub_desc);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplWGPU_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
|
||||||
|
if (!bd->wgpuDevice)
|
||||||
|
return false;
|
||||||
|
if (bd->pipelineState)
|
||||||
|
ImGui_ImplWGPU_InvalidateDeviceObjects();
|
||||||
|
|
||||||
|
// Create render pipeline
|
||||||
|
WGPURenderPipelineDescriptor graphics_pipeline_desc = {};
|
||||||
|
graphics_pipeline_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList;
|
||||||
|
graphics_pipeline_desc.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
|
||||||
|
graphics_pipeline_desc.primitive.frontFace = WGPUFrontFace_CW;
|
||||||
|
graphics_pipeline_desc.primitive.cullMode = WGPUCullMode_None;
|
||||||
|
graphics_pipeline_desc.multisample = bd->initInfo.PipelineMultisampleState;
|
||||||
|
|
||||||
|
// Bind group layouts
|
||||||
|
WGPUBindGroupLayoutEntry common_bg_layout_entries[2] = {};
|
||||||
|
common_bg_layout_entries[0].binding = 0;
|
||||||
|
common_bg_layout_entries[0].visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment;
|
||||||
|
common_bg_layout_entries[0].buffer.type = WGPUBufferBindingType_Uniform;
|
||||||
|
common_bg_layout_entries[1].binding = 1;
|
||||||
|
common_bg_layout_entries[1].visibility = WGPUShaderStage_Fragment;
|
||||||
|
common_bg_layout_entries[1].sampler.type = WGPUSamplerBindingType_Filtering;
|
||||||
|
|
||||||
|
WGPUBindGroupLayoutEntry image_bg_layout_entries[1] = {};
|
||||||
|
image_bg_layout_entries[0].binding = 0;
|
||||||
|
image_bg_layout_entries[0].visibility = WGPUShaderStage_Fragment;
|
||||||
|
image_bg_layout_entries[0].texture.sampleType = WGPUTextureSampleType_Float;
|
||||||
|
image_bg_layout_entries[0].texture.viewDimension = WGPUTextureViewDimension_2D;
|
||||||
|
|
||||||
|
WGPUBindGroupLayoutDescriptor common_bg_layout_desc = {};
|
||||||
|
common_bg_layout_desc.entryCount = 2;
|
||||||
|
common_bg_layout_desc.entries = common_bg_layout_entries;
|
||||||
|
|
||||||
|
WGPUBindGroupLayoutDescriptor image_bg_layout_desc = {};
|
||||||
|
image_bg_layout_desc.entryCount = 1;
|
||||||
|
image_bg_layout_desc.entries = image_bg_layout_entries;
|
||||||
|
|
||||||
|
WGPUBindGroupLayout bg_layouts[2];
|
||||||
|
bg_layouts[0] = wgpuDeviceCreateBindGroupLayout(bd->wgpuDevice, &common_bg_layout_desc);
|
||||||
|
bg_layouts[1] = wgpuDeviceCreateBindGroupLayout(bd->wgpuDevice, &image_bg_layout_desc);
|
||||||
|
|
||||||
|
WGPUPipelineLayoutDescriptor layout_desc = {};
|
||||||
|
layout_desc.bindGroupLayoutCount = 2;
|
||||||
|
layout_desc.bindGroupLayouts = bg_layouts;
|
||||||
|
graphics_pipeline_desc.layout = wgpuDeviceCreatePipelineLayout(bd->wgpuDevice, &layout_desc);
|
||||||
|
|
||||||
|
// Create the vertex shader
|
||||||
|
WGPUProgrammableStageDescriptor vertex_shader_desc = ImGui_ImplWGPU_CreateShaderModule(__shader_vert_wgsl);
|
||||||
|
graphics_pipeline_desc.vertex.module = vertex_shader_desc.module;
|
||||||
|
graphics_pipeline_desc.vertex.entryPoint = vertex_shader_desc.entryPoint;
|
||||||
|
|
||||||
|
// Vertex input configuration
|
||||||
|
WGPUVertexAttribute attribute_desc[] =
|
||||||
|
{
|
||||||
|
#ifdef IMGUI_IMPL_WEBGPU_BACKEND_DAWN
|
||||||
|
{ nullptr, WGPUVertexFormat_Float32x2, (uint64_t)offsetof(ImDrawVert, pos), 0 },
|
||||||
|
{ nullptr, WGPUVertexFormat_Float32x2, (uint64_t)offsetof(ImDrawVert, uv), 1 },
|
||||||
|
{ nullptr, WGPUVertexFormat_Unorm8x4, (uint64_t)offsetof(ImDrawVert, col), 2 },
|
||||||
|
#else
|
||||||
|
{ WGPUVertexFormat_Float32x2, (uint64_t)offsetof(ImDrawVert, pos), 0 },
|
||||||
|
{ WGPUVertexFormat_Float32x2, (uint64_t)offsetof(ImDrawVert, uv), 1 },
|
||||||
|
{ WGPUVertexFormat_Unorm8x4, (uint64_t)offsetof(ImDrawVert, col), 2 },
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
WGPUVertexBufferLayout buffer_layouts[1];
|
||||||
|
buffer_layouts[0].arrayStride = sizeof(ImDrawVert);
|
||||||
|
buffer_layouts[0].stepMode = WGPUVertexStepMode_Vertex;
|
||||||
|
buffer_layouts[0].attributeCount = 3;
|
||||||
|
buffer_layouts[0].attributes = attribute_desc;
|
||||||
|
|
||||||
|
graphics_pipeline_desc.vertex.bufferCount = 1;
|
||||||
|
graphics_pipeline_desc.vertex.buffers = buffer_layouts;
|
||||||
|
|
||||||
|
// Create the pixel shader
|
||||||
|
WGPUProgrammableStageDescriptor pixel_shader_desc = ImGui_ImplWGPU_CreateShaderModule(__shader_frag_wgsl);
|
||||||
|
|
||||||
|
// Create the blending setup
|
||||||
|
WGPUBlendState blend_state = {};
|
||||||
|
blend_state.alpha.operation = WGPUBlendOperation_Add;
|
||||||
|
blend_state.alpha.srcFactor = WGPUBlendFactor_One;
|
||||||
|
blend_state.alpha.dstFactor = WGPUBlendFactor_OneMinusSrcAlpha;
|
||||||
|
blend_state.color.operation = WGPUBlendOperation_Add;
|
||||||
|
blend_state.color.srcFactor = WGPUBlendFactor_SrcAlpha;
|
||||||
|
blend_state.color.dstFactor = WGPUBlendFactor_OneMinusSrcAlpha;
|
||||||
|
|
||||||
|
WGPUColorTargetState color_state = {};
|
||||||
|
color_state.format = bd->renderTargetFormat;
|
||||||
|
color_state.blend = &blend_state;
|
||||||
|
color_state.writeMask = WGPUColorWriteMask_All;
|
||||||
|
|
||||||
|
WGPUFragmentState fragment_state = {};
|
||||||
|
fragment_state.module = pixel_shader_desc.module;
|
||||||
|
fragment_state.entryPoint = pixel_shader_desc.entryPoint;
|
||||||
|
fragment_state.targetCount = 1;
|
||||||
|
fragment_state.targets = &color_state;
|
||||||
|
|
||||||
|
graphics_pipeline_desc.fragment = &fragment_state;
|
||||||
|
|
||||||
|
// Create depth-stencil State
|
||||||
|
WGPUDepthStencilState depth_stencil_state = {};
|
||||||
|
depth_stencil_state.format = bd->depthStencilFormat;
|
||||||
|
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
depth_stencil_state.depthWriteEnabled = WGPUOptionalBool_False;
|
||||||
|
#else
|
||||||
|
depth_stencil_state.depthWriteEnabled = false;
|
||||||
|
#endif
|
||||||
|
depth_stencil_state.depthCompare = WGPUCompareFunction_Always;
|
||||||
|
depth_stencil_state.stencilFront.compare = WGPUCompareFunction_Always;
|
||||||
|
depth_stencil_state.stencilFront.failOp = WGPUStencilOperation_Keep;
|
||||||
|
depth_stencil_state.stencilFront.depthFailOp = WGPUStencilOperation_Keep;
|
||||||
|
depth_stencil_state.stencilFront.passOp = WGPUStencilOperation_Keep;
|
||||||
|
depth_stencil_state.stencilBack.compare = WGPUCompareFunction_Always;
|
||||||
|
depth_stencil_state.stencilBack.failOp = WGPUStencilOperation_Keep;
|
||||||
|
depth_stencil_state.stencilBack.depthFailOp = WGPUStencilOperation_Keep;
|
||||||
|
depth_stencil_state.stencilBack.passOp = WGPUStencilOperation_Keep;
|
||||||
|
|
||||||
|
// Configure disabled depth-stencil state
|
||||||
|
graphics_pipeline_desc.depthStencil = (bd->depthStencilFormat == WGPUTextureFormat_Undefined) ? nullptr : &depth_stencil_state;
|
||||||
|
|
||||||
|
bd->pipelineState = wgpuDeviceCreateRenderPipeline(bd->wgpuDevice, &graphics_pipeline_desc);
|
||||||
|
|
||||||
|
ImGui_ImplWGPU_CreateUniformBuffer();
|
||||||
|
|
||||||
|
// Create sampler
|
||||||
|
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
|
||||||
|
WGPUSamplerDescriptor sampler_desc = {};
|
||||||
|
sampler_desc.minFilter = WGPUFilterMode_Linear;
|
||||||
|
sampler_desc.magFilter = WGPUFilterMode_Linear;
|
||||||
|
sampler_desc.mipmapFilter = WGPUMipmapFilterMode_Linear;
|
||||||
|
sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge;
|
||||||
|
sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge;
|
||||||
|
sampler_desc.addressModeW = WGPUAddressMode_ClampToEdge;
|
||||||
|
sampler_desc.maxAnisotropy = 1;
|
||||||
|
bd->renderResources.Sampler = wgpuDeviceCreateSampler(bd->wgpuDevice, &sampler_desc);
|
||||||
|
|
||||||
|
// Create resource bind group
|
||||||
|
WGPUBindGroupEntry common_bg_entries[] =
|
||||||
|
{
|
||||||
|
{ nullptr, 0, bd->renderResources.Uniforms, 0, MEMALIGN(sizeof(Uniforms), 16), 0, 0 },
|
||||||
|
{ nullptr, 1, 0, 0, 0, bd->renderResources.Sampler, 0 },
|
||||||
|
};
|
||||||
|
WGPUBindGroupDescriptor common_bg_descriptor = {};
|
||||||
|
common_bg_descriptor.layout = bg_layouts[0];
|
||||||
|
common_bg_descriptor.entryCount = sizeof(common_bg_entries) / sizeof(WGPUBindGroupEntry);
|
||||||
|
common_bg_descriptor.entries = common_bg_entries;
|
||||||
|
bd->renderResources.CommonBindGroup = wgpuDeviceCreateBindGroup(bd->wgpuDevice, &common_bg_descriptor);
|
||||||
|
bd->renderResources.ImageBindGroupLayout = bg_layouts[1];
|
||||||
|
|
||||||
|
SafeRelease(vertex_shader_desc.module);
|
||||||
|
SafeRelease(pixel_shader_desc.module);
|
||||||
|
SafeRelease(graphics_pipeline_desc.layout);
|
||||||
|
SafeRelease(bg_layouts[0]);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplWGPU_InvalidateDeviceObjects()
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
|
||||||
|
if (!bd->wgpuDevice)
|
||||||
|
return;
|
||||||
|
|
||||||
|
SafeRelease(bd->pipelineState);
|
||||||
|
SafeRelease(bd->renderResources);
|
||||||
|
|
||||||
|
// Destroy all textures
|
||||||
|
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
|
||||||
|
if (tex->RefCount == 1)
|
||||||
|
ImGui_ImplWGPU_DestroyTexture(tex);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < bd->numFramesInFlight; i++)
|
||||||
|
SafeRelease(bd->pFrameResources[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplWGPU_Data* bd = IM_NEW(ImGui_ImplWGPU_Data)();
|
||||||
|
io.BackendRendererUserData = (void*)bd;
|
||||||
|
#if defined(__EMSCRIPTEN__)
|
||||||
|
io.BackendRendererName = "imgui_impl_webgpu_emscripten";
|
||||||
|
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||||
|
io.BackendRendererName = "imgui_impl_webgpu_dawn";
|
||||||
|
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||||
|
io.BackendRendererName = "imgui_impl_webgpu_wgpu";
|
||||||
|
#else
|
||||||
|
io.BackendRendererName = "imgui_impl_webgpu";
|
||||||
|
#endif
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
|
||||||
|
|
||||||
|
bd->initInfo = *init_info;
|
||||||
|
bd->wgpuDevice = init_info->Device;
|
||||||
|
bd->defaultQueue = wgpuDeviceGetQueue(bd->wgpuDevice);
|
||||||
|
bd->renderTargetFormat = init_info->RenderTargetFormat;
|
||||||
|
bd->depthStencilFormat = init_info->DepthStencilFormat;
|
||||||
|
bd->numFramesInFlight = init_info->NumFramesInFlight;
|
||||||
|
bd->frameIndex = UINT_MAX;
|
||||||
|
|
||||||
|
bd->renderResources.Sampler = nullptr;
|
||||||
|
bd->renderResources.Uniforms = nullptr;
|
||||||
|
bd->renderResources.CommonBindGroup = nullptr;
|
||||||
|
bd->renderResources.ImageBindGroups.Data.reserve(100);
|
||||||
|
bd->renderResources.ImageBindGroupLayout = nullptr;
|
||||||
|
|
||||||
|
// Create buffers with a default size (they will later be grown as needed)
|
||||||
|
bd->pFrameResources = new FrameResources[bd->numFramesInFlight];
|
||||||
|
for (unsigned int i = 0; i < bd->numFramesInFlight; i++)
|
||||||
|
{
|
||||||
|
FrameResources* fr = &bd->pFrameResources[i];
|
||||||
|
fr->IndexBuffer = nullptr;
|
||||||
|
fr->VertexBuffer = nullptr;
|
||||||
|
fr->IndexBufferHost = nullptr;
|
||||||
|
fr->VertexBufferHost = nullptr;
|
||||||
|
fr->IndexBufferSize = 10000;
|
||||||
|
fr->VertexBufferSize = 5000;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplWGPU_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
ImGui_ImplWGPU_InvalidateDeviceObjects();
|
||||||
|
delete[] bd->pFrameResources;
|
||||||
|
bd->pFrameResources = nullptr;
|
||||||
|
wgpuQueueRelease(bd->defaultQueue);
|
||||||
|
bd->wgpuDevice = nullptr;
|
||||||
|
bd->numFramesInFlight = 0;
|
||||||
|
bd->frameIndex = UINT_MAX;
|
||||||
|
|
||||||
|
io.BackendRendererName = nullptr;
|
||||||
|
io.BackendRendererUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplWGPU_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
|
||||||
|
if (!bd->pipelineState)
|
||||||
|
if (!ImGui_ImplWGPU_CreateDeviceObjects())
|
||||||
|
IM_ASSERT(0 && "ImGui_ImplWGPU_CreateDeviceObjects() failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
71
imgui/backends/imgui_impl_wgpu.h
Normal file
71
imgui/backends/imgui_impl_wgpu.h
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
// dear imgui: Renderer for WebGPU
|
||||||
|
// This needs to be used along with a Platform Binding (e.g. GLFW)
|
||||||
|
// (Please note that WebGPU is currently experimental, will not run on non-beta browsers, and may break.)
|
||||||
|
|
||||||
|
// Important note to dawn and/or wgpu users: when targeting native platforms (i.e. NOT emscripten),
|
||||||
|
// one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided.
|
||||||
|
// Add #define to your imconfig.h file, or as a compilation flag in your build system.
|
||||||
|
// This requirement will be removed once WebGPU stabilizes and backends converge on a unified interface.
|
||||||
|
//#define IMGUI_IMPL_WEBGPU_BACKEND_DAWN
|
||||||
|
//#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Renderer: User texture binding. Use 'WGPUTextureView' as ImTextureID. Read the FAQ about ImTextureID/ImTextureRef!
|
||||||
|
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
|
||||||
|
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
|
||||||
|
// [X] Renderer: Texture updates support for dynamic font system (ImGuiBackendFlags_RendererHasTextures).
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
#include <webgpu/webgpu.h>
|
||||||
|
|
||||||
|
// Initialization data, for ImGui_ImplWGPU_Init()
|
||||||
|
struct ImGui_ImplWGPU_InitInfo
|
||||||
|
{
|
||||||
|
WGPUDevice Device;
|
||||||
|
int NumFramesInFlight = 3;
|
||||||
|
WGPUTextureFormat RenderTargetFormat = WGPUTextureFormat_Undefined;
|
||||||
|
WGPUTextureFormat DepthStencilFormat = WGPUTextureFormat_Undefined;
|
||||||
|
WGPUMultisampleState PipelineMultisampleState = {};
|
||||||
|
|
||||||
|
ImGui_ImplWGPU_InitInfo()
|
||||||
|
{
|
||||||
|
PipelineMultisampleState.count = 1;
|
||||||
|
PipelineMultisampleState.mask = UINT32_MAX;
|
||||||
|
PipelineMultisampleState.alphaToCoverageEnabled = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplWGPU_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplWGPU_NewFrame();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder pass_encoder);
|
||||||
|
|
||||||
|
// Use if you want to reset your rendering device without losing Dear ImGui state.
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplWGPU_CreateDeviceObjects();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplWGPU_InvalidateDeviceObjects();
|
||||||
|
|
||||||
|
// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplWGPU_UpdateTexture(ImTextureData* tex);
|
||||||
|
|
||||||
|
// [BETA] Selected render state data shared with callbacks.
|
||||||
|
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplWGPU_RenderDrawData() call.
|
||||||
|
// (Please open an issue if you feel you need access to more data)
|
||||||
|
struct ImGui_ImplWGPU_RenderState
|
||||||
|
{
|
||||||
|
WGPUDevice Device;
|
||||||
|
WGPURenderPassEncoder RenderPassEncoder;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
969
imgui/backends/imgui_impl_win32.cpp
Normal file
969
imgui/backends/imgui_impl_win32.cpp
Normal file
@@ -0,0 +1,969 @@
|
|||||||
|
// dear imgui: Platform Backend for Windows (standard windows API for 32-bits AND 64-bits applications)
|
||||||
|
// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Clipboard support (for Win32 this is actually part of core dear imgui)
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen.
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy VK_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Gamepad support.
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
// Configuration flags to add in your imconfig file:
|
||||||
|
//#define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD // Disable gamepad support. This was meaningful before <1.81 but we now load XInput dynamically so the option is now less relevant.
|
||||||
|
|
||||||
|
// CHANGELOG
|
||||||
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
|
// 2025-04-30: Inputs: Fixed an issue where externally losing mouse capture (due to e.g. focus loss) would fail to claim it again the next subsequent click. (#8594)
|
||||||
|
// 2025-03-10: When dealing with OEM keys, use scancodes instead of translated keycodes to choose ImGuiKey values. (#7136, #7201, #7206, #7306, #7670, #7672, #8468)
|
||||||
|
// 2025-02-18: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support.
|
||||||
|
// 2024-07-08: Inputs: Fixed ImGuiMod_Super being mapped to VK_APPS instead of VK_LWIN||VK_RWIN. (#7768)
|
||||||
|
// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
|
||||||
|
// 2023-09-25: Inputs: Synthesize key-down event on key-up for VK_SNAPSHOT / ImGuiKey_PrintScreen as Windows doesn't emit it (same behavior as GLFW/SDL).
|
||||||
|
// 2023-09-07: Inputs: Added support for keyboard codepage conversion for when application is compiled in MBCS mode and using a non-Unicode window.
|
||||||
|
// 2023-04-19: Added ImGui_ImplWin32_InitForOpenGL() to facilitate combining raw Win32/Winapi with OpenGL. (#3218)
|
||||||
|
// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen/ImGuiMouseSource_Pen. (#2702)
|
||||||
|
// 2023-02-15: Inputs: Use WM_NCMOUSEMOVE / WM_NCMOUSELEAVE to track mouse position over non-client area (e.g. OS decorations) when app is not focused. (#6045, #6162)
|
||||||
|
// 2023-02-02: Inputs: Flipping WM_MOUSEHWHEEL (horizontal mouse-wheel) value to match other backends and offer consistent horizontal scrolling direction. (#4019, #6096, #1463)
|
||||||
|
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
|
||||||
|
// 2022-09-28: Inputs: Convert WM_CHAR values with MultiByteToWideChar() when window class was registered as MBCS (not Unicode).
|
||||||
|
// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
|
||||||
|
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
|
||||||
|
// 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
|
||||||
|
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
|
||||||
|
// 2022-01-17: Inputs: always update key mods next and before a key event (not in NewFrame) to fix input queue with very low framerates.
|
||||||
|
// 2022-01-12: Inputs: Update mouse inputs using WM_MOUSEMOVE/WM_MOUSELEAVE + fallback to provide it when focused but not hovered/captured. More standard and will allow us to pass it to future input queue API.
|
||||||
|
// 2022-01-12: Inputs: Maintain our own copy of MouseButtonsDown mask instead of using ImGui::IsAnyMouseDown() which will be obsoleted.
|
||||||
|
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
|
||||||
|
// 2021-12-16: Inputs: Fill VK_LCONTROL/VK_RCONTROL/VK_LSHIFT/VK_RSHIFT/VK_LMENU/VK_RMENU for completeness.
|
||||||
|
// 2021-08-17: Calling io.AddFocusEvent() on WM_SETFOCUS/WM_KILLFOCUS messages.
|
||||||
|
// 2021-08-02: Inputs: Fixed keyboard modifiers being reported when host window doesn't have focus.
|
||||||
|
// 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using TrackMouseEvent() to receive WM_MOUSELEAVE events).
|
||||||
|
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
|
||||||
|
// 2021-06-08: Fixed ImGui_ImplWin32_EnableDpiAwareness() and ImGui_ImplWin32_GetDpiScaleForMonitor() to handle Windows 8.1/10 features without a manifest (per-monitor DPI, and properly calls SetProcessDpiAwareness() on 8.1).
|
||||||
|
// 2021-03-23: Inputs: Clearing keyboard down array when losing focus (WM_KILLFOCUS).
|
||||||
|
// 2021-02-18: Added ImGui_ImplWin32_EnableAlphaCompositing(). Non Visual Studio users will need to link with dwmapi.lib (MinGW/gcc: use -ldwmapi).
|
||||||
|
// 2021-02-17: Fixed ImGui_ImplWin32_EnableDpiAwareness() attempting to get SetProcessDpiAwareness from shcore.dll on Windows 8 whereas it is only supported on Windows 8.1.
|
||||||
|
// 2021-01-25: Inputs: Dynamically loading XInput DLL.
|
||||||
|
// 2020-12-04: Misc: Fixed setting of io.DisplaySize to invalid/uninitialized data when after hwnd has been closed.
|
||||||
|
// 2020-03-03: Inputs: Calling AddInputCharacterUTF16() to support surrogate pairs leading to codepoint >= 0x10000 (for more complete CJK inputs)
|
||||||
|
// 2020-02-17: Added ImGui_ImplWin32_EnableDpiAwareness(), ImGui_ImplWin32_GetDpiScaleForHwnd(), ImGui_ImplWin32_GetDpiScaleForMonitor() helper functions.
|
||||||
|
// 2020-01-14: Inputs: Added support for #define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD/IMGUI_IMPL_WIN32_DISABLE_LINKING_XINPUT.
|
||||||
|
// 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
|
||||||
|
// 2019-05-11: Inputs: Don't filter value from WM_CHAR before calling AddInputCharacter().
|
||||||
|
// 2019-01-17: Misc: Using GetForegroundWindow()+IsChild() instead of GetActiveWindow() to be compatible with windows created in a different thread or parent.
|
||||||
|
// 2019-01-17: Inputs: Added support for mouse buttons 4 and 5 via WM_XBUTTON* messages.
|
||||||
|
// 2019-01-15: Inputs: Added support for XInput gamepads (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
|
||||||
|
// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
|
||||||
|
// 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
|
||||||
|
// 2018-06-10: Inputs: Fixed handling of mouse wheel messages to support fine position messages (typically sent by track-pads).
|
||||||
|
// 2018-06-08: Misc: Extracted imgui_impl_win32.cpp/.h away from the old combined DX9/DX10/DX11/DX12 examples.
|
||||||
|
// 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag.
|
||||||
|
// 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling).
|
||||||
|
// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
|
||||||
|
// 2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
|
||||||
|
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
||||||
|
// 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
|
||||||
|
// 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert.
|
||||||
|
// 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag.
|
||||||
|
// 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read.
|
||||||
|
// 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging.
|
||||||
|
// 2016-11-12: Inputs: Only call Win32 ::SetCursor(nullptr) when io.MouseDrawCursor is set.
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
#include "imgui_impl_win32.h"
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
#include <windowsx.h> // GET_X_LPARAM(), GET_Y_LPARAM()
|
||||||
|
#include <tchar.h>
|
||||||
|
#include <dwmapi.h>
|
||||||
|
|
||||||
|
// Using XInput for gamepad (will load DLL dynamically)
|
||||||
|
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
|
||||||
|
#include <xinput.h>
|
||||||
|
typedef DWORD(WINAPI* PFN_XInputGetCapabilities)(DWORD, DWORD, XINPUT_CAPABILITIES*);
|
||||||
|
typedef DWORD(WINAPI* PFN_XInputGetState)(DWORD, XINPUT_STATE*);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Clang/GCC warnings with -Weverything
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader)
|
||||||
|
#endif
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind
|
||||||
|
#pragma GCC diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct ImGui_ImplWin32_Data
|
||||||
|
{
|
||||||
|
HWND hWnd;
|
||||||
|
HWND MouseHwnd;
|
||||||
|
int MouseTrackedArea; // 0: not tracked, 1: client area, 2: non-client area
|
||||||
|
int MouseButtonsDown;
|
||||||
|
INT64 Time;
|
||||||
|
INT64 TicksPerSecond;
|
||||||
|
ImGuiMouseCursor LastMouseCursor;
|
||||||
|
UINT32 KeyboardCodePage;
|
||||||
|
|
||||||
|
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
|
||||||
|
bool HasGamepad;
|
||||||
|
bool WantUpdateHasGamepad;
|
||||||
|
HMODULE XInputDLL;
|
||||||
|
PFN_XInputGetCapabilities XInputGetCapabilities;
|
||||||
|
PFN_XInputGetState XInputGetState;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ImGui_ImplWin32_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
|
||||||
|
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||||
|
// FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
|
||||||
|
// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
|
||||||
|
static ImGui_ImplWin32_Data* ImGui_ImplWin32_GetBackendData()
|
||||||
|
{
|
||||||
|
return ImGui::GetCurrentContext() ? (ImGui_ImplWin32_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
|
||||||
|
}
|
||||||
|
static ImGui_ImplWin32_Data* ImGui_ImplWin32_GetBackendData(ImGuiIO& io)
|
||||||
|
{
|
||||||
|
return (ImGui_ImplWin32_Data*)io.BackendPlatformUserData;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
static void ImGui_ImplWin32_UpdateKeyboardCodePage(ImGuiIO& io)
|
||||||
|
{
|
||||||
|
// Retrieve keyboard code page, required for handling of non-Unicode Windows.
|
||||||
|
ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io);
|
||||||
|
HKL keyboard_layout = ::GetKeyboardLayout(0);
|
||||||
|
LCID keyboard_lcid = MAKELCID(HIWORD(keyboard_layout), SORT_DEFAULT);
|
||||||
|
if (::GetLocaleInfoA(keyboard_lcid, (LOCALE_RETURN_NUMBER | LOCALE_IDEFAULTANSICODEPAGE), (LPSTR)&bd->KeyboardCodePage, sizeof(bd->KeyboardCodePage)) == 0)
|
||||||
|
bd->KeyboardCodePage = CP_ACP; // Fallback to default ANSI code page when fails.
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ImGui_ImplWin32_InitEx(void* hwnd, bool platform_has_own_dc)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
|
||||||
|
|
||||||
|
INT64 perf_frequency, perf_counter;
|
||||||
|
if (!::QueryPerformanceFrequency((LARGE_INTEGER*)&perf_frequency))
|
||||||
|
return false;
|
||||||
|
if (!::QueryPerformanceCounter((LARGE_INTEGER*)&perf_counter))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Setup backend capabilities flags
|
||||||
|
ImGui_ImplWin32_Data* bd = IM_NEW(ImGui_ImplWin32_Data)();
|
||||||
|
io.BackendPlatformUserData = (void*)bd;
|
||||||
|
io.BackendPlatformName = "imgui_impl_win32";
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
|
||||||
|
|
||||||
|
bd->hWnd = (HWND)hwnd;
|
||||||
|
bd->TicksPerSecond = perf_frequency;
|
||||||
|
bd->Time = perf_counter;
|
||||||
|
bd->LastMouseCursor = ImGuiMouseCursor_COUNT;
|
||||||
|
ImGui_ImplWin32_UpdateKeyboardCodePage(io);
|
||||||
|
|
||||||
|
// Set platform dependent data in viewport
|
||||||
|
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
|
||||||
|
main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (void*)bd->hWnd;
|
||||||
|
IM_UNUSED(platform_has_own_dc); // Used in 'docking' branch
|
||||||
|
|
||||||
|
// Dynamically load XInput library
|
||||||
|
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
|
||||||
|
bd->WantUpdateHasGamepad = true;
|
||||||
|
const char* xinput_dll_names[] =
|
||||||
|
{
|
||||||
|
"xinput1_4.dll", // Windows 8+
|
||||||
|
"xinput1_3.dll", // DirectX SDK
|
||||||
|
"xinput9_1_0.dll", // Windows Vista, Windows 7
|
||||||
|
"xinput1_2.dll", // DirectX SDK
|
||||||
|
"xinput1_1.dll" // DirectX SDK
|
||||||
|
};
|
||||||
|
for (int n = 0; n < IM_ARRAYSIZE(xinput_dll_names); n++)
|
||||||
|
if (HMODULE dll = ::LoadLibraryA(xinput_dll_names[n]))
|
||||||
|
{
|
||||||
|
bd->XInputDLL = dll;
|
||||||
|
bd->XInputGetCapabilities = (PFN_XInputGetCapabilities)::GetProcAddress(dll, "XInputGetCapabilities");
|
||||||
|
bd->XInputGetState = (PFN_XInputGetState)::GetProcAddress(dll, "XInputGetState");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif // IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplWin32_Init(void* hwnd)
|
||||||
|
{
|
||||||
|
return ImGui_ImplWin32_InitEx(hwnd, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplWin32_InitForOpenGL(void* hwnd)
|
||||||
|
{
|
||||||
|
// OpenGL needs CS_OWNDC
|
||||||
|
return ImGui_ImplWin32_InitEx(hwnd, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplWin32_Shutdown()
|
||||||
|
{
|
||||||
|
ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Unload XInput library
|
||||||
|
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
|
||||||
|
if (bd->XInputDLL)
|
||||||
|
::FreeLibrary(bd->XInputDLL);
|
||||||
|
#endif // IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
|
||||||
|
|
||||||
|
io.BackendPlatformName = nullptr;
|
||||||
|
io.BackendPlatformUserData = nullptr;
|
||||||
|
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad);
|
||||||
|
IM_DELETE(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ImGui_ImplWin32_UpdateMouseCursor(ImGuiIO& io, ImGuiMouseCursor imgui_cursor)
|
||||||
|
{
|
||||||
|
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
|
||||||
|
{
|
||||||
|
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
|
||||||
|
::SetCursor(nullptr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Show OS mouse cursor
|
||||||
|
LPTSTR win32_cursor = IDC_ARROW;
|
||||||
|
switch (imgui_cursor)
|
||||||
|
{
|
||||||
|
case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break;
|
||||||
|
case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break;
|
||||||
|
case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break;
|
||||||
|
case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break;
|
||||||
|
case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break;
|
||||||
|
case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break;
|
||||||
|
case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break;
|
||||||
|
case ImGuiMouseCursor_Hand: win32_cursor = IDC_HAND; break;
|
||||||
|
case ImGuiMouseCursor_Wait: win32_cursor = IDC_WAIT; break;
|
||||||
|
case ImGuiMouseCursor_Progress: win32_cursor = IDC_APPSTARTING; break;
|
||||||
|
case ImGuiMouseCursor_NotAllowed: win32_cursor = IDC_NO; break;
|
||||||
|
}
|
||||||
|
::SetCursor(::LoadCursor(nullptr, win32_cursor));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool IsVkDown(int vk)
|
||||||
|
{
|
||||||
|
return (::GetKeyState(vk) & 0x8000) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplWin32_AddKeyEvent(ImGuiIO& io, ImGuiKey key, bool down, int native_keycode, int native_scancode = -1)
|
||||||
|
{
|
||||||
|
io.AddKeyEvent(key, down);
|
||||||
|
io.SetKeyEventNativeData(key, native_keycode, native_scancode); // To support legacy indexing (<1.87 user code)
|
||||||
|
IM_UNUSED(native_scancode);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplWin32_ProcessKeyEventsWorkarounds(ImGuiIO& io)
|
||||||
|
{
|
||||||
|
// Left & right Shift keys: when both are pressed together, Windows tend to not generate the WM_KEYUP event for the first released one.
|
||||||
|
if (ImGui::IsKeyDown(ImGuiKey_LeftShift) && !IsVkDown(VK_LSHIFT))
|
||||||
|
ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftShift, false, VK_LSHIFT);
|
||||||
|
if (ImGui::IsKeyDown(ImGuiKey_RightShift) && !IsVkDown(VK_RSHIFT))
|
||||||
|
ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightShift, false, VK_RSHIFT);
|
||||||
|
|
||||||
|
// Sometimes WM_KEYUP for Win key is not passed down to the app (e.g. for Win+V on some setups, according to GLFW).
|
||||||
|
if (ImGui::IsKeyDown(ImGuiKey_LeftSuper) && !IsVkDown(VK_LWIN))
|
||||||
|
ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftSuper, false, VK_LWIN);
|
||||||
|
if (ImGui::IsKeyDown(ImGuiKey_RightSuper) && !IsVkDown(VK_RWIN))
|
||||||
|
ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightSuper, false, VK_RWIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplWin32_UpdateKeyModifiers(ImGuiIO& io)
|
||||||
|
{
|
||||||
|
io.AddKeyEvent(ImGuiMod_Ctrl, IsVkDown(VK_CONTROL));
|
||||||
|
io.AddKeyEvent(ImGuiMod_Shift, IsVkDown(VK_SHIFT));
|
||||||
|
io.AddKeyEvent(ImGuiMod_Alt, IsVkDown(VK_MENU));
|
||||||
|
io.AddKeyEvent(ImGuiMod_Super, IsVkDown(VK_LWIN) || IsVkDown(VK_RWIN));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplWin32_UpdateMouseData(ImGuiIO& io)
|
||||||
|
{
|
||||||
|
ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io);
|
||||||
|
IM_ASSERT(bd->hWnd != 0);
|
||||||
|
|
||||||
|
HWND focused_window = ::GetForegroundWindow();
|
||||||
|
const bool is_app_focused = (focused_window == bd->hWnd);
|
||||||
|
if (is_app_focused)
|
||||||
|
{
|
||||||
|
// (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
|
||||||
|
if (io.WantSetMousePos)
|
||||||
|
{
|
||||||
|
POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
|
||||||
|
if (::ClientToScreen(bd->hWnd, &pos))
|
||||||
|
::SetCursorPos(pos.x, pos.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
// (Optional) Fallback to provide mouse position when focused (WM_MOUSEMOVE already provides this when hovered or captured)
|
||||||
|
// This also fills a short gap when clicking non-client area: WM_NCMOUSELEAVE -> modal OS move -> gap -> WM_NCMOUSEMOVE
|
||||||
|
if (!io.WantSetMousePos && bd->MouseTrackedArea == 0)
|
||||||
|
{
|
||||||
|
POINT pos;
|
||||||
|
if (::GetCursorPos(&pos) && ::ScreenToClient(bd->hWnd, &pos))
|
||||||
|
io.AddMousePosEvent((float)pos.x, (float)pos.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gamepad navigation mapping
|
||||||
|
static void ImGui_ImplWin32_UpdateGamepads(ImGuiIO& io)
|
||||||
|
{
|
||||||
|
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
|
||||||
|
ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io);
|
||||||
|
|
||||||
|
// Calling XInputGetState() every frame on disconnected gamepads is unfortunately too slow.
|
||||||
|
// Instead we refresh gamepad availability by calling XInputGetCapabilities() _only_ after receiving WM_DEVICECHANGE.
|
||||||
|
if (bd->WantUpdateHasGamepad)
|
||||||
|
{
|
||||||
|
XINPUT_CAPABILITIES caps = {};
|
||||||
|
bd->HasGamepad = bd->XInputGetCapabilities ? (bd->XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS) : false;
|
||||||
|
bd->WantUpdateHasGamepad = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
|
||||||
|
XINPUT_STATE xinput_state;
|
||||||
|
XINPUT_GAMEPAD& gamepad = xinput_state.Gamepad;
|
||||||
|
if (!bd->HasGamepad || bd->XInputGetState == nullptr || bd->XInputGetState(0, &xinput_state) != ERROR_SUCCESS)
|
||||||
|
return;
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
|
||||||
|
|
||||||
|
#define IM_SATURATE(V) (V < 0.0f ? 0.0f : V > 1.0f ? 1.0f : V)
|
||||||
|
#define MAP_BUTTON(KEY_NO, BUTTON_ENUM) { io.AddKeyEvent(KEY_NO, (gamepad.wButtons & BUTTON_ENUM) != 0); }
|
||||||
|
#define MAP_ANALOG(KEY_NO, VALUE, V0, V1) { float vn = (float)(VALUE - V0) / (float)(V1 - V0); io.AddKeyAnalogEvent(KEY_NO, vn > 0.10f, IM_SATURATE(vn)); }
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadStart, XINPUT_GAMEPAD_START);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadBack, XINPUT_GAMEPAD_BACK);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadFaceLeft, XINPUT_GAMEPAD_X);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadFaceRight, XINPUT_GAMEPAD_B);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadFaceUp, XINPUT_GAMEPAD_Y);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadFaceDown, XINPUT_GAMEPAD_A);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadDpadLeft, XINPUT_GAMEPAD_DPAD_LEFT);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadDpadRight, XINPUT_GAMEPAD_DPAD_RIGHT);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadDpadUp, XINPUT_GAMEPAD_DPAD_UP);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadDpadDown, XINPUT_GAMEPAD_DPAD_DOWN);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadL1, XINPUT_GAMEPAD_LEFT_SHOULDER);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadR1, XINPUT_GAMEPAD_RIGHT_SHOULDER);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadL2, gamepad.bLeftTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD, 255);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadR2, gamepad.bRightTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD, 255);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadL3, XINPUT_GAMEPAD_LEFT_THUMB);
|
||||||
|
MAP_BUTTON(ImGuiKey_GamepadR3, XINPUT_GAMEPAD_RIGHT_THUMB);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadLStickLeft, gamepad.sThumbLX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadLStickRight, gamepad.sThumbLX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadLStickUp, gamepad.sThumbLY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadLStickDown, gamepad.sThumbLY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadRStickLeft, gamepad.sThumbRX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadRStickRight, gamepad.sThumbRX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadRStickUp, gamepad.sThumbRY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
|
||||||
|
MAP_ANALOG(ImGuiKey_GamepadRStickDown, gamepad.sThumbRY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
|
||||||
|
#undef MAP_BUTTON
|
||||||
|
#undef MAP_ANALOG
|
||||||
|
#else // #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
|
||||||
|
IM_UNUSED(io);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplWin32_NewFrame()
|
||||||
|
{
|
||||||
|
ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData();
|
||||||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized? Did you call ImGui_ImplWin32_Init()?");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Setup display size (every frame to accommodate for window resizing)
|
||||||
|
RECT rect = { 0, 0, 0, 0 };
|
||||||
|
::GetClientRect(bd->hWnd, &rect);
|
||||||
|
io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
|
||||||
|
|
||||||
|
// Setup time step
|
||||||
|
INT64 current_time = 0;
|
||||||
|
::QueryPerformanceCounter((LARGE_INTEGER*)¤t_time);
|
||||||
|
io.DeltaTime = (float)(current_time - bd->Time) / bd->TicksPerSecond;
|
||||||
|
bd->Time = current_time;
|
||||||
|
|
||||||
|
// Update OS mouse position
|
||||||
|
ImGui_ImplWin32_UpdateMouseData(io);
|
||||||
|
|
||||||
|
// Process workarounds for known Windows key handling issues
|
||||||
|
ImGui_ImplWin32_ProcessKeyEventsWorkarounds(io);
|
||||||
|
|
||||||
|
// Update OS mouse cursor with the cursor requested by imgui
|
||||||
|
ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
|
||||||
|
if (bd->LastMouseCursor != mouse_cursor)
|
||||||
|
{
|
||||||
|
bd->LastMouseCursor = mouse_cursor;
|
||||||
|
ImGui_ImplWin32_UpdateMouseCursor(io, mouse_cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update game controllers (if enabled and available)
|
||||||
|
ImGui_ImplWin32_UpdateGamepads(io);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map VK_xxx to ImGuiKey_xxx.
|
||||||
|
// Not static to allow third-party code to use that if they want to (but undocumented)
|
||||||
|
ImGuiKey ImGui_ImplWin32_KeyEventToImGuiKey(WPARAM wParam, LPARAM lParam);
|
||||||
|
ImGuiKey ImGui_ImplWin32_KeyEventToImGuiKey(WPARAM wParam, LPARAM lParam)
|
||||||
|
{
|
||||||
|
// There is no distinct VK_xxx for keypad enter, instead it is VK_RETURN + KF_EXTENDED.
|
||||||
|
if ((wParam == VK_RETURN) && (HIWORD(lParam) & KF_EXTENDED))
|
||||||
|
return ImGuiKey_KeypadEnter;
|
||||||
|
|
||||||
|
const int scancode = (int)LOBYTE(HIWORD(lParam));
|
||||||
|
//IMGUI_DEBUG_LOG("scancode %3d, keycode = 0x%02X\n", scancode, wParam);
|
||||||
|
switch (wParam)
|
||||||
|
{
|
||||||
|
case VK_TAB: return ImGuiKey_Tab;
|
||||||
|
case VK_LEFT: return ImGuiKey_LeftArrow;
|
||||||
|
case VK_RIGHT: return ImGuiKey_RightArrow;
|
||||||
|
case VK_UP: return ImGuiKey_UpArrow;
|
||||||
|
case VK_DOWN: return ImGuiKey_DownArrow;
|
||||||
|
case VK_PRIOR: return ImGuiKey_PageUp;
|
||||||
|
case VK_NEXT: return ImGuiKey_PageDown;
|
||||||
|
case VK_HOME: return ImGuiKey_Home;
|
||||||
|
case VK_END: return ImGuiKey_End;
|
||||||
|
case VK_INSERT: return ImGuiKey_Insert;
|
||||||
|
case VK_DELETE: return ImGuiKey_Delete;
|
||||||
|
case VK_BACK: return ImGuiKey_Backspace;
|
||||||
|
case VK_SPACE: return ImGuiKey_Space;
|
||||||
|
case VK_RETURN: return ImGuiKey_Enter;
|
||||||
|
case VK_ESCAPE: return ImGuiKey_Escape;
|
||||||
|
//case VK_OEM_7: return ImGuiKey_Apostrophe;
|
||||||
|
case VK_OEM_COMMA: return ImGuiKey_Comma;
|
||||||
|
//case VK_OEM_MINUS: return ImGuiKey_Minus;
|
||||||
|
case VK_OEM_PERIOD: return ImGuiKey_Period;
|
||||||
|
//case VK_OEM_2: return ImGuiKey_Slash;
|
||||||
|
//case VK_OEM_1: return ImGuiKey_Semicolon;
|
||||||
|
//case VK_OEM_PLUS: return ImGuiKey_Equal;
|
||||||
|
//case VK_OEM_4: return ImGuiKey_LeftBracket;
|
||||||
|
//case VK_OEM_5: return ImGuiKey_Backslash;
|
||||||
|
//case VK_OEM_6: return ImGuiKey_RightBracket;
|
||||||
|
//case VK_OEM_3: return ImGuiKey_GraveAccent;
|
||||||
|
case VK_CAPITAL: return ImGuiKey_CapsLock;
|
||||||
|
case VK_SCROLL: return ImGuiKey_ScrollLock;
|
||||||
|
case VK_NUMLOCK: return ImGuiKey_NumLock;
|
||||||
|
case VK_SNAPSHOT: return ImGuiKey_PrintScreen;
|
||||||
|
case VK_PAUSE: return ImGuiKey_Pause;
|
||||||
|
case VK_NUMPAD0: return ImGuiKey_Keypad0;
|
||||||
|
case VK_NUMPAD1: return ImGuiKey_Keypad1;
|
||||||
|
case VK_NUMPAD2: return ImGuiKey_Keypad2;
|
||||||
|
case VK_NUMPAD3: return ImGuiKey_Keypad3;
|
||||||
|
case VK_NUMPAD4: return ImGuiKey_Keypad4;
|
||||||
|
case VK_NUMPAD5: return ImGuiKey_Keypad5;
|
||||||
|
case VK_NUMPAD6: return ImGuiKey_Keypad6;
|
||||||
|
case VK_NUMPAD7: return ImGuiKey_Keypad7;
|
||||||
|
case VK_NUMPAD8: return ImGuiKey_Keypad8;
|
||||||
|
case VK_NUMPAD9: return ImGuiKey_Keypad9;
|
||||||
|
case VK_DECIMAL: return ImGuiKey_KeypadDecimal;
|
||||||
|
case VK_DIVIDE: return ImGuiKey_KeypadDivide;
|
||||||
|
case VK_MULTIPLY: return ImGuiKey_KeypadMultiply;
|
||||||
|
case VK_SUBTRACT: return ImGuiKey_KeypadSubtract;
|
||||||
|
case VK_ADD: return ImGuiKey_KeypadAdd;
|
||||||
|
case VK_LSHIFT: return ImGuiKey_LeftShift;
|
||||||
|
case VK_LCONTROL: return ImGuiKey_LeftCtrl;
|
||||||
|
case VK_LMENU: return ImGuiKey_LeftAlt;
|
||||||
|
case VK_LWIN: return ImGuiKey_LeftSuper;
|
||||||
|
case VK_RSHIFT: return ImGuiKey_RightShift;
|
||||||
|
case VK_RCONTROL: return ImGuiKey_RightCtrl;
|
||||||
|
case VK_RMENU: return ImGuiKey_RightAlt;
|
||||||
|
case VK_RWIN: return ImGuiKey_RightSuper;
|
||||||
|
case VK_APPS: return ImGuiKey_Menu;
|
||||||
|
case '0': return ImGuiKey_0;
|
||||||
|
case '1': return ImGuiKey_1;
|
||||||
|
case '2': return ImGuiKey_2;
|
||||||
|
case '3': return ImGuiKey_3;
|
||||||
|
case '4': return ImGuiKey_4;
|
||||||
|
case '5': return ImGuiKey_5;
|
||||||
|
case '6': return ImGuiKey_6;
|
||||||
|
case '7': return ImGuiKey_7;
|
||||||
|
case '8': return ImGuiKey_8;
|
||||||
|
case '9': return ImGuiKey_9;
|
||||||
|
case 'A': return ImGuiKey_A;
|
||||||
|
case 'B': return ImGuiKey_B;
|
||||||
|
case 'C': return ImGuiKey_C;
|
||||||
|
case 'D': return ImGuiKey_D;
|
||||||
|
case 'E': return ImGuiKey_E;
|
||||||
|
case 'F': return ImGuiKey_F;
|
||||||
|
case 'G': return ImGuiKey_G;
|
||||||
|
case 'H': return ImGuiKey_H;
|
||||||
|
case 'I': return ImGuiKey_I;
|
||||||
|
case 'J': return ImGuiKey_J;
|
||||||
|
case 'K': return ImGuiKey_K;
|
||||||
|
case 'L': return ImGuiKey_L;
|
||||||
|
case 'M': return ImGuiKey_M;
|
||||||
|
case 'N': return ImGuiKey_N;
|
||||||
|
case 'O': return ImGuiKey_O;
|
||||||
|
case 'P': return ImGuiKey_P;
|
||||||
|
case 'Q': return ImGuiKey_Q;
|
||||||
|
case 'R': return ImGuiKey_R;
|
||||||
|
case 'S': return ImGuiKey_S;
|
||||||
|
case 'T': return ImGuiKey_T;
|
||||||
|
case 'U': return ImGuiKey_U;
|
||||||
|
case 'V': return ImGuiKey_V;
|
||||||
|
case 'W': return ImGuiKey_W;
|
||||||
|
case 'X': return ImGuiKey_X;
|
||||||
|
case 'Y': return ImGuiKey_Y;
|
||||||
|
case 'Z': return ImGuiKey_Z;
|
||||||
|
case VK_F1: return ImGuiKey_F1;
|
||||||
|
case VK_F2: return ImGuiKey_F2;
|
||||||
|
case VK_F3: return ImGuiKey_F3;
|
||||||
|
case VK_F4: return ImGuiKey_F4;
|
||||||
|
case VK_F5: return ImGuiKey_F5;
|
||||||
|
case VK_F6: return ImGuiKey_F6;
|
||||||
|
case VK_F7: return ImGuiKey_F7;
|
||||||
|
case VK_F8: return ImGuiKey_F8;
|
||||||
|
case VK_F9: return ImGuiKey_F9;
|
||||||
|
case VK_F10: return ImGuiKey_F10;
|
||||||
|
case VK_F11: return ImGuiKey_F11;
|
||||||
|
case VK_F12: return ImGuiKey_F12;
|
||||||
|
case VK_F13: return ImGuiKey_F13;
|
||||||
|
case VK_F14: return ImGuiKey_F14;
|
||||||
|
case VK_F15: return ImGuiKey_F15;
|
||||||
|
case VK_F16: return ImGuiKey_F16;
|
||||||
|
case VK_F17: return ImGuiKey_F17;
|
||||||
|
case VK_F18: return ImGuiKey_F18;
|
||||||
|
case VK_F19: return ImGuiKey_F19;
|
||||||
|
case VK_F20: return ImGuiKey_F20;
|
||||||
|
case VK_F21: return ImGuiKey_F21;
|
||||||
|
case VK_F22: return ImGuiKey_F22;
|
||||||
|
case VK_F23: return ImGuiKey_F23;
|
||||||
|
case VK_F24: return ImGuiKey_F24;
|
||||||
|
case VK_BROWSER_BACK: return ImGuiKey_AppBack;
|
||||||
|
case VK_BROWSER_FORWARD: return ImGuiKey_AppForward;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to scancode
|
||||||
|
// https://handmade.network/forums/t/2011-keyboard_inputs_-_scancodes,_raw_input,_text_input,_key_names
|
||||||
|
switch (scancode)
|
||||||
|
{
|
||||||
|
case 41: return ImGuiKey_GraveAccent; // VK_OEM_8 in EN-UK, VK_OEM_3 in EN-US, VK_OEM_7 in FR, VK_OEM_5 in DE, etc.
|
||||||
|
case 12: return ImGuiKey_Minus;
|
||||||
|
case 13: return ImGuiKey_Equal;
|
||||||
|
case 26: return ImGuiKey_LeftBracket;
|
||||||
|
case 27: return ImGuiKey_RightBracket;
|
||||||
|
case 86: return ImGuiKey_Oem102;
|
||||||
|
case 43: return ImGuiKey_Backslash;
|
||||||
|
case 39: return ImGuiKey_Semicolon;
|
||||||
|
case 40: return ImGuiKey_Apostrophe;
|
||||||
|
case 51: return ImGuiKey_Comma;
|
||||||
|
case 52: return ImGuiKey_Period;
|
||||||
|
case 53: return ImGuiKey_Slash;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ImGuiKey_None;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
|
||||||
|
#ifndef WM_MOUSEHWHEEL
|
||||||
|
#define WM_MOUSEHWHEEL 0x020E
|
||||||
|
#endif
|
||||||
|
#ifndef DBT_DEVNODES_CHANGED
|
||||||
|
#define DBT_DEVNODES_CHANGED 0x0007
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Helper to obtain the source of mouse messages.
|
||||||
|
// See https://learn.microsoft.com/en-us/windows/win32/tablet/system-events-and-mouse-messages
|
||||||
|
// Prefer to call this at the top of the message handler to avoid the possibility of other Win32 calls interfering with this.
|
||||||
|
static ImGuiMouseSource ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo()
|
||||||
|
{
|
||||||
|
LPARAM extra_info = ::GetMessageExtraInfo();
|
||||||
|
if ((extra_info & 0xFFFFFF80) == 0xFF515700)
|
||||||
|
return ImGuiMouseSource_Pen;
|
||||||
|
if ((extra_info & 0xFFFFFF80) == 0xFF515780)
|
||||||
|
return ImGuiMouseSource_TouchScreen;
|
||||||
|
return ImGuiMouseSource_Mouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Win32 message handler (process Win32 mouse/keyboard inputs, etc.)
|
||||||
|
// Call from your application's message handler. Keep calling your message handler unless this function returns TRUE.
|
||||||
|
// When implementing your own backend, you can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if Dear ImGui wants to use your inputs.
|
||||||
|
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
|
||||||
|
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
|
||||||
|
// Generally you may always pass all inputs to Dear ImGui, and hide them from your application based on those two flags.
|
||||||
|
// PS: We treat DBLCLK messages as regular mouse down messages, so this code will work on windows classes that have the CS_DBLCLKS flag set. Our own example app code doesn't set this flag.
|
||||||
|
|
||||||
|
// Copy either line into your .cpp file to forward declare the function:
|
||||||
|
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); // Use ImGui::GetCurrentContext()
|
||||||
|
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandlerEx(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, ImGuiIO& io); // Doesn't use ImGui::GetCurrentContext()
|
||||||
|
|
||||||
|
IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
||||||
|
{
|
||||||
|
// Most backends don't have silent checks like this one, but we need it because WndProc are called early in CreateWindow().
|
||||||
|
// We silently allow both context or just only backend data to be nullptr.
|
||||||
|
if (ImGui::GetCurrentContext() == nullptr)
|
||||||
|
return 0;
|
||||||
|
return ImGui_ImplWin32_WndProcHandlerEx(hwnd, msg, wParam, lParam, ImGui::GetIO());
|
||||||
|
}
|
||||||
|
|
||||||
|
// This version is in theory thread-safe in the sense that no path should access ImGui::GetCurrentContext().
|
||||||
|
IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandlerEx(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, ImGuiIO& io)
|
||||||
|
{
|
||||||
|
ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io);
|
||||||
|
if (bd == nullptr)
|
||||||
|
return 0;
|
||||||
|
switch (msg)
|
||||||
|
{
|
||||||
|
case WM_MOUSEMOVE:
|
||||||
|
case WM_NCMOUSEMOVE:
|
||||||
|
{
|
||||||
|
// We need to call TrackMouseEvent in order to receive WM_MOUSELEAVE events
|
||||||
|
ImGuiMouseSource mouse_source = ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo();
|
||||||
|
const int area = (msg == WM_MOUSEMOVE) ? 1 : 2;
|
||||||
|
bd->MouseHwnd = hwnd;
|
||||||
|
if (bd->MouseTrackedArea != area)
|
||||||
|
{
|
||||||
|
TRACKMOUSEEVENT tme_cancel = { sizeof(tme_cancel), TME_CANCEL, hwnd, 0 };
|
||||||
|
TRACKMOUSEEVENT tme_track = { sizeof(tme_track), (DWORD)((area == 2) ? (TME_LEAVE | TME_NONCLIENT) : TME_LEAVE), hwnd, 0 };
|
||||||
|
if (bd->MouseTrackedArea != 0)
|
||||||
|
::TrackMouseEvent(&tme_cancel);
|
||||||
|
::TrackMouseEvent(&tme_track);
|
||||||
|
bd->MouseTrackedArea = area;
|
||||||
|
}
|
||||||
|
POINT mouse_pos = { (LONG)GET_X_LPARAM(lParam), (LONG)GET_Y_LPARAM(lParam) };
|
||||||
|
if (msg == WM_NCMOUSEMOVE && ::ScreenToClient(hwnd, &mouse_pos) == FALSE) // WM_NCMOUSEMOVE are provided in absolute coordinates.
|
||||||
|
return 0;
|
||||||
|
io.AddMouseSourceEvent(mouse_source);
|
||||||
|
io.AddMousePosEvent((float)mouse_pos.x, (float)mouse_pos.y);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
case WM_MOUSELEAVE:
|
||||||
|
case WM_NCMOUSELEAVE:
|
||||||
|
{
|
||||||
|
const int area = (msg == WM_MOUSELEAVE) ? 1 : 2;
|
||||||
|
if (bd->MouseTrackedArea == area)
|
||||||
|
{
|
||||||
|
if (bd->MouseHwnd == hwnd)
|
||||||
|
bd->MouseHwnd = nullptr;
|
||||||
|
bd->MouseTrackedArea = 0;
|
||||||
|
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
case WM_DESTROY:
|
||||||
|
if (bd->MouseHwnd == hwnd && bd->MouseTrackedArea != 0)
|
||||||
|
{
|
||||||
|
TRACKMOUSEEVENT tme_cancel = { sizeof(tme_cancel), TME_CANCEL, hwnd, 0 };
|
||||||
|
::TrackMouseEvent(&tme_cancel);
|
||||||
|
bd->MouseHwnd = nullptr;
|
||||||
|
bd->MouseTrackedArea = 0;
|
||||||
|
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
|
||||||
|
case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
|
||||||
|
case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
|
||||||
|
case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK:
|
||||||
|
{
|
||||||
|
ImGuiMouseSource mouse_source = ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo();
|
||||||
|
int button = 0;
|
||||||
|
if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { button = 0; }
|
||||||
|
if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; }
|
||||||
|
if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; }
|
||||||
|
if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
|
||||||
|
HWND hwnd_with_capture = ::GetCapture();
|
||||||
|
if (bd->MouseButtonsDown != 0 && hwnd_with_capture != hwnd) // Did we externally lost capture?
|
||||||
|
bd->MouseButtonsDown = 0;
|
||||||
|
if (bd->MouseButtonsDown == 0 && hwnd_with_capture == nullptr)
|
||||||
|
::SetCapture(hwnd); // Allow us to read mouse coordinates when dragging mouse outside of our window bounds.
|
||||||
|
bd->MouseButtonsDown |= 1 << button;
|
||||||
|
io.AddMouseSourceEvent(mouse_source);
|
||||||
|
io.AddMouseButtonEvent(button, true);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
case WM_LBUTTONUP:
|
||||||
|
case WM_RBUTTONUP:
|
||||||
|
case WM_MBUTTONUP:
|
||||||
|
case WM_XBUTTONUP:
|
||||||
|
{
|
||||||
|
ImGuiMouseSource mouse_source = ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo();
|
||||||
|
int button = 0;
|
||||||
|
if (msg == WM_LBUTTONUP) { button = 0; }
|
||||||
|
if (msg == WM_RBUTTONUP) { button = 1; }
|
||||||
|
if (msg == WM_MBUTTONUP) { button = 2; }
|
||||||
|
if (msg == WM_XBUTTONUP) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
|
||||||
|
bd->MouseButtonsDown &= ~(1 << button);
|
||||||
|
if (bd->MouseButtonsDown == 0 && ::GetCapture() == hwnd)
|
||||||
|
::ReleaseCapture();
|
||||||
|
io.AddMouseSourceEvent(mouse_source);
|
||||||
|
io.AddMouseButtonEvent(button, false);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
case WM_MOUSEWHEEL:
|
||||||
|
io.AddMouseWheelEvent(0.0f, (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA);
|
||||||
|
return 0;
|
||||||
|
case WM_MOUSEHWHEEL:
|
||||||
|
io.AddMouseWheelEvent(-(float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA, 0.0f);
|
||||||
|
return 0;
|
||||||
|
case WM_KEYDOWN:
|
||||||
|
case WM_KEYUP:
|
||||||
|
case WM_SYSKEYDOWN:
|
||||||
|
case WM_SYSKEYUP:
|
||||||
|
{
|
||||||
|
const bool is_key_down = (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN);
|
||||||
|
if (wParam < 256)
|
||||||
|
{
|
||||||
|
// Submit modifiers
|
||||||
|
ImGui_ImplWin32_UpdateKeyModifiers(io);
|
||||||
|
|
||||||
|
// Obtain virtual key code and convert to ImGuiKey
|
||||||
|
const ImGuiKey key = ImGui_ImplWin32_KeyEventToImGuiKey(wParam, lParam);
|
||||||
|
const int vk = (int)wParam;
|
||||||
|
const int scancode = (int)LOBYTE(HIWORD(lParam));
|
||||||
|
|
||||||
|
// Special behavior for VK_SNAPSHOT / ImGuiKey_PrintScreen as Windows doesn't emit the key down event.
|
||||||
|
if (key == ImGuiKey_PrintScreen && !is_key_down)
|
||||||
|
ImGui_ImplWin32_AddKeyEvent(io, key, true, vk, scancode);
|
||||||
|
|
||||||
|
// Submit key event
|
||||||
|
if (key != ImGuiKey_None)
|
||||||
|
ImGui_ImplWin32_AddKeyEvent(io, key, is_key_down, vk, scancode);
|
||||||
|
|
||||||
|
// Submit individual left/right modifier events
|
||||||
|
if (vk == VK_SHIFT)
|
||||||
|
{
|
||||||
|
// Important: Shift keys tend to get stuck when pressed together, missing key-up events are corrected in ImGui_ImplWin32_ProcessKeyEventsWorkarounds()
|
||||||
|
if (IsVkDown(VK_LSHIFT) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftShift, is_key_down, VK_LSHIFT, scancode); }
|
||||||
|
if (IsVkDown(VK_RSHIFT) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightShift, is_key_down, VK_RSHIFT, scancode); }
|
||||||
|
}
|
||||||
|
else if (vk == VK_CONTROL)
|
||||||
|
{
|
||||||
|
if (IsVkDown(VK_LCONTROL) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftCtrl, is_key_down, VK_LCONTROL, scancode); }
|
||||||
|
if (IsVkDown(VK_RCONTROL) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightCtrl, is_key_down, VK_RCONTROL, scancode); }
|
||||||
|
}
|
||||||
|
else if (vk == VK_MENU)
|
||||||
|
{
|
||||||
|
if (IsVkDown(VK_LMENU) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftAlt, is_key_down, VK_LMENU, scancode); }
|
||||||
|
if (IsVkDown(VK_RMENU) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightAlt, is_key_down, VK_RMENU, scancode); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
case WM_SETFOCUS:
|
||||||
|
case WM_KILLFOCUS:
|
||||||
|
io.AddFocusEvent(msg == WM_SETFOCUS);
|
||||||
|
return 0;
|
||||||
|
case WM_INPUTLANGCHANGE:
|
||||||
|
ImGui_ImplWin32_UpdateKeyboardCodePage(io);
|
||||||
|
return 0;
|
||||||
|
case WM_CHAR:
|
||||||
|
if (::IsWindowUnicode(hwnd))
|
||||||
|
{
|
||||||
|
// You can also use ToAscii()+GetKeyboardState() to retrieve characters.
|
||||||
|
if (wParam > 0 && wParam < 0x10000)
|
||||||
|
io.AddInputCharacterUTF16((unsigned short)wParam);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
wchar_t wch = 0;
|
||||||
|
::MultiByteToWideChar(bd->KeyboardCodePage, MB_PRECOMPOSED, (char*)&wParam, 1, &wch, 1);
|
||||||
|
io.AddInputCharacter(wch);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
case WM_SETCURSOR:
|
||||||
|
// This is required to restore cursor when transitioning from e.g resize borders to client area.
|
||||||
|
if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor(io, bd->LastMouseCursor))
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
case WM_DEVICECHANGE:
|
||||||
|
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
|
||||||
|
if ((UINT)wParam == DBT_DEVNODES_CHANGED)
|
||||||
|
bd->WantUpdateHasGamepad = true;
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------------
|
||||||
|
// DPI-related helpers (optional)
|
||||||
|
//--------------------------------------------------------------------------------------------------------
|
||||||
|
// - Use to enable DPI awareness without having to create an application manifest.
|
||||||
|
// - Your own app may already do this via a manifest or explicit calls. This is mostly useful for our examples/ apps.
|
||||||
|
// - In theory we could call simple functions from Windows SDK such as SetProcessDPIAware(), SetProcessDpiAwareness(), etc.
|
||||||
|
// but most of the functions provided by Microsoft require Windows 8.1/10+ SDK at compile time and Windows 8/10+ at runtime,
|
||||||
|
// neither we want to require the user to have. So we dynamically select and load those functions to avoid dependencies.
|
||||||
|
//---------------------------------------------------------------------------------------------------------
|
||||||
|
// This is the scheme successfully used by GLFW (from which we borrowed some of the code) and other apps aiming to be highly portable.
|
||||||
|
// ImGui_ImplWin32_EnableDpiAwareness() is just a helper called by main.cpp, we don't call it automatically.
|
||||||
|
// If you are trying to implement your own backend for your own engine, you may ignore that noise.
|
||||||
|
//---------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Perform our own check with RtlVerifyVersionInfo() instead of using functions from <VersionHelpers.h> as they
|
||||||
|
// require a manifest to be functional for checks above 8.1. See https://github.com/ocornut/imgui/issues/4200
|
||||||
|
static BOOL _IsWindowsVersionOrGreater(WORD major, WORD minor, WORD)
|
||||||
|
{
|
||||||
|
typedef LONG(WINAPI* PFN_RtlVerifyVersionInfo)(OSVERSIONINFOEXW*, ULONG, ULONGLONG);
|
||||||
|
static PFN_RtlVerifyVersionInfo RtlVerifyVersionInfoFn = nullptr;
|
||||||
|
if (RtlVerifyVersionInfoFn == nullptr)
|
||||||
|
if (HMODULE ntdllModule = ::GetModuleHandleA("ntdll.dll"))
|
||||||
|
RtlVerifyVersionInfoFn = (PFN_RtlVerifyVersionInfo)GetProcAddress(ntdllModule, "RtlVerifyVersionInfo");
|
||||||
|
if (RtlVerifyVersionInfoFn == nullptr)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
RTL_OSVERSIONINFOEXW versionInfo = { };
|
||||||
|
ULONGLONG conditionMask = 0;
|
||||||
|
versionInfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
|
||||||
|
versionInfo.dwMajorVersion = major;
|
||||||
|
versionInfo.dwMinorVersion = minor;
|
||||||
|
VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
|
||||||
|
VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
|
||||||
|
return (RtlVerifyVersionInfoFn(&versionInfo, VER_MAJORVERSION | VER_MINORVERSION, conditionMask) == 0) ? TRUE : FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define _IsWindowsVistaOrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0600), LOBYTE(0x0600), 0) // _WIN32_WINNT_VISTA
|
||||||
|
#define _IsWindows8OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0602), LOBYTE(0x0602), 0) // _WIN32_WINNT_WIN8
|
||||||
|
#define _IsWindows8Point1OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0603), LOBYTE(0x0603), 0) // _WIN32_WINNT_WINBLUE
|
||||||
|
#define _IsWindows10OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0A00), LOBYTE(0x0A00), 0) // _WIN32_WINNT_WINTHRESHOLD / _WIN32_WINNT_WIN10
|
||||||
|
|
||||||
|
#ifndef DPI_ENUMS_DECLARED
|
||||||
|
typedef enum { PROCESS_DPI_UNAWARE = 0, PROCESS_SYSTEM_DPI_AWARE = 1, PROCESS_PER_MONITOR_DPI_AWARE = 2 } PROCESS_DPI_AWARENESS;
|
||||||
|
typedef enum { MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2, MDT_DEFAULT = MDT_EFFECTIVE_DPI } MONITOR_DPI_TYPE;
|
||||||
|
#endif
|
||||||
|
#ifndef _DPI_AWARENESS_CONTEXTS_
|
||||||
|
DECLARE_HANDLE(DPI_AWARENESS_CONTEXT);
|
||||||
|
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE (DPI_AWARENESS_CONTEXT)-3
|
||||||
|
#endif
|
||||||
|
#ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
|
||||||
|
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (DPI_AWARENESS_CONTEXT)-4
|
||||||
|
#endif
|
||||||
|
typedef HRESULT(WINAPI* PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS); // Shcore.lib + dll, Windows 8.1+
|
||||||
|
typedef HRESULT(WINAPI* PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); // Shcore.lib + dll, Windows 8.1+
|
||||||
|
typedef DPI_AWARENESS_CONTEXT(WINAPI* PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); // User32.lib + dll, Windows 10 v1607+ (Creators Update)
|
||||||
|
|
||||||
|
// Helper function to enable DPI awareness without setting up a manifest
|
||||||
|
void ImGui_ImplWin32_EnableDpiAwareness()
|
||||||
|
{
|
||||||
|
if (_IsWindows10OrGreater())
|
||||||
|
{
|
||||||
|
static HINSTANCE user32_dll = ::LoadLibraryA("user32.dll"); // Reference counted per-process
|
||||||
|
if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "SetThreadDpiAwarenessContext"))
|
||||||
|
{
|
||||||
|
SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (_IsWindows8Point1OrGreater())
|
||||||
|
{
|
||||||
|
static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
|
||||||
|
if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, "SetProcessDpiAwareness"))
|
||||||
|
{
|
||||||
|
SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#if _WIN32_WINNT >= 0x0600
|
||||||
|
::SetProcessDPIAware();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(_MSC_VER) && !defined(NOGDI)
|
||||||
|
#pragma comment(lib, "gdi32") // Link with gdi32.lib for GetDeviceCaps(). MinGW will require linking with '-lgdi32'
|
||||||
|
#endif
|
||||||
|
|
||||||
|
float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor)
|
||||||
|
{
|
||||||
|
UINT xdpi = 96, ydpi = 96;
|
||||||
|
if (_IsWindows8Point1OrGreater())
|
||||||
|
{
|
||||||
|
static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
|
||||||
|
static PFN_GetDpiForMonitor GetDpiForMonitorFn = nullptr;
|
||||||
|
if (GetDpiForMonitorFn == nullptr && shcore_dll != nullptr)
|
||||||
|
GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, "GetDpiForMonitor");
|
||||||
|
if (GetDpiForMonitorFn != nullptr)
|
||||||
|
{
|
||||||
|
GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi);
|
||||||
|
IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert!
|
||||||
|
return xdpi / 96.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#ifndef NOGDI
|
||||||
|
const HDC dc = ::GetDC(nullptr);
|
||||||
|
xdpi = ::GetDeviceCaps(dc, LOGPIXELSX);
|
||||||
|
ydpi = ::GetDeviceCaps(dc, LOGPIXELSY);
|
||||||
|
IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert!
|
||||||
|
::ReleaseDC(nullptr, dc);
|
||||||
|
#endif
|
||||||
|
return xdpi / 96.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd)
|
||||||
|
{
|
||||||
|
HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST);
|
||||||
|
return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------------
|
||||||
|
// Transparency related helpers (optional)
|
||||||
|
//--------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#pragma comment(lib, "dwmapi") // Link with dwmapi.lib. MinGW will require linking with '-ldwmapi'
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// [experimental]
|
||||||
|
// Borrowed from GLFW's function updateFramebufferTransparency() in src/win32_window.c
|
||||||
|
// (the Dwm* functions are Vista era functions but we are borrowing logic from GLFW)
|
||||||
|
void ImGui_ImplWin32_EnableAlphaCompositing(void* hwnd)
|
||||||
|
{
|
||||||
|
if (!_IsWindowsVistaOrGreater())
|
||||||
|
return;
|
||||||
|
|
||||||
|
BOOL composition;
|
||||||
|
if (FAILED(::DwmIsCompositionEnabled(&composition)) || !composition)
|
||||||
|
return;
|
||||||
|
|
||||||
|
BOOL opaque;
|
||||||
|
DWORD color;
|
||||||
|
if (_IsWindows8OrGreater() || (SUCCEEDED(::DwmGetColorizationColor(&color, &opaque)) && !opaque))
|
||||||
|
{
|
||||||
|
HRGN region = ::CreateRectRgn(0, 0, -1, -1);
|
||||||
|
DWM_BLURBEHIND bb = {};
|
||||||
|
bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
|
||||||
|
bb.hRgnBlur = region;
|
||||||
|
bb.fEnable = TRUE;
|
||||||
|
::DwmEnableBlurBehindWindow((HWND)hwnd, &bb);
|
||||||
|
::DeleteObject(region);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DWM_BLURBEHIND bb = {};
|
||||||
|
bb.dwFlags = DWM_BB_ENABLE;
|
||||||
|
::DwmEnableBlurBehindWindow((HWND)hwnd, &bb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
#endif
|
||||||
|
#if defined(__clang__)
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
53
imgui/backends/imgui_impl_win32.h
Normal file
53
imgui/backends/imgui_impl_win32.h
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
// dear imgui: Platform Backend for Windows (standard windows API for 32-bits AND 64-bits applications)
|
||||||
|
// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
|
||||||
|
|
||||||
|
// Implemented features:
|
||||||
|
// [X] Platform: Clipboard support (for Win32 this is actually part of core dear imgui)
|
||||||
|
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen.
|
||||||
|
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy VK_* values are obsolete since 1.87 and not supported since 1.91.5]
|
||||||
|
// [X] Platform: Gamepad support.
|
||||||
|
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||||
|
|
||||||
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||||
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||||
|
// Learn about Dear ImGui:
|
||||||
|
// - FAQ https://dearimgui.com/faq
|
||||||
|
// - Getting Started https://dearimgui.com/getting-started
|
||||||
|
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||||
|
// - Introduction, links and more at the top of imgui.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "imgui.h" // IMGUI_IMPL_API
|
||||||
|
#ifndef IMGUI_DISABLE
|
||||||
|
|
||||||
|
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplWin32_Init(void* hwnd);
|
||||||
|
IMGUI_IMPL_API bool ImGui_ImplWin32_InitForOpenGL(void* hwnd);
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplWin32_Shutdown();
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplWin32_NewFrame();
|
||||||
|
|
||||||
|
// Win32 message handler your application need to call.
|
||||||
|
// - Intentionally commented out in a '#if 0' block to avoid dragging dependencies on <windows.h> from this helper.
|
||||||
|
// - You should COPY the line below into your .cpp code to forward declare the function and then you can call it.
|
||||||
|
// - Call from your application's message handler. Keep calling your message handler unless this function returns TRUE.
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// DPI-related helpers (optional)
|
||||||
|
// - Use to enable DPI awareness without having to create an application manifest.
|
||||||
|
// - Your own app may already do this via a manifest or explicit calls. This is mostly useful for our examples/ apps.
|
||||||
|
// - In theory we could call simple functions from Windows SDK such as SetProcessDPIAware(), SetProcessDpiAwareness(), etc.
|
||||||
|
// but most of the functions provided by Microsoft require Windows 8.1/10+ SDK at compile time and Windows 8/10+ at runtime,
|
||||||
|
// neither we want to require the user to have. So we dynamically select and load those functions to avoid dependencies.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplWin32_EnableDpiAwareness();
|
||||||
|
IMGUI_IMPL_API float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd); // HWND hwnd
|
||||||
|
IMGUI_IMPL_API float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor); // HMONITOR monitor
|
||||||
|
|
||||||
|
// Transparency related helpers (optional) [experimental]
|
||||||
|
// - Use to enable alpha compositing transparency with the desktop.
|
||||||
|
// - Use together with e.g. clearing your framebuffer with zero-alpha.
|
||||||
|
IMGUI_IMPL_API void ImGui_ImplWin32_EnableAlphaCompositing(void* hwnd); // HWND hwnd
|
||||||
|
|
||||||
|
#endif // #ifndef IMGUI_DISABLE
|
||||||
40
imgui/backends/sdlgpu3/build_instructions.txt
Normal file
40
imgui/backends/sdlgpu3/build_instructions.txt
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
|
||||||
|
Instructions to rebuild imgui_impl_sdlgpu3_shaders.h
|
||||||
|
(You don't need to copy this folder if you are using the backend as-is)
|
||||||
|
|
||||||
|
1) Compile the raw shader files to SPIRV:
|
||||||
|
|
||||||
|
glslc -o vertex.spv -c shader.vert
|
||||||
|
glslc -o fragment.spv -c shader.frag
|
||||||
|
|
||||||
|
|
||||||
|
2) Build SDL_shadercross (https://github.com/libsdl-org/SDL_shadercross)
|
||||||
|
|
||||||
|
|
||||||
|
3-A) Compiling for the Vulkan Driver:
|
||||||
|
|
||||||
|
Nothing to do, you just need the previous vertex.spv/fragment.spv, proceed to step 4
|
||||||
|
|
||||||
|
|
||||||
|
3-B) Compiling for the DirectX 12 Driver:
|
||||||
|
|
||||||
|
./shadercross vertex.spv -s SPIRV -d DXBC -t vertex -e main -o vertex.dxbc
|
||||||
|
./shadercross fragment.spv -s SPIRV -d DXBC -t fragment -e main -o fragment.dxbc
|
||||||
|
|
||||||
|
Proceed to step 4
|
||||||
|
|
||||||
|
|
||||||
|
3-C) Compiling for Metal (On windows you'll need the Metal Developer Tools for Windows, on linux you might use wine, but I never tested it):
|
||||||
|
|
||||||
|
./shadercross vertex.spv -s SPIRV -d MSL -t vertex -e main -o vertex.metal
|
||||||
|
./shadercross fragment.spv -s SPIRV -d MSL -t fragment -e main -o fragment.metal
|
||||||
|
|
||||||
|
xcrun -sdk macosx metal -o vertex.ir -c vertex.metal
|
||||||
|
xcrun -sdk macosx metal -o fragment.ir -c fragment.metal
|
||||||
|
xcrun -sdk macosx metallib -o vertex.metallib -c vertex.ir
|
||||||
|
xcrun -sdk macosx metallib -o fragment.metallib -c fragment.ir
|
||||||
|
|
||||||
|
Proceed to step 4
|
||||||
|
|
||||||
|
|
||||||
|
4) Use a tool like https://notisrac.github.io/FileToCArray/ or misc/fonts/binary_to_compressed_c.cpp in imgui repository to convert the file to a uint8_t array.
|
||||||
15
imgui/backends/sdlgpu3/shader.frag
Normal file
15
imgui/backends/sdlgpu3/shader.frag
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#version 450 core
|
||||||
|
layout(location = 0) out vec4 fColor;
|
||||||
|
|
||||||
|
layout(set=2, binding=0) uniform sampler2D sTexture;
|
||||||
|
|
||||||
|
layout(location = 0) in struct
|
||||||
|
{
|
||||||
|
vec4 Color;
|
||||||
|
vec2 UV;
|
||||||
|
} In;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
fColor = In.Color * texture(sTexture, In.UV.st);
|
||||||
|
}
|
||||||
24
imgui/backends/sdlgpu3/shader.vert
Normal file
24
imgui/backends/sdlgpu3/shader.vert
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#version 450 core
|
||||||
|
layout(location = 0) in vec2 aPos;
|
||||||
|
layout(location = 1) in vec2 aUV;
|
||||||
|
layout(location = 2) in vec4 aColor;
|
||||||
|
|
||||||
|
layout(set=1,binding=0) uniform UBO
|
||||||
|
{
|
||||||
|
vec2 uScale;
|
||||||
|
vec2 uTranslate;
|
||||||
|
} ubo;
|
||||||
|
|
||||||
|
layout(location = 0) out struct
|
||||||
|
{
|
||||||
|
vec4 Color;
|
||||||
|
vec2 UV;
|
||||||
|
} Out;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
Out.Color = aColor;
|
||||||
|
Out.UV = aUV;
|
||||||
|
gl_Position = vec4(aPos * ubo.uScale + ubo.uTranslate, 0, 1);
|
||||||
|
gl_Position.y *= -1.0f;
|
||||||
|
}
|
||||||
4
imgui/backends/vulkan/build_instructions.txt
Normal file
4
imgui/backends/vulkan/build_instructions.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
Script to rebuild shaders stored inside imgui_impl_vulkan.h
|
||||||
|
(You don't need to copy this folder if you are using the backend as-is)
|
||||||
|
|
||||||
6
imgui/backends/vulkan/generate_spv.sh
Executable file
6
imgui/backends/vulkan/generate_spv.sh
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
## -V: create SPIR-V binary
|
||||||
|
## -x: save binary output as text-based 32-bit hexadecimal numbers
|
||||||
|
## -o: output file
|
||||||
|
glslangValidator -V -x -o glsl_shader.frag.u32 glsl_shader.frag
|
||||||
|
glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert
|
||||||
14
imgui/backends/vulkan/glsl_shader.frag
Normal file
14
imgui/backends/vulkan/glsl_shader.frag
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#version 450 core
|
||||||
|
layout(location = 0) out vec4 fColor;
|
||||||
|
|
||||||
|
layout(set=0, binding=0) uniform sampler2D sTexture;
|
||||||
|
|
||||||
|
layout(location = 0) in struct {
|
||||||
|
vec4 Color;
|
||||||
|
vec2 UV;
|
||||||
|
} In;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
fColor = In.Color * texture(sTexture, In.UV.st);
|
||||||
|
}
|
||||||
25
imgui/backends/vulkan/glsl_shader.vert
Normal file
25
imgui/backends/vulkan/glsl_shader.vert
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#version 450 core
|
||||||
|
layout(location = 0) in vec2 aPos;
|
||||||
|
layout(location = 1) in vec2 aUV;
|
||||||
|
layout(location = 2) in vec4 aColor;
|
||||||
|
|
||||||
|
layout(push_constant) uniform uPushConstant {
|
||||||
|
vec2 uScale;
|
||||||
|
vec2 uTranslate;
|
||||||
|
} pc;
|
||||||
|
|
||||||
|
out gl_PerVertex {
|
||||||
|
vec4 gl_Position;
|
||||||
|
};
|
||||||
|
|
||||||
|
layout(location = 0) out struct {
|
||||||
|
vec4 Color;
|
||||||
|
vec2 UV;
|
||||||
|
} Out;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
Out.Color = aColor;
|
||||||
|
Out.UV = aUV;
|
||||||
|
gl_Position = vec4(aPos * pc.uScale + pc.uTranslate, 0, 1);
|
||||||
|
}
|
||||||
141
imgui/imconfig.h
Normal file
141
imgui/imconfig.h
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// DEAR IMGUI COMPILE-TIME OPTIONS
|
||||||
|
// Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure.
|
||||||
|
// You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// A) You may edit imconfig.h (and not overwrite it when updating Dear ImGui, or maintain a patch/rebased branch with your modifications to it)
|
||||||
|
// B) or '#define IMGUI_USER_CONFIG "my_imgui_config.h"' in your project and then add directives in your own file without touching this template.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// You need to make sure that configuration settings are defined consistently _everywhere_ Dear ImGui is used, which include the imgui*.cpp
|
||||||
|
// files but also _any_ of your code that uses Dear ImGui. This is because some compile-time options have an affect on data structures.
|
||||||
|
// Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts.
|
||||||
|
// Call IMGUI_CHECKVERSION() from your .cpp file to verify that the data structures your files are using are matching the ones imgui.cpp is using.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
//---- Define assertion handler. Defaults to calling assert().
|
||||||
|
// If your macro uses multiple statements, make sure is enclosed in a 'do { .. } while (0)' block so it can be used as a single statement.
|
||||||
|
//#define IM_ASSERT(_EXPR) MyAssert(_EXPR)
|
||||||
|
//#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts
|
||||||
|
|
||||||
|
//---- Define attributes of all API symbols declarations, e.g. for DLL under Windows
|
||||||
|
// Using Dear ImGui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility.
|
||||||
|
// - Windows DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions()
|
||||||
|
// for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for more details.
|
||||||
|
//#define IMGUI_API __declspec(dllexport) // MSVC Windows: DLL export
|
||||||
|
//#define IMGUI_API __declspec(dllimport) // MSVC Windows: DLL import
|
||||||
|
//#define IMGUI_API __attribute__((visibility("default"))) // GCC/Clang: override visibility when set is hidden
|
||||||
|
|
||||||
|
//---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to clean your code of obsolete function/names.
|
||||||
|
//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||||
|
|
||||||
|
//---- Disable all of Dear ImGui or don't implement standard windows/tools.
|
||||||
|
// It is very strongly recommended to NOT disable the demo windows and debug tool during development. They are extremely useful in day to day work. Please read comments in imgui_demo.cpp.
|
||||||
|
//#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty.
|
||||||
|
//#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty.
|
||||||
|
//#define IMGUI_DISABLE_DEBUG_TOOLS // Disable metrics/debugger and other debug tools: ShowMetricsWindow(), ShowDebugLogWindow() and ShowIDStackToolWindow() will be empty.
|
||||||
|
|
||||||
|
//---- Don't implement some functions to reduce linkage requirements.
|
||||||
|
//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. (user32.lib/.a, kernel32.lib/.a)
|
||||||
|
//#define IMGUI_ENABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with Visual Studio] Implement default IME handler (require imm32.lib/.a, auto-link for Visual Studio, -limm32 on command-line for MinGW)
|
||||||
|
//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with non-Visual Studio compilers] Don't implement default IME handler (won't require imm32.lib/.a)
|
||||||
|
//#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, IME).
|
||||||
|
//#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default).
|
||||||
|
//#define IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS // Don't implement default platform_io.Platform_OpenInShellFn() handler (Win32: ShellExecute(), require shell32.lib/.a, Mac/Linux: use system("")).
|
||||||
|
//#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf)
|
||||||
|
//#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself.
|
||||||
|
//#define IMGUI_DISABLE_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle at all (replace them with dummies)
|
||||||
|
//#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function.
|
||||||
|
//#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions().
|
||||||
|
//#define IMGUI_DISABLE_DEFAULT_FONT // Disable default embedded font (ProggyClean.ttf), remove ~9.5 KB from output binary. AddFontDefault() will assert.
|
||||||
|
//#define IMGUI_DISABLE_SSE // Disable use of SSE intrinsics even if available
|
||||||
|
|
||||||
|
//---- Enable Test Engine / Automation features.
|
||||||
|
//#define IMGUI_ENABLE_TEST_ENGINE // Enable imgui_test_engine hooks. Generally set automatically by include "imgui_te_config.h", see Test Engine for details.
|
||||||
|
|
||||||
|
//---- Include imgui_user.h at the end of imgui.h as a convenience
|
||||||
|
// May be convenient for some users to only explicitly include vanilla imgui.h and have extra stuff included.
|
||||||
|
//#define IMGUI_INCLUDE_IMGUI_USER_H
|
||||||
|
//#define IMGUI_USER_H_FILENAME "my_folder/my_imgui_user.h"
|
||||||
|
|
||||||
|
//---- Pack vertex colors as BGRA8 instead of RGBA8 (to avoid converting from one to another). Need dedicated backend support.
|
||||||
|
//#define IMGUI_USE_BGRA_PACKED_COLOR
|
||||||
|
|
||||||
|
//---- Use legacy CRC32-adler tables (used before 1.91.6), in order to preserve old .ini data that you cannot afford to invalidate.
|
||||||
|
//#define IMGUI_USE_LEGACY_CRC32_ADLER
|
||||||
|
|
||||||
|
//---- Use 32-bit for ImWchar (default is 16-bit) to support Unicode planes 1-16. (e.g. point beyond 0xFFFF like emoticons, dingbats, symbols, shapes, ancient languages, etc...)
|
||||||
|
//#define IMGUI_USE_WCHAR32
|
||||||
|
|
||||||
|
//---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version
|
||||||
|
// By default the embedded implementations are declared static and not available outside of Dear ImGui sources files.
|
||||||
|
//#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h"
|
||||||
|
//#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h"
|
||||||
|
//#define IMGUI_STB_SPRINTF_FILENAME "my_folder/stb_sprintf.h" // only used if IMGUI_USE_STB_SPRINTF is defined.
|
||||||
|
//#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
|
||||||
|
//#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
|
||||||
|
//#define IMGUI_DISABLE_STB_SPRINTF_IMPLEMENTATION // only disabled if IMGUI_USE_STB_SPRINTF is defined.
|
||||||
|
|
||||||
|
//---- Use stb_sprintf.h for a faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined)
|
||||||
|
// Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by stb_sprintf.h.
|
||||||
|
//#define IMGUI_USE_STB_SPRINTF
|
||||||
|
|
||||||
|
//---- Use FreeType to build and rasterize the font atlas (instead of stb_truetype which is embedded by default in Dear ImGui)
|
||||||
|
// Requires FreeType headers to be available in the include path. Requires program to be compiled with 'misc/freetype/imgui_freetype.cpp' (in this repository) + the FreeType library (not provided).
|
||||||
|
// On Windows you may use vcpkg with 'vcpkg install freetype --triplet=x64-windows' + 'vcpkg integrate install'.
|
||||||
|
//#define IMGUI_ENABLE_FREETYPE
|
||||||
|
|
||||||
|
//---- Use FreeType + plutosvg or lunasvg to render OpenType SVG fonts (SVGinOT)
|
||||||
|
// Only works in combination with IMGUI_ENABLE_FREETYPE.
|
||||||
|
// - plutosvg is currently easier to install, as e.g. it is part of vcpkg. It will support more fonts and may load them faster. See misc/freetype/README for instructions.
|
||||||
|
// - Both require headers to be available in the include path + program to be linked with the library code (not provided).
|
||||||
|
// - (note: lunasvg implementation is based on Freetype's rsvg-port.c which is licensed under CeCILL-C Free Software License Agreement)
|
||||||
|
//#define IMGUI_ENABLE_FREETYPE_PLUTOSVG
|
||||||
|
//#define IMGUI_ENABLE_FREETYPE_LUNASVG
|
||||||
|
|
||||||
|
//---- Use stb_truetype to build and rasterize the font atlas (default)
|
||||||
|
// The only purpose of this define is if you want force compilation of the stb_truetype backend ALONG with the FreeType backend.
|
||||||
|
//#define IMGUI_ENABLE_STB_TRUETYPE
|
||||||
|
|
||||||
|
//---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4.
|
||||||
|
// This will be inlined as part of ImVec2 and ImVec4 class declarations.
|
||||||
|
/*
|
||||||
|
#define IM_VEC2_CLASS_EXTRA \
|
||||||
|
constexpr ImVec2(const MyVec2& f) : x(f.x), y(f.y) {} \
|
||||||
|
operator MyVec2() const { return MyVec2(x,y); }
|
||||||
|
|
||||||
|
#define IM_VEC4_CLASS_EXTRA \
|
||||||
|
constexpr ImVec4(const MyVec4& f) : x(f.x), y(f.y), z(f.z), w(f.w) {} \
|
||||||
|
operator MyVec4() const { return MyVec4(x,y,z,w); }
|
||||||
|
*/
|
||||||
|
//---- ...Or use Dear ImGui's own very basic math operators.
|
||||||
|
//#define IMGUI_DEFINE_MATH_OPERATORS
|
||||||
|
|
||||||
|
//---- Use 32-bit vertex indices (default is 16-bit) is one way to allow large meshes with more than 64K vertices.
|
||||||
|
// Your renderer backend will need to support it (most example renderer backends support both 16/32-bit indices).
|
||||||
|
// Another way to allow large meshes while keeping 16-bit indices is to handle ImDrawCmd::VtxOffset in your renderer.
|
||||||
|
// Read about ImGuiBackendFlags_RendererHasVtxOffset for details.
|
||||||
|
//#define ImDrawIdx unsigned int
|
||||||
|
|
||||||
|
//---- Override ImDrawCallback signature (will need to modify renderer backends accordingly)
|
||||||
|
//struct ImDrawList;
|
||||||
|
//struct ImDrawCmd;
|
||||||
|
//typedef void (*MyImDrawCallback)(const ImDrawList* draw_list, const ImDrawCmd* cmd, void* my_renderer_user_data);
|
||||||
|
//#define ImDrawCallback MyImDrawCallback
|
||||||
|
|
||||||
|
//---- Debug Tools: Macro to break in Debugger (we provide a default implementation of this in the codebase)
|
||||||
|
// (use 'Metrics->Tools->Item Picker' to pick widgets with the mouse and break into them for easy debugging.)
|
||||||
|
//#define IM_DEBUG_BREAK IM_ASSERT(0)
|
||||||
|
//#define IM_DEBUG_BREAK __debugbreak()
|
||||||
|
|
||||||
|
//---- Debug Tools: Enable slower asserts
|
||||||
|
//#define IMGUI_DEBUG_PARANOID
|
||||||
|
|
||||||
|
//---- Tip: You can add extra functions within the ImGui:: namespace from anywhere (e.g. your own sources/header files)
|
||||||
|
/*
|
||||||
|
namespace ImGui
|
||||||
|
{
|
||||||
|
void MyFunction(const char* name, MyMatrix44* mtx);
|
||||||
|
}
|
||||||
|
*/
|
||||||
17773
imgui/imgui.cpp
Normal file
17773
imgui/imgui.cpp
Normal file
File diff suppressed because it is too large
Load Diff
4121
imgui/imgui.h
Normal file
4121
imgui/imgui.h
Normal file
File diff suppressed because it is too large
Load Diff
10829
imgui/imgui_demo.cpp
Normal file
10829
imgui/imgui_demo.cpp
Normal file
File diff suppressed because it is too large
Load Diff
6308
imgui/imgui_draw.cpp
Normal file
6308
imgui/imgui_draw.cpp
Normal file
File diff suppressed because it is too large
Load Diff
3897
imgui/imgui_internal.h
Normal file
3897
imgui/imgui_internal.h
Normal file
File diff suppressed because it is too large
Load Diff
4559
imgui/imgui_tables.cpp
Normal file
4559
imgui/imgui_tables.cpp
Normal file
File diff suppressed because it is too large
Load Diff
10605
imgui/imgui_widgets.cpp
Normal file
10605
imgui/imgui_widgets.cpp
Normal file
File diff suppressed because it is too large
Load Diff
3261
imgui/imnodes.cpp
Normal file
3261
imgui/imnodes.cpp
Normal file
File diff suppressed because it is too large
Load Diff
438
imgui/imnodes.h
Normal file
438
imgui/imnodes.h
Normal file
@@ -0,0 +1,438 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
|
#ifdef IMNODES_USER_CONFIG
|
||||||
|
#include IMNODES_USER_CONFIG
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef IMNODES_NAMESPACE
|
||||||
|
#define IMNODES_NAMESPACE ImNodes
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef int ImNodesCol; // -> enum ImNodesCol_
|
||||||
|
typedef int ImNodesStyleVar; // -> enum ImNodesStyleVar_
|
||||||
|
typedef int ImNodesStyleFlags; // -> enum ImNodesStyleFlags_
|
||||||
|
typedef int ImNodesPinShape; // -> enum ImNodesPinShape_
|
||||||
|
typedef int ImNodesAttributeFlags; // -> enum ImNodesAttributeFlags_
|
||||||
|
typedef int ImNodesMiniMapLocation; // -> enum ImNodesMiniMapLocation_
|
||||||
|
|
||||||
|
enum ImNodesCol_
|
||||||
|
{
|
||||||
|
ImNodesCol_NodeBackground = 0,
|
||||||
|
ImNodesCol_NodeBackgroundHovered,
|
||||||
|
ImNodesCol_NodeBackgroundSelected,
|
||||||
|
ImNodesCol_NodeOutline,
|
||||||
|
ImNodesCol_TitleBar,
|
||||||
|
ImNodesCol_TitleBarHovered,
|
||||||
|
ImNodesCol_TitleBarSelected,
|
||||||
|
ImNodesCol_Link,
|
||||||
|
ImNodesCol_LinkHovered,
|
||||||
|
ImNodesCol_LinkSelected,
|
||||||
|
ImNodesCol_Pin,
|
||||||
|
ImNodesCol_PinHovered,
|
||||||
|
ImNodesCol_BoxSelector,
|
||||||
|
ImNodesCol_BoxSelectorOutline,
|
||||||
|
ImNodesCol_GridBackground,
|
||||||
|
ImNodesCol_GridLine,
|
||||||
|
ImNodesCol_GridLinePrimary,
|
||||||
|
ImNodesCol_MiniMapBackground,
|
||||||
|
ImNodesCol_MiniMapBackgroundHovered,
|
||||||
|
ImNodesCol_MiniMapOutline,
|
||||||
|
ImNodesCol_MiniMapOutlineHovered,
|
||||||
|
ImNodesCol_MiniMapNodeBackground,
|
||||||
|
ImNodesCol_MiniMapNodeBackgroundHovered,
|
||||||
|
ImNodesCol_MiniMapNodeBackgroundSelected,
|
||||||
|
ImNodesCol_MiniMapNodeOutline,
|
||||||
|
ImNodesCol_MiniMapLink,
|
||||||
|
ImNodesCol_MiniMapLinkSelected,
|
||||||
|
ImNodesCol_MiniMapCanvas,
|
||||||
|
ImNodesCol_MiniMapCanvasOutline,
|
||||||
|
ImNodesCol_COUNT
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImNodesStyleVar_
|
||||||
|
{
|
||||||
|
ImNodesStyleVar_GridSpacing = 0,
|
||||||
|
ImNodesStyleVar_NodeCornerRounding,
|
||||||
|
ImNodesStyleVar_NodePadding,
|
||||||
|
ImNodesStyleVar_NodeBorderThickness,
|
||||||
|
ImNodesStyleVar_LinkThickness,
|
||||||
|
ImNodesStyleVar_LinkLineSegmentsPerLength,
|
||||||
|
ImNodesStyleVar_LinkHoverDistance,
|
||||||
|
ImNodesStyleVar_PinCircleRadius,
|
||||||
|
ImNodesStyleVar_PinQuadSideLength,
|
||||||
|
ImNodesStyleVar_PinTriangleSideLength,
|
||||||
|
ImNodesStyleVar_PinLineThickness,
|
||||||
|
ImNodesStyleVar_PinHoverRadius,
|
||||||
|
ImNodesStyleVar_PinOffset,
|
||||||
|
ImNodesStyleVar_MiniMapPadding,
|
||||||
|
ImNodesStyleVar_MiniMapOffset,
|
||||||
|
ImNodesStyleVar_COUNT
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImNodesStyleFlags_
|
||||||
|
{
|
||||||
|
ImNodesStyleFlags_None = 0,
|
||||||
|
ImNodesStyleFlags_NodeOutline = 1 << 0,
|
||||||
|
ImNodesStyleFlags_GridLines = 1 << 2,
|
||||||
|
ImNodesStyleFlags_GridLinesPrimary = 1 << 3,
|
||||||
|
ImNodesStyleFlags_GridSnapping = 1 << 4
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImNodesPinShape_
|
||||||
|
{
|
||||||
|
ImNodesPinShape_Circle,
|
||||||
|
ImNodesPinShape_CircleFilled,
|
||||||
|
ImNodesPinShape_Triangle,
|
||||||
|
ImNodesPinShape_TriangleFilled,
|
||||||
|
ImNodesPinShape_Quad,
|
||||||
|
ImNodesPinShape_QuadFilled
|
||||||
|
};
|
||||||
|
|
||||||
|
// This enum controls the way the attribute pins behave.
|
||||||
|
enum ImNodesAttributeFlags_
|
||||||
|
{
|
||||||
|
ImNodesAttributeFlags_None = 0,
|
||||||
|
// Allow detaching a link by left-clicking and dragging the link at a pin it is connected to.
|
||||||
|
// NOTE: the user has to actually delete the link for this to work. A deleted link can be
|
||||||
|
// detected by calling IsLinkDestroyed() after EndNodeEditor().
|
||||||
|
ImNodesAttributeFlags_EnableLinkDetachWithDragClick = 1 << 0,
|
||||||
|
// Visual snapping of an in progress link will trigger IsLink Created/Destroyed events. Allows
|
||||||
|
// for previewing the creation of a link while dragging it across attributes. See here for demo:
|
||||||
|
// https://github.com/Nelarius/imnodes/issues/41#issuecomment-647132113 NOTE: the user has to
|
||||||
|
// actually delete the link for this to work. A deleted link can be detected by calling
|
||||||
|
// IsLinkDestroyed() after EndNodeEditor().
|
||||||
|
ImNodesAttributeFlags_EnableLinkCreationOnSnap = 1 << 1
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImNodesIO
|
||||||
|
{
|
||||||
|
struct EmulateThreeButtonMouse
|
||||||
|
{
|
||||||
|
EmulateThreeButtonMouse();
|
||||||
|
|
||||||
|
// The keyboard modifier to use in combination with mouse left click to pan the editor view.
|
||||||
|
// Set to NULL by default. To enable this feature, set the modifier to point to a boolean
|
||||||
|
// indicating the state of a modifier. For example,
|
||||||
|
//
|
||||||
|
// ImNodes::GetIO().EmulateThreeButtonMouse.Modifier = &ImGui::GetIO().KeyAlt;
|
||||||
|
const bool* Modifier;
|
||||||
|
} EmulateThreeButtonMouse;
|
||||||
|
|
||||||
|
struct LinkDetachWithModifierClick
|
||||||
|
{
|
||||||
|
LinkDetachWithModifierClick();
|
||||||
|
|
||||||
|
// Pointer to a boolean value indicating when the desired modifier is pressed. Set to NULL
|
||||||
|
// by default. To enable the feature, set the modifier to point to a boolean indicating the
|
||||||
|
// state of a modifier. For example,
|
||||||
|
//
|
||||||
|
// ImNodes::GetIO().LinkDetachWithModifierClick.Modifier = &ImGui::GetIO().KeyCtrl;
|
||||||
|
//
|
||||||
|
// Left-clicking a link with this modifier pressed will detach that link. NOTE: the user has
|
||||||
|
// to actually delete the link for this to work. A deleted link can be detected by calling
|
||||||
|
// IsLinkDestroyed() after EndNodeEditor().
|
||||||
|
const bool* Modifier;
|
||||||
|
} LinkDetachWithModifierClick;
|
||||||
|
|
||||||
|
struct MultipleSelectModifier
|
||||||
|
{
|
||||||
|
MultipleSelectModifier();
|
||||||
|
|
||||||
|
// Pointer to a boolean value indicating when the desired modifier is pressed. Set to NULL
|
||||||
|
// by default. To enable the feature, set the modifier to point to a boolean indicating the
|
||||||
|
// state of a modifier. For example,
|
||||||
|
//
|
||||||
|
// ImNodes::GetIO().MultipleSelectModifier.Modifier = &ImGui::GetIO().KeyCtrl;
|
||||||
|
//
|
||||||
|
// Left-clicking a node with this modifier pressed will add the node to the list of
|
||||||
|
// currently selected nodes. If this value is NULL, the Ctrl key will be used.
|
||||||
|
const bool* Modifier;
|
||||||
|
} MultipleSelectModifier;
|
||||||
|
|
||||||
|
// Holding alt mouse button pans the node area, by default middle mouse button will be used
|
||||||
|
// Set based on ImGuiMouseButton values
|
||||||
|
int AltMouseButton;
|
||||||
|
|
||||||
|
// Panning speed when dragging an element and mouse is outside the main editor view.
|
||||||
|
float AutoPanningSpeed;
|
||||||
|
|
||||||
|
ImNodesIO();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImNodesStyle
|
||||||
|
{
|
||||||
|
float GridSpacing;
|
||||||
|
|
||||||
|
float NodeCornerRounding;
|
||||||
|
ImVec2 NodePadding;
|
||||||
|
float NodeBorderThickness;
|
||||||
|
|
||||||
|
float LinkThickness;
|
||||||
|
float LinkLineSegmentsPerLength;
|
||||||
|
float LinkHoverDistance;
|
||||||
|
|
||||||
|
// The following variables control the look and behavior of the pins. The default size of each
|
||||||
|
// pin shape is balanced to occupy approximately the same surface area on the screen.
|
||||||
|
|
||||||
|
// The circle radius used when the pin shape is either ImNodesPinShape_Circle or
|
||||||
|
// ImNodesPinShape_CircleFilled.
|
||||||
|
float PinCircleRadius;
|
||||||
|
// The quad side length used when the shape is either ImNodesPinShape_Quad or
|
||||||
|
// ImNodesPinShape_QuadFilled.
|
||||||
|
float PinQuadSideLength;
|
||||||
|
// The equilateral triangle side length used when the pin shape is either
|
||||||
|
// ImNodesPinShape_Triangle or ImNodesPinShape_TriangleFilled.
|
||||||
|
float PinTriangleSideLength;
|
||||||
|
// The thickness of the line used when the pin shape is not filled.
|
||||||
|
float PinLineThickness;
|
||||||
|
// The radius from the pin's center position inside of which it is detected as being hovered
|
||||||
|
// over.
|
||||||
|
float PinHoverRadius;
|
||||||
|
// Offsets the pins' positions from the edge of the node to the outside of the node.
|
||||||
|
float PinOffset;
|
||||||
|
|
||||||
|
// Mini-map padding size between mini-map edge and mini-map content.
|
||||||
|
ImVec2 MiniMapPadding;
|
||||||
|
// Mini-map offset from the screen side.
|
||||||
|
ImVec2 MiniMapOffset;
|
||||||
|
|
||||||
|
// By default, ImNodesStyleFlags_NodeOutline and ImNodesStyleFlags_Gridlines are enabled.
|
||||||
|
ImNodesStyleFlags Flags;
|
||||||
|
// Set these mid-frame using Push/PopColorStyle. You can index this color array with with a
|
||||||
|
// ImNodesCol value.
|
||||||
|
unsigned int Colors[ImNodesCol_COUNT];
|
||||||
|
|
||||||
|
ImNodesStyle();
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImNodesMiniMapLocation_
|
||||||
|
{
|
||||||
|
ImNodesMiniMapLocation_BottomLeft,
|
||||||
|
ImNodesMiniMapLocation_BottomRight,
|
||||||
|
ImNodesMiniMapLocation_TopLeft,
|
||||||
|
ImNodesMiniMapLocation_TopRight,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImGuiContext;
|
||||||
|
struct ImVec2;
|
||||||
|
|
||||||
|
struct ImNodesContext;
|
||||||
|
|
||||||
|
// An editor context corresponds to a set of nodes in a single workspace (created with a single
|
||||||
|
// Begin/EndNodeEditor pair)
|
||||||
|
//
|
||||||
|
// By default, the library creates an editor context behind the scenes, so using any of the imnodes
|
||||||
|
// functions doesn't require you to explicitly create a context.
|
||||||
|
struct ImNodesEditorContext;
|
||||||
|
|
||||||
|
// Callback type used to specify special behavior when hovering a node in the minimap
|
||||||
|
#ifndef ImNodesMiniMapNodeHoveringCallback
|
||||||
|
typedef void (*ImNodesMiniMapNodeHoveringCallback)(int, void*);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef ImNodesMiniMapNodeHoveringCallbackUserData
|
||||||
|
typedef void* ImNodesMiniMapNodeHoveringCallbackUserData;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace IMNODES_NAMESPACE
|
||||||
|
{
|
||||||
|
// Call this function if you are compiling imnodes in to a dll, separate from ImGui. Calling this
|
||||||
|
// function sets the GImGui global variable, which is not shared across dll boundaries.
|
||||||
|
void SetImGuiContext(ImGuiContext* ctx);
|
||||||
|
|
||||||
|
ImNodesContext* CreateContext();
|
||||||
|
void DestroyContext(ImNodesContext* ctx = NULL); // NULL = destroy current context
|
||||||
|
ImNodesContext* GetCurrentContext();
|
||||||
|
void SetCurrentContext(ImNodesContext* ctx);
|
||||||
|
|
||||||
|
ImNodesEditorContext* EditorContextCreate();
|
||||||
|
void EditorContextFree(ImNodesEditorContext*);
|
||||||
|
void EditorContextSet(ImNodesEditorContext*);
|
||||||
|
ImVec2 EditorContextGetPanning();
|
||||||
|
void EditorContextResetPanning(const ImVec2& pos);
|
||||||
|
void EditorContextMoveToNode(const int node_id);
|
||||||
|
|
||||||
|
ImNodesIO& GetIO();
|
||||||
|
|
||||||
|
// Returns the global style struct. See the struct declaration for default values.
|
||||||
|
ImNodesStyle& GetStyle();
|
||||||
|
// Style presets matching the dear imgui styles of the same name. If dest is NULL, the active
|
||||||
|
// context's ImNodesStyle instance will be used as the destination.
|
||||||
|
void StyleColorsDark(ImNodesStyle* dest = NULL); // on by default
|
||||||
|
void StyleColorsClassic(ImNodesStyle* dest = NULL);
|
||||||
|
void StyleColorsLight(ImNodesStyle* dest = NULL);
|
||||||
|
|
||||||
|
// The top-level function call. Call this before calling BeginNode/EndNode. Calling this function
|
||||||
|
// will result the node editor grid workspace being rendered.
|
||||||
|
void BeginNodeEditor();
|
||||||
|
void EndNodeEditor();
|
||||||
|
|
||||||
|
// Add a navigable minimap to the editor; call before EndNodeEditor after all
|
||||||
|
// nodes and links have been specified
|
||||||
|
void MiniMap(
|
||||||
|
const float minimap_size_fraction = 0.2f,
|
||||||
|
const ImNodesMiniMapLocation location = ImNodesMiniMapLocation_TopLeft,
|
||||||
|
const ImNodesMiniMapNodeHoveringCallback node_hovering_callback = NULL,
|
||||||
|
const ImNodesMiniMapNodeHoveringCallbackUserData node_hovering_callback_data = NULL);
|
||||||
|
|
||||||
|
// Use PushColorStyle and PopColorStyle to modify ImNodesStyle::Colors mid-frame.
|
||||||
|
void PushColorStyle(ImNodesCol item, unsigned int color);
|
||||||
|
void PopColorStyle();
|
||||||
|
void PushStyleVar(ImNodesStyleVar style_item, float value);
|
||||||
|
void PushStyleVar(ImNodesStyleVar style_item, const ImVec2& value);
|
||||||
|
void PopStyleVar(int count = 1);
|
||||||
|
|
||||||
|
// id can be any positive or negative integer, but INT_MIN is currently reserved for internal use.
|
||||||
|
void BeginNode(int id);
|
||||||
|
void EndNode();
|
||||||
|
|
||||||
|
ImVec2 GetNodeDimensions(int id);
|
||||||
|
|
||||||
|
// Place your node title bar content (such as the node title, using ImGui::Text) between the
|
||||||
|
// following function calls. These functions have to be called before adding any attributes, or the
|
||||||
|
// layout of the node will be incorrect.
|
||||||
|
void BeginNodeTitleBar();
|
||||||
|
void EndNodeTitleBar();
|
||||||
|
|
||||||
|
// Attributes are ImGui UI elements embedded within the node. Attributes can have pin shapes
|
||||||
|
// rendered next to them. Links are created between pins.
|
||||||
|
//
|
||||||
|
// The activity status of an attribute can be checked via the IsAttributeActive() and
|
||||||
|
// IsAnyAttributeActive() function calls. This is one easy way of checking for any changes made to
|
||||||
|
// an attribute's drag float UI, for instance.
|
||||||
|
//
|
||||||
|
// Each attribute id must be unique.
|
||||||
|
|
||||||
|
// Create an input attribute block. The pin is rendered on left side.
|
||||||
|
void BeginInputAttribute(int id, ImNodesPinShape shape = ImNodesPinShape_CircleFilled);
|
||||||
|
void EndInputAttribute();
|
||||||
|
// Create an output attribute block. The pin is rendered on the right side.
|
||||||
|
void BeginOutputAttribute(int id, ImNodesPinShape shape = ImNodesPinShape_CircleFilled);
|
||||||
|
void EndOutputAttribute();
|
||||||
|
// Create a static attribute block. A static attribute has no pin, and therefore can't be linked to
|
||||||
|
// anything. However, you can still use IsAttributeActive() and IsAnyAttributeActive() to check for
|
||||||
|
// attribute activity.
|
||||||
|
void BeginStaticAttribute(int id);
|
||||||
|
void EndStaticAttribute();
|
||||||
|
|
||||||
|
// Push a single AttributeFlags value. By default, only AttributeFlags_None is set.
|
||||||
|
void PushAttributeFlag(ImNodesAttributeFlags flag);
|
||||||
|
void PopAttributeFlag();
|
||||||
|
|
||||||
|
// Render a link between attributes.
|
||||||
|
// The attributes ids used here must match the ids used in Begin(Input|Output)Attribute function
|
||||||
|
// calls. The order of start_attr and end_attr doesn't make a difference for rendering the link.
|
||||||
|
void Link(int id, int start_attribute_id, int end_attribute_id);
|
||||||
|
|
||||||
|
// Enable or disable the ability to click and drag a specific node.
|
||||||
|
void SetNodeDraggable(int node_id, const bool draggable);
|
||||||
|
|
||||||
|
// The node's position can be expressed in three coordinate systems:
|
||||||
|
// * screen space coordinates, -- the origin is the upper left corner of the window.
|
||||||
|
// * editor space coordinates -- the origin is the upper left corner of the node editor window
|
||||||
|
// * grid space coordinates, -- the origin is the upper left corner of the node editor window,
|
||||||
|
// translated by the current editor panning vector (see EditorContextGetPanning() and
|
||||||
|
// EditorContextResetPanning())
|
||||||
|
|
||||||
|
// Use the following functions to get and set the node's coordinates in these coordinate systems.
|
||||||
|
|
||||||
|
void SetNodeScreenSpacePos(int node_id, const ImVec2& screen_space_pos);
|
||||||
|
void SetNodeEditorSpacePos(int node_id, const ImVec2& editor_space_pos);
|
||||||
|
void SetNodeGridSpacePos(int node_id, const ImVec2& grid_pos);
|
||||||
|
|
||||||
|
ImVec2 GetNodeScreenSpacePos(const int node_id);
|
||||||
|
ImVec2 GetNodeEditorSpacePos(const int node_id);
|
||||||
|
ImVec2 GetNodeGridSpacePos(const int node_id);
|
||||||
|
|
||||||
|
// If ImNodesStyleFlags_GridSnapping is enabled, snap the specified node's origin to the grid.
|
||||||
|
void SnapNodeToGrid(int node_id);
|
||||||
|
|
||||||
|
// Returns true if the current node editor canvas is being hovered over by the mouse, and is not
|
||||||
|
// blocked by any other windows.
|
||||||
|
bool IsEditorHovered();
|
||||||
|
// The following functions return true if a UI element is being hovered over by the mouse cursor.
|
||||||
|
// Assigns the id of the UI element being hovered over to the function argument. Use these functions
|
||||||
|
// after EndNodeEditor() has been called.
|
||||||
|
bool IsNodeHovered(int* node_id);
|
||||||
|
bool IsLinkHovered(int* link_id);
|
||||||
|
bool IsPinHovered(int* attribute_id);
|
||||||
|
|
||||||
|
// Use The following two functions to query the number of selected nodes or links in the current
|
||||||
|
// editor. Use after calling EndNodeEditor().
|
||||||
|
int NumSelectedNodes();
|
||||||
|
int NumSelectedLinks();
|
||||||
|
// Get the selected node/link ids. The pointer argument should point to an integer array with at
|
||||||
|
// least as many elements as the respective NumSelectedNodes/NumSelectedLinks function call
|
||||||
|
// returned.
|
||||||
|
void GetSelectedNodes(int* node_ids);
|
||||||
|
void GetSelectedLinks(int* link_ids);
|
||||||
|
// Clears the list of selected nodes/links. Useful if you want to delete a selected node or link.
|
||||||
|
void ClearNodeSelection();
|
||||||
|
void ClearLinkSelection();
|
||||||
|
// Use the following functions to add or remove individual nodes or links from the current editors
|
||||||
|
// selection. Note that all functions require the id to be an existing valid id for this editor.
|
||||||
|
// Select-functions has the precondition that the object is currently considered unselected.
|
||||||
|
// Clear-functions has the precondition that the object is currently considered selected.
|
||||||
|
// Preconditions listed above can be checked via IsNodeSelected/IsLinkSelected if not already
|
||||||
|
// known.
|
||||||
|
void SelectNode(int node_id);
|
||||||
|
void ClearNodeSelection(int node_id);
|
||||||
|
bool IsNodeSelected(int node_id);
|
||||||
|
void SelectLink(int link_id);
|
||||||
|
void ClearLinkSelection(int link_id);
|
||||||
|
bool IsLinkSelected(int link_id);
|
||||||
|
|
||||||
|
// Was the previous attribute active? This will continuously return true while the left mouse button
|
||||||
|
// is being pressed over the UI content of the attribute.
|
||||||
|
bool IsAttributeActive();
|
||||||
|
// Was any attribute active? If so, sets the active attribute id to the output function argument.
|
||||||
|
bool IsAnyAttributeActive(int* attribute_id = NULL);
|
||||||
|
|
||||||
|
// Use the following functions to query a change of state for an existing link, or new link. Call
|
||||||
|
// these after EndNodeEditor().
|
||||||
|
|
||||||
|
// Did the user start dragging a new link from a pin?
|
||||||
|
bool IsLinkStarted(int* started_at_attribute_id);
|
||||||
|
// Did the user drop the dragged link before attaching it to a pin?
|
||||||
|
// There are two different kinds of situations to consider when handling this event:
|
||||||
|
// 1) a link which is created at a pin and then dropped
|
||||||
|
// 2) an existing link which is detached from a pin and then dropped
|
||||||
|
// Use the including_detached_links flag to control whether this function triggers when the user
|
||||||
|
// detaches a link and drops it.
|
||||||
|
bool IsLinkDropped(int* started_at_attribute_id = NULL, bool including_detached_links = true);
|
||||||
|
// Did the user finish creating a new link?
|
||||||
|
bool IsLinkCreated(
|
||||||
|
int* started_at_attribute_id,
|
||||||
|
int* ended_at_attribute_id,
|
||||||
|
bool* created_from_snap = NULL);
|
||||||
|
bool IsLinkCreated(
|
||||||
|
int* started_at_node_id,
|
||||||
|
int* started_at_attribute_id,
|
||||||
|
int* ended_at_node_id,
|
||||||
|
int* ended_at_attribute_id,
|
||||||
|
bool* created_from_snap = NULL);
|
||||||
|
|
||||||
|
// Was an existing link detached from a pin by the user? The detached link's id is assigned to the
|
||||||
|
// output argument link_id.
|
||||||
|
bool IsLinkDestroyed(int* link_id);
|
||||||
|
|
||||||
|
// Use the following functions to write the editor context's state to a string, or directly to a
|
||||||
|
// file. The editor context is serialized in the INI file format.
|
||||||
|
|
||||||
|
const char* SaveCurrentEditorStateToIniString(size_t* data_size = NULL);
|
||||||
|
const char* SaveEditorStateToIniString(
|
||||||
|
const ImNodesEditorContext* editor,
|
||||||
|
size_t* data_size = NULL);
|
||||||
|
|
||||||
|
void LoadCurrentEditorStateFromIniString(const char* data, size_t data_size);
|
||||||
|
void LoadEditorStateFromIniString(ImNodesEditorContext* editor, const char* data, size_t data_size);
|
||||||
|
|
||||||
|
void SaveCurrentEditorStateToIniFile(const char* file_name);
|
||||||
|
void SaveEditorStateToIniFile(const ImNodesEditorContext* editor, const char* file_name);
|
||||||
|
|
||||||
|
void LoadCurrentEditorStateFromIniFile(const char* file_name);
|
||||||
|
void LoadEditorStateFromIniFile(ImNodesEditorContext* editor, const char* file_name);
|
||||||
|
} // namespace IMNODES_NAMESPACE
|
||||||
500
imgui/imnodes_internal.h
Normal file
500
imgui/imnodes_internal.h
Normal file
@@ -0,0 +1,500 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define IMGUI_DEFINE_MATH_OPERATORS
|
||||||
|
#include <imgui.h>
|
||||||
|
#include <imgui_internal.h>
|
||||||
|
|
||||||
|
#include "imnodes.h"
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
// the structure of this file:
|
||||||
|
//
|
||||||
|
// [SECTION] internal enums
|
||||||
|
// [SECTION] internal data structures
|
||||||
|
// [SECTION] global and editor context structs
|
||||||
|
// [SECTION] object pool implementation
|
||||||
|
|
||||||
|
struct ImNodesContext;
|
||||||
|
|
||||||
|
extern ImNodesContext* GImNodes;
|
||||||
|
|
||||||
|
// [SECTION] internal enums
|
||||||
|
|
||||||
|
typedef int ImNodesScope;
|
||||||
|
typedef int ImNodesAttributeType;
|
||||||
|
typedef int ImNodesUIState;
|
||||||
|
typedef int ImNodesClickInteractionType;
|
||||||
|
typedef int ImNodesLinkCreationType;
|
||||||
|
|
||||||
|
enum ImNodesScope_
|
||||||
|
{
|
||||||
|
ImNodesScope_None = 1,
|
||||||
|
ImNodesScope_Editor = 1 << 1,
|
||||||
|
ImNodesScope_Node = 1 << 2,
|
||||||
|
ImNodesScope_Attribute = 1 << 3
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImNodesAttributeType_
|
||||||
|
{
|
||||||
|
ImNodesAttributeType_None,
|
||||||
|
ImNodesAttributeType_Input,
|
||||||
|
ImNodesAttributeType_Output
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImNodesUIState_
|
||||||
|
{
|
||||||
|
ImNodesUIState_None = 0,
|
||||||
|
ImNodesUIState_LinkStarted = 1 << 0,
|
||||||
|
ImNodesUIState_LinkDropped = 1 << 1,
|
||||||
|
ImNodesUIState_LinkCreated = 1 << 2
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImNodesClickInteractionType_
|
||||||
|
{
|
||||||
|
ImNodesClickInteractionType_Node,
|
||||||
|
ImNodesClickInteractionType_Link,
|
||||||
|
ImNodesClickInteractionType_LinkCreation,
|
||||||
|
ImNodesClickInteractionType_Panning,
|
||||||
|
ImNodesClickInteractionType_BoxSelection,
|
||||||
|
ImNodesClickInteractionType_ImGuiItem,
|
||||||
|
ImNodesClickInteractionType_None
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImNodesLinkCreationType_
|
||||||
|
{
|
||||||
|
ImNodesLinkCreationType_Standard,
|
||||||
|
ImNodesLinkCreationType_FromDetach
|
||||||
|
};
|
||||||
|
|
||||||
|
// [SECTION] internal data structures
|
||||||
|
|
||||||
|
// The object T must have the following interface:
|
||||||
|
//
|
||||||
|
// struct T
|
||||||
|
// {
|
||||||
|
// T();
|
||||||
|
//
|
||||||
|
// int id;
|
||||||
|
// };
|
||||||
|
template<typename T>
|
||||||
|
struct ImObjectPool
|
||||||
|
{
|
||||||
|
ImVector<T> Pool;
|
||||||
|
ImVector<bool> InUse;
|
||||||
|
ImVector<int> FreeList;
|
||||||
|
ImGuiStorage IdMap;
|
||||||
|
|
||||||
|
ImObjectPool() : Pool(), InUse(), FreeList(), IdMap() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Emulates std::optional<int> using the sentinel value `INVALID_INDEX`.
|
||||||
|
struct ImOptionalIndex
|
||||||
|
{
|
||||||
|
ImOptionalIndex() : _Index(INVALID_INDEX) {}
|
||||||
|
ImOptionalIndex(const int value) : _Index(value) {}
|
||||||
|
|
||||||
|
// Observers
|
||||||
|
|
||||||
|
inline bool HasValue() const { return _Index != INVALID_INDEX; }
|
||||||
|
|
||||||
|
inline int Value() const
|
||||||
|
{
|
||||||
|
IM_ASSERT(HasValue());
|
||||||
|
return _Index;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modifiers
|
||||||
|
|
||||||
|
inline ImOptionalIndex& operator=(const int value)
|
||||||
|
{
|
||||||
|
_Index = value;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Reset() { _Index = INVALID_INDEX; }
|
||||||
|
|
||||||
|
inline bool operator==(const ImOptionalIndex& rhs) const { return _Index == rhs._Index; }
|
||||||
|
|
||||||
|
inline bool operator==(const int rhs) const { return _Index == rhs; }
|
||||||
|
|
||||||
|
inline bool operator!=(const ImOptionalIndex& rhs) const { return _Index != rhs._Index; }
|
||||||
|
|
||||||
|
inline bool operator!=(const int rhs) const { return _Index != rhs; }
|
||||||
|
|
||||||
|
static const int INVALID_INDEX = -1;
|
||||||
|
|
||||||
|
private:
|
||||||
|
int _Index;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImNodeData
|
||||||
|
{
|
||||||
|
int Id;
|
||||||
|
ImVec2 Origin; // The node origin is in editor space
|
||||||
|
ImRect TitleBarContentRect;
|
||||||
|
ImRect Rect;
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
ImU32 Background, BackgroundHovered, BackgroundSelected, Outline, Titlebar, TitlebarHovered,
|
||||||
|
TitlebarSelected;
|
||||||
|
} ColorStyle;
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float CornerRounding;
|
||||||
|
ImVec2 Padding;
|
||||||
|
float BorderThickness;
|
||||||
|
} LayoutStyle;
|
||||||
|
|
||||||
|
ImVector<int> PinIndices;
|
||||||
|
bool Draggable;
|
||||||
|
|
||||||
|
ImNodeData(const int node_id)
|
||||||
|
: Id(node_id), Origin(0.0f, 0.0f), TitleBarContentRect(),
|
||||||
|
Rect(ImVec2(0.0f, 0.0f), ImVec2(0.0f, 0.0f)), ColorStyle(), LayoutStyle(), PinIndices(),
|
||||||
|
Draggable(true)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~ImNodeData() { Id = INT_MIN; }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImPinData
|
||||||
|
{
|
||||||
|
int Id;
|
||||||
|
int ParentNodeIdx;
|
||||||
|
ImRect AttributeRect;
|
||||||
|
ImNodesAttributeType Type;
|
||||||
|
ImNodesPinShape Shape;
|
||||||
|
ImVec2 Pos; // screen-space coordinates
|
||||||
|
int Flags;
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
ImU32 Background, Hovered;
|
||||||
|
} ColorStyle;
|
||||||
|
|
||||||
|
ImPinData(const int pin_id)
|
||||||
|
: Id(pin_id), ParentNodeIdx(), AttributeRect(), Type(ImNodesAttributeType_None),
|
||||||
|
Shape(ImNodesPinShape_CircleFilled), Pos(), Flags(ImNodesAttributeFlags_None),
|
||||||
|
ColorStyle()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImLinkData
|
||||||
|
{
|
||||||
|
int Id;
|
||||||
|
int StartPinIdx, EndPinIdx;
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
ImU32 Base, Hovered, Selected;
|
||||||
|
} ColorStyle;
|
||||||
|
|
||||||
|
ImLinkData(const int link_id) : Id(link_id), StartPinIdx(), EndPinIdx(), ColorStyle() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImClickInteractionState
|
||||||
|
{
|
||||||
|
ImNodesClickInteractionType Type;
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
int StartPinIdx;
|
||||||
|
ImOptionalIndex EndPinIdx;
|
||||||
|
ImNodesLinkCreationType Type;
|
||||||
|
} LinkCreation;
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
ImRect Rect; // Coordinates in grid space
|
||||||
|
} BoxSelector;
|
||||||
|
|
||||||
|
ImClickInteractionState() : Type(ImNodesClickInteractionType_None) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImNodesColElement
|
||||||
|
{
|
||||||
|
ImU32 Color;
|
||||||
|
ImNodesCol Item;
|
||||||
|
|
||||||
|
ImNodesColElement(const ImU32 c, const ImNodesCol s) : Color(c), Item(s) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImNodesStyleVarElement
|
||||||
|
{
|
||||||
|
ImNodesStyleVar Item;
|
||||||
|
float FloatValue[2];
|
||||||
|
|
||||||
|
ImNodesStyleVarElement(const ImNodesStyleVar variable, const float value) : Item(variable)
|
||||||
|
{
|
||||||
|
FloatValue[0] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImNodesStyleVarElement(const ImNodesStyleVar variable, const ImVec2 value) : Item(variable)
|
||||||
|
{
|
||||||
|
FloatValue[0] = value.x;
|
||||||
|
FloatValue[1] = value.y;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// [SECTION] global and editor context structs
|
||||||
|
|
||||||
|
struct ImNodesEditorContext
|
||||||
|
{
|
||||||
|
ImObjectPool<ImNodeData> Nodes;
|
||||||
|
ImObjectPool<ImPinData> Pins;
|
||||||
|
ImObjectPool<ImLinkData> Links;
|
||||||
|
|
||||||
|
ImVector<int> NodeDepthOrder;
|
||||||
|
|
||||||
|
// ui related fields
|
||||||
|
ImVec2 Panning;
|
||||||
|
ImVec2 AutoPanningDelta;
|
||||||
|
// Minimum and maximum extents of all content in grid space. Valid after final
|
||||||
|
// ImNodes::EndNode() call.
|
||||||
|
ImRect GridContentBounds;
|
||||||
|
|
||||||
|
ImVector<int> SelectedNodeIndices;
|
||||||
|
ImVector<int> SelectedLinkIndices;
|
||||||
|
|
||||||
|
// Relative origins of selected nodes for snapping of dragged nodes
|
||||||
|
ImVector<ImVec2> SelectedNodeOffsets;
|
||||||
|
// Offset of the primary node origin relative to the mouse cursor.
|
||||||
|
ImVec2 PrimaryNodeOffset;
|
||||||
|
|
||||||
|
ImClickInteractionState ClickInteraction;
|
||||||
|
|
||||||
|
// Mini-map state set by MiniMap()
|
||||||
|
|
||||||
|
bool MiniMapEnabled;
|
||||||
|
ImNodesMiniMapLocation MiniMapLocation;
|
||||||
|
float MiniMapSizeFraction;
|
||||||
|
ImNodesMiniMapNodeHoveringCallback MiniMapNodeHoveringCallback;
|
||||||
|
ImNodesMiniMapNodeHoveringCallbackUserData MiniMapNodeHoveringCallbackUserData;
|
||||||
|
|
||||||
|
// Mini-map state set during EndNodeEditor() call
|
||||||
|
|
||||||
|
ImRect MiniMapRectScreenSpace;
|
||||||
|
ImRect MiniMapContentScreenSpace;
|
||||||
|
float MiniMapScaling;
|
||||||
|
|
||||||
|
ImNodesEditorContext()
|
||||||
|
: Nodes(), Pins(), Links(), Panning(0.f, 0.f), SelectedNodeIndices(), SelectedLinkIndices(),
|
||||||
|
SelectedNodeOffsets(), PrimaryNodeOffset(0.f, 0.f), ClickInteraction(),
|
||||||
|
MiniMapEnabled(false), MiniMapSizeFraction(0.0f), MiniMapNodeHoveringCallback(NULL),
|
||||||
|
MiniMapNodeHoveringCallbackUserData(NULL), MiniMapScaling(0.0f)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImNodesContext
|
||||||
|
{
|
||||||
|
ImNodesEditorContext* DefaultEditorCtx;
|
||||||
|
ImNodesEditorContext* EditorCtx;
|
||||||
|
|
||||||
|
// Canvas draw list and helper state
|
||||||
|
ImDrawList* CanvasDrawList;
|
||||||
|
ImGuiStorage NodeIdxToSubmissionIdx;
|
||||||
|
ImVector<int> NodeIdxSubmissionOrder;
|
||||||
|
ImVector<int> NodeIndicesOverlappingWithMouse;
|
||||||
|
ImVector<int> OccludedPinIndices;
|
||||||
|
|
||||||
|
// Canvas extents
|
||||||
|
ImVec2 CanvasOriginScreenSpace;
|
||||||
|
ImRect CanvasRectScreenSpace;
|
||||||
|
|
||||||
|
// Debug helpers
|
||||||
|
ImNodesScope CurrentScope;
|
||||||
|
|
||||||
|
// Configuration state
|
||||||
|
ImNodesIO Io;
|
||||||
|
ImNodesStyle Style;
|
||||||
|
ImVector<ImNodesColElement> ColorModifierStack;
|
||||||
|
ImVector<ImNodesStyleVarElement> StyleModifierStack;
|
||||||
|
ImGuiTextBuffer TextBuffer;
|
||||||
|
|
||||||
|
int CurrentAttributeFlags;
|
||||||
|
ImVector<int> AttributeFlagStack;
|
||||||
|
|
||||||
|
// UI element state
|
||||||
|
int CurrentNodeIdx;
|
||||||
|
int CurrentPinIdx;
|
||||||
|
int CurrentAttributeId;
|
||||||
|
|
||||||
|
ImOptionalIndex HoveredNodeIdx;
|
||||||
|
ImOptionalIndex HoveredLinkIdx;
|
||||||
|
ImOptionalIndex HoveredPinIdx;
|
||||||
|
|
||||||
|
ImOptionalIndex DeletedLinkIdx;
|
||||||
|
ImOptionalIndex SnapLinkIdx;
|
||||||
|
|
||||||
|
// Event helper state
|
||||||
|
// TODO: this should be a part of a state machine, and not a member of the global struct.
|
||||||
|
// Unclear what parts of the code this relates to.
|
||||||
|
int ImNodesUIState;
|
||||||
|
|
||||||
|
int ActiveAttributeId;
|
||||||
|
bool ActiveAttribute;
|
||||||
|
|
||||||
|
// ImGui::IO cache
|
||||||
|
|
||||||
|
ImVec2 MousePos;
|
||||||
|
|
||||||
|
bool LeftMouseClicked;
|
||||||
|
bool LeftMouseReleased;
|
||||||
|
bool AltMouseClicked;
|
||||||
|
bool LeftMouseDragging;
|
||||||
|
bool AltMouseDragging;
|
||||||
|
float AltMouseScrollDelta;
|
||||||
|
bool MultipleSelectModifier;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace IMNODES_NAMESPACE
|
||||||
|
{
|
||||||
|
static inline ImNodesEditorContext& EditorContextGet()
|
||||||
|
{
|
||||||
|
// No editor context was set! Did you forget to call ImNodes::CreateContext()?
|
||||||
|
IM_ASSERT(GImNodes->EditorCtx != NULL);
|
||||||
|
return *GImNodes->EditorCtx;
|
||||||
|
}
|
||||||
|
|
||||||
|
// [SECTION] ObjectPool implementation
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static inline int ObjectPoolFind(const ImObjectPool<T>& objects, const int id)
|
||||||
|
{
|
||||||
|
const int index = objects.IdMap.GetInt(static_cast<ImGuiID>(id), -1);
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static inline void ObjectPoolUpdate(ImObjectPool<T>& objects)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < objects.InUse.size(); ++i)
|
||||||
|
{
|
||||||
|
const int id = objects.Pool[i].Id;
|
||||||
|
|
||||||
|
if (!objects.InUse[i] && objects.IdMap.GetInt(id, -1) == i)
|
||||||
|
{
|
||||||
|
objects.IdMap.SetInt(id, -1);
|
||||||
|
objects.FreeList.push_back(i);
|
||||||
|
(objects.Pool.Data + i)->~T();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline void ObjectPoolUpdate(ImObjectPool<ImNodeData>& nodes)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < nodes.InUse.size(); ++i)
|
||||||
|
{
|
||||||
|
if (nodes.InUse[i])
|
||||||
|
{
|
||||||
|
nodes.Pool[i].PinIndices.clear();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const int id = nodes.Pool[i].Id;
|
||||||
|
|
||||||
|
if (nodes.IdMap.GetInt(id, -1) == i)
|
||||||
|
{
|
||||||
|
// Remove node idx form depth stack the first time we detect that this idx slot is
|
||||||
|
// unused
|
||||||
|
ImVector<int>& depth_stack = EditorContextGet().NodeDepthOrder;
|
||||||
|
const int* const elem = depth_stack.find(i);
|
||||||
|
IM_ASSERT(elem != depth_stack.end());
|
||||||
|
depth_stack.erase(elem);
|
||||||
|
|
||||||
|
nodes.IdMap.SetInt(id, -1);
|
||||||
|
nodes.FreeList.push_back(i);
|
||||||
|
(nodes.Pool.Data + i)->~ImNodeData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static inline void ObjectPoolReset(ImObjectPool<T>& objects)
|
||||||
|
{
|
||||||
|
if (!objects.InUse.empty())
|
||||||
|
{
|
||||||
|
memset(objects.InUse.Data, 0, objects.InUse.size_in_bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static inline int ObjectPoolFindOrCreateIndex(ImObjectPool<T>& objects, const int id)
|
||||||
|
{
|
||||||
|
int index = objects.IdMap.GetInt(static_cast<ImGuiID>(id), -1);
|
||||||
|
|
||||||
|
// Construct new object
|
||||||
|
if (index == -1)
|
||||||
|
{
|
||||||
|
if (objects.FreeList.empty())
|
||||||
|
{
|
||||||
|
index = objects.Pool.size();
|
||||||
|
IM_ASSERT(objects.Pool.size() == objects.InUse.size());
|
||||||
|
const int new_size = objects.Pool.size() + 1;
|
||||||
|
objects.Pool.resize(new_size);
|
||||||
|
objects.InUse.resize(new_size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
index = objects.FreeList.back();
|
||||||
|
objects.FreeList.pop_back();
|
||||||
|
}
|
||||||
|
IM_PLACEMENT_NEW(objects.Pool.Data + index) T(id);
|
||||||
|
objects.IdMap.SetInt(static_cast<ImGuiID>(id), index);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flag it as used
|
||||||
|
objects.InUse[index] = true;
|
||||||
|
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline int ObjectPoolFindOrCreateIndex(ImObjectPool<ImNodeData>& nodes, const int node_id)
|
||||||
|
{
|
||||||
|
int node_idx = nodes.IdMap.GetInt(static_cast<ImGuiID>(node_id), -1);
|
||||||
|
|
||||||
|
// Construct new node
|
||||||
|
if (node_idx == -1)
|
||||||
|
{
|
||||||
|
if (nodes.FreeList.empty())
|
||||||
|
{
|
||||||
|
node_idx = nodes.Pool.size();
|
||||||
|
IM_ASSERT(nodes.Pool.size() == nodes.InUse.size());
|
||||||
|
const int new_size = nodes.Pool.size() + 1;
|
||||||
|
nodes.Pool.resize(new_size);
|
||||||
|
nodes.InUse.resize(new_size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
node_idx = nodes.FreeList.back();
|
||||||
|
nodes.FreeList.pop_back();
|
||||||
|
}
|
||||||
|
IM_PLACEMENT_NEW(nodes.Pool.Data + node_idx) ImNodeData(node_id);
|
||||||
|
nodes.IdMap.SetInt(static_cast<ImGuiID>(node_id), node_idx);
|
||||||
|
|
||||||
|
ImNodesEditorContext& editor = EditorContextGet();
|
||||||
|
editor.NodeDepthOrder.push_back(node_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flag node as used
|
||||||
|
nodes.InUse[node_idx] = true;
|
||||||
|
|
||||||
|
return node_idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static inline T& ObjectPoolFindOrCreateObject(ImObjectPool<T>& objects, const int id)
|
||||||
|
{
|
||||||
|
const int index = ObjectPoolFindOrCreateIndex(objects, id);
|
||||||
|
return objects.Pool[index];
|
||||||
|
}
|
||||||
|
} // namespace IMNODES_NAMESPACE
|
||||||
5885
imgui/implot.cpp
Normal file
5885
imgui/implot.cpp
Normal file
File diff suppressed because it is too large
Load Diff
1297
imgui/implot.h
Normal file
1297
imgui/implot.h
Normal file
File diff suppressed because it is too large
Load Diff
1669
imgui/implot_internal.h
Normal file
1669
imgui/implot_internal.h
Normal file
File diff suppressed because it is too large
Load Diff
2808
imgui/implot_items.cpp
Normal file
2808
imgui/implot_items.cpp
Normal file
File diff suppressed because it is too large
Load Diff
627
imgui/imstb_rectpack.h
Normal file
627
imgui/imstb_rectpack.h
Normal file
@@ -0,0 +1,627 @@
|
|||||||
|
// [DEAR IMGUI]
|
||||||
|
// This is a slightly modified version of stb_rect_pack.h 1.01.
|
||||||
|
// Grep for [DEAR IMGUI] to find the changes.
|
||||||
|
//
|
||||||
|
// stb_rect_pack.h - v1.01 - public domain - rectangle packing
|
||||||
|
// Sean Barrett 2014
|
||||||
|
//
|
||||||
|
// Useful for e.g. packing rectangular textures into an atlas.
|
||||||
|
// Does not do rotation.
|
||||||
|
//
|
||||||
|
// Before #including,
|
||||||
|
//
|
||||||
|
// #define STB_RECT_PACK_IMPLEMENTATION
|
||||||
|
//
|
||||||
|
// in the file that you want to have the implementation.
|
||||||
|
//
|
||||||
|
// Not necessarily the awesomest packing method, but better than
|
||||||
|
// the totally naive one in stb_truetype (which is primarily what
|
||||||
|
// this is meant to replace).
|
||||||
|
//
|
||||||
|
// Has only had a few tests run, may have issues.
|
||||||
|
//
|
||||||
|
// More docs to come.
|
||||||
|
//
|
||||||
|
// No memory allocations; uses qsort() and assert() from stdlib.
|
||||||
|
// Can override those by defining STBRP_SORT and STBRP_ASSERT.
|
||||||
|
//
|
||||||
|
// This library currently uses the Skyline Bottom-Left algorithm.
|
||||||
|
//
|
||||||
|
// Please note: better rectangle packers are welcome! Please
|
||||||
|
// implement them to the same API, but with a different init
|
||||||
|
// function.
|
||||||
|
//
|
||||||
|
// Credits
|
||||||
|
//
|
||||||
|
// Library
|
||||||
|
// Sean Barrett
|
||||||
|
// Minor features
|
||||||
|
// Martins Mozeiko
|
||||||
|
// github:IntellectualKitty
|
||||||
|
//
|
||||||
|
// Bugfixes / warning fixes
|
||||||
|
// Jeremy Jaussaud
|
||||||
|
// Fabian Giesen
|
||||||
|
//
|
||||||
|
// Version history:
|
||||||
|
//
|
||||||
|
// 1.01 (2021-07-11) always use large rect mode, expose STBRP__MAXVAL in public section
|
||||||
|
// 1.00 (2019-02-25) avoid small space waste; gracefully fail too-wide rectangles
|
||||||
|
// 0.99 (2019-02-07) warning fixes
|
||||||
|
// 0.11 (2017-03-03) return packing success/fail result
|
||||||
|
// 0.10 (2016-10-25) remove cast-away-const to avoid warnings
|
||||||
|
// 0.09 (2016-08-27) fix compiler warnings
|
||||||
|
// 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0)
|
||||||
|
// 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0)
|
||||||
|
// 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort
|
||||||
|
// 0.05: added STBRP_ASSERT to allow replacing assert
|
||||||
|
// 0.04: fixed minor bug in STBRP_LARGE_RECTS support
|
||||||
|
// 0.01: initial release
|
||||||
|
//
|
||||||
|
// LICENSE
|
||||||
|
//
|
||||||
|
// See end of file for license information.
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// INCLUDE SECTION
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef STB_INCLUDE_STB_RECT_PACK_H
|
||||||
|
#define STB_INCLUDE_STB_RECT_PACK_H
|
||||||
|
|
||||||
|
#define STB_RECT_PACK_VERSION 1
|
||||||
|
|
||||||
|
#ifdef STBRP_STATIC
|
||||||
|
#define STBRP_DEF static
|
||||||
|
#else
|
||||||
|
#define STBRP_DEF extern
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct stbrp_context stbrp_context;
|
||||||
|
typedef struct stbrp_node stbrp_node;
|
||||||
|
typedef struct stbrp_rect stbrp_rect;
|
||||||
|
|
||||||
|
typedef int stbrp_coord;
|
||||||
|
|
||||||
|
#define STBRP__MAXVAL 0x7fffffff
|
||||||
|
// Mostly for internal use, but this is the maximum supported coordinate value.
|
||||||
|
|
||||||
|
STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
|
||||||
|
// Assign packed locations to rectangles. The rectangles are of type
|
||||||
|
// 'stbrp_rect' defined below, stored in the array 'rects', and there
|
||||||
|
// are 'num_rects' many of them.
|
||||||
|
//
|
||||||
|
// Rectangles which are successfully packed have the 'was_packed' flag
|
||||||
|
// set to a non-zero value and 'x' and 'y' store the minimum location
|
||||||
|
// on each axis (i.e. bottom-left in cartesian coordinates, top-left
|
||||||
|
// if you imagine y increasing downwards). Rectangles which do not fit
|
||||||
|
// have the 'was_packed' flag set to 0.
|
||||||
|
//
|
||||||
|
// You should not try to access the 'rects' array from another thread
|
||||||
|
// while this function is running, as the function temporarily reorders
|
||||||
|
// the array while it executes.
|
||||||
|
//
|
||||||
|
// To pack into another rectangle, you need to call stbrp_init_target
|
||||||
|
// again. To continue packing into the same rectangle, you can call
|
||||||
|
// this function again. Calling this multiple times with multiple rect
|
||||||
|
// arrays will probably produce worse packing results than calling it
|
||||||
|
// a single time with the full rectangle array, but the option is
|
||||||
|
// available.
|
||||||
|
//
|
||||||
|
// The function returns 1 if all of the rectangles were successfully
|
||||||
|
// packed and 0 otherwise.
|
||||||
|
|
||||||
|
struct stbrp_rect
|
||||||
|
{
|
||||||
|
// reserved for your use:
|
||||||
|
int id;
|
||||||
|
|
||||||
|
// input:
|
||||||
|
stbrp_coord w, h;
|
||||||
|
|
||||||
|
// output:
|
||||||
|
stbrp_coord x, y;
|
||||||
|
int was_packed; // non-zero if valid packing
|
||||||
|
|
||||||
|
}; // 16 bytes, nominally
|
||||||
|
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
|
||||||
|
// Initialize a rectangle packer to:
|
||||||
|
// pack a rectangle that is 'width' by 'height' in dimensions
|
||||||
|
// using temporary storage provided by the array 'nodes', which is 'num_nodes' long
|
||||||
|
//
|
||||||
|
// You must call this function every time you start packing into a new target.
|
||||||
|
//
|
||||||
|
// There is no "shutdown" function. The 'nodes' memory must stay valid for
|
||||||
|
// the following stbrp_pack_rects() call (or calls), but can be freed after
|
||||||
|
// the call (or calls) finish.
|
||||||
|
//
|
||||||
|
// Note: to guarantee best results, either:
|
||||||
|
// 1. make sure 'num_nodes' >= 'width'
|
||||||
|
// or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1'
|
||||||
|
//
|
||||||
|
// If you don't do either of the above things, widths will be quantized to multiples
|
||||||
|
// of small integers to guarantee the algorithm doesn't run out of temporary storage.
|
||||||
|
//
|
||||||
|
// If you do #2, then the non-quantized algorithm will be used, but the algorithm
|
||||||
|
// may run out of temporary storage and be unable to pack some rectangles.
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
|
||||||
|
// Optionally call this function after init but before doing any packing to
|
||||||
|
// change the handling of the out-of-temp-memory scenario, described above.
|
||||||
|
// If you call init again, this will be reset to the default (false).
|
||||||
|
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
|
||||||
|
// Optionally select which packing heuristic the library should use. Different
|
||||||
|
// heuristics will produce better/worse results for different data sets.
|
||||||
|
// If you call init again, this will be reset to the default.
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
STBRP_HEURISTIC_Skyline_default=0,
|
||||||
|
STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
|
||||||
|
STBRP_HEURISTIC_Skyline_BF_sortHeight
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// the details of the following structures don't matter to you, but they must
|
||||||
|
// be visible so you can handle the memory allocations for them
|
||||||
|
|
||||||
|
struct stbrp_node
|
||||||
|
{
|
||||||
|
stbrp_coord x,y;
|
||||||
|
stbrp_node *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct stbrp_context
|
||||||
|
{
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
int align;
|
||||||
|
int init_mode;
|
||||||
|
int heuristic;
|
||||||
|
int num_nodes;
|
||||||
|
stbrp_node *active_head;
|
||||||
|
stbrp_node *free_head;
|
||||||
|
stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// IMPLEMENTATION SECTION
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifdef STB_RECT_PACK_IMPLEMENTATION
|
||||||
|
#ifndef STBRP_SORT
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define STBRP_SORT qsort
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef STBRP_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#define STBRP_ASSERT assert
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define STBRP__NOTUSED(v) (void)(v)
|
||||||
|
#define STBRP__CDECL __cdecl
|
||||||
|
#else
|
||||||
|
#define STBRP__NOTUSED(v) (void)sizeof(v)
|
||||||
|
#define STBRP__CDECL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
STBRP__INIT_skyline = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
|
||||||
|
{
|
||||||
|
switch (context->init_mode) {
|
||||||
|
case STBRP__INIT_skyline:
|
||||||
|
STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
|
||||||
|
context->heuristic = heuristic;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
STBRP_ASSERT(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
|
||||||
|
{
|
||||||
|
if (allow_out_of_mem)
|
||||||
|
// if it's ok to run out of memory, then don't bother aligning them;
|
||||||
|
// this gives better packing, but may fail due to OOM (even though
|
||||||
|
// the rectangles easily fit). @TODO a smarter approach would be to only
|
||||||
|
// quantize once we've hit OOM, then we could get rid of this parameter.
|
||||||
|
context->align = 1;
|
||||||
|
else {
|
||||||
|
// if it's not ok to run out of memory, then quantize the widths
|
||||||
|
// so that num_nodes is always enough nodes.
|
||||||
|
//
|
||||||
|
// I.e. num_nodes * align >= width
|
||||||
|
// align >= width / num_nodes
|
||||||
|
// align = ceil(width/num_nodes)
|
||||||
|
|
||||||
|
context->align = (context->width + context->num_nodes-1) / context->num_nodes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i=0; i < num_nodes-1; ++i)
|
||||||
|
nodes[i].next = &nodes[i+1];
|
||||||
|
nodes[i].next = NULL;
|
||||||
|
context->init_mode = STBRP__INIT_skyline;
|
||||||
|
context->heuristic = STBRP_HEURISTIC_Skyline_default;
|
||||||
|
context->free_head = &nodes[0];
|
||||||
|
context->active_head = &context->extra[0];
|
||||||
|
context->width = width;
|
||||||
|
context->height = height;
|
||||||
|
context->num_nodes = num_nodes;
|
||||||
|
stbrp_setup_allow_out_of_mem(context, 0);
|
||||||
|
|
||||||
|
// node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly)
|
||||||
|
context->extra[0].x = 0;
|
||||||
|
context->extra[0].y = 0;
|
||||||
|
context->extra[0].next = &context->extra[1];
|
||||||
|
context->extra[1].x = (stbrp_coord) width;
|
||||||
|
context->extra[1].y = (1<<30);
|
||||||
|
context->extra[1].next = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find minimum y position if it starts at x1
|
||||||
|
static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
|
||||||
|
{
|
||||||
|
stbrp_node *node = first;
|
||||||
|
int x1 = x0 + width;
|
||||||
|
int min_y, visited_width, waste_area;
|
||||||
|
|
||||||
|
STBRP__NOTUSED(c);
|
||||||
|
|
||||||
|
STBRP_ASSERT(first->x <= x0);
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
// skip in case we're past the node
|
||||||
|
while (node->next->x <= x0)
|
||||||
|
++node;
|
||||||
|
#else
|
||||||
|
STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency
|
||||||
|
#endif
|
||||||
|
|
||||||
|
STBRP_ASSERT(node->x <= x0);
|
||||||
|
|
||||||
|
min_y = 0;
|
||||||
|
waste_area = 0;
|
||||||
|
visited_width = 0;
|
||||||
|
while (node->x < x1) {
|
||||||
|
if (node->y > min_y) {
|
||||||
|
// raise min_y higher.
|
||||||
|
// we've accounted for all waste up to min_y,
|
||||||
|
// but we'll now add more waste for everything we've visted
|
||||||
|
waste_area += visited_width * (node->y - min_y);
|
||||||
|
min_y = node->y;
|
||||||
|
// the first time through, visited_width might be reduced
|
||||||
|
if (node->x < x0)
|
||||||
|
visited_width += node->next->x - x0;
|
||||||
|
else
|
||||||
|
visited_width += node->next->x - node->x;
|
||||||
|
} else {
|
||||||
|
// add waste area
|
||||||
|
int under_width = node->next->x - node->x;
|
||||||
|
if (under_width + visited_width > width)
|
||||||
|
under_width = width - visited_width;
|
||||||
|
waste_area += under_width * (min_y - node->y);
|
||||||
|
visited_width += under_width;
|
||||||
|
}
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
*pwaste = waste_area;
|
||||||
|
return min_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int x,y;
|
||||||
|
stbrp_node **prev_link;
|
||||||
|
} stbrp__findresult;
|
||||||
|
|
||||||
|
static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
|
||||||
|
{
|
||||||
|
int best_waste = (1<<30), best_x, best_y = (1 << 30);
|
||||||
|
stbrp__findresult fr;
|
||||||
|
stbrp_node **prev, *node, *tail, **best = NULL;
|
||||||
|
|
||||||
|
// align to multiple of c->align
|
||||||
|
width = (width + c->align - 1);
|
||||||
|
width -= width % c->align;
|
||||||
|
STBRP_ASSERT(width % c->align == 0);
|
||||||
|
|
||||||
|
// if it can't possibly fit, bail immediately
|
||||||
|
if (width > c->width || height > c->height) {
|
||||||
|
fr.prev_link = NULL;
|
||||||
|
fr.x = fr.y = 0;
|
||||||
|
return fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
node = c->active_head;
|
||||||
|
prev = &c->active_head;
|
||||||
|
while (node->x + width <= c->width) {
|
||||||
|
int y,waste;
|
||||||
|
y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
|
||||||
|
if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL
|
||||||
|
// bottom left
|
||||||
|
if (y < best_y) {
|
||||||
|
best_y = y;
|
||||||
|
best = prev;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// best-fit
|
||||||
|
if (y + height <= c->height) {
|
||||||
|
// can only use it if it first vertically
|
||||||
|
if (y < best_y || (y == best_y && waste < best_waste)) {
|
||||||
|
best_y = y;
|
||||||
|
best_waste = waste;
|
||||||
|
best = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prev = &node->next;
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
best_x = (best == NULL) ? 0 : (*best)->x;
|
||||||
|
|
||||||
|
// if doing best-fit (BF), we also have to try aligning right edge to each node position
|
||||||
|
//
|
||||||
|
// e.g, if fitting
|
||||||
|
//
|
||||||
|
// ____________________
|
||||||
|
// |____________________|
|
||||||
|
//
|
||||||
|
// into
|
||||||
|
//
|
||||||
|
// | |
|
||||||
|
// | ____________|
|
||||||
|
// |____________|
|
||||||
|
//
|
||||||
|
// then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned
|
||||||
|
//
|
||||||
|
// This makes BF take about 2x the time
|
||||||
|
|
||||||
|
if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
|
||||||
|
tail = c->active_head;
|
||||||
|
node = c->active_head;
|
||||||
|
prev = &c->active_head;
|
||||||
|
// find first node that's admissible
|
||||||
|
while (tail->x < width)
|
||||||
|
tail = tail->next;
|
||||||
|
while (tail) {
|
||||||
|
int xpos = tail->x - width;
|
||||||
|
int y,waste;
|
||||||
|
STBRP_ASSERT(xpos >= 0);
|
||||||
|
// find the left position that matches this
|
||||||
|
while (node->next->x <= xpos) {
|
||||||
|
prev = &node->next;
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
|
||||||
|
y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
|
||||||
|
if (y + height <= c->height) {
|
||||||
|
if (y <= best_y) {
|
||||||
|
if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
|
||||||
|
best_x = xpos;
|
||||||
|
//STBRP_ASSERT(y <= best_y); [DEAR IMGUI]
|
||||||
|
best_y = y;
|
||||||
|
best_waste = waste;
|
||||||
|
best = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tail = tail->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fr.prev_link = best;
|
||||||
|
fr.x = best_x;
|
||||||
|
fr.y = best_y;
|
||||||
|
return fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
|
||||||
|
{
|
||||||
|
// find best position according to heuristic
|
||||||
|
stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
|
||||||
|
stbrp_node *node, *cur;
|
||||||
|
|
||||||
|
// bail if:
|
||||||
|
// 1. it failed
|
||||||
|
// 2. the best node doesn't fit (we don't always check this)
|
||||||
|
// 3. we're out of memory
|
||||||
|
if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
|
||||||
|
res.prev_link = NULL;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// on success, create new node
|
||||||
|
node = context->free_head;
|
||||||
|
node->x = (stbrp_coord) res.x;
|
||||||
|
node->y = (stbrp_coord) (res.y + height);
|
||||||
|
|
||||||
|
context->free_head = node->next;
|
||||||
|
|
||||||
|
// insert the new node into the right starting point, and
|
||||||
|
// let 'cur' point to the remaining nodes needing to be
|
||||||
|
// stiched back in
|
||||||
|
|
||||||
|
cur = *res.prev_link;
|
||||||
|
if (cur->x < res.x) {
|
||||||
|
// preserve the existing one, so start testing with the next one
|
||||||
|
stbrp_node *next = cur->next;
|
||||||
|
cur->next = node;
|
||||||
|
cur = next;
|
||||||
|
} else {
|
||||||
|
*res.prev_link = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// from here, traverse cur and free the nodes, until we get to one
|
||||||
|
// that shouldn't be freed
|
||||||
|
while (cur->next && cur->next->x <= res.x + width) {
|
||||||
|
stbrp_node *next = cur->next;
|
||||||
|
// move the current node to the free list
|
||||||
|
cur->next = context->free_head;
|
||||||
|
context->free_head = cur;
|
||||||
|
cur = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// stitch the list back in
|
||||||
|
node->next = cur;
|
||||||
|
|
||||||
|
if (cur->x < res.x + width)
|
||||||
|
cur->x = (stbrp_coord) (res.x + width);
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
|
cur = context->active_head;
|
||||||
|
while (cur->x < context->width) {
|
||||||
|
STBRP_ASSERT(cur->x < cur->next->x);
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
STBRP_ASSERT(cur->next == NULL);
|
||||||
|
|
||||||
|
{
|
||||||
|
int count=0;
|
||||||
|
cur = context->active_head;
|
||||||
|
while (cur) {
|
||||||
|
cur = cur->next;
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
cur = context->free_head;
|
||||||
|
while (cur) {
|
||||||
|
cur = cur->next;
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
STBRP_ASSERT(count == context->num_nodes+2);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int STBRP__CDECL rect_height_compare(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const stbrp_rect *p = (const stbrp_rect *) a;
|
||||||
|
const stbrp_rect *q = (const stbrp_rect *) b;
|
||||||
|
if (p->h > q->h)
|
||||||
|
return -1;
|
||||||
|
if (p->h < q->h)
|
||||||
|
return 1;
|
||||||
|
return (p->w > q->w) ? -1 : (p->w < q->w);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int STBRP__CDECL rect_original_order(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const stbrp_rect *p = (const stbrp_rect *) a;
|
||||||
|
const stbrp_rect *q = (const stbrp_rect *) b;
|
||||||
|
return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
|
||||||
|
}
|
||||||
|
|
||||||
|
STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
|
||||||
|
{
|
||||||
|
int i, all_rects_packed = 1;
|
||||||
|
|
||||||
|
// we use the 'was_packed' field internally to allow sorting/unsorting
|
||||||
|
for (i=0; i < num_rects; ++i) {
|
||||||
|
rects[i].was_packed = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort according to heuristic
|
||||||
|
STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
|
||||||
|
|
||||||
|
for (i=0; i < num_rects; ++i) {
|
||||||
|
if (rects[i].w == 0 || rects[i].h == 0) {
|
||||||
|
rects[i].x = rects[i].y = 0; // empty rect needs no space
|
||||||
|
} else {
|
||||||
|
stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
|
||||||
|
if (fr.prev_link) {
|
||||||
|
rects[i].x = (stbrp_coord) fr.x;
|
||||||
|
rects[i].y = (stbrp_coord) fr.y;
|
||||||
|
} else {
|
||||||
|
rects[i].x = rects[i].y = STBRP__MAXVAL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// unsort
|
||||||
|
STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
|
||||||
|
|
||||||
|
// set was_packed flags and all_rects_packed status
|
||||||
|
for (i=0; i < num_rects; ++i) {
|
||||||
|
rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
|
||||||
|
if (!rects[i].was_packed)
|
||||||
|
all_rects_packed = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the all_rects_packed status
|
||||||
|
return all_rects_packed;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
This software is available under 2 licenses -- choose whichever you prefer.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
ALTERNATIVE A - MIT License
|
||||||
|
Copyright (c) 2017 Sean Barrett
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||||
|
This is free and unencumbered software released into the public domain.
|
||||||
|
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||||
|
software, either in source code form or as a compiled binary, for any purpose,
|
||||||
|
commercial or non-commercial, and by any means.
|
||||||
|
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||||
|
software dedicate any and all copyright interest in the software to the public
|
||||||
|
domain. We make this dedication for the benefit of the public at large and to
|
||||||
|
the detriment of our heirs and successors. We intend this dedication to be an
|
||||||
|
overt act of relinquishment in perpetuity of all present and future rights to
|
||||||
|
this software under copyright law.
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
1488
imgui/imstb_textedit.h
Normal file
1488
imgui/imstb_textedit.h
Normal file
File diff suppressed because it is too large
Load Diff
5085
imgui/imstb_truetype.h
Normal file
5085
imgui/imstb_truetype.h
Normal file
File diff suppressed because it is too large
Load Diff
190
model.c
Normal file
190
model.c
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
#include "model.h"
|
||||||
|
|
||||||
|
#include "stb_ds.h"
|
||||||
|
|
||||||
|
#include "render.h"
|
||||||
|
|
||||||
|
#include "HandmadeMath.h"
|
||||||
|
|
||||||
|
#include "math.h"
|
||||||
|
#include "time.h"
|
||||||
|
|
||||||
|
#define CGLTF_IMPLEMENTATION
|
||||||
|
#include "cgltf.h"
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "jsffi.h"
|
||||||
|
|
||||||
|
SDL_GPUBuffer *texcoord_floats(float *f, int n)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GPUBuffer *float_buffer(float *f, int v)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GPUBuffer *index_buffer(float *f, int verts)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t pack_int10_n2(float *norm)
|
||||||
|
{
|
||||||
|
uint32_t ret = 0;
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
int n = (norm[i]+1.0)*511;
|
||||||
|
ret |= (n & 0x3ff) << (10*i);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pack an array of normals into
|
||||||
|
SDL_GPUBuffer *normal_floats(float *f, int n)
|
||||||
|
{
|
||||||
|
return float_buffer(f, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GPUBuffer *ubyten_buffer(float *f, int v)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GPUBuffer *ubyte_buffer(float *f, int v)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GPUBuffer *accessor2buffer(cgltf_accessor *a, int type)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void packFloats(float *src, float *dest, int srcLength) {
|
||||||
|
int i, j;
|
||||||
|
for (i = 0, j = 0; i < srcLength; i += 3, j += 4) {
|
||||||
|
dest[j] = src[i];
|
||||||
|
dest[j + 1] = src[i + 1];
|
||||||
|
dest[j + 2] = src[i + 2];
|
||||||
|
dest[j + 3] = 0.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static md5joint *node2joint(skin *sk, cgltf_node *n, cgltf_skin *skin)
|
||||||
|
{
|
||||||
|
int k = 0;
|
||||||
|
while (skin->joints[k] != n && k < skin->joints_count) k++;
|
||||||
|
return sk->joints+k;
|
||||||
|
}
|
||||||
|
|
||||||
|
animation *gltf_anim(cgltf_animation *anim, skin *sk, cgltf_skin *skin)
|
||||||
|
{
|
||||||
|
animation *ret = calloc(sizeof(*ret), 1);
|
||||||
|
animation an = *ret;
|
||||||
|
arrsetlen(an.samplers, anim->samplers_count);
|
||||||
|
|
||||||
|
for (int i = 0; i < anim->samplers_count; i++) {
|
||||||
|
cgltf_animation_sampler s = anim->samplers[i];
|
||||||
|
sampler samp = (sampler){0};
|
||||||
|
int n = cgltf_accessor_unpack_floats(s.input, NULL, 0);
|
||||||
|
arrsetlen(samp.times, n);
|
||||||
|
cgltf_accessor_unpack_floats(s.input, samp.times, n);
|
||||||
|
|
||||||
|
n = cgltf_accessor_unpack_floats(s.output, NULL, 0);
|
||||||
|
int comp = cgltf_num_components(s.output->type);
|
||||||
|
arrsetlen(samp.data, n/comp);
|
||||||
|
if (comp == 4)
|
||||||
|
cgltf_accessor_unpack_floats(s.output, samp.data, n);
|
||||||
|
else {
|
||||||
|
float *out = malloc(sizeof(*out)*n);
|
||||||
|
cgltf_accessor_unpack_floats(s.output, out, n);
|
||||||
|
packFloats(out, samp.data, n);
|
||||||
|
free(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
samp.type = s.interpolation;
|
||||||
|
|
||||||
|
if (samp.type == LINEAR && comp == 4)
|
||||||
|
samp.type = SLERP;
|
||||||
|
|
||||||
|
an.samplers[i] = samp;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < anim->channels_count; i++) {
|
||||||
|
cgltf_animation_channel ch = anim->channels[i];
|
||||||
|
struct anim_channel ach = (struct anim_channel){0};
|
||||||
|
md5joint *md = node2joint(sk, ch.target_node, skin);
|
||||||
|
switch(ch.target_path) {
|
||||||
|
case cgltf_animation_path_type_translation:
|
||||||
|
ach.target = &md->pos;
|
||||||
|
break;
|
||||||
|
case cgltf_animation_path_type_rotation:
|
||||||
|
ach.target = &md->rot;
|
||||||
|
break;
|
||||||
|
case cgltf_animation_path_type_scale:
|
||||||
|
ach.target = &md->scale;
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
ach.sampler = an.samplers+(ch.sampler-anim->samplers);
|
||||||
|
|
||||||
|
arrput(an.channels, ach);
|
||||||
|
}
|
||||||
|
|
||||||
|
an.time = 0;
|
||||||
|
|
||||||
|
*ret = an;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
skin *make_gltf_skin(cgltf_skin *skin, cgltf_data *data)
|
||||||
|
{
|
||||||
|
int n = cgltf_accessor_unpack_floats(skin->inverse_bind_matrices, NULL, 0);
|
||||||
|
struct skin *sk = NULL;
|
||||||
|
sk = calloc(sizeof(*sk),1);
|
||||||
|
arrsetlen(sk->invbind, n/16);
|
||||||
|
cgltf_accessor_unpack_floats(skin->inverse_bind_matrices, sk->invbind, n);
|
||||||
|
|
||||||
|
arrsetlen(sk->joints, skin->joints_count);
|
||||||
|
|
||||||
|
for (int i = 0; i < 50; i++)
|
||||||
|
sk->binds[i] = MAT1;
|
||||||
|
|
||||||
|
for (int i = 0; i < skin->joints_count; i++) {
|
||||||
|
cgltf_node *n = skin->joints[i];
|
||||||
|
md5joint *j = sk->joints+i;
|
||||||
|
|
||||||
|
if (n == skin->skeleton)
|
||||||
|
j->parent = NULL;
|
||||||
|
else
|
||||||
|
j->parent = node2joint(sk, n->parent, skin);
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
j->pos.e[i] = n->translation[i];
|
||||||
|
j->scale.e[i] = n->scale[i];
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
j->rot.e[i] = n->rotation[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
sk->anim = gltf_anim(data->animations+0, sk, skin);
|
||||||
|
|
||||||
|
return sk;
|
||||||
|
}
|
||||||
|
|
||||||
|
void skin_calculate(skin *sk)
|
||||||
|
{
|
||||||
|
// animation_run(sk->anim, apptime());
|
||||||
|
for (int i = 0; i < arrlen(sk->joints); i++) {
|
||||||
|
md5joint *md = sk->joints+i;
|
||||||
|
md->t = HMM_M4TRS(md->pos.xyz, md->rot, md->scale.xyz);
|
||||||
|
if (md->parent)
|
||||||
|
md->t = HMM_MulM4(md->parent->t, md->t);
|
||||||
|
|
||||||
|
sk->binds[i] = HMM_MulM4(md->t, sk->invbind[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
53
model.h
Normal file
53
model.h
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
#ifndef MODEL_H
|
||||||
|
#define MODEL_H
|
||||||
|
|
||||||
|
#include "HandmadeMath.h"
|
||||||
|
#include "transform.h"
|
||||||
|
#include "anim.h"
|
||||||
|
|
||||||
|
#include "cgltf.h"
|
||||||
|
|
||||||
|
#define MAT_POS 0
|
||||||
|
#define MAT_UV 1
|
||||||
|
#define MAT_NORM 2
|
||||||
|
#define MAT_BONE 3
|
||||||
|
#define MAT_WEIGHT 4
|
||||||
|
#define MAT_COLOR 5
|
||||||
|
#define MAT_TAN 6
|
||||||
|
#define MAT_ANGLE 7
|
||||||
|
#define MAT_WH 8
|
||||||
|
#define MAT_ST 9
|
||||||
|
#define MAT_PPOS 10
|
||||||
|
#define MAT_SCALE 11
|
||||||
|
#define MAT_INDEX 100
|
||||||
|
|
||||||
|
typedef struct md5joint {
|
||||||
|
struct md5joint *parent;
|
||||||
|
HMM_Vec4 pos;
|
||||||
|
HMM_Quat rot;
|
||||||
|
HMM_Vec4 scale;
|
||||||
|
HMM_Mat4 t;
|
||||||
|
} md5joint;
|
||||||
|
|
||||||
|
typedef struct skin {
|
||||||
|
md5joint *joints;
|
||||||
|
HMM_Mat4 *invbind;
|
||||||
|
HMM_Mat4 binds[50]; /* binds = joint * invbind */
|
||||||
|
animation *anim;
|
||||||
|
} skin;
|
||||||
|
|
||||||
|
//sg_buffer accessor2buffer(cgltf_accessor *a, int type);
|
||||||
|
skin *make_gltf_skin(cgltf_skin *skin, cgltf_data *data);
|
||||||
|
void skin_calculate(skin *sk);
|
||||||
|
|
||||||
|
/*sg_buffer float_buffer(float *f, int v);
|
||||||
|
sg_buffer index_buffer(float *f, int verts);
|
||||||
|
sg_buffer texcoord_floats(float *f, int n);
|
||||||
|
sg_buffer par_idx_buffer(uint32_t *i, int v);
|
||||||
|
sg_buffer normal_floats(float *f, int n);
|
||||||
|
sg_buffer ubyten_buffer(float *f, int v);
|
||||||
|
sg_buffer ubyte_buffer(float *f, int v);
|
||||||
|
sg_buffer joint_buf(float *f, int v);
|
||||||
|
sg_buffer weight_buf(float *f, int v);
|
||||||
|
*/
|
||||||
|
#endif
|
||||||
717
msf_gif.h
Normal file
717
msf_gif.h
Normal file
@@ -0,0 +1,717 @@
|
|||||||
|
/*
|
||||||
|
HOW TO USE:
|
||||||
|
|
||||||
|
In exactly one translation unit (.c or .cpp file), #define MSF_GIF_IMPL before including the header, like so:
|
||||||
|
|
||||||
|
#define MSF_GIF_IMPL
|
||||||
|
#include "msf_gif.h"
|
||||||
|
|
||||||
|
Everywhere else, just include the header like normal.
|
||||||
|
|
||||||
|
|
||||||
|
USAGE EXAMPLE:
|
||||||
|
|
||||||
|
int width = 480, height = 320, centisecondsPerFrame = 5, bitDepth = 16;
|
||||||
|
MsfGifState gifState = {};
|
||||||
|
// msf_gif_bgra_flag = true; //optionally, set this flag if your pixels are in BGRA format instead of RGBA
|
||||||
|
// msf_gif_alpha_threshold = 128; //optionally, enable transparency (see function documentation below for details)
|
||||||
|
msf_gif_begin(&gifState, width, height);
|
||||||
|
msf_gif_frame(&gifState, ..., centisecondsPerFrame, bitDepth, width * 4); //frame 1
|
||||||
|
msf_gif_frame(&gifState, ..., centisecondsPerFrame, bitDepth, width * 4); //frame 2
|
||||||
|
msf_gif_frame(&gifState, ..., centisecondsPerFrame, bitDepth, width * 4); //frame 3, etc...
|
||||||
|
MsfGifResult result = msf_gif_end(&gifState);
|
||||||
|
if (result.data) {
|
||||||
|
FILE * fp = fopen("MyGif.gif", "wb");
|
||||||
|
fwrite(result.data, result.dataSize, 1, fp);
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
msf_gif_free(result);
|
||||||
|
|
||||||
|
Detailed function documentation can be found in the header section below.
|
||||||
|
|
||||||
|
|
||||||
|
ERROR HANDLING:
|
||||||
|
|
||||||
|
If memory allocation fails, the functions will signal the error via their return values.
|
||||||
|
If one function call fails, the library will free all of its allocations,
|
||||||
|
and all subsequent calls will safely no-op and return 0 until the next call to `msf_gif_begin()`.
|
||||||
|
Therefore, it's safe to check only the return value of `msf_gif_end()`.
|
||||||
|
|
||||||
|
|
||||||
|
REPLACING MALLOC:
|
||||||
|
|
||||||
|
This library uses malloc+realloc+free internally for memory allocation.
|
||||||
|
To facilitate integration with custom memory allocators, these calls go through macros, which can be redefined.
|
||||||
|
The expected function signature equivalents of the macros are as follows:
|
||||||
|
|
||||||
|
void * MSF_GIF_MALLOC(void * context, size_t newSize)
|
||||||
|
void * MSF_GIF_REALLOC(void * context, void * oldMemory, size_t oldSize, size_t newSize)
|
||||||
|
void MSF_GIF_FREE(void * context, void * oldMemory, size_t oldSize)
|
||||||
|
|
||||||
|
If your allocator needs a context pointer, you can set the `customAllocatorContext` field of the MsfGifState struct
|
||||||
|
before calling msf_gif_begin(), and it will be passed to all subsequent allocator macro calls.
|
||||||
|
|
||||||
|
The maximum number of bytes the library will allocate to encode a single gif is bounded by the following formula:
|
||||||
|
`(2 * 1024 * 1024) + (width * height * 8) + ((1024 + width * height * 1.5) * 3 * frameCount)`
|
||||||
|
The peak heap memory usage in bytes, if using a general-purpose heap allocator, is bounded by the following formula:
|
||||||
|
`(2 * 1024 * 1024) + (width * height * 9.5) + 1024 + (16 * frameCount) + (2 * sizeOfResultingGif)
|
||||||
|
|
||||||
|
|
||||||
|
See end of file for license information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//version 2.2
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// HEADER ///
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifndef MSF_GIF_H
|
||||||
|
#define MSF_GIF_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void * data;
|
||||||
|
size_t dataSize;
|
||||||
|
|
||||||
|
size_t allocSize; //internal use
|
||||||
|
void * contextPointer; //internal use
|
||||||
|
} MsfGifResult;
|
||||||
|
|
||||||
|
typedef struct { //internal use
|
||||||
|
uint32_t * pixels;
|
||||||
|
int depth, count, rbits, gbits, bbits;
|
||||||
|
} MsfCookedFrame;
|
||||||
|
|
||||||
|
typedef struct MsfGifBuffer {
|
||||||
|
struct MsfGifBuffer * next;
|
||||||
|
size_t size;
|
||||||
|
uint8_t data[1];
|
||||||
|
} MsfGifBuffer;
|
||||||
|
|
||||||
|
typedef size_t (* MsfGifFileWriteFunc) (const void * buffer, size_t size, size_t count, void * stream);
|
||||||
|
typedef struct {
|
||||||
|
MsfGifFileWriteFunc fileWriteFunc;
|
||||||
|
void * fileWriteData;
|
||||||
|
MsfCookedFrame previousFrame;
|
||||||
|
MsfCookedFrame currentFrame;
|
||||||
|
int16_t * lzwMem;
|
||||||
|
MsfGifBuffer * listHead;
|
||||||
|
MsfGifBuffer * listTail;
|
||||||
|
int width, height;
|
||||||
|
void * customAllocatorContext;
|
||||||
|
int framesSubmitted; //needed for transparency to work correctly (because we reach into the previous frame)
|
||||||
|
} MsfGifState;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif //__cplusplus
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param width Image width in pixels.
|
||||||
|
* @param height Image height in pixels.
|
||||||
|
* @return Non-zero on success, 0 on error.
|
||||||
|
*/
|
||||||
|
int msf_gif_begin(MsfGifState * handle, int width, int height);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pixelData Pointer to raw framebuffer data. Rows must be contiguous in memory, in RGBA8 format
|
||||||
|
* (or BGRA8 if you have set `msf_gif_bgra_flag = true`).
|
||||||
|
* Note: This function does NOT free `pixelData`. You must free it yourself afterwards.
|
||||||
|
* @param centiSecondsPerFrame How many hundredths of a second this frame should be displayed for.
|
||||||
|
* Note: This being specified in centiseconds is a limitation of the GIF format.
|
||||||
|
* @param maxBitDepth Limits how many bits per pixel can be used when quantizing the gif.
|
||||||
|
* The actual bit depth chosen for a given frame will be less than or equal to
|
||||||
|
* the supplied maximum, depending on the variety of colors used in the frame.
|
||||||
|
* `maxBitDepth` will be clamped between 1 and 16. The recommended default is 16.
|
||||||
|
* Lowering this value can result in faster exports and smaller gifs,
|
||||||
|
* but the quality may suffer.
|
||||||
|
* Please experiment with this value to find what works best for your application.
|
||||||
|
* @param pitchInBytes The number of bytes from the beginning of one row of pixels to the beginning of the next.
|
||||||
|
* If you want to flip the image, just pass in a negative pitch.
|
||||||
|
* @return Non-zero on success, 0 on error.
|
||||||
|
*/
|
||||||
|
int msf_gif_frame(MsfGifState * handle, uint8_t * pixelData, int centiSecondsPerFame, int maxBitDepth, int pitchInBytes);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return A block of memory containing the gif file data, or NULL on error.
|
||||||
|
* You are responsible for freeing this via `msf_gif_free()`.
|
||||||
|
*/
|
||||||
|
MsfGifResult msf_gif_end(MsfGifState * handle);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param result The MsfGifResult struct, verbatim as it was returned from `msf_gif_end()`.
|
||||||
|
*/
|
||||||
|
void msf_gif_free(MsfGifResult result);
|
||||||
|
|
||||||
|
//The gif format only supports 1-bit transparency, meaning a pixel will either be fully transparent or fully opaque.
|
||||||
|
//Pixels with an alpha value less than the alpha threshold will be treated as transparent.
|
||||||
|
//To enable exporting transparent gifs, set it to a value between 1 and 255 (inclusive) before calling msf_gif_frame().
|
||||||
|
//Setting it to 0 causes the alpha channel to be ignored. Its initial value is 0.
|
||||||
|
extern int msf_gif_alpha_threshold;
|
||||||
|
|
||||||
|
//Set `msf_gif_bgra_flag = true` before calling `msf_gif_frame()` if your pixels are in BGRA byte order instead of RBGA.
|
||||||
|
extern int msf_gif_bgra_flag;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//TO-FILE FUNCTIONS
|
||||||
|
//These functions are equivalent to the ones above, but they write results to a file incrementally,
|
||||||
|
//instead of building a buffer in memory. This can result in lower memory usage when saving large gifs,
|
||||||
|
//because memory usage is bounded by only the size of a single frame, and is not dependent on the number of frames.
|
||||||
|
//There is currently no reason to use these unless you are on a memory-constrained platform.
|
||||||
|
//If in doubt about which API to use, for now you should use the normal (non-file) functions above.
|
||||||
|
//The signature of MsfGifFileWriteFunc matches fwrite for convenience, so that you can use the C file API like so:
|
||||||
|
// FILE * fp = fopen("MyGif.gif", "wb");
|
||||||
|
// msf_gif_begin_to_file(&handle, width, height, (MsfGifFileWriteFunc) fwrite, (void *) fp);
|
||||||
|
// msf_gif_frame_to_file(...)
|
||||||
|
// msf_gif_end_to_file(&handle);
|
||||||
|
// fclose(fp);
|
||||||
|
//If you use a custom file write function, you must take care to return the same values that fwrite() would return.
|
||||||
|
//Note that all three functions will potentially write to the file.
|
||||||
|
int msf_gif_begin_to_file(MsfGifState * handle, int width, int height, MsfGifFileWriteFunc func, void * filePointer);
|
||||||
|
int msf_gif_frame_to_file(MsfGifState * handle, uint8_t * pixelData, int centiSecondsPerFame, int maxBitDepth, int pitchInBytes);
|
||||||
|
int msf_gif_end_to_file(MsfGifState * handle); //returns 0 on error and non-zero on success
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif //__cplusplus
|
||||||
|
|
||||||
|
#endif //MSF_GIF_H
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// IMPLEMENTATION ///
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifdef MSF_GIF_IMPL
|
||||||
|
#ifndef MSF_GIF_ALREADY_IMPLEMENTED_IN_THIS_TRANSLATION_UNIT
|
||||||
|
#define MSF_GIF_ALREADY_IMPLEMENTED_IN_THIS_TRANSLATION_UNIT
|
||||||
|
|
||||||
|
//ensure the library user has either defined all of malloc/realloc/free, or none
|
||||||
|
#if defined(MSF_GIF_MALLOC) && defined(MSF_GIF_REALLOC) && defined(MSF_GIF_FREE) //ok
|
||||||
|
#elif !defined(MSF_GIF_MALLOC) && !defined(MSF_GIF_REALLOC) && !defined(MSF_GIF_FREE) //ok
|
||||||
|
#else
|
||||||
|
#error "You must either define all of MSF_GIF_MALLOC, MSF_GIF_REALLOC, and MSF_GIF_FREE, or define none of them"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//provide default allocator definitions that redirect to the standard global allocator
|
||||||
|
#if !defined(MSF_GIF_MALLOC)
|
||||||
|
#include <stdlib.h> //malloc, etc.
|
||||||
|
#define MSF_GIF_MALLOC(contextPointer, newSize) malloc(newSize)
|
||||||
|
#define MSF_GIF_REALLOC(contextPointer, oldMemory, oldSize, newSize) realloc(oldMemory, newSize)
|
||||||
|
#define MSF_GIF_FREE(contextPointer, oldMemory, oldSize) free(oldMemory)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//instrumentation for capturing profiling traces (useless for the library user, but useful for the library author)
|
||||||
|
#ifdef MSF_GIF_ENABLE_TRACING
|
||||||
|
#define MsfTimeFunc TimeFunc
|
||||||
|
#define MsfTimeLoop TimeLoop
|
||||||
|
#define msf_init_profiling_thread init_profiling_thread
|
||||||
|
#else
|
||||||
|
#define MsfTimeFunc
|
||||||
|
#define MsfTimeLoop(name)
|
||||||
|
#define msf_init_profiling_thread()
|
||||||
|
#endif //MSF_GIF_ENABLE_TRACING
|
||||||
|
|
||||||
|
#include <string.h> //memcpy
|
||||||
|
|
||||||
|
//TODO: use compiler-specific notation to force-inline functions currently marked inline
|
||||||
|
#if defined(__GNUC__) //gcc, clang
|
||||||
|
static inline int msf_bit_log(int i) { return 32 - __builtin_clz(i); }
|
||||||
|
#elif defined(_MSC_VER) //msvc
|
||||||
|
#include <intrin.h>
|
||||||
|
static inline int msf_bit_log(int i) { unsigned long idx; _BitScanReverse(&idx, i); return idx + 1; }
|
||||||
|
#else //fallback implementation for other compilers
|
||||||
|
//from https://stackoverflow.com/a/31718095/3064745 - thanks!
|
||||||
|
static inline int msf_bit_log(int i) {
|
||||||
|
static const int MultiplyDeBruijnBitPosition[32] = {
|
||||||
|
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
||||||
|
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31,
|
||||||
|
};
|
||||||
|
i |= i >> 1;
|
||||||
|
i |= i >> 2;
|
||||||
|
i |= i >> 4;
|
||||||
|
i |= i >> 8;
|
||||||
|
i |= i >> 16;
|
||||||
|
return MultiplyDeBruijnBitPosition[(uint32_t)(i * 0x07C4ACDDU) >> 27] + 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
static inline int msf_imin(int a, int b) { return a < b? a : b; }
|
||||||
|
static inline int msf_imax(int a, int b) { return b < a? a : b; }
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// Frame Cooking ///
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#if (defined (__SSE2__) || defined (_M_X64) || _M_IX86_FP == 2) && !defined(MSF_GIF_NO_SSE2)
|
||||||
|
#include <emmintrin.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int msf_gif_alpha_threshold = 0;
|
||||||
|
int msf_gif_bgra_flag = 0;
|
||||||
|
|
||||||
|
static void msf_cook_frame(MsfCookedFrame * frame, uint8_t * raw, uint8_t * used,
|
||||||
|
int width, int height, int pitch, int depth)
|
||||||
|
{ MsfTimeFunc
|
||||||
|
//bit depth for each channel
|
||||||
|
const static int rdepthsArray[17] = { 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5 };
|
||||||
|
const static int gdepthsArray[17] = { 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6 };
|
||||||
|
const static int bdepthsArray[17] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5 };
|
||||||
|
//this extra level of indirection looks unnecessary but we need to explicitly decay the arrays to pointers
|
||||||
|
//in order to be able to swap them because of C's annoying not-quite-pointers, not-quite-value-types stack arrays.
|
||||||
|
const int * rdepths = msf_gif_bgra_flag? bdepthsArray : rdepthsArray;
|
||||||
|
const int * gdepths = gdepthsArray;
|
||||||
|
const int * bdepths = msf_gif_bgra_flag? rdepthsArray : bdepthsArray;
|
||||||
|
|
||||||
|
const static int ditherKernel[16] = {
|
||||||
|
0 << 12, 8 << 12, 2 << 12, 10 << 12,
|
||||||
|
12 << 12, 4 << 12, 14 << 12, 6 << 12,
|
||||||
|
3 << 12, 11 << 12, 1 << 12, 9 << 12,
|
||||||
|
15 << 12, 7 << 12, 13 << 12, 5 << 12,
|
||||||
|
};
|
||||||
|
|
||||||
|
uint32_t * cooked = frame->pixels;
|
||||||
|
int count = 0;
|
||||||
|
MsfTimeLoop("do") do {
|
||||||
|
int rbits = rdepths[depth], gbits = gdepths[depth], bbits = bdepths[depth];
|
||||||
|
int paletteSize = (1 << (rbits + gbits + bbits)) + 1;
|
||||||
|
memset(used, 0, paletteSize * sizeof(uint8_t));
|
||||||
|
|
||||||
|
//TODO: document what this math does and why it's correct
|
||||||
|
int rdiff = (1 << (8 - rbits)) - 1;
|
||||||
|
int gdiff = (1 << (8 - gbits)) - 1;
|
||||||
|
int bdiff = (1 << (8 - bbits)) - 1;
|
||||||
|
short rmul = (short) ((255.0f - rdiff) / 255.0f * 257);
|
||||||
|
short gmul = (short) ((255.0f - gdiff) / 255.0f * 257);
|
||||||
|
short bmul = (short) ((255.0f - bdiff) / 255.0f * 257);
|
||||||
|
|
||||||
|
int gmask = ((1 << gbits) - 1) << rbits;
|
||||||
|
int bmask = ((1 << bbits) - 1) << rbits << gbits;
|
||||||
|
|
||||||
|
MsfTimeLoop("cook") for (int y = 0; y < height; ++y) {
|
||||||
|
int x = 0;
|
||||||
|
|
||||||
|
#if (defined (__SSE2__) || defined (_M_X64) || _M_IX86_FP == 2) && !defined(MSF_GIF_NO_SSE2)
|
||||||
|
__m128i k = _mm_loadu_si128((__m128i *) &ditherKernel[(y & 3) * 4]);
|
||||||
|
__m128i k2 = _mm_or_si128(_mm_srli_epi32(k, rbits), _mm_slli_epi32(_mm_srli_epi32(k, bbits), 16));
|
||||||
|
for (; x < width - 3; x += 4) {
|
||||||
|
uint8_t * pixels = &raw[y * pitch + x * 4];
|
||||||
|
__m128i p = _mm_loadu_si128((__m128i *) pixels);
|
||||||
|
|
||||||
|
__m128i rb = _mm_and_si128(p, _mm_set1_epi32(0x00FF00FF));
|
||||||
|
__m128i rb1 = _mm_mullo_epi16(rb, _mm_set_epi16(bmul, rmul, bmul, rmul, bmul, rmul, bmul, rmul));
|
||||||
|
__m128i rb2 = _mm_adds_epu16(rb1, k2);
|
||||||
|
__m128i r3 = _mm_srli_epi32(_mm_and_si128(rb2, _mm_set1_epi32(0x0000FFFF)), 16 - rbits);
|
||||||
|
__m128i b3 = _mm_and_si128(_mm_srli_epi32(rb2, 32 - rbits - gbits - bbits), _mm_set1_epi32(bmask));
|
||||||
|
|
||||||
|
__m128i g = _mm_and_si128(_mm_srli_epi32(p, 8), _mm_set1_epi32(0x000000FF));
|
||||||
|
__m128i g1 = _mm_mullo_epi16(g, _mm_set1_epi32(gmul));
|
||||||
|
__m128i g2 = _mm_adds_epu16(g1, _mm_srli_epi32(k, gbits));
|
||||||
|
__m128i g3 = _mm_and_si128(_mm_srli_epi32(g2, 16 - rbits - gbits), _mm_set1_epi32(gmask));
|
||||||
|
|
||||||
|
__m128i out = _mm_or_si128(_mm_or_si128(r3, g3), b3);
|
||||||
|
|
||||||
|
//mask in transparency based on threshold
|
||||||
|
//NOTE: we can theoretically do a sub instead of srli by doing an unsigned compare via bias
|
||||||
|
// to maybe save a TINY amount of throughput? but lol who cares maybe I'll do it later -m
|
||||||
|
__m128i invAlphaMask = _mm_cmplt_epi32(_mm_srli_epi32(p, 24), _mm_set1_epi32(msf_gif_alpha_threshold));
|
||||||
|
out = _mm_or_si128(_mm_and_si128(invAlphaMask, _mm_set1_epi32(paletteSize - 1)), _mm_andnot_si128(invAlphaMask, out));
|
||||||
|
|
||||||
|
//TODO: does storing this as a __m128i then reading it back as a uint32_t violate strict aliasing?
|
||||||
|
uint32_t * c = &cooked[y * width + x];
|
||||||
|
_mm_storeu_si128((__m128i *) c, out);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//scalar cleanup loop
|
||||||
|
for (; x < width; ++x) {
|
||||||
|
uint8_t * p = &raw[y * pitch + x * 4];
|
||||||
|
|
||||||
|
//transparent pixel if alpha is low
|
||||||
|
if (p[3] < msf_gif_alpha_threshold) {
|
||||||
|
cooked[y * width + x] = paletteSize - 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int dx = x & 3, dy = y & 3;
|
||||||
|
int k = ditherKernel[dy * 4 + dx];
|
||||||
|
cooked[y * width + x] =
|
||||||
|
(msf_imin(65535, p[2] * bmul + (k >> bbits)) >> (16 - rbits - gbits - bbits) & bmask) |
|
||||||
|
(msf_imin(65535, p[1] * gmul + (k >> gbits)) >> (16 - rbits - gbits ) & gmask) |
|
||||||
|
msf_imin(65535, p[0] * rmul + (k >> rbits)) >> (16 - rbits );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
count = 0;
|
||||||
|
MsfTimeLoop("mark") for (int i = 0; i < width * height; ++i) {
|
||||||
|
used[cooked[i]] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//count used colors, transparent is ignored
|
||||||
|
MsfTimeLoop("count") for (int j = 0; j < paletteSize - 1; ++j) {
|
||||||
|
count += used[j];
|
||||||
|
}
|
||||||
|
} while (count >= 256 && --depth);
|
||||||
|
|
||||||
|
MsfCookedFrame ret = { cooked, depth, count, rdepths[depth], gdepths[depth], bdepths[depth] };
|
||||||
|
*frame = ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// Frame Compression ///
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
static inline void msf_put_code(uint8_t * * writeHead, uint32_t * blockBits, int len, uint32_t code) {
|
||||||
|
//insert new code into block buffer
|
||||||
|
int idx = *blockBits / 8;
|
||||||
|
int bit = *blockBits % 8;
|
||||||
|
(*writeHead)[idx + 0] |= code << bit ;
|
||||||
|
(*writeHead)[idx + 1] |= code >> ( 8 - bit);
|
||||||
|
(*writeHead)[idx + 2] |= code >> (16 - bit);
|
||||||
|
*blockBits += len;
|
||||||
|
|
||||||
|
//prep the next block buffer if the current one is full
|
||||||
|
if (*blockBits >= 256 * 8) {
|
||||||
|
*blockBits -= 255 * 8;
|
||||||
|
(*writeHead) += 256;
|
||||||
|
(*writeHead)[2] = (*writeHead)[1];
|
||||||
|
(*writeHead)[1] = (*writeHead)[0];
|
||||||
|
(*writeHead)[0] = 255;
|
||||||
|
memset((*writeHead) + 4, 0, 256);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int16_t * data;
|
||||||
|
int len;
|
||||||
|
int stride;
|
||||||
|
} MsfStridedList;
|
||||||
|
|
||||||
|
static inline void msf_lzw_reset(MsfStridedList * lzw, int tableSize, int stride) { MsfTimeFunc
|
||||||
|
memset(lzw->data, 0xFF, 4096 * stride * sizeof(int16_t));
|
||||||
|
lzw->len = tableSize + 2;
|
||||||
|
lzw->stride = stride;
|
||||||
|
}
|
||||||
|
|
||||||
|
static MsfGifBuffer * msf_compress_frame(void * allocContext, int width, int height, int centiSeconds,
|
||||||
|
MsfCookedFrame frame, MsfGifState * handle, uint8_t * used, int16_t * lzwMem)
|
||||||
|
{ MsfTimeFunc
|
||||||
|
//NOTE: we reserve enough memory for theoretical the worst case upfront because it's a reasonable amount,
|
||||||
|
// and prevents us from ever having to check size or realloc during compression
|
||||||
|
int maxBufSize = offsetof(MsfGifBuffer, data) + 32 + 256 * 3 + width * height * 3 / 2; //headers + color table + data
|
||||||
|
MsfGifBuffer * buffer = (MsfGifBuffer *) MSF_GIF_MALLOC(allocContext, maxBufSize);
|
||||||
|
if (!buffer) { return NULL; }
|
||||||
|
uint8_t * writeHead = buffer->data;
|
||||||
|
MsfStridedList lzw = { lzwMem };
|
||||||
|
|
||||||
|
//allocate tlb
|
||||||
|
int totalBits = frame.rbits + frame.gbits + frame.bbits;
|
||||||
|
int tlbSize = (1 << totalBits) + 1;
|
||||||
|
uint8_t tlb[(1 << 16) + 1]; //only 64k, so stack allocating is fine
|
||||||
|
|
||||||
|
//generate palette
|
||||||
|
typedef struct { uint8_t r, g, b; } Color3;
|
||||||
|
Color3 table[256] = { {0} };
|
||||||
|
int tableIdx = 1; //we start counting at 1 because 0 is the transparent color
|
||||||
|
//transparent is always last in the table
|
||||||
|
tlb[tlbSize-1] = 0;
|
||||||
|
MsfTimeLoop("table") for (int i = 0; i < tlbSize-1; ++i) {
|
||||||
|
if (used[i]) {
|
||||||
|
tlb[i] = tableIdx;
|
||||||
|
int rmask = (1 << frame.rbits) - 1;
|
||||||
|
int gmask = (1 << frame.gbits) - 1;
|
||||||
|
//isolate components
|
||||||
|
int r = i & rmask;
|
||||||
|
int g = i >> frame.rbits & gmask;
|
||||||
|
int b = i >> (frame.rbits + frame.gbits);
|
||||||
|
//shift into highest bits
|
||||||
|
r <<= 8 - frame.rbits;
|
||||||
|
g <<= 8 - frame.gbits;
|
||||||
|
b <<= 8 - frame.bbits;
|
||||||
|
table[tableIdx].r = r | r >> frame.rbits | r >> (frame.rbits * 2) | r >> (frame.rbits * 3);
|
||||||
|
table[tableIdx].g = g | g >> frame.gbits | g >> (frame.gbits * 2) | g >> (frame.gbits * 3);
|
||||||
|
table[tableIdx].b = b | b >> frame.bbits | b >> (frame.bbits * 2) | b >> (frame.bbits * 3);
|
||||||
|
if (msf_gif_bgra_flag) {
|
||||||
|
uint8_t temp = table[tableIdx].r;
|
||||||
|
table[tableIdx].r = table[tableIdx].b;
|
||||||
|
table[tableIdx].b = temp;
|
||||||
|
}
|
||||||
|
++tableIdx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int hasTransparentPixels = used[tlbSize-1];
|
||||||
|
|
||||||
|
//SPEC: "Because of some algorithmic constraints however, black & white images which have one color bit
|
||||||
|
// must be indicated as having a code size of 2."
|
||||||
|
int tableBits = msf_imax(2, msf_bit_log(tableIdx - 1));
|
||||||
|
int tableSize = 1 << tableBits;
|
||||||
|
//NOTE: we don't just compare `depth` field here because it will be wrong for the first frame and we will segfault
|
||||||
|
MsfCookedFrame previous = handle->previousFrame;
|
||||||
|
int hasSamePal = frame.rbits == previous.rbits && frame.gbits == previous.gbits && frame.bbits == previous.bbits;
|
||||||
|
int framesCompatible = hasSamePal && !hasTransparentPixels;
|
||||||
|
|
||||||
|
//NOTE: because __attribute__((__packed__)) is annoyingly compiler-specific, we do this unreadable weirdness
|
||||||
|
char headerBytes[19] = "\x21\xF9\x04\x05\0\0\0\0" "\x2C\0\0\0\0\0\0\0\0\x80";
|
||||||
|
//NOTE: we need to check the frame number because if we reach into the buffer prior to the first frame,
|
||||||
|
// we'll just clobber the file header instead, which is a bug
|
||||||
|
if (hasTransparentPixels && handle->framesSubmitted > 0) {
|
||||||
|
handle->listTail->data[3] = 0x09; //set the previous frame's disposal to background, so transparency is possible
|
||||||
|
}
|
||||||
|
memcpy(&headerBytes[4], ¢iSeconds, 2);
|
||||||
|
memcpy(&headerBytes[13], &width, 2);
|
||||||
|
memcpy(&headerBytes[15], &height, 2);
|
||||||
|
headerBytes[17] |= tableBits - 1;
|
||||||
|
memcpy(writeHead, headerBytes, 18);
|
||||||
|
writeHead += 18;
|
||||||
|
|
||||||
|
//local color table
|
||||||
|
memcpy(writeHead, table, tableSize * sizeof(Color3));
|
||||||
|
writeHead += tableSize * sizeof(Color3);
|
||||||
|
*writeHead++ = tableBits;
|
||||||
|
|
||||||
|
//prep block
|
||||||
|
memset(writeHead, 0, 260);
|
||||||
|
writeHead[0] = 255;
|
||||||
|
uint32_t blockBits = 8; //relative to block.head
|
||||||
|
|
||||||
|
//SPEC: "Encoders should output a Clear code as the first code of each image data stream."
|
||||||
|
msf_lzw_reset(&lzw, tableSize, tableIdx);
|
||||||
|
msf_put_code(&writeHead, &blockBits, msf_bit_log(lzw.len - 1), tableSize);
|
||||||
|
|
||||||
|
int lastCode = framesCompatible && frame.pixels[0] == previous.pixels[0]? 0 : tlb[frame.pixels[0]];
|
||||||
|
MsfTimeLoop("compress") for (int i = 1; i < width * height; ++i) {
|
||||||
|
//PERF: branching vs. branchless version of this line is observed to have no discernable impact on speed
|
||||||
|
int color = framesCompatible && frame.pixels[i] == previous.pixels[i]? 0 : tlb[frame.pixels[i]];
|
||||||
|
int code = (&lzw.data[lastCode * lzw.stride])[color];
|
||||||
|
if (code < 0) {
|
||||||
|
//write to code stream
|
||||||
|
int codeBits = msf_bit_log(lzw.len - 1);
|
||||||
|
msf_put_code(&writeHead, &blockBits, codeBits, lastCode);
|
||||||
|
|
||||||
|
if (lzw.len > 4095) {
|
||||||
|
//reset buffer code table
|
||||||
|
msf_put_code(&writeHead, &blockBits, codeBits, tableSize);
|
||||||
|
msf_lzw_reset(&lzw, tableSize, tableIdx);
|
||||||
|
} else {
|
||||||
|
(&lzw.data[lastCode * lzw.stride])[color] = lzw.len;
|
||||||
|
++lzw.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastCode = color;
|
||||||
|
} else {
|
||||||
|
lastCode = code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//write code for leftover index buffer contents, then the end code
|
||||||
|
msf_put_code(&writeHead, &blockBits, msf_imin(12, msf_bit_log(lzw.len - 1)), lastCode);
|
||||||
|
msf_put_code(&writeHead, &blockBits, msf_imin(12, msf_bit_log(lzw.len)), tableSize + 1);
|
||||||
|
|
||||||
|
//flush remaining data
|
||||||
|
if (blockBits > 8) {
|
||||||
|
int bytes = (blockBits + 7) / 8; //round up
|
||||||
|
writeHead[0] = bytes - 1;
|
||||||
|
writeHead += bytes;
|
||||||
|
}
|
||||||
|
*writeHead++ = 0; //terminating block
|
||||||
|
|
||||||
|
//fill in buffer header and shrink buffer to fit data
|
||||||
|
buffer->next = NULL;
|
||||||
|
buffer->size = writeHead - buffer->data;
|
||||||
|
MsfGifBuffer * moved =
|
||||||
|
(MsfGifBuffer *) MSF_GIF_REALLOC(allocContext, buffer, maxBufSize, offsetof(MsfGifBuffer, data) + buffer->size);
|
||||||
|
if (!moved) { MSF_GIF_FREE(allocContext, buffer, maxBufSize); return NULL; }
|
||||||
|
return moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// To-memory API ///
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
static const int lzwAllocSize = 4096 * 256 * sizeof(int16_t);
|
||||||
|
|
||||||
|
//NOTE: by C standard library conventions, freeing NULL should be a no-op,
|
||||||
|
// but just in case the user's custom free doesn't follow that rule, we do null checks on our end as well.
|
||||||
|
static void msf_free_gif_state(MsfGifState * handle) {
|
||||||
|
if (handle->previousFrame.pixels) MSF_GIF_FREE(handle->customAllocatorContext, handle->previousFrame.pixels,
|
||||||
|
handle->width * handle->height * sizeof(uint32_t));
|
||||||
|
if (handle->currentFrame.pixels) MSF_GIF_FREE(handle->customAllocatorContext, handle->currentFrame.pixels,
|
||||||
|
handle->width * handle->height * sizeof(uint32_t));
|
||||||
|
if (handle->lzwMem) MSF_GIF_FREE(handle->customAllocatorContext, handle->lzwMem, lzwAllocSize);
|
||||||
|
for (MsfGifBuffer * node = handle->listHead; node;) {
|
||||||
|
MsfGifBuffer * next = node->next; //NOTE: we have to copy the `next` pointer BEFORE freeing the node holding it
|
||||||
|
MSF_GIF_FREE(handle->customAllocatorContext, node, offsetof(MsfGifBuffer, data) + node->size);
|
||||||
|
node = next;
|
||||||
|
}
|
||||||
|
handle->listHead = NULL; //this implicitly marks the handle as invalid until the next msf_gif_begin() call
|
||||||
|
}
|
||||||
|
|
||||||
|
int msf_gif_begin(MsfGifState * handle, int width, int height) { MsfTimeFunc
|
||||||
|
//NOTE: we cannot stomp the entire struct to zero because we must preserve `customAllocatorContext`.
|
||||||
|
MsfCookedFrame empty = {0}; //god I hate MSVC...
|
||||||
|
handle->previousFrame = empty;
|
||||||
|
handle->currentFrame = empty;
|
||||||
|
handle->width = width;
|
||||||
|
handle->height = height;
|
||||||
|
handle->framesSubmitted = 0;
|
||||||
|
|
||||||
|
//allocate memory for LZW buffer
|
||||||
|
//NOTE: Unfortunately we can't just use stack memory for the LZW table because it's 2MB,
|
||||||
|
// which is more stack space than most operating systems give by default,
|
||||||
|
// and we can't realistically expect users to be willing to override that just to use our library,
|
||||||
|
// so we have to allocate this on the heap.
|
||||||
|
handle->lzwMem = (int16_t *) MSF_GIF_MALLOC(handle->customAllocatorContext, lzwAllocSize);
|
||||||
|
handle->previousFrame.pixels =
|
||||||
|
(uint32_t *) MSF_GIF_MALLOC(handle->customAllocatorContext, handle->width * handle->height * sizeof(uint32_t));
|
||||||
|
handle->currentFrame.pixels =
|
||||||
|
(uint32_t *) MSF_GIF_MALLOC(handle->customAllocatorContext, handle->width * handle->height * sizeof(uint32_t));
|
||||||
|
|
||||||
|
//setup header buffer header (lol)
|
||||||
|
handle->listHead = (MsfGifBuffer *) MSF_GIF_MALLOC(handle->customAllocatorContext, offsetof(MsfGifBuffer, data) + 32);
|
||||||
|
if (!handle->listHead || !handle->lzwMem || !handle->previousFrame.pixels || !handle->currentFrame.pixels) {
|
||||||
|
msf_free_gif_state(handle);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
handle->listTail = handle->listHead;
|
||||||
|
handle->listHead->next = NULL;
|
||||||
|
handle->listHead->size = 32;
|
||||||
|
|
||||||
|
//NOTE: because __attribute__((__packed__)) is annoyingly compiler-specific, we do this unreadable weirdness
|
||||||
|
char headerBytes[33] = "GIF89a\0\0\0\0\x70\0\0" "\x21\xFF\x0BNETSCAPE2.0\x03\x01\0\0\0";
|
||||||
|
memcpy(&headerBytes[6], &width, 2);
|
||||||
|
memcpy(&headerBytes[8], &height, 2);
|
||||||
|
memcpy(handle->listHead->data, headerBytes, 32);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int msf_gif_frame(MsfGifState * handle, uint8_t * pixelData, int centiSecondsPerFame, int maxBitDepth, int pitchInBytes)
|
||||||
|
{ MsfTimeFunc
|
||||||
|
if (!handle->listHead) { return 0; }
|
||||||
|
|
||||||
|
maxBitDepth = msf_imax(1, msf_imin(16, maxBitDepth));
|
||||||
|
if (pitchInBytes == 0) pitchInBytes = handle->width * 4;
|
||||||
|
if (pitchInBytes < 0) pixelData -= pitchInBytes * (handle->height - 1);
|
||||||
|
|
||||||
|
uint8_t used[(1 << 16) + 1]; //only 64k, so stack allocating is fine
|
||||||
|
msf_cook_frame(&handle->currentFrame, pixelData, used, handle->width, handle->height, pitchInBytes,
|
||||||
|
msf_imin(maxBitDepth, handle->previousFrame.depth + 160 / msf_imax(1, handle->previousFrame.count)));
|
||||||
|
|
||||||
|
MsfGifBuffer * buffer = msf_compress_frame(handle->customAllocatorContext, handle->width, handle->height,
|
||||||
|
centiSecondsPerFame, handle->currentFrame, handle, used, handle->lzwMem);
|
||||||
|
if (!buffer) { msf_free_gif_state(handle); return 0; }
|
||||||
|
handle->listTail->next = buffer;
|
||||||
|
handle->listTail = buffer;
|
||||||
|
|
||||||
|
//swap current and previous frames
|
||||||
|
MsfCookedFrame tmp = handle->previousFrame;
|
||||||
|
handle->previousFrame = handle->currentFrame;
|
||||||
|
handle->currentFrame = tmp;
|
||||||
|
|
||||||
|
handle->framesSubmitted += 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
MsfGifResult msf_gif_end(MsfGifState * handle) { MsfTimeFunc
|
||||||
|
if (!handle->listHead) { MsfGifResult empty = {0}; return empty; }
|
||||||
|
|
||||||
|
//first pass: determine total size
|
||||||
|
size_t total = 1; //1 byte for trailing marker
|
||||||
|
for (MsfGifBuffer * node = handle->listHead; node; node = node->next) { total += node->size; }
|
||||||
|
|
||||||
|
//second pass: write data
|
||||||
|
uint8_t * buffer = (uint8_t *) MSF_GIF_MALLOC(handle->customAllocatorContext, total);
|
||||||
|
if (buffer) {
|
||||||
|
uint8_t * writeHead = buffer;
|
||||||
|
for (MsfGifBuffer * node = handle->listHead; node; node = node->next) {
|
||||||
|
memcpy(writeHead, node->data, node->size);
|
||||||
|
writeHead += node->size;
|
||||||
|
}
|
||||||
|
*writeHead++ = 0x3B;
|
||||||
|
}
|
||||||
|
|
||||||
|
//third pass: free buffers
|
||||||
|
msf_free_gif_state(handle);
|
||||||
|
|
||||||
|
MsfGifResult ret = { buffer, total, total, handle->customAllocatorContext };
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void msf_gif_free(MsfGifResult result) { MsfTimeFunc
|
||||||
|
if (result.data) { MSF_GIF_FREE(result.contextPointer, result.data, result.allocSize); }
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// To-file API ///
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
int msf_gif_begin_to_file(MsfGifState * handle, int width, int height, MsfGifFileWriteFunc func, void * filePointer) {
|
||||||
|
handle->fileWriteFunc = func;
|
||||||
|
handle->fileWriteData = filePointer;
|
||||||
|
return msf_gif_begin(handle, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
int msf_gif_frame_to_file(MsfGifState * handle, uint8_t * pixelData, int centiSecondsPerFame, int maxBitDepth, int pitchInBytes) {
|
||||||
|
if (!msf_gif_frame(handle, pixelData, centiSecondsPerFame, maxBitDepth, pitchInBytes)) { return 0; }
|
||||||
|
|
||||||
|
//NOTE: this is a somewhat hacky implementation which is not perfectly efficient, but it's good enough for now
|
||||||
|
MsfGifBuffer * head = handle->listHead;
|
||||||
|
if (!handle->fileWriteFunc(head->data, head->size, 1, handle->fileWriteData)) { msf_free_gif_state(handle); return 0; }
|
||||||
|
handle->listHead = head->next;
|
||||||
|
MSF_GIF_FREE(handle->customAllocatorContext, head, offsetof(MsfGifBuffer, data) + head->size);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int msf_gif_end_to_file(MsfGifState * handle) {
|
||||||
|
//NOTE: this is a somewhat hacky implementation which is not perfectly efficient, but it's good enough for now
|
||||||
|
MsfGifResult result = msf_gif_end(handle);
|
||||||
|
int ret = (int) handle->fileWriteFunc(result.data, result.dataSize, 1, handle->fileWriteData);
|
||||||
|
msf_gif_free(result);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //MSF_GIF_ALREADY_IMPLEMENTED_IN_THIS_TRANSLATION_UNIT
|
||||||
|
#endif //MSF_GIF_IMPL
|
||||||
|
|
||||||
|
/*
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
This software is available under 2 licenses -- choose whichever you prefer.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
ALTERNATIVE A - MIT License
|
||||||
|
Copyright (c) 2021 Miles Fogle
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||||
|
This is free and unencumbered software released into the public domain.
|
||||||
|
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||||
|
software, either in source code form or as a compiled binary, for any purpose,
|
||||||
|
commercial or non-commercial, and by any means.
|
||||||
|
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||||
|
software dedicate any and all copyright interest in the software to the public
|
||||||
|
domain. We make this dedication for the benefit of the public at large and to
|
||||||
|
the detriment of our heirs and successors. We intend this dedication to be an
|
||||||
|
overt act of relinquishment in perpetuity of all present and future rights to
|
||||||
|
this software under copyright law.
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
1459
nanosvgrast.h
Normal file
1459
nanosvgrast.h
Normal file
File diff suppressed because it is too large
Load Diff
563
par/par_bluenoise.h
Normal file
563
par/par_bluenoise.h
Normal file
@@ -0,0 +1,563 @@
|
|||||||
|
// BLUENOISE :: https://github.com/prideout/par
|
||||||
|
// Generator for infinite 2D point sequences using Recursive Wang Tiles.
|
||||||
|
//
|
||||||
|
// In addition to this source code, you'll need to download one of the following
|
||||||
|
// tilesets, the first being 2 MB while the other is 257 KB. The latter cheats
|
||||||
|
// by referencing the point sequence from the first tile for all 8 tiles. This
|
||||||
|
// obviously produces poor results, but in many contexts, it isn't noticeable.
|
||||||
|
//
|
||||||
|
// https://prideout.net/assets/bluenoise.bin
|
||||||
|
// https://prideout.net/assets/bluenoise.trimmed.bin
|
||||||
|
//
|
||||||
|
// The code herein is an implementation of the algorithm described in:
|
||||||
|
//
|
||||||
|
// Recursive Wang Tiles for Real-Time Blue Noise
|
||||||
|
// Johannes Kopf, Daniel Cohen-Or, Oliver Deussen, Dani Lischinski
|
||||||
|
// ACM Transactions on Graphics 25, 3 (Proc. SIGGRAPH 2006)
|
||||||
|
//
|
||||||
|
// If you use this software for research purposes, please cite the above paper
|
||||||
|
// in any resulting publication.
|
||||||
|
//
|
||||||
|
// EXAMPLE
|
||||||
|
//
|
||||||
|
// Generate point samples whose density is guided by a 512x512 grayscale image:
|
||||||
|
//
|
||||||
|
// int npoints;
|
||||||
|
// float* points;
|
||||||
|
// int maxpoints = 1e6;
|
||||||
|
// float density = 30000;
|
||||||
|
// par_bluenoise_context* ctx;
|
||||||
|
// ctx = par_bluenoise_from_file("bluenoise.bin", maxpoints);
|
||||||
|
// par_bluenoise_density_from_gray(ctx, source_pixels, 512, 512, 1);
|
||||||
|
// points = par_bluenoise_generate(ctx, density, &npoints);
|
||||||
|
// ... Draw points here. Each point is a three-tuple of (X Y RANK).
|
||||||
|
// par_bluenoise_free(ctx);
|
||||||
|
//
|
||||||
|
// Distributed under the MIT License, see bottom of file.
|
||||||
|
|
||||||
|
#ifndef PAR_BLUENOISE_H
|
||||||
|
#define PAR_BLUENOISE_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// BEGIN PUBLIC API
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef unsigned char par_byte;
|
||||||
|
|
||||||
|
// Encapsulates a tile set and an optional density function.
|
||||||
|
typedef struct par_bluenoise_context_s par_bluenoise_context;
|
||||||
|
|
||||||
|
// Creates a bluenoise context using the given tileset. The first argument is
|
||||||
|
// the file path the bin file. The second argument is the maximum number of
|
||||||
|
// points that you expect to ever be generated.
|
||||||
|
par_bluenoise_context* par_bluenoise_from_file(char const* path, int maxpts);
|
||||||
|
|
||||||
|
// Creates a bluenoise context using the given tileset. The first and second
|
||||||
|
// arguments describe a memory buffer containing the contents of the bin file.
|
||||||
|
// The third argument is the maximum number of points that you expect to ever
|
||||||
|
// be generated.
|
||||||
|
par_bluenoise_context* par_bluenoise_from_buffer(
|
||||||
|
par_byte const* buffer, int nbytes, int maxpts);
|
||||||
|
|
||||||
|
// Sets up a scissoring rectangle using the given lower-left and upper-right
|
||||||
|
// coordinates. By default the scissor encompasses [-0.5, -0.5] - [0.5, 0.5],
|
||||||
|
// which is the entire sampling domain for the two "generate" methods.
|
||||||
|
void par_bluenoise_set_viewport(
|
||||||
|
par_bluenoise_context*, float left, float bottom, float right, float top);
|
||||||
|
|
||||||
|
// Sets up a reference window size. The only purpose of this is to ensure
|
||||||
|
// that apparent density remains constant when the window gets resized.
|
||||||
|
// Clients should call this *before* calling par_bluenoise_set_viewport.
|
||||||
|
void par_bluenoise_set_window(par_bluenoise_context*, int width, int height);
|
||||||
|
|
||||||
|
// Frees all memory associated with the given bluenoise context.
|
||||||
|
void par_bluenoise_free(par_bluenoise_context* ctx);
|
||||||
|
|
||||||
|
// Copies a grayscale image into the bluenoise context to guide point density.
|
||||||
|
// Darker regions generate a higher number of points. The given bytes-per-pixel
|
||||||
|
// value is the stride between pixels.
|
||||||
|
void par_bluenoise_density_from_gray(par_bluenoise_context* ctx,
|
||||||
|
const unsigned char* pixels, int width, int height, int bpp);
|
||||||
|
|
||||||
|
// Creates a binary mask to guide point density. The given bytes-per-pixel
|
||||||
|
// value is the stride between pixels, which must be 4 or less.
|
||||||
|
void par_bluenoise_density_from_color(par_bluenoise_context* ctx,
|
||||||
|
const unsigned char* pixels, int width, int height, int bpp,
|
||||||
|
unsigned int background_color, int invert);
|
||||||
|
|
||||||
|
// Generates samples using Recursive Wang Tiles. This is really fast!
|
||||||
|
// The returned pointer is a list of three-tuples, where XY are in [-0.5, +0.5]
|
||||||
|
// and Z is a rank value that can be used to create a progressive ordering.
|
||||||
|
// The caller should not free the returned pointer.
|
||||||
|
float* par_bluenoise_generate(
|
||||||
|
par_bluenoise_context* ctx, float density, int* npts);
|
||||||
|
|
||||||
|
// Generates an ordered sequence of tuples with the specified sequence length.
|
||||||
|
// This is slower than the other "generate" method because it uses a dumb
|
||||||
|
// backtracking method to determine a reasonable density value, and it
|
||||||
|
// automatically sorts the output by rank. The dims argument must be 2 or more;
|
||||||
|
// it represents the desired stride (in floats) between consecutive verts in the
|
||||||
|
// returned data buffer.
|
||||||
|
float* par_bluenoise_generate_exact(
|
||||||
|
par_bluenoise_context* ctx, int npts, int dims);
|
||||||
|
|
||||||
|
// Performs an in-place sort of 3-tuples, based on the 3rd component, then
|
||||||
|
// replaces the 3rd component with an index.
|
||||||
|
void par_bluenoise_sort_by_rank(float* pts, int npts);
|
||||||
|
|
||||||
|
#ifndef PAR_PI
|
||||||
|
#define PAR_PI (3.14159265359)
|
||||||
|
#define PAR_MIN(a, b) (a > b ? b : a)
|
||||||
|
#define PAR_MAX(a, b) (a > b ? a : b)
|
||||||
|
#define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
|
||||||
|
#define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
|
||||||
|
#define PAR_SQR(a) ((a) * (a))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef PAR_MALLOC
|
||||||
|
#define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
|
||||||
|
#define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
|
||||||
|
#define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * (N)))
|
||||||
|
#define PAR_FREE(BUF) free(BUF)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// END PUBLIC API
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifdef PAR_BLUENOISE_IMPLEMENTATION
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define PAR_MINI(a, b) ((a < b) ? a : b)
|
||||||
|
#define PAR_MAXI(a, b) ((a > b) ? a : b)
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
} par_vec2;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float rank;
|
||||||
|
} par_vec3;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int n, e, s, w;
|
||||||
|
int nsubtiles, nsubdivs, npoints, nsubpts;
|
||||||
|
int** subdivs;
|
||||||
|
par_vec2* points;
|
||||||
|
par_vec2* subpts;
|
||||||
|
} par_tile;
|
||||||
|
|
||||||
|
struct par_bluenoise_context_s {
|
||||||
|
par_vec3* points;
|
||||||
|
par_tile* tiles;
|
||||||
|
float global_density;
|
||||||
|
float left, bottom, right, top;
|
||||||
|
int ntiles, nsubtiles, nsubdivs;
|
||||||
|
int npoints;
|
||||||
|
int maxpoints;
|
||||||
|
int density_width;
|
||||||
|
int density_height;
|
||||||
|
unsigned char* density;
|
||||||
|
float mag;
|
||||||
|
int window_width;
|
||||||
|
int window_height;
|
||||||
|
int abridged;
|
||||||
|
};
|
||||||
|
|
||||||
|
static float sample_density(par_bluenoise_context* ctx, float x, float y)
|
||||||
|
{
|
||||||
|
unsigned char* density = ctx->density;
|
||||||
|
if (!density) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int width = ctx->density_width;
|
||||||
|
int height = ctx->density_height;
|
||||||
|
y = 1 - y;
|
||||||
|
x -= 0.5;
|
||||||
|
y -= 0.5;
|
||||||
|
float tx = x * PAR_MAXI(width, height);
|
||||||
|
float ty = y * PAR_MAXI(width, height);
|
||||||
|
x += 0.5;
|
||||||
|
y += 0.5;
|
||||||
|
tx += width / 2;
|
||||||
|
ty += height / 2;
|
||||||
|
int ix = PAR_CLAMP((int) tx, 0, width - 2);
|
||||||
|
int iy = PAR_CLAMP((int) ty, 0, height - 2);
|
||||||
|
return density[iy * width + ix] / 255.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void recurse_tile(
|
||||||
|
par_bluenoise_context* ctx, par_tile* tile, float x, float y, int level)
|
||||||
|
{
|
||||||
|
float left = ctx->left, right = ctx->right;
|
||||||
|
float top = ctx->top, bottom = ctx->bottom;
|
||||||
|
float mag = ctx->mag;
|
||||||
|
float tileSize = 1.f / powf(ctx->nsubtiles, level);
|
||||||
|
if (x + tileSize < left || x > right || y + tileSize < bottom || y > top) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
float depth = powf(ctx->nsubtiles, 2 * level);
|
||||||
|
float threshold = mag / depth * ctx->global_density - tile->npoints;
|
||||||
|
int ntests = PAR_MINI(tile->nsubpts, threshold);
|
||||||
|
float factor = 1.f / mag * depth / ctx->global_density;
|
||||||
|
for (int i = 0; i < ntests; i++) {
|
||||||
|
float px = x + tile->subpts[i].x * tileSize;
|
||||||
|
float py = y + tile->subpts[i].y * tileSize;
|
||||||
|
if (px < left || px > right || py < bottom || py > top) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (sample_density(ctx, px, py) < (i + tile->npoints) * factor) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
ctx->points[ctx->npoints].x = px - 0.5;
|
||||||
|
ctx->points[ctx->npoints].y = py - 0.5;
|
||||||
|
ctx->points[ctx->npoints].rank = (level + 1) + i * factor;
|
||||||
|
ctx->npoints++;
|
||||||
|
if (ctx->npoints >= ctx->maxpoints) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const float scale = tileSize / ctx->nsubtiles;
|
||||||
|
if (threshold <= tile->nsubpts) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
level++;
|
||||||
|
for (int ty = 0; ty < ctx->nsubtiles; ty++) {
|
||||||
|
for (int tx = 0; tx < ctx->nsubtiles; tx++) {
|
||||||
|
int tileIndex = tile->subdivs[0][ty * ctx->nsubtiles + tx];
|
||||||
|
par_tile* subtile = &ctx->tiles[tileIndex];
|
||||||
|
recurse_tile(ctx, subtile, x + tx * scale, y + ty * scale, level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void par_bluenoise_set_window(par_bluenoise_context* ctx, int width, int height)
|
||||||
|
{
|
||||||
|
ctx->window_width = width;
|
||||||
|
ctx->window_height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
void par_bluenoise_set_viewport(par_bluenoise_context* ctx, float left,
|
||||||
|
float bottom, float right, float top)
|
||||||
|
{
|
||||||
|
// Transform [-.5, +.5] to [0, 1]
|
||||||
|
left = ctx->left = left + 0.5;
|
||||||
|
right = ctx->right = right + 0.5;
|
||||||
|
bottom = ctx->bottom = bottom + 0.5;
|
||||||
|
top = ctx->top = top + 0.5;
|
||||||
|
|
||||||
|
// Determine magnification factor BEFORE clamping.
|
||||||
|
float scale = 1000 * (top - bottom) / ctx->window_height;
|
||||||
|
ctx->mag = powf(scale, -2);
|
||||||
|
|
||||||
|
// The density function is only sampled in [0, +1].
|
||||||
|
ctx->left = PAR_CLAMP(left, 0, 1);
|
||||||
|
ctx->right = PAR_CLAMP(right, 0, 1);
|
||||||
|
ctx->bottom = PAR_CLAMP(bottom, 0, 1);
|
||||||
|
ctx->top = PAR_CLAMP(top, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
float* par_bluenoise_generate(
|
||||||
|
par_bluenoise_context* ctx, float density, int* npts)
|
||||||
|
{
|
||||||
|
ctx->global_density = density;
|
||||||
|
ctx->npoints = 0;
|
||||||
|
float left = ctx->left;
|
||||||
|
float right = ctx->right;
|
||||||
|
float bottom = ctx->bottom;
|
||||||
|
float top = ctx->top;
|
||||||
|
float mag = ctx->mag;
|
||||||
|
|
||||||
|
int ntests = PAR_MINI(ctx->tiles[0].npoints, mag * ctx->global_density);
|
||||||
|
float factor = 1.f / mag / ctx->global_density;
|
||||||
|
for (int i = 0; i < ntests; i++) {
|
||||||
|
float px = ctx->tiles[0].points[i].x;
|
||||||
|
float py = ctx->tiles[0].points[i].y;
|
||||||
|
if (px < left || px > right || py < bottom || py > top) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (sample_density(ctx, px, py) < (i + 1) * factor) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
ctx->points[ctx->npoints].x = px - 0.5;
|
||||||
|
ctx->points[ctx->npoints].y = py - 0.5;
|
||||||
|
ctx->points[ctx->npoints].rank = i * factor;
|
||||||
|
ctx->npoints++;
|
||||||
|
if (ctx->npoints >= ctx->maxpoints) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
recurse_tile(ctx, &ctx->tiles[0], 0, 0, 0);
|
||||||
|
*npts = ctx->npoints;
|
||||||
|
return &ctx->points->x;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define freadi() \
|
||||||
|
*((int*) ptr); \
|
||||||
|
ptr += sizeof(int)
|
||||||
|
|
||||||
|
#define freadf() \
|
||||||
|
*((float*) ptr); \
|
||||||
|
ptr += sizeof(float)
|
||||||
|
|
||||||
|
static par_bluenoise_context* par_bluenoise_create(
|
||||||
|
char const* filepath, int nbytes, int maxpts)
|
||||||
|
{
|
||||||
|
par_bluenoise_context* ctx = PAR_MALLOC(par_bluenoise_context, 1);
|
||||||
|
ctx->maxpoints = maxpts;
|
||||||
|
ctx->points = PAR_MALLOC(par_vec3, maxpts);
|
||||||
|
ctx->density = 0;
|
||||||
|
ctx->abridged = 0;
|
||||||
|
par_bluenoise_set_window(ctx, 1024, 768);
|
||||||
|
par_bluenoise_set_viewport(ctx, -.5, -.5, .5, .5);
|
||||||
|
|
||||||
|
char* buf = 0;
|
||||||
|
if (nbytes == 0) {
|
||||||
|
FILE* fin = fopen(filepath, "rb");
|
||||||
|
assert(fin);
|
||||||
|
fseek(fin, 0, SEEK_END);
|
||||||
|
nbytes = (int) ftell(fin);
|
||||||
|
fseek(fin, 0, SEEK_SET);
|
||||||
|
buf = PAR_MALLOC(char, nbytes);
|
||||||
|
int consumed = (int) fread(buf, nbytes, 1, fin);
|
||||||
|
assert(consumed == 1);
|
||||||
|
fclose(fin);
|
||||||
|
}
|
||||||
|
|
||||||
|
char const* ptr = buf ? buf : filepath;
|
||||||
|
int ntiles = ctx->ntiles = freadi();
|
||||||
|
int nsubtiles = ctx->nsubtiles = freadi();
|
||||||
|
int nsubdivs = ctx->nsubdivs = freadi();
|
||||||
|
par_tile* tiles = ctx->tiles = PAR_MALLOC(par_tile, ntiles);
|
||||||
|
for (int i = 0; i < ntiles; i++) {
|
||||||
|
tiles[i].n = freadi();
|
||||||
|
tiles[i].e = freadi();
|
||||||
|
tiles[i].s = freadi();
|
||||||
|
tiles[i].w = freadi();
|
||||||
|
tiles[i].subdivs = PAR_MALLOC(int*, nsubdivs);
|
||||||
|
for (int j = 0; j < nsubdivs; j++) {
|
||||||
|
int* subdiv = PAR_MALLOC(int, PAR_SQR(nsubtiles));
|
||||||
|
for (int k = 0; k < PAR_SQR(nsubtiles); k++) {
|
||||||
|
subdiv[k] = freadi();
|
||||||
|
}
|
||||||
|
tiles[i].subdivs[j] = subdiv;
|
||||||
|
}
|
||||||
|
tiles[i].npoints = freadi();
|
||||||
|
tiles[i].points = PAR_MALLOC(par_vec2, tiles[i].npoints);
|
||||||
|
for (int j = 0; j < tiles[i].npoints; j++) {
|
||||||
|
tiles[i].points[j].x = freadf();
|
||||||
|
tiles[i].points[j].y = freadf();
|
||||||
|
}
|
||||||
|
tiles[i].nsubpts = freadi();
|
||||||
|
tiles[i].subpts = PAR_MALLOC(par_vec2, tiles[i].nsubpts);
|
||||||
|
for (int j = 0; j < tiles[i].nsubpts; j++) {
|
||||||
|
tiles[i].subpts[j].x = freadf();
|
||||||
|
tiles[i].subpts[j].y = freadf();
|
||||||
|
}
|
||||||
|
|
||||||
|
// The following hack allows for an optimization whereby
|
||||||
|
// the first tile's point set is re-used for every other tile.
|
||||||
|
// This goes against the entire purpose of Recursive Wang Tiles,
|
||||||
|
// but in many applications the qualatitive loss is not
|
||||||
|
// observable, and the footprint savings are huge (10x).
|
||||||
|
|
||||||
|
if (tiles[i].npoints == 0) {
|
||||||
|
ctx->abridged = 1;
|
||||||
|
tiles[i].npoints = tiles[0].npoints;
|
||||||
|
tiles[i].points = tiles[0].points;
|
||||||
|
tiles[i].nsubpts = tiles[0].nsubpts;
|
||||||
|
tiles[i].subpts = tiles[0].subpts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(buf);
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
par_bluenoise_context* par_bluenoise_from_file(char const* path, int maxpts)
|
||||||
|
{
|
||||||
|
return par_bluenoise_create(path, 0, maxpts);
|
||||||
|
}
|
||||||
|
|
||||||
|
par_bluenoise_context* par_bluenoise_from_buffer(
|
||||||
|
par_byte const* buffer, int nbytes, int maxpts)
|
||||||
|
{
|
||||||
|
return par_bluenoise_create((char const*) buffer, nbytes, maxpts);
|
||||||
|
}
|
||||||
|
|
||||||
|
void par_bluenoise_density_from_gray(par_bluenoise_context* ctx,
|
||||||
|
const unsigned char* pixels, int width, int height, int bpp)
|
||||||
|
{
|
||||||
|
ctx->density_width = width;
|
||||||
|
ctx->density_height = height;
|
||||||
|
ctx->density = PAR_MALLOC(unsigned char, width * height);
|
||||||
|
unsigned char* dst = ctx->density;
|
||||||
|
for (int j = 0; j < height; j++) {
|
||||||
|
for (int i = 0; i < width; i++) {
|
||||||
|
*dst++ = 255 - (*pixels);
|
||||||
|
pixels += bpp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void par_bluenoise_density_from_color(par_bluenoise_context* ctx,
|
||||||
|
const unsigned char* pixels, int width, int height, int bpp,
|
||||||
|
unsigned int background_color, int invert)
|
||||||
|
{
|
||||||
|
unsigned int bkgd = background_color;
|
||||||
|
ctx->density_width = width;
|
||||||
|
ctx->density_height = height;
|
||||||
|
ctx->density = PAR_MALLOC(unsigned char, width * height);
|
||||||
|
unsigned char* dst = ctx->density;
|
||||||
|
unsigned int mask = 0x000000ffu;
|
||||||
|
if (bpp > 1) {
|
||||||
|
mask |= 0x0000ff00u;
|
||||||
|
}
|
||||||
|
if (bpp > 2) {
|
||||||
|
mask |= 0x00ff0000u;
|
||||||
|
}
|
||||||
|
if (bpp > 3) {
|
||||||
|
mask |= 0xff000000u;
|
||||||
|
}
|
||||||
|
assert(bpp <= 4);
|
||||||
|
for (int j = 0; j < height; j++) {
|
||||||
|
for (int i = 0; i < width; i++) {
|
||||||
|
unsigned int val = (*((unsigned int*) pixels)) & mask;
|
||||||
|
val = invert ? (val == bkgd) : (val != bkgd);
|
||||||
|
*dst++ = val ? 255 : 0;
|
||||||
|
pixels += bpp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void par_bluenoise_free(par_bluenoise_context* ctx)
|
||||||
|
{
|
||||||
|
free(ctx->points);
|
||||||
|
for (int t = 0; t < ctx->ntiles; t++) {
|
||||||
|
for (int s = 0; s < ctx->nsubdivs; s++) {
|
||||||
|
free(ctx->tiles[t].subdivs[s]);
|
||||||
|
}
|
||||||
|
free(ctx->tiles[t].subdivs);
|
||||||
|
if (t == 0 || !ctx->abridged) {
|
||||||
|
free(ctx->tiles[t].points);
|
||||||
|
free(ctx->tiles[t].subpts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(ctx->tiles);
|
||||||
|
free(ctx->density);
|
||||||
|
}
|
||||||
|
|
||||||
|
int cmp(const void* a, const void* b)
|
||||||
|
{
|
||||||
|
const par_vec3* v1 = (const par_vec3*) a;
|
||||||
|
const par_vec3* v2 = (const par_vec3*) b;
|
||||||
|
if (v1->rank < v2->rank) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (v1->rank > v2->rank) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void par_bluenoise_sort_by_rank(float* floats, int npts)
|
||||||
|
{
|
||||||
|
par_vec3* vecs = (par_vec3*) floats;
|
||||||
|
qsort(vecs, npts, sizeof(vecs[0]), cmp);
|
||||||
|
for (int i = 0; i < npts; i++) {
|
||||||
|
vecs[i].rank = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float* par_bluenoise_generate_exact(
|
||||||
|
par_bluenoise_context* ctx, int npts, int stride)
|
||||||
|
{
|
||||||
|
assert(stride >= 2);
|
||||||
|
int maxpoints = npts * 2;
|
||||||
|
if (ctx->maxpoints < maxpoints) {
|
||||||
|
free(ctx->points);
|
||||||
|
ctx->maxpoints = maxpoints;
|
||||||
|
ctx->points = PAR_MALLOC(par_vec3, maxpoints);
|
||||||
|
}
|
||||||
|
int ngenerated = 0;
|
||||||
|
int nprevious = 0;
|
||||||
|
int ndesired = npts;
|
||||||
|
float density = 2048;
|
||||||
|
while (ngenerated < ndesired) {
|
||||||
|
par_bluenoise_generate(ctx, density, &ngenerated);
|
||||||
|
|
||||||
|
// Might be paranoid, but break if something fishy is going on:
|
||||||
|
if (ngenerated == nprevious) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perform crazy heuristic to approach a nice density:
|
||||||
|
if (ndesired / ngenerated >= 2) {
|
||||||
|
density *= 2;
|
||||||
|
} else {
|
||||||
|
density += density / 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
nprevious = ngenerated;
|
||||||
|
}
|
||||||
|
par_bluenoise_sort_by_rank(&ctx->points->x, ngenerated);
|
||||||
|
if (stride != 3) {
|
||||||
|
int nbytes = sizeof(float) * stride * ndesired;
|
||||||
|
float* pts = PAR_MALLOC(float, stride * ndesired);
|
||||||
|
float* dst = pts;
|
||||||
|
const float* src = &ctx->points->x;
|
||||||
|
for (int i = 0; i < ndesired; i++, src++) {
|
||||||
|
*dst++ = *src++;
|
||||||
|
*dst++ = *src++;
|
||||||
|
if (stride > 3) {
|
||||||
|
*dst++ = *src;
|
||||||
|
dst += stride - 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
memcpy(ctx->points, pts, nbytes);
|
||||||
|
free(pts);
|
||||||
|
}
|
||||||
|
return &ctx->points->x;
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef PAR_MINI
|
||||||
|
#undef PAR_MAXI
|
||||||
|
|
||||||
|
#endif // PAR_BLUENOISE_IMPLEMENTATION
|
||||||
|
#endif // PAR_BLUENOISE_H
|
||||||
|
|
||||||
|
// par_bluenoise is distributed under the MIT license:
|
||||||
|
//
|
||||||
|
// Copyright (c) 2019 Philip Rideout
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in
|
||||||
|
// all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
1451
par/par_bubbles.h
Normal file
1451
par/par_bubbles.h
Normal file
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user