Unnamed: 0
int64 0
0
| repo_id
stringlengths 5
186
| file_path
stringlengths 15
223
| content
stringlengths 1
32.8M
⌀ |
---|---|---|---|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dotnetc/EditorForm.Designer.cs
|
///////////////////////////////////////////////////////////////////////////////
// //
// EditorForm.Designer..cs //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
///////////////////////////////////////////////////////////////////////////////
namespace MainNs
{
partial class EditorForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.TheStatusStrip = new System.Windows.Forms.StatusStrip();
this.TheStatusStripLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.TheMenuStrip = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.NewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.recentFilesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripSeparator();
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.editToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.undoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripSeparator();
this.cutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.copyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.pasteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.deleteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripSeparator();
this.selectAllToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripSeparator();
this.findAndReplaceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.quickFindToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.goToToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.fileVariablesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.FontGrowToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.FontShrinkToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.autoUpdateToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.bitstreamToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.bitstreamFromClipboardToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ColorMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.debugInformationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.errorListToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.renderToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.outputToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.buildToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.compileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.exportCompiledObjectToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.optionsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.rewriterToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.rewriteNobodyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.CodeBox = new System.Windows.Forms.RichTextBox();
this.AnalysisTabControl = new System.Windows.Forms.TabControl();
this.CompilationTabPage = new System.Windows.Forms.TabPage();
this.btnCompile = new System.Windows.Forms.Button();
this.tbOptions = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.cbProfile = new System.Windows.Forms.ComboBox();
this.label5 = new System.Windows.Forms.Label();
this.tbEntry = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.DisassemblyTabPage = new System.Windows.Forms.TabPage();
this.DisassemblyTextBox = new System.Windows.Forms.RichTextBox();
this.ASTTabPage = new System.Windows.Forms.TabPage();
this.ASTDumpBox = new System.Windows.Forms.RichTextBox();
this.OptimizerTabPage = new System.Windows.Forms.TabPage();
this.InteractiveEditorButton = new System.Windows.Forms.Button();
this.ResetDefaultPassesButton = new System.Windows.Forms.Button();
this.AnalyzeCheckBox = new System.Windows.Forms.CheckBox();
this.AddPrintModuleButton = new System.Windows.Forms.Button();
this.RunPassesButton = new System.Windows.Forms.Button();
this.SelectPassDownButton = new System.Windows.Forms.Button();
this.SelectPassUpButton = new System.Windows.Forms.Button();
this.SelectedPassesBox = new System.Windows.Forms.ListBox();
this.PassesContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
this.PassPropertiesMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem5 = new System.Windows.Forms.ToolStripSeparator();
this.copyToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.copyAllToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.PastePassesMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.DeleteAllPassesMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.AvailablePassesBox = new System.Windows.Forms.ListBox();
this.TheToolTip = new System.Windows.Forms.ToolTip(this.components);
this.TopSplitContainer = new System.Windows.Forms.SplitContainer();
this.OutputTabControl = new System.Windows.Forms.TabControl();
this.RenderLogTabPage = new System.Windows.Forms.TabPage();
this.RenderLogBox = new System.Windows.Forms.TextBox();
this.RewriterOutputTextBox = new System.Windows.Forms.RichTextBox();
this.TheStatusStrip.SuspendLayout();
this.TheMenuStrip.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.AnalysisTabControl.SuspendLayout();
this.CompilationTabPage.SuspendLayout();
this.DisassemblyTabPage.SuspendLayout();
this.ASTTabPage.SuspendLayout();
this.OptimizerTabPage.SuspendLayout();
this.PassesContextMenu.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.TopSplitContainer)).BeginInit();
this.TopSplitContainer.Panel1.SuspendLayout();
this.TopSplitContainer.Panel2.SuspendLayout();
this.TopSplitContainer.SuspendLayout();
this.OutputTabControl.SuspendLayout();
this.RenderLogTabPage.SuspendLayout();
this.SuspendLayout();
//
// TheStatusStrip
//
this.TheStatusStrip.ImageScalingSize = new System.Drawing.Size(24, 24);
this.TheStatusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.TheStatusStripLabel});
this.TheStatusStrip.Location = new System.Drawing.Point(0, 1155);
this.TheStatusStrip.Name = "TheStatusStrip";
this.TheStatusStrip.Padding = new System.Windows.Forms.Padding(2, 0, 8, 0);
this.TheStatusStrip.Size = new System.Drawing.Size(2115, 42);
this.TheStatusStrip.TabIndex = 0;
this.TheStatusStrip.Text = "statusStrip1";
//
// TheStatusStripLabel
//
this.TheStatusStripLabel.Name = "TheStatusStripLabel";
this.TheStatusStripLabel.Size = new System.Drawing.Size(84, 32);
this.TheStatusStripLabel.Text = "Ready.";
//
// TheMenuStrip
//
this.TheMenuStrip.ImageScalingSize = new System.Drawing.Size(24, 24);
this.TheMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem,
this.editToolStripMenuItem,
this.viewToolStripMenuItem,
this.buildToolStripMenuItem,
this.toolsToolStripMenuItem,
this.helpToolStripMenuItem});
this.TheMenuStrip.Location = new System.Drawing.Point(0, 0);
this.TheMenuStrip.Name = "TheMenuStrip";
this.TheMenuStrip.Padding = new System.Windows.Forms.Padding(3, 1, 0, 1);
this.TheMenuStrip.Size = new System.Drawing.Size(2115, 38);
this.TheMenuStrip.TabIndex = 1;
this.TheMenuStrip.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.NewToolStripMenuItem,
this.openToolStripMenuItem,
this.saveToolStripMenuItem,
this.saveAsToolStripMenuItem,
this.toolStripSeparator1,
this.recentFilesToolStripMenuItem,
this.toolStripMenuItem4,
this.exitToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(72, 36);
this.fileToolStripMenuItem.Text = "&File";
this.fileToolStripMenuItem.DropDownOpening += new System.EventHandler(this.fileToolStripMenuItem_DropDownOpening);
//
// NewToolStripMenuItem
//
this.NewToolStripMenuItem.Name = "NewToolStripMenuItem";
this.NewToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N)));
this.NewToolStripMenuItem.Size = new System.Drawing.Size(309, 44);
this.NewToolStripMenuItem.Text = "&New";
this.NewToolStripMenuItem.Click += new System.EventHandler(this.NewToolStripMenuItem_Click);
//
// openToolStripMenuItem
//
this.openToolStripMenuItem.Name = "openToolStripMenuItem";
this.openToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
this.openToolStripMenuItem.Size = new System.Drawing.Size(309, 44);
this.openToolStripMenuItem.Text = "&Open...";
this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S)));
this.saveToolStripMenuItem.Size = new System.Drawing.Size(309, 44);
this.saveToolStripMenuItem.Text = "&Save";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
//
// saveAsToolStripMenuItem
//
this.saveAsToolStripMenuItem.Name = "saveAsToolStripMenuItem";
this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(309, 44);
this.saveAsToolStripMenuItem.Text = "Save &As...";
this.saveAsToolStripMenuItem.Click += new System.EventHandler(this.saveAsToolStripMenuItem_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(306, 6);
//
// recentFilesToolStripMenuItem
//
this.recentFilesToolStripMenuItem.Name = "recentFilesToolStripMenuItem";
this.recentFilesToolStripMenuItem.Size = new System.Drawing.Size(309, 44);
this.recentFilesToolStripMenuItem.Text = "Recent &Files";
//
// toolStripMenuItem4
//
this.toolStripMenuItem4.Name = "toolStripMenuItem4";
this.toolStripMenuItem4.Size = new System.Drawing.Size(306, 6);
//
// exitToolStripMenuItem
//
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(309, 44);
this.exitToolStripMenuItem.Text = "E&xit";
this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
//
// editToolStripMenuItem
//
this.editToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.undoToolStripMenuItem,
this.toolStripMenuItem1,
this.cutToolStripMenuItem,
this.copyToolStripMenuItem,
this.pasteToolStripMenuItem,
this.deleteToolStripMenuItem,
this.toolStripMenuItem2,
this.selectAllToolStripMenuItem,
this.toolStripMenuItem3,
this.findAndReplaceToolStripMenuItem,
this.goToToolStripMenuItem,
this.fileVariablesToolStripMenuItem,
this.FontGrowToolStripMenuItem,
this.FontShrinkToolStripMenuItem});
this.editToolStripMenuItem.Name = "editToolStripMenuItem";
this.editToolStripMenuItem.Size = new System.Drawing.Size(75, 36);
this.editToolStripMenuItem.Text = "&Edit";
//
// undoToolStripMenuItem
//
this.undoToolStripMenuItem.Name = "undoToolStripMenuItem";
this.undoToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Z)));
this.undoToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.undoToolStripMenuItem.Text = "&Undo";
this.undoToolStripMenuItem.Click += new System.EventHandler(this.undoToolStripMenuItem_Click);
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(328, 6);
//
// cutToolStripMenuItem
//
this.cutToolStripMenuItem.Name = "cutToolStripMenuItem";
this.cutToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.X)));
this.cutToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.cutToolStripMenuItem.Text = "Cu&t";
this.cutToolStripMenuItem.Click += new System.EventHandler(this.cutToolStripMenuItem_Click);
//
// copyToolStripMenuItem
//
this.copyToolStripMenuItem.Name = "copyToolStripMenuItem";
this.copyToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C)));
this.copyToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.copyToolStripMenuItem.Text = "&Copy";
this.copyToolStripMenuItem.Click += new System.EventHandler(this.copyToolStripMenuItem_Click);
//
// pasteToolStripMenuItem
//
this.pasteToolStripMenuItem.Name = "pasteToolStripMenuItem";
this.pasteToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.V)));
this.pasteToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.pasteToolStripMenuItem.Text = "&Paste";
this.pasteToolStripMenuItem.Click += new System.EventHandler(this.pasteToolStripMenuItem_Click);
//
// deleteToolStripMenuItem
//
this.deleteToolStripMenuItem.Name = "deleteToolStripMenuItem";
this.deleteToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.deleteToolStripMenuItem.Text = "&Delete";
this.deleteToolStripMenuItem.Click += new System.EventHandler(this.deleteToolStripMenuItem_Click);
//
// toolStripMenuItem2
//
this.toolStripMenuItem2.Name = "toolStripMenuItem2";
this.toolStripMenuItem2.Size = new System.Drawing.Size(328, 6);
//
// selectAllToolStripMenuItem
//
this.selectAllToolStripMenuItem.Name = "selectAllToolStripMenuItem";
this.selectAllToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.A)));
this.selectAllToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.selectAllToolStripMenuItem.Text = "Select &All";
this.selectAllToolStripMenuItem.Click += new System.EventHandler(this.selectAllToolStripMenuItem_Click);
//
// toolStripMenuItem3
//
this.toolStripMenuItem3.Name = "toolStripMenuItem3";
this.toolStripMenuItem3.Size = new System.Drawing.Size(328, 6);
//
// findAndReplaceToolStripMenuItem
//
this.findAndReplaceToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.quickFindToolStripMenuItem});
this.findAndReplaceToolStripMenuItem.Name = "findAndReplaceToolStripMenuItem";
this.findAndReplaceToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.findAndReplaceToolStripMenuItem.Text = "&Find and Replace";
//
// quickFindToolStripMenuItem
//
this.quickFindToolStripMenuItem.Name = "quickFindToolStripMenuItem";
this.quickFindToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.F)));
this.quickFindToolStripMenuItem.Size = new System.Drawing.Size(343, 44);
this.quickFindToolStripMenuItem.Text = "Quick &Find";
this.quickFindToolStripMenuItem.Click += new System.EventHandler(this.quickFindToolStripMenuItem_Click);
//
// goToToolStripMenuItem
//
this.goToToolStripMenuItem.Name = "goToToolStripMenuItem";
this.goToToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.G)));
this.goToToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.goToToolStripMenuItem.Text = "&Go To...";
this.goToToolStripMenuItem.Click += new System.EventHandler(this.goToToolStripMenuItem_Click);
//
// fileVariablesToolStripMenuItem
//
this.fileVariablesToolStripMenuItem.Name = "fileVariablesToolStripMenuItem";
this.fileVariablesToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.fileVariablesToolStripMenuItem.Text = "File &Variables...";
this.fileVariablesToolStripMenuItem.Click += new System.EventHandler(this.fileVariablesToolStripMenuItem_Click);
//
// FontGrowToolStripMenuItem
//
this.FontGrowToolStripMenuItem.Name = "FontGrowToolStripMenuItem";
this.FontGrowToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift)
| System.Windows.Forms.Keys.OemPeriod)));
this.FontGrowToolStripMenuItem.ShowShortcutKeys = false;
this.FontGrowToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.FontGrowToolStripMenuItem.Text = "Font G&row";
this.FontGrowToolStripMenuItem.Click += new System.EventHandler(this.FontGrowToolStripMenuItem_Click);
//
// FontShrinkToolStripMenuItem
//
this.FontShrinkToolStripMenuItem.Name = "FontShrinkToolStripMenuItem";
this.FontShrinkToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift)
| System.Windows.Forms.Keys.Oemcomma)));
this.FontShrinkToolStripMenuItem.ShowShortcutKeys = false;
this.FontShrinkToolStripMenuItem.Size = new System.Drawing.Size(331, 44);
this.FontShrinkToolStripMenuItem.Text = "Font Shrin&k";
this.FontShrinkToolStripMenuItem.Click += new System.EventHandler(this.FontShrinkToolStripMenuItem_Click);
//
// viewToolStripMenuItem
//
this.viewToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.autoUpdateToolStripMenuItem,
this.bitstreamToolStripMenuItem,
this.bitstreamFromClipboardToolStripMenuItem,
this.ColorMenuItem,
this.debugInformationToolStripMenuItem,
this.errorListToolStripMenuItem,
this.renderToolStripMenuItem,
this.outputToolStripMenuItem});
this.viewToolStripMenuItem.Name = "viewToolStripMenuItem";
this.viewToolStripMenuItem.Size = new System.Drawing.Size(86, 36);
this.viewToolStripMenuItem.Text = "&View";
//
// autoUpdateToolStripMenuItem
//
this.autoUpdateToolStripMenuItem.Name = "autoUpdateToolStripMenuItem";
this.autoUpdateToolStripMenuItem.Size = new System.Drawing.Size(413, 44);
this.autoUpdateToolStripMenuItem.Text = "&Auto-Update";
this.autoUpdateToolStripMenuItem.Click += new System.EventHandler(this.autoUpdateToolStripMenuItem_Click);
//
// bitstreamToolStripMenuItem
//
this.bitstreamToolStripMenuItem.Name = "bitstreamToolStripMenuItem";
this.bitstreamToolStripMenuItem.Size = new System.Drawing.Size(413, 44);
this.bitstreamToolStripMenuItem.Text = "&Bitstream";
this.bitstreamToolStripMenuItem.Click += new System.EventHandler(this.bitstreamToolStripMenuItem_Click);
//
// bitstreamFromClipboardToolStripMenuItem
//
this.bitstreamFromClipboardToolStripMenuItem.Name = "bitstreamFromClipboardToolStripMenuItem";
this.bitstreamFromClipboardToolStripMenuItem.Size = new System.Drawing.Size(413, 44);
this.bitstreamFromClipboardToolStripMenuItem.Text = "Bitstream from clipboard";
this.bitstreamFromClipboardToolStripMenuItem.Click += new System.EventHandler(this.bitstreamFromClipboardToolStripMenuItem_Click);
//
// ColorMenuItem
//
this.ColorMenuItem.Name = "ColorMenuItem";
this.ColorMenuItem.Size = new System.Drawing.Size(413, 44);
this.ColorMenuItem.Text = "&Color";
this.ColorMenuItem.Click += new System.EventHandler(this.colorToolStripMenuItem_Click);
//
// debugInformationToolStripMenuItem
//
this.debugInformationToolStripMenuItem.Name = "debugInformationToolStripMenuItem";
this.debugInformationToolStripMenuItem.Size = new System.Drawing.Size(413, 44);
this.debugInformationToolStripMenuItem.Text = "&Debug Information";
this.debugInformationToolStripMenuItem.Click += new System.EventHandler(this.debugInformationToolStripMenuItem_Click);
//
// errorListToolStripMenuItem
//
this.errorListToolStripMenuItem.Name = "errorListToolStripMenuItem";
this.errorListToolStripMenuItem.Size = new System.Drawing.Size(413, 44);
this.errorListToolStripMenuItem.Text = "Error L&ist";
this.errorListToolStripMenuItem.Click += new System.EventHandler(this.errorListToolStripMenuItem_Click);
//
// renderToolStripMenuItem
//
this.renderToolStripMenuItem.Name = "renderToolStripMenuItem";
this.renderToolStripMenuItem.Size = new System.Drawing.Size(413, 44);
this.renderToolStripMenuItem.Text = "&Render";
this.renderToolStripMenuItem.Click += new System.EventHandler(this.renderToolStripMenuItem_Click);
//
// outputToolStripMenuItem
//
this.outputToolStripMenuItem.Name = "outputToolStripMenuItem";
this.outputToolStripMenuItem.Size = new System.Drawing.Size(413, 44);
this.outputToolStripMenuItem.Text = "&Output";
this.outputToolStripMenuItem.Click += new System.EventHandler(this.outputToolStripMenuItem_Click);
//
// buildToolStripMenuItem
//
this.buildToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.compileToolStripMenuItem,
this.exportCompiledObjectToolStripMenuItem});
this.buildToolStripMenuItem.Name = "buildToolStripMenuItem";
this.buildToolStripMenuItem.Size = new System.Drawing.Size(89, 36);
this.buildToolStripMenuItem.Text = "&Build";
//
// compileToolStripMenuItem
//
this.compileToolStripMenuItem.Name = "compileToolStripMenuItem";
this.compileToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.F7)));
this.compileToolStripMenuItem.Size = new System.Drawing.Size(403, 44);
this.compileToolStripMenuItem.Text = "Co&mpile";
this.compileToolStripMenuItem.Click += new System.EventHandler(this.compileToolStripMenuItem_Click);
//
// exportCompiledObjectToolStripMenuItem
//
this.exportCompiledObjectToolStripMenuItem.Name = "exportCompiledObjectToolStripMenuItem";
this.exportCompiledObjectToolStripMenuItem.Size = new System.Drawing.Size(403, 44);
this.exportCompiledObjectToolStripMenuItem.Text = "&Export Compiled Object";
this.exportCompiledObjectToolStripMenuItem.Click += new System.EventHandler(this.exportCompiledObjectToolStripMenuItem_Click);
//
// toolsToolStripMenuItem
//
this.toolsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.optionsToolStripMenuItem,
this.rewriterToolStripMenuItem,
this.rewriteNobodyToolStripMenuItem});
this.toolsToolStripMenuItem.Name = "toolsToolStripMenuItem";
this.toolsToolStripMenuItem.Size = new System.Drawing.Size(90, 36);
this.toolsToolStripMenuItem.Text = "&Tools";
//
// optionsToolStripMenuItem
//
this.optionsToolStripMenuItem.Name = "optionsToolStripMenuItem";
this.optionsToolStripMenuItem.Size = new System.Drawing.Size(313, 44);
this.optionsToolStripMenuItem.Text = "&Options...";
this.optionsToolStripMenuItem.Click += new System.EventHandler(this.optionsToolStripMenuItem_Click);
//
// rewriterToolStripMenuItem
//
this.rewriterToolStripMenuItem.Name = "rewriterToolStripMenuItem";
this.rewriterToolStripMenuItem.Size = new System.Drawing.Size(313, 44);
this.rewriterToolStripMenuItem.Text = "Rewriter";
this.rewriterToolStripMenuItem.Click += new System.EventHandler(this.rewriterToolStripMenuItem_Click);
//
// rewriteNobodyToolStripMenuItem
//
this.rewriteNobodyToolStripMenuItem.Name = "rewriteNobodyToolStripMenuItem";
this.rewriteNobodyToolStripMenuItem.Size = new System.Drawing.Size(313, 44);
this.rewriteNobodyToolStripMenuItem.Text = "RewriteNobody";
this.rewriteNobodyToolStripMenuItem.Click += new System.EventHandler(this.rewriteNobodyToolStripMenuItem_Click);
//
// helpToolStripMenuItem
//
this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.aboutToolStripMenuItem});
this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
this.helpToolStripMenuItem.Size = new System.Drawing.Size(85, 36);
this.helpToolStripMenuItem.Text = "&Help";
//
// aboutToolStripMenuItem
//
this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
this.aboutToolStripMenuItem.Size = new System.Drawing.Size(229, 44);
this.aboutToolStripMenuItem.Text = "&About...";
this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Margin = new System.Windows.Forms.Padding(2);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.CodeBox);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.AnalysisTabControl);
this.splitContainer1.Size = new System.Drawing.Size(2115, 1117);
this.splitContainer1.SplitterDistance = 759;
this.splitContainer1.SplitterWidth = 2;
this.splitContainer1.TabIndex = 2;
//
// CodeBox
//
this.CodeBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.CodeBox.Font = new System.Drawing.Font("Consolas", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.CodeBox.Location = new System.Drawing.Point(0, 0);
this.CodeBox.Margin = new System.Windows.Forms.Padding(2);
this.CodeBox.Name = "CodeBox";
this.CodeBox.Size = new System.Drawing.Size(759, 1117);
this.CodeBox.TabIndex = 0;
this.CodeBox.Text = "";
this.CodeBox.WordWrap = false;
this.CodeBox.SelectionChanged += new System.EventHandler(this.CodeBox_SelectionChanged);
this.CodeBox.TextChanged += new System.EventHandler(this.CodeBox_TextChanged);
this.CodeBox.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.CodeBox_HelpRequested);
//
// AnalysisTabControl
//
this.AnalysisTabControl.Controls.Add(this.CompilationTabPage);
this.AnalysisTabControl.Controls.Add(this.DisassemblyTabPage);
this.AnalysisTabControl.Controls.Add(this.ASTTabPage);
this.AnalysisTabControl.Controls.Add(this.OptimizerTabPage);
this.AnalysisTabControl.Dock = System.Windows.Forms.DockStyle.Fill;
this.AnalysisTabControl.Location = new System.Drawing.Point(0, 0);
this.AnalysisTabControl.Margin = new System.Windows.Forms.Padding(2);
this.AnalysisTabControl.Name = "AnalysisTabControl";
this.AnalysisTabControl.SelectedIndex = 0;
this.AnalysisTabControl.Size = new System.Drawing.Size(1354, 1117);
this.AnalysisTabControl.TabIndex = 0;
this.AnalysisTabControl.Selecting += new System.Windows.Forms.TabControlCancelEventHandler(this.AnalysisTabControl_Selecting);
//
// CompilationTabPage
//
this.CompilationTabPage.Controls.Add(this.btnCompile);
this.CompilationTabPage.Controls.Add(this.tbOptions);
this.CompilationTabPage.Controls.Add(this.label6);
this.CompilationTabPage.Controls.Add(this.cbProfile);
this.CompilationTabPage.Controls.Add(this.label5);
this.CompilationTabPage.Controls.Add(this.tbEntry);
this.CompilationTabPage.Controls.Add(this.label4);
this.CompilationTabPage.Location = new System.Drawing.Point(8, 27);
this.CompilationTabPage.Margin = new System.Windows.Forms.Padding(2);
this.CompilationTabPage.Name = "CompilationTabPage";
this.CompilationTabPage.Size = new System.Drawing.Size(1338, 1082);
this.CompilationTabPage.TabIndex = 3;
this.CompilationTabPage.Text = "Compilation";
this.CompilationTabPage.UseVisualStyleBackColor = true;
//
// btnCompile
//
this.btnCompile.AutoSize = true;
this.btnCompile.Location = new System.Drawing.Point(135, 13);
this.btnCompile.Margin = new System.Windows.Forms.Padding(2);
this.btnCompile.Name = "btnCompile";
this.btnCompile.Size = new System.Drawing.Size(142, 36);
this.btnCompile.TabIndex = 2;
this.btnCompile.Text = "Compile (Ctrl+F7)";
this.btnCompile.UseVisualStyleBackColor = true;
this.btnCompile.Click += new System.EventHandler(this.compileToolStripMenuItem_Click);
//
// tbOptions
//
this.tbOptions.Location = new System.Drawing.Point(4, 78);
this.tbOptions.Margin = new System.Windows.Forms.Padding(2);
this.tbOptions.Name = "tbOptions";
this.tbOptions.Size = new System.Drawing.Size(286, 20);
this.tbOptions.TabIndex = 3;
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(2, 63);
this.label6.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(46, 13);
this.label6.TabIndex = 7;
this.label6.Text = "Options:";
//
// cbProfile
//
this.cbProfile.FormattingEnabled = true;
this.cbProfile.Items.AddRange(new object[] {
"ps_6_0",
"ps_6_1",
"ps_6_2",
"ps_6_3",
"ps_6_4",
"ps_6_5",
"ps_6_6",
"ps_6_7",
"ps_6_8",
"vs_6_0",
"vs_6_1",
"vs_6_2",
"vs_6_3",
"vs_6_4",
"vs_6_5",
"vs_6_6",
"vs_6_7",
"vs_6_8",
"cs_6_0",
"cs_6_1",
"cs_6_2",
"cs_6_3",
"cs_6_4",
"cs_6_5",
"cs_6_6",
"cs_6_7",
"cs_6_8",
"gs_6_0",
"gs_6_1",
"gs_6_2",
"gs_6_3",
"gs_6_4",
"gs_6_5",
"gs_6_6",
"gs_6_7",
"gs_6_8",
"hs_6_0",
"hs_6_1",
"hs_6_2",
"hs_6_3",
"hs_6_4",
"hs_6_5",
"hs_6_6",
"hs_6_7",
"hs_6_8",
"ds_6_0",
"ds_6_1",
"ds_6_2",
"ds_6_3",
"ds_6_4",
"ds_6_5",
"ds_6_6",
"ds_6_7",
"ds_6_8",
"as_6_5",
"as_6_6",
"as_6_7",
"as_6_8",
"ms_6_5",
"ms_6_6",
"ms_6_7",
"ms_6_8",
"lib_6_3",
"lib_6_4",
"lib_6_5",
"lib_6_6",
"lib_6_7",
"lib_6_8",
"lib_6_x"});
this.cbProfile.Location = new System.Drawing.Point(4, 45);
this.cbProfile.Margin = new System.Windows.Forms.Padding(2);
this.cbProfile.Name = "cbProfile";
this.cbProfile.Size = new System.Drawing.Size(102, 21);
this.cbProfile.TabIndex = 0;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(2, 31);
this.label5.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(76, 13);
this.label5.TabIndex = 6;
this.label5.Text = "Shader Model:";
//
// tbEntry
//
this.tbEntry.Location = new System.Drawing.Point(4, 13);
this.tbEntry.Margin = new System.Windows.Forms.Padding(2);
this.tbEntry.Name = "tbEntry";
this.tbEntry.Size = new System.Drawing.Size(108, 20);
this.tbEntry.TabIndex = 4;
this.tbEntry.Text = "main";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(2, 0);
this.label4.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(34, 13);
this.label4.TabIndex = 5;
this.label4.Text = "Entry:";
//
// DisassemblyTabPage
//
this.DisassemblyTabPage.Controls.Add(this.DisassemblyTextBox);
this.DisassemblyTabPage.Location = new System.Drawing.Point(8, 27);
this.DisassemblyTabPage.Margin = new System.Windows.Forms.Padding(2);
this.DisassemblyTabPage.Name = "DisassemblyTabPage";
this.DisassemblyTabPage.Padding = new System.Windows.Forms.Padding(2);
this.DisassemblyTabPage.Size = new System.Drawing.Size(1719, 1353);
this.DisassemblyTabPage.TabIndex = 0;
this.DisassemblyTabPage.Text = "Disassembly";
this.DisassemblyTabPage.UseVisualStyleBackColor = true;
//
// DisassemblyTextBox
//
this.DisassemblyTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.DisassemblyTextBox.Location = new System.Drawing.Point(2, 2);
this.DisassemblyTextBox.Margin = new System.Windows.Forms.Padding(2);
this.DisassemblyTextBox.Name = "DisassemblyTextBox";
this.DisassemblyTextBox.ReadOnly = true;
this.DisassemblyTextBox.Size = new System.Drawing.Size(1715, 1349);
this.DisassemblyTextBox.TabIndex = 0;
this.DisassemblyTextBox.Text = "";
this.DisassemblyTextBox.WordWrap = false;
this.DisassemblyTextBox.SelectionChanged += new System.EventHandler(this.DisassemblyTextBox_SelectionChanged);
//
// ASTTabPage
//
this.ASTTabPage.Controls.Add(this.ASTDumpBox);
this.ASTTabPage.Location = new System.Drawing.Point(8, 27);
this.ASTTabPage.Margin = new System.Windows.Forms.Padding(2);
this.ASTTabPage.Name = "ASTTabPage";
this.ASTTabPage.Padding = new System.Windows.Forms.Padding(2);
this.ASTTabPage.Size = new System.Drawing.Size(1719, 1353);
this.ASTTabPage.TabIndex = 1;
this.ASTTabPage.Text = "AST";
this.ASTTabPage.UseVisualStyleBackColor = true;
//
// ASTDumpBox
//
this.ASTDumpBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.ASTDumpBox.Location = new System.Drawing.Point(2, 2);
this.ASTDumpBox.Margin = new System.Windows.Forms.Padding(2);
this.ASTDumpBox.Name = "ASTDumpBox";
this.ASTDumpBox.ReadOnly = true;
this.ASTDumpBox.Size = new System.Drawing.Size(1715, 1349);
this.ASTDumpBox.TabIndex = 0;
this.ASTDumpBox.Text = "";
//
// OptimizerTabPage
//
this.OptimizerTabPage.Controls.Add(this.InteractiveEditorButton);
this.OptimizerTabPage.Controls.Add(this.ResetDefaultPassesButton);
this.OptimizerTabPage.Controls.Add(this.AnalyzeCheckBox);
this.OptimizerTabPage.Controls.Add(this.AddPrintModuleButton);
this.OptimizerTabPage.Controls.Add(this.RunPassesButton);
this.OptimizerTabPage.Controls.Add(this.SelectPassDownButton);
this.OptimizerTabPage.Controls.Add(this.SelectPassUpButton);
this.OptimizerTabPage.Controls.Add(this.SelectedPassesBox);
this.OptimizerTabPage.Controls.Add(this.label2);
this.OptimizerTabPage.Controls.Add(this.label1);
this.OptimizerTabPage.Controls.Add(this.AvailablePassesBox);
this.OptimizerTabPage.Location = new System.Drawing.Point(8, 27);
this.OptimizerTabPage.Margin = new System.Windows.Forms.Padding(2);
this.OptimizerTabPage.Name = "OptimizerTabPage";
this.OptimizerTabPage.Padding = new System.Windows.Forms.Padding(2);
this.OptimizerTabPage.Size = new System.Drawing.Size(1719, 1353);
this.OptimizerTabPage.TabIndex = 2;
this.OptimizerTabPage.Text = "Optimizer";
this.OptimizerTabPage.UseVisualStyleBackColor = true;
//
// InteractiveEditorButton
//
this.InteractiveEditorButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.InteractiveEditorButton.Location = new System.Drawing.Point(1567, 1332);
this.InteractiveEditorButton.Margin = new System.Windows.Forms.Padding(2, 3, 2, 3);
this.InteractiveEditorButton.Name = "InteractiveEditorButton";
this.InteractiveEditorButton.Size = new System.Drawing.Size(112, 18);
this.InteractiveEditorButton.TabIndex = 11;
this.InteractiveEditorButton.Text = "Interactive Editor...";
this.InteractiveEditorButton.UseVisualStyleBackColor = true;
this.InteractiveEditorButton.Click += new System.EventHandler(this.InteractiveEditorButton_Click);
//
// ResetDefaultPassesButton
//
this.ResetDefaultPassesButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ResetDefaultPassesButton.Location = new System.Drawing.Point(1567, 1289);
this.ResetDefaultPassesButton.Margin = new System.Windows.Forms.Padding(2);
this.ResetDefaultPassesButton.Name = "ResetDefaultPassesButton";
this.ResetDefaultPassesButton.Size = new System.Drawing.Size(112, 19);
this.ResetDefaultPassesButton.TabIndex = 9;
this.ResetDefaultPassesButton.Text = "Reset Default Passes";
this.ResetDefaultPassesButton.UseVisualStyleBackColor = true;
this.ResetDefaultPassesButton.Click += new System.EventHandler(this.ResetDefaultPassesButton_Click);
//
// AnalyzeCheckBox
//
this.AnalyzeCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.AnalyzeCheckBox.AutoSize = true;
this.AnalyzeCheckBox.Location = new System.Drawing.Point(4, 1251);
this.AnalyzeCheckBox.Margin = new System.Windows.Forms.Padding(2, 3, 2, 3);
this.AnalyzeCheckBox.Name = "AnalyzeCheckBox";
this.AnalyzeCheckBox.Size = new System.Drawing.Size(112, 27);
this.AnalyzeCheckBox.TabIndex = 8;
this.AnalyzeCheckBox.Text = "Analyze passes";
this.AnalyzeCheckBox.UseVisualStyleBackColor = true;
//
// AddPrintModuleButton
//
this.AddPrintModuleButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.AddPrintModuleButton.Location = new System.Drawing.Point(3, 1282);
this.AddPrintModuleButton.Margin = new System.Windows.Forms.Padding(2);
this.AddPrintModuleButton.Name = "AddPrintModuleButton";
this.AddPrintModuleButton.Size = new System.Drawing.Size(112, 19);
this.AddPrintModuleButton.TabIndex = 7;
this.AddPrintModuleButton.Text = "Add Print Module";
this.AddPrintModuleButton.UseVisualStyleBackColor = true;
this.AddPrintModuleButton.Click += new System.EventHandler(this.AddPrintModuleButton_Click);
//
// RunPassesButton
//
this.RunPassesButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.RunPassesButton.Location = new System.Drawing.Point(1567, 1310);
this.RunPassesButton.Margin = new System.Windows.Forms.Padding(2);
this.RunPassesButton.Name = "RunPassesButton";
this.RunPassesButton.Size = new System.Drawing.Size(112, 19);
this.RunPassesButton.TabIndex = 6;
this.RunPassesButton.Text = "Run Passes";
this.RunPassesButton.UseVisualStyleBackColor = true;
this.RunPassesButton.Click += new System.EventHandler(this.RunPassesButton_Click);
//
// SelectPassDownButton
//
this.SelectPassDownButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.SelectPassDownButton.Location = new System.Drawing.Point(1625, 1265);
this.SelectPassDownButton.Margin = new System.Windows.Forms.Padding(2);
this.SelectPassDownButton.Name = "SelectPassDownButton";
this.SelectPassDownButton.Size = new System.Drawing.Size(57, 19);
this.SelectPassDownButton.TabIndex = 5;
this.SelectPassDownButton.Text = "Swap Down";
this.SelectPassDownButton.UseVisualStyleBackColor = true;
this.SelectPassDownButton.Click += new System.EventHandler(this.SelectPassDownButton_Click);
//
// SelectPassUpButton
//
this.SelectPassUpButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.SelectPassUpButton.Location = new System.Drawing.Point(1567, 1265);
this.SelectPassUpButton.Margin = new System.Windows.Forms.Padding(2);
this.SelectPassUpButton.Name = "SelectPassUpButton";
this.SelectPassUpButton.Size = new System.Drawing.Size(54, 19);
this.SelectPassUpButton.TabIndex = 4;
this.SelectPassUpButton.Text = "Swap Up";
this.SelectPassUpButton.UseVisualStyleBackColor = true;
this.SelectPassUpButton.Click += new System.EventHandler(this.SelectPassUpButton_Click);
//
// SelectedPassesBox
//
this.SelectedPassesBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.SelectedPassesBox.ContextMenuStrip = this.PassesContextMenu;
this.SelectedPassesBox.FormattingEnabled = true;
this.SelectedPassesBox.IntegralHeight = false;
this.SelectedPassesBox.Location = new System.Drawing.Point(1567, 24);
this.SelectedPassesBox.Margin = new System.Windows.Forms.Padding(2);
this.SelectedPassesBox.Name = "SelectedPassesBox";
this.SelectedPassesBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.SelectedPassesBox.Size = new System.Drawing.Size(159, 1239);
this.SelectedPassesBox.TabIndex = 3;
this.SelectedPassesBox.DoubleClick += new System.EventHandler(this.SelectedPassesBox_DoubleClick);
this.SelectedPassesBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SelectedPassesBox_KeyUp);
//
// PassesContextMenu
//
this.PassesContextMenu.ImageScalingSize = new System.Drawing.Size(32, 32);
this.PassesContextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.PassPropertiesMenuItem,
this.toolStripMenuItem5,
this.copyToolStripMenuItem1,
this.copyAllToolStripMenuItem,
this.PastePassesMenuItem,
this.DeleteAllPassesMenuItem});
this.PassesContextMenu.Name = "PassesContextMenu";
this.PassesContextMenu.Size = new System.Drawing.Size(213, 200);
//
// PassPropertiesMenuItem
//
this.PassPropertiesMenuItem.Name = "PassPropertiesMenuItem";
this.PassPropertiesMenuItem.Size = new System.Drawing.Size(212, 38);
this.PassPropertiesMenuItem.Text = "P&roperties...";
this.PassPropertiesMenuItem.Click += new System.EventHandler(this.PassPropertiesMenuItem_Click);
//
// toolStripMenuItem5
//
this.toolStripMenuItem5.Name = "toolStripMenuItem5";
this.toolStripMenuItem5.Size = new System.Drawing.Size(209, 6);
//
// copyToolStripMenuItem1
//
this.copyToolStripMenuItem1.Name = "copyToolStripMenuItem1";
this.copyToolStripMenuItem1.Size = new System.Drawing.Size(212, 38);
this.copyToolStripMenuItem1.Text = "&Copy";
this.copyToolStripMenuItem1.Click += new System.EventHandler(this.copyToolStripMenuItem_Click);
//
// copyAllToolStripMenuItem
//
this.copyAllToolStripMenuItem.Name = "copyAllToolStripMenuItem";
this.copyAllToolStripMenuItem.Size = new System.Drawing.Size(212, 38);
this.copyAllToolStripMenuItem.Text = "Copy &All";
this.copyAllToolStripMenuItem.Click += new System.EventHandler(this.copyAllToolStripMenuItem_Click);
//
// PastePassesMenuItem
//
this.PastePassesMenuItem.Name = "PastePassesMenuItem";
this.PastePassesMenuItem.Size = new System.Drawing.Size(212, 38);
this.PastePassesMenuItem.Text = "&Paste";
this.PastePassesMenuItem.Click += new System.EventHandler(this.PastePassesMenuItem_Click);
//
// DeleteAllPassesMenuItem
//
this.DeleteAllPassesMenuItem.Name = "DeleteAllPassesMenuItem";
this.DeleteAllPassesMenuItem.Size = new System.Drawing.Size(212, 38);
this.DeleteAllPassesMenuItem.Text = "Delete All";
this.DeleteAllPassesMenuItem.Click += new System.EventHandler(this.DeleteAllPassesMenuItem_Click);
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(1565, 6);
this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(47, 13);
this.label2.TabIndex = 2;
this.label2.Text = "&Pipeline:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(3, 6);
this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(189, 13);
this.label1.TabIndex = 1;
this.label1.Text = "&Available Passes (double-click to add):";
//
// AvailablePassesBox
//
this.AvailablePassesBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.AvailablePassesBox.FormattingEnabled = true;
this.AvailablePassesBox.IntegralHeight = false;
this.AvailablePassesBox.Location = new System.Drawing.Point(4, 24);
this.AvailablePassesBox.Margin = new System.Windows.Forms.Padding(2);
this.AvailablePassesBox.Name = "AvailablePassesBox";
this.AvailablePassesBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.AvailablePassesBox.Size = new System.Drawing.Size(1559, 1239);
this.AvailablePassesBox.TabIndex = 0;
this.AvailablePassesBox.DoubleClick += new System.EventHandler(this.AvailablePassesBox_DoubleClick);
//
// TopSplitContainer
//
this.TopSplitContainer.Dock = System.Windows.Forms.DockStyle.Fill;
this.TopSplitContainer.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
this.TopSplitContainer.Location = new System.Drawing.Point(0, 38);
this.TopSplitContainer.Margin = new System.Windows.Forms.Padding(2);
this.TopSplitContainer.Name = "TopSplitContainer";
this.TopSplitContainer.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// TopSplitContainer.Panel1
//
this.TopSplitContainer.Panel1.Controls.Add(this.splitContainer1);
//
// TopSplitContainer.Panel2
//
this.TopSplitContainer.Panel2.Controls.Add(this.OutputTabControl);
this.TopSplitContainer.Panel2Collapsed = true;
this.TopSplitContainer.Panel2MinSize = 75;
this.TopSplitContainer.Size = new System.Drawing.Size(2115, 1117);
this.TopSplitContainer.SplitterDistance = 25;
this.TopSplitContainer.SplitterWidth = 2;
this.TopSplitContainer.TabIndex = 3;
//
// OutputTabControl
//
this.OutputTabControl.Controls.Add(this.RenderLogTabPage);
this.OutputTabControl.Dock = System.Windows.Forms.DockStyle.Fill;
this.OutputTabControl.Location = new System.Drawing.Point(0, 0);
this.OutputTabControl.Margin = new System.Windows.Forms.Padding(2);
this.OutputTabControl.Name = "OutputTabControl";
this.OutputTabControl.SelectedIndex = 0;
this.OutputTabControl.Size = new System.Drawing.Size(150, 46);
this.OutputTabControl.TabIndex = 0;
//
// RenderLogTabPage
//
this.RenderLogTabPage.Controls.Add(this.RenderLogBox);
this.RenderLogTabPage.Location = new System.Drawing.Point(4, 22);
this.RenderLogTabPage.Margin = new System.Windows.Forms.Padding(2);
this.RenderLogTabPage.Name = "RenderLogTabPage";
this.RenderLogTabPage.Padding = new System.Windows.Forms.Padding(2);
this.RenderLogTabPage.Size = new System.Drawing.Size(142, 20);
this.RenderLogTabPage.TabIndex = 0;
this.RenderLogTabPage.Text = "Render Log";
this.RenderLogTabPage.UseVisualStyleBackColor = true;
//
// RenderLogBox
//
this.RenderLogBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.RenderLogBox.Location = new System.Drawing.Point(2, 2);
this.RenderLogBox.Margin = new System.Windows.Forms.Padding(2);
this.RenderLogBox.Multiline = true;
this.RenderLogBox.Name = "RenderLogBox";
this.RenderLogBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.RenderLogBox.Size = new System.Drawing.Size(138, 16);
this.RenderLogBox.TabIndex = 0;
this.RenderLogBox.WordWrap = false;
//
// RewriterOutputTextBox
//
this.RewriterOutputTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.RewriterOutputTextBox.Location = new System.Drawing.Point(4, 4);
this.RewriterOutputTextBox.Margin = new System.Windows.Forms.Padding(4);
this.RewriterOutputTextBox.Name = "RewriterOutputTextBox";
this.RewriterOutputTextBox.ReadOnly = true;
this.RewriterOutputTextBox.Size = new System.Drawing.Size(970, 937);
this.RewriterOutputTextBox.TabIndex = 1;
this.RewriterOutputTextBox.Text = "";
this.RewriterOutputTextBox.WordWrap = false;
//
// EditorForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(2115, 1197);
this.Controls.Add(this.TopSplitContainer);
this.Controls.Add(this.TheStatusStrip);
this.Controls.Add(this.TheMenuStrip);
this.MainMenuStrip = this.TheMenuStrip;
this.Margin = new System.Windows.Forms.Padding(2);
this.Name = "EditorForm";
this.Text = "DirectX Compiler Editor";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.EditorForm_FormClosing);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.EditorForm_FormClosed);
this.Load += new System.EventHandler(this.EditorForm_Load);
this.Shown += new System.EventHandler(this.EditorForm_Shown);
this.TheStatusStrip.ResumeLayout(false);
this.TheStatusStrip.PerformLayout();
this.TheMenuStrip.ResumeLayout(false);
this.TheMenuStrip.PerformLayout();
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.AnalysisTabControl.ResumeLayout(false);
this.CompilationTabPage.ResumeLayout(false);
this.CompilationTabPage.PerformLayout();
this.DisassemblyTabPage.ResumeLayout(false);
this.ASTTabPage.ResumeLayout(false);
this.OptimizerTabPage.ResumeLayout(false);
this.OptimizerTabPage.PerformLayout();
this.PassesContextMenu.ResumeLayout(false);
this.TopSplitContainer.Panel1.ResumeLayout(false);
this.TopSplitContainer.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.TopSplitContainer)).EndInit();
this.TopSplitContainer.ResumeLayout(false);
this.OutputTabControl.ResumeLayout(false);
this.RenderLogTabPage.ResumeLayout(false);
this.RenderLogTabPage.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.StatusStrip TheStatusStrip;
private System.Windows.Forms.ToolStripStatusLabel TheStatusStripLabel;
private System.Windows.Forms.MenuStrip TheMenuStrip;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.RichTextBox CodeBox;
private System.Windows.Forms.ToolStripMenuItem editToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem viewToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem NewToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem undoToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem cutToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem copyToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem pasteToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem deleteToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem2;
private System.Windows.Forms.ToolStripMenuItem selectAllToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem3;
private System.Windows.Forms.ToolStripMenuItem goToToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem bitstreamToolStripMenuItem;
private System.Windows.Forms.ToolTip TheToolTip;
private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveAsToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripMenuItem buildToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem compileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem autoUpdateToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem exportCompiledObjectToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem fileVariablesToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem recentFilesToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem4;
private System.Windows.Forms.ToolStripMenuItem findAndReplaceToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem quickFindToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem errorListToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem helpToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem;
private System.Windows.Forms.ContextMenuStrip PassesContextMenu;
private System.Windows.Forms.ToolStripMenuItem PassPropertiesMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem5;
private System.Windows.Forms.ToolStripMenuItem copyToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem copyAllToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem renderToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem outputToolStripMenuItem;
private System.Windows.Forms.SplitContainer TopSplitContainer;
private System.Windows.Forms.TabControl OutputTabControl;
private System.Windows.Forms.TabPage RenderLogTabPage;
private System.Windows.Forms.TextBox RenderLogBox;
private System.Windows.Forms.ToolStripMenuItem FontGrowToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem FontShrinkToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem toolsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem optionsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem ColorMenuItem;
private System.Windows.Forms.ToolStripMenuItem rewriterToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem rewriteNobodyToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem debugInformationToolStripMenuItem;
private System.Windows.Forms.TabControl AnalysisTabControl;
private System.Windows.Forms.TabPage DisassemblyTabPage;
private System.Windows.Forms.RichTextBox DisassemblyTextBox;
private System.Windows.Forms.TabPage ASTTabPage;
private System.Windows.Forms.RichTextBox ASTDumpBox;
private System.Windows.Forms.TabPage OptimizerTabPage;
private System.Windows.Forms.Button ResetDefaultPassesButton;
private System.Windows.Forms.CheckBox AnalyzeCheckBox;
private System.Windows.Forms.Button AddPrintModuleButton;
private System.Windows.Forms.Button RunPassesButton;
private System.Windows.Forms.Button SelectPassDownButton;
private System.Windows.Forms.Button SelectPassUpButton;
private System.Windows.Forms.ListBox SelectedPassesBox;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox AvailablePassesBox;
private System.Windows.Forms.RichTextBox RewriterOutputTextBox;
private System.Windows.Forms.ToolStripMenuItem PastePassesMenuItem;
private System.Windows.Forms.ToolStripMenuItem DeleteAllPassesMenuItem;
private System.Windows.Forms.Button InteractiveEditorButton;
private System.Windows.Forms.ComboBox cbProfile;
private System.Windows.Forms.Button btnCompile;
private System.Windows.Forms.TextBox tbOptions;
private System.Windows.Forms.TextBox tbEntry;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TabPage CompilationTabPage;
private System.Windows.Forms.ToolStripMenuItem bitstreamFromClipboardToolStripMenuItem;
}
}
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dotnetc/README.md
|
# dndxc
## Help System
This README.md file also acts as the entry point into the help system.
Press F1 to get contextual help.
## Help Topics
### Descriptor
Attributes: Name, ResName, CounterName, Kind (one of UAV,SRV,CBV)
Attributes from D3D12_UNORDERED_ACCESS_VIEW_DESC and D3D12_SHADER_RESOURCE_VIEW_DESC:
- Format
- Dimension
When Dimension is D3D12_SRV_DIMENSION_BUFFER:
- FirstElement, Flags (empty or RAW), NumElements, StructureByteStride
When Dimension is D3D12_UAV_DIMENSION_BUFFER:
- FirstElement, NumElements, StructuredByteStride, CounterOffsetInBytes, Flags
Flags can be emptyy or RAW. If RAW, Format is forced to DXGI_FORMAT_R32_TYPELESS.
When Dimension is D3D12_UAV_DIMENSION_TEXTURE1D:
- MipSlice
When Dimension is D3D12_UAV_DIMENSION_TEXTURE1DARRAY:
- MipSlice, FirstArraySlice, ArraySize
When Dimension is D3D12_UAV_DIMENSION_TEXTURE2D:
- MipSlice, PlaneSlice
When Dimension is D3D12_UAV_DIMENSION_TEXTURE2DARRAY:
- MipSlice, FirstArraySlice, ArraySize, PlaneSlice
When Dimension is D3D12_UAV_DIMENSION_TEXTURE3D:
- MipSlice, FirstWSlice, WSize
If either of Name or ResName is assigned and the other isn't, they default to the assigned name.
### DescriptorHeap
Attributes: Name, Flags, NodeMask, NumDescriptors, Type
Flags defaults to D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE.
Type is one of (CBV_SRV_UAV,SAMPLER,RTV,DSV) and defaults to D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV.
When Type is D3D12_DESCRIPTOR_HEAP_TYPE_RTV, Flags defaults to NONE.
### Resource
Attributes: Name, Init, ReadBack, HeapType, CPUPageProperty, MemoryPoolPreference, CreationNodeMask, VisibleNodeMask
Attributes from D3D12_RESOURCE_DESC:
- Dimension
- Alignment
- Width
- Height
- DepthOrArraySize
- MipLevels
- Format
- SampleCount, SampleQual
- Layout
- Flags
Other attributes:
- HeapFlags
- InitialResourceState
- TransitionTo
- Topology
Some values are fixed depending on the value of Dimension:
if (pResource->Desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) {
pResource->Desc.Height = 1;
pResource->Desc.DepthOrArraySize = 1;
pResource->Desc.MipLevels = 1;
pResource->Desc.Format = DXGI_FORMAT_UNKNOWN;
pResource->Desc.SampleDesc.Count = 1;
pResource->Desc.SampleDesc.Quality = 0;
pResource->Desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
}
if (pResource->Desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D) {
if (pResource->Desc.Height == 0) pResource->Desc.Height = 1;
if (pResource->Desc.DepthOrArraySize == 0) pResource->Desc.DepthOrArraySize = 1;
if (pResource->Desc.SampleDesc.Count == 0) pResource->Desc.SampleDesc.Count = 1;
}
if (pResource->Desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D) {
if (pResource->Desc.DepthOrArraySize == 0) pResource->Desc.DepthOrArraySize = 1;
if (pResource->Desc.SampleDesc.Count == 0 ) pResource->Desc.SampleDesc.Count = 1;
}
The contents of the element initialize the values within.
The following characters are ignored: '{', '}', ',', '\w'
The suffix of each value determines how it's interpreted: h, l, u, i, f
Friendly names include nan, inf, +inf, -inf, -denorm, denorm
### RootSignature
This element defines the root signature string.
See https://msdn.microsoft.com/en-us/library/windows/desktop/dn913202(v=vs.85).aspx for a reference.
Example:
RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT),CBV(b0),SRV(t0),UAV(u0),DescriptorTable(CBV(b1),SRV(t1,numDescriptors=2),UAV(u1))
### RootValue
This element defines a single value in a RootValues collection.
Attributes: ResName, HeapName, Index
HeapName names the heap in which the resource is placed, ResName the name, and Index an index into that resource table.
HeapName and ResName are exclusive. HeapName refers to a descriptor table (allocated on its own heap) and ResName refers to a Resource.
Remember: SRV or UAV root descriptors can only be Raw or Structured buffers.
### RootValues
This element defines the root values to be used in a ShaderOp.
Element: RootValue
### ShaderOp
This elements define a single Draw or Dispatch operation.
Attributes: Name, CS, VS, PS, DispatchX, DispatchY, DispatchZ, TopologyType
Elements: InputElements, Shader, RootSignature, RenderTargets, Resource, DescriptorHeap, RootValues
### ShaderOpSet
This element defines a set of shader operations. It's useful when bundling
multiple shaders in a single file.
Elements: ShaderOp
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dotnetc/FindDialog.Designer.cs
|
///////////////////////////////////////////////////////////////////////////////
// //
// FindDialog.Designer..cs //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
///////////////////////////////////////////////////////////////////////////////
namespace MainNs
{
partial class FindDialog
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.findButton = new System.Windows.Forms.Button();
this.cancelButton = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.findControl = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// findButton
//
this.findButton.Location = new System.Drawing.Point(278, 4);
this.findButton.Name = "findButton";
this.findButton.Size = new System.Drawing.Size(75, 23);
this.findButton.TabIndex = 2;
this.findButton.Text = "Find Next";
this.findButton.UseVisualStyleBackColor = true;
this.findButton.Click += new System.EventHandler(this.FindButton_Click);
//
// cancelButton
//
this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.cancelButton.Location = new System.Drawing.Point(278, 33);
this.cancelButton.Name = "cancelButton";
this.cancelButton.Size = new System.Drawing.Size(75, 23);
this.cancelButton.TabIndex = 3;
this.cancelButton.Text = "Cancel";
this.cancelButton.UseVisualStyleBackColor = true;
this.cancelButton.Click += new System.EventHandler(this.CancelButton_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Fi&nd what:";
//
// findControl
//
this.findControl.Location = new System.Drawing.Point(74, 6);
this.findControl.Name = "findControl";
this.findControl.Size = new System.Drawing.Size(198, 20);
this.findControl.TabIndex = 1;
//
// FindDialog
//
this.AcceptButton = this.findButton;
this.CancelButton = this.cancelButton;
this.ClientSize = new System.Drawing.Size(360, 63);
this.ControlBox = false;
this.Controls.Add(this.findControl);
this.Controls.Add(this.label1);
this.Controls.Add(this.cancelButton);
this.Controls.Add(this.findButton);
this.MinimizeBox = false;
this.Name = "FindDialog";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Find";
this.Load += new System.EventHandler(this.FindDialog_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button findButton;
private System.Windows.Forms.Button cancelButton;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox findControl;
}
}
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dotnetc/Tom.cs
|
///////////////////////////////////////////////////////////////////////////////
// //
// Tom.cs //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Runtime.InteropServices;
namespace Tom
{
[ComImport, Guid("8CC497C0-A1DF-11ce-8098-00AA0047BE5D"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
internal interface ITextDocument
{
void Placeholder_Name();
ITextSelection Selection
{
[return: MarshalAs(UnmanagedType.Interface)]
get;
}
void Placeholder_StoryCount();
void Placeholder_StoryRanges();
void Placeholder_get_Saved();
void Placeholder_set_Saved();
void Placeholder_get_DefaultTabStop();
void Placeholder_set_DefaultTabStop();
void Placeholder_New();
void Placeholder_Open();
void Placeholder_Save();
void Placeholder_Freeze();
void Placeholder_Unfreeze();
void Placeholder_BeginEditCollection();
void Placeholder_EndEditCollection();
void Placeholder_Undo();
void Placeholder_Redo();
[return: MarshalAs(UnmanagedType.Interface)]
ITextRange Range(int cp1, int cp2);
[return: MarshalAs(UnmanagedType.Interface)]
ITextRange RangeFromPoint(int x, int y);
}
[ComImport, Guid("8CC497C2-A1DF-11ce-8098-00AA0047BE5D"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
internal interface ITextRange
{
string Text { get; }
void Placeholder_set_Text();
void Placeholder_get_Char();
void Placeholder_set_Char();
//void GetDuplicate([MarshalAs(UnmanagedType.Interface)]out ITextRange ppRange);
[return: MarshalAs(UnmanagedType.Interface)]
ITextRange GetDuplicate();
void Placeholder_get_FormattedText();
void Placeholder_set_FormattedText();
int Start { get; set; }
int End { get; set; }
ITextFont Font { get; set; }
ITextPara Para { get; set; }
int StoryLength { get; }
TomStory StoryType { get; }
void Collapse(TomStartEnd bStart);
int Expand(TomUnit unit);
void Placeholder_GetIndex();
void Placeholder_SetIndex();
void SetRange(int cp1, int cp2);
TomBool InRange(ITextRange range);
void Placeholder_InStory();
TomBool IsEqual(ITextRange range);
void Select();
int StartOf(int type, int extend);
int EndOf(TomUnit unit, TomExtend extend);
int Move(TomUnit unit, int count);
int MoveStart(TomUnit unit, int count);
int MoveEnd(TomUnit unit, int count);
void Placeholder_MoveWhile();
void Placeholder_MoveStartWhile();
void Placeholder_MoveEndWhile();
void Placeholder_MoveUntil();
void Placeholder_MoveStartUntil();
void Placeholder_MoveEndUntil();
int FindText(string bstr, int count, TomMatch flags);
int FindTextStart(string bstr, int count, TomMatch flags);
int FindTextEnd(string bstr, int count, TomMatch flags);
void Placeholder_Delete();
void Placeholder_Cut();
void Placeholder_Copy();
void Placeholder_Paste();
void Placeholder_CanPaste();
void Placeholder_CanEdit();
void Placeholder_ChangeCase();
[PreserveSig]
int GetPoint(TomGetPoint type, out int px, out int py);
void Placeholder_SetPoint();
void ScrollIntoView(TomStartEnd scrollvalue);
[PreserveSig]
int GetEmbeddedObject([MarshalAs(UnmanagedType.IUnknown)]out object ppObj);
}
[ComImport, Guid("8CC497C1-A1DF-11ce-8098-00AA0047BE5D"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
internal interface ITextSelection : ITextRange
{
#region ITextRange
new string Text { get; }
new void Placeholder_set_Text();
new void Placeholder_get_Char();
new void Placeholder_set_Char();
//new void GetDuplicate([MarshalAs(UnmanagedType.Interface)]out ITextRange ppRange);
[return: MarshalAs(UnmanagedType.Interface)]
new ITextRange GetDuplicate();
new void Placeholder_get_FormattedText();
new void Placeholder_set_FormattedText();
new int Start { get; set; }
new int End { get; set; }
new ITextFont Font { get; set; }
new ITextPara Para { get; set; }
new int StoryLength { get; }
new TomStory StoryType { get; }
new void Collapse(TomStartEnd bStart);
new int Expand(TomUnit unit);
new void Placeholder_GetIndex();
new void Placeholder_SetIndex();
new void SetRange(int cp1, int cp2);
new TomBool InRange(ITextRange range);
new void Placeholder_InStory();
new TomBool IsEqual(ITextRange range);
new void Select();
new int StartOf(int type, int extend);
new int EndOf(TomUnit unit, TomExtend extend);
new int Move(TomUnit unit, int count);
new int MoveStart(TomUnit unit, int count);
new int MoveEnd(TomUnit unit, int count);
new void Placeholder_MoveWhile();
new void Placeholder_MoveStartWhile();
new void Placeholder_MoveEndWhile();
new void Placeholder_MoveUntil();
new void Placeholder_MoveStartUntil();
new void Placeholder_MoveEndUntil();
new int FindText(string bstr, int count, TomMatch flags);
new int FindTextStart(string bstr, int count, TomMatch flags);
new int FindTextEnd(string bstr, int count, TomMatch flags);
new void Placeholder_Delete();
new void Placeholder_Cut();
new void Placeholder_Copy();
new void Placeholder_Paste();
new void Placeholder_CanPaste();
new void Placeholder_CanEdit();
new void Placeholder_ChangeCase();
[PreserveSig]
new int GetPoint(TomGetPoint type, out int px, out int py);
new void Placeholder_SetPoint();
new void ScrollIntoView(TomStartEnd scrollvalue);
[PreserveSig]
new int GetEmbeddedObject([MarshalAs(UnmanagedType.IUnknown)]out object ppObj);
#endregion ITextRange
TomSelectionFlags Flags { get; set; }
void Placeholder_Type();
void Placeholder_MoveLeft();
void Placeholder_MoveRight();
void Placeholder_MoveUp();
void Placeholder_MoveDown();
void Placeholder_HomeKey();
void Placeholder_EndKey();
void Placeholder_TypeText();
}
[ComImport, Guid("8CC497C3-A1DF-11ce-8098-00AA0047BE5D"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
internal interface ITextFont
{
//[propget]GetDuplicate([retval][out] ITextFont **ppFont)
ITextFont GetDuplicate();
//[propput]SetDuplicate([in] ITextFont *pFont)
void Placeholder_SetDuplicate();
//CanChange([retval][out] long *pB)
void Placeholder_CanChange();
//IsEqual([in] ITextFont *pFont, [retval][out] long *pB)
void Placeholder_IsEqual();
//Reset([in] long Value)
void Placeholder_Reset();
//[propget]GetStyle([retval][out] long *pValue)
void Placeholder_GetStyle();
//[propput]SetStyle([in] long Value)
void Placeholder_SetStyle();
//[propget]GetAllCaps([retval][out] long *pValue)
//[propput]SetAllCaps([in] long Value)
TomBool AllCaps { get; set; }
//[propget]GetAnimation([retval][out] long *pValue)
//[propput]SetAnimation([in] long Value)
TomAnimation Animation { get; set; }
//[propget]GetBackColor([retval][out] long *pValue)
//[propput]SetBackColor([in] long Value)
// if high byte is zero then value is COLORREF.
// if high byte is one then PALETTEINDEX.
int BackColor { get; set; }
//[propget]GetBold([retval][out] long *pValue)
void Placeholder_GetBold();
//[propput]SetBold([in] long Value)
void Placeholder_SetBold();
//[propget]GetEmboss([retval][out] long *pValue)
//[propput]SetEmboss([in] long Value)
TomBool Emboss { get; set; }
//[propget]GetForeColor([retval][out] long *pValue)
//[propput]SetForeColor([in] long Value)
int ForeColor { get; set; }
//[propget]GetHidden([retval][out] long *pValue)
//[propput]SetHidden([in] long Value)
TomBool Hidden { get; set; }
//[propget]GetEngrave([retval][out] long *pValue)
//[propput]SetEngrave([in] long Value)
TomBool Engrave { get; set; }
//[propget]GetItalic([retval][out] long *pValue)
//[propput]SetItalic([in] long Value)
TomBool Italic { get; set; }
//[propget]GetKerning([retval][out] float *pValue)
void Placeholder_GetKerning();
//[propput]SetKerning([in] float Value)
void Placeholder_SetKerning();
//[propget]GetLanguageID([retval][out] long *pValue)
void Placeholder_GetLanguageID();
//[propput]SetLanguageID([in] long Value)
void Placeholder_SetLanguageID();
//[propget]GetName([retval][out] BSTR *pbstr)
//[propput]SetName([in] BSTR bstr)
string Name { get; set; }
//[propget]GetOutline([retval][out] long *pValue)
//[propput]SetOutline([in] long Value)
TomBool Outline { get; set; }
//[propget]GetPosition([retval][out] float *pValue)
void Placeholder_GetPosition();
//[propput]SetPosition([in] float Value)
void Placeholder_SetPosition();
//[propget]GetProtected([retval][out] long *pValue)
//[propput]SetProtected([in] long Value)
TomBool Protected { get; set; }
//[propget]GetShadow([retval][out] long *pValue)
//[propput]SetShadow([in] long Value)
TomBool Shadow { get; set; }
//[propget]GetSize([retval][out] float *pValue)
//[propput]SetSize([in] float Value)
float Size { get; set; }
//[propget]GetSmallCaps([retval][out] long *pValue)
//[propput]SetSmallCaps([in] long Value)
TomBool SmallCaps { get; set; }
//[propget]GetSpacing([retval][out] float *pValue)
void Placeholder_GetSpacing();
//[propput]SetSpacing([in] float Value)
void Placeholder_SetSpacing();
//[propget]GetStrikeThrough([retval][out] long *pValue)
//[propput]SetStrikeThrough([in] long Value)
TomBool StrikeThrough { get; set; }
//[propget]GetSubscript([retval][out] long *pValue)
//[propput]SetSubscript([in] long Value)
TomBool Subscript { get; set; }
//[propget]GetSuperscript([retval][out] long *pValue)
//[propput]SetSuperscript([in] long Value)
TomBool Superscript { get; set; }
//[propget]GetUnderline([retval][out] long *pValue)
//[propput]SetUnderline([in] long Value)
TomUnderline Underline { get; set; }
//[propget]GetWeight([retval][out] long *pValue)
//[propput]SetWeight([in] long Value)
int Weight { get; set; }
}
[ComImport, Guid("8CC497C4-A1DF-11CE-8098-00AA0047BE5D"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
internal interface ITextPara
{
//[propget]GetDuplicate([retval][out]ITextPara * *ppPara)
void Placeholder_GetDuplicate();
//[propput]SetDuplicate([in]ITextPara *pPara)
void Placeholder_SetDuplicate();
//CanChange([retval][out]long *pB)
void Placeholder_CanChange();
//IsEqual([in]ITextPara *pPara,[retval][out]long *pB)
void Placeholder_IsEqual();
//Reset([in]long Value)
void Placeholder_Reset();
//[propget]GetStyle([retval][out]long *pValue)
void Placeholder_GetStyle();
//[propput]SetStyle([in]long Value)
void Placeholder_SetStyle();
//[propget]GetAlignment([retval][out]long *pValue)
//[propput]SetAlignment([in]long Value)
TomAlignment Alignment { get; set; }
//[propget]GetHyphenation([retval][out]long *pValue)
void Placeholder_GetHyphenation();
//[propput]SetHyphenation([in]long Value)
void Placeholder_SetHyphenation();
//[propget]GetFirstLineIndent([retval][out]float *pValue)
float FirstLineIndent { get; }
//[propget]GetKeepTogether([retval][out]long *pValue)
void Placeholder_GetKeepTogether();
//[propput]SetKeepTogether([in]long Value)
void Placeholder_SetKeepTogether();
//[propget]GetKeepWithNext([retval][out]long *pValue)
void Placeholder_GetKeepWithNext();
//[propput]SetKeepWithNext([in]long Value)
void Placeholder_SetKeepWithNext();
//[propget]GetLeftIndent([retval][out]float *pValue)
float LeftIndent { get; }
//[propget]GetLineSpacing([retval][out]float *pValue)
void Placeholder_GetLineSpacing();
//[propget]GetLineSpacingRule([retval][out]long *pValue)
void Placeholder_GetLineSpacingRule();
//[propget]GetListAlignment([retval][out]long *pValue)
void Placeholder_GetListAlignment();
//[propput]SetListAlignment([in]long Value)
void Placeholder_SetListAlignment();
//[propget]GetListLevelIndex([retval][out]long *pValue)
void Placeholder_GetListLevelIndex();
//[propput]SetListLevelIndex([in]long Value)
void Placeholder_SetListLevelIndex();
//[propget]GetListStart([retval][out]long *pValue)
void Placeholder_GetListStart();
//[propput]SetListStart([in]long Value)
void Placeholder_SetListStart();
//[propget]GetListTab([retval][out]float *pValue)
void Placeholder_GetListTab();
//[propput]SetListTab([in]float Value)
void Placeholder_SetListTab();
//[propget]GetListType([retval][out]long *pValue)
//[propput]SetListType([in]long Value)
TomListType ListType { get; set; }
//[propget]GetNoLineNumber([retval][out]long *pValue)
void Placeholder_GetNoLineNumber();
//[propput]SetNoLineNumber([in]long Value)
void Placeholder_SetNoLineNumber();
//[propget]GetPageBreakBefore([retval][out]long *pValue)
void Placeholder_GetPageBreakBefore();
//[propput]SetPageBreakBefore([in]long Value)
void Placeholder_SetPageBreakBefore();
//[propget]GetRightIndent([retval][out]float *pValue)
//[propput]SetRightIndent([in]float Value)
float RightIndent { get; set; }
//SetIndents([in]float StartIndent,[in]float LeftIndent,[in]float RightIndent)
void Placeholder_SetIndents();
//SetLineSpacing([in]long LineSpacingRule,[in]float LineSpacing)
void Placeholder_SetLineSpacing();
//[propget]GetSpaceAfter([retval][out]float *pValue)
void Placeholder_GetSpaceAfter();
//[propput]SetSpaceAfter([in]float Value)
void Placeholder_SetSpaceAfter();
//[propget]GetSpaceBefore([retval][out]float *pValue)
void Placeholder_GetSpaceBefore();
//[propput]SetSpaceBefore([in]float Value)
void Placeholder_SetSpaceBefore();
//[propget]GetWidowControl([retval][out]long *pValue)
void Placeholder_GetWidowControl();
//[propput]SetWidowControl([in]long Value)
void Placeholder_SetWidowControl();
//[propget]GetTabCount([retval][out]long *pCount)
int TabCount { get; }
//AddTab([in]float tbPos,[in]long tbAlign,[in]long tbLeader)
void Placeholder_AddTab();
//ClearAllTabs(void)
void Placeholder_ClearAllTabs();
//DeleteTab([in]float tbPos)
void Placeholder_DeleteTab();
//GetTab([in]long iTab,[out]float *ptbPos,[out]long *ptbAlign,[out]long *ptbLeader)
void GetTab(int iTab, out float tbPos, out TomAlignment tbAlign, out TomLeader tbLeader);
}
internal enum TomAlignment : int
{
tomUndefined = -9999999,
tomAlignLeft = 0,
tomAlignCenter = 1,
tomAlignRight = 2,
tomAlignJustify = 3,
tomAlignDecimal = 3,
tomAlignBar = 4,
}
internal enum TomAnimation : int
{
tomUndefined = -9999999,
tomNoAnimation = 0,
tomLasVegasLights = 1,
tomBlinkingBackground = 2,
tomSparkleText = 3,
tomMarchingBlackAnts = 4,
tomMarchingRedAnts = 5,
tomShimmer = 6,
}
internal enum TomBool : int
{
tomUndefined = -9999999,
tomTrue = -1,
tomFalse = 0,
}
// miscellaneous integer constants
internal enum TomConst : int
{
tomUndefined = -9999999,
tomAutocolor = -9999997,
}
internal enum TomExtend : int
{
tomMove = 0,
tomExtend = 1,
}
[Flags]
internal enum TomGetPoint : int
{
//TA_LEFT = 0
//TA_TOP = 0
TA_RIGHT = 2,
TA_CENTER = 6,
TA_BOTTOM = 8,
TA_BASELINE = 24,
tomStart = 32,
}
internal enum TomLeader : int
{
tomDots = 1,
tomDashes = 2,
tomLines = 3,
}
// See ITextPara::GetListType documentation
[Flags]
internal enum TomListType : int
{
tomUndefined = -9999999,
tomListNone = 0,
tomListBullet = 1,
tomListNumberAsArabic = 2,
tomListNumberAsLCLetter = 3,
tomListNumberAsUCLetter = 4,
tomListNumberAsLCRoman = 5,
tomListNumberAsUCRoman = 6,
tomListNumberAsSequence = 7,
tomListTypeMask = 0xf,
tomListParentheses = 0x10000,
tomListPeriod = 0x20000,
tomListPlain = 0x30000,
tomListFormatMask = 0xf0000,
}
[Flags]
internal enum TomMatch : int
{
tomMatchWord = 2,
tomMatchCase = 4,
tomMatchPattern = 8,
}
// ITextSelection Flags values
[Flags]
internal enum TomSelectionFlags : int
{
tomSelStartActive = 1,
tomSelAtEOL = 2,
tomSelOvertype = 4,
tomSelActive = 8,
tomSelReplace = 16,
}
internal enum TomStartEnd : int
{
tomStart = 32,
tomEnd = 0,
}
internal enum TomStory : int
{
tomUnknownStory = 0,
tomMainTextStory = 1,
tomFootnotesStory = 2,
tomEndnotesStory = 3,
tomCommentsStory = 4,
tomTextFrameStory = 5,
tomEvenPagesHeaderStory = 6,
tomPrimaryHeaderStory = 7,
tomEvenPagesFooterStory = 8,
tomPrimaryFooterStory = 9,
tomFirstPageHeaderStory = 10,
tomFirstPageFooterStory = 11,
}
internal enum TomUnderline : int
{
tomUndefined = -9999999,
tomTrue = -1,
tomNone = 0,
tomSingle = 1,
tomWords = 2,
tomDouble = 3,
tomDotted = 4,
tomDash = 5,
tomDashDot = 6,
tomDashDotDot = 7,
tomWave = 8,
tomThick = 9,
tomHair = 10,
}
internal enum TomUnit : int
{
tomCharacter = 1,
tomWord = 2,
tomSentence = 3,
tomParagraph = 4,
tomLine = 5,
tomStory = 6,
tomScreen = 7,
tomSection = 8,
tomColumn = 9,
tomRow = 10,
tomWindow = 11,
tomCell = 12,
tomCharFormat = 13,
tomParaFormat = 14,
tomTable = 15,
tomObject = 16,
}
}
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dotnetc/HlslHost.cs
|
///////////////////////////////////////////////////////////////////////////////
// //
// HlslHost.cs //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Provides support for working with an out-of-process rendering host. //
// //
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;
namespace MainNs
{
internal class HlslHost
{
#region Private fields.
/// <summary>Host component.</summary>
private CHlslHost host;
#endregion Private fields.
#region Internal classes.
internal enum HhMessageId : uint
{
GetPidMsgId = 1,
ShutdownMsgId = 2,
StartRendererMsgId = 3,
StopRendererMsgId = 4,
SetPayloadMsgId = 5,
ReadLogMsgId = 6,
SetSizeMsgId = 7,
SetParentHwndMsgId = 8,
GetPidMsgReplyId = 100 + GetPidMsgId,
StartRendererMsgReplyId = 100 + StartRendererMsgId,
StopRendererMsgReplyId = 100 + StopRendererMsgId,
SetPayloadMsgReplyId = 100 + SetPayloadMsgId,
ReadLogMsgReplyId = 100 + ReadLogMsgId,
SetSizeMsgReplyId = 100 + SetSizeMsgId,
SetParentHwndMsgReplyId = 100 + SetParentHwndMsgId,
}
internal class HhMessageReply
{
public HhMessageReply(uint kind)
{
Kind = kind;
}
public uint Kind;
}
internal class HhGetPidReply : HhMessageReply
{
public HhGetPidReply(uint pid) : base(GetPidMsgReplyId)
{
Pid = pid;
}
public uint Pid;
}
internal class HhResultReply : HhMessageReply
{
public HhResultReply(uint kind, uint hresult) : base(kind)
{
HResult = hresult;
}
public uint HResult;
}
internal class HhLogReply : HhMessageReply
{
public HhLogReply(string log) : base(ReadLogMsgReplyId)
{
Log = log;
}
public string Log;
}
#endregion Internal classes.
#region Public properties.
internal bool IsActive
{
get
{
return this.host != null;
}
set
{
if (!value)
{
// Tear down host.
if (host != null)
{
try
{
SendHostMessage(ShutdownMsgId);
}
catch (System.Runtime.InteropServices.COMException)
{
host = null;
}
}
return;
}
EnsureActive();
}
}
#endregion Public properties.
#region Public methods.
internal HhMessageReply GetReply()
{
try
{
var str = host as System.Runtime.InteropServices.ComTypes.IStream;
byte[] response = new byte[8];
str.Read(response, (int)HhMessageHeader.FixedSize, IntPtr.Zero);
System.IO.BinaryReader r = new System.IO.BinaryReader(new System.IO.MemoryStream(response));
uint len = r.ReadUInt32();
if (len == 0) return null;
uint kind = r.ReadUInt32();
switch (kind)
{
case GetPidMsgReplyId:
str.Read(response, 4, IntPtr.Zero);
return new HhGetPidReply(BytesAsUInt32(response));
case StartRendererMsgReplyId:
case StopRendererMsgReplyId:
case SetPayloadMsgReplyId:
case (uint)HhMessageId.SetParentHwndMsgReplyId:
str.Read(response, 4, IntPtr.Zero);
return new HhResultReply(kind, BytesAsUInt32(response));
case ReadLogMsgReplyId:
response = new byte[len - 8];
str.Read(response, response.Length, IntPtr.Zero);
System.IO.BinaryReader logReader = new System.IO.BinaryReader(new System.IO.MemoryStream(response), Encoding.Unicode);
uint charCount = logReader.ReadUInt32(); // text size.
string text = new string(logReader.ReadChars((int)charCount));
return new HhLogReply(text);
default:
throw new InvalidOperationException("Unknown reply kind from host: " + kind);
}
}
catch (Exception runError)
{
System.Diagnostics.Debug.WriteLine(runError);
host = null;
throw;
}
}
internal void SendHostMessage(HhMessageId kind)
{
SendHostMessage((uint)kind);
}
internal void SendHostMessagePlay(string payload)
{
var str = host as System.Runtime.InteropServices.ComTypes.IStream;
HhMessageHeader h = new HhMessageHeader();
h.Kind = SetPayloadMsgId;
byte[] payloadBytes = Encoding.UTF8.GetBytes(payload);
h.Length = (uint)(HhMessageHeader.FixedSize + payloadBytes.Length + 1);
var stream = new System.IO.MemoryStream();
var writer = new System.IO.BinaryWriter(stream);
writer.Write(h.Length);
writer.Write(h.Kind);
writer.Write(payloadBytes);
writer.Write('\0');
writer.Flush();
str.Write(stream.ToArray(), (int)h.Length, IntPtr.Zero);
}
internal void SetParentHwnd(IntPtr handle)
{
var str = host as System.Runtime.InteropServices.ComTypes.IStream;
HhMessageHeader h = new HhMessageHeader();
h.Kind = (uint)HhMessageId.SetParentHwndMsgId;
h.Length = (uint)(HhMessageHeader.FixedSize + sizeof(UInt64));
var stream = new System.IO.MemoryStream();
var writer = new System.IO.BinaryWriter(stream);
writer.Write(h.Length);
writer.Write(h.Kind);
writer.Write(handle.ToInt64());
writer.Flush();
str.Write(stream.ToArray(), (int)h.Length, IntPtr.Zero);
}
internal static string GetLogReplyText(HhMessageReply reply)
{
if (reply == null)
return null;
HhGetPidReply pidReply = reply as HhGetPidReply;
if (pidReply != null)
{
return "PID for host process is " + pidReply.Pid;
}
HhResultReply resultReply = reply as HhResultReply;
if (resultReply != null)
{
return "Operation result for " + HhKindToText(resultReply.Kind) +
": " + HResultToString(resultReply.HResult);
}
HhLogReply logReply = reply as HhLogReply;
if (logReply != null)
{
return logReply.Log;
}
return null;
}
internal static string GetShaderOpPayload(string shaderText, string xml)
{
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.LoadXml(xml);
XmlElement opElement;
if (d.DocumentElement.LocalName != "ShaderOpSet")
{
var setElement = d.CreateElement("ShaderOpSet");
opElement = (XmlElement)d.RemoveChild(d.DocumentElement);
setElement.AppendChild(opElement);
d.AppendChild(setElement);
}
else
{
opElement = d.DocumentElement;
}
if (opElement.LocalName != "ShaderOp")
{
throw new InvalidOperationException("Expected 'ShaderOp' or 'ShaderOpSet' root elements.");
}
var shaders = opElement.ChildNodes.OfType<XmlElement>().Where(elem => elem.LocalName == "Shader").ToList();
foreach (var shader in shaders)
{
if (!shader.HasChildNodes || String.IsNullOrWhiteSpace(shader.InnerText))
{
shader.InnerText = shaderText;
}
}
return d.OuterXml;
}
internal void EnsureActive()
{
uint pid;
if (TryGetProcessId(out pid))
{
return;
}
// Setup a communication channel.
try
{
host = new CHlslHost();
}
catch (System.Runtime.InteropServices.COMException ctorErr)
{
System.Diagnostics.Debug.WriteLine(ctorErr);
// Start the host process.
ProcessStartInfo psi = new ProcessStartInfo("HLSLHost.exe");
try
{
Process p = System.Diagnostics.Process.Start(psi);
}
catch (System.ComponentModel.Win32Exception startErr)
{
System.Diagnostics.Debug.WriteLine(startErr);
throw;
}
System.Threading.Thread.Sleep(200);
host = new CHlslHost();
}
}
internal bool TryGetProcessId(out uint pid)
{
pid = 0;
try
{
this.SendHostMessage(HhMessageId.GetPidMsgId);
for (;;)
{
HhMessageReply reply = this.GetReply();
if (reply == null)
return false;
HhGetPidReply pidReply = reply as HhGetPidReply;
if (pidReply != null)
{
pid = pidReply.Pid;
return true;
}
}
}
catch (Exception)
{
return false;
}
}
#endregion Public methods.
#region Private methods.
private void SendHostMessage(uint kind)
{
var str = host as System.Runtime.InteropServices.ComTypes.IStream;
HhMessageHeader h = new HhMessageHeader();
h.Length = HhMessageHeader.FixedSize;
h.Kind = kind;
str.Write(h.ToByteArray(), (int)h.Length, IntPtr.Zero);
}
private uint BytesAsUInt32(byte[] bytes)
{
return (new System.IO.BinaryReader(new System.IO.MemoryStream(bytes))).ReadUInt32();
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct HhMessageHeader
{
public uint Length;
public uint Kind;
public static readonly uint FixedSize = 8;
public byte[] ToByteArray()
{
var stream = new System.IO.MemoryStream();
var writer = new System.IO.BinaryWriter(stream);
writer.Write(this.Length);
writer.Write(this.Kind);
writer.Flush();
return stream.ToArray();
}
}
public const uint GetPidMsgId = 1;
public const uint ShutdownMsgId = 2;
public const uint StartRendererMsgId = 3;
public const uint StopRendererMsgId = 4;
public const uint SetPayloadMsgId = 5;
public const uint ReadLogMsgId = 6;
public const uint GetPidMsgReplyId = 100 + GetPidMsgId;
public const uint StartRendererMsgReplyId = 100 + StartRendererMsgId;
public const uint StopRendererMsgReplyId = 100 + StopRendererMsgId;
public const uint SetPayloadMsgReplyId = 100 + SetPayloadMsgId;
public const uint ReadLogMsgReplyId = 100 + ReadLogMsgId;
[System.Runtime.InteropServices.ComImport]
[System.Runtime.InteropServices.Guid("7FD7A859-6C6B-4352-8F1E-C67BB62E774B")]
class CHlslHost
{
}
private static string HhKindToText(uint kind)
{
return ((HhMessageId)kind).ToString();
}
private static string HResultToString(uint hr)
{
if (hr == 0) return "OK";
if (hr == 1) return "S_FALSE";
return "0x" + hr.ToString("x");
}
#endregion Private methods.
}
}
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dotnetc/OptEditorForm.cs
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DotNetDxc;
namespace MainNs
{
public partial class OptEditorForm : Form
{
private TextSection[] sections;
public OptEditorForm()
{
InitializeComponent();
}
public Font CodeFont
{
get { return this.CodeBox.Font; }
set
{
this.CodeBox.Font = value;
this.LogBox.Font = value;
}
}
public TextSection[] Sections
{
get
{
return sections;
}
set
{
this.sections = value;
PassesListBox.Items.AddRange(value);
}
}
public IDxcLibrary Library { get; set; }
public IDxcBlob HighLevelSource { get; set; }
private void UpdateCodeBox()
{
CodeBox.Clear();
int index = PassesListBox.SelectedIndex;
if (index == -1) return;
TextSection section = (TextSection)PassesListBox.SelectedItem;
TextSection prior = index == 0 ? null : PassesListBox.Items[index - 1] as TextSection;
if (prior == null || section.Text == prior.Text || RightButton.Checked)
CodeBox.Text = section.Text;
else if (LeftButton.Checked)
CodeBox.Text = (prior == null) ? "(no prior text)" : prior.Text;
else
TextSection.RunDiff(prior, section, CodeBox);
CodeBox.Modified = false;
InvalidateApplyChanges();
}
private void LeftButton_CheckedChanged(object sender, EventArgs e)
{
UpdateCodeBox();
}
private void ApplyChangesButton_Click(object sender, EventArgs e)
{
// Turn the text into the expected encoding.
IDxcBlobEncoding sourceBlob = EditorForm.CreateBlobForText(this.Library, this.CodeBox.Text);
sourceBlob = this.Library.GetBlobAstUf8(sourceBlob);
IDxcBlob bitcodeBlob = sourceBlob;
List<string> passes = new List<string>();
passes.Add("hlsl-passes-resume");
for (int i = PassesListBox.SelectedIndex; i < PassesListBox.Items.Count; ++i)
{
passes.Add(((TextSection)PassesListBox.Items[i]).Title);
}
string[] options = EditorForm.CreatePassOptions(passes, false, true);
EditorForm.OptimizeResult opt = EditorForm.RunOptimize(this.Library, options, bitcodeBlob);
if (!opt.Succeeded)
{
MessageBox.Show("Failed to optimize: " + opt.ResultText);
return;
}
OptEditorForm form = new OptEditorForm();
form.CodeFont = this.CodeBox.Font;
form.Library = this.Library;
form.HighLevelSource = this.HighLevelSource;
form.Sections = TextSection.EnumerateSections(new string[] { "MODULE-PRINT", "Phase:" }, opt.ResultText).ToArray();
form.StartPosition = FormStartPosition.CenterParent;
form.Show(this);
}
private void OptEditorForm_Load(object sender, EventArgs e)
{
RichTextBox rtb = CodeBox;
var helper = new EditorForm.LogContextMenuHelper(rtb);
rtb.ContextMenu = new ContextMenu(
new MenuItem[]
{
new MenuItem("Show Graph", helper.ShowGraphClick)
});
}
private void CodeBox_TextChanged(object sender, EventArgs e)
{
InvalidateApplyChanges();
}
private void InvalidateApplyChanges()
{
this.ApplyChangesButton.Enabled = this.CodeBox.Modified && RightButton.Checked;
}
private void PassesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateCodeBox();
}
private void CodeBox_SelectionChanged(object sender, EventArgs e)
{
EditorForm.HandleCodeSelectionChanged(this.CodeBox, null);
}
private void CopyContainerButton_Click(object sender, EventArgs e)
{
// The intent is to copy compiled code (possibly customized) into the
// clipboard so it can be pasted into an XML file to be run interactively.
var text = this.CodeBox.Text;
var source = EditorForm.CreateBlobForText(this.Library, text);
var assembler = HlslDxcLib.CreateDxcAssembler();
var assembleResult = assembler.AssembleToContainer(source);
if (assembleResult.GetStatus() < 0)
{
var errors = assembleResult.GetErrors();
MessageBox.Show(EditorForm.GetStringFromBlob(this.Library, errors));
return;
}
var container = assembleResult.GetResult();
// Now copy that to the clipboard.
var bytes = ContainerData.BlobToBytes(container);
var stream = new System.IO.MemoryStream(bytes);
var dataObj = new DataObject();
dataObj.SetData(ContainerData.DataFormat.Name, stream);
dataObj.SetText(text);
Clipboard.SetDataObject(dataObj, true);
}
private void btnSaveAll_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
string fileName = saveFileDialog1.FileName;
for (int i = 0; i < sections.Length; i++)
{
TextSection Section = sections[i];
string fullName = string.Format("{0}_{1}_{2}.ll", fileName, i, Section.Title);
System.IO.File.WriteAllLines(fullName, Section.Lines);
}
}
private void btnViewCFGOnly_Click(object sender, EventArgs e)
{
if (PassesListBox.SelectedIndex == -1)
{
MessageBox.Show("Select a pass first");
return;
}
TextSection section = (TextSection)PassesListBox.SelectedItem;
var source = EditorForm.CreateBlobForText(this.Library, section.Text);
source = this.Library.GetBlobAstUf8(source);
string[] options = new string[1];
options[0] = "-view-cfg-only";
EditorForm.OptimizeResult opt = EditorForm.RunOptimize(this.Library, options, source);
if (!opt.Succeeded)
{
MessageBox.Show("Failed to optimize: " + opt.ResultText);
return;
}
const string dotStart = "digraph";
string dotText = opt.ResultText.Substring(opt.ResultText.IndexOf(dotStart));
while (dotText.LastIndexOf(dotStart) != -1)
{
string dot = dotText.Substring(dotText.LastIndexOf(dotStart));
dot = dot.Substring(0, dot.LastIndexOf("}") + 1);
dot = dot.Replace("\u0001??", "");
dot = dot.Replace("\u0001?", "");
EditorForm.LogContextMenuHelper.ShowDot(dot);
dotText = dotText.Substring(0, dotText.LastIndexOf(dotStart));
}
}
}
public class TextSection
{
private string[] lines;
private int[] lineHashes;
public string Title;
public string Text;
public bool HasChange;
public string[] Lines
{
get
{
if (lines == null)
lines = Text.Split(new char[] { '\n' });
return lines;
}
}
public int[] LineHashes
{
get
{
if (lineHashes == null)
lineHashes = lines.Select(l => l.GetHashCode()).ToArray();
return lineHashes;
}
}
public override string ToString()
{
return HasChange ? "* " + Title : Title;
}
private static bool ClosestMatch(string text, ref int start, string[] separators, out string separator)
{
int closest = -1;
separator = null;
for (int i = 0; i < separators.Length; ++i)
{
int idx = text.IndexOf(separators[i], start);
if (idx >= 0 && (closest < 0 || idx < closest))
{
closest = idx;
separator = separators[i];
}
}
start = closest;
return closest >= 0;
}
public static IEnumerable<TextSection> EnumerateSections(string[] separators, string text)
{
string prior = null;
string separator;
int idx = 0;
while (idx >= 0 && ClosestMatch(text, ref idx, separators, out separator))
{
int lineEnd = text.IndexOf('\n', idx);
if (lineEnd < 0) break;
string title = text.Substring(idx + separator.Length, lineEnd - (idx + separator.Length));
title = title.Trim();
int next = lineEnd;
if (!ClosestMatch(text, ref next, separators, out separator))
next = -1;
string sectionText = (next < 0) ? text.Substring(lineEnd + 1) : text.Substring(lineEnd + 1, next - (lineEnd + 1));
sectionText = sectionText.Trim() + "\n";
bool hasChange = sectionText != prior;
yield return new TextSection { HasChange = hasChange, Title = title, Text = hasChange ? sectionText : prior };
idx = next;
prior = sectionText;
}
}
public static void RunDiff(TextSection oldText, TextSection newText, RichTextBox rtb)
{
// Longest common subsequence, simple edition. If/when something faster is needed,
// should probably take a dependency on a proper diff package. Other than shorter
// comparison windows, other things to look for are avoiding creating strings here,
// working on the RichTextBox buffer directly for color, and unique'ing lines.
string[] oldLines = oldText.Lines;
string[] newLines = newText.Lines;
// Reduce strings to hashes.
int[] oldHashes = oldText.LineHashes;
int[] newHashes = newText.LineHashes;
// Reduce by trimming prefix and suffix.
int diffStart = 0;
while (diffStart < oldHashes.Length && diffStart < newHashes.Length && oldHashes[diffStart] == newHashes[diffStart])
diffStart++;
int newDiffEndExc = newLines.Length, oldDiffEndExc = oldLines.Length;
while (newDiffEndExc > diffStart && oldDiffEndExc > diffStart)
{
if (oldHashes[oldDiffEndExc - 1] == newHashes[newDiffEndExc - 1])
{
oldDiffEndExc--;
newDiffEndExc--;
}
else
break;
}
int suffixLength = (newLines.Length - newDiffEndExc);
// Build LCS table.
int oldLen = oldDiffEndExc - diffStart, newLen = newDiffEndExc - diffStart;
int[,] lcs = new int[oldLen + 1, newLen + 1]; // already zero-initialized
for (int i = 0; i < oldLen; i++)
for (int j = 0; j < newLen; j++)
if (oldHashes[i + diffStart] == newHashes[j + diffStart])
lcs[i + 1, j + 1] = lcs[i, j] + 1;
else
lcs[i + 1, j + 1] = Math.Max(lcs[i, j + 1], lcs[i + 1, j]);
// Print the diff - common prefix, backtracked diff and common suffix.
rtb.AppendLines(" ", newLines, 0, diffStart, Color.White);
{
int i = oldLen, j = newLen;
Stack<string> o = new Stack<string>();
for (;;)
{
if (i > 0 && j > 0 && oldHashes[diffStart + i - 1] == newHashes[diffStart + j - 1])
{
o.Push(" " + oldLines[diffStart + i - 1]);
i--;
j--;
}
else if (j > 0 && (i == 0 || lcs[i, j - 1] >= lcs[i - 1, j]))
{
o.Push("+ " + newLines[diffStart + j - 1]);
j--;
}
else if (i > 0 && (j == 0 || lcs[i, j - 1] < lcs[i - 1, j]))
{
o.Push("- " + oldLines[diffStart + i - 1]);
i--;
}
else
{
break;
}
}
while (o.Count != 0)
{
string line = o.Pop();
Color c = (line[0] == ' ') ? Color.White :
((line[0] == '+') ? Color.Yellow : Color.Red);
rtb.AppendLine(line, c);
}
}
rtb.AppendLines(" ", newLines, newDiffEndExc, (newLines.Length - newDiffEndExc), Color.White);
}
}
}
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dotnetc/AsmColorizer.cs
|
///////////////////////////////////////////////////////////////////////////////
// //
// AsmColorizer.cs //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Provides a component that provides color information for DXIL assembler. //
// //
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using DotNetDxc;
namespace MainNs
{
enum AsmRangeKind
{
WS,
Comment,
LLVMTypeName,
Keyword,
Metadata,
Punctuation,
Instruction,
Label,
GlobalVar,
LocalVar,
Number,
StringConstant,
AttributeGroup,
Other
}
struct AsmRange
{
public AsmRange(int start, int length, AsmRangeKind rangeKind)
{
this.Start = start;
this.Length = length;
this.RangeKind = rangeKind;
}
public int Start;
public int Length;
public AsmRangeKind RangeKind;
}
/// <summary>
/// Simplified colorizer. Takes some shortcuts assuming well-formed assembler in some cases.
/// </summary>
/// <remarks>
/// See lib\AsmParser\LLToken.h for more details.
/// </remarks>
class AsmColorizer
{
private static readonly string Punctuation = ".,(){}\\\"";
private static readonly string WS = " \t\r\n";
private static readonly string[] InstKeywords;
private static readonly string[] TypeKeywords;
private static readonly string[] Keywords;
private static bool IsAlpha(char ch)
{
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z');
}
private static bool IsAlnum(char ch)
{
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9');
}
private static bool IsMetadataChar(char ch)
{
// digits can't be leads, but already identified by leading '$'
return IsAlnum(ch) || ch == '-' || ch == '$' || ch == '.' || ch == '_' || ch == '\\';
}
private static bool IsNumericLead(char ch)
{
return ('0' <= ch && ch <= '9') || ch == '-' || ch == '+';
}
private static bool IsNumeric(char ch)
{
return ch == 'E' || ch == 'e' || ch == '.' || IsNumericLead(ch);
}
private static bool IsLabelChar(char ch)
{
return IsAlnum(ch) || ch == '-' || ch == '$' || ch == '.' || ch == '_';
}
private static bool IsWS(char ch)
{
return WS.IndexOf(ch) >= 0;
}
private static bool IsPunctuation(char ch)
{
return Punctuation.IndexOf(ch) >= 0;
}
private static AsmRangeKind ClassifyIdentifierLike(string text, int start, int end)
{
char last = text[end - 1];
if (last == ':')
return AsmRangeKind.Label;
int intWidth;
if (text[start] == 'i' && Int32.TryParse(text.Substring(start + 1, 1), out intWidth))
return AsmRangeKind.LLVMTypeName;
string val = text.Substring(start, end - start);
if (Array.BinarySearch(Keywords, val, StringComparer.Ordinal) >= 0)
return AsmRangeKind.Keyword;
if (Array.BinarySearch(TypeKeywords, val, StringComparer.Ordinal) >= 0)
return AsmRangeKind.LLVMTypeName;
if (Array.BinarySearch(InstKeywords, val, StringComparer.Ordinal) >= 0)
return AsmRangeKind.Instruction;
// Pending: DWKEYWORD, a few others
return AsmRangeKind.Other;
}
static AsmColorizer()
{
string[] keywords = (
"true,false,declare,define,global,constant," +
"private,internal,available_externally,linkonce,linkonce_odr,weak,weak_odr,appending,dllimport,dllexport," +
"common,default,hidden,protected,unnamed_addr,externally_initialized,extern_weak,external,thread_local,localdynamic,initialexec,localexec,zeroinitializer,undef," +
"null,to,tail,musttail,target,triple,unwind,deplibs,datalayout,volatile,atomic,unordered,monotonic,acquire,release,acq_rel,seq_cst,singlethread," +
"nnan,ninf,nsz,arcp,fast,nuw,nsw,exact,inbounds,align,addrspace,section,alias,module,asm,sideeffect,alignstack,inteldialect,gc,prefix,prologue," +
"null,to,tail,musttail,target,triple,unwind,deplibs,datalayout,volatile,atomic,unordered,monotonic,acquire,release,acq_rel,seq_cst,singlethread," +
"ccc,fastcc,coldcc,x86_stdcallcc,x86_fastcallcc,x86_thiscallcc,x86_vectorcallcc,arm_apcscc,arm_aapcscc,arm_aapcs_vfpcc,msp430_intrcc," +
"ptx_kernel,ptx_device,spir_kernel,spir_func,intel_ocl_bicc,x86_64_sysvcc,x86_64_win64cc,webkit_jscc,anyregcc," +
"preserve_mostcc,preserve_allcc,ghccc,cc,c," +
"attributes," +
"alwaysinline,argmemonly,builtin,byval,inalloca,cold,convergent,dereferenceable,dereferenceable_or_null,inlinehint,inreg,jumptable," +
"minsize,naked,nest,noalias,nobuiltin,nocapture,noduplicate,noimplicitfloat,noinline,nonlazybind,nonnull,noredzone,noreturn," +
"nounwind,optnone,optsize,readnone,readonly,returned,returns_twice,signext,sret,ssp,sspreq,sspstrong,safestack," +
"sanitize_address,sanitize_thread,sanitize_memory,uwtable,zeroext," +
"type,opaque," +
"comdat," +
"any,exactmatch,largest,noduplicates,samesize," + // Comdat types
"eq,ne,slt,sgt,sle,sge,ult,ugt,ule,uge,oeq,one,olt,ogt,ole,oge,ord,uno,ueq,une," +
"xchg,nand,max,min,umax,umin," +
"x,blockaddress," +
"distinct," + // Metadata types.
"uselistorder,uselistorder_bb," + // Use-list order directives.
"personality,cleanup,catch,filter"
).Split(',');
Array.Sort(keywords, StringComparer.Ordinal);
Keywords = keywords;
string[] typeKeywords = "void,half,float,double,x86_fp80,fp128,ppc_fp128,label,metadata,x86_mmx".Split(',');
Array.Sort(typeKeywords, StringComparer.Ordinal);
TypeKeywords = typeKeywords;
string[] instKeywords = (
"add,fadd,sub,fsub,mul,fmul,udiv,sdiv,fdiv,urem,srem,frem,shl,lshr,ashr,and,or,xor,icmp,fcmp," +
"phi,call,trunc,zext,sext,fptrunc,fpext,uitofp,sitofp,fptoui,fptosi,inttoptr,ptrtoint,bitcast,addrspacecast,select,va,ret,br,switch,indirectbr,invoke,resume,unreachable," +
"alloca,load,store,cmpxchg,atomicrmw,fence,getelementptr," +
"extractelement,insertelement,shufflevector,extractvalue,insertvalue,landingpad"
).Split(',');
Array.Sort(instKeywords, StringComparer.Ordinal);
InstKeywords = instKeywords;
}
private static void LexVar(string text, int start, ref int end)
{
char ch = text[start];
if (ch == '"')
{
for (;;)
{
if (end == text.Length) return;
if (text[end] == '"') return; // should unescape here
++end;
}
}
for (;;)
{
if (end == text.Length) return;
if (!IsLabelChar(text[end])) return;
++end;
}
}
internal IEnumerable<AsmRange> GetColorRanges(string text)
{
AsmRange range = new AsmRange();
while (GetNextRange(text, ref range))
{
yield return range;
}
}
internal IEnumerable<AsmRange> GetColorRanges(string text, int start, int end)
{
AsmRange range = new AsmRange();
range.Start = start;
while (range.Start < end && GetNextRange(text, ref range))
{
yield return range;
}
}
internal bool GetNextRange(string text, ref AsmRange range)
{
int start, end;
start = range.Start + range.Length;
while (start != text.Length)
{
end = start + 1;
AsmRangeKind kind = AsmRangeKind.Other;
char ch = text[start];
if (IsWS(ch))
{
kind = AsmRangeKind.WS;
while (end != text.Length && IsWS(text[end]))
++end;
}
else if (ch == ';')
{
kind = AsmRangeKind.Comment;
while (end != text.Length && text[end] != '\n')
++end;
if (end != text.Length) ++end;
}
else if (ch == '@')
{
kind = AsmRangeKind.GlobalVar;
LexVar(text, start, ref end);
}
//else if (ch == '$')
//{
// lex dollar
//}
else if (ch == '%')
{
kind = AsmRangeKind.LocalVar;
LexVar(text, start, ref end);
}
else if (ch == '"')
{
kind = AsmRangeKind.StringConstant;
while (end != text.Length && (text[end] != '"'))
++end;
if (end != text.Length) ++end;
if (end != text.Length && text[end] == ':')
{
++end;
kind = AsmRangeKind.Label;
}
}
else if (ch == '!')
{
kind = AsmRangeKind.Metadata;
while (end != text.Length && IsMetadataChar(text[end]))
++end;
}
else if (ch == '#')
{
kind = AsmRangeKind.AttributeGroup;
while (end != text.Length && Char.IsDigit(text[end]))
++end;
if (end != text.Length) ++end;
}
else if (IsNumericLead(ch))
{
// lex number or positive
kind = AsmRangeKind.Number;
while (end != text.Length && IsNumeric(text[end]))
++end;
if (end != text.Length) ++end;
}
else if (IsPunctuation(ch))
{
kind = AsmRangeKind.Punctuation;
}
else
{
kind = AsmRangeKind.Other;
while (end != text.Length && !IsWS(text[end]) && !IsPunctuation(text[end]))
++end;
kind = ClassifyIdentifierLike(text, start, end);
}
range = new AsmRange(start, end - start, kind);
return true;
}
return false;
}
}
}
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dotnetc/dia2.cs
|
///////////////////////////////////////////////////////////////////////////////
// //
// dia2.cs //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
///////////////////////////////////////////////////////////////////////////////
namespace dia2
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
[ComImport]
[Guid("e6756135-1e65-4d17-8576-610761398c3c")]
public class DiaDataSource
{
}
[ComImport]
[Guid("79F1BB5F-B66E-48e5-B6A9-1545C323CA3D")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDiaDataSource
{
[return: MarshalAs(UnmanagedType.BStr)]
string get_lastError();
void loadDataFromPdb(string path);
void loadAndValidateDataFromPdb(string path, ref Guid pcsig70, UInt32 sig, UInt32 age);
void loadDataForExe(string executable, string searchPath, [MarshalAs(UnmanagedType.IUnknown)] object pCallback);
void loadDataFromIStream(IStream pIStream);
IDiaSession openSession();
// loadDataFromCodeViewInfo
// loadDataFromMiscInfo
}
[ComImport]
[Guid("2F609EE1-D1C8-4E24-8288-3326BADCD211")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDiaSession
{
UInt64 get_loadAddress();
void put_loadAddress(UInt64 value);
IDiaSymbol get_globalScope();
IDiaEnumTables getEnumTables();
void getSymbolsByAddr();
void findChildren();
void findChildrenEx();
void findChildrenExByAddr();
void findChildrenExByVA();
void findChildrenExByRVA();
void findSymbolByAddr();
void findSymbolByRVA();
void findSymbolByVA();
void findSymbolByToken();
void symsAreEquiv();
void symbolById();
void findSymbolByRVAEx();
void findSymbolByVAEx();
void findFile();
void findFileById();
void findLines();
void findLinesByAddr();
void findLinesByRVA();
void findLinesByVA();
void findLinesByLinenum();
object /*IDiaEnumInjectedSources*/ findInjectedSource(string srcFile);
object /*IDiaEnumDebugStreams*/ getEnumDebugStreams();
void findInlineFramesByAddr();
void findInlineFramesByRVA();
void findInlineFramesByVA();
void findInlineeLines();
void findInlineeLinesByAddr();
void findInlineeLinesByRVA();
void findInlineeLinesByVA();
void findInlineeLinesByLinenum();
void findInlineesByName();
void findAcceleratorInlineeLinesByLinenum();
void findSymbolsForAcceleratorPointerTag();
void findSymbolsByRVAForAcceleratorPointerTag();
void findAcceleratorInlineesByName();
void addressForVA();
void addressForRVA();
void findILOffsetsByAddr();
void findILOffsetsByRVA();
void findILOffsetsByVA();
void findInputAssemblyFiles();
void findInputAssembly();
void findInputAssemblyById();
void getFuncMDTokenMapSize();
void getFuncMDTokenMap();
void getTypeMDTokenMapSize();
void getTypeMDTokenMap();
void getNumberOfFunctionFragments_VA();
void getNumberOfFunctionFragments_RVA();
void getFunctionFragments_VA();
void getFunctionFragments_RVA();
object /*IDiaEnumSymbols*/ getExports();
object /*IDiaEnumSymbols*/ getHeapAllocationSites();
void findInputAssemblyFile();
}
[ComImport]
[Guid("cb787b2f-bd6c-4635-ba52-933126bd2dcd")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDiaSymbol
{
UInt32 get_symIndexId();
UInt32 get_symTag();
[return: MarshalAs(UnmanagedType.BStr)]
string get_name();
IDiaSymbol get_lexicalParent();
IDiaSymbol get_classParent();
IDiaSymbol get_type();
UInt32 get_dataKind();
UInt32 get_locationType();
UInt32 get_addressSection();
UInt32 get_addressOffset();
UInt32 get_relativeVirtualAddress();
UInt64 get_virtualAddress();
UInt32 get_registerId();
Int32 get_offset();
UInt64 get_length();
UInt32 get_slot();
bool get_volatileType();
bool get_constType();
bool get_unalignedType();
UInt32 get_access();
[return: MarshalAs(UnmanagedType.BStr)]
string get_libraryName();
UInt32 get_platform();
UInt32 get_language();
bool get_editAndContinueEnabled();
UInt32 get_frontEndMajor();
UInt32 get_frontEndMinor();
UInt32 get_frontEndBuild();
UInt32 get_backEndMajor();
UInt32 get_backEndMinor();
UInt32 get_backEndBuild();
[return: MarshalAs(UnmanagedType.BStr)]
string get_sourceFileName();
[return: MarshalAs(UnmanagedType.BStr)]
string get_unused();
UInt32 get_thunkOrdinal();
Int32 get_thisAdjust();
UInt32 get_virtualBaseOffset();
bool get_virtual();
bool get_intro();
bool get_pure();
UInt32 get_callingConvention();
[return: MarshalAs(UnmanagedType.AsAny)] // VARIANT
object get_value();
UInt32 get_baseType();
UInt32 get_token();
UInt32 get_timeStamp();
Guid get_guid();
[return: MarshalAs(UnmanagedType.BStr)]
string get_symbolsFileName();
bool get_reference();
UInt32 get_count();
UInt32 get_bitPosition();
IDiaSymbol get_arrayIndexType();
bool get_packed();
bool get_constructor();
bool get_overloadedOperator();
bool get_nested();
bool get_hasNestedTypes();
bool get_hasAssignmentOperator();
bool get_hasCastOperator();
bool get_scoped();
bool get_virtualBaseClass();
bool get_indirectVirtualBaseClass();
Int32 get_virtualBasePointerOffset();
IDiaSymbol get_virtualTableShape();
UInt32 get_lexicalParentId();
UInt32 get_classParentId();
UInt32 get_typeId();
UInt32 get_arrayIndexTypeId();
UInt32 get_virtualTableShapeId();
bool get_code();
bool get_function();
bool get_managed();
bool get_msil();
UInt32 get_virtualBaseDispIndex();
[return: MarshalAs(UnmanagedType.BStr)]
string get_undecoratedName();
UInt32 get_age();
UInt32 get_signature();
bool get_compilerGenerated();
bool get_addressTaken();
UInt32 get_rank();
IDiaSymbol get_lowerBound();
IDiaSymbol get_upperBound();
UInt32 get_lowerBoundId();
UInt32 get_upperBoundId();
void get_dataBytes(UInt32 cbData, out UInt32 pcbData, out byte[] pbData);
void findChildren();
void findChildrenEx();
void findChildrenExByAddr();
void findChildrenExByVA();
void findChildrenExByRVA();
UInt32 get_targetSection();
UInt32 get_targetOffset();
UInt32 get_targetRelativeVirtualAddress();
UInt64 get_targetVirtualAddress();
UInt32 get_machineType();
UInt32 get_oemId();
UInt32 get_oemSymbolId();
void get_types();
void get_typeIds();
IDiaSymbol get_objectPointerType();
UInt32 get_udtKind();
void get_undecoratedNameEx();
bool get_noReturn();
bool get_customCallingConvention();
bool get_noInline();
bool get_optimizedCodeDebugInfo();
bool get_notReached();
bool get_interruptReturn();
bool get_farReturn();
bool get_isStatic();
bool get_hasDebugInfo();
bool get_isLTCG();
bool get_isDataAligned();
bool get_hasSecurityChecks();
[return: MarshalAs(UnmanagedType.BStr)]
string get_compilerName();
bool get_hasAlloca();
bool get_hasSetJump();
bool get_hasLongJump();
bool get_hasInlAsm();
bool get_hasEH();
bool get_hasSEH();
bool get_hasEHa();
bool get_isNaked();
bool get_isAggregated();
bool get_isSplitted();
IDiaSymbol get_container();
bool get_inlSpec();
bool get_noStackOrdering();
IDiaSymbol get_virtualBaseTableType();
bool get_hasManagedCode();
bool get_isHotpatchable();
bool get_isCVTCIL();
bool get_isMSILNetmodule();
bool get_isCTypes();
bool get_isStripped();
UInt32 get_frontEndQFE();
UInt32 get_backEndQFE();
bool get_wasInlined();
bool get_strictGSCheck();
bool get_isCxxReturnUdt();
bool get_isConstructorVirtualBase();
bool get_RValueReference();
IDiaSymbol get_unmodifiedType();
bool get_framePointerPresent();
bool get_isSafeBuffers();
bool get_intrinsic();
bool get_sealed();
bool get_hfaFloat();
bool get_hfaDouble();
UInt32 get_liveRangeStartAddressSection();
UInt32 get_liveRangeStartAddressOffset();
UInt32 get_liveRangeStartRelativeVirtualAddress();
UInt32 get_countLiveRanges();
UInt64 get_liveRangeLength();
UInt32 get_offsetInUdt();
UInt32 get_paramBasePointerRegisterId();
UInt32 get_localBasePointerRegisterId();
bool get_isLocationControlFlowDependent();
UInt32 get_stride();
UInt32 get_numberOfRows();
UInt32 get_numberOfColumns();
bool get_isMatrixRowMajor();
void get_numericProperties();
void get_modifierValues();
bool get_isReturnValue();
bool get_isOptimizedAway();
UInt32 get_builtInKind();
UInt32 get_registerType();
UInt32 get_baseDataSlot();
UInt32 get_baseDataOffset();
UInt32 get_textureSlot();
UInt32 get_samplerSlot();
UInt32 get_uavSlot();
UInt32 get_sizeInUdt();
UInt32 get_memorySpaceKind();
UInt32 get_unmodifiedTypeId();
UInt32 get_subTypeId();
IDiaSymbol get_subType();
UInt32 get_numberOfModifiers();
UInt32 get_numberOfRegisterIndices();
bool get_isHLSLData();
bool get_isPointerToDataMember();
bool get_isPointerToMemberFunction();
bool get_isSingleInheritance();
bool get_isMultipleInheritance();
bool get_isVirtualInheritance();
bool get_restrictedType();
bool get_isPointerBasedOnSymbolValue();
IDiaSymbol get_baseSymbol();
UInt32 get_baseSymbolId();
[return: MarshalAs(UnmanagedType.BStr)]
string get_objectFileName();
bool get_isAcceleratorGroupSharedLocal();
bool get_isAcceleratorPointerTagLiveRange();
bool get_isAcceleratorStubFunction();
UInt32 get_numberOfAcceleratorPointerTags();
bool get_isSdl();
bool get_isWinRTPointer();
bool get_isRefUdt();
bool get_isValueUdt();
bool get_isInterfaceUdt();
void findInlineFramesByAddr();
void findInlineFramesByRVA();
void findInlineFramesByVA();
void findInlineeLines();
void findInlineeLinesByAddr();
void findInlineeLinesByRVA();
void findInlineeLinesByVA();
void findSymbolsForAcceleratorPointerTag();
void findSymbolsByRVAForAcceleratorPointerTag();
void get_acceleratorPointerTags();
void getSrcLineOnTypeDefn();
bool get_isPGO();
bool get_hasValidPGOCounts();
bool get_isOptimizedForSpeed();
UInt32 get_PGOEntryCount();
UInt32 get_PGOEdgeCount();
UInt64 get_PGODynamicInstructionCount();
UInt32 get_staticSize();
UInt32 get_finalLiveStaticSize();
[return: MarshalAs(UnmanagedType.BStr)]
string get_phaseName();
bool get_hasControlFlowCheck();
bool get_constantExport();
bool get_dataExport();
bool get_privateExport();
bool get_noNameExport();
bool get_exportHasExplicitlyAssignedOrdinal();
bool get_exportIsForwarder();
UInt32 get_ordinal();
UInt32 get_frameSize();
UInt32 get_exceptionHandlerAddressSection();
UInt32 get_exceptionHandlerAddressOffset();
UInt32 get_exceptionHandlerRelativeVirtualAddress();
UInt64 get_exceptionHandlerVirtualAddress();
void findInputAssemblyFile();
UInt32 get_characteristics();
IDiaSymbol get_coffGroup();
UInt32 get_bindID();
UInt32 get_bindSpace();
UInt32 get_bindSlot();
}
[ComImport]
[Guid("C65C2B0A-1150-4d7a-AFCC-E05BF3DEE81E")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDiaEnumTables
{
void get__NewEnum();
UInt32 get_Count();
IDiaTable Item(object index);
UInt32 Next(UInt32 count,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.Interface, SizeParamIndex = 2)]
ref IDiaTable[] tables, out UInt32 fetched);
void Skip(UInt32 count);
void Reset();
IDiaEnumTables Clone();
}
[ComImport]
[Guid("B388EB14-BE4D-421d-A8A1-6CF7AB057086")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDiaLineNumber
{
IDiaSymbol get_compiland();
IDiaSourceFile get_sourceFile();
uint get_lineNumber();
uint get_lineNumberEnd();
uint get_columnNumber();
uint get_columnNumberEnd();
uint get_addressSection();
uint get_addressOffset();
uint get_relativeVirtualAddress();
UInt64 get_virtualAddress();
uint get_length();
uint get_sourceFileId();
bool get_statement();
uint get_compilandId();
}
[ComImport]
[Guid("0CF4B60E-35B1-4c6c-BDD8-854B9C8E3857")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDiaSectionContrib
{
IDiaSymbol get_compiland();
uint get_addressSection();
uint get_addressOffset();
uint get_relativeVirtualAddress();
UInt64 get_virtualAddress();
uint get_length();
bool get_notPaged();
bool get_code();
bool get_initializedData();
bool get_uninitializedData();
bool get_remove();
bool get_comdat();
bool get_discardable();
bool get_notCached();
bool get_share();
bool get_execute();
bool get_read();
bool get_write();
uint get_dataCrc();
uint get_relocationsCrc();
uint get_compilandId();
bool get_code16bit();
}
[ComImport]
[Guid("0775B784-C75B-4449-848B-B7BD3159545B")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDiaSegment
{
uint get_frame();
uint get_offset();
uint get_length();
bool get_read();
bool get_write();
bool get_execute();
uint get_addressSection();
uint get_relativeVirtualAddress();
UInt64 get_virtualAddress();
}
[ComImport]
[Guid("00000100-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IEnumUnknown
{
UInt32 Next(UInt32 count,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.IUnknown, SizeParamIndex = 2)]
ref object[] tables, out UInt32 fetched);
void Skip(UInt32 count);
void Reset();
IEnumUnknown Clone();
}
[ComImport]
[Guid("A2EF5353-F5A8-4eb3-90D2-CB526ACB3CDD")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDiaSourceFile
{
uint get_uniqueId();
[return: MarshalAs(UnmanagedType.BStr)]
string get_fileName();
void /* IDiaEnumSymbols */ get_compilands();
[PreserveSig] int get_checksum(uint cbData, out uint pcbData,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1, SizeParamIndex = 1)] ref byte[] pbData);
}
[ComImport]
[Guid("AE605CDC-8105-4a23-B710-3259F1E26112")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDiaInjectedSource
{
uint get_crc();
uint get_length();
[return: MarshalAs(UnmanagedType.BStr)]
string get_fileName();
[return: MarshalAs(UnmanagedType.BStr)]
string get_objectFilename();
[return: MarshalAs(UnmanagedType.BStr)]
string get_virtualFilename();
uint get_sourceCompression();
[PreserveSig]
int get_source(uint cbData, out uint pcbData,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U8, SizeParamIndex = 0)] byte[] pbData);
}
[ComImport]
[Guid("4A59FB77-ABAC-469b-A30B-9ECC85BFEF14")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDiaTable // : IEnumUnknown - need to replay vtable
{
UInt32 Next(UInt32 count,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.IUnknown, SizeParamIndex = 2)]
ref object[] tables, out UInt32 fetched);
void Skip(UInt32 count);
void Reset();
IEnumUnknown Clone();
[return: MarshalAs(UnmanagedType.IUnknown)]
object get__NewEnum();
[return: MarshalAs(UnmanagedType.BStr)]
string get_name();
Int32 get_Count();
[return: MarshalAs(UnmanagedType.IUnknown)]
object Item(UInt32 index);
}
public enum SymTagEnum
{
SymTagNull,
SymTagExe,
SymTagCompiland,
SymTagCompilandDetails,
SymTagCompilandEnv,
SymTagFunction,
SymTagBlock,
SymTagData,
SymTagAnnotation,
SymTagLabel,
SymTagPublicSymbol,
SymTagUDT,
SymTagEnum,
SymTagFunctionType,
SymTagPointerType,
SymTagArrayType,
SymTagBaseType,
SymTagTypedef,
SymTagBaseClass,
SymTagFriend,
SymTagFunctionArgType,
SymTagFuncDebugStart,
SymTagFuncDebugEnd,
SymTagUsingNamespace,
SymTagVTableShape,
SymTagVTable,
SymTagCustom,
SymTagThunk,
SymTagCustomType,
SymTagManagedType,
SymTagDimension,
SymTagCallSite,
SymTagInlineSite,
SymTagBaseInterface,
SymTagVectorType,
SymTagMatrixType,
SymTagHLSLType,
SymTagCaller,
SymTagCallee,
SymTagExport,
SymTagHeapAllocationSite,
SymTagCoffGroup,
SymTagMax
};
}
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dotnetc/FindDialog.cs
|
///////////////////////////////////////////////////////////////////////////////
// //
// FindDialog.cs //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;
namespace MainNs
{
public partial class FindDialog : Form
{
public FindDialog()
{
this.InitializeComponent();
}
public string FindText
{
get { return this.findControl.Text; }
set { this.findControl.Text = value; }
}
public TextBoxBase Target { get; set; }
private void CancelButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void FindButton_Click(object sender, EventArgs e)
{
string findText = this.FindText;
if (String.IsNullOrEmpty(findText)) return;
TextBoxBase target = this.Target;
if (target == null) return;
target.HideSelection = false;
int start = target.SelectionStart;
int end = start + target.SelectionLength;
int nextIndex = target.Text.IndexOf(findText, end);
if (nextIndex >= 0)
{
target.Select(nextIndex, findText.Length);
}
}
private void FindDialog_Load(object sender, EventArgs e)
{
this.ActiveControl = this.findControl;
}
}
}
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dxcvalidator/dxcvalidator.h
|
///////////////////////////////////////////////////////////////////////////////
// //
// dxcvalidator.h //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Implements the Dxil Validation //
// //
///////////////////////////////////////////////////////////////////////////////
#pragma once
#include <cstdint>
struct IDxcOperationResult;
struct IDxcBlob;
struct DxcBuffer;
namespace llvm {
class Module;
class raw_ostream;
class LLVMContext;
} // namespace llvm
namespace hlsl {
// For internal use only.
uint32_t validateWithOptDebugModule(
IDxcBlob *Shader, // Shader to validate.
uint32_t Flags, // Validation flags.
llvm::Module *DebugModule, // Debug module to validate, if available
IDxcOperationResult **Result // Validation output status, buffer, and errors
);
// IDxcValidator2
uint32_t validateWithDebug(
IDxcBlob *Shader, // Shader to validate.
uint32_t Flags, // Validation flags.
DxcBuffer *OptDebugBitcode, // Optional debug module bitcode to provide
// line numbers
IDxcOperationResult **Result // Validation output status, buffer, and errors
);
uint32_t getValidationVersion(unsigned *pMajor, unsigned *pMinor);
} // namespace hlsl
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dxcvalidator/CMakeLists.txt
|
# Copyright (C) Microsoft Corporation. All rights reserved.
# This file is distributed under the University of Illinois Open Source License.
# See LICENSE.TXT for details.
set( LLVM_LINK_COMPONENTS
${LLVM_TARGETS_TO_BUILD}
dxcsupport
DXIL
DxilContainer
DxilHash
DxilValidation
Option # option library
Support # just for assert and raw streams
)
add_clang_library(dxcvalidator
dxcvalidator.cpp
)
if (MINGW)
target_link_options(dxcvalidator PUBLIC -mconsole -municode)
target_link_libraries(dxcvalidator PRIVATE version)
endif()
target_compile_definitions(dxcvalidator
PRIVATE VERSION_STRING_SUFFIX=" for ${CMAKE_SYSTEM_NAME}")
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/dxcvalidator/dxcvalidator.cpp
|
///////////////////////////////////////////////////////////////////////////////
// //
// dxcvalidator.cpp //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Implements the DirectX Validator object. //
// //
///////////////////////////////////////////////////////////////////////////////
#include "dxc/Support/WinIncludes.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/LLVMContext.h"
#include "dxc/DxilContainer/DxilContainer.h"
#include "dxc/DxilHash/DxilHash.h"
#include "dxc/DxilValidation/DxilValidation.h"
#include "dxc/dxcapi.h"
#include "dxcvalidator.h"
#include "dxc/DxilRootSignature/DxilRootSignature.h"
#include "dxc/Support/FileIOHelper.h"
#include "dxc/Support/Global.h"
#include "dxc/Support/dxcapi.impl.h"
#ifdef _WIN32
#include "dxcetw.h"
#endif
using namespace llvm;
using namespace hlsl;
static void HashAndUpdate(DxilContainerHeader *Container) {
// Compute hash and update stored hash.
// Hash the container from this offset to the end.
static const uint32_t DXBCHashStartOffset =
offsetof(struct DxilContainerHeader, Version);
const unsigned char *DataToHash =
(const unsigned char *)Container + DXBCHashStartOffset;
unsigned AmountToHash = Container->ContainerSizeInBytes - DXBCHashStartOffset;
ComputeHashRetail(DataToHash, AmountToHash, Container->Hash.Digest);
}
static void HashAndUpdateOrCopy(uint32_t Flags, IDxcBlob *Shader,
IDxcBlob **Hashed) {
if (Flags & DxcValidatorFlags_InPlaceEdit) {
HashAndUpdate((DxilContainerHeader *)Shader->GetBufferPointer());
*Hashed = Shader;
Shader->AddRef();
} else {
CComPtr<AbstractMemoryStream> HashedBlobStream;
IFT(CreateMemoryStream(DxcGetThreadMallocNoRef(), &HashedBlobStream));
unsigned long CB;
IFT(HashedBlobStream->Write(Shader->GetBufferPointer(),
Shader->GetBufferSize(), &CB));
HashAndUpdate((DxilContainerHeader *)HashedBlobStream->GetPtr());
IFT(HashedBlobStream.QueryInterface(Hashed));
}
}
static uint32_t runValidation(
IDxcBlob *Shader,
uint32_t Flags, // Validation flags.
llvm::Module *DebugModule, // Debug module to validate, if available
AbstractMemoryStream *DiagMemStream) {
// Run validation may throw, but that indicates an inability to validate,
// not that the validation failed (eg out of memory). That is indicated
// by a failing HRESULT, and possibly error messages in the diagnostics
// stream.
raw_stream_ostream DiagStream(DiagMemStream);
return ValidateDxilContainer(Shader->GetBufferPointer(),
Shader->GetBufferSize(), DebugModule,
DiagStream);
}
static uint32_t
runRootSignatureValidation(IDxcBlob *Shader,
AbstractMemoryStream *DiagMemStream) {
const DxilContainerHeader *DxilContainer =
IsDxilContainerLike(Shader->GetBufferPointer(), Shader->GetBufferSize());
if (!DxilContainer)
return DXC_E_IR_VERIFICATION_FAILED;
const DxilProgramHeader *ProgramHeader =
GetDxilProgramHeader(DxilContainer, DFCC_DXIL);
const DxilPartHeader *PSVPart =
GetDxilPartByType(DxilContainer, DFCC_PipelineStateValidation);
const DxilPartHeader *RSPart =
GetDxilPartByType(DxilContainer, DFCC_RootSignature);
if (!RSPart)
return DXC_E_MISSING_PART;
if (ProgramHeader) {
// Container has shader part, make sure we have PSV.
if (!PSVPart)
return DXC_E_MISSING_PART;
}
try {
RootSignatureHandle RSH;
RSH.LoadSerialized((const uint8_t *)GetDxilPartData(RSPart),
RSPart->PartSize);
RSH.Deserialize();
raw_stream_ostream DiagStream(DiagMemStream);
if (ProgramHeader) {
if (!VerifyRootSignatureWithShaderPSV(
RSH.GetDesc(),
GetVersionShaderType(ProgramHeader->ProgramVersion),
GetDxilPartData(PSVPart), PSVPart->PartSize, DiagStream))
return DXC_E_INCORRECT_ROOT_SIGNATURE;
} else {
if (!VerifyRootSignature(RSH.GetDesc(), DiagStream, false))
return DXC_E_INCORRECT_ROOT_SIGNATURE;
}
} catch (...) {
return DXC_E_IR_VERIFICATION_FAILED;
}
return S_OK;
}
static uint32_t runDxilModuleValidation(IDxcBlob *Shader, // Shader to validate.
AbstractMemoryStream *DiagMemStream) {
if (IsDxilContainerLike(Shader->GetBufferPointer(), Shader->GetBufferSize()))
return E_INVALIDARG;
raw_stream_ostream DiagStream(DiagMemStream);
return ValidateDxilBitcode((const char *)Shader->GetBufferPointer(),
(uint32_t)Shader->GetBufferSize(), DiagStream);
}
uint32_t hlsl::validateWithDebug(
IDxcBlob *Shader, // Shader to validate.
uint32_t Flags, // Validation flags.
DxcBuffer *OptDebugBitcode, // Optional debug module bitcode to provide
// line numbers
IDxcOperationResult **Result // Validation output status, buffer, and errors
) {
if (Result == nullptr)
return E_INVALIDARG;
*Result = nullptr;
if (Shader == nullptr || Flags & ~DxcValidatorFlags_ValidMask)
return E_INVALIDARG;
if ((Flags & DxcValidatorFlags_ModuleOnly) &&
(Flags &
(DxcValidatorFlags_InPlaceEdit | DxcValidatorFlags_RootSignatureOnly)))
return E_INVALIDARG;
if (OptDebugBitcode &&
(OptDebugBitcode->Ptr == nullptr || OptDebugBitcode->Size == 0 ||
OptDebugBitcode->Size >= UINT32_MAX))
return E_INVALIDARG;
HRESULT hr = S_OK;
DxcThreadMalloc TM(DxcGetThreadMallocNoRef());
try {
LLVMContext Ctx;
CComPtr<AbstractMemoryStream> DiagMemStream;
hr = CreateMemoryStream(TM.GetInstalledAllocator(), &DiagMemStream);
if (FAILED(hr))
throw hlsl::Exception(hr);
raw_stream_ostream DiagStream(DiagMemStream);
llvm::DiagnosticPrinterRawOStream DiagPrinter(DiagStream);
PrintDiagnosticContext DiagContext(DiagPrinter);
Ctx.setDiagnosticHandler(PrintDiagnosticContext::PrintDiagnosticHandler,
&DiagContext, true);
std::unique_ptr<llvm::Module> DebugModule;
if (OptDebugBitcode) {
hr = ValidateLoadModule((const char *)OptDebugBitcode->Ptr,
(uint32_t)OptDebugBitcode->Size, DebugModule, Ctx,
DiagStream, /*bLazyLoad*/ false);
if (FAILED(hr))
throw hlsl::Exception(hr);
}
return validateWithOptDebugModule(Shader, Flags, DebugModule.get(), Result);
}
CATCH_CPP_ASSIGN_HRESULT();
return hr;
}
uint32_t hlsl::validateWithOptDebugModule(
IDxcBlob *Shader, // Shader to validate.
uint32_t Flags, // Validation flags.
llvm::Module *DebugModule, // Debug module to validate, if available
IDxcOperationResult **Result // Validation output status, buffer, and errors
) {
*Result = nullptr;
HRESULT hr = S_OK;
HRESULT validationStatus = S_OK;
DxcEtw_DxcValidation_Start();
DxcThreadMalloc TM(DxcGetThreadMallocNoRef());
try {
CComPtr<AbstractMemoryStream> DiagStream;
hr = CreateMemoryStream(TM.GetInstalledAllocator(), &DiagStream);
if (FAILED(hr))
throw hlsl::Exception(hr);
// Run validation may throw, but that indicates an inability to validate,
// not that the validation failed (eg out of memory).
if (Flags & DxcValidatorFlags_RootSignatureOnly)
validationStatus = runRootSignatureValidation(Shader, DiagStream);
else if (Flags & DxcValidatorFlags_ModuleOnly)
validationStatus = runDxilModuleValidation(Shader, DiagStream);
else
validationStatus = runValidation(Shader, Flags, DebugModule, DiagStream);
if (FAILED(validationStatus)) {
std::string msg("Validation failed.\n");
ULONG cbWritten;
DiagStream->Write(msg.c_str(), msg.size(), &cbWritten);
}
if (Flags & (DxcValidatorFlags_ModuleOnly)) {
// Validating a module only, return DXC_OUT_NONE instead of
// DXC_OUT_OBJECT.
CComPtr<IDxcBlob> pDiagBlob;
hr = DiagStream.QueryInterface(&pDiagBlob);
DXASSERT_NOMSG(SUCCEEDED(hr));
hr = DxcResult::Create(validationStatus, DXC_OUT_NONE,
{DxcOutputObject::ErrorOutput(
CP_UTF8, // TODO Support DefaultTextCodePage
(LPCSTR)pDiagBlob->GetBufferPointer(),
pDiagBlob->GetBufferSize())},
Result);
if (FAILED(hr))
throw hlsl::Exception(hr);
} else {
CComPtr<IDxcBlob> HashedBlob;
// Assemble the result object.
CComPtr<IDxcBlob> DiagBlob;
CComPtr<IDxcBlobEncoding> DiagBlobEnconding;
hr = DiagStream.QueryInterface(&DiagBlob);
DXASSERT_NOMSG(SUCCEEDED(hr));
hr = DxcCreateBlobWithEncodingSet(DiagBlob, CP_UTF8, &DiagBlobEnconding);
if (FAILED(hr))
throw hlsl::Exception(hr);
HashAndUpdateOrCopy(Flags, Shader, &HashedBlob);
hr = DxcResult::Create(
validationStatus, DXC_OUT_OBJECT,
{DxcOutputObject::DataOutput(DXC_OUT_OBJECT, HashedBlob),
DxcOutputObject::DataOutput(DXC_OUT_ERRORS, DiagBlobEnconding)},
Result);
if (FAILED(hr))
throw hlsl::Exception(hr);
}
}
CATCH_CPP_ASSIGN_HRESULT();
DxcEtw_DxcValidation_Stop(SUCCEEDED(hr) ? validationStatus : hr);
return hr;
}
uint32_t hlsl::getValidationVersion(unsigned *Major, unsigned *Minor) {
if (Major == nullptr || Minor == nullptr)
return E_INVALIDARG;
hlsl::GetValidationVersion(Major, Minor);
return S_OK;
}
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/d3dcomp/d3dcomp.cpp
|
///////////////////////////////////////////////////////////////////////////////
// //
// d3dcomp.cpp //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Provides functions to bridge from d3dcompiler_47 to dxcompiler. //
// //
///////////////////////////////////////////////////////////////////////////////
#include "dxc/DxilContainer/DxilContainer.h"
#include "dxc/Support/Global.h"
#include "dxc/Support/WinIncludes.h"
#include "dxc/dxcapi.h"
#include <d3dcompiler.h>
#include <string>
#include <vector>
HRESULT CreateLibrary(IDxcLibrary **pLibrary) {
return DxcCreateInstance(CLSID_DxcLibrary, __uuidof(IDxcLibrary),
(void **)pLibrary);
}
HRESULT CreateCompiler(IDxcCompiler **ppCompiler) {
return DxcCreateInstance(CLSID_DxcCompiler, __uuidof(IDxcCompiler),
(void **)ppCompiler);
}
HRESULT CreateContainerReflection(IDxcContainerReflection **ppReflection) {
return DxcCreateInstance(CLSID_DxcContainerReflection,
__uuidof(IDxcContainerReflection),
(void **)ppReflection);
}
HRESULT CompileFromBlob(IDxcBlobEncoding *pSource, LPCWSTR pSourceName,
const D3D_SHADER_MACRO *pDefines,
IDxcIncludeHandler *pInclude, LPCSTR pEntrypoint,
LPCSTR pTarget, UINT Flags1, UINT Flags2,
ID3DBlob **ppCode, ID3DBlob **ppErrorMsgs) {
CComPtr<IDxcCompiler> compiler;
CComPtr<IDxcOperationResult> operationResult;
HRESULT hr;
// Upconvert legacy targets
char Target[7] = "?s_6_0";
Target[6] = 0;
if (pTarget[3] < '6') {
Target[0] = pTarget[0];
pTarget = Target;
}
try {
CA2W pEntrypointW(pEntrypoint);
CA2W pTargetProfileW(pTarget);
std::vector<std::wstring> defineValues;
std::vector<DxcDefine> defines;
if (pDefines) {
CONST D3D_SHADER_MACRO *pCursor = pDefines;
// Convert to UTF-16.
while (pCursor->Name) {
defineValues.push_back(std::wstring(CA2W(pCursor->Name)));
if (pCursor->Definition)
defineValues.push_back(std::wstring(CA2W(pCursor->Definition)));
else
defineValues.push_back(std::wstring());
++pCursor;
}
// Build up array.
pCursor = pDefines;
size_t i = 0;
while (pCursor->Name) {
defines.push_back(
DxcDefine{defineValues[i++].c_str(), defineValues[i++].c_str()});
++pCursor;
}
}
std::vector<LPCWSTR> arguments;
if (Flags1 & D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY)
arguments.push_back(L"/Gec");
// /Ges Not implemented:
// if(Flags1 & D3DCOMPILE_ENABLE_STRICTNESS) arguments.push_back(L"/Ges");
if (Flags1 & D3DCOMPILE_IEEE_STRICTNESS)
arguments.push_back(L"/Gis");
if (Flags1 & D3DCOMPILE_OPTIMIZATION_LEVEL2) {
switch (Flags1 & D3DCOMPILE_OPTIMIZATION_LEVEL2) {
case D3DCOMPILE_OPTIMIZATION_LEVEL0:
arguments.push_back(L"/O0");
break;
case D3DCOMPILE_OPTIMIZATION_LEVEL2:
arguments.push_back(L"/O2");
break;
case D3DCOMPILE_OPTIMIZATION_LEVEL3:
arguments.push_back(L"/O3");
break;
}
}
// Currently, /Od turns off too many optimization passes, causing incorrect
// DXIL to be generated. Re-enable once /Od is implemented properly:
// if(Flags1 & D3DCOMPILE_SKIP_OPTIMIZATION) arguments.push_back(L"/Od");
if (Flags1 & D3DCOMPILE_DEBUG)
arguments.push_back(L"/Zi");
if (Flags1 & D3DCOMPILE_PACK_MATRIX_ROW_MAJOR)
arguments.push_back(L"/Zpr");
if (Flags1 & D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR)
arguments.push_back(L"/Zpc");
if (Flags1 & D3DCOMPILE_AVOID_FLOW_CONTROL)
arguments.push_back(L"/Gfa");
if (Flags1 & D3DCOMPILE_PREFER_FLOW_CONTROL)
arguments.push_back(L"/Gfp");
// We don't implement this:
// if(Flags1 & D3DCOMPILE_PARTIAL_PRECISION) arguments.push_back(L"/Gpp");
if (Flags1 & D3DCOMPILE_RESOURCES_MAY_ALIAS)
arguments.push_back(L"/res_may_alias");
arguments.push_back(L"-HV");
arguments.push_back(L"2016");
IFR(CreateCompiler(&compiler));
IFR(compiler->Compile(pSource, pSourceName, pEntrypointW, pTargetProfileW,
arguments.data(), (UINT)arguments.size(),
defines.data(), (UINT)defines.size(), pInclude,
&operationResult));
} catch (const std::bad_alloc &) {
return E_OUTOFMEMORY;
} catch (const CAtlException &err) {
return err.m_hr;
}
operationResult->GetStatus(&hr);
if (SUCCEEDED(hr)) {
return operationResult->GetResult((IDxcBlob **)ppCode);
} else {
if (ppErrorMsgs)
operationResult->GetErrorBuffer((IDxcBlobEncoding **)ppErrorMsgs);
return hr;
}
}
HRESULT WINAPI BridgeD3DCompile(LPCVOID pSrcData, SIZE_T SrcDataSize,
LPCSTR pSourceName,
const D3D_SHADER_MACRO *pDefines,
ID3DInclude *pInclude, LPCSTR pEntrypoint,
LPCSTR pTarget, UINT Flags1, UINT Flags2,
ID3DBlob **ppCode, ID3DBlob **ppErrorMsgs) {
CComPtr<IDxcLibrary> library;
CComPtr<IDxcBlobEncoding> source;
CComPtr<IDxcIncludeHandler> includeHandler;
*ppCode = nullptr;
if (ppErrorMsgs != nullptr)
*ppErrorMsgs = nullptr;
IFR(CreateLibrary(&library));
IFR(library->CreateBlobWithEncodingFromPinned(pSrcData, SrcDataSize, CP_ACP,
&source));
// Until we actually wrap the include handler, fail if there's a user-supplied
// handler.
if (D3D_COMPILE_STANDARD_FILE_INCLUDE == pInclude) {
IFR(library->CreateIncludeHandler(&includeHandler));
} else if (pInclude) {
return E_INVALIDARG;
}
try {
CA2W pFileName(pSourceName);
return CompileFromBlob(source, pFileName, pDefines, includeHandler,
pEntrypoint, pTarget, Flags1, Flags2, ppCode,
ppErrorMsgs);
} catch (const std::bad_alloc &) {
return E_OUTOFMEMORY;
} catch (const CAtlException &err) {
return err.m_hr;
}
}
HRESULT WINAPI BridgeD3DCompile2(
LPCVOID pSrcData, SIZE_T SrcDataSize, LPCSTR pSourceName,
const D3D_SHADER_MACRO *pDefines, ID3DInclude *pInclude, LPCSTR pEntrypoint,
LPCSTR pTarget, UINT Flags1, UINT Flags2, UINT SecondaryDataFlags,
LPCVOID pSecondaryData, SIZE_T SecondaryDataSize, ID3DBlob **ppCode,
ID3DBlob **ppErrorMsgs) {
if (SecondaryDataFlags == 0 || pSecondaryData == nullptr) {
return BridgeD3DCompile(pSrcData, SrcDataSize, pSourceName, pDefines,
pInclude, pEntrypoint, pTarget, Flags1, Flags2,
ppCode, ppErrorMsgs);
}
return E_NOTIMPL;
}
HRESULT WINAPI BridgeD3DCompileFromFile(
LPCWSTR pFileName, const D3D_SHADER_MACRO *pDefines, ID3DInclude *pInclude,
LPCSTR pEntrypoint, LPCSTR pTarget, UINT Flags1, UINT Flags2,
ID3DBlob **ppCode, ID3DBlob **ppErrorMsgs) {
CComPtr<IDxcLibrary> library;
CComPtr<IDxcBlobEncoding> source;
CComPtr<IDxcIncludeHandler> includeHandler;
HRESULT hr;
*ppCode = nullptr;
if (ppErrorMsgs != nullptr)
*ppErrorMsgs = nullptr;
hr = CreateLibrary(&library);
if (FAILED(hr))
return hr;
hr = library->CreateBlobFromFile(pFileName, nullptr, &source);
if (FAILED(hr))
return hr;
// Until we actually wrap the include handler, fail if there's a user-supplied
// handler.
if (D3D_COMPILE_STANDARD_FILE_INCLUDE == pInclude) {
IFT(library->CreateIncludeHandler(&includeHandler));
} else if (pInclude) {
return E_INVALIDARG;
}
return CompileFromBlob(source, pFileName, pDefines, includeHandler,
pEntrypoint, pTarget, Flags1, Flags2, ppCode,
ppErrorMsgs);
}
HRESULT WINAPI BridgeD3DDisassemble(LPCVOID pSrcData, SIZE_T SrcDataSize,
UINT Flags, LPCSTR szComments,
ID3DBlob **ppDisassembly) {
CComPtr<IDxcLibrary> library;
CComPtr<IDxcCompiler> compiler;
CComPtr<IDxcBlobEncoding> source;
CComPtr<IDxcBlobEncoding> disassemblyText;
*ppDisassembly = nullptr;
UNREFERENCED_PARAMETER(szComments);
UNREFERENCED_PARAMETER(Flags);
IFR(CreateLibrary(&library));
IFR(library->CreateBlobWithEncodingFromPinned(pSrcData, SrcDataSize, CP_ACP,
&source));
IFR(CreateCompiler(&compiler));
IFR(compiler->Disassemble(source, &disassemblyText));
IFR(disassemblyText.QueryInterface(ppDisassembly));
return S_OK;
}
HRESULT WINAPI BridgeD3DReflect(LPCVOID pSrcData, SIZE_T SrcDataSize,
REFIID pInterface, void **ppReflector) {
CComPtr<IDxcLibrary> library;
CComPtr<IDxcBlobEncoding> source;
CComPtr<IDxcContainerReflection> reflection;
UINT shaderIdx;
*ppReflector = nullptr;
IFR(CreateLibrary(&library));
IFR(library->CreateBlobWithEncodingOnHeapCopy(pSrcData, SrcDataSize, CP_ACP,
&source));
IFR(CreateContainerReflection(&reflection));
IFR(reflection->Load(source));
IFR(reflection->FindFirstPartKind(hlsl::DFCC_DXIL, &shaderIdx));
IFR(reflection->GetPartReflection(shaderIdx, pInterface,
(void **)ppReflector));
return S_OK;
}
HRESULT WINAPI BridgeReadFileToBlob(LPCWSTR pFileName, ID3DBlob **ppContents) {
if (!ppContents)
return E_INVALIDARG;
*ppContents = nullptr;
CComPtr<IDxcLibrary> library;
IFR(CreateLibrary(&library));
IFR(library->CreateBlobFromFile(pFileName, CP_ACP,
(IDxcBlobEncoding **)ppContents));
return S_OK;
}
HRESULT PreprocessFromBlob(IDxcBlobEncoding *pSource, LPCWSTR pSourceName,
const D3D_SHADER_MACRO *pDefines,
IDxcIncludeHandler *pInclude, ID3DBlob **ppCodeText,
ID3DBlob **ppErrorMsgs) {
CComPtr<IDxcCompiler> compiler;
CComPtr<IDxcOperationResult> operationResult;
HRESULT hr;
try {
std::vector<std::wstring> defineValues;
std::vector<DxcDefine> defines;
if (pDefines) {
CONST D3D_SHADER_MACRO *pCursor = pDefines;
// Convert to UTF-16.
while (pCursor->Name) {
defineValues.push_back(std::wstring(CA2W(pCursor->Name)));
if (pCursor->Definition)
defineValues.push_back(std::wstring(CA2W(pCursor->Definition)));
else
defineValues.push_back(std::wstring());
++pCursor;
}
// Build up array.
pCursor = pDefines;
size_t i = 0;
while (pCursor->Name) {
defines.push_back(
DxcDefine{defineValues[i++].c_str(), defineValues[i++].c_str()});
++pCursor;
}
}
std::vector<LPCWSTR> arguments;
IFR(CreateCompiler(&compiler));
IFR(compiler->Preprocess(pSource, pSourceName, arguments.data(),
(UINT)arguments.size(), defines.data(),
(UINT)defines.size(), pInclude, &operationResult));
} catch (const std::bad_alloc &) {
return E_OUTOFMEMORY;
} catch (const CAtlException &err) {
return err.m_hr;
}
operationResult->GetStatus(&hr);
if (SUCCEEDED(hr)) {
return operationResult->GetResult((IDxcBlob **)ppCodeText);
} else {
if (ppErrorMsgs)
operationResult->GetErrorBuffer((IDxcBlobEncoding **)ppErrorMsgs);
return hr;
}
}
HRESULT WINAPI BridgeD3DPreprocess(LPCVOID pSrcData, SIZE_T SrcDataSize,
LPCSTR pSourceName,
const D3D_SHADER_MACRO *pDefines,
ID3DInclude *pInclude, ID3DBlob **ppCodeText,
ID3DBlob **ppErrorMsgs) {
CComPtr<IDxcLibrary> library;
CComPtr<IDxcBlobEncoding> source;
CComPtr<IDxcIncludeHandler> includeHandler;
*ppCodeText = nullptr;
if (ppErrorMsgs != nullptr)
*ppErrorMsgs = nullptr;
IFR(CreateLibrary(&library));
IFR(library->CreateBlobWithEncodingFromPinned(pSrcData, SrcDataSize, CP_ACP,
&source));
// Until we actually wrap the include handler, fail if there's a user-supplied
// handler.
if (D3D_COMPILE_STANDARD_FILE_INCLUDE == pInclude) {
IFR(library->CreateIncludeHandler(&includeHandler));
} else if (pInclude) {
return E_INVALIDARG;
}
try {
CA2W pFileName(pSourceName);
return PreprocessFromBlob(source, pFileName, pDefines, includeHandler,
ppCodeText, ppErrorMsgs);
} catch (const std::bad_alloc &) {
return E_OUTOFMEMORY;
} catch (const CAtlException &err) {
return err.m_hr;
}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID) { return TRUE; }
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/d3dcomp/d3dcomp.def
|
LIBRARY d3dcompiler_dxc_bridge
EXPORTS
D3DCompile=BridgeD3DCompile
D3DCompile2=BridgeD3DCompile2
D3DCompileFromFile=BridgeD3DCompileFromFile
D3DDisassemble=BridgeD3DDisassemble
D3DReflect=BridgeD3DReflect
D3DReadFileToBlob=BridgeReadFileToBlob
D3DPreprocess=BridgeD3DPreprocess
|
0 |
repos/DirectXShaderCompiler/tools/clang/tools
|
repos/DirectXShaderCompiler/tools/clang/tools/d3dcomp/CMakeLists.txt
|
# Copyright (C) Microsoft Corporation. All rights reserved.
# This file is distributed under the University of Illinois Open Source License. See LICENSE.TXT for details.
# Build a d3dcompiler_dxc_bridge.dll component that bridges to dxcompiler.dll
set(SOURCES
d3dcomp.cpp
d3dcomp.def
)
add_clang_library(d3dcompiler_dxc_bridge SHARED ${SOURCES})
target_link_libraries(d3dcompiler_dxc_bridge PRIVATE dxcompiler)
add_dependencies(d3dcompiler_dxc_bridge dxcompiler)
set_target_properties(d3dcompiler_dxc_bridge
PROPERTIES
OUTPUT_NAME "d3dcompiler_dxc_bridge"
VERSION ${LIBCLANG_LIBRARY_VERSION})
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/RemoteTargetMessage.h
|
//===---- RemoteTargetMessage.h - LLI out-of-process message protocol -----===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Definition of the LLIMessageType enum which is used for communication with a
// child process for remote execution.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLI_REMOTETARGETMESSAGE_H
#define LLVM_TOOLS_LLI_REMOTETARGETMESSAGE_H
namespace llvm {
// LLI messages from parent-to-child or vice versa follow an exceedingly simple
// protocol where the first four bytes represent the message type, the next
// four bytes represent the size of data for the command and following bytes
// represent the actual data.
//
// The protocol is not intended to be robust, secure or fault-tolerant. It is
// only here for testing purposes and is therefore intended to be the simplest
// implementation that will work. It is assumed that the parent and child
// share characteristics like endianness.
//
// Quick description of the protocol:
//
// { Header + Payload Size + Payload }
//
// The protocol message consist of a header, the payload size (which can be
// zero), and the payload itself. The payload can contain any number of items,
// and the size has to be the sum of them all. Each end is responsible for
// reading/writing the correct number of items with the correct sizes.
//
// The current four known exchanges are:
//
// * Allocate Space:
// Parent: { LLI_AllocateSpace, 8, Alignment, Size }
// Child: { LLI_AllocationResult, 8, Address }
//
// * Load Data:
// Parent: { LLI_LoadDataSection, 8+Size, Address, Data }
// Child: { LLI_LoadComplete, 4, StatusCode }
//
// * Load Code:
// Parent: { LLI_LoadCodeSection, 8+Size, Address, Code }
// Child: { LLI_LoadComplete, 4, StatusCode }
//
// * Execute Code:
// Parent: { LLI_Execute, 8, Address }
// Child: { LLI_ExecutionResult, 4, Result }
//
// It is the responsibility of either side to check for correct headers,
// sizes and payloads, since any inconsistency would misalign the pipe, and
// result in data corruption.
enum LLIMessageType {
LLI_Error = -1,
LLI_ChildActive = 0, // Data = not used
LLI_AllocateSpace, // Data = struct { uint32_t Align, uint_32t Size }
LLI_AllocationResult, // Data = uint64_t Address (child memory space)
LLI_LoadCodeSection, // Data = uint64_t Address, void * SectionData
LLI_LoadDataSection, // Data = uint64_t Address, void * SectionData
LLI_LoadResult, // Data = uint32_t LLIMessageStatus
LLI_Execute, // Data = uint64_t Address
LLI_ExecutionResult, // Data = uint32_t Result
LLI_Terminate // Data = not used
};
enum LLIMessageStatus {
LLI_Status_Success = 0, // Operation succeeded
LLI_Status_NotAllocated, // Address+Size not allocated in child space
LLI_Status_IncompleteMsg // Size received doesn't match request
};
} // end namespace llvm
#endif
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/OrcLazyJIT.cpp
|
//===------ OrcLazyJIT.cpp - Basic Orc-based JIT for lazy execution -------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "OrcLazyJIT.h"
#include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DynamicLibrary.h"
#include <cstdio>
#include <system_error>
using namespace llvm;
namespace {
enum class DumpKind { NoDump, DumpFuncsToStdOut, DumpModsToStdErr,
DumpModsToDisk };
cl::opt<DumpKind> OrcDumpKind("orc-lazy-debug",
cl::desc("Debug dumping for the orc-lazy JIT."),
cl::init(DumpKind::NoDump),
cl::values(
clEnumValN(DumpKind::NoDump, "no-dump",
"Don't dump anything."),
clEnumValN(DumpKind::DumpFuncsToStdOut,
"funcs-to-stdout",
"Dump function names to stdout."),
clEnumValN(DumpKind::DumpModsToStdErr,
"mods-to-stderr",
"Dump modules to stderr."),
clEnumValN(DumpKind::DumpModsToDisk,
"mods-to-disk",
"Dump modules to the current "
"working directory. (WARNING: "
"will overwrite existing files)."),
clEnumValEnd));
}
OrcLazyJIT::CallbackManagerBuilder
OrcLazyJIT::createCallbackManagerBuilder(Triple T) {
switch (T.getArch()) {
default: return nullptr;
case Triple::x86_64: {
typedef orc::JITCompileCallbackManager<IRDumpLayerT,
orc::OrcX86_64> CCMgrT;
return [](IRDumpLayerT &IRDumpLayer, RuntimeDyld::MemoryManager &MemMgr,
LLVMContext &Context) {
return llvm::make_unique<CCMgrT>(IRDumpLayer, MemMgr, Context, 0,
64);
};
}
}
}
OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
switch (OrcDumpKind) {
case DumpKind::NoDump:
return [](std::unique_ptr<Module> M) { return M; };
case DumpKind::DumpFuncsToStdOut:
return [](std::unique_ptr<Module> M) {
printf("[ ");
for (const auto &F : *M) {
if (F.isDeclaration())
continue;
if (F.hasName()) {
std::string Name(F.getName());
printf("%s ", Name.c_str());
} else
printf("<anon> ");
}
printf("]\n");
return M;
};
case DumpKind::DumpModsToStdErr:
return [](std::unique_ptr<Module> M) {
dbgs() << "----- Module Start -----\n" << *M
<< "----- Module End -----\n";
return M;
};
case DumpKind::DumpModsToDisk:
return [](std::unique_ptr<Module> M) {
std::error_code EC;
raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
sys::fs::F_Text);
if (EC) {
errs() << "Couldn't open " << M->getModuleIdentifier()
<< " for dumping.\nError:" << EC.message() << "\n";
exit(1);
}
Out << *M;
return M;
};
}
llvm_unreachable("Unknown DumpKind");
}
// Defined in lli.cpp.
CodeGenOpt::Level getOptLevel();
int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
// Add the program's symbols into the JIT's search space.
if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
errs() << "Error loading program symbols.\n";
return 1;
}
// Grab a target machine and try to build a factory function for the
// target-specific Orc callback manager.
EngineBuilder EB;
EB.setOptLevel(getOptLevel());
auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
auto &Context = getGlobalContext();
auto CallbackMgrBuilder =
OrcLazyJIT::createCallbackManagerBuilder(Triple(TM->getTargetTriple()));
// If we couldn't build the factory function then there must not be a callback
// manager for this target. Bail out.
if (!CallbackMgrBuilder) {
errs() << "No callback manager available for target '"
<< TM->getTargetTriple().str() << "'.\n";
return 1;
}
// Everything looks good. Build the JIT.
OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder);
// Add the module, look up main and run it.
auto MainHandle = J.addModule(std::move(M));
auto MainSym = J.findSymbolIn(MainHandle, "main");
if (!MainSym) {
errs() << "Could not find main function.\n";
return 1;
}
typedef int (*MainFnPtr)(int, char*[]);
auto Main = OrcLazyJIT::fromTargetAddress<MainFnPtr>(MainSym.getAddress());
return Main(ArgC, ArgV);
}
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/RemoteTarget.cpp
|
//===- RemoteTarget.cpp - LLVM Remote process JIT execution --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Implementation of the RemoteTarget class which executes JITed code in a
// separate address range from where it was built.
//
//===----------------------------------------------------------------------===//
#include "RemoteTarget.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Memory.h"
#include <stdlib.h>
#include <string>
using namespace llvm;
////////////////////////////////////////////////////////////////////////////////
// Simulated remote execution
//
// This implementation will simply move generated code and data to a new memory
// location in the current executable and let it run from there.
////////////////////////////////////////////////////////////////////////////////
bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment,
uint64_t &Address) {
sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : nullptr;
sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg);
if (Mem.base() == nullptr)
return false;
if ((uintptr_t)Mem.base() % Alignment) {
ErrorMsg = "unable to allocate sufficiently aligned memory";
return false;
}
Address = reinterpret_cast<uint64_t>(Mem.base());
Allocations.push_back(Mem);
return true;
}
bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) {
memcpy ((void*)Address, Data, Size);
return true;
}
bool RemoteTarget::loadCode(uint64_t Address, const void *Data, size_t Size) {
memcpy ((void*)Address, Data, Size);
sys::MemoryBlock Mem((void*)Address, Size);
sys::Memory::setExecutable(Mem, &ErrorMsg);
return true;
}
bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) {
int (*fn)(void) = (int(*)(void))Address;
RetVal = fn();
return true;
}
bool RemoteTarget::create() {
return true;
}
void RemoteTarget::stop() {
for (unsigned i = 0, e = Allocations.size(); i != e; ++i)
sys::Memory::ReleaseRWX(Allocations[i]);
}
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/RPCChannel.h
|
//===---------- RPCChannel.h - LLVM out-of-process JIT execution ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Definition of the RemoteTargetExternal class which executes JITed code in a
// separate process from where it was built.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLI_RPCCHANNEL_H
#define LLVM_TOOLS_LLI_RPCCHANNEL_H
#include <stdlib.h>
#include <string>
namespace llvm {
class RPCChannel {
public:
std::string ChildName;
RPCChannel() {}
~RPCChannel();
/// Start the remote process.
///
/// @returns True on success. On failure, ErrorMsg is updated with
/// descriptive text of the encountered error.
bool createServer();
bool createClient();
// This will get filled in as a point to an OS-specific structure.
void *ConnectionData;
bool WriteBytes(const void *Data, size_t Size);
bool ReadBytes(void *Data, size_t Size);
void Wait();
};
} // end namespace llvm
#endif
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/RemoteTargetExternal.cpp
|
//===---- RemoteTargetExternal.cpp - LLVM out-of-process JIT execution ----===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Implementation of the RemoteTargetExternal class which executes JITed code
// in a separate process from where it was built.
//
//===----------------------------------------------------------------------===//
#include "llvm/Config/config.h"
#include "RemoteTarget.h"
#include "RemoteTargetExternal.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include <string>
using namespace llvm;
#define DEBUG_TYPE "lli"
bool RemoteTargetExternal::allocateSpace(size_t Size, unsigned Alignment,
uint64_t &Address) {
DEBUG(dbgs() << "Message [allocate space] size: " << Size <<
", align: " << Alignment << "\n");
if (!SendAllocateSpace(Alignment, Size)) {
ErrorMsg += ", (RemoteTargetExternal::allocateSpace)";
return false;
}
if (!Receive(LLI_AllocationResult, Address)) {
ErrorMsg += ", (RemoteTargetExternal::allocateSpace)";
return false;
}
if (Address == 0) {
ErrorMsg += "failed allocation, (RemoteTargetExternal::allocateSpace)";
return false;
}
DEBUG(dbgs() << "Message [allocate space] addr: 0x" <<
format("%llx", Address) << "\n");
return true;
}
bool RemoteTargetExternal::loadData(uint64_t Address, const void *Data, size_t Size) {
DEBUG(dbgs() << "Message [load data] addr: 0x" << format("%llx", Address) <<
", size: " << Size << "\n");
if (!SendLoadSection(Address, Data, (uint32_t)Size, false)) {
ErrorMsg += ", (RemoteTargetExternal::loadData)";
return false;
}
int Status = LLI_Status_Success;
if (!Receive(LLI_LoadResult, Status)) {
ErrorMsg += ", (RemoteTargetExternal::loadData)";
return false;
}
if (Status == LLI_Status_IncompleteMsg) {
ErrorMsg += "incomplete load data, (RemoteTargetExternal::loadData)";
return false;
}
if (Status == LLI_Status_NotAllocated) {
ErrorMsg += "data memory not allocated, (RemoteTargetExternal::loadData)";
return false;
}
DEBUG(dbgs() << "Message [load data] complete\n");
return true;
}
bool RemoteTargetExternal::loadCode(uint64_t Address, const void *Data, size_t Size) {
DEBUG(dbgs() << "Message [load code] addr: 0x" << format("%llx", Address) <<
", size: " << Size << "\n");
if (!SendLoadSection(Address, Data, (uint32_t)Size, true)) {
ErrorMsg += ", (RemoteTargetExternal::loadCode)";
return false;
}
int Status = LLI_Status_Success;
if (!Receive(LLI_LoadResult, Status)) {
ErrorMsg += ", (RemoteTargetExternal::loadCode)";
return false;
}
if (Status == LLI_Status_IncompleteMsg) {
ErrorMsg += "incomplete load data, (RemoteTargetExternal::loadData)";
return false;
}
if (Status == LLI_Status_NotAllocated) {
ErrorMsg += "data memory not allocated, (RemoteTargetExternal::loadData)";
return false;
}
DEBUG(dbgs() << "Message [load code] complete\n");
return true;
}
bool RemoteTargetExternal::executeCode(uint64_t Address, int32_t &RetVal) {
DEBUG(dbgs() << "Message [exectue code] addr: " << Address << "\n");
if (!SendExecute(Address)) {
ErrorMsg += ", (RemoteTargetExternal::executeCode)";
return false;
}
if (!Receive(LLI_ExecutionResult, RetVal)) {
ErrorMsg += ", (RemoteTargetExternal::executeCode)";
return false;
}
DEBUG(dbgs() << "Message [exectue code] return: " << RetVal << "\n");
return true;
}
void RemoteTargetExternal::stop() {
SendTerminate();
RPC.Wait();
}
bool RemoteTargetExternal::SendAllocateSpace(uint32_t Alignment, uint32_t Size) {
if (!SendHeader(LLI_AllocateSpace)) {
ErrorMsg += ", (RemoteTargetExternal::SendAllocateSpace)";
return false;
}
AppendWrite((const void *)&Alignment, 4);
AppendWrite((const void *)&Size, 4);
if (!SendPayload()) {
ErrorMsg += ", (RemoteTargetExternal::SendAllocateSpace)";
return false;
}
return true;
}
bool RemoteTargetExternal::SendLoadSection(uint64_t Addr,
const void *Data,
uint32_t Size,
bool IsCode) {
LLIMessageType MsgType = IsCode ? LLI_LoadCodeSection : LLI_LoadDataSection;
if (!SendHeader(MsgType)) {
ErrorMsg += ", (RemoteTargetExternal::SendLoadSection)";
return false;
}
AppendWrite((const void *)&Addr, 8);
AppendWrite(Data, Size);
if (!SendPayload()) {
ErrorMsg += ", (RemoteTargetExternal::SendLoadSection)";
return false;
}
return true;
}
bool RemoteTargetExternal::SendExecute(uint64_t Addr) {
if (!SendHeader(LLI_Execute)) {
ErrorMsg += ", (RemoteTargetExternal::SendExecute)";
return false;
}
AppendWrite((const void *)&Addr, 8);
if (!SendPayload()) {
ErrorMsg += ", (RemoteTargetExternal::SendExecute)";
return false;
}
return true;
}
bool RemoteTargetExternal::SendTerminate() {
return SendHeader(LLI_Terminate);
// No data or data size is sent with Terminate
}
bool RemoteTargetExternal::Receive(LLIMessageType Msg) {
if (!ReceiveHeader(Msg))
return false;
int Unused;
AppendRead(&Unused, 0);
if (!ReceivePayload())
return false;
ReceiveData.clear();
Sizes.clear();
return true;
}
bool RemoteTargetExternal::Receive(LLIMessageType Msg, int32_t &Data) {
if (!ReceiveHeader(Msg))
return false;
AppendRead(&Data, 4);
if (!ReceivePayload())
return false;
ReceiveData.clear();
Sizes.clear();
return true;
}
bool RemoteTargetExternal::Receive(LLIMessageType Msg, uint64_t &Data) {
if (!ReceiveHeader(Msg))
return false;
AppendRead(&Data, 8);
if (!ReceivePayload())
return false;
ReceiveData.clear();
Sizes.clear();
return true;
}
bool RemoteTargetExternal::ReceiveHeader(LLIMessageType ExpectedMsgType) {
assert(ReceiveData.empty() && Sizes.empty() &&
"Payload vector not empty to receive header");
// Message header, with type to follow
uint32_t MsgType;
if (!ReadBytes(&MsgType, 4)) {
ErrorMsg += ", (RemoteTargetExternal::ReceiveHeader)";
return false;
}
if (MsgType != (uint32_t)ExpectedMsgType) {
ErrorMsg = "received unexpected message type";
ErrorMsg += ". Expecting: ";
ErrorMsg += ExpectedMsgType;
ErrorMsg += ", Got: ";
ErrorMsg += MsgType;
return false;
}
return true;
}
bool RemoteTargetExternal::ReceivePayload() {
assert(!ReceiveData.empty() &&
"Payload vector empty to receive");
assert(ReceiveData.size() == Sizes.size() &&
"Unexpected mismatch between data and size");
uint32_t TotalSize = 0;
for (int I=0, E=Sizes.size(); I < E; I++)
TotalSize += Sizes[I];
// Payload size header
uint32_t DataSize;
if (!ReadBytes(&DataSize, 4)) {
ErrorMsg += ", invalid data size";
return false;
}
if (DataSize != TotalSize) {
ErrorMsg = "unexpected data size";
ErrorMsg += ". Expecting: ";
ErrorMsg += TotalSize;
ErrorMsg += ", Got: ";
ErrorMsg += DataSize;
return false;
}
if (DataSize == 0)
return true;
// Payload itself
for (int I=0, E=Sizes.size(); I < E; I++) {
if (!ReadBytes(ReceiveData[I], Sizes[I])) {
ErrorMsg = "unexpected data while reading message";
return false;
}
}
return true;
}
bool RemoteTargetExternal::SendHeader(LLIMessageType MsgType) {
assert(SendData.empty() && Sizes.empty() &&
"Payload vector not empty to send header");
// Message header, with type to follow
if (!WriteBytes(&MsgType, 4)) {
ErrorMsg += ", (RemoteTargetExternal::SendHeader)";
return false;
}
return true;
}
bool RemoteTargetExternal::SendPayload() {
assert(!SendData.empty() && !Sizes.empty() &&
"Payload vector empty to send");
assert(SendData.size() == Sizes.size() &&
"Unexpected mismatch between data and size");
uint32_t TotalSize = 0;
for (int I=0, E=Sizes.size(); I < E; I++)
TotalSize += Sizes[I];
// Payload size header
if (!WriteBytes(&TotalSize, 4)) {
ErrorMsg += ", invalid data size";
return false;
}
if (TotalSize == 0)
return true;
// Payload itself
for (int I=0, E=Sizes.size(); I < E; I++) {
if (!WriteBytes(SendData[I], Sizes[I])) {
ErrorMsg = "unexpected data while writing message";
return false;
}
}
SendData.clear();
Sizes.clear();
return true;
}
void RemoteTargetExternal::AppendWrite(const void *Data, uint32_t Size) {
SendData.push_back(Data);
Sizes.push_back(Size);
}
void RemoteTargetExternal::AppendRead(void *Data, uint32_t Size) {
ReceiveData.push_back(Data);
Sizes.push_back(Size);
}
#ifdef LLVM_ON_UNIX
#include "Unix/RPCChannel.inc"
#endif
#ifdef LLVM_ON_WIN32
#include "Windows/RPCChannel.inc"
#endif
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/OrcLazyJIT.h
|
//===--- OrcLazyJIT.h - Basic Orc-based JIT for lazy execution --*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Simple Orc-based JIT. Uses the compile-on-demand layer to break up and
// lazily compile modules.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLI_ORCLAZYJIT_H
#define LLVM_TOOLS_LLI_ORCLAZYJIT_H
#include "llvm/ADT/Triple.h"
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
#include "llvm/IR/LLVMContext.h"
namespace llvm {
class OrcLazyJIT {
public:
typedef orc::JITCompileCallbackManagerBase CompileCallbackMgr;
typedef orc::ObjectLinkingLayer<> ObjLayerT;
typedef orc::IRCompileLayer<ObjLayerT> CompileLayerT;
typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)>
TransformFtor;
typedef orc::IRTransformLayer<CompileLayerT, TransformFtor> IRDumpLayerT;
typedef orc::CompileOnDemandLayer<IRDumpLayerT, CompileCallbackMgr> CODLayerT;
typedef CODLayerT::ModuleSetHandleT ModuleHandleT;
typedef std::function<
std::unique_ptr<CompileCallbackMgr>(IRDumpLayerT&,
RuntimeDyld::MemoryManager&,
LLVMContext&)>
CallbackManagerBuilder;
static CallbackManagerBuilder createCallbackManagerBuilder(Triple T);
OrcLazyJIT(std::unique_ptr<TargetMachine> TM, LLVMContext &Context,
CallbackManagerBuilder &BuildCallbackMgr)
: TM(std::move(TM)),
ObjectLayer(),
CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)),
IRDumpLayer(CompileLayer, createDebugDumper()),
CCMgr(BuildCallbackMgr(IRDumpLayer, CCMgrMemMgr, Context)),
CODLayer(IRDumpLayer, *CCMgr, false),
CXXRuntimeOverrides([this](const std::string &S) { return mangle(S); }) {}
~OrcLazyJIT() {
// Run any destructors registered with __cxa_atexit.
CXXRuntimeOverrides.runDestructors();
// Run any IR destructors.
for (auto &DtorRunner : IRStaticDestructorRunners)
DtorRunner.runViaLayer(CODLayer);
}
template <typename PtrTy>
static PtrTy fromTargetAddress(orc::TargetAddress Addr) {
return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
}
ModuleHandleT addModule(std::unique_ptr<Module> M) {
// Attach a data-layout if one isn't already present.
if (M->getDataLayout().isDefault())
M->setDataLayout(*TM->getDataLayout());
// Record the static constructors and destructors. We have to do this before
// we hand over ownership of the module to the JIT.
std::vector<std::string> CtorNames, DtorNames;
for (auto Ctor : orc::getConstructors(*M))
CtorNames.push_back(mangle(Ctor.Func->getName()));
for (auto Dtor : orc::getDestructors(*M))
DtorNames.push_back(mangle(Dtor.Func->getName()));
// Symbol resolution order:
// 1) Search the JIT symbols.
// 2) Check for C++ runtime overrides.
// 3) Search the host process (LLI)'s symbol table.
std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver =
orc::createLambdaResolver(
[this](const std::string &Name) {
if (auto Sym = CODLayer.findSymbol(Name, true))
return RuntimeDyld::SymbolInfo(Sym.getAddress(),
Sym.getFlags());
if (auto Sym = CXXRuntimeOverrides.searchOverrides(Name))
return Sym;
if (auto Addr =
RTDyldMemoryManager::getSymbolAddressInProcess(Name))
return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported);
return RuntimeDyld::SymbolInfo(nullptr);
},
[](const std::string &Name) {
return RuntimeDyld::SymbolInfo(nullptr);
}
);
// Add the module to the JIT.
std::vector<std::unique_ptr<Module>> S;
S.push_back(std::move(M));
auto H = CODLayer.addModuleSet(std::move(S), nullptr, std::move(Resolver));
// Run the static constructors, and save the static destructor runner for
// execution when the JIT is torn down.
orc::CtorDtorRunner<CODLayerT> CtorRunner(std::move(CtorNames), H);
CtorRunner.runViaLayer(CODLayer);
IRStaticDestructorRunners.emplace_back(std::move(DtorNames), H);
return H;
}
orc::JITSymbol findSymbol(const std::string &Name) {
return CODLayer.findSymbol(mangle(Name), true);
}
orc::JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {
return CODLayer.findSymbolIn(H, mangle(Name), true);
}
private:
std::string mangle(const std::string &Name) {
std::string MangledName;
{
raw_string_ostream MangledNameStream(MangledName);
Mangler::getNameWithPrefix(MangledNameStream, Name, *TM->getDataLayout());
}
return MangledName;
}
static TransformFtor createDebugDumper();
std::unique_ptr<TargetMachine> TM;
SectionMemoryManager CCMgrMemMgr;
ObjLayerT ObjectLayer;
CompileLayerT CompileLayer;
IRDumpLayerT IRDumpLayer;
std::unique_ptr<CompileCallbackMgr> CCMgr;
CODLayerT CODLayer;
orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
std::vector<orc::CtorDtorRunner<CODLayerT>> IRStaticDestructorRunners;
};
int runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]);
} // end namespace llvm
#endif
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/CMakeLists.txt
|
add_subdirectory(ChildTarget)
set(LLVM_LINK_COMPONENTS
CodeGen
Core
ExecutionEngine
IRReader
Instrumentation
Interpreter
# MC # HLSL Change
# MCJIT # HLSL Change
Object
OrcJIT
RuntimeDyld
SelectionDAG
Support
Target
TransformUtils
native
)
if( LLVM_USE_OPROFILE )
set(LLVM_LINK_COMPONENTS
${LLVM_LINK_COMPONENTS}
OProfileJIT
)
endif( LLVM_USE_OPROFILE )
if( LLVM_USE_INTEL_JITEVENTS )
set(LLVM_LINK_COMPONENTS
${LLVM_LINK_COMPONENTS}
DebugInfoDWARF
IntelJITEvents
Object
)
endif( LLVM_USE_INTEL_JITEVENTS )
add_llvm_tool(lli
lli.cpp
OrcLazyJIT.cpp
RemoteMemoryManager.cpp
RemoteTarget.cpp
RemoteTargetExternal.cpp
)
export_executable_symbols(lli)
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/lli.cpp
|
//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This utility provides a simple wrapper around the LLVM Execution Engines,
// which allow the direct execution of LLVM programs through a Just-In-Time
// compiler, or through an interpreter if no JIT is available for this platform.
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/LLVMContext.h"
#include "OrcLazyJIT.h"
#include "RemoteMemoryManager.h"
#include "RemoteTarget.h"
#include "RemoteTargetExternal.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/CodeGen/LinkAllCodegenComponents.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/ExecutionEngine/JITEventListener.h"
// #include "llvm/ExecutionEngine/MCJIT.h" // HLSL Change
#include "llvm/ExecutionEngine/ObjectCache.h"
#include "llvm/ExecutionEngine/OrcMCJITReplacement.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
// #include "llvm/ExecutionEngine/SectionMemoryManager.h" // HLSL Change
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/TypeBuilder.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Instrumentation.h"
#include <cerrno>
#ifdef __CYGWIN__
#include <cygwin/version.h>
#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
#define DO_NOTHING_ATEXIT 1
#endif
#endif
using namespace llvm;
#define DEBUG_TYPE "lli"
namespace {
enum class JITKind { MCJIT, OrcMCJITReplacement, OrcLazy };
cl::opt<std::string>
InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
cl::list<std::string>
InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
cl::opt<bool> ForceInterpreter("force-interpreter",
cl::desc("Force interpretation: disable JIT"),
cl::init(false));
cl::opt<JITKind> UseJITKind("jit-kind",
cl::desc("Choose underlying JIT kind."),
cl::init(JITKind::MCJIT),
cl::values(
clEnumValN(JITKind::MCJIT, "mcjit",
"MCJIT"),
clEnumValN(JITKind::OrcMCJITReplacement,
"orc-mcjit",
"Orc-based MCJIT replacement"),
clEnumValN(JITKind::OrcLazy,
"orc-lazy",
"Orc-based lazy JIT."),
clEnumValEnd));
// The MCJIT supports building for a target address space separate from
// the JIT compilation process. Use a forked process and a copying
// memory manager with IPC to execute using this functionality.
cl::opt<bool> RemoteMCJIT("remote-mcjit",
cl::desc("Execute MCJIT'ed code in a separate process."),
cl::init(false));
// Manually specify the child process for remote execution. This overrides
// the simulated remote execution that allocates address space for child
// execution. The child process will be executed and will communicate with
// lli via stdin/stdout pipes.
cl::opt<std::string>
ChildExecPath("mcjit-remote-process",
cl::desc("Specify the filename of the process to launch "
"for remote MCJIT execution. If none is specified,"
"\n\tremote execution will be simulated in-process."),
cl::value_desc("filename"), cl::init(""));
// Determine optimization level.
cl::opt<char>
OptLevel("O",
cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
"(default = '-O2')"),
cl::Prefix,
cl::ZeroOrMore,
cl::init(' '));
cl::opt<std::string>
TargetTriple("mtriple", cl::desc("Override target triple for module"));
cl::opt<std::string>
MArch("march",
cl::desc("Architecture to generate assembly for (see --version)"));
cl::opt<std::string>
MCPU("mcpu",
cl::desc("Target a specific cpu type (-mcpu=help for details)"),
cl::value_desc("cpu-name"),
cl::init(""));
cl::list<std::string>
MAttrs("mattr",
cl::CommaSeparated,
cl::desc("Target specific attributes (-mattr=help for details)"),
cl::value_desc("a1,+a2,-a3,..."));
cl::opt<std::string>
EntryFunc("entry-function",
cl::desc("Specify the entry function (default = 'main') "
"of the executable"),
cl::value_desc("function"),
cl::init("main"));
cl::list<std::string>
ExtraModules("extra-module",
cl::desc("Extra modules to be loaded"),
cl::value_desc("input bitcode"));
cl::list<std::string>
ExtraObjects("extra-object",
cl::desc("Extra object files to be loaded"),
cl::value_desc("input object"));
cl::list<std::string>
ExtraArchives("extra-archive",
cl::desc("Extra archive files to be loaded"),
cl::value_desc("input archive"));
cl::opt<bool>
EnableCacheManager("enable-cache-manager",
cl::desc("Use cache manager to save/load mdoules"),
cl::init(false));
cl::opt<std::string>
ObjectCacheDir("object-cache-dir",
cl::desc("Directory to store cached object files "
"(must be user writable)"),
cl::init(""));
cl::opt<std::string>
FakeArgv0("fake-argv0",
cl::desc("Override the 'argv[0]' value passed into the executing"
" program"), cl::value_desc("executable"));
cl::opt<bool>
DisableCoreFiles("disable-core-files", cl::Hidden,
cl::desc("Disable emission of core files if possible"));
cl::opt<bool>
NoLazyCompilation("disable-lazy-compilation",
cl::desc("Disable JIT lazy compilation"),
cl::init(false));
cl::opt<Reloc::Model>
RelocModel("relocation-model",
cl::desc("Choose relocation model"),
cl::init(Reloc::Default),
cl::values(
clEnumValN(Reloc::Default, "default",
"Target default relocation model"),
clEnumValN(Reloc::Static, "static",
"Non-relocatable code"),
clEnumValN(Reloc::PIC_, "pic",
"Fully relocatable, position independent code"),
clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
"Relocatable external references, non-relocatable code"),
clEnumValEnd));
cl::opt<llvm::CodeModel::Model>
CMModel("code-model",
cl::desc("Choose code model"),
cl::init(CodeModel::JITDefault),
cl::values(clEnumValN(CodeModel::JITDefault, "default",
"Target default JIT code model"),
clEnumValN(CodeModel::Small, "small",
"Small code model"),
clEnumValN(CodeModel::Kernel, "kernel",
"Kernel code model"),
clEnumValN(CodeModel::Medium, "medium",
"Medium code model"),
clEnumValN(CodeModel::Large, "large",
"Large code model"),
clEnumValEnd));
cl::opt<bool>
GenerateSoftFloatCalls("soft-float",
cl::desc("Generate software floating point library calls"),
cl::init(false));
cl::opt<llvm::FloatABI::ABIType>
FloatABIForCalls("float-abi",
cl::desc("Choose float ABI type"),
cl::init(FloatABI::Default),
cl::values(
clEnumValN(FloatABI::Default, "default",
"Target default float ABI type"),
clEnumValN(FloatABI::Soft, "soft",
"Soft float ABI (implied by -soft-float)"),
clEnumValN(FloatABI::Hard, "hard",
"Hard float ABI (uses FP registers)"),
clEnumValEnd));
}
//===----------------------------------------------------------------------===//
// Object cache
//
// This object cache implementation writes cached objects to disk to the
// directory specified by CacheDir, using a filename provided in the module
// descriptor. The cache tries to load a saved object using that path if the
// file exists. CacheDir defaults to "", in which case objects are cached
// alongside their originating bitcodes.
//
class LLIObjectCache : public ObjectCache {
public:
LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
// Add trailing '/' to cache dir if necessary.
if (!this->CacheDir.empty() &&
this->CacheDir[this->CacheDir.size() - 1] != '/')
this->CacheDir += '/';
}
~LLIObjectCache() override {}
void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
const std::string ModuleID = M->getModuleIdentifier();
std::string CacheName;
if (!getCacheFilename(ModuleID, CacheName))
return;
if (!CacheDir.empty()) { // Create user-defined cache dir.
SmallString<128> dir(CacheName);
sys::path::remove_filename(dir);
sys::fs::create_directories(Twine(dir));
}
std::error_code EC;
raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
outfile.close();
}
std::unique_ptr<MemoryBuffer> getObject(const Module* M) override {
const std::string ModuleID = M->getModuleIdentifier();
std::string CacheName;
if (!getCacheFilename(ModuleID, CacheName))
return nullptr;
// Load the object from the cache filename
ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
MemoryBuffer::getFile(CacheName.c_str(), -1, false);
// If the file isn't there, that's OK.
if (!IRObjectBuffer)
return nullptr;
// MCJIT will want to write into this buffer, and we don't want that
// because the file has probably just been mmapped. Instead we make
// a copy. The filed-based buffer will be released when it goes
// out of scope.
return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
}
private:
std::string CacheDir;
bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
std::string Prefix("file:");
size_t PrefixLength = Prefix.length();
if (ModID.substr(0, PrefixLength) != Prefix)
return false;
std::string CacheSubdir = ModID.substr(PrefixLength);
#if defined(_WIN32)
// Transform "X:\foo" => "/X\foo" for convenience.
if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
CacheSubdir[1] = CacheSubdir[0];
CacheSubdir[0] = '/';
}
#endif
CacheName = CacheDir + CacheSubdir;
size_t pos = CacheName.rfind('.');
CacheName.replace(pos, CacheName.length() - pos, ".o");
return true;
}
};
static ExecutionEngine *EE = nullptr;
static LLIObjectCache *CacheManager = nullptr;
// HLSL Change: changed calling convention to __cdecl
static void __cdecl do_shutdown() {
// Cygwin-1.5 invokes DLL's dtors before atexit handler.
#ifndef DO_NOTHING_ATEXIT
delete EE;
if (CacheManager)
delete CacheManager;
llvm_shutdown();
#endif
}
// On Mingw and Cygwin, an external symbol named '__main' is called from the
// generated 'main' function to allow static intialization. To avoid linking
// problems with remote targets (because lli's remote target support does not
// currently handle external linking) we add a secondary module which defines
// an empty '__main' function.
static void addCygMingExtraModule(ExecutionEngine *EE,
LLVMContext &Context,
StringRef TargetTripleStr) {
IRBuilder<> Builder(Context);
Triple TargetTriple(TargetTripleStr);
// Create a new module.
std::unique_ptr<Module> M = make_unique<Module>("CygMingHelper", Context);
M->setTargetTriple(TargetTripleStr);
// Create an empty function named "__main".
Function *Result;
if (TargetTriple.isArch64Bit()) {
Result = Function::Create(
TypeBuilder<int64_t(void), false>::get(Context),
GlobalValue::ExternalLinkage, "__main", M.get());
} else {
Result = Function::Create(
TypeBuilder<int32_t(void), false>::get(Context),
GlobalValue::ExternalLinkage, "__main", M.get());
}
BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
Builder.SetInsertPoint(BB);
Value *ReturnVal;
if (TargetTriple.isArch64Bit())
ReturnVal = ConstantInt::get(Context, APInt(64, 0));
else
ReturnVal = ConstantInt::get(Context, APInt(32, 0));
Builder.CreateRet(ReturnVal);
// Add this new module to the ExecutionEngine.
EE->addModule(std::move(M));
}
CodeGenOpt::Level getOptLevel() {
switch (OptLevel) {
default:
errs() << "lli: Invalid optimization level.\n";
exit(1);
case '0': return CodeGenOpt::None;
case '1': return CodeGenOpt::Less;
case ' ':
case '2': return CodeGenOpt::Default;
case '3': return CodeGenOpt::Aggressive;
}
llvm_unreachable("Unrecognized opt level.");
}
//===----------------------------------------------------------------------===//
// main Driver function
//
// HLSL Change: changed calling convention to __cdecl
int __cdecl main(int argc, char **argv, char * const *envp) {
sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram X(argc, argv);
LLVMContext &Context = getGlobalContext();
atexit(do_shutdown); // Call llvm_shutdown() on exit.
// If we have a native target, initialize it to ensure it is linked in and
// usable by the JIT.
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
cl::ParseCommandLineOptions(argc, argv,
"llvm interpreter & dynamic compiler\n");
// If the user doesn't want core files, disable them.
if (DisableCoreFiles)
sys::Process::PreventCoreFiles();
// Load the bitcode...
SMDiagnostic Err;
std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
Module *Mod = Owner.get();
if (!Mod) {
Err.print(argv[0], errs());
return 1;
}
if (UseJITKind == JITKind::OrcLazy)
return runOrcLazyJIT(std::move(Owner), argc, argv);
if (EnableCacheManager) {
std::string CacheName("file:");
CacheName.append(InputFile);
Mod->setModuleIdentifier(CacheName);
}
// If not jitting lazily, load the whole bitcode file eagerly too.
if (NoLazyCompilation) {
if (std::error_code EC = Mod->materializeAllPermanently()) {
errs() << argv[0] << ": bitcode didn't read correctly.\n";
errs() << "Reason: " << EC.message() << "\n";
exit(1);
}
}
std::string ErrorMsg;
EngineBuilder builder(std::move(Owner));
builder.setMArch(MArch);
builder.setMCPU(MCPU);
builder.setMAttrs(MAttrs);
builder.setRelocationModel(RelocModel);
builder.setCodeModel(CMModel);
builder.setErrorStr(&ErrorMsg);
builder.setEngineKind(ForceInterpreter
? EngineKind::Interpreter
: EngineKind::JIT);
builder.setUseOrcMCJITReplacement(UseJITKind == JITKind::OrcMCJITReplacement);
// If we are supposed to override the target triple, do so now.
if (!TargetTriple.empty())
Mod->setTargetTriple(Triple::normalize(TargetTriple));
// Enable MCJIT if desired.
RTDyldMemoryManager *RTDyldMM = nullptr;
#if 0 // HLSL Change Starts - disable MCJIT
if (!ForceInterpreter) {
if (RemoteMCJIT)
RTDyldMM = new RemoteMemoryManager();
else
RTDyldMM = new SectionMemoryManager();
// Deliberately construct a temp std::unique_ptr to pass in. Do not null out
// RTDyldMM: We still use it below, even though we don't own it.
builder.setMCJITMemoryManager(
std::unique_ptr<RTDyldMemoryManager>(RTDyldMM));
} else if (RemoteMCJIT) {
errs() << "error: Remote process execution does not work with the "
"interpreter.\n";
exit(1);
}
#endif // HLSL Change Ends
builder.setOptLevel(getOptLevel());
TargetOptions Options;
if (FloatABIForCalls != FloatABI::Default)
Options.FloatABIType = FloatABIForCalls;
builder.setTargetOptions(Options);
EE = builder.create();
if (!EE) {
if (!ErrorMsg.empty())
errs() << argv[0] << ": error creating EE: " << ErrorMsg << "\n";
else
errs() << argv[0] << ": unknown error creating EE!\n";
exit(1);
}
if (EnableCacheManager) {
CacheManager = new LLIObjectCache(ObjectCacheDir);
EE->setObjectCache(CacheManager);
}
// Load any additional modules specified on the command line.
for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context);
if (!XMod) {
Err.print(argv[0], errs());
return 1;
}
if (EnableCacheManager) {
std::string CacheName("file:");
CacheName.append(ExtraModules[i]);
XMod->setModuleIdentifier(CacheName);
}
EE->addModule(std::move(XMod));
}
for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
ErrorOr<object::OwningBinary<object::ObjectFile>> Obj =
object::ObjectFile::createObjectFile(ExtraObjects[i]);
if (!Obj) {
Err.print(argv[0], errs());
return 1;
}
object::OwningBinary<object::ObjectFile> &O = Obj.get();
EE->addObjectFile(std::move(O));
}
for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr =
MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]);
if (!ArBufOrErr) {
Err.print(argv[0], errs());
return 1;
}
std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get();
ErrorOr<std::unique_ptr<object::Archive>> ArOrErr =
object::Archive::create(ArBuf->getMemBufferRef());
if (std::error_code EC = ArOrErr.getError()) {
errs() << EC.message();
return 1;
}
std::unique_ptr<object::Archive> &Ar = ArOrErr.get();
object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf));
EE->addArchive(std::move(OB));
}
// If the target is Cygwin/MingW and we are generating remote code, we
// need an extra module to help out with linking.
if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
addCygMingExtraModule(EE, Context, Mod->getTargetTriple());
}
// The following functions have no effect if their respective profiling
// support wasn't enabled in the build configuration.
EE->RegisterJITEventListener(
JITEventListener::createOProfileJITEventListener());
EE->RegisterJITEventListener(
JITEventListener::createIntelJITEventListener());
if (!NoLazyCompilation && RemoteMCJIT) {
errs() << "warning: remote mcjit does not support lazy compilation\n";
NoLazyCompilation = true;
}
EE->DisableLazyCompilation(NoLazyCompilation);
// If the user specifically requested an argv[0] to pass into the program,
// do it now.
if (!FakeArgv0.empty()) {
InputFile = static_cast<std::string>(FakeArgv0);
} else {
// Otherwise, if there is a .bc suffix on the executable strip it off, it
// might confuse the program.
if (StringRef(InputFile).endswith(".bc"))
InputFile.erase(InputFile.length() - 3);
}
// Add the module's name to the start of the vector of arguments to main().
InputArgv.insert(InputArgv.begin(), InputFile);
// Call the main function from M as if its signature were:
// int main (int argc, char **argv, const char **envp)
// using the contents of Args to determine argc & argv, and the contents of
// EnvVars to determine envp.
//
Function *EntryFn = Mod->getFunction(EntryFunc);
if (!EntryFn) {
errs() << '\'' << EntryFunc << "\' function not found in module.\n";
return -1;
}
// Reset errno to zero on entry to main.
errno = 0;
int Result;
if (!RemoteMCJIT) {
// If the program doesn't explicitly call exit, we will need the Exit
// function later on to make an explicit call, so get the function now.
Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context),
Type::getInt32Ty(Context),
nullptr);
// Run static constructors.
if (!ForceInterpreter) {
// Give MCJIT a chance to apply relocations and set page permissions.
EE->finalizeObject();
}
EE->runStaticConstructorsDestructors(false);
// Trigger compilation separately so code regions that need to be
// invalidated will be known.
(void)EE->getPointerToFunction(EntryFn);
// Clear instruction cache before code will be executed.
#if 0 // HLSL Change Starts
if (RTDyldMM)
static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
#endif // HLSL Change Ends
// Run main.
Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
// Run static destructors.
EE->runStaticConstructorsDestructors(true);
// If the program didn't call exit explicitly, we should call it now.
// This ensures that any atexit handlers get called correctly.
if (Function *ExitF = dyn_cast<Function>(Exit)) {
std::vector<GenericValue> Args;
GenericValue ResultGV;
ResultGV.IntVal = APInt(32, Result);
Args.push_back(ResultGV);
EE->runFunction(ExitF, Args);
errs() << "ERROR: exit(" << Result << ") returned!\n";
abort();
} else {
errs() << "ERROR: exit defined with wrong prototype!\n";
abort();
}
} else {
// else == "if (RemoteMCJIT)"
// Remote target MCJIT doesn't (yet) support static constructors. No reason
// it couldn't. This is a limitation of the LLI implemantation, not the
// MCJIT itself. FIXME.
//
RemoteMemoryManager *MM = static_cast<RemoteMemoryManager*>(RTDyldMM);
// Everything is prepared now, so lay out our program for the target
// address space, assign the section addresses to resolve any relocations,
// and send it to the target.
std::unique_ptr<RemoteTarget> Target;
if (!ChildExecPath.empty()) { // Remote execution on a child process
#ifndef LLVM_ON_UNIX
// FIXME: Remove this pointless fallback mode which causes tests to "pass"
// on platforms where they should XFAIL.
errs() << "Warning: host does not support external remote targets.\n"
<< " Defaulting to simulated remote execution\n";
Target.reset(new RemoteTarget);
#else
if (!sys::fs::can_execute(ChildExecPath)) {
errs() << "Unable to find usable child executable: '" << ChildExecPath
<< "'\n";
return -1;
}
Target.reset(new RemoteTargetExternal(ChildExecPath));
#endif
} else {
// No child process name provided, use simulated remote execution.
Target.reset(new RemoteTarget);
}
// Give the memory manager a pointer to our remote target interface object.
MM->setRemoteTarget(Target.get());
// Create the remote target.
if (!Target->create()) {
errs() << "ERROR: " << Target->getErrorMsg() << "\n";
return EXIT_FAILURE;
}
// Since we're executing in a (at least simulated) remote address space,
// we can't use the ExecutionEngine::runFunctionAsMain(). We have to
// grab the function address directly here and tell the remote target
// to execute the function.
//
// Our memory manager will map generated code into the remote address
// space as it is loaded and copy the bits over during the finalizeMemory
// operation.
//
// FIXME: argv and envp handling.
uint64_t Entry = EE->getFunctionAddress(EntryFn->getName().str());
DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"
<< format("%llx", Entry) << "\n");
if (!Target->executeCode(Entry, Result))
errs() << "ERROR: " << Target->getErrorMsg() << "\n";
// Like static constructors, the remote target MCJIT support doesn't handle
// this yet. It could. FIXME.
// Stop the remote target
Target->stop();
}
return Result;
}
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/LLVMBuild.txt
|
;===- ./tools/lli/LLVMBuild.txt --------------------------------*- Conf -*--===;
;
; The LLVM Compiler Infrastructure
;
; This file is distributed under the University of Illinois Open Source
; License. See LICENSE.TXT for details.
;
;===------------------------------------------------------------------------===;
;
; This is an LLVMBuild description file for the components in this subdirectory.
;
; For more information on the LLVMBuild system, please see:
;
; http://llvm.org/docs/LLVMBuild.html
;
;===------------------------------------------------------------------------===;
[common]
subdirectories = ChildTarget
[component_0]
type = Tool
name = lli
parent = Tools
required_libraries =
AsmParser
BitReader
IRReader
Instrumentation
Interpreter
MCJIT
Native
NativeCodeGen
SelectionDAG
TransformUtils
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/RemoteTarget.h
|
//===- RemoteTarget.h - LLVM Remote process JIT execution ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Definition of the RemoteTarget class which executes JITed code in a
// separate address range from where it was built.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLI_REMOTETARGET_H
#define LLVM_TOOLS_LLI_REMOTETARGET_H
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Memory.h"
#include <stdlib.h>
#include <string>
namespace llvm {
class RemoteTarget {
bool IsRunning;
typedef SmallVector<sys::MemoryBlock, 16> AllocMapType;
AllocMapType Allocations;
protected:
std::string ErrorMsg;
public:
StringRef getErrorMsg() const { return ErrorMsg; }
/// Allocate space in the remote target address space.
///
/// @param Size Amount of space, in bytes, to allocate.
/// @param Alignment Required minimum alignment for allocated space.
/// @param[out] Address Remote address of the allocated memory.
///
/// @returns True on success. On failure, ErrorMsg is updated with
/// descriptive text of the encountered error.
virtual bool allocateSpace(size_t Size,
unsigned Alignment,
uint64_t &Address);
bool isAllocatedMemory(uint64_t Address, uint32_t Size) {
uint64_t AddressEnd = Address + Size;
for (AllocMapType::const_iterator I = Allocations.begin(),
E = Allocations.end();
I != E; ++I) {
if (Address >= (uint64_t)I->base() &&
AddressEnd <= (uint64_t)I->base() + I->size())
return true;
}
return false;
}
/// Load data into the target address space.
///
/// @param Address Destination address in the target process.
/// @param Data Source address in the host process.
/// @param Size Number of bytes to copy.
///
/// @returns True on success. On failure, ErrorMsg is updated with
/// descriptive text of the encountered error.
virtual bool loadData(uint64_t Address,
const void *Data,
size_t Size);
/// Load code into the target address space and prepare it for execution.
///
/// @param Address Destination address in the target process.
/// @param Data Source address in the host process.
/// @param Size Number of bytes to copy.
///
/// @returns True on success. On failure, ErrorMsg is updated with
/// descriptive text of the encountered error.
virtual bool loadCode(uint64_t Address,
const void *Data,
size_t Size);
/// Execute code in the target process. The called function is required
/// to be of signature int "(*)(void)".
///
/// @param Address Address of the loaded function in the target
/// process.
/// @param[out] RetVal The integer return value of the called function.
///
/// @returns True on success. On failure, ErrorMsg is updated with
/// descriptive text of the encountered error.
virtual bool executeCode(uint64_t Address,
int &RetVal);
/// Minimum alignment for memory permissions. Used to separate code and
/// data regions to make sure data doesn't get marked as code or vice
/// versa.
///
/// @returns Page alignment return value. Default of 4k.
virtual unsigned getPageAlignment() { return 4096; }
/// Start the remote process.
virtual bool create();
/// Terminate the remote process.
virtual void stop();
RemoteTarget() : IsRunning(false), ErrorMsg("") {}
virtual ~RemoteTarget() { if (IsRunning) stop(); }
private:
// Main processing function for the remote target process. Command messages
// are received on file descriptor CmdFD and responses come back on OutFD.
static void doRemoteTargeting(int CmdFD, int OutFD);
};
} // end namespace llvm
#endif
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/RemoteMemoryManager.cpp
|
//===---- RemoteMemoryManager.cpp - Recording memory manager --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This memory manager allocates local storage and keeps a record of each
// allocation. Iterators are provided for all data and code allocations.
//
//===----------------------------------------------------------------------===//
#include "RemoteMemoryManager.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
#define DEBUG_TYPE "lli"
RemoteMemoryManager::~RemoteMemoryManager() {
for (SmallVector<Allocation, 2>::iterator
I = AllocatedSections.begin(), E = AllocatedSections.end();
I != E; ++I)
sys::Memory::releaseMappedMemory(I->MB);
}
uint8_t *RemoteMemoryManager::
allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName) {
// The recording memory manager is just a local copy of the remote target.
// The alignment requirement is just stored here for later use. Regular
// heap storage is sufficient here, but we're using mapped memory to work
// around a bug in MCJIT.
sys::MemoryBlock Block = allocateSection(Size);
// AllocatedSections will own this memory.
AllocatedSections.push_back( Allocation(Block, Alignment, true) );
// UnmappedSections has the same information but does not own the memory.
UnmappedSections.push_back( Allocation(Block, Alignment, true) );
return (uint8_t*)Block.base();
}
uint8_t *RemoteMemoryManager::
allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, StringRef SectionName,
bool IsReadOnly) {
// The recording memory manager is just a local copy of the remote target.
// The alignment requirement is just stored here for later use. Regular
// heap storage is sufficient here, but we're using mapped memory to work
// around a bug in MCJIT.
sys::MemoryBlock Block = allocateSection(Size);
// AllocatedSections will own this memory.
AllocatedSections.push_back( Allocation(Block, Alignment, false) );
// UnmappedSections has the same information but does not own the memory.
UnmappedSections.push_back( Allocation(Block, Alignment, false) );
return (uint8_t*)Block.base();
}
sys::MemoryBlock RemoteMemoryManager::allocateSection(uintptr_t Size) {
std::error_code ec;
sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(Size,
&Near,
sys::Memory::MF_READ |
sys::Memory::MF_WRITE,
ec);
assert(!ec && MB.base());
// FIXME: This is part of a work around to keep sections near one another
// when MCJIT performs relocations after code emission but before
// the generated code is moved to the remote target.
// Save this address as the basis for our next request
Near = MB;
return MB;
}
void RemoteMemoryManager::notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &Obj) {
// The client should have called setRemoteTarget() before triggering any
// code generation.
assert(Target);
if (!Target)
return;
// FIXME: Make this function thread safe.
// Lay out our sections in order, with all the code sections first, then
// all the data sections.
uint64_t CurOffset = 0;
unsigned MaxAlign = Target->getPageAlignment();
SmallVector<std::pair<Allocation, uint64_t>, 16> Offsets;
unsigned NumSections = UnmappedSections.size();
// We're going to go through the list twice to separate code and data, but
// it's a very small list, so that's OK.
for (size_t i = 0, e = NumSections; i != e; ++i) {
Allocation &Section = UnmappedSections[i];
if (Section.IsCode) {
unsigned Size = Section.MB.size();
unsigned Align = Section.Alignment;
DEBUG(dbgs() << "code region: size " << Size
<< ", alignment " << Align << "\n");
// Align the current offset up to whatever is needed for the next
// section.
CurOffset = (CurOffset + Align - 1) / Align * Align;
// Save off the address of the new section and allocate its space.
Offsets.push_back(std::pair<Allocation,uint64_t>(Section, CurOffset));
CurOffset += Size;
}
}
// Adjust to keep code and data aligned on separate pages.
CurOffset = (CurOffset + MaxAlign - 1) / MaxAlign * MaxAlign;
for (size_t i = 0, e = NumSections; i != e; ++i) {
Allocation &Section = UnmappedSections[i];
if (!Section.IsCode) {
unsigned Size = Section.MB.size();
unsigned Align = Section.Alignment;
DEBUG(dbgs() << "data region: size " << Size
<< ", alignment " << Align << "\n");
// Align the current offset up to whatever is needed for the next
// section.
CurOffset = (CurOffset + Align - 1) / Align * Align;
// Save off the address of the new section and allocate its space.
Offsets.push_back(std::pair<Allocation,uint64_t>(Section, CurOffset));
CurOffset += Size;
}
}
// Allocate space in the remote target.
uint64_t RemoteAddr;
if (!Target->allocateSpace(CurOffset, MaxAlign, RemoteAddr))
report_fatal_error(Target->getErrorMsg());
// Map the section addresses so relocations will get updated in the local
// copies of the sections.
for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
uint64_t Addr = RemoteAddr + Offsets[i].second;
EE->mapSectionAddress(const_cast<void*>(Offsets[i].first.MB.base()), Addr);
DEBUG(dbgs() << " Mapping local: " << Offsets[i].first.MB.base()
<< " to remote: 0x" << format("%llx", Addr) << "\n");
MappedSections[Addr] = Offsets[i].first;
}
UnmappedSections.clear();
}
bool RemoteMemoryManager::finalizeMemory(std::string *ErrMsg) {
// FIXME: Make this function thread safe.
for (DenseMap<uint64_t, Allocation>::iterator
I = MappedSections.begin(), E = MappedSections.end();
I != E; ++I) {
uint64_t RemoteAddr = I->first;
const Allocation &Section = I->second;
if (Section.IsCode) {
if (!Target->loadCode(RemoteAddr, Section.MB.base(), Section.MB.size()))
report_fatal_error(Target->getErrorMsg());
DEBUG(dbgs() << " loading code: " << Section.MB.base()
<< " to remote: 0x" << format("%llx", RemoteAddr) << "\n");
} else {
if (!Target->loadData(RemoteAddr, Section.MB.base(), Section.MB.size()))
report_fatal_error(Target->getErrorMsg());
DEBUG(dbgs() << " loading data: " << Section.MB.base()
<< " to remote: 0x" << format("%llx", RemoteAddr) << "\n");
}
}
MappedSections.clear();
return false;
}
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/RemoteTargetExternal.h
|
//===----- RemoteTargetExternal.h - LLVM out-of-process JIT execution -----===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Definition of the RemoteTargetExternal class which executes JITed code in a
// separate process from where it was built.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLI_REMOTETARGETEXTERNAL_H
#define LLVM_TOOLS_LLI_REMOTETARGETEXTERNAL_H
#include "RPCChannel.h"
#include "RemoteTarget.h"
#include "RemoteTargetMessage.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Config/config.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Memory.h"
#include <stdlib.h>
#include <string>
namespace llvm {
class RemoteTargetExternal : public RemoteTarget {
RPCChannel RPC;
bool WriteBytes(const void *Data, size_t Size) {
return RPC.WriteBytes(Data, Size);
}
bool ReadBytes(void *Data, size_t Size) { return RPC.ReadBytes(Data, Size); }
public:
/// Allocate space in the remote target address space.
///
/// @param Size Amount of space, in bytes, to allocate.
/// @param Alignment Required minimum alignment for allocated space.
/// @param[out] Address Remote address of the allocated memory.
///
/// @returns True on success. On failure, ErrorMsg is updated with
/// descriptive text of the encountered error.
bool allocateSpace(size_t Size, unsigned Alignment,
uint64_t &Address) override;
/// Load data into the target address space.
///
/// @param Address Destination address in the target process.
/// @param Data Source address in the host process.
/// @param Size Number of bytes to copy.
///
/// @returns True on success. On failure, ErrorMsg is updated with
/// descriptive text of the encountered error.
bool loadData(uint64_t Address, const void *Data, size_t Size) override;
/// Load code into the target address space and prepare it for execution.
///
/// @param Address Destination address in the target process.
/// @param Data Source address in the host process.
/// @param Size Number of bytes to copy.
///
/// @returns True on success. On failure, ErrorMsg is updated with
/// descriptive text of the encountered error.
bool loadCode(uint64_t Address, const void *Data, size_t Size) override;
/// Execute code in the target process. The called function is required
/// to be of signature int "(*)(void)".
///
/// @param Address Address of the loaded function in the target
/// process.
/// @param[out] RetVal The integer return value of the called function.
///
/// @returns True on success. On failure, ErrorMsg is updated with
/// descriptive text of the encountered error.
bool executeCode(uint64_t Address, int &RetVal) override;
/// Minimum alignment for memory permissions. Used to separate code and
/// data regions to make sure data doesn't get marked as code or vice
/// versa.
///
/// @returns Page alignment return value. Default of 4k.
unsigned getPageAlignment() override { return 4096; }
bool create() override {
RPC.ChildName = ChildName;
if (!RPC.createServer())
return true;
// We must get Ack from the client (blocking read)
if (!Receive(LLI_ChildActive)) {
ErrorMsg += ", (RPCChannel::create) - Stopping process!";
stop();
return false;
}
return true;
}
/// Terminate the remote process.
void stop() override;
RemoteTargetExternal(std::string &Name) : RemoteTarget(), ChildName(Name) {}
~RemoteTargetExternal() override {}
private:
std::string ChildName;
bool SendAllocateSpace(uint32_t Alignment, uint32_t Size);
bool SendLoadSection(uint64_t Addr,
const void *Data,
uint32_t Size,
bool IsCode);
bool SendExecute(uint64_t Addr);
bool SendTerminate();
// High-level wrappers for receiving data
bool Receive(LLIMessageType Msg);
bool Receive(LLIMessageType Msg, int32_t &Data);
bool Receive(LLIMessageType Msg, uint64_t &Data);
// Lower level target-independent read/write to deal with errors
bool ReceiveHeader(LLIMessageType Msg);
bool ReceivePayload();
bool SendHeader(LLIMessageType Msg);
bool SendPayload();
// Functions to append/retrieve data from the payload
SmallVector<const void *, 2> SendData;
SmallVector<void *, 1> ReceiveData; // Future proof
SmallVector<int, 2> Sizes;
void AppendWrite(const void *Data, uint32_t Size);
void AppendRead(void *Data, uint32_t Size);
};
} // end namespace llvm
#endif
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/lli/RemoteMemoryManager.h
|
//===- RemoteMemoryManager.h - LLI MCJIT recording memory manager ------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This memory manager allocates local storage and keeps a record of each
// allocation. Iterators are provided for all data and code allocations.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLI_REMOTEMEMORYMANAGER_H
#define LLVM_TOOLS_LLI_REMOTEMEMORYMANAGER_H
#include "RemoteTarget.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Memory.h"
#include <utility>
namespace llvm {
class RemoteMemoryManager : public RTDyldMemoryManager {
public:
// Notice that this structure takes ownership of the memory allocated.
struct Allocation {
Allocation() {}
Allocation(sys::MemoryBlock mb, unsigned a, bool code)
: MB(mb), Alignment(a), IsCode(code) {}
sys::MemoryBlock MB;
unsigned Alignment;
bool IsCode;
};
private:
// This vector contains Allocation objects for all sections which we have
// allocated. This vector effectively owns the memory associated with the
// allocations.
SmallVector<Allocation, 2> AllocatedSections;
// This vector contains pointers to Allocation objects for any sections we
// have allocated locally but have not yet remapped for the remote target.
// When we receive notification of a completed module load, we will map
// these sections into the remote target.
SmallVector<Allocation, 2> UnmappedSections;
// This map tracks the sections we have remapped for the remote target
// but have not yet copied to the target.
DenseMap<uint64_t, Allocation> MappedSections;
// FIXME: This is part of a work around to keep sections near one another
// when MCJIT performs relocations after code emission but before
// the generated code is moved to the remote target.
sys::MemoryBlock Near;
sys::MemoryBlock allocateSection(uintptr_t Size);
RemoteTarget *Target;
public:
RemoteMemoryManager() : Target(nullptr) {}
~RemoteMemoryManager() override;
uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID,
StringRef SectionName) override;
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, StringRef SectionName,
bool IsReadOnly) override;
// For now, remote symbol resolution is not support in lli. The MCJIT
// interface does support this, but clients must provide their own
// mechanism for finding remote symbol addresses. MCJIT will resolve
// symbols from Modules it contains.
uint64_t getSymbolAddress(const std::string &Name) override { return 0; }
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &Obj) override;
bool finalizeMemory(std::string *ErrMsg) override;
// For now, remote EH frame registration isn't supported. Remote symbol
// resolution is a prerequisite to supporting remote EH frame registration.
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
size_t Size) override {}
void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
size_t Size) override {}
// This is a non-interface function used by lli
void setRemoteTarget(RemoteTarget *T) { Target = T; }
};
} // end namespace llvm
#endif
|
0 |
repos/DirectXShaderCompiler/tools/lli
|
repos/DirectXShaderCompiler/tools/lli/Unix/RPCChannel.inc
|
//=- RPCChannel.inc - LLVM out-of-process JIT execution for Unix --=//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Implementation of the Unix-specific parts of the RPCChannel class
// which executes JITed code in a separate process from where it was built.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/Errno.h"
#include "llvm/Support/raw_ostream.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
namespace {
struct ConnectionData_t {
int InputPipe;
int OutputPipe;
ConnectionData_t(int in, int out) : InputPipe(in), OutputPipe(out) {}
};
} // namespace
namespace llvm {
bool RPCChannel::createServer() {
int PipeFD[2][2];
pid_t ChildPID;
// Create two pipes.
if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
perror("Error creating pipe: ");
ChildPID = fork();
if (ChildPID == 0) {
// In the child...
// Close the parent ends of the pipes
close(PipeFD[0][1]);
close(PipeFD[1][0]);
// Use our pipes as stdin and stdout
if (PipeFD[0][0] != STDIN_FILENO) {
dup2(PipeFD[0][0], STDIN_FILENO);
close(PipeFD[0][0]);
}
if (PipeFD[1][1] != STDOUT_FILENO) {
dup2(PipeFD[1][1], STDOUT_FILENO);
close(PipeFD[1][1]);
}
// Execute the child process.
char *args[1] = { nullptr };
int rc = execv(ChildName.c_str(), args);
if (rc != 0)
perror("Error executing child process: ");
} else {
// In the parent...
// Close the child ends of the pipes
close(PipeFD[0][0]);
close(PipeFD[1][1]);
// Store the parent ends of the pipes
ConnectionData = (void *)new ConnectionData_t(PipeFD[1][0], PipeFD[0][1]);
return true;
}
return false;
}
bool RPCChannel::createClient() {
// Store the parent ends of the pipes
ConnectionData = (void *)new ConnectionData_t(STDIN_FILENO, STDOUT_FILENO);
return true;
}
void RPCChannel::Wait() { wait(nullptr); }
static bool CheckError(int rc, size_t Size, const char *Desc) {
if (rc < 0) {
llvm::errs() << "IO Error: " << Desc << ": " << sys::StrError() << '\n';
return false;
} else if ((size_t)rc != Size) {
std::string ErrorMsg;
char Number[10] = { 0 };
ErrorMsg += "Expecting ";
sprintf(Number, "%d", (uint32_t)Size);
ErrorMsg += Number;
ErrorMsg += " bytes, Got ";
sprintf(Number, "%d", rc);
ErrorMsg += Number;
llvm::errs() << "RPC Error: " << Desc << ": " << ErrorMsg << '\n';
return false;
}
return true;
}
bool RPCChannel::WriteBytes(const void *Data, size_t Size) {
int rc = write(((ConnectionData_t *)ConnectionData)->OutputPipe, Data, Size);
return CheckError(rc, Size, "WriteBytes");
}
bool RPCChannel::ReadBytes(void *Data, size_t Size) {
int rc = read(((ConnectionData_t *)ConnectionData)->InputPipe, Data, Size);
return CheckError(rc, Size, "ReadBytes");
}
RPCChannel::~RPCChannel() {
delete static_cast<ConnectionData_t *>(ConnectionData);
}
} // namespace llvm
|
0 |
repos/DirectXShaderCompiler/tools/lli
|
repos/DirectXShaderCompiler/tools/lli/ChildTarget/ChildTarget.cpp
|
#include "llvm/Config/config.h"
#include "../RPCChannel.h"
#include "../RemoteTarget.h"
#include "../RemoteTargetMessage.h"
#include "llvm/Support/Memory.h"
#include <assert.h>
#include <map>
#include <stdint.h>
#include <string>
#include <vector>
using namespace llvm;
class LLIChildTarget {
public:
void initialize();
LLIMessageType waitForIncomingMessage();
void handleMessage(LLIMessageType messageType);
RemoteTarget *RT;
RPCChannel RPC;
private:
// Incoming message handlers
void handleAllocateSpace();
void handleLoadSection(bool IsCode);
void handleExecute();
// Outgoing message handlers
void sendChildActive();
void sendAllocationResult(uint64_t Addr);
void sendLoadStatus(uint32_t Status);
void sendExecutionComplete(int Result);
// OS-specific functions
void initializeConnection();
int WriteBytes(const void *Data, size_t Size) {
return RPC.WriteBytes(Data, Size) ? Size : -1;
}
int ReadBytes(void *Data, size_t Size) {
return RPC.ReadBytes(Data, Size) ? Size : -1;
}
// Communication handles (OS-specific)
void *ConnectionData;
};
int main() {
LLIChildTarget ThisChild;
ThisChild.RT = new RemoteTarget();
ThisChild.initialize();
LLIMessageType MsgType;
do {
MsgType = ThisChild.waitForIncomingMessage();
ThisChild.handleMessage(MsgType);
} while (MsgType != LLI_Terminate &&
MsgType != LLI_Error);
delete ThisChild.RT;
return 0;
}
// Public methods
void LLIChildTarget::initialize() {
RPC.createClient();
sendChildActive();
}
LLIMessageType LLIChildTarget::waitForIncomingMessage() {
int32_t MsgType = -1;
if (ReadBytes(&MsgType, 4) > 0)
return (LLIMessageType)MsgType;
return LLI_Error;
}
void LLIChildTarget::handleMessage(LLIMessageType messageType) {
switch (messageType) {
case LLI_AllocateSpace:
handleAllocateSpace();
break;
case LLI_LoadCodeSection:
handleLoadSection(true);
break;
case LLI_LoadDataSection:
handleLoadSection(false);
break;
case LLI_Execute:
handleExecute();
break;
case LLI_Terminate:
RT->stop();
break;
default:
// FIXME: Handle error!
break;
}
}
// Incoming message handlers
void LLIChildTarget::handleAllocateSpace() {
// Read and verify the message data size.
uint32_t DataSize = 0;
int rc = ReadBytes(&DataSize, 4);
(void)rc;
assert(rc == 4);
assert(DataSize == 8);
// Read the message arguments.
uint32_t Alignment = 0;
uint32_t AllocSize = 0;
rc = ReadBytes(&Alignment, 4);
assert(rc == 4);
rc = ReadBytes(&AllocSize, 4);
assert(rc == 4);
// Allocate the memory.
uint64_t Addr;
RT->allocateSpace(AllocSize, Alignment, Addr);
// Send AllocationResult message.
sendAllocationResult(Addr);
}
void LLIChildTarget::handleLoadSection(bool IsCode) {
// Read the message data size.
uint32_t DataSize = 0;
int rc = ReadBytes(&DataSize, 4);
(void)rc;
assert(rc == 4);
// Read the target load address.
uint64_t Addr = 0;
rc = ReadBytes(&Addr, 8);
assert(rc == 8);
size_t BufferSize = DataSize - 8;
if (!RT->isAllocatedMemory(Addr, BufferSize))
return sendLoadStatus(LLI_Status_NotAllocated);
// Read section data into previously allocated buffer
rc = ReadBytes((void*)Addr, BufferSize);
if (rc != (int)(BufferSize))
return sendLoadStatus(LLI_Status_IncompleteMsg);
// If IsCode, mark memory executable
if (IsCode)
sys::Memory::InvalidateInstructionCache((void *)Addr, BufferSize);
// Send MarkLoadComplete message.
sendLoadStatus(LLI_Status_Success);
}
void LLIChildTarget::handleExecute() {
// Read the message data size.
uint32_t DataSize = 0;
int rc = ReadBytes(&DataSize, 4);
(void)rc;
assert(rc == 4);
assert(DataSize == 8);
// Read the target address.
uint64_t Addr = 0;
rc = ReadBytes(&Addr, 8);
assert(rc == 8);
// Call function
int32_t Result = -1;
RT->executeCode(Addr, Result);
// Send ExecutionResult message.
sendExecutionComplete(Result);
}
// Outgoing message handlers
void LLIChildTarget::sendChildActive() {
// Write the message type.
uint32_t MsgType = (uint32_t)LLI_ChildActive;
int rc = WriteBytes(&MsgType, 4);
(void)rc;
assert(rc == 4);
// Write the data size.
uint32_t DataSize = 0;
rc = WriteBytes(&DataSize, 4);
assert(rc == 4);
}
void LLIChildTarget::sendAllocationResult(uint64_t Addr) {
// Write the message type.
uint32_t MsgType = (uint32_t)LLI_AllocationResult;
int rc = WriteBytes(&MsgType, 4);
(void)rc;
assert(rc == 4);
// Write the data size.
uint32_t DataSize = 8;
rc = WriteBytes(&DataSize, 4);
assert(rc == 4);
// Write the allocated address.
rc = WriteBytes(&Addr, 8);
assert(rc == 8);
}
void LLIChildTarget::sendLoadStatus(uint32_t Status) {
// Write the message type.
uint32_t MsgType = (uint32_t)LLI_LoadResult;
int rc = WriteBytes(&MsgType, 4);
(void)rc;
assert(rc == 4);
// Write the data size.
uint32_t DataSize = 4;
rc = WriteBytes(&DataSize, 4);
assert(rc == 4);
// Write the result.
rc = WriteBytes(&Status, 4);
assert(rc == 4);
}
void LLIChildTarget::sendExecutionComplete(int Result) {
// Write the message type.
uint32_t MsgType = (uint32_t)LLI_ExecutionResult;
int rc = WriteBytes(&MsgType, 4);
(void)rc;
assert(rc == 4);
// Write the data size.
uint32_t DataSize = 4;
rc = WriteBytes(&DataSize, 4);
assert(rc == 4);
// Write the result.
rc = WriteBytes(&Result, 4);
assert(rc == 4);
}
#ifdef LLVM_ON_UNIX
#include "../Unix/RPCChannel.inc"
#endif
#ifdef LLVM_ON_WIN32
#include "../Windows/RPCChannel.inc"
#endif
|
0 |
repos/DirectXShaderCompiler/tools/lli
|
repos/DirectXShaderCompiler/tools/lli/ChildTarget/CMakeLists.txt
|
set(LLVM_LINK_COMPONENTS support)
add_llvm_executable(lli-child-target
ChildTarget.cpp
../RemoteTarget.cpp
)
set_target_properties(lli-child-target PROPERTIES FOLDER "Misc")
|
0 |
repos/DirectXShaderCompiler/tools/lli
|
repos/DirectXShaderCompiler/tools/lli/ChildTarget/LLVMBuild.txt
|
;===- ./tools/lli/ChildTarget/LLVMBuild.txt --------------------*- Conf -*--===;
;
; The LLVM Compiler Infrastructure
;
; This file is distributed under the University of Illinois Open Source
; License. See LICENSE.TXT for details.
;
;===------------------------------------------------------------------------===;
;
; This is an LLVMBuild description file for the components in this subdirectory.
;
; For more information on the LLVMBuild system, please see:
;
; http://llvm.org/docs/LLVMBuild.html
;
;===------------------------------------------------------------------------===;
[component_0]
type = Tool
name = lli-child-target
parent = lli
|
0 |
repos/DirectXShaderCompiler/tools/lli
|
repos/DirectXShaderCompiler/tools/lli/Windows/RPCChannel.inc
|
//=- RPCChannel.inc - LLVM out-of-process JIT execution for Windows --=//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Implementation of the Windows-specific parts of the RPCChannel class
// which executes JITed code in a separate process from where it was built.
//
//===----------------------------------------------------------------------===//
namespace llvm {
bool RPCChannel::createServer() { return false; }
bool RPCChannel::createClient() { return false; }
bool RPCChannel::WriteBytes(const void *Data, size_t Size) { return false; }
bool RPCChannel::ReadBytes(void *Data, size_t Size) { return false; }
void RPCChannel::Wait() {}
RPCChannel::~RPCChannel() {}
} // namespace llvm
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/llvm-dis/CMakeLists.txt
|
set(LLVM_LINK_COMPONENTS
BitReader
Core
DxilContainer
Support
MSSupport # HLSL Change
)
add_llvm_tool(llvm-dis
llvm-dis.cpp
)
# HLSL Change Starts
if (NOT HLSL_OPTIONAL_PROJS_IN_DEFAULT)
set_target_properties(llvm-dis PROPERTIES EXCLUDE_FROM_ALL ON EXCLUDE_FROM_DEFAULT_BUILD ON)
endif ()
# HLSL Change Ends
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/llvm-dis/LLVMBuild.txt
|
;===- ./tools/llvm-dis/LLVMBuild.txt ---------------------------*- Conf -*--===;
;
; The LLVM Compiler Infrastructure
;
; This file is distributed under the University of Illinois Open Source
; License. See LICENSE.TXT for details.
;
;===------------------------------------------------------------------------===;
;
; This is an LLVMBuild description file for the components in this subdirectory.
;
; For more information on the LLVMBuild system, please see:
;
; http://llvm.org/docs/LLVMBuild.html
;
;===------------------------------------------------------------------------===;
[component_0]
type = Tool
name = llvm-dis
parent = Tools
required_libraries = Analysis BitReader
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/llvm-dis/llvm-dis.cpp
|
//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This utility may be invoked in the following manner:
// llvm-dis [options] - Read LLVM bitcode from stdin, write asm to stdout
// llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm
// to the x.ll file.
// Options:
// --help - Output information about command line switches
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/LLVMContext.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/IR/AssemblyAnnotationWriter.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/DataStream.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/ToolOutputFile.h"
#include "dxc/DxilContainer/DxilContainer.h" // HLSL Change
#include <system_error>
using namespace llvm;
static cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
static cl::opt<std::string>
OutputFilename("o", cl::desc("Override output filename"),
cl::value_desc("filename"));
static cl::opt<bool>
Force("f", cl::desc("Enable binary output on terminals"));
static cl::opt<bool>
DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
static cl::opt<bool>
ShowAnnotations("show-annotations",
cl::desc("Add informational comments to the .ll file"));
static cl::opt<bool> PreserveAssemblyUseListOrder(
"preserve-ll-uselistorder",
cl::desc("Preserve use-list order when writing LLVM assembly."),
cl::init(false), cl::Hidden);
namespace {
static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
OS << DL.getLine() << ":" << DL.getCol();
if (DILocation *IDL = DL.getInlinedAt()) {
OS << "@";
printDebugLoc(IDL, OS);
}
}
class CommentWriter : public AssemblyAnnotationWriter {
public:
void emitFunctionAnnot(const Function *F,
formatted_raw_ostream &OS) override {
OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
OS << '\n';
}
void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
bool Padded = false;
if (!V.getType()->isVoidTy()) {
OS.PadToColumn(50);
Padded = true;
// Output # uses and type
OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
}
if (const Instruction *I = dyn_cast<Instruction>(&V)) {
if (const DebugLoc &DL = I->getDebugLoc()) {
if (!Padded) {
OS.PadToColumn(50);
Padded = true;
OS << ";";
}
OS << " [debug line = ";
printDebugLoc(DL,OS);
OS << "]";
}
if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
if (!Padded) {
OS.PadToColumn(50);
OS << ";";
}
OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
}
else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
if (!Padded) {
OS.PadToColumn(50);
OS << ";";
}
OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
}
}
}
};
} // end anon namespace
static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
raw_ostream &OS = errs();
OS << (char *)Context << ": ";
switch (DI.getSeverity()) {
case DS_Error: OS << "error: "; break;
case DS_Warning: OS << "warning: "; break;
case DS_Remark: OS << "remark: "; break;
case DS_Note: OS << "note: "; break;
}
DiagnosticPrinterRawOStream DP(OS);
DI.print(DP);
OS << '\n';
if (DI.getSeverity() == DS_Error)
exit(1);
}
// HLSL Change Starts
#include "dxc/Support/WinIncludes.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MSFileSystem.h"
// HLSL Change Ends
int __cdecl main(int argc, char **argv) { // HLSL Change - __cdecl
// Print a stack trace if we signal out.
// sys::PrintStackTraceOnErrorSignal(); // HLSL Change - disable this
// PrettyStackTraceProgram X(argc, argv); // HLSL Change - disable this
// HLSL Change Starts
if (llvm::sys::fs::SetupPerThreadFileSystem())
return 1;
llvm::sys::fs::MSFileSystem* msfPtr;
HRESULT hr;
if (!SUCCEEDED(hr = CreateMSFileSystemForDisk(&msfPtr)))
return 1;
std::unique_ptr<llvm::sys::fs::MSFileSystem> msf(msfPtr);
llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
// HLSL Change Ends
LLVMContext &Context = getGlobalContext();
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
std::string ErrorMessage;
std::unique_ptr<Module> M;
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFileOrSTDIN(InputFilename);
if (std::error_code EC = FileOrErr.getError()) {
errs() << argv[0] << ": "
<< "Could not open file '" << InputFilename << "': " << EC.message()
<< '\n';
return 1;
}
std::unique_ptr<MemoryBuffer> &Buf = FileOrErr.get();
MemoryBufferRef BitcodeData = Buf->getMemBufferRef();
if (Buf->getBuffer().startswith("DXBC")) {
// move along until I get to the bitcode
const hlsl::DxilContainerHeader *Header =
reinterpret_cast<const hlsl::DxilContainerHeader *>(Buf->getBufferStart());
const hlsl::DxilProgramHeader *DXILHeader =
hlsl::GetDxilProgramHeader(Header, hlsl::DFCC_DXIL);
if (!DXILHeader) {
errs() << argv[0] << ": DXBC file '" << InputFilename
<< "': Does not contain DXIL part\n";
return 1;
}
StringRef DXILData = StringRef(hlsl::GetDxilBitcodeData(DXILHeader),
hlsl::GetDxilBitcodeSize(DXILHeader));
BitcodeData = MemoryBufferRef(DXILData, "");
}
ErrorOr<std::unique_ptr<Module>> MOrErr =
parseBitcodeFile(BitcodeData, Context);
if (std::error_code EC = MOrErr.getError()) {
errs() << argv[0] << ": "
<< "Could not load bitcode from file '" << InputFilename
<< "': " << EC.message() << '\n';
return 1;
}
M = std::move(*MOrErr);
M->materializeAllPermanently();
// Just use stdout. We won't actually print anything on it.
if (DontPrint)
OutputFilename = "-";
if (OutputFilename.empty()) { // Unspecified output, infer it.
if (InputFilename == "-") {
OutputFilename = "-";
} else {
StringRef IFN = InputFilename;
OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
OutputFilename += ".ll";
}
}
std::error_code EC;
std::unique_ptr<tool_output_file> Out(
new tool_output_file(OutputFilename, EC, sys::fs::F_None));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
std::unique_ptr<AssemblyAnnotationWriter> Annotator;
if (ShowAnnotations)
Annotator.reset(new CommentWriter());
// All that llvm-dis does is write the assembly to a file.
if (!DontPrint)
M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
// Declare success.
Out->keep();
return 0;
}
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/llvm-stress/CMakeLists.txt
|
set(LLVM_LINK_COMPONENTS
Core
IPA
Support
)
add_llvm_tool(llvm-stress
llvm-stress.cpp
)
export_executable_symbols(llvm-stress)
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/llvm-stress/LLVMBuild.txt
|
;===- ./tools/llvm-stress/LLVMBuild.txt -------------------------*- Conf -*--===;
;
; The LLVM Compiler Infrastructure
;
; This file is distributed under the University of Illinois Open Source
; License. See LICENSE.TXT for details.
;
;===------------------------------------------------------------------------===;
;
; This is an LLVMBuild description file for the components in this subdirectory.
;
; For more information on the LLVMBuild system, please see:
;
; http://llvm.org/docs/LLVMBuild.html
;
;===------------------------------------------------------------------------===;
[component_0]
type = Tool
name = llvm-stress
parent = Tools
required_libraries = AsmParser BitReader BitWriter IPO Instrumentation Scalar
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/llvm-stress/llvm-stress.cpp
|
//===-- llvm-stress.cpp - Generate random LL files to stress-test LLVM ----===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This program is a utility that generates random .ll files to stress-test
// different components in LLVM.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/CallGraphSCCPass.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassNameParser.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/ToolOutputFile.h"
#include <algorithm>
#include <set>
#include <sstream>
#include <random>
#include <vector>
namespace llvm {
static cl::opt<unsigned> SeedCL("seed",
cl::desc("Seed used for randomness"), cl::init(0));
static cl::opt<unsigned> SizeCL("size",
cl::desc("The estimated size of the generated function (# of instrs)"),
cl::init(100));
static cl::opt<std::string>
OutputFilename("o", cl::desc("Override output filename"),
cl::value_desc("filename"));
namespace cl {
template <> class parser<Type*> final : public basic_parser<Type*> {
public:
parser(Option &O) : basic_parser(O) {}
// Parse options as IR types. Return true on error.
bool parse(Option &O, StringRef, StringRef Arg, Type *&Value) {
auto &Context = getGlobalContext();
if (Arg == "half") Value = Type::getHalfTy(Context);
else if (Arg == "fp128") Value = Type::getFP128Ty(Context);
else if (Arg == "x86_fp80") Value = Type::getX86_FP80Ty(Context);
else if (Arg == "ppc_fp128") Value = Type::getPPC_FP128Ty(Context);
else if (Arg == "x86_mmx") Value = Type::getX86_MMXTy(Context);
else if (Arg.startswith("i")) {
unsigned N = 0;
Arg.drop_front().getAsInteger(10, N);
if (N > 0)
Value = Type::getIntNTy(Context, N);
}
if (!Value)
return O.error("Invalid IR scalar type: '" + Arg + "'!");
return false;
}
const char *getValueName() const override { return "IR scalar type"; }
};
}
static cl::list<Type*> AdditionalScalarTypes("types", cl::CommaSeparated,
cl::desc("Additional IR scalar types "
"(always includes i1, i8, i16, i32, i64, float and double)"));
namespace {
/// A utility class to provide a pseudo-random number generator which is
/// the same across all platforms. This is somewhat close to the libc
/// implementation. Note: This is not a cryptographically secure pseudorandom
/// number generator.
class Random {
public:
/// C'tor
Random(unsigned _seed):Seed(_seed) {}
/// Return a random integer, up to a
/// maximum of 2**19 - 1.
uint32_t Rand() {
uint32_t Val = Seed + 0x000b07a1;
Seed = (Val * 0x3c7c0ac1);
// Only lowest 19 bits are random-ish.
return Seed & 0x7ffff;
}
/// Return a random 32 bit integer.
uint32_t Rand32() {
uint32_t Val = Rand();
Val &= 0xffff;
return Val | (Rand() << 16);
}
/// Return a random 64 bit integer.
uint64_t Rand64() {
uint64_t Val = Rand32();
return Val | (uint64_t(Rand32()) << 32);
}
/// Rand operator for STL algorithms.
ptrdiff_t operator()(ptrdiff_t y) {
return Rand64() % y;
}
/// Make this like a C++11 random device
typedef uint32_t result_type;
uint32_t operator()() { return Rand32(); }
static constexpr result_type min() { return 0; }
static constexpr result_type max() { return 0x7ffff; }
private:
unsigned Seed;
};
/// Generate an empty function with a default argument list.
Function *GenEmptyFunction(Module *M) {
// Define a few arguments
LLVMContext &Context = M->getContext();
Type* ArgsTy[] = {
Type::getInt8PtrTy(Context),
Type::getInt32PtrTy(Context),
Type::getInt64PtrTy(Context),
Type::getInt32Ty(Context),
Type::getInt64Ty(Context),
Type::getInt8Ty(Context)
};
auto *FuncTy = FunctionType::get(Type::getVoidTy(Context), ArgsTy, false);
// Pick a unique name to describe the input parameters
Twine Name = "autogen_SD" + Twine{SeedCL};
auto *Func = Function::Create(FuncTy, GlobalValue::ExternalLinkage, Name, M);
Func->setCallingConv(CallingConv::C);
return Func;
}
/// A base class, implementing utilities needed for
/// modifying and adding new random instructions.
struct Modifier {
/// Used to store the randomly generated values.
typedef std::vector<Value*> PieceTable;
public:
/// C'tor
Modifier(BasicBlock *Block, PieceTable *PT, Random *R):
BB(Block),PT(PT),Ran(R),Context(BB->getContext()) {}
/// virtual D'tor to silence warnings.
virtual ~Modifier() {}
/// Add a new instruction.
virtual void Act() = 0;
/// Add N new instructions,
virtual void ActN(unsigned n) {
for (unsigned i=0; i<n; ++i)
Act();
}
protected:
/// Return a random value from the list of known values.
Value *getRandomVal() {
assert(PT->size());
return PT->at(Ran->Rand() % PT->size());
}
Constant *getRandomConstant(Type *Tp) {
if (Tp->isIntegerTy()) {
if (Ran->Rand() & 1)
return ConstantInt::getAllOnesValue(Tp);
return ConstantInt::getNullValue(Tp);
} else if (Tp->isFloatingPointTy()) {
if (Ran->Rand() & 1)
return ConstantFP::getAllOnesValue(Tp);
return ConstantFP::getNullValue(Tp);
}
return UndefValue::get(Tp);
}
/// Return a random value with a known type.
Value *getRandomValue(Type *Tp) {
unsigned index = Ran->Rand();
for (unsigned i=0; i<PT->size(); ++i) {
Value *V = PT->at((index + i) % PT->size());
if (V->getType() == Tp)
return V;
}
// If the requested type was not found, generate a constant value.
if (Tp->isIntegerTy()) {
if (Ran->Rand() & 1)
return ConstantInt::getAllOnesValue(Tp);
return ConstantInt::getNullValue(Tp);
} else if (Tp->isFloatingPointTy()) {
if (Ran->Rand() & 1)
return ConstantFP::getAllOnesValue(Tp);
return ConstantFP::getNullValue(Tp);
} else if (Tp->isVectorTy()) {
VectorType *VTp = cast<VectorType>(Tp);
std::vector<Constant*> TempValues;
TempValues.reserve(VTp->getNumElements());
for (unsigned i = 0; i < VTp->getNumElements(); ++i)
TempValues.push_back(getRandomConstant(VTp->getScalarType()));
ArrayRef<Constant*> VectorValue(TempValues);
return ConstantVector::get(VectorValue);
}
return UndefValue::get(Tp);
}
/// Return a random value of any pointer type.
Value *getRandomPointerValue() {
unsigned index = Ran->Rand();
for (unsigned i=0; i<PT->size(); ++i) {
Value *V = PT->at((index + i) % PT->size());
if (V->getType()->isPointerTy())
return V;
}
return UndefValue::get(pickPointerType());
}
/// Return a random value of any vector type.
Value *getRandomVectorValue() {
unsigned index = Ran->Rand();
for (unsigned i=0; i<PT->size(); ++i) {
Value *V = PT->at((index + i) % PT->size());
if (V->getType()->isVectorTy())
return V;
}
return UndefValue::get(pickVectorType());
}
/// Pick a random type.
Type *pickType() {
return (Ran->Rand() & 1 ? pickVectorType() : pickScalarType());
}
/// Pick a random pointer type.
Type *pickPointerType() {
Type *Ty = pickType();
return PointerType::get(Ty, 0);
}
/// Pick a random vector type.
Type *pickVectorType(unsigned len = (unsigned)-1) {
// Pick a random vector width in the range 2**0 to 2**4.
// by adding two randoms we are generating a normal-like distribution
// around 2**3.
unsigned width = 1<<((Ran->Rand() % 3) + (Ran->Rand() % 3));
Type *Ty;
// Vectors of x86mmx are illegal; keep trying till we get something else.
do {
Ty = pickScalarType();
} while (Ty->isX86_MMXTy());
if (len != (unsigned)-1)
width = len;
return VectorType::get(Ty, width);
}
/// Pick a random scalar type.
Type *pickScalarType() {
static std::vector<Type*> ScalarTypes;
if (ScalarTypes.empty()) {
ScalarTypes.assign({
Type::getInt1Ty(Context),
Type::getInt8Ty(Context),
Type::getInt16Ty(Context),
Type::getInt32Ty(Context),
Type::getInt64Ty(Context),
Type::getFloatTy(Context),
Type::getDoubleTy(Context)
});
ScalarTypes.insert(ScalarTypes.end(),
AdditionalScalarTypes.begin(), AdditionalScalarTypes.end());
}
return ScalarTypes[Ran->Rand() % ScalarTypes.size()];
}
/// Basic block to populate
BasicBlock *BB;
/// Value table
PieceTable *PT;
/// Random number generator
Random *Ran;
/// Context
LLVMContext &Context;
};
struct LoadModifier: public Modifier {
LoadModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
void Act() override {
// Try to use predefined pointers. If non-exist, use undef pointer value;
Value *Ptr = getRandomPointerValue();
Value *V = new LoadInst(Ptr, "L", BB->getTerminator());
PT->push_back(V);
}
};
struct StoreModifier: public Modifier {
StoreModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
void Act() override {
// Try to use predefined pointers. If non-exist, use undef pointer value;
Value *Ptr = getRandomPointerValue();
Type *Tp = Ptr->getType();
Value *Val = getRandomValue(Tp->getContainedType(0));
Type *ValTy = Val->getType();
// Do not store vectors of i1s because they are unsupported
// by the codegen.
if (ValTy->isVectorTy() && ValTy->getScalarSizeInBits() == 1)
return;
new StoreInst(Val, Ptr, BB->getTerminator());
}
};
struct BinModifier: public Modifier {
BinModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
void Act() override {
Value *Val0 = getRandomVal();
Value *Val1 = getRandomValue(Val0->getType());
// Don't handle pointer types.
if (Val0->getType()->isPointerTy() ||
Val1->getType()->isPointerTy())
return;
// Don't handle i1 types.
if (Val0->getType()->getScalarSizeInBits() == 1)
return;
bool isFloat = Val0->getType()->getScalarType()->isFloatingPointTy();
Instruction* Term = BB->getTerminator();
unsigned R = Ran->Rand() % (isFloat ? 7 : 13);
Instruction::BinaryOps Op;
switch (R) {
default: llvm_unreachable("Invalid BinOp");
case 0:{Op = (isFloat?Instruction::FAdd : Instruction::Add); break; }
case 1:{Op = (isFloat?Instruction::FSub : Instruction::Sub); break; }
case 2:{Op = (isFloat?Instruction::FMul : Instruction::Mul); break; }
case 3:{Op = (isFloat?Instruction::FDiv : Instruction::SDiv); break; }
case 4:{Op = (isFloat?Instruction::FDiv : Instruction::UDiv); break; }
case 5:{Op = (isFloat?Instruction::FRem : Instruction::SRem); break; }
case 6:{Op = (isFloat?Instruction::FRem : Instruction::URem); break; }
case 7: {Op = Instruction::Shl; break; }
case 8: {Op = Instruction::LShr; break; }
case 9: {Op = Instruction::AShr; break; }
case 10:{Op = Instruction::And; break; }
case 11:{Op = Instruction::Or; break; }
case 12:{Op = Instruction::Xor; break; }
}
PT->push_back(BinaryOperator::Create(Op, Val0, Val1, "B", Term));
}
};
/// Generate constant values.
struct ConstModifier: public Modifier {
ConstModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
void Act() override {
Type *Ty = pickType();
if (Ty->isVectorTy()) {
switch (Ran->Rand() % 2) {
case 0: if (Ty->getScalarType()->isIntegerTy())
return PT->push_back(ConstantVector::getAllOnesValue(Ty));
break;
case 1: if (Ty->getScalarType()->isIntegerTy())
return PT->push_back(ConstantVector::getNullValue(Ty));
}
}
if (Ty->isFloatingPointTy()) {
// Generate 128 random bits, the size of the (currently)
// largest floating-point types.
uint64_t RandomBits[2];
for (unsigned i = 0; i < 2; ++i)
RandomBits[i] = Ran->Rand64();
APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits));
APFloat RandomFloat(Ty->getFltSemantics(), RandomInt);
if (Ran->Rand() & 1)
return PT->push_back(ConstantFP::getNullValue(Ty));
return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
}
if (Ty->isIntegerTy()) {
switch (Ran->Rand() % 7) {
case 0: if (Ty->isIntegerTy())
return PT->push_back(ConstantInt::get(Ty,
APInt::getAllOnesValue(Ty->getPrimitiveSizeInBits())));
break;
case 1: if (Ty->isIntegerTy())
return PT->push_back(ConstantInt::get(Ty,
APInt::getNullValue(Ty->getPrimitiveSizeInBits())));
break;
case 2: case 3: case 4: case 5:
case 6: if (Ty->isIntegerTy())
PT->push_back(ConstantInt::get(Ty, Ran->Rand()));
}
}
}
};
struct AllocaModifier: public Modifier {
AllocaModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R){}
void Act() override {
Type *Tp = pickType();
PT->push_back(new AllocaInst(Tp, "A", BB->getFirstNonPHI()));
}
};
struct ExtractElementModifier: public Modifier {
ExtractElementModifier(BasicBlock *BB, PieceTable *PT, Random *R):
Modifier(BB, PT, R) {}
void Act() override {
Value *Val0 = getRandomVectorValue();
Value *V = ExtractElementInst::Create(Val0,
ConstantInt::get(Type::getInt32Ty(BB->getContext()),
Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
"E", BB->getTerminator());
return PT->push_back(V);
}
};
struct ShuffModifier: public Modifier {
ShuffModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
void Act() override {
Value *Val0 = getRandomVectorValue();
Value *Val1 = getRandomValue(Val0->getType());
unsigned Width = cast<VectorType>(Val0->getType())->getNumElements();
std::vector<Constant*> Idxs;
Type *I32 = Type::getInt32Ty(BB->getContext());
for (unsigned i=0; i<Width; ++i) {
Constant *CI = ConstantInt::get(I32, Ran->Rand() % (Width*2));
// Pick some undef values.
if (!(Ran->Rand() % 5))
CI = UndefValue::get(I32);
Idxs.push_back(CI);
}
Constant *Mask = ConstantVector::get(Idxs);
Value *V = new ShuffleVectorInst(Val0, Val1, Mask, "Shuff",
BB->getTerminator());
PT->push_back(V);
}
};
struct InsertElementModifier: public Modifier {
InsertElementModifier(BasicBlock *BB, PieceTable *PT, Random *R):
Modifier(BB, PT, R) {}
void Act() override {
Value *Val0 = getRandomVectorValue();
Value *Val1 = getRandomValue(Val0->getType()->getScalarType());
Value *V = InsertElementInst::Create(Val0, Val1,
ConstantInt::get(Type::getInt32Ty(BB->getContext()),
Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
"I", BB->getTerminator());
return PT->push_back(V);
}
};
struct CastModifier: public Modifier {
CastModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
void Act() override {
Value *V = getRandomVal();
Type *VTy = V->getType();
Type *DestTy = pickScalarType();
// Handle vector casts vectors.
if (VTy->isVectorTy()) {
VectorType *VecTy = cast<VectorType>(VTy);
DestTy = pickVectorType(VecTy->getNumElements());
}
// no need to cast.
if (VTy == DestTy) return;
// Pointers:
if (VTy->isPointerTy()) {
if (!DestTy->isPointerTy())
DestTy = PointerType::get(DestTy, 0);
return PT->push_back(
new BitCastInst(V, DestTy, "PC", BB->getTerminator()));
}
unsigned VSize = VTy->getScalarType()->getPrimitiveSizeInBits();
unsigned DestSize = DestTy->getScalarType()->getPrimitiveSizeInBits();
// Generate lots of bitcasts.
if ((Ran->Rand() & 1) && VSize == DestSize) {
return PT->push_back(
new BitCastInst(V, DestTy, "BC", BB->getTerminator()));
}
// Both types are integers:
if (VTy->getScalarType()->isIntegerTy() &&
DestTy->getScalarType()->isIntegerTy()) {
if (VSize > DestSize) {
return PT->push_back(
new TruncInst(V, DestTy, "Tr", BB->getTerminator()));
} else {
assert(VSize < DestSize && "Different int types with the same size?");
if (Ran->Rand() & 1)
return PT->push_back(
new ZExtInst(V, DestTy, "ZE", BB->getTerminator()));
return PT->push_back(new SExtInst(V, DestTy, "Se", BB->getTerminator()));
}
}
// Fp to int.
if (VTy->getScalarType()->isFloatingPointTy() &&
DestTy->getScalarType()->isIntegerTy()) {
if (Ran->Rand() & 1)
return PT->push_back(
new FPToSIInst(V, DestTy, "FC", BB->getTerminator()));
return PT->push_back(new FPToUIInst(V, DestTy, "FC", BB->getTerminator()));
}
// Int to fp.
if (VTy->getScalarType()->isIntegerTy() &&
DestTy->getScalarType()->isFloatingPointTy()) {
if (Ran->Rand() & 1)
return PT->push_back(
new SIToFPInst(V, DestTy, "FC", BB->getTerminator()));
return PT->push_back(new UIToFPInst(V, DestTy, "FC", BB->getTerminator()));
}
// Both floats.
if (VTy->getScalarType()->isFloatingPointTy() &&
DestTy->getScalarType()->isFloatingPointTy()) {
if (VSize > DestSize) {
return PT->push_back(
new FPTruncInst(V, DestTy, "Tr", BB->getTerminator()));
} else if (VSize < DestSize) {
return PT->push_back(
new FPExtInst(V, DestTy, "ZE", BB->getTerminator()));
}
// If VSize == DestSize, then the two types must be fp128 and ppc_fp128,
// for which there is no defined conversion. So do nothing.
}
}
};
struct SelectModifier: public Modifier {
SelectModifier(BasicBlock *BB, PieceTable *PT, Random *R):
Modifier(BB, PT, R) {}
void Act() override {
// Try a bunch of different select configuration until a valid one is found.
Value *Val0 = getRandomVal();
Value *Val1 = getRandomValue(Val0->getType());
Type *CondTy = Type::getInt1Ty(Context);
// If the value type is a vector, and we allow vector select, then in 50%
// of the cases generate a vector select.
if (Val0->getType()->isVectorTy() && (Ran->Rand() % 1)) {
unsigned NumElem = cast<VectorType>(Val0->getType())->getNumElements();
CondTy = VectorType::get(CondTy, NumElem);
}
Value *Cond = getRandomValue(CondTy);
Value *V = SelectInst::Create(Cond, Val0, Val1, "Sl", BB->getTerminator());
return PT->push_back(V);
}
};
struct CmpModifier: public Modifier {
CmpModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
void Act() override {
Value *Val0 = getRandomVal();
Value *Val1 = getRandomValue(Val0->getType());
if (Val0->getType()->isPointerTy()) return;
bool fp = Val0->getType()->getScalarType()->isFloatingPointTy();
int op;
if (fp) {
op = Ran->Rand() %
(CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE) +
CmpInst::FIRST_FCMP_PREDICATE;
} else {
op = Ran->Rand() %
(CmpInst::LAST_ICMP_PREDICATE - CmpInst::FIRST_ICMP_PREDICATE) +
CmpInst::FIRST_ICMP_PREDICATE;
}
Value *V = CmpInst::Create(fp ? Instruction::FCmp : Instruction::ICmp,
op, Val0, Val1, "Cmp", BB->getTerminator());
return PT->push_back(V);
}
};
} // end anonymous namespace
static void FillFunction(Function *F, Random &R) {
// Create a legal entry block.
BasicBlock *BB = BasicBlock::Create(F->getContext(), "BB", F);
ReturnInst::Create(F->getContext(), BB);
// Create the value table.
Modifier::PieceTable PT;
// Consider arguments as legal values.
for (auto &arg : F->args())
PT.push_back(&arg);
// List of modifiers which add new random instructions.
std::vector<std::unique_ptr<Modifier>> Modifiers;
Modifiers.emplace_back(new LoadModifier(BB, &PT, &R));
Modifiers.emplace_back(new StoreModifier(BB, &PT, &R));
auto SM = Modifiers.back().get();
Modifiers.emplace_back(new ExtractElementModifier(BB, &PT, &R));
Modifiers.emplace_back(new ShuffModifier(BB, &PT, &R));
Modifiers.emplace_back(new InsertElementModifier(BB, &PT, &R));
Modifiers.emplace_back(new BinModifier(BB, &PT, &R));
Modifiers.emplace_back(new CastModifier(BB, &PT, &R));
Modifiers.emplace_back(new SelectModifier(BB, &PT, &R));
Modifiers.emplace_back(new CmpModifier(BB, &PT, &R));
// Generate the random instructions
AllocaModifier{BB, &PT, &R}.ActN(5); // Throw in a few allocas
ConstModifier{BB, &PT, &R}.ActN(40); // Throw in a few constants
for (unsigned i = 0; i < SizeCL / Modifiers.size(); ++i)
for (auto &Mod : Modifiers)
Mod->Act();
SM->ActN(5); // Throw in a few stores.
}
static void IntroduceControlFlow(Function *F, Random &R) {
std::vector<Instruction*> BoolInst;
for (auto &Instr : F->front()) {
if (Instr.getType() == IntegerType::getInt1Ty(F->getContext()))
BoolInst.push_back(&Instr);
}
std::shuffle(BoolInst.begin(), BoolInst.end(), R);
for (auto *Instr : BoolInst) {
BasicBlock *Curr = Instr->getParent();
BasicBlock::iterator Loc = Instr;
BasicBlock *Next = Curr->splitBasicBlock(Loc, "CF");
Instr->moveBefore(Curr->getTerminator());
if (Curr != &F->getEntryBlock()) {
BranchInst::Create(Curr, Next, Instr, Curr->getTerminator());
Curr->getTerminator()->eraseFromParent();
}
}
}
}
int __cdecl main(int argc, char **argv) { // HLSL Change - __cdecl
using namespace llvm;
// Init LLVM, call llvm_shutdown() on exit, parse args, etc.
PrettyStackTraceProgram X(argc, argv);
cl::ParseCommandLineOptions(argc, argv, "llvm codegen stress-tester\n");
llvm_shutdown_obj Y;
auto M = make_unique<Module>("/tmp/autogen.bc", getGlobalContext());
Function *F = GenEmptyFunction(M.get());
// Pick an initial seed value
Random R(SeedCL);
// Generate lots of random instructions inside a single basic block.
FillFunction(F, R);
// Break the basic block into many loops.
IntroduceControlFlow(F, R);
// Figure out what stream we are supposed to write to...
std::unique_ptr<tool_output_file> Out;
// Default to standard output.
if (OutputFilename.empty())
OutputFilename = "-";
std::error_code EC;
Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
legacy::PassManager Passes;
Passes.add(createVerifierPass());
Passes.add(createPrintModulePass(Out->os()));
Passes.run(*M.get());
Out->keep();
return 0;
}
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/verify-uselistorder/verify-uselistorder.cpp
|
//===- verify-uselistorder.cpp - The LLVM Modular Optimizer ---------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Verify that use-list order can be serialized correctly. After reading the
// provided IR, this tool shuffles the use-lists and then writes and reads to a
// separate Module whose use-list orders are compared to the original.
//
// The shuffles are deterministic, but guarantee that use-lists will change.
// The algorithm per iteration is as follows:
//
// 1. Seed the random number generator. The seed is different for each
// shuffle. Shuffle 0 uses default+0, shuffle 1 uses default+1, and so on.
//
// 2. Visit every Value in a deterministic order.
//
// 3. Assign a random number to each Use in the Value's use-list in order.
//
// 4. If the numbers are already in order, reassign numbers until they aren't.
//
// 5. Sort the use-list using Value::sortUseList(), which is a stable sort.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/UseListOrder.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/SystemUtils.h"
#include "llvm/Support/raw_ostream.h"
#include <random>
#include <vector>
using namespace llvm;
#define DEBUG_TYPE "uselistorder"
static cl::opt<std::string> InputFilename(cl::Positional,
cl::desc("<input bitcode file>"),
cl::init("-"),
cl::value_desc("filename"));
static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temp files"),
cl::init(false));
static cl::opt<unsigned>
NumShuffles("num-shuffles",
cl::desc("Number of times to shuffle and verify use-lists"),
cl::init(1));
namespace {
struct TempFile {
std::string Filename;
FileRemover Remover;
bool init(const std::string &Ext);
bool writeBitcode(const Module &M) const;
bool writeAssembly(const Module &M) const;
std::unique_ptr<Module> readBitcode(LLVMContext &Context) const;
std::unique_ptr<Module> readAssembly(LLVMContext &Context) const;
};
struct ValueMapping {
DenseMap<const Value *, unsigned> IDs;
std::vector<const Value *> Values;
/// \brief Construct a value mapping for module.
///
/// Creates mapping from every value in \c M to an ID. This mapping includes
/// un-referencable values.
///
/// Every \a Value that gets serialized in some way should be represented
/// here. The order needs to be deterministic, but it's unnecessary to match
/// the value-ids in the bitcode writer.
///
/// All constants that are referenced by other values are included in the
/// mapping, but others -- which wouldn't be serialized -- are not.
ValueMapping(const Module &M);
/// \brief Map a value.
///
/// Maps a value. If it's a constant, maps all of its operands first.
void map(const Value *V);
unsigned lookup(const Value *V) const { return IDs.lookup(V); }
};
} // end namespace
bool TempFile::init(const std::string &Ext) {
SmallVector<char, 64> Vector;
DEBUG(dbgs() << " - create-temp-file\n");
if (auto EC = sys::fs::createTemporaryFile("uselistorder", Ext, Vector)) {
errs() << "verify-uselistorder: error: " << EC.message() << "\n";
return true;
}
assert(!Vector.empty());
Filename.assign(Vector.data(), Vector.data() + Vector.size());
Remover.setFile(Filename, !SaveTemps);
if (SaveTemps)
outs() << " - filename = " << Filename << "\n";
return false;
}
bool TempFile::writeBitcode(const Module &M) const {
DEBUG(dbgs() << " - write bitcode\n");
std::error_code EC;
raw_fd_ostream OS(Filename, EC, sys::fs::F_None);
if (EC) {
errs() << "verify-uselistorder: error: " << EC.message() << "\n";
return true;
}
WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
return false;
}
bool TempFile::writeAssembly(const Module &M) const {
DEBUG(dbgs() << " - write assembly\n");
std::error_code EC;
raw_fd_ostream OS(Filename, EC, sys::fs::F_Text);
if (EC) {
errs() << "verify-uselistorder: error: " << EC.message() << "\n";
return true;
}
M.print(OS, nullptr, /* ShouldPreserveUseListOrder */ true);
return false;
}
std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
DEBUG(dbgs() << " - read bitcode\n");
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOr =
MemoryBuffer::getFile(Filename);
if (!BufferOr) {
errs() << "verify-uselistorder: error: " << BufferOr.getError().message()
<< "\n";
return nullptr;
}
MemoryBuffer *Buffer = BufferOr.get().get();
ErrorOr<std::unique_ptr<Module>> ModuleOr =
parseBitcodeFile(Buffer->getMemBufferRef(), Context);
if (!ModuleOr) {
errs() << "verify-uselistorder: error: " << ModuleOr.getError().message()
<< "\n";
return nullptr;
}
return std::move(ModuleOr.get());
}
std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
DEBUG(dbgs() << " - read assembly\n");
SMDiagnostic Err;
std::unique_ptr<Module> M = parseAssemblyFile(Filename, Err, Context);
if (!M.get())
Err.print("verify-uselistorder", errs());
return M;
}
ValueMapping::ValueMapping(const Module &M) {
// Every value should be mapped, including things like void instructions and
// basic blocks that are kept out of the ValueEnumerator.
//
// The current mapping order makes it easier to debug the tables. It happens
// to be similar to the ID mapping when writing ValueEnumerator, but they
// aren't (and needn't be) in sync.
// Globals.
for (const GlobalVariable &G : M.globals())
map(&G);
for (const GlobalAlias &A : M.aliases())
map(&A);
for (const Function &F : M)
map(&F);
// Constants used by globals.
for (const GlobalVariable &G : M.globals())
if (G.hasInitializer())
map(G.getInitializer());
for (const GlobalAlias &A : M.aliases())
map(A.getAliasee());
for (const Function &F : M) {
if (F.hasPrefixData())
map(F.getPrefixData());
if (F.hasPrologueData())
map(F.getPrologueData());
if (F.hasPersonalityFn())
map(F.getPersonalityFn());
}
// Function bodies.
for (const Function &F : M) {
for (const Argument &A : F.args())
map(&A);
for (const BasicBlock &BB : F)
map(&BB);
for (const BasicBlock &BB : F)
for (const Instruction &I : BB)
map(&I);
// Constants used by instructions.
for (const BasicBlock &BB : F)
for (const Instruction &I : BB)
for (const Value *Op : I.operands())
if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
isa<InlineAsm>(Op))
map(Op);
}
}
void ValueMapping::map(const Value *V) {
if (IDs.lookup(V))
return;
if (auto *C = dyn_cast<Constant>(V))
if (!isa<GlobalValue>(C))
for (const Value *Op : C->operands())
map(Op);
Values.push_back(V);
IDs[V] = Values.size();
}
#ifndef NDEBUG
static void dumpMapping(const ValueMapping &VM) {
dbgs() << "value-mapping (size = " << VM.Values.size() << "):\n";
for (unsigned I = 0, E = VM.Values.size(); I != E; ++I) {
dbgs() << " - id = " << I << ", value = ";
VM.Values[I]->dump();
}
}
static void debugValue(const ValueMapping &M, unsigned I, StringRef Desc) {
const Value *V = M.Values[I];
dbgs() << " - " << Desc << " value = ";
V->dump();
for (const Use &U : V->uses()) {
dbgs() << " => use: op = " << U.getOperandNo()
<< ", user-id = " << M.IDs.lookup(U.getUser()) << ", user = ";
U.getUser()->dump();
}
}
static void debugUserMismatch(const ValueMapping &L, const ValueMapping &R,
unsigned I) {
dbgs() << " - fail: user mismatch: ID = " << I << "\n";
debugValue(L, I, "LHS");
debugValue(R, I, "RHS");
dbgs() << "\nlhs-";
dumpMapping(L);
dbgs() << "\nrhs-";
dumpMapping(R);
}
static void debugSizeMismatch(const ValueMapping &L, const ValueMapping &R) {
dbgs() << " - fail: map size: " << L.Values.size()
<< " != " << R.Values.size() << "\n";
dbgs() << "\nlhs-";
dumpMapping(L);
dbgs() << "\nrhs-";
dumpMapping(R);
}
#endif
static bool matches(const ValueMapping &LM, const ValueMapping &RM) {
DEBUG(dbgs() << "compare value maps\n");
if (LM.Values.size() != RM.Values.size()) {
DEBUG(debugSizeMismatch(LM, RM));
return false;
}
// This mapping doesn't include dangling constant users, since those don't
// get serialized. However, checking if users are constant and calling
// isConstantUsed() on every one is very expensive. Instead, just check if
// the user is mapped.
auto skipUnmappedUsers =
[&](Value::const_use_iterator &U, Value::const_use_iterator E,
const ValueMapping &M) {
while (U != E && !M.lookup(U->getUser()))
++U;
};
// Iterate through all values, and check that both mappings have the same
// users.
for (unsigned I = 0, E = LM.Values.size(); I != E; ++I) {
const Value *L = LM.Values[I];
const Value *R = RM.Values[I];
auto LU = L->use_begin(), LE = L->use_end();
auto RU = R->use_begin(), RE = R->use_end();
skipUnmappedUsers(LU, LE, LM);
skipUnmappedUsers(RU, RE, RM);
while (LU != LE) {
if (RU == RE) {
DEBUG(debugUserMismatch(LM, RM, I));
return false;
}
if (LM.lookup(LU->getUser()) != RM.lookup(RU->getUser())) {
DEBUG(debugUserMismatch(LM, RM, I));
return false;
}
if (LU->getOperandNo() != RU->getOperandNo()) {
DEBUG(debugUserMismatch(LM, RM, I));
return false;
}
skipUnmappedUsers(++LU, LE, LM);
skipUnmappedUsers(++RU, RE, RM);
}
if (RU != RE) {
DEBUG(debugUserMismatch(LM, RM, I));
return false;
}
}
return true;
}
static void verifyAfterRoundTrip(const Module &M,
std::unique_ptr<Module> OtherM) {
if (!OtherM)
report_fatal_error("parsing failed");
if (verifyModule(*OtherM, &errs()))
report_fatal_error("verification failed");
if (!matches(ValueMapping(M), ValueMapping(*OtherM)))
report_fatal_error("use-list order changed");
}
static void verifyBitcodeUseListOrder(const Module &M) {
TempFile F;
if (F.init("bc"))
report_fatal_error("failed to initialize bitcode file");
if (F.writeBitcode(M))
report_fatal_error("failed to write bitcode");
LLVMContext Context;
verifyAfterRoundTrip(M, F.readBitcode(Context));
}
static void verifyAssemblyUseListOrder(const Module &M) {
TempFile F;
if (F.init("ll"))
report_fatal_error("failed to initialize assembly file");
if (F.writeAssembly(M))
report_fatal_error("failed to write assembly");
LLVMContext Context;
verifyAfterRoundTrip(M, F.readAssembly(Context));
}
static void verifyUseListOrder(const Module &M) {
outs() << "verify bitcode\n";
verifyBitcodeUseListOrder(M);
outs() << "verify assembly\n";
verifyAssemblyUseListOrder(M);
}
static void shuffleValueUseLists(Value *V, std::minstd_rand0 &Gen,
DenseSet<Value *> &Seen) {
if (!Seen.insert(V).second)
return;
if (auto *C = dyn_cast<Constant>(V))
if (!isa<GlobalValue>(C))
for (Value *Op : C->operands())
shuffleValueUseLists(Op, Gen, Seen);
if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
// Nothing to shuffle for 0 or 1 users.
return;
// Generate random numbers between 10 and 99, which will line up nicely in
// debug output. We're not worried about collisons here.
DEBUG(dbgs() << "V = "; V->dump());
std::uniform_int_distribution<short> Dist(10, 99);
SmallDenseMap<const Use *, short, 16> Order;
auto compareUses =
[&Order](const Use &L, const Use &R) { return Order[&L] < Order[&R]; };
do {
for (const Use &U : V->uses()) {
auto I = Dist(Gen);
Order[&U] = I;
DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo()
<< ", U = ";
U.getUser()->dump());
}
} while (std::is_sorted(V->use_begin(), V->use_end(), compareUses));
DEBUG(dbgs() << " => shuffle\n");
V->sortUseList(compareUses);
DEBUG({
for (const Use &U : V->uses()) {
dbgs() << " - order: " << Order.lookup(&U)
<< ", op = " << U.getOperandNo() << ", U = ";
U.getUser()->dump();
}
});
}
static void reverseValueUseLists(Value *V, DenseSet<Value *> &Seen) {
if (!Seen.insert(V).second)
return;
if (auto *C = dyn_cast<Constant>(V))
if (!isa<GlobalValue>(C))
for (Value *Op : C->operands())
reverseValueUseLists(Op, Seen);
if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
// Nothing to shuffle for 0 or 1 users.
return;
DEBUG({
dbgs() << "V = ";
V->dump();
for (const Use &U : V->uses()) {
dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
U.getUser()->dump();
}
dbgs() << " => reverse\n";
});
V->reverseUseList();
DEBUG({
for (const Use &U : V->uses()) {
dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
U.getUser()->dump();
}
});
}
template <class Changer>
static void changeUseLists(Module &M, Changer changeValueUseList) {
// Visit every value that would be serialized to an IR file.
//
// Globals.
for (GlobalVariable &G : M.globals())
changeValueUseList(&G);
for (GlobalAlias &A : M.aliases())
changeValueUseList(&A);
for (Function &F : M)
changeValueUseList(&F);
// Constants used by globals.
for (GlobalVariable &G : M.globals())
if (G.hasInitializer())
changeValueUseList(G.getInitializer());
for (GlobalAlias &A : M.aliases())
changeValueUseList(A.getAliasee());
for (Function &F : M) {
if (F.hasPrefixData())
changeValueUseList(F.getPrefixData());
if (F.hasPrologueData())
changeValueUseList(F.getPrologueData());
if (F.hasPersonalityFn())
changeValueUseList(F.getPersonalityFn());
}
// Function bodies.
for (Function &F : M) {
for (Argument &A : F.args())
changeValueUseList(&A);
for (BasicBlock &BB : F)
changeValueUseList(&BB);
for (BasicBlock &BB : F)
for (Instruction &I : BB)
changeValueUseList(&I);
// Constants used by instructions.
for (BasicBlock &BB : F)
for (Instruction &I : BB)
for (Value *Op : I.operands())
if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
isa<InlineAsm>(Op))
changeValueUseList(Op);
}
if (verifyModule(M, &errs()))
report_fatal_error("verification failed");
}
static void shuffleUseLists(Module &M, unsigned SeedOffset) {
std::minstd_rand0 Gen(std::minstd_rand0::default_seed + SeedOffset);
DenseSet<Value *> Seen;
changeUseLists(M, [&](Value *V) { shuffleValueUseLists(V, Gen, Seen); });
DEBUG(dbgs() << "\n");
}
static void reverseUseLists(Module &M) {
DenseSet<Value *> Seen;
changeUseLists(M, [&](Value *V) { reverseValueUseLists(V, Seen); });
DEBUG(dbgs() << "\n");
}
int main(int argc, char **argv) {
sys::PrintStackTraceOnErrorSignal();
llvm::PrettyStackTraceProgram X(argc, argv);
// Enable debug stream buffering.
EnableDebugBuffering = true;
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
LLVMContext &Context = getGlobalContext();
cl::ParseCommandLineOptions(argc, argv,
"llvm tool to verify use-list order\n");
SMDiagnostic Err;
// Load the input module...
std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
if (!M.get()) {
Err.print(argv[0], errs());
return 1;
}
if (verifyModule(*M, &errs())) {
errs() << argv[0] << ": " << InputFilename
<< ": error: input module is broken!\n";
return 1;
}
// Verify the use lists now and after reversing them.
outs() << "*** verify-uselistorder ***\n";
verifyUseListOrder(*M);
outs() << "reverse\n";
reverseUseLists(*M);
verifyUseListOrder(*M);
for (unsigned I = 0, E = NumShuffles; I != E; ++I) {
outs() << "\n";
// Shuffle with a different (deterministic) seed each time.
outs() << "shuffle (" << I + 1 << " of " << E << ")\n";
shuffleUseLists(*M, I);
// Verify again before and after reversing.
verifyUseListOrder(*M);
outs() << "reverse\n";
reverseUseLists(*M);
verifyUseListOrder(*M);
}
return 0;
}
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/verify-uselistorder/CMakeLists.txt
|
set(LLVM_LINK_COMPONENTS
AsmParser
BitReader
BitWriter
Core
IRReader
Support
)
add_llvm_tool(verify-uselistorder
verify-uselistorder.cpp
)
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/verify-uselistorder/LLVMBuild.txt
|
;===- ./tools/verify-uselistorder/LLVMBuild.txt ----------------*- Conf -*--===;
;
; The LLVM Compiler Infrastructure
;
; This file is distributed under the University of Illinois Open Source
; License. See LICENSE.TXT for details.
;
;===------------------------------------------------------------------------===;
;
; This is an LLVMBuild description file for the components in this subdirectory.
;
; For more information on the LLVMBuild system, please see:
;
; http://llvm.org/docs/LLVMBuild.html
;
;===------------------------------------------------------------------------===;
[component_0]
type = Tool
name = verify-uselistorder
parent = Tools
required_libraries = IRReader BitWriter Support
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/llvm-mcmarkup/CMakeLists.txt
|
set(LLVM_LINK_COMPONENTS support)
add_llvm_tool(llvm-mcmarkup
llvm-mcmarkup.cpp
)
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/llvm-mcmarkup/LLVMBuild.txt
|
;===- ./tools/llvm-mcmarkup/LLVMBuild.txt ----------------------*- Conf -*--===;
;
; The LLVM Compiler Infrastructure
;
; This file is distributed under the University of Illinois Open Source
; License. See LICENSE.TXT for details.
;
;===------------------------------------------------------------------------===;
;
; This is an LLVMBuild description file for the components in this subdirectory.
;
; For more information on the LLVMBuild system, please see:
;
; http://llvm.org/docs/LLVMBuild.html
;
;===------------------------------------------------------------------------===;
[component_0]
type = Tool
name = llvm-mcmarkup
parent = Tools
required_libraries = Support
|
0 |
repos/DirectXShaderCompiler/tools
|
repos/DirectXShaderCompiler/tools/llvm-mcmarkup/llvm-mcmarkup.cpp
|
//===-- llvm-mcmarkup.cpp - Parse the MC assembly markup tags -------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Example simple parser implementation for the MC assembly markup language.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include <system_error>
using namespace llvm;
static cl::list<std::string>
InputFilenames(cl::Positional, cl::desc("<input files>"),
cl::ZeroOrMore);
static cl::opt<bool>
DumpTags("dump-tags", cl::desc("List all tags encountered in input"));
static StringRef ToolName;
/// Trivial lexer for the markup parser. Input is always handled a character
/// at a time. The lexer just encapsulates EOF and lookahead handling.
class MarkupLexer {
StringRef::const_iterator Start;
StringRef::const_iterator CurPtr;
StringRef::const_iterator End;
public:
MarkupLexer(StringRef Source)
: Start(Source.begin()), CurPtr(Source.begin()), End(Source.end()) {}
// When processing non-markup, input is consumed a character at a time.
bool isEOF() { return CurPtr == End; }
int getNextChar() {
if (CurPtr == End) return EOF;
return *CurPtr++;
}
int peekNextChar() {
if (CurPtr == End) return EOF;
return *CurPtr;
}
StringRef::const_iterator getPosition() const { return CurPtr; }
};
/// A markup tag is a name and a (usually empty) list of modifiers.
class MarkupTag {
StringRef Name;
StringRef Modifiers;
SMLoc StartLoc;
public:
MarkupTag(StringRef n, StringRef m, SMLoc Loc)
: Name(n), Modifiers(m), StartLoc(Loc) {}
StringRef getName() const { return Name; }
StringRef getModifiers() const { return Modifiers; }
SMLoc getLoc() const { return StartLoc; }
};
/// A simple parser implementation for creating MarkupTags from input text.
class MarkupParser {
MarkupLexer &Lex;
SourceMgr &SM;
public:
MarkupParser(MarkupLexer &lex, SourceMgr &SrcMgr) : Lex(lex), SM(SrcMgr) {}
/// Create a MarkupTag from the current position in the MarkupLexer.
/// The parseTag() method should be called when the lexer has processed
/// the opening '<' character. Input will be consumed up to and including
/// the ':' which terminates the tag open.
MarkupTag parseTag();
/// Issue a diagnostic and terminate program execution.
void FatalError(SMLoc Loc, StringRef Msg);
};
void MarkupParser::FatalError(SMLoc Loc, StringRef Msg) {
SM.PrintMessage(Loc, SourceMgr::DK_Error, Msg);
exit(1);
}
// Example handler for when a tag is recognized.
static void processStartTag(MarkupTag &Tag) {
// If we're just printing the tags, do that, otherwise do some simple
// colorization.
if (DumpTags) {
outs() << Tag.getName();
if (Tag.getModifiers().size())
outs() << " " << Tag.getModifiers();
outs() << "\n";
return;
}
if (!outs().has_colors())
return;
// Color registers as red and immediates as cyan. Those don't have nested
// tags, so don't bother keeping a stack of colors to reset to.
if (Tag.getName() == "reg")
outs().changeColor(raw_ostream::RED);
else if (Tag.getName() == "imm")
outs().changeColor(raw_ostream::CYAN);
}
// Example handler for when the end of a tag is recognized.
static void processEndTag(MarkupTag &Tag) {
// If we're printing the tags, there's nothing more to do here. Otherwise,
// set the color back the normal.
if (DumpTags)
return;
if (!outs().has_colors())
return;
// Just reset to basic white.
outs().changeColor(raw_ostream::WHITE, false);
}
MarkupTag MarkupParser::parseTag() {
// First off, extract the tag into it's own StringRef so we can look at it
// outside of the context of consuming input.
StringRef::const_iterator Start = Lex.getPosition();
SMLoc Loc = SMLoc::getFromPointer(Start - 1);
while(Lex.getNextChar() != ':') {
// EOF is an error.
if (Lex.isEOF())
FatalError(SMLoc::getFromPointer(Start), "unterminated markup tag");
}
StringRef RawTag(Start, Lex.getPosition() - Start - 1);
std::pair<StringRef, StringRef> SplitTag = RawTag.split(' ');
return MarkupTag(SplitTag.first, SplitTag.second, Loc);
}
static void parseMCMarkup(StringRef Filename) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
MemoryBuffer::getFileOrSTDIN(Filename);
if (std::error_code EC = BufferPtr.getError()) {
errs() << ToolName << ": " << EC.message() << '\n';
return;
}
std::unique_ptr<MemoryBuffer> &Buffer = BufferPtr.get();
SourceMgr SrcMgr;
StringRef InputSource = Buffer->getBuffer();
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
MarkupLexer Lex(InputSource);
MarkupParser Parser(Lex, SrcMgr);
SmallVector<MarkupTag, 4> TagStack;
for (int CurChar = Lex.getNextChar();
CurChar != EOF;
CurChar = Lex.getNextChar()) {
switch (CurChar) {
case '<': {
// A "<<" is output as a literal '<' and does not start a markup tag.
if (Lex.peekNextChar() == '<') {
(void)Lex.getNextChar();
break;
}
// Parse the markup entry.
TagStack.push_back(Parser.parseTag());
// Do any special handling for the start of a tag.
processStartTag(TagStack.back());
continue;
}
case '>': {
SMLoc Loc = SMLoc::getFromPointer(Lex.getPosition() - 1);
// A ">>" is output as a literal '>' and does not end a markup tag.
if (Lex.peekNextChar() == '>') {
(void)Lex.getNextChar();
break;
}
// Close out the innermost tag.
if (TagStack.empty())
Parser.FatalError(Loc, "'>' without matching '<'");
// Do any special handling for the end of a tag.
processEndTag(TagStack.back());
TagStack.pop_back();
continue;
}
default:
break;
}
// For anything else, just echo the character back out.
if (!DumpTags && CurChar != EOF)
outs() << (char)CurChar;
}
// If there are any unterminated markup tags, issue diagnostics for them.
while (!TagStack.empty()) {
MarkupTag &Tag = TagStack.back();
SrcMgr.PrintMessage(Tag.getLoc(), SourceMgr::DK_Error,
"unterminated markup tag");
TagStack.pop_back();
}
}
// HLSL Change: changed calling convention to __cdecl
int __cdecl main(int argc, char **argv) {
// Print a stack trace if we signal out.
sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram X(argc, argv);
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
cl::ParseCommandLineOptions(argc, argv, "llvm MC markup parser\n");
ToolName = argv[0];
// If no input files specified, read from stdin.
if (InputFilenames.size() == 0)
InputFilenames.push_back("-");
std::for_each(InputFilenames.begin(), InputFilenames.end(),
parseMCMarkup);
return 0;
}
|
0 |
repos
|
repos/3bc-zig/README.md
|
# 3bc-zig
Raw 3BC bindings for Zig.
This zig binding is designed for testing interoperability between both languages.
## Whats is [3BC language](https://3bc-lang.org/)?
Low-level language, tiny virtual machine, intermediate representation, embeddable, easy for beginners.
## Requirements
- zig [v0.12 or higher](https://ziglang.org/download)
- git for download 3bc-lang dependency
## Acknowledgements
- **Rodrigo Dornelles** is author of 3BC language.
|
0 |
repos
|
repos/3bc-zig/GitRepoStep.zig
|
//! Publish Date: 2023_03_19
//! This file is hosted at github.com/marler8997/zig-build-repos and is meant to be copied
//! to projects that use it.
const std = @import("std");
const GitRepoStep = @This();
pub const ShaCheck = enum {
none,
warn,
err,
pub fn reportFail(self: ShaCheck, comptime fmt: []const u8, args: anytype) void {
switch (self) {
.none => unreachable,
.warn => std.log.warn(fmt, args),
.err => {
std.log.err(fmt, args);
std.os.exit(0xff);
},
}
}
};
step: std.build.Step,
url: []const u8,
name: []const u8,
branch: ?[]const u8 = null,
sha: []const u8,
path: []const u8,
sha_check: ShaCheck = .warn,
fetch_enabled: bool,
var cached_default_fetch_option: ?bool = null;
pub fn defaultFetchOption(b: *std.Build) bool {
if (cached_default_fetch_option) |_| {} else {
cached_default_fetch_option = if (b.option(bool, "fetch", "automatically fetch network resources")) |o| o else false;
}
return cached_default_fetch_option.?;
}
pub fn create(b: *std.Build, opt: struct {
url: []const u8,
branch: ?[]const u8 = null,
sha: []const u8,
path: ?[]const u8 = null,
sha_check: ShaCheck = .warn,
fetch_enabled: ?bool = null,
first_ret_addr: ?usize = null,
}) *GitRepoStep {
var result = b.allocator.create(GitRepoStep) catch @panic("memory");
const name = std.fs.path.basename(opt.url);
result.* = GitRepoStep{
.step = std.Build.Step.init(.{
.id = .custom,
.name = b.fmt("clone git repository '{s}'", .{name}),
.owner = b,
.makeFn = make,
.first_ret_addr = opt.first_ret_addr orelse @returnAddress(),
.max_rss = 0,
}),
.url = opt.url,
.name = name,
.branch = opt.branch,
.sha = opt.sha,
.path = if (opt.path) |p| b.allocator.dupe(u8, p) catch @panic("OOM") else b.pathFromRoot(b.pathJoin(&.{ "dep", name })),
.sha_check = opt.sha_check,
.fetch_enabled = if (opt.fetch_enabled) |fe| fe else defaultFetchOption(b),
};
return result;
}
// TODO: this should be included in std.build, it helps find bugs in build files
fn hasDependency(step: *const std.Build.Step, dep_candidate: *const std.Build.Step) bool {
for (step.dependencies.items) |dep| {
// TODO: should probably use step.loop_flag to prevent infinite recursion
// when a circular reference is encountered, or maybe keep track of
// the steps encounterd with a hash set
if (dep == dep_candidate or hasDependency(dep, dep_candidate))
return true;
}
return false;
}
fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void {
_ = prog_node;
const self = @fieldParentPtr(GitRepoStep, "step", step);
std.fs.accessAbsolute(self.path, .{}) catch {
const branch_args = if (self.branch) |b| &[2][]const u8{ " -b ", b } else &[2][]const u8{ "", "" };
if (!self.fetch_enabled) {
std.debug.print("Error: git repository '{s}' does not exist\n", .{self.path});
std.debug.print(" Use -Dfetch to download it automatically, or run the following to clone it:\n", .{});
std.debug.print(" git clone {s}{s}{s} {s} && git -C {3s} checkout {s} -b fordep\n", .{
self.url,
branch_args[0],
branch_args[1],
self.path,
self.sha,
});
std.os.exit(1);
}
{
var args = std.ArrayList([]const u8).init(self.step.owner.allocator);
defer args.deinit();
try args.append("git");
try args.append("clone");
try args.append(self.url);
// TODO: clone it to a temporary location in case of failure
// also, remove that temporary location before running
try args.append(self.path);
if (self.branch) |branch| {
try args.append("-b");
try args.append(branch);
}
try run(self.step.owner, args.items);
}
try run(self.step.owner, &[_][]const u8{
"git",
"-C",
self.path,
"checkout",
self.sha,
"-b",
"fordep",
});
};
try self.checkSha();
}
fn checkSha(self: GitRepoStep) !void {
if (self.sha_check == .none)
return;
const result: union(enum) { failed: anyerror, output: []const u8 } = blk: {
const result = std.ChildProcess.run(.{
.allocator = self.step.owner.allocator,
.argv = &[_][]const u8{
"git",
"-C",
self.path,
"rev-parse",
"HEAD",
},
.cwd = self.step.owner.build_root.path,
.env_map = self.step.owner.env_map,
}) catch |e| break :blk .{ .failed = e };
try std.io.getStdErr().writer().writeAll(result.stderr);
switch (result.term) {
.Exited => |code| {
if (code == 0) break :blk .{ .output = result.stdout };
break :blk .{ .failed = error.GitProcessNonZeroExit };
},
.Signal => break :blk .{ .failed = error.GitProcessFailedWithSignal },
.Stopped => break :blk .{ .failed = error.GitProcessWasStopped },
.Unknown => break :blk .{ .failed = error.GitProcessFailed },
}
};
switch (result) {
.failed => |err| {
return self.sha_check.reportFail("failed to retreive sha for repository '{s}': {s}", .{ self.name, @errorName(err) });
},
.output => |output| {
if (!std.mem.eql(u8, std.mem.trimRight(u8, output, "\n\r"), self.sha)) {
return self.sha_check.reportFail("repository '{s}' sha does not match\nexpected: {s}\nactual : {s}\n", .{ self.name, self.sha, output });
}
},
}
}
fn run(builder: *std.Build, argv: []const []const u8) !void {
{
var msg = std.ArrayList(u8).init(builder.allocator);
defer msg.deinit();
const writer = msg.writer();
var prefix: []const u8 = "";
for (argv) |arg| {
try writer.print("{s}\"{s}\"", .{ prefix, arg });
prefix = " ";
}
std.log.info("[RUN] {s}", .{msg.items});
}
var child = std.ChildProcess.init(argv, builder.allocator);
child.stdin_behavior = .Ignore;
child.stdout_behavior = .Inherit;
child.stderr_behavior = .Inherit;
child.cwd = builder.build_root.path;
child.env_map = builder.env_map;
try child.spawn();
const result = try child.wait();
switch (result) {
.Exited => |code| if (code != 0) {
std.log.err("git clone failed with exit code {}", .{code});
std.os.exit(0xff);
},
else => {
std.log.err("git clone failed with: {}", .{result});
std.os.exit(0xff);
},
}
}
// Get's the repository path and also verifies that the step requesting the path
// is dependent on this step.
pub fn getPath(self: *const GitRepoStep, who_wants_to_know: *const std.Build.Step) []const u8 {
if (!hasDependency(who_wants_to_know, &self.step))
@panic("a step called GitRepoStep.getPath but has not added it as a dependency");
return self.path;
}
|
0 |
repos
|
repos/3bc-zig/build.zig
|
const std = @import("std");
const GitRepoStep = @import("GitRepoStep.zig");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const libdep = libHello(b, .{
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = "hello-zig",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
for (libdep.include_dirs.items) |dir| {
exe.include_dirs.append(dir) catch {};
}
exe.linkLibrary(libdep);
exe.linkLibC();
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
fn libHello(b: *std.Build, properties: BuildProperties) *std.Build.Step.Compile {
const repo = GitRepoStep.create(b, .{
.url = "https://github.com/RodrigoDornelles/3bc-lang.git",
.branch = "develop-wip-wip",
.sha = "c562e9852989784dea1caaa12afe188bc983800d",
.fetch_enabled = true,
});
const lib = b.addStaticLibrary(.{
.name = "3bc_hello",
.target = properties.target,
.optimize = properties.optimize,
});
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/alu" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/cpu" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/driver" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/bus" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/sys" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/util" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/interpreter" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/types" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/legacy" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/i18n" }) });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ repo.path, "src/ds" }) });
lib.addCSourceFiles(.{
.files = &[_][]const u8{
b.pathJoin(&.{ repo.path, "src/alu/alu_none.c" }),
b.pathJoin(&.{ repo.path, "src/bus/bus_cfg_hello.c" }),
b.pathJoin(&.{ repo.path, "src/bus/bus_cpu_hello.c" }),
b.pathJoin(&.{ repo.path, "src/bus/bus_sys_hello.c" }),
b.pathJoin(&.{ repo.path, "src/cpu/cpu_common.c" }),
b.pathJoin(&.{ repo.path, "src/cpu/cpu_string.c" }),
b.pathJoin(&.{ repo.path, "src/driver/driver_cpu.c" }),
b.pathJoin(&.{ repo.path, "src/driver/driver_error.c" }),
b.pathJoin(&.{ repo.path, "src/driver/driver_gc.c" }),
b.pathJoin(&.{ repo.path, "src/driver/driver_interrupt.c" }),
b.pathJoin(&.{ repo.path, "src/driver/driver_stack.c" }),
b.pathJoin(&.{ repo.path, "src/ds/ds_prog_array.c" }),
b.pathJoin(&.{ repo.path, "src/i18n/i18n_no-ne.c" }),
b.pathJoin(&.{ repo.path, "src/interpreter/interpreter_mock.c" }),
b.pathJoin(&.{ repo.path, "src/sys/sys_common_mock.c" }),
b.pathJoin(&.{ repo.path, "src/util/util_itos.c" }),
if (lib.target.isWindows())
b.pathJoin(&.{ repo.path, "src/sys/sys_windows_output.c" })
else
b.pathJoin(&.{ repo.path, "src/sys/sys_posix_output.c" }),
},
.flags = &.{
"-Wall",
"-Wextra",
"-Weverything",
"-std=gnu99",
// "-Werror",
},
});
lib.linkLibC();
lib.step.dependOn(&repo.step);
return lib;
}
// const src = &.{
// b.pathJoin(&.{ repo.path, "src/alu/alu_math.c"}),
// b.pathJoin(&.{ repo.path, "src/bus/bus_cfg_lang.c"}),
// b.pathJoin(&.{ repo.path, "src/bus/bus_cpu_lang.c"}),
// b.pathJoin(&.{ repo.path, "src/bus/bus_sys_lang.c"}),
// b.pathJoin(&.{ repo.path, "src/cpu/cpu_jump.c"}),
// b.pathJoin(&.{ repo.path, "src/cpu/cpu_math.c"}),
// b.pathJoin(&.{ repo.path, "src/cpu/cpu_memory.c"}),
// b.pathJoin(&.{ repo.path, "src/cpu/cpu_procedure.c"}),
// b.pathJoin(&.{ repo.path, "src/cpu/cpu_sleep.c"}),
// b.pathJoin(&.{ repo.path, "src/ds/ds_ram_array.c"}),
// b.pathJoin(&.{ repo.path, "src/i18n/i18n_en-us.c"}),
// b.pathJoin(&.{ repo.path, "src/interpreter/interpreter_parser.c"}),
// b.pathJoin(&.{ repo.path, "src/interpreter/interpreter_readln.c"}),
// b.pathJoin(&.{ repo.path, "src/interpreter/interpreter_syntax.c"}),
// b.pathJoin(&.{ repo.path, "src/interpreter/interpreter_ticket.c"}),
// b.pathJoin(&.{ repo.path, "src/interpreter/interpreter_tokens.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/driver_accumulator.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/driver_custom.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/driver_gpio.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/driver_idle.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/driver_mode.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/driver_power.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/driver_program.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/driver_tty.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/ds_hypervisor_darray.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/ds_label_hash.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/ds_memory_llrbt.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/ds_procedure_lifo.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/driver_memory.c"}),
// b.pathJoin(&.{ repo.path, "src/legacy/ds_program_fifo.c"}),
// b.pathJoin(&.{ repo.path, "src/sys/sys_common_pexa.c"}),
// b.pathJoin(&.{ repo.path, "src/sys/sys_conio_output.c"}),
// b.pathJoin(&.{ repo.path, "src/sys/sys_nes_output.c"}),
// b.pathJoin(&.{ repo.path, "src/sys/sys_windows_output.c"}),
// b.pathJoin(&.{ repo.path, "src/util/util_djb2.c"}),
// b.pathJoin(&.{ repo.path, "src/util/util_stoi.c"}),
// };
const BuildProperties = struct {
target: std.zig.CrossTarget,
optimize: std.builtin.OptimizeMode,
};
|
0 |
repos/3bc-zig
|
repos/3bc-zig/src/main.zig
|
const std = @import("std");
const tbc = @cImport(@cInclude("3bc_header.h"));
// zig fmt: off
const prog = [_]tbc.tbc_u8_t{
tbc.MODE, tbc.NILL, tbc.TBC_MODE_STRING,
tbc.STRC, tbc.NILL, 'H',
tbc.STRC, tbc.NILL, 'E',
tbc.STRC, tbc.NILL, 'L',
tbc.STRC, tbc.NILL, 'L',
tbc.STRC, tbc.NILL, 'O',
tbc.STRC, tbc.NILL, '!',
tbc.STRC, tbc.NILL, '\n'
};
// zig fmt: on
pub fn main() !void {
const instance = struct {
var static: tbc.app_3bc_s = undefined;
};
instance.static.cin.tty_storage.io.arr.ptr = @constCast(@ptrCast(&prog));
instance.static.stack.cfgmin.prog_size = prog.len;
while (tbc.driver_interrupt(&instance.static)) {
continue;
}
}
|
0 |
repos/advent-of-code-2023/Dec_1st
|
repos/advent-of-code-2023/Dec_1st/First_Challenge/build.zig
|
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "First_Challenge",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}
|
0 |
repos/advent-of-code-2023/Dec_1st/First_Challenge
|
repos/advent-of-code-2023/Dec_1st/First_Challenge/src/main.zig
|
const std = @import("std");
const testing = std.testing;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var file = try std.fs.cwd().openFile("src/puzzle.txt", .{});
defer file.close();
var buffered = std.io.bufferedReader(file.reader());
var reader = buffered.reader();
var arr = std.ArrayList(u8).init(allocator);
defer arr.deinit();
var result: isize = 0;
while (true) {
reader.streamUntilDelimiter(arr.writer(), '\n', null) catch |err| switch (err) {
error.EndOfStream => break,
else => return err,
};
var calibrationValue = try getCalibrationValue(arr.items);
result = result + calibrationValue;
arr.clearRetainingCapacity();
}
std.debug.print("result: {d}\n", .{result});
}
fn isDigit(byte: u8) bool {
return byte >= '0' and byte <= '9'; // ASCII range for '0' to '9'
}
fn getDigits(bytes: []const u8) ![]u8 {
var digits = std.ArrayList(u8).init(std.heap.page_allocator);
defer digits.deinit();
for (bytes) |elem| {
if (isDigit(elem)) {
try digits.append(elem);
}
}
return digits.toOwnedSlice();
}
fn combineDigits(first: u8, last: u8) !i16 {
var buffer: [2]u8 = undefined;
buffer[0] = first;
buffer[1] = last;
var combination: []u8 = buffer[0..];
var result: i16 = 0;
for (combination) |digit| {
const intValue = @as(i16, @intCast(digit - '0'));
result = result * 10 + intValue;
}
return result;
}
fn getCalibrationValue(sentence: []const u8) !i16 {
var digits = try getDigits(sentence);
if (digits.len == 0) {
return 0;
}
var firstDigit = digits[0];
var lastDigit = digits[digits.len - 1];
return combineDigits(firstDigit, lastDigit);
}
test "isDigit with valid digits" {
const validDigits = "0123456789";
for (validDigits) |byte| {
const result = isDigit(byte);
testing.expect(result).toBe(true);
}
}
test "isDigit with non-digits" {
const nonDigits = "abcABC!@#$";
for (nonDigits) |byte| {
const result = isDigit(byte);
testing.expect(result).toBe(false);
}
}
|
0 |
repos/advent-of-code-2023/Dec_1st/First_Challenge
|
repos/advent-of-code-2023/Dec_1st/First_Challenge/src/puzzle.txt
|
8eight1
98126
fourfourthreehnbhkmscqxdfksg64bvpppznkh
8fivenvvtrlj
six3zbhvrfhsevennine
427nine6chnqrssxfour
threevvxhvx38rktdqm3
eightzgqzr3eight
xgjjmnlvznf2nineltmsevennine6
ninethree15seven
37cjnsfbfkqpkxpdvgk8
sgeightwo3
9sbxg
1spnthree59ninejjgjdlx
six9six2gxmn
twothreefdbl6five3zcqvcqxkcvdfkl4
46six47seven27one
four15
7sixvmsrrzqnngonethree
3fvsghvkqkbfivenine
zhthfghk6ppc48bdx6two
1fouronesixsvhbglmvxx
7lcsixmzmseven
two37sixccrpqngpvthree
86fivelqmlmbnhq
bvcz4
6fivendr7seven31nine4
jmkhtgklhpxxblc2slhlnfkoneonefour
8smpnsc29threesix
r795vgnhqhs92one
eightfive6
4threeqzslpcvkpfdjh
8cthp65
twoonenine87
dzttdmpfxtnine6onefourone3vnnz
8tnnfrsdffpthree67xh
seven5seven27hf
3sevenone
seven9foursskfhnchhf481
vrfbrcsgp81vsztr
six9dnqqgxtvrngpz7879two
lkvzzdxmr71lglsixfour
two6jkpz9
1sevenpmjfv
nlvrdmdhxsix7three7frmdlckfjninesix
gfb89qrhtzsfvbnpbp6two
f8mbbtzjvrqkrszlgrlzrlrgmbmmrdqv8seven
sphkkpptpvjzbflkzz2vbndc3foureighttbn
eightnine5zpbzhltpmsxrggdcmbrzkvczqbfsevendtjsr
2snpbrhpnine3sevensevenhftpqgk5
seventjxr91sevenseveneightwojf
1hfkvhjxrtjfivefive
3fxgmlxtwo
stwoeighttwo3
eight45t
sixthreecdhbfive3sevensrskjm
fiveklmj94sixlhpfztlk95
fivefive6
fiventjm7tndhd57
teightwo87nine83
6jskjmxrttwo93four7
dftzgsdc19threesevennine3twonevl
zmpcgkonethree1cspnkkpjr9
hmsfrjtxpgn9dqkr9dqvrtq4
3eightmfggjleight
qfglzvvrrtpfl4
7eightonefpqq3
tgtbsmpftwo4hvnvsixfrdqbvqbhspcczfhvqcslvfszvrtzbks
twofivesix34nine
dhlhkjcjsbgnxd6sgrdnzchtxtplltbqlk
oneonegcndlfourdngmbgdeightsix2
twotj9l8onetwoned
jpz2fourjgrjzf44
9fivesixmlqr8rxqpvpfzfive2nine
6xkfkrfnine196
four7gppqtzvbftzcbzt8nqq1nineqdlrff
2threekxgcqjzt4qdbthmjx
three61eightrnkckq
9knfvqgmdsmdqlkfzl
mxsqv6twojgmhpmlfq
7fouronelngndbsmljbfzmsix
fjm8fdpnlldlgvkqdcdcdjpxzdt
7five2
tsp9seven
eight2hzpkpqrjgcrnxksix
jfourcgjbpzbgdn61dzqbgthree
7threefive
1ncmzhqghthree3sixllvknhf
one5six9
krzkzmbccr1
74mqnzmqnf6
four9pfhjktmvxtwo
6bl21bpx2
4threefdpfqck
jjksjbsjlgdonefour5
2hbthreegfh1
threegzbn1four4hbqkmtmoneightrhg
fiveeightnine83
3five49jvrhvmnjmmzlzmhpbpsfpn4
3vvhshsixone3
55three7svtlzzggkqjhjx
fxdmnjsvpjbg69gcknlzgrqhqdsxvk3p
eightfcnmbzl2jhlklgmlmsevenkjbgzhstwo
6sevengdmtlfjmns63
ktgqthreedfmrpdvnkfvs3jsgcvtdkh
4seven1
34onexkninex6pnldxrfs
threenine3five9eightrvg9
xqbnmtrlsjninenine57rpcsjpqfdcbd6
216two
xhrdmfcvjzt4sixfkjsvkfhtldfqckj
2six8zeight1
cmpklgjgf19twoqstfhphbxxtfdgj2
lvgqnkqhltwo9r
twosevenfive2rgfsgmzqjbnine
7ddpjxmfschtqdgnjqc18one
oneninefiveeight6
qs4five5
rpxbcff9vpmlvvrlonefourdnczqzjbvnine3
7592r3qjtwothree
sixtwoninegzrfp9hq6six9oneightp
f8seven1dcrjqchxchfour8
bnhmfmsbzbhfive1rfqqkzkptjnnxpone2jqhrfnvkfk
qlhpvzdkcfsq84
dfour5one
hcvxnfm32kpzxkhvmvkjsevenllpjpgdhl
dlhhnk2ctvxqmbmsc9five
one7sevenninesix
two4rkb5sixtx17
28three7
msixeightnqmtfmtftsix9
zff8nineflfpz1six1
vk9
86ninelzskddf9784
mrntnhjrvkjgmntone715ktgb7
sevensixthreegkldpptbmteightsix2nfnljvkfkgc
nineonenine3ssnjhkhdplvq3six
jrdgsz8
5nine6bglkndgfnineq
nineone8seven5zhdd2eight
17qjzvtwo
4rncnvtp5ssznhncdnnzndr9
6srf
5two6xxkzdrbfsix
five314sevenseven
eightsix7lhfqone
gdz3one
6sdplqg
rsnnm3twoqlxjjvzeightvxszxhmnck
7onelvrtdktmcmrm55
1eightzvcgfqgfh
ssgrzpb6jntxrdhvmrkeight
5oneninetwo
gjktwopxxql4gxfourseven9
four55sevenvglcrqsdt
nsqrjfhs2eight
seventhree5g9
threehvmrmvglvvsix7pqxzfjvv
cfgfmgg5vmqnnqrxdxone35
two2fxsmdjmb
rrlxrkjkxmfrbvxsevenrlllvclhvvfourlp4seven
djdoneightthree82eight6five6
two4threecdbgnmzvdd53oneeightkmx
one5qdtnrpcmrnnsbeighttwotwoninegtpv
86htqkfrkmone217
onelqd5jvlbm4
eightone3rpnkglzfcveight12one
fourqfxndmpgbqrcpclbnn86hxsxqrrkjcgtgxbdxjdjlzcrhkp
1one18tjcsjzpmzmpmstzmcn9
threesevenpgt3
four9sixthree1
twoninethreesxxqgqlqmqxkmkl2qdrqpftwo
seven678
two29six958kqxq3
dbpjzgdrhnsixfjvs9eightsjdgtckdtsjmhplkjeightwodd
7fourninenine
fourthreedlhf9rf
mjkvfivekltmfourone2
7twozrrtg498one
4eightcxnx
one62eightdktgeight2five
bnpxcdxpzp4tklvsm
d8sixninegrbx
threefourfivetwo23
nine3onemqtbrhlp8jqslv66ssjrkh
1four9fournine2
xfsl83two4
65twonine
1twofivevctxpfvr18
819gc4six79
27vtvmsix1phzrone2nine
boneight1mskkndkgfive
1xmpkmg3
one8sixthree9zjtmqjjjtc1nine
six36gqjctdsevenlnmmbr7dplrsdptwo
8jjvgmffgxr5
fivehgkc5
2qmbvphfivexggrxljhj9qmjrzmrx8eight
3rndntwoseven
boneight6tworgxffournine
htbvm1grtsgdgbl
1hkndcqv
32sl9six9
894
vrhxnstqqmflm6glgfnpfour
5ninekrfivesix8
5six5
eightpdzjhvbgm5oneonetwothmnkflz
vnfttnhcs892rmpvjvqvfftmzlkcrzthkbhfour2
9twoeight
73553bkzzhq
fvxmvs47
3bhmjpqvzs
pcftqc671
sixtwo8
threesdhhzmxtzpsfgs286nmqpvrtkbktcblqqb
7pmhlfcmdxthree11gxpncfqntjtwoone
8fivel3sevenxqtpxjntbjbtxhxtqdfnfour
6seven72zzseven
seven9kcthreetwo2five7br
kvkhl1fzvpseven
5oneoneeighteight6
lvfx6onefour
ftjzq4
slbgthc5xsxvplbskk
64nkjjvrmsqzdone
twoonelgcnrmrm52
gnfmrdkzoneseven7twotxgsjsix1one
1xlrpbbdbdgftkd
8jmphpvclbf87two
724threesixthreetwozsix
bcxdnrd91cprdfcqfive
zxjcgbhfr3
scfklbckxjggbgz9txqnzf6
1twohctsdfkgsdteight2
7threethree
jdj9
99
sixfzv24nine2bz
onefour1frf79tcfjchqeight
onethreenkgjgtpxfkmncvmsqs2bsqppgg
fourfourtksqrmnmqfour351lrsnv
6559hthdpeightkfive3
sixone7
eightoneeightjcsggnqthree5qqmp
sixeightonegltz7
three4plz3kls
8sixmssvtthreeseventhreenskzq6
tbzxjqxkspseven9dtbqvchseven8mbpznrrh1tdmqs
1b7eight
thk23eightfivekvzphvmsfour
7cnlrjzvk66threefxzrtgrbvmbp
bhvponeonefouronedcstbnl8
eightninesix4fourbrpfp
four29eight
kqh5zkgqphzjvz
4zvh
fivesixnzbsvfjhj2
rkrnbpsqjdthreez78mg
eight4gzxfdjpnjnskzghlzfdmjhz
seven8fourxzdmlcq1mnine7qbblrqtrsq
gd369
hgtqgfive8
pcbgddgvvzpmone4onethreeoneonefive
95nnjhmhrqgkkstwo66
723sixone9threefiveone
threekfvqsscfjr8
3vzltxllq65jvthree
4977d
35pgnlsjjpbgseven33
g25twonineeight
three8eight3six
8five8vjnzglnrbsbxmjqzfvrsoneightlpx
3bksdkncgtmdrlvkgboneffkszhfive8
five1hfh6
fourfoursix2nine9t
6onetkhqqonetwo
26hdfljh8
6onethreefvgnoneoneninefour1
jghct6rzcbjsfivefourtsrbsjkgmtfive
nine93eightxqknsqhrmjthree2
fivenine44sixsixlxdkpm
threeeight16jjhdkbhlninekvdvlpbq
onethpbzthhlqxnlrmtwo8
seveneight1eight
5two8
4four8vdpmqvf3onenmfg9
eight9jtxgdzdrtwovznpmnqvnnlffsix
456four
9hxkq
713kthreexfbdrtbx
1five59
1mxpmfive63
sixfour1ffivezjj
97eightthreecjnxblrnine
kgqcxfzc4sixnineoneone3eight
9fbklklqqsfourbfhmfmgnbtls3three
xxjnbdxzeightjqvvd82kfchrthmgkfive
zfdeightwokbq3seven
6four696ninezfhrbg
zrznfshdvlxcstqtghjnjvxq1dlklzghbh
steightwoxfbfzlnzvlj7919four
nine48oneeightseven89five
ninesxtdnjkmg7rn32qdljqprj3
five6xkcg1qmpkddfbsnlhzltlqqjr8
99sgptmns4five
2ninejdstk
gvclfcg9
n9lmv
jrftwo3
sxrxlnvdhr47dppck
jvtwonesixlzdshrfjtzgqddsix7
7peightoneeightwobsc
three6txdjgqqmsdeightcxmljmmzxksseven
fqpx9three96xrvjvjjnrhknsvfour
9fournjmslzbmx34cnt
f3twovfc
shbtwone3kzdsdrxgr
4five1tfhvd27
rdccbvkr9gjsmxhfdzgvjcbkdmsdeightlvjkdgtdqmbvscvhpmdz
pzsqhqthreetwo1qxfbhreight
threeeightxonenineninepmjfgzsdhd1
nine7sevenone
hbsqqtlqcxkjjccmmpv5
6nineltr5plmbgzfqbxninerxzbzrblmfsmjggvr
5eightwolxp
5cscpfqm
four3nthbmbxfbvtrqqkfmltmtjh
pvxtx89jp9onesevennbxmjlzmfk
threetwoeightzcgkbv1
7rxxfpgvl3eight8dmhkf
nine6two
tpnr21five1eight
ktsx6threeseven
8four31drvszglmv
fgkmbcgsrjgs6eightfivefqqdksrndrtdbsxgmfgtggmrc
sixnineninefzvthdq5tkzrhrgdqblzndmv
7lfdrntqsm
seven8dgxgqvgvmtdkhqsfkhtqcjpvqlfnine
znltfour6twopsdmcthree
oneeightvvvxxptmk4eight2n
5six1jmmqtknpveight
eightsixxxpbvqhpknz27mqxvf
rbdchcmqhseightnine9
944five8gxsjf98
smdhcnbmone9
one64six
seven4fivesevenfournine
fivetjt27qqxncqcone
five49655onet5
mlqgmhfgsix3two2
jxdfkbvdgnjqznineh1rdqsv7
fqdvcktwoeightsixsevenkkczb3
3krndk27
qjpmscfqf2gzgvzgksc
5zs11fourqqzfour
5h
fourtjzxtgrtd2twokrkpfm
fourgxprrlvmhgpggsmzbone7
ldbprdzrqkgqkx23pjvgrc6
ninevfh2seven4four
23gfmpvvsrjr4vlntkxkeight
sevensixonefivefour9two
vmchfrmfntwo3sv1twoonenngblvmjsf9
sqhzppseven79kzdppfnk8foureighttwo
1ljltwo8xhszxqmtponethreesevendkx
4fourfive
5fpzhcd7pmbcxcrkp
cdhqgkhfive61seven
eightqtzrscqhr3
brrftwothreethree1
r145pbthqtvxd63
ninebdnxdvchzf57oneightpp
7one1three68
five8nknine341msfflpnz
sixxmsevennm6
2n5cdvvtghfg2lmfrbjbvtwothreefour
39one16
l3two
xsmtghfiveonesix6lkzfgq4threethree
6onettqbfp
nhxlqqp3vkcgsjgkgmjrpll
xgbzrtkbthbfour7mptxqnbrkvhninetwo
7foureight55cvpgsvsr
3twonine
gsqhbhfrzfour9twovjjbskdonegseven4
2fourthreedcxrcmjmkprdsbone5one
519955six
two8fjgvtwo7vnttwo5qzlgxcfkz
rvk4eightthree
9six6
clpxqfn3kcn3
1pzbkzdn9sixxlq89tcnm
nineninetwogtsjfour2twollrrllvk
three5ql7hdjkxdbrcqsfouronekqhc
ltgeightwothree5ccxbhssxrsbj
42three3
dkmm8eightnine4five
vqmvzpb9tmmsfgscfive6mtjjbleightfour
ms39zmtbptwofive
4xxxkdxlhjjx
6p
threejfqfjhxp9txlrlrbczdxzpmrkz9sevensjlgstbpffhtb
three2zqtrtpzvsffdkjsevencjmrdqthree6three
two681
fourlhcsspzf5fivezgzkclbrdrqdjcksfive8
ss3
znrzctdlgjpzszsixzssrfpcvhlbpg8spfxsgbctpttbxxxsc
mjfsxjtthljlzzjlxspdsgsstltmlxf6
48zvpktkpk3
7khsdbtp43rgggnxf
2ninedkttzmpqddsix
xjcqvkcnbpbqsmclfnsdbnnine3eightfive
7eight6
eight8zqcdbzqfour3
one8eight27
ninemmheight3jdkktmthree
oneeight3lbsrnqrn
8ninefive71
hd58sixtmdj
fivesevenfourtwo4
fivecdsvgkfgbntwo42
593jxn
sevenfxkvdxfjgskjhhphnjrgfhs8sixcnine2
fourtwo2sixthreefive7rq4
threesixdhlkvz3fvdslkbs3
nine3fkrzbjbmjbsr
nine7seven36
sevenbnncfhrnzq52eight6lvfdmqhxkdqjl6
zshsmpsmbzpnfthree1five72fivetwo
threenine59seventkthreeflgkvtp
nine9nine6
8five4foursix4
jnz1m3fmzzgt9kbtpr
81btdvrcspvfour
three1smvpjdnhveight1sevenfour5rqkqvq
49fiveeightonefive
2xmdtnineczgbqmhfivef1six
638nine
eightsix6twooneseven
jgtwone6gzchdrkts
93tsrvf48
1qkrdfhpseightninelddrnffp2gc
nqvkkprztqsqfk74threeseven5six
14two
dmhnlgxqpjxxvpshqt2sevenplzbgvsz6
bffour72
zgkgvone8bxsnnlninegmjtnsqtdp
19oneninezm88blmdhbcrns
2fivessqk1fonevghgnz
ddszqsldhxpsbprdbqkmhs6ztkbzbkm8cddpqm2
7eight5xhdtpfive6mbf
qxcnnmdt5vctqqhmvrmqblsixfour72
6onetworpstxdq3tvrcmssmkfhbr
7twoeight1
64two3sixsixckbkshnkjmtwo
7csixonegdbrvreight
bfdptjcjdq4five7sevenfdhhqhseveneight
8one8dzbninethrmdldmpn
three7xghxhr1threerqfxseven
two9xrhvzkgcck3vfour
5lvtrgmpttk3fourfour
five581cjndb
3clp81dhzcbzxc
threesixsixbtst27
fivesixnines146
gppzks5cqggtq
4rgdzsnqlcxone8vninegkgqh5fcbsfl
jfpdpfcccrjpcfive2threeseven
ninefoursix6sixtwooneone7
twothree3njhxgc5rhntgnckg
3twopvtmvrcrnr238
k4mgzqbnkrthreeseven9
bzkoneight2jxllsevennmhxpgbdkfive
886svgcltdl222
sevenqjgkjdfrrskhklvrg9fourtwofive
cdsfdkdsvtcjhzdpqdgtwo454five
lmksxbv8foureightbxskfblhllsdrxfs4v6
49sixfour9nine15two
75mtcmthreelvmsksnine9
foureight849zgl69
92eight6ninezjfive
8onejsfvnjktqksixlfnxqrjmdvt8vhttwo
48three
onesixcksvcdeight3tr
hvxqb1fourseven4
four755grdsixthree
seventwokzpljzth6mzkvsdbgldfbdc
ninexjgzkcxl3sevenfive
949klmgj3
53threethree
lzczjzsftmcmclqqbrgjftfhxz3
9rpzfnrseven3tsqxxgtrsq
fcndgzmtsj86psclvsdvggbhfhd8five
qxkd2lxzhhcjlxonel48ndktqt4
2five5twovplrbfxfjzvmdvjs96
4drtzsix5phvgbqjsknnine9fk
btmdjvnkrqxvjzchkkdpqcnldljs7eight6znfphg
xtrjonefour8threexksbbvkxmkthree
sixhtxkz7pnfzsnpnxnine79
eight5826
hrxqbqq5sixkrdjszbblnpdhfour
xlcmcgprrp2
seven4vfm3
bztngjjhfivethreenineeight5
oneonedvgbrpgqrnh77
jfnphpvcggfour5zoneeighttjxhcqhsml
796fcone4
7fivetrmdsb
2fiveseven
fmtdrcczngfivefour6fiveqbkn
eight25lxjdzkrrqj
fiveninefour373
fournr2kzbls1tghseven
3nlmhvbninenine1fourpppthree1
j4fourtwo8
one2f1oneightc
8g2six8
nineqzlsxdktwokdhsevenpkqqhjb4jz
cqsnztq6hjvkcnpxhmplfxfv
6svktmthzeight1nfxldggeightqfmhhzzsdthreesix
five7six9
thlhmgvvrmqv9
fourpvksthrjlxxdddseighteight55
qsccghmztwosevengcrfqnzzmrhbgrxlrsxqxtmthbv7three1
onetwoj19eight1
8xgtltlrbnscvtone5nine4
ftx5
knsvnknplxrgtnrlqcl5
nflmxkm2dznjhp7three8
five2tttpdfnmdx3fdrtmxzqx38three
two3onegkj
47fivekd
six9one5nine
l3onegpj8threekkgdkfzscrrx
four4ninekfndgvhrrbfzpzrnvpzlxss2l
ltgsxcrgzqrfpkkb3oneeightfive91knzsppbbx
sevenvplfzkncmzninenine1
sixhjjsxgrkgstwoeight1knhgsmscrnpcsone4
1jbm89two63two
onedmpxhvvcjssixeightkpvdmzjltwo3eight
fivevjjtfeight6nine6
thffrjkhsixsix1one7one
dbpvngh3eighteight86foursix
pppcpjmbmhsvhjmkfour2onedlnrg
2onev8
55gpvxsvsnpfrpmn
lfsmfbone58
tv48threetf3
8threeonefour71
ghfslsb73dtlgjnj
565twofive7
sixljhfccjg5eighthpskg
5four2four988rd
xtgtb99
kkzfxjszrfjvqllzlhzsninehzbfbvnhtzjf2
165
3jtklkpsfxpxlrgltwojxcpqjdsfbs
three7vknxbtthree
7fourfourfive
sevengseven71nklkzdeightsevenfive
eight37one
onenine2
8nine9vkxxhnm6threethree
7one96fivesix
58bjnfhjxsm146six2mzv
99jdqbtb5seven
8mqztm7bdjh4
6ninevninenvxflxvgg
k2twonek
gzmtwokrk9ptrsixfjbktjc9
8fivesix
onesqqkvdtrs5vrsbgnvkjseven
bnbxpqhqxthree79
nine5l96ninesixqhtxpkzb6
sevenfive8tklnkqtwo6three
qb2onenldzvsixdjmjzdfourqgjsssr
thqmrjnjg3dbnksktvk
318
three71blglscfrzql3six
9nine6nine4
sfive1seven1m
five3dj16
5ninetwotwo8srrvkx3
spcsttljpl7
6rslsdkfs4834
dshfour8knjplkghpfpzcszqsix1
ninephtlxnnl8hzn
bmr5
tm4czfdfbhps
sevenseven4three9sixtsmg
sevenprdqm1six47cnbskdfivethree
7one6gzfdvfvfourtwoninelbndhdkqeight
jzxczvgjgxdfrzn2nineseven
zpcspqvhbfxlcgt1onesevenqktvgjhm
bmzdhnljms4nkjzhkrtfzngbn
nine6mjfbftpcfh4five9eight
qrvsldss4nine4
lffkvnhbpbtnxzncjs4
76ckghqnbsnlnjftwo72
7jrqrkdksixsixtwonett
3xc
mzveightwofive2onesix7five2
74five6mrqx464
qtwoqlrkvlmpqfjvm8onetwo
41bchstnlzkntwonineseven1pskh
twoonejtjph15three
fzmkkgdq6
21one
eight59fivenine
jbfmfbseightmzlknl84
four4rlzplmmmcplfour4mllmtxx1
q349j
6dbpqgttninepx623bpbhsnxcmg
52hvsvpk
8fivesevengzz
3fourfnfldsxngrknxnvts
18s
three2561b1onethree
zlppzxzzsixtwo4hksclkzhtkdsldfgjhtfhxcdsmdbr
26eight65five9threeeight
seven178
fvljxzhdln11eight
49four6fourqmspqkbphjzvgzgbvrbmjfsm
onelnmplhjtjtfivesqzn7qmcpchjtxfcc
hd8
9pnneight
five1qgdzkj9eight
rfhbnhbkzteightfivesixtwo3
bczljcceight2
145sevenhmfive
8lbgkvdxlccgzzvvttcfgmthreehhmqfivexcdhdd
6bqxzzzgpdk48
twobcxchbfour5m2two1seven
twotwotwo6513fg
48one6tmjxclchtnpltmjkpmmm
vhkcklgdhk9xmvtbktdvjmhjtwoned
eightfourfour9one7
fourqhgjdpr4p3sevennine
vlctnqfour4724
9spbtvbsh7scvvx7hvbxlh
eightone1ccdhppbsninejfktrxst
448fourhjk3r3mhstwones
hbgfourtkvg87sixlcvtjz
fivesphnkzrn5threeninefive27five
92two95seven5
twosix84zjqtp
9bxppvsjgdmbqvkstrqdsixtwofourfour
fourfive3sixnine9gmrdhfhkjtrnbl8
gxcdvsjxgll46mrrdfpdkdl
4foursevenzklvhrdvpj
seven1rz24rfmnjxjkthreenfvjdsjtgqkd
2xbfivelpfhvhsttgjqfsqkffctppeight
6vxfjbssixfkjqsdh83xgzdmtntgm
5four98two
xdk6
ptxtvnlpxbpkxptbhxheightfour7
6four5sqgfour
eight8rbngtqpvninelzhffxpmtwoonefourfour
three1eight2
1two8nine
9eight82
5fourshdfivefpfd3
two667dsmv3lvhszg4
kcxpmrv99threeone
seven7five3ninerhjbmtfbzsevenfive
4four1threegvxkdsfour
qdqrgf6fourfour46
7nlztdphcjdnqm4zddjhfreight
sixthree62
vs86four
sssbdgzsjnlftms8
3ninethree45twofsfsvtnl3three
four7zcfjtngsninethreeninethree
gcjxkcnsevenfivefourhrxzvxftnine4sixzm
thsdcone23bvrfour71five
fiveeight5fourrlhjtmrtwo36
61lxhrpfvbqkd
55three5seven1
sixsix9
gklpxzmcmnnbhsgeight63
fourninelxm5cpkqvbpbsvjndn4
hcjghqfssevenxzdljnvqsix1sjvrhxxhxthreesix
rtfttcqmxszrsixtwo37scglpjfjt5
eighthvbsldnhfzxr7eightsrmqhsgonebx
zcvqxjn1vdddlsevenninefourninethree
qr7three3two4tdrqd
qbjttlfsrrrhrmkhpvgc7zkone
6fiveseven7ndfhzxzrrf
fkxhpzb568eightspdxfngrp82
seven4sixtwo
nineddhgjn4
37chjlxcbbjrjghzjdsix4threeone
jhngdhntvfmcbjjvlgx979
9thpnx12835one
onesixfzjgpfourseven1nine2jrxjp
5rzvhjqnlvnt1lone
9dmftktf9
eight5eight
grkfgrhnkjm9
23ksknkqrdthreecvg2rbxkkhlqttfivefive
3foureightshnff5six72
19ninethreebcgxkmjqhgpmfx3
28rdsm5fiveeightthree
dmgpjdzfl1two819
pbrvnfpftb3vptbqncmqszbbctnj363
fzdc29331
bnkjk9m289rxgqn
fivenine4seven6plsczmxr4
fourmxrltjgdleight1three
nvjxvpgtplgszgbeightcccxxjhbsfqdrj7
5stjbvxglkdrbp6kllbbnptdfzl
dmtpxndvvrbksfournine5one6
vnlhxcssevenjlfgfournine69hmnddrthree
blchmddjmktwo4fxsqlpfn69xhbpblrlmone
17qdxzqlpllfourszcpxmtcxhrqzqqhm45
fchdhsjzt6two9
srs3vcq7three
twotwofour4seven
three4six8lsdg1
1fdvdhxrxmmvzonehmkzspfdlrdvlnfzfive9
7threeone
96ninegjcxpstqhxbdzfsrczm2
xthree19
bgptbh73sixfour
eight82
sevenb3sixthree9zjflmmdjfour
kcninefour2ninetzqgrztwojq
eightfourhfqttttxteight5
zghnvzdpfivevvgfjzfzvcbvccmtwo5tkzj7
33qxzctgqsvxnine9
6onejdndbcjsseven
294xqcrnine3844
two6ssqvnvm889
rzfbhbr15jpnfqmzthreepnine41
eightjlltprrrtv63twoptg4
threetwolzdbgcpkm3seven1five
mcbjrtbtbp7twoseven7vdttjvmxbxkfmgmvhrs
three5hfivesevenqrc6sbst
eightdvvtsvmfqeightfz5six
kklpnghzsfv88two86eightplfgjz
bcgntz6gqxbxxtnnonegqlz4fivexmjxqf
mvbgjpsmhntf7mmshslgvv17thlknqfivel
fourmjflcbtwo7fivetdntjstntv8
89fctlsxz8eight
1twosix57sevenfive
3ninefivencdgmlqxlnine96
twofoursevensixeightpmpfpfljqtnm9six
5hqnxvlmnh1bxxhnpkjhgzxlhgffv
nine9fourtwo8tzseven
grcfkddgseven55g
seven8nineeight4one7lnntwonebc
one1ktgggr
76eighttwoqzfnllqnkq
cgjkzlksl1823749
9twonvhlhrlxnine7eight
qtzmndkrqqcpmjcxz98kklrtjgbbnzglstnnlnqksrgkzz
cqxknrmb5cfhfgpk
9lhrnlqrnninethree
ftlpbv55nine
2nine5tfqhqtwo
mqcfgssixtwofour6cvncgmxnlq
946eight84kvflb
h1nineglvgmh9txxg8
lxqqqdfive5mcgrjkxmlzdqbkldjqkhlxnbg
jjkvdzzkpxqp1three59dqthvgkfmfjvlvnrtlzlmqvcspjfc
sixfoursix87oneoneseven
1ptrtgd6
5threeeightqqjrmchfoureight
one42mdmfkrzln5ck8qxbfqf
seven819ninejzrddsevensix
vfpeight4pngqpbvsbz
threesixseven1
7twoonetwo9eight88slpctv
fqfsixtwofpm14seven
6xcdvsftlsrsrgseven5seven
mhmgzdgrc6lnnzv6pnr4stxnmrreightblzp
9glsqqcrttdgsdqkfour
82two
ftqzqnlddctlxmb3rdlg
sevencxbpxvznqmjqgglfccqkvjqmjbpthreenineeightnhszbvnff7
twosixplgdjplfthree9kvhvnzzqzk
1pfvkfblrmz
2n
threeeighteighttwo7qmlnjx
6sevendhc6
nineklzvllx3fivesix
2nrrx5fourfcljkdhpk
eightone9eight
gqhsmccbpjc46twooneighttbv
sixtwo2bxpdqfour5fivexkjxzb8
six4nineggtqsxjd16mhgpjflprmsix
tndgkrpmfour1foursevensflkzt
bpxtdkrcr5nsevendltgfgkzxdtbxsghkkninesixone
one22threefivetwo
lzq7fzbqsd49
cgthgcsix1
389seven
cmgctmtwo8eight
2qvrbrnvp559
eightjpkjnpgplseven7oneightdv
cdzoneightsevensixeightmfjsevenk2eightfour
2sgtftmrrlfhgqxdmhtfqncslmnlvdhnjpdtpg45
fiveone1
ps2threeeight9hvmjrc3
pdsr2xphzmmztnqxpzq
nineeight7ninevhlksv
jjbsm55fscpfpklq1sixseven
fivefive1nkpzcxrxbfour
fivesixdkxvbtzvtlsevensix3fivekh
twovrzxlmnxnhonedpdmvn9
hmqccxh6
five6nine8vcbzzqrvn29
5nfmfthree4nthreenine5four
kpsbqshc21dzrpktfseven
cdfsszrtpnsfhgsix9bzqjdltbtzjsz
twofive4eighttxjxoneeightninet
six4tppbxfiveblktrzgdvks1
eightzmvmdbbfnft2
7npgl9vvtdl2
3twoone
seventhree6skl
onepdfjql18nine631
jpjtvxck3sncsvnsix112
njrkcgznhvfour1
gfmtkdr2pvvlnh9one2qgvmxfm
4jfxzmcdonep
one9threenine
544
one5sevenzhmljdbfb8pncqtdkch
nine72eightxvjtrbzpzvcqvlone
sixfive8hpnvvjzld4
24f8
6bqtwothreectlcqkkzfiveppvbgbvspncldjhb
ljmrlhxvsone1qdsfhqqqlljd6seven2one
mfksxgxhrqxbtg73r1eight
ztwogdpcgllnpp516ztdkj
two8fivefourbs5jlzfnleightwoqvk
seven5qnrvpqtrvlt4
ninedbnd8mrchlnineqhhqjnznts
onethreezmhshprm2jb98three
3sixfivesix
two6dfgpzqrhzp8z368
twoonesixfscgflrlkmbtmv5893
fiveninegkfbgczqjhnine7g
rbvknineflmkl4btbqbkpsd1eight4
39324
sjrgxtxppvxsseveneight4fourkdcdsmfhnnqjpbtxg
6threevcgjmclnhcfxvvbncnhjkmmsqqhqffivetwo4
foursix1four
zbztwoxptgdffourgzgzzhhmhg78ljlchqh
dcpbt25vcdf3three611
v58dtgrpl
rntgtrrc67mdlfxzgkvjjpqtfournineninefour
lpcts8vjznknlj
bpmv3
5z
zmsevenbnckdjhpbsvglrfsskcsevenfour2nine
6threesevenprcjlk5644
nineczcd6922
2vksfctzbjz
kz93
twopfhlqplngsj6z13four
8nm3sevenxqzj
cjgoneightthree5kmpndvrmkljeight6sflmeight
three3b3seven55xgrrkssnzsghd
5lvxlnpc24fbrbtg5
7qgmhfnine
sixpbvfjzkdsqdpsthreekj91nine
sbmxhcxonefggt4f95oneseven
onemgkvjpgfdjmhcsxsmlfjgzqqrftgxgx2jdmbvbfdf4
bvzcskfrlg8jseight
hzltjqmxjt98eightfseven
2seven7vbfm44four
3bvfnine
4mqprdhhrdqz
twothree2
seven3threeeightsllttbrdcthree
gmpxgkvv4four
vntmvnd4two
fqhvkqrxdtwo1threeqrmg5
5f
fivenscdbpvlz1
vzdcg921fivelplfsbdccrsdsq
sixnine2rnrdqkfpmdfives2
5seveneight4sr9gffive
hknjrm5eight34nine
7965
2t34two73xlfzpd
626
1kdfnbjgtshsonenine1eight1kzp
two8mxrlgchctdtflkknonezstpfgxsdx28
sixfdjgvfm1sevengg656vqcql
threesevenbjcfxlbml8lhxjmzgspfour
4phlqzbhvk5four
1lkccpmxmlxlqrxzz8lkmbpmdncgpzsxgqdcb
klqcnhjvone75
5two7
2ninecgrltzpgzpfourfour47seven
threeoneeight758threegtwo
54j
seven9lqbfgrsthreefivefour
six7kjm2fzcrdeightfour84
four198766
six3mfgmcrmlnine6lhddlhgl9
rkpksqvfiverxvbseven7
734six
five5one9qd4bkgfdjgtxzlxfjr
17eightsmjzbgdbd2nine
eightqnthjhlnfive7nddfjcxq
fourxkn6two6three5
twoseven2
eightrbhdkpxpbsvmfivethreetwox4f
4bpgpdngvnineh
lvpxjhgkz77pthnktwomvonekjvkkbr
gqpcqlzfive2threejfknlchf
zgjslb74two
sixhxmt4
4bcqxrcf
onesix59jktnrtlone9one
9seven1msixkhxbkgbnqkpfjrr
four1lhslrtdrnxxsmptbjjgj5
eightcvbzqczt9ninegxlpsevenfour
threenine2foursixtwo2bldhzmsdvj
two8dmzmbnkjqfdqtmzxndrsnkrvdnl
9lqtcrvkvhdkmbfivenine6tkqh
foureightjdpqhldvzeight17vbkmjvfs
vkzvsphbltfxjfxxsl9
shdgdlft27
87twopsix7eightwoj
57threeone
pshspdc9fourone5eight79sxrjkcmcb
sevenhmzvvbpdktxxjrlvntx1
ninethreendlnnine9
nine2tkmkrpccone
7sixsixvdv
vpktkldsjcpsxjztmthree3onefdbcxkrvhcm
9lqmbltfoursnsbrqn22sdnrpxfsjfour
five8mgctnvstcone24fcfbcqtknjmddknkzeightwod
8xfbqxtwonmjthm24twofivelckrr
xhnldpddxcjnm5twoddtqkqd
sevensixdrcvgfxqpmvrtmgqdjqvclgnqjsfhnbbs3jvxnmjqcbtshp
mfour1zcpnpfcdeight
6twofour
r1seven7tdjgxshntl
eight5sixh
xntwonetwofqpxsgshnbhjcsrld75fourfjbllqscg
three3vbvqklcp7pfkngtnineslmrlq
7six3onenmttntsfq3
fivefive1seveneight
p2rpjkcpcsix75fivednd
ninekkzpsone167hnqlhvmtbffive
2qmtbkslqgjkrqhnine3eightonefour
2nlfivefive6
xjvbkbtdqhgvsseven719fiveseven
prbtnineqdvknxkblzgj7
lkgvhtfreightninegtfrmqhd4njvsgdrhdqfxrjrcvvcz4zs
25four24dhqqkpbprx
29two
7xsbfsfivexrrxhthree
fivesrnvbdtnqnfourninelctfnxkbtv6eight
9kkpzgthree1stb
sevenhzxdbbb81one
2jszvqhv
zkvq25sj2
hxxfg4kqjbjpjczzddrx9spkgdcvcsvmblvfxxcn
486l2clmtcgfive47
dfhkfnfour76
52hnpcc
kmktwonejdpgch9tthree
x6bkqrlhhcm96
tf7kndclhgjsoneoneightxcx
mdzmsfkl5pnine8
ghzv2three
vctrqxfsxhtblghfchthzhninefk3eightqjjjjg
three2jmvkmfour
rlkbtjpnlctsxpfp521rxbjdfsixhvp1
7vxlkff32mdfsqrmbnmtwotwo2nine
gl6seven35two1
two2rjtvndrzvthreethree
tjpxszlthree54hsftvc
jsthree48
seven6fourtwotwo
threetpnh5ninernztgb
18pvqllhjf5eight9vldjjqcjfmlhnddone
two9nine6hgbprcpxvmntdjkfivetwonept
7four8eightktlhdpmptone
rrzbgtfrrqkspsix3rkpzddzrbcrzvxzstjbqhmqq
84qxbnxdpqppjfiveeightfive
n6two1brpjhf
plmkvpjbqr1
sixsixqbksfrndvg42hclgpgfggpxmts9
1htlmmvbnsix
dleightwolvbvmsggs9njseven5fivethreenine
eight6kxqqdnqp
5eightgdvgthfiveshthreesixfive
6seventntzffjkkvvhtgtwoonethreefivekzvptvxfjg
three6blrfsgdqsxgkbqj3
eight1eighteight8
8four419eighteight1bpv
|
0 |
repos
|
repos/ZestAllocators/README.md
|
# Zest Allocators
Zig Test(ZEST) code to explore the concept of the allocators.
|
0 |
repos
|
repos/ZestAllocators/build.zig
|
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "ZestAllocators",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}
|
0 |
repos/ZestAllocators
|
repos/ZestAllocators/src/main.zig
|
const std = @import("std");
const expect = std.testing.expect;
pub fn main() !void {}
test "Test General Purpose" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer {
const deinit_status = gpa.deinit();
//fail test; can't try in defer as defer is executed after we return
if (deinit_status == .leak) expect(false) catch @panic("TEST FAIL");
}
const bytes = try allocator.alloc(u8, 100);
defer allocator.free(bytes);
}
|
0 |
repos
|
repos/mibu/README.md
|
# mibu
**mibu** is pure Zig library for low-level terminal manipulation.
> Tested with zig version `0.13.0`
## Features
- Allocation free.
- UTF-8 support.
- Style (bold, italic, underline, etc).
- Termios / Raw mode.
- 8-16 colors.
- True Color (24-bit RGB).
- Cursor controls.
- Clear(Erase) functions.
- Key events.
- Partial Mouse events. (Click, Scroll, Release)
## How to use
First we add the library as a dependency in our `build.zig.zon` file.
```zig
.dependencies = .{
.string = .{
.url = "https://github.com/xyaman/mibu/archive/refs/heads/main.zip",
//the correct hash will be suggested by the zig compiler, you can copy it from there
}
}
```
And we add it to `build.zig` file.
```zig
const mibu_dep = b.dependency("mibu", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("mibu", mibu_dep.module("mibu"));
```
Now we can use the library in our code.
```zig
const std = @import("std");
const mibu = @import("mibu");
const color = mibu.color;
pub fn main() void {
std.debug.print("{s}Hello World in purple!\n", .{color.print.bgRGB(97, 37, 160)});
}
```
## Getting Started
See the [examples directory](examples/)
You can run the examples with the following command:
```bash
# Prints text with different colors
zig build color
# Prints what key you pressed, until you press `q` or `ctrl+c`
zig build event
```
## TODO
- Mouse events
- [x] Left, middle, right click
- [x] Scroll up, down
- [x] Release
- [x] Modifiers (shift, ctrl, alt)
- [x] Move
- [ ] Click and move (drag)
- Support all keys events
|
0 |
repos
|
repos/mibu/build.zig.zon
|
.{
.name = "mibu",
.version = "0.0.1",
//.minimum_zig_version = "0.13.0",
.dependencies = .{},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"LICENSE",
},
}
|
0 |
repos
|
repos/mibu/build.zig
|
const std = @import("std");
pub fn build(b: *std.Build) void {
const mibu_mod = b.addModule("mibu", .{
.root_source_file = b.path("src/main.zig"),
});
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_lib_tests = b.addRunArtifact(lib_tests);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_lib_tests.step);
// examples
const examples = [_][]const u8{
"color",
"event",
};
for (examples) |example_name| {
const example = b.addExecutable(.{
.name = example_name,
.root_source_file = b.path(b.fmt("examples/{s}.zig", .{example_name})),
.target = target,
.optimize = optimize,
});
const install_example = b.addRunArtifact(example);
example.root_module.addImport(
"mibu",
mibu_mod,
);
const example_step = b.step(example_name, b.fmt("Run {s} example", .{example_name}));
example_step.dependOn(&install_example.step);
example_step.dependOn(&example.step);
}
}
|
0 |
repos/mibu
|
repos/mibu/src/color.zig
|
const std = @import("std");
const fmt = std.fmt;
const utils = @import("main.zig").utils;
/// 256 colors
pub const Color = enum(u8) {
black = 0,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
default,
};
pub const print = struct {
/// Returns a string to change text foreground using 256 colors
pub inline fn fg(comptime color: Color) []const u8 {
return utils.comptimeCsi("38;5;{d}m", .{@intFromEnum(color)});
}
/// Returns a string to change text background using 256 colors
pub inline fn bg(comptime color: Color) []const u8 {
return utils.comptimeCsi("48;5;{d}m", .{@intFromEnum(color)});
}
/// Returns a string to change text foreground using rgb colors
/// Uses a buffer.
pub inline fn fgRGB(r: u8, g: u8, b: u8) []const u8 {
var buf: [22]u8 = undefined;
return fmt.bufPrint(&buf, "\x1b[38;2;{d};{d};{d}m", .{ r, g, b }) catch unreachable;
}
/// Returns a string to change text background using rgb colors
/// Uses a buffer.
pub inline fn bgRGB(r: u8, g: u8, b: u8) []const u8 {
var buf: [22]u8 = undefined;
return fmt.bufPrint(&buf, "\x1b[48;2;{d};{d};{d}m", .{ r, g, b }) catch unreachable;
}
pub const reset = utils.comptimeCsi("0m", .{});
};
/// Writes the escape sequence code to change foreground to `color` (using 256 colors)
pub fn fg256(writer: anytype, color: Color) !void {
return std.fmt.format(writer, utils.csi ++ utils.fg_256 ++ "{d}m", .{@intFromEnum(color)});
}
/// Writes the escape sequence code to change background to `color` (using 256 colors)
pub fn bg256(writer: anytype, color: Color) !void {
return std.fmt.format(writer, utils.csi ++ utils.bg_256 ++ "{d}m", .{@intFromEnum(color)});
}
/// Writes the escape sequence code to change foreground to rgb color
pub fn fgRGB(writer: anytype, r: u8, g: u8, b: u8) !void {
return std.fmt.format(writer, utils.csi ++ utils.fg_rgb ++ "{d};{d};{d}m", .{ r, g, b });
}
/// Writes the escape sequence code to change background to rgb color
pub fn bgRGB(writer: anytype, r: u8, g: u8, b: u8) !void {
return std.fmt.format(writer, utils.csi ++ utils.bg_rgb ++ "{d};{d};{d}m", .{ r, g, b });
}
/// Writes the escape code to reset style and color
pub fn resetAll(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ utils.reset_all, .{});
}
|
0 |
repos/mibu
|
repos/mibu/src/term.zig
|
const std = @import("std");
const os = std.os;
const io = std.io;
const posix = std.posix;
const builtin = @import("builtin");
/// ReadMode defines the read behaivour when using raw mode
pub const ReadMode = enum {
blocking,
nonblocking,
};
pub fn enableRawMode(handle: posix.fd_t, blocking: ReadMode) !RawTerm {
// var original_termios = try os.tcgetattr(handle);
const original_termios = try posix.tcgetattr(handle);
var termios = original_termios;
// https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html
// All of this are bitflags, so we do NOT and then AND to disable
// ICRNL (iflag) : fix CTRL-M (carriage returns)
// IXON (iflag) : disable Ctrl-S and Ctrl-Q
// OPOST (oflag) : turn off all output processing
// ECHO (lflag) : disable prints every key to terminal
// ICANON (lflag): disable to reads byte per byte instead of line (or when user press enter)
// IEXTEN (lflag): disable Ctrl-V
// ISIG (lflag) : disable Ctrl-C and Ctrl-Z
// Miscellaneous flags (most modern terminal already have them disabled)
// BRKINT, INPCK, ISTRIP and CS8
termios.iflag.BRKINT = false;
termios.iflag.ICRNL = false;
termios.iflag.INPCK = false;
termios.iflag.ISTRIP = false;
termios.iflag.IXON = false;
termios.oflag.OPOST = false;
termios.cflag.CSIZE = .CS8;
termios.lflag.ECHO = false;
termios.lflag.ICANON = false;
termios.lflag.IEXTEN = false;
termios.lflag.ISIG = false;
switch (blocking) {
// Wait until it reads at least one byte
.blocking => termios.cc[@intFromEnum(posix.V.MIN)] = 1,
// Don't wait
.nonblocking => termios.cc[@intFromEnum(posix.V.MIN)] = 0,
}
// Wait 100 miliseconds at maximum.
termios.cc[@intFromEnum(posix.V.TIME)] = 1;
// apply changes
try posix.tcsetattr(handle, .FLUSH, termios);
return RawTerm{
.orig_termios = original_termios,
.handle = handle,
};
}
/// A raw terminal representation, you can enter terminal raw mode
/// using this struct. Raw mode is essential to create a TUI.
pub const RawTerm = struct {
orig_termios: std.posix.termios,
/// The OS-specific file descriptor or file handle.
handle: os.linux.fd_t,
const Self = @This();
/// Returns to the previous terminal state
pub fn disableRawMode(self: *Self) !void {
try posix.tcsetattr(self.handle, .FLUSH, self.orig_termios);
}
};
/// Returned by `getSize()`
pub const TermSize = struct {
width: u16,
height: u16,
};
/// Get the terminal size, use `fd` equals to 0 use stdin
pub fn getSize(fd: posix.fd_t) !TermSize {
if (builtin.os.tag != .linux) {
return error.UnsupportedPlatform;
}
var ws: posix.winsize = undefined;
// https://github.com/ziglang/zig/blob/master/lib/std/os/linux/errno/generic.zig
const err = std.os.linux.ioctl(fd, posix.T.IOCGWINSZ, @intFromPtr(&ws));
if (posix.errno(err) != .SUCCESS) {
return error.IoctlError;
}
return TermSize{
.width = ws.ws_col,
.height = ws.ws_row,
};
}
test "entering stdin raw mode" {
const tty = (try std.fs.cwd().openFile("/dev/tty", .{})).reader();
const termsize = try getSize(tty.context.handle);
std.debug.print("Terminal size: {d}x{d}\n", .{ termsize.width, termsize.height });
// stdin.handle is the same as os.STDIN_FILENO
// var term = try enableRawMode(tty.context.handle, .blocking);
// defer term.disableRawMode() catch {};
}
|
0 |
repos/mibu
|
repos/mibu/src/utils.zig
|
const std = @import("std");
pub const csi = "\x1b[";
/// Sequence to set foreground color using 256 colors table
pub const fg_256 = "38;5;";
/// Sequence to set foreground color using 256 colors table
pub const bg_256 = "48;5;";
/// Sequence to set foreground color using 256 colors table
pub const fg_rgb = "38;2;";
/// Sequence to set foreground color using 256 colors table
pub const bg_rgb = "48;2;";
/// Sequence to reset color and style
pub const reset_all = "0m";
/// Sequence to clear from cursor until end of screen
pub const clear_screen_from_cursor = "0J";
/// Sequence to clear from beginning to cursor.
pub const clear_screen_to_cursor = "1J";
/// Sequence to clear all screen
pub const clear_all = "2J";
/// Clear from cursor to end of line
pub const clear_line_from_cursor = "0K";
/// Clear start of line to the cursor
pub const clear_line_to_cursor = "1K";
/// Clear entire line
pub const clear_line = "2K";
/// Returns the ANSI sequence to set bold mode
pub const style_bold = "1m";
pub const style_no_bold = "22m";
/// Returns the ANSI sequence to set dim mode
pub const style_dim = "2m";
pub const style_no_dim = "22m";
/// Returnstyle_s the ANSI sequence to set italic mode
pub const style_italic = "3m";
pub const style_no_italic = "23m";
/// Returnstyle_s the ANSI sequence to set underline mode
pub const style_underline = "4m";
pub const style_no_underline = "24m";
/// Returnstyle_s the ANSI sequence to set blinking mode
pub const style_blinking = "5m";
pub const style_no_blinking = "25m";
/// Returnstyle_s the ANSI sequence to set reverse mode
pub const style_reverse = "7m";
pub const style_no_reverse = "27m";
/// Returnstyle_s the ANSI sequence to set hidden/invisible mode
pub const style_invisible = "8m";
pub const style_no_invisible = "28m";
/// Returnstyle_s the ANSI sequence to set strikethrough mode
pub const style_strikethrough = "9m";
pub const style_no_strikethrough = "29m";
// When enable_mouse_tracking is sent to the terminal
// mouse events will be received
pub const enable_mouse_tracking = "\x1b[?1003h";
// When disable_mouse_tracking is sent to the terminal
// mouse events will stop being received. Needs to be
// called after enable_mouse_tracking, otherwise the
// terminal will not stop sending mouse events, even when the program
// has finished.
pub const disable_mouse_tracking = "\x1b[?1003l";
pub inline fn comptimeCsi(comptime fmt: []const u8, args: anytype) []const u8 {
const str = "\x1b[" ++ fmt;
return std.fmt.comptimePrint(str, args);
}
|
0 |
repos/mibu
|
repos/mibu/src/cursor.zig
|
const std = @import("std");
const fmt = std.fmt;
const lib = @import("main.zig");
const term = lib.term;
const utils = lib.utils;
pub const print = struct {
/// Moves cursor to `x` column and `y` row
pub inline fn goTo(x: anytype, y: anytype) []const u8 {
// i guess is ok with this size for now
var buf: [30]u8 = undefined;
return fmt.bufPrint(&buf, "\x1b[{d};{d}H", .{ y, x }) catch unreachable;
}
/// Moves cursor up `y` rows
pub inline fn goUp(y: anytype) []const u8 {
// i guess is ok with this size for now
var buf: [30]u8 = undefined;
return fmt.bufPrint(&buf, "\x1b[{d}A", .{y}) catch unreachable;
}
/// Moves cursor down `y` rows
pub inline fn goDown(y: anytype) []const u8 {
// i guess is ok with this size for now
var buf: [30]u8 = undefined;
return fmt.bufPrint(&buf, "\x1b[{d}A", .{y}) catch unreachable;
}
/// Moves cursor left `x` columns
pub inline fn goLeft(x: anytype) []const u8 {
// i guess is ok with this size for now
var buf: [30]u8 = undefined;
return fmt.bufPrint(&buf, "\x1b[{d}D", .{x}) catch unreachable;
}
/// Moves cursor right `x` columns
pub inline fn goRight(x: anytype) []const u8 {
// i guess is ok with this size for now
var buf: [30]u8 = undefined;
return fmt.bufPrint(&buf, "\x1b[{d}C", .{x}) catch unreachable;
}
/// Hide the cursor
pub inline fn hide() []const u8 {
return utils.comptimeCsi("?25l", .{});
}
/// Show the cursor
pub inline fn show() []const u8 {
return utils.comptimeCsi("?25h", .{});
}
/// Save cursor position
pub inline fn save() []const u8 {
return utils.comptimeCsi("u", .{});
}
/// Restore cursor position
pub inline fn restore() []const u8 {
return utils.comptimeCsi("s", .{});
}
};
/// Moves cursor to `x` column and `y` row
pub fn goTo(writer: anytype, x: anytype, y: anytype) !void {
return std.fmt.format(writer, utils.csi ++ "{d};{d}H", .{ y, x });
}
/// Moves cursor up `y` rows
pub fn goUp(writer: anytype, y: anytype) !void {
return std.fmt.format(writer, utils.csi ++ "{d}A", .{y});
}
/// Moves cursor down `y` rows
pub fn goDown(writer: anytype, y: anytype) !void {
return std.fmt.format(writer, utils.csi ++ "{d}B", .{y});
}
/// Moves cursor left `x` columns
pub fn goLeft(writer: anytype, x: anytype) !void {
return std.fmt.format(writer, utils.csi ++ "{d}D", .{x});
}
/// Moves cursor right `x` columns
pub fn goRight(writer: anytype, x: anytype) !void {
return std.fmt.format(writer, utils.csi ++ "{d}C", .{x});
}
/// Hide the cursor
pub fn hide(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ "?25l", .{});
}
/// Show the cursor
pub fn show(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ "?25h", .{});
}
/// Save cursor position
pub fn save(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ "u", .{});
}
/// Restore cursor position
pub fn restore(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ "s", .{});
}
|
0 |
repos/mibu
|
repos/mibu/src/event.zig
|
const std = @import("std");
const io = std.io;
const cursor = @import("cursor.zig");
const Key = union(enum) {
// unicode character
char: u21,
ctrl: u21,
alt: u21,
ctrl_alt: u21,
fun: u8,
// arrow keys
up: void,
down: void,
left: void,
right: void,
// shift + arrow keys
shift_up: void,
shift_down: void,
shift_left: void,
shift_right: void,
// ctrl + arrow keys
ctrl_up: void,
ctrl_down: void,
ctrl_left: void,
ctrl_right: void,
// ctrl + shift + arrow keys
ctrl_shift_up: void,
ctrl_shift_down: void,
ctrl_shift_left: void,
ctrl_shift_right: void,
// ctrl + alt + arrow keys
ctrl_alt_up: void,
ctrl_alt_down: void,
ctrl_alt_left: void,
ctrl_alt_right: void,
// special keys
esc: void,
backspace: void,
delete: void,
insert: void,
enter: void,
page_up: void,
page_down: void,
home: void,
end: void,
__non_exhaustive: void,
pub fn format(
value: Key,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = options;
_ = fmt;
try writer.writeAll("Key.");
switch (value) {
.ctrl => |c| try std.fmt.format(writer, "ctrl({u})", .{c}),
.alt => |c| try std.fmt.format(writer, "alt({u})", .{c}),
.ctrl_alt => |c| try std.fmt.format(writer, "ctrl_alt({u})", .{c}),
.char => |c| try std.fmt.format(writer, "char({u})", .{c}),
.fun => |d| try std.fmt.format(writer, "fun({d})", .{d}),
// arrow keys
.up => try std.fmt.format(writer, "up", .{}),
.down => try std.fmt.format(writer, "down", .{}),
.left => try std.fmt.format(writer, "left", .{}),
.right => try std.fmt.format(writer, "right", .{}),
// shift + arrow keys
.shift_up => try std.fmt.format(writer, "shift_up", .{}),
.shift_down => try std.fmt.format(writer, "shift_down", .{}),
.shift_left => try std.fmt.format(writer, "shift_left", .{}),
.shift_right => try std.fmt.format(writer, "shift_right", .{}),
// ctrl + arrow keys
.ctrl_up => try std.fmt.format(writer, "ctrl_up", .{}),
.ctrl_down => try std.fmt.format(writer, "ctrl_down", .{}),
.ctrl_left => try std.fmt.format(writer, "ctrl_left", .{}),
.ctrl_right => try std.fmt.format(writer, "ctrl_right", .{}),
// ctrl + shift + arrow keys
.ctrl_shift_up => try std.fmt.format(writer, "ctrl_shift_up", .{}),
.ctrl_shift_down => try std.fmt.format(writer, "ctrl_shift_down", .{}),
.ctrl_shift_left => try std.fmt.format(writer, "ctrl_shift_left", .{}),
.ctrl_shift_right => try std.fmt.format(writer, "ctrl_shift_right", .{}),
// ctrl + alt + arrow keys
.ctrl_alt_up => try std.fmt.format(writer, "ctrl_alt_up", .{}),
.ctrl_alt_down => try std.fmt.format(writer, "ctrl_alt_down", .{}),
.ctrl_alt_left => try std.fmt.format(writer, "ctrl_alt_left", .{}),
.ctrl_alt_right => try std.fmt.format(writer, "ctrl_alt_right", .{}),
// special keys
.esc => try std.fmt.format(writer, "esc", .{}),
.enter => try std.fmt.format(writer, "enter", .{}),
.backspace => try std.fmt.format(writer, "backspace", .{}),
.delete => try std.fmt.format(writer, "delete", .{}),
.insert => try std.fmt.format(writer, "insert", .{}),
.page_up => try std.fmt.format(writer, "page_up", .{}),
.page_down => try std.fmt.format(writer, "page_down", .{}),
.home => try std.fmt.format(writer, "home", .{}),
.end => try std.fmt.format(writer, "end", .{}),
else => try std.fmt.format(writer, "Not available yet", .{}),
}
}
};
pub const MouseButton = enum {
left,
middle,
right,
release,
scroll_up,
scroll_down,
move,
move_rightclick,
__non_exhaustive,
};
pub const Mouse = struct {
x: u16,
y: u16,
button: MouseButton,
is_alt: bool,
is_shift: bool,
is_ctrl: bool,
pub fn format(
value: Mouse,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = options;
_ = fmt;
try writer.writeAll("Mouse.");
try writer.print("x: {d}, y: {d}, button: {any}, is_alt: {any}, is_shift: {any}, is_ctrl: {any}", .{ value.x, value.y, value.button, value.is_alt, value.is_shift, value.is_ctrl });
}
};
/// Returns the next event received.
/// When used with canonical mode, the user needs to press enter to receive the event.
/// When raw term is `.blocking` it will block until read at least one event.
/// otherwise it will return `.none` if it didnt read any event
///
/// `in`: needs to be reader
pub fn next(in: anytype) !Event {
// TODO: Check buffer size
var buf: [20]u8 = undefined;
const c = try in.read(&buf);
if (c == 0) {
return .none;
}
// const view = try std.unicode.Utf8View.init(buf[0..c]);
// This is hacky to make mouse code work
// utf8 view failes to parse mouse events, dont know why, need to check it later
// TODO: check why utf8 view fails to parse mouse events
// It's related with the value, if its greater than 127, it fails
const view = std.unicode.Utf8View.init(buf[0..c]) catch {
return parse_csi(buf[2..c]);
};
var iter = view.iterator();
const event: Event = .none;
// std.debug.print("\n\r{any}\n", .{view});
// TODO: Find a better way to iterate buffer
if (iter.nextCodepoint()) |c0| switch (c0) {
'\x1b' => {
if (iter.nextCodepoint()) |c1| switch (c1) {
// fn (1 - 4)
// O - 0x6f - 111
'\x4f' => {
return Event{ .key = Key{ .fun = (1 + buf[2] - '\x50') } };
},
// csi
'[' => {
return try parse_csi(buf[2..c]);
},
'\x01'...'\x0C', '\x0E'...'\x1A' => return Event{ .key = Key{ .ctrl_alt = c1 + '\x60' } },
// alt key
else => {
return Event{ .key = Key{ .alt = c1 } };
},
} else {
return Event{ .key = .esc };
}
},
// tab is equal to ctrl-i
// ctrl keys (avoids ctrl-m)
'\x01'...'\x0C', '\x0E'...'\x1A' => return Event{ .key = Key{ .ctrl = c0 + '\x60' } },
// special chars
'\x7f' => return Event{ .key = .backspace },
'\x0D' => return Event{ .key = .enter },
// chars and shift + chars
else => return Event{ .key = Key{ .char = c0 } },
};
return event;
}
fn parse_csi(buf: []const u8) !Event {
switch (buf[0]) {
// keys
'A' => return Event{ .key = .up },
'B' => return Event{ .key = .down },
'C' => return Event{ .key = .right },
'D' => return Event{ .key = .left },
'1' => {
switch (buf[1]) {
'5' => return Event{ .key = Key{ .fun = 5 } },
'7' => return Event{ .key = Key{ .fun = 6 } },
'8' => return Event{ .key = Key{ .fun = 7 } },
'9' => return Event{ .key = Key{ .fun = 8 } },
'~' => return Event{ .key = .home },
// shift + arrow keys
';' => {
switch (buf[2]) {
'2' => {
switch (buf[3]) {
'A' => return Event{ .key = .shift_up },
'B' => return Event{ .key = .shift_down },
'C' => return Event{ .key = .shift_right },
'D' => return Event{ .key = .shift_left },
else => {},
}
},
'5' => {
switch (buf[3]) {
'A' => return Event{ .key = .ctrl_up },
'B' => return Event{ .key = .ctrl_down },
'C' => return Event{ .key = .ctrl_right },
'D' => return Event{ .key = .ctrl_left },
else => {},
}
},
'6' => {
switch (buf[3]) {
'A' => return Event{ .key = .ctrl_shift_up },
'B' => return Event{ .key = .ctrl_shift_down },
'C' => return Event{ .key = .ctrl_shift_right },
'D' => return Event{ .key = .ctrl_shift_left },
else => {},
}
},
'7' => {
switch (buf[3]) {
'A' => return Event{ .key = .ctrl_alt_up },
'B' => return Event{ .key = .ctrl_alt_down },
'C' => return Event{ .key = .ctrl_alt_right },
'D' => return Event{ .key = .ctrl_alt_left },
else => {},
}
},
else => {},
}
},
else => {},
}
},
'2' => {
switch (buf[1]) {
'0' => return Event{ .key = Key{ .fun = 9 } },
'1' => return Event{ .key = Key{ .fun = 10 } },
'3' => return Event{ .key = Key{ .fun = 11 } },
'4' => return Event{ .key = Key{ .fun = 12 } },
'~' => return Event{ .key = .insert },
else => {},
}
},
'3' => return Event{ .key = .delete },
'4' => return Event{ .key = .end },
'5' => return Event{ .key = .page_up },
'6' => return Event{ .key = .page_down },
// Mouse Events
// On button press, xterm sends CSI MCbCxCy (6 characters) = "\x1b[MbCxCy"
// - Cb is button-1, where button is 1, 2 or 3.
// - Cx and Cy are the x and y coordinates of the mouse when the button
// was pressed.
'M' => {
const x = buf[2];
const y = buf[3];
var mouse_event = parse_mouse_action(buf[1]) catch {
return .not_supported;
};
// x and y are 1-based
mouse_event.x = x - 1;
mouse_event.y = y - 1;
return Event{ .mouse = mouse_event };
},
else => {},
}
return .not_supported;
}
fn parse_mouse_action(action: u8) !Mouse {
// Normal tracking mode sends an escape sequence on both button press and
// release. Modifier key (shift, ctrl, meta) information is also sent. It
// is enabled by specifying parameter 1000 to DECSET. On button press or
// release, xterm sends CSI M CbCxCy.
//
// o The low two bits of Cb encode button information:
//
// 0=MB1 pressed,
// 1=MB2 pressed,
// 2=MB3 pressed, and
// 3=release.
//
// o The next three bits encode the modifiers which were down when the
// button was pressed and are added together:
//
// 4=Shift,
// 8=Meta, and
// 16=Control.
var mouse_event = Mouse{
.x = 0,
.y = 0,
.button = MouseButton.left,
.is_alt = false,
.is_shift = false,
.is_ctrl = false,
};
// modifiers
mouse_event.is_shift = action & 0b0000_01000 != 0;
mouse_event.is_alt = action & 0b0000_1000 != 0;
mouse_event.is_ctrl = action & 0b0001_0000 != 0;
if (action & 0b0100_0000 != 0) {
// Click and move mouse results into the following events:
// 1. Left/Middle/Right click
// 2. Scroll up/down (where left click is scroll up and middle/right click is scroll down)
//
// So to get the "drag" event, it needs to be handled in the frontend
// or the main application.
// EDIT: ok, so right click and move has an own event
// TODO: check mouse
switch (action & 0b0000_0011) {
0 => mouse_event.button = MouseButton.scroll_up,
1 => mouse_event.button = MouseButton.scroll_down,
2 => mouse_event.button = MouseButton.move_rightclick,
3 => mouse_event.button = MouseButton.move,
else => return error.InvalidMouseButton,
}
return mouse_event;
}
// button clicks
mouse_event.button = switch (action & 0b0000_0011) {
0 => MouseButton.left,
1 => MouseButton.middle,
2 => MouseButton.right,
3 => MouseButton.release,
else => return error.InvalidMouseButton,
};
return mouse_event;
}
pub const Event = union(enum) {
key: Key,
// TODO: Signal = SIGWINCH
// Probably, we will need a separate thread to handle signals
resize,
not_supported,
// mouse: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking
mouse: Mouse,
none,
};
test "next" {
const term = @import("main.zig").term;
const tty = (try std.fs.cwd().openFile("/dev/tty", .{})).reader();
var raw = try term.enableRawMode(tty.context.handle, .blocking);
defer raw.disableRawMode() catch {};
var i: usize = 0;
while (i < 3) : (i += 1) {
const key = try next(tty);
std.debug.print("\n\r{any}\n", .{key});
}
}
|
0 |
repos/mibu
|
repos/mibu/src/main.zig
|
const std = @import("std");
pub const clear = @import("clear.zig");
pub const color = @import("color.zig");
pub const cursor = @import("cursor.zig");
pub const style = @import("style.zig");
pub const utils = @import("utils.zig");
pub const term = @import("term.zig");
pub const events = @import("event.zig");
test {
std.testing.refAllDecls(@This());
}
|
0 |
repos/mibu
|
repos/mibu/src/style.zig
|
const utils = @import("main.zig").utils;
const std = @import("std");
pub const print = struct {
/// Returns the ANSI sequence as a []const u8
pub const reset = utils.comptimeCsi(utils.reset, .{});
/// Returns the ANSI sequence to set bold mode
pub const bold = utils.comptimeCsi(utils.style_bold, .{});
pub const no_bold = utils.comptimeCsi(utils.style_no_bold, .{});
/// Returns the ANSI sequence to set dim mode
pub const dim = utils.comptimeCsi(utils.style_dim, .{});
pub const no_dim = utils.comptimeCsi(utils.style_no_dim, .{});
/// Returns the ANSI sequence to set italic mode
pub const italic = utils.comptimeCsi(utils.style_italic, .{});
pub const no_italic = utils.comptimeCsi(utils.style_no_italic, .{});
/// Returns the ANSI sequence to set underline mode
pub const underline = utils.comptimeCsi(utils.style_underline, .{});
pub const no_underline = utils.comptimeCsi(utils.style_no_underline, .{});
/// Returns the ANSI sequence to set blinking mode
pub const blinking = utils.comptimeCsi(utils.style_blinking, .{});
pub const no_blinking = utils.comptimeCsi(utils.style_no_blinking, .{});
/// Returns the ANSI sequence to set reverse mode
pub const reverse = utils.comptimeCsi(utils.style_reverse, .{});
pub const no_reverse = utils.comptimeCsi(utils.style_no_reverse, .{});
/// Returns the ANSI sequence to set hidden/invisible mode
pub const invisible = utils.comptimeCsi(utils.style_invisible, .{});
pub const no_invisible = utils.comptimeCsi(utils.style_no_invisible, .{});
/// Returns the ANSI sequence to set strikethrough mode
pub const strikethrough = utils.comptimeCsi(utils.style_strikethrough, .{});
pub const no_strikethrough = utils.comptimeCsi(utils.style_no_strikethrough, .{});
};
/// Returns the ANSI sequence as a []const u8
pub fn reset(writer: anytype) !void {
return std.fmt.format(writer, print.reset, .{});
}
/// Returns the ANSI sequence to set bold mode
pub fn bold(writer: anytype) !void {
return std.fmt.format(writer, print.bold, .{});
}
/// Returns the ANSI sequence to unset bold mode
pub fn noBold(writer: anytype) !void {
return std.fmt.format(writer, print.no_bold, .{});
}
/// Returns the ANSI sequence to set dim mode
pub fn dim(writer: anytype) !void {
return std.fmt.format(writer, print.dim, .{});
}
/// Returns the ANSI sequence to unset dim mode
pub fn noDim(writer: anytype) !void {
return std.fmt.format(writer, print.no_dim, .{});
}
/// Returns the ANSI sequence to set italic mode
pub fn italic(writer: anytype) !void {
return std.fmt.format(writer, print.italic, .{});
}
/// Returns the ANSI sequence to unset italic mode
pub fn noItalic(writer: anytype) !void {
return std.fmt.format(writer, print.no_italic, .{});
}
/// Returns the ANSI sequence to set underline mode
pub fn underline(writer: anytype) !void {
return std.fmt.format(writer, print.underline, .{});
}
/// Returns the ANSI sequence to unset underline mode
pub fn noUnderline(writer: anytype) !void {
return std.fmt.format(writer, print.no_underline, .{});
}
/// Returns the ANSI sequence to set blinking mode
pub fn blinking(writer: anytype) !void {
return std.fmt.format(writer, print.blinking, .{});
}
/// Returns the ANSI sequence to unset blinking mode
pub fn noBlinking(writer: anytype) !void {
return std.fmt.format(writer, print.no_blinking, .{});
}
/// Returns the ANSI sequence to set reverse mode
pub fn reverse(writer: anytype) !void {
return std.fmt.format(writer, print.reverse, .{});
}
/// Returns the ANSI sequence to unset reverse mode
pub fn noReverse(writer: anytype) !void {
return std.fmt.format(writer, print.no_reverse, .{});
}
/// Returns the ANSI sequence to set hidden/invisible mode
pub fn hidden(writer: anytype) !void {
return std.fmt.format(writer, print.invisible, .{});
}
/// Returns the ANSI sequence to unset hidden/invisible mode
pub fn noHidden(writer: anytype) !void {
return std.fmt.format(writer, print.no_invisible, .{});
}
/// Returns the ansi sequence to set strikethrough mode
pub fn strikethrough(writer: anytype) !void {
return std.fmt.format(writer, print.strikethrough, .{});
}
/// Returns the ansi sequence to unset strikethrough mode
pub fn noStrikethrough(writer: anytype) !void {
return std.fmt.format(writer, print.no_strikethrough, .{});
}
|
0 |
repos/mibu
|
repos/mibu/src/clear.zig
|
//! Clear screen.
//! Note: Clear doesn't move the cursor, so the cursor will stay at the same position,
//! to move cursor check `Cursor`.
const std = @import("std");
const lib = @import("main.zig");
const utils = lib.utils;
pub const print = struct {
/// Clear from cursor until end of screen
pub const screen_from_cursor = utils.comptimeCsi("0J", .{});
/// Clear from cursor to beginning of screen
pub const screen_to_cursor = utils.comptimeCsi("1J", .{});
/// Clear all screen
pub const all = utils.comptimeCsi("2J", .{});
/// Clear from cursor to end of line
pub const line_from_cursor = utils.comptimeCsi("0K", .{});
/// Clear start of line to the cursor
pub const line_to_cursor = utils.comptimeCsi("1K", .{});
/// Clear entire line
pub const line = utils.comptimeCsi("2K", .{});
};
/// Clear from cursor until end of screen
pub fn screenFromCursor(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ utils.clear_screen_from_cursor, .{});
}
/// Clear from cursor to beginning of screen
pub fn screenToCursor(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ utils.clear_screen_to_cursor, .{});
}
/// Clear all screen
pub fn all(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ utils.clear_all, .{});
}
/// Clear from cursor to end of line
pub fn line_from_cursor(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ utils.clear_line_from_cursor, .{});
}
/// Clear start of line to the cursor
pub fn line_to_cursor(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ utils.clear_line_to_cursor, .{});
}
/// Clear entire line
pub fn entire_line(writer: anytype) !void {
return std.fmt.format(writer, utils.csi ++ utils.clear_line, .{});
}
|
0 |
repos/mibu
|
repos/mibu/examples/color.zig
|
const std = @import("std");
const io = std.io;
const mibu = @import("mibu");
const color = mibu.color;
const cursor = mibu.cursor;
pub fn main() !void {
const stdout = io.getStdOut();
try stdout.writer().print("{s}Warning text\n", .{color.print.fg(.red)});
try color.fg256(stdout.writer(), .blue);
try stdout.writer().print("Blue text\n", .{});
try color.fgRGB(stdout.writer(), 97, 37, 160);
try stdout.writer().print("Purple text\n", .{});
}
|
0 |
repos/mibu
|
repos/mibu/examples/event.zig
|
const std = @import("std");
const io = std.io;
const mibu = @import("mibu");
const events = mibu.events;
const term = mibu.term;
const utils = mibu.utils;
pub fn main() !void {
const stdin = io.getStdIn();
const stdout = io.getStdOut();
// Enable terminal raw mode, its very recommended when listening for events
var raw_term = try term.enableRawMode(stdin.handle, .blocking);
defer raw_term.disableRawMode() catch {};
// To listen mouse events, we need to enable mouse tracking
try stdout.writer().print("{s}", .{utils.enable_mouse_tracking});
defer stdout.writer().print("{s}", .{utils.disable_mouse_tracking}) catch {};
try stdout.writer().print("Press q or Ctrl-C to exit...\n\r", .{});
while (true) {
const next = try events.next(stdin);
switch (next) {
.key => |k| switch (k) {
// char can have more than 1 u8, because of unicode
.char => |c| switch (c) {
'q' => break,
else => try stdout.writer().print("Key char: {u}\n\r", .{c}),
},
.ctrl => |c| switch (c) {
'c' => break,
else => try stdout.writer().print("Key: {s}\n\r", .{k}),
},
else => try stdout.writer().print("Key: {s}\n\r", .{k}),
},
.mouse => |m| try stdout.writer().print("Mouse: {s}\n\r", .{m}),
// ex. mouse events not supported yet
else => try stdout.writer().print("Event: {any}\n\r", .{next}),
}
}
try stdout.writer().print("Bye bye\n\r", .{});
}
|
0 |
repos
|
repos/sokol/sokol_args.h
|
#if defined(SOKOL_IMPL) && !defined(SOKOL_ARGS_IMPL)
#define SOKOL_ARGS_IMPL
#endif
#ifndef SOKOL_ARGS_INCLUDED
/*
sokol_args.h -- cross-platform key/value arg-parsing for web and native
Project URL: https://github.com/floooh/sokol
Do this:
#define SOKOL_IMPL or
#define SOKOL_ARGS_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
Optionally provide the following defines with your own implementations:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_ARGS_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_ARGS_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
If sokol_args.h is compiled as a DLL, define the following before
including the declaration or implementation:
SOKOL_DLL
On Windows, SOKOL_DLL will define SOKOL_ARGS_API_DECL as __declspec(dllexport)
or __declspec(dllimport) as needed.
OVERVIEW
========
sokol_args.h provides a simple unified argument parsing API for WebAssembly and
native apps.
When running as a WebAssembly app, arguments are taken from the page URL:
https://floooh.github.io/tiny8bit/kc85.html?type=kc85_3&mod=m022&snapshot=kc85/jungle.kcc
The same arguments provided to a command line app:
kc85 type=kc85_3 mod=m022 snapshot=kc85/jungle.kcc
You can also use standalone keys without value:
https://floooh.github.io/tiny8bit/kc85.html?bla&blub
On the command line:
kc85 bla blub
Such value-less keys are reported as the value being an empty string, but they
can be tested with `sapp_exists("bla")` or `sapp_boolean("blub")`.
ARGUMENT FORMATTING
===================
On the web platform, arguments must be formatted as a valid URL query string
with 'percent encoding' used for special characters.
Strings are expected to be UTF-8 encoded (although sokol_args.h doesn't
contain any special UTF-8 handling). See below on how to obtain
UTF-8 encoded argc/argv values on Windows when using WinMain() as
entry point.
On native platforms the following rules must be followed:
Arguments have the general form
key=value
or
key
When a key has no value, the value will be assigned an empty string.
Key/value pairs are separated by 'whitespace', valid whitespace
characters are space and tab.
Whitespace characters in front and after the separating '=' character
are ignored:
key = value
...is the same as
key=value
The 'key' string must be a simple string without escape sequences or whitespace.
The 'value' string can be quoted, and quoted value strings can contain
whitespace:
key = 'single-quoted value'
key = "double-quoted value"
Single-quoted value strings can contain double quotes, and vice-versa:
key = 'single-quoted value "can contain double-quotes"'
key = "double-quoted value 'can contain single-quotes'"
Note that correct quoting can be tricky on some shells, since command
shells may remove quotes, unless they're escaped.
Value strings can contain a small selection of escape sequences:
\n - newline
\r - carriage return
\t - tab
\\ - escaped backslash
(more escape codes may be added in the future).
CODE EXAMPLE
============
int main(int argc, char* argv[]) {
// initialize sokol_args with default parameters
sargs_setup(&(sargs_desc){
.argc = argc,
.argv = argv
});
// check if a key exists...
if (sargs_exists("bla")) {
...
}
// get value string for key, if not found, return empty string ""
const char* val0 = sargs_value("bla");
// get value string for key, or default string if key not found
const char* val1 = sargs_value_def("bla", "default_value");
// check if a key matches expected value
if (sargs_equals("type", "kc85_4")) {
...
}
// check if a key's value is "true", "yes" or "on" or if this is a standalone key
if (sargs_boolean("joystick_enabled")) {
...
}
// iterate over keys and values
for (int i = 0; i < sargs_num_args(); i++) {
printf("key: %s, value: %s\n", sargs_key_at(i), sargs_value_at(i));
}
// lookup argument index by key string, will return -1 if key
// is not found, sargs_key_at() and sargs_value_at() will return
// an empty string for invalid indices
int index = sargs_find("bla");
printf("key: %s, value: %s\n", sargs_key_at(index), sargs_value_at(index));
// shutdown sokol-args
sargs_shutdown();
}
WINMAIN AND ARGC / ARGV
=======================
On Windows with WinMain() based apps, getting UTF8-encoded command line
arguments is a bit more complicated:
First call GetCommandLineW(), this returns the entire command line
as UTF-16 string. Then call CommandLineToArgvW(), this parses the
command line string into the usual argc/argv format (but in UTF-16).
Finally convert the UTF-16 strings in argv[] into UTF-8 via
WideCharToMultiByte().
See the function _sapp_win32_command_line_to_utf8_argv() in sokol_app.h
for example code how to do this (if you're using sokol_app.h, it will
already convert the command line arguments to UTF-8 for you of course,
so you can plug them directly into sokol_app.h).
API DOCUMENTATION
=================
void sargs_setup(const sargs_desc* desc)
Initialize sokol_args, desc contains the following configuration
parameters:
int argc - the main function's argc parameter
char** argv - the main function's argv parameter
int max_args - max number of key/value pairs, default is 16
int buf_size - size of the internal string buffer, default is 16384
Note that on the web, argc and argv will be ignored and the arguments
will be taken from the page URL instead.
sargs_setup() will allocate 2 memory chunks: one for keeping track
of the key/value args of size 'max_args*8', and a string buffer
of size 'buf_size'.
void sargs_shutdown(void)
Shutdown sokol-args and free any allocated memory.
bool sargs_isvalid(void)
Return true between sargs_setup() and sargs_shutdown()
bool sargs_exists(const char* key)
Test if an argument exists by its key name.
const char* sargs_value(const char* key)
Return value associated with key. Returns an empty string ("") if the
key doesn't exist, or if the key doesn't have a value.
const char* sargs_value_def(const char* key, const char* default)
Return value associated with key, or the provided default value if the
key doesn't exist, or this is a value-less key.
bool sargs_equals(const char* key, const char* val);
Return true if the value associated with key matches
the 'val' argument.
bool sargs_boolean(const char* key)
Return true if the value string of 'key' is one of 'true', 'yes', 'on',
or this is a key without value.
int sargs_find(const char* key)
Find argument by key name and return its index, or -1 if not found.
int sargs_num_args(void)
Return number of key/value pairs.
const char* sargs_key_at(int index)
Return the key name of argument at index. Returns empty string if
is index is outside range.
const char* sargs_value_at(int index)
Return the value of argument at index. Returns empty string
if the key at index has no value, or the index is out-of-range.
MEMORY ALLOCATION OVERRIDE
==========================
You can override the memory allocation functions at initialization time
like this:
void* my_alloc(size_t size, void* user_data) {
return malloc(size);
}
void my_free(void* ptr, void* user_data) {
free(ptr);
}
...
sargs_setup(&(sargs_desc){
// ...
.allocator = {
.alloc_fn = my_alloc,
.free_fn = my_free,
.user_data = ...,
}
});
...
If no overrides are provided, malloc and free will be used.
This only affects memory allocation calls done by sokol_args.h
itself though, not any allocations in OS libraries.
TODO
====
- parsing errors?
LICENSE
=======
zlib/libpng license
Copyright (c) 2018 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define SOKOL_ARGS_INCLUDED (1)
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h> // size_t
#if defined(SOKOL_API_DECL) && !defined(SOKOL_ARGS_API_DECL)
#define SOKOL_ARGS_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_ARGS_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_ARGS_IMPL)
#define SOKOL_ARGS_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_ARGS_API_DECL __declspec(dllimport)
#else
#define SOKOL_ARGS_API_DECL extern
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
sargs_allocator
Used in sargs_desc to provide custom memory-alloc and -free functions
to sokol_args.h. If memory management should be overridden, both the
alloc_fn and free_fn function must be provided (e.g. it's not valid to
override one function but not the other).
*/
typedef struct sargs_allocator {
void* (*alloc_fn)(size_t size, void* user_data);
void (*free_fn)(void* ptr, void* user_data);
void* user_data;
} sargs_allocator;
typedef struct sargs_desc {
int argc;
char** argv;
int max_args;
int buf_size;
sargs_allocator allocator;
} sargs_desc;
/* setup sokol-args */
SOKOL_ARGS_API_DECL void sargs_setup(const sargs_desc* desc);
/* shutdown sokol-args */
SOKOL_ARGS_API_DECL void sargs_shutdown(void);
/* true between sargs_setup() and sargs_shutdown() */
SOKOL_ARGS_API_DECL bool sargs_isvalid(void);
/* test if an argument exists by key name */
SOKOL_ARGS_API_DECL bool sargs_exists(const char* key);
/* get value by key name, return empty string if key doesn't exist or an existing key has no value */
SOKOL_ARGS_API_DECL const char* sargs_value(const char* key);
/* get value by key name, return provided default if key doesn't exist or has no value */
SOKOL_ARGS_API_DECL const char* sargs_value_def(const char* key, const char* def);
/* return true if val arg matches the value associated with key */
SOKOL_ARGS_API_DECL bool sargs_equals(const char* key, const char* val);
/* return true if key's value is "true", "yes", "on" or an existing key has no value */
SOKOL_ARGS_API_DECL bool sargs_boolean(const char* key);
/* get index of arg by key name, return -1 if not exists */
SOKOL_ARGS_API_DECL int sargs_find(const char* key);
/* get number of parsed arguments */
SOKOL_ARGS_API_DECL int sargs_num_args(void);
/* get key name of argument at index, or empty string */
SOKOL_ARGS_API_DECL const char* sargs_key_at(int index);
/* get value string of argument at index, or empty string */
SOKOL_ARGS_API_DECL const char* sargs_value_at(int index);
#ifdef __cplusplus
} /* extern "C" */
/* reference-based equivalents for c++ */
inline void sargs_setup(const sargs_desc& desc) { return sargs_setup(&desc); }
#endif
#endif // SOKOL_ARGS_INCLUDED
/*--- IMPLEMENTATION ---------------------------------------------------------*/
#ifdef SOKOL_ARGS_IMPL
#define SOKOL_ARGS_IMPL_INCLUDED (1)
#if defined(SOKOL_MALLOC) || defined(SOKOL_CALLOC) || defined(SOKOL_FREE)
#error "SOKOL_MALLOC/CALLOC/FREE macros are no longer supported, please use sargs_desc.allocator to override memory allocation functions"
#endif
#include <string.h> // memset, strcmp
#include <stdlib.h> // malloc, free
#if defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h>
#endif
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
#ifndef SOKOL_DEBUG
#ifndef NDEBUG
#define SOKOL_DEBUG
#endif
#endif
#ifndef SOKOL_ASSERT
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
#ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static
#else
#define _SOKOL_PRIVATE static
#endif
#endif
#define _sargs_def(val, def) (((val) == 0) ? (def) : (val))
#define _SARGS_MAX_ARGS_DEF (16)
#define _SARGS_BUF_SIZE_DEF (16*1024)
/* parser state */
#define _SARGS_EXPECT_KEY (1<<0)
#define _SARGS_EXPECT_SEP (1<<1)
#define _SARGS_EXPECT_VAL (1<<2)
#define _SARGS_PARSING_KEY (1<<3)
#define _SARGS_PARSING_VAL (1<<4)
#define _SARGS_ERROR (1<<5)
/* a key/value pair struct */
typedef struct {
int key; /* index to start of key string in buf */
int val; /* index to start of value string in buf */
} _sargs_kvp_t;
/* sokol-args state */
typedef struct {
int max_args; /* number of key/value pairs in args array */
int num_args; /* number of valid items in args array */
_sargs_kvp_t* args; /* key/value pair array */
int buf_size; /* size of buffer in bytes */
int buf_pos; /* current buffer position */
char* buf; /* character buffer, first char is reserved and zero for 'empty string' */
bool valid;
uint32_t parse_state;
char quote; /* current quote char, 0 if not in a quote */
bool in_escape; /* currently in an escape sequence */
sargs_allocator allocator;
} _sargs_state_t;
static _sargs_state_t _sargs;
/*== PRIVATE IMPLEMENTATION FUNCTIONS ========================================*/
_SOKOL_PRIVATE void _sargs_clear(void* ptr, size_t size) {
SOKOL_ASSERT(ptr && (size > 0));
memset(ptr, 0, size);
}
_SOKOL_PRIVATE void* _sargs_malloc(size_t size) {
SOKOL_ASSERT(size > 0);
void* ptr;
if (_sargs.allocator.alloc_fn) {
ptr = _sargs.allocator.alloc_fn(size, _sargs.allocator.user_data);
} else {
ptr = malloc(size);
}
SOKOL_ASSERT(ptr);
return ptr;
}
_SOKOL_PRIVATE void* _sargs_malloc_clear(size_t size) {
void* ptr = _sargs_malloc(size);
_sargs_clear(ptr, size);
return ptr;
}
_SOKOL_PRIVATE void _sargs_free(void* ptr) {
if (_sargs.allocator.free_fn) {
_sargs.allocator.free_fn(ptr, _sargs.allocator.user_data);
} else {
free(ptr);
}
}
_SOKOL_PRIVATE void _sargs_putc(char c) {
if ((_sargs.buf_pos+2) < _sargs.buf_size) {
_sargs.buf[_sargs.buf_pos++] = c;
}
}
_SOKOL_PRIVATE const char* _sargs_str(int index) {
SOKOL_ASSERT((index >= 0) && (index < _sargs.buf_size));
return &_sargs.buf[index];
}
/*-- argument parser functions ------------------*/
_SOKOL_PRIVATE void _sargs_expect_key(void) {
_sargs.parse_state = _SARGS_EXPECT_KEY;
}
_SOKOL_PRIVATE bool _sargs_key_expected(void) {
return 0 != (_sargs.parse_state & _SARGS_EXPECT_KEY);
}
_SOKOL_PRIVATE void _sargs_expect_val(void) {
_sargs.parse_state = _SARGS_EXPECT_VAL;
}
_SOKOL_PRIVATE bool _sargs_val_expected(void) {
return 0 != (_sargs.parse_state & _SARGS_EXPECT_VAL);
}
_SOKOL_PRIVATE void _sargs_expect_sep_or_key(void) {
_sargs.parse_state = _SARGS_EXPECT_SEP | _SARGS_EXPECT_KEY;
}
_SOKOL_PRIVATE bool _sargs_any_expected(void) {
return 0 != (_sargs.parse_state & (_SARGS_EXPECT_KEY | _SARGS_EXPECT_VAL | _SARGS_EXPECT_SEP));
}
_SOKOL_PRIVATE bool _sargs_is_separator(char c) {
return c == '=';
}
_SOKOL_PRIVATE bool _sargs_is_quote(char c) {
if (0 == _sargs.quote) {
return (c == '\'') || (c == '"');
}
else {
return c == _sargs.quote;
}
}
_SOKOL_PRIVATE void _sargs_begin_quote(char c) {
_sargs.quote = c;
}
_SOKOL_PRIVATE void _sargs_end_quote(void) {
_sargs.quote = 0;
}
_SOKOL_PRIVATE bool _sargs_in_quotes(void) {
return 0 != _sargs.quote;
}
_SOKOL_PRIVATE bool _sargs_is_whitespace(char c) {
return !_sargs_in_quotes() && ((c == ' ') || (c == '\t'));
}
_SOKOL_PRIVATE void _sargs_start_key(void) {
SOKOL_ASSERT((_sargs.num_args >= 0) && (_sargs.num_args < _sargs.max_args));
_sargs.parse_state = _SARGS_PARSING_KEY;
_sargs.args[_sargs.num_args].key = _sargs.buf_pos;
}
_SOKOL_PRIVATE void _sargs_end_key(void) {
SOKOL_ASSERT((_sargs.num_args >= 0) && (_sargs.num_args < _sargs.max_args));
_sargs_putc(0);
// declare val as empty string in case this is a key-only arg
_sargs.args[_sargs.num_args].val = _sargs.buf_pos - 1;
_sargs.num_args++;
_sargs.parse_state = 0;
}
_SOKOL_PRIVATE bool _sargs_parsing_key(void) {
return 0 != (_sargs.parse_state & _SARGS_PARSING_KEY);
}
_SOKOL_PRIVATE void _sargs_start_val(void) {
SOKOL_ASSERT((_sargs.num_args > 0) && (_sargs.num_args <= _sargs.max_args));
_sargs.parse_state = _SARGS_PARSING_VAL;
_sargs.args[_sargs.num_args - 1].val = _sargs.buf_pos;
}
_SOKOL_PRIVATE void _sargs_end_val(void) {
_sargs_putc(0);
_sargs.parse_state = 0;
}
_SOKOL_PRIVATE bool _sargs_is_escape(char c) {
return '\\' == c;
}
_SOKOL_PRIVATE void _sargs_start_escape(void) {
_sargs.in_escape = true;
}
_SOKOL_PRIVATE bool _sargs_in_escape(void) {
return _sargs.in_escape;
}
_SOKOL_PRIVATE char _sargs_escape(char c) {
switch (c) {
case 'n': return '\n';
case 't': return '\t';
case 'r': return '\r';
case '\\': return '\\';
default: return c;
}
}
_SOKOL_PRIVATE void _sargs_end_escape(void) {
_sargs.in_escape = false;
}
_SOKOL_PRIVATE bool _sargs_parsing_val(void) {
return 0 != (_sargs.parse_state & _SARGS_PARSING_VAL);
}
_SOKOL_PRIVATE bool _sargs_parse_carg(const char* src) {
char c;
while (0 != (c = *src++)) {
if (_sargs_in_escape()) {
c = _sargs_escape(c);
_sargs_end_escape();
}
else if (_sargs_is_escape(c)) {
_sargs_start_escape();
continue;
}
if (_sargs_any_expected()) {
if (!_sargs_is_whitespace(c)) {
/* start of key, value or separator */
if (_sargs_is_separator(c)) {
/* skip separator and expect value */
_sargs_expect_val();
continue;
}
else if (_sargs_key_expected()) {
/* start of new key */
_sargs_start_key();
}
else if (_sargs_val_expected()) {
/* start of value */
if (_sargs_is_quote(c)) {
_sargs_begin_quote(c);
continue;
}
_sargs_start_val();
}
}
else {
/* skip white space */
continue;
}
}
else if (_sargs_parsing_key()) {
if (_sargs_is_whitespace(c) || _sargs_is_separator(c)) {
/* end of key string */
_sargs_end_key();
if (_sargs_is_separator(c)) {
_sargs_expect_val();
}
else {
_sargs_expect_sep_or_key();
}
continue;
}
}
else if (_sargs_parsing_val()) {
if (_sargs_in_quotes()) {
/* when in quotes, whitespace is a normal character
and a matching quote ends the value string
*/
if (_sargs_is_quote(c)) {
_sargs_end_quote();
_sargs_end_val();
_sargs_expect_key();
continue;
}
}
else if (_sargs_is_whitespace(c)) {
/* end of value string (no quotes) */
_sargs_end_val();
_sargs_expect_key();
continue;
}
}
_sargs_putc(c);
}
if (_sargs_parsing_key()) {
_sargs_end_key();
_sargs_expect_sep_or_key();
}
else if (_sargs_parsing_val() && !_sargs_in_quotes()) {
_sargs_end_val();
_sargs_expect_key();
}
return true;
}
_SOKOL_PRIVATE bool _sargs_parse_cargs(int argc, const char** argv) {
_sargs_expect_key();
bool retval = true;
for (int i = 1; i < argc; i++) {
retval &= _sargs_parse_carg(argv[i]);
}
_sargs.parse_state = 0;
return retval;
}
/*-- EMSCRIPTEN IMPLEMENTATION -----------------------------------------------*/
#if defined(__EMSCRIPTEN__)
#ifdef __cplusplus
extern "C" {
#endif
#if defined(EM_JS_DEPS)
EM_JS_DEPS(sokol_audio, "$withStackSave,$stringToUTF8OnStack");
#endif
EMSCRIPTEN_KEEPALIVE void _sargs_add_kvp(const char* key, const char* val) {
SOKOL_ASSERT(_sargs.valid && key && val);
if (_sargs.num_args >= _sargs.max_args) {
return;
}
/* copy key string */
char c;
_sargs.args[_sargs.num_args].key = _sargs.buf_pos;
const char* ptr = key;
while (0 != (c = *ptr++)) {
_sargs_putc(c);
}
_sargs_putc(0);
/* copy value string */
_sargs.args[_sargs.num_args].val = _sargs.buf_pos;
ptr = val;
while (0 != (c = *ptr++)) {
_sargs_putc(c);
}
_sargs_putc(0);
_sargs.num_args++;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
/* JS function to extract arguments from the page URL */
EM_JS(void, sargs_js_parse_url, (void), {
const params = new URLSearchParams(window.location.search).entries();
for (let p = params.next(); !p.done; p = params.next()) {
const key = p.value[0];
const val = p.value[1];
withStackSave(() => {
const key_cstr = stringToUTF8OnStack(key);
const val_cstr = stringToUTF8OnStack(val);
__sargs_add_kvp(key_cstr, val_cstr)
});
}
});
#endif /* EMSCRIPTEN */
/*== PUBLIC IMPLEMENTATION FUNCTIONS =========================================*/
SOKOL_API_IMPL void sargs_setup(const sargs_desc* desc) {
SOKOL_ASSERT(desc);
_sargs_clear(&_sargs, sizeof(_sargs));
_sargs.max_args = _sargs_def(desc->max_args, _SARGS_MAX_ARGS_DEF);
_sargs.buf_size = _sargs_def(desc->buf_size, _SARGS_BUF_SIZE_DEF);
SOKOL_ASSERT(_sargs.buf_size > 8);
_sargs.args = (_sargs_kvp_t*) _sargs_malloc_clear((size_t)_sargs.max_args * sizeof(_sargs_kvp_t));
_sargs.buf = (char*) _sargs_malloc_clear((size_t)_sargs.buf_size * sizeof(char));
/* the first character in buf is reserved and always zero, this is the 'empty string' */
_sargs.buf_pos = 1;
_sargs.allocator = desc->allocator;
_sargs.valid = true;
/* parse argc/argv */
_sargs_parse_cargs(desc->argc, (const char**) desc->argv);
#if defined(__EMSCRIPTEN__)
/* on emscripten, also parse the page URL*/
sargs_js_parse_url();
#endif
}
SOKOL_API_IMPL void sargs_shutdown(void) {
SOKOL_ASSERT(_sargs.valid);
if (_sargs.args) {
_sargs_free(_sargs.args);
_sargs.args = 0;
}
if (_sargs.buf) {
_sargs_free(_sargs.buf);
_sargs.buf = 0;
}
_sargs.valid = false;
}
SOKOL_API_IMPL bool sargs_isvalid(void) {
return _sargs.valid;
}
SOKOL_API_IMPL int sargs_find(const char* key) {
SOKOL_ASSERT(_sargs.valid && key);
for (int i = 0; i < _sargs.num_args; i++) {
if (0 == strcmp(_sargs_str(_sargs.args[i].key), key)) {
return i;
}
}
return -1;
}
SOKOL_API_IMPL int sargs_num_args(void) {
SOKOL_ASSERT(_sargs.valid);
return _sargs.num_args;
}
SOKOL_API_IMPL const char* sargs_key_at(int index) {
SOKOL_ASSERT(_sargs.valid);
if ((index >= 0) && (index < _sargs.num_args)) {
return _sargs_str(_sargs.args[index].key);
}
else {
/* index 0 is always the empty string */
return _sargs_str(0);
}
}
SOKOL_API_IMPL const char* sargs_value_at(int index) {
SOKOL_ASSERT(_sargs.valid);
if ((index >= 0) && (index < _sargs.num_args)) {
return _sargs_str(_sargs.args[index].val);
}
else {
/* index 0 is always the empty string */
return _sargs_str(0);
}
}
SOKOL_API_IMPL bool sargs_exists(const char* key) {
SOKOL_ASSERT(_sargs.valid && key);
return -1 != sargs_find(key);
}
SOKOL_API_IMPL const char* sargs_value(const char* key) {
SOKOL_ASSERT(_sargs.valid && key);
return sargs_value_at(sargs_find(key));
}
SOKOL_API_IMPL const char* sargs_value_def(const char* key, const char* def) {
SOKOL_ASSERT(_sargs.valid && key && def);
int arg_index = sargs_find(key);
if (-1 != arg_index) {
const char* res = sargs_value_at(arg_index);
SOKOL_ASSERT(res);
if (res[0] == 0) {
return def;
} else {
return res;
}
}
else {
return def;
}
}
SOKOL_API_IMPL bool sargs_equals(const char* key, const char* val) {
SOKOL_ASSERT(_sargs.valid && key && val);
return 0 == strcmp(sargs_value(key), val);
}
SOKOL_API_IMPL bool sargs_boolean(const char* key) {
if (sargs_exists(key)) {
const char* val = sargs_value(key);
return (0 == strcmp("true", val)) ||
(0 == strcmp("yes", val)) ||
(0 == strcmp("on", val)) ||
(0 == strcmp("", val));
} else {
return false;
}
}
#endif /* SOKOL_ARGS_IMPL */
|
0 |
repos
|
repos/sokol/CHANGELOG.md
|
## Updates
### 02-Sep-2024
- Minor breaking change in sokol_gfx.h: The enum item `SG_FILTER_NONE` has been
removed. Until around Oct-2023 this was required to be used as mip-filter
on textures without mipmaps because of an unnecessary restriction in the
GL backend (see https://github.com/floooh/sokol/issues/929 for details).
The concept of a 'none' mipmap filter never mapped to some 3D backends
(specifically D3D11 and WebGPU).
If you are currently creating samplers with `.mipmap_filter = SG_FILTER_NONE`
you can simply remove that line. The new default value is `SG_FILTER_NEAREST`.
To restrict mipmap sampling access to a specific mipmap (or mipmap range),
use the `.min_lod` and `.max_lod` items in struct `sg_sampler_desc`.
The change has been implemented in PR https://github.com/floooh/sokol/pull/1103.
### 01-Sep-2024
- sokol_gfx.h d3d11: added a new configuration flag `d3d11_shader_debugging`
to the `sg_desc` struct. When this is true, D3D11 shaders which are provided
as HLSL source code will be compiled with debug information and no optimization
which allows shader debugging in tools like RenderDoc. If you use `sokol-shdc`
to build shaders, just omit the `--bytecode / -b` cmdline option to get
HLSL source code instead of bytecode, if you use the `fips` build system
wrapper (like the `sokol-samples` project), just replace the cmake macro
`sokol_shader()` with `sokol_shader_debuggable()`.
For details see issue https://github.com/floooh/sokol/issues/1043
and PR: https://github.com/floooh/sokol/pull/1101.
### 31-Aug-2024
- Some cleanup work in the WebGPU backend bindgroups cache which fixes
a number of issues: Destroying an image, sampler, storage buffer
or pipeline object now properly evicts any associated item in
the bindgroups cache and releases the associated WebGPU BindGroup
object. Doing this while the BindGroup is in flight also no longer
causes WebGPU errors.
For details see issue https://github.com/floooh/sokol/issues/1066
and PR https://github.com/floooh/sokol/pull/1097
- A fix in the sokol-zig bindings generator for a breaking naming convention
change in the Zig stdlib. The fix supports both the old and new naming
convention so that sokol-zig continues to be compatible with zig 0.13.0.
To update the sokol-zig dependency in your project, just run:
```
zig fetch --save=sokol git+https://github.com/floooh/sokol-zig.git
```
More Details in PR https://github.com/floooh/sokol/pull/1100
### 26-Aug-2024
A small behaviour update for sokol_gl.h (may be breaking if you call `sgl_error()`):
- Instead of skipping rendering completely for the current frame if an error is encountered
(for instance the vertex- or command-buffer running full), sokol-gl will now
render all successfully recorded draw commands before the error was recorded.
- Minor breaking change: `sgl_error_t` has been changed from an error code enum to
a struct with a boolean flag per error type, that way no error information is
lost if multiple error happen in the same frame.
- Two new functions to query the current number of recorded vertices and commands
in the current frame:
- `int sgl_num_vertices(void)`
- `int sgl_num_commands(void)`
Also see ticket https://github.com/floooh/sokol/issues/1092 and PR https://github.com/floooh/sokol/pull/1096 for details!
### 14-Aug-2024
The previously 'unofficial' Jai bindings at https://github.com/colinbellino/sokol-jai
have now been properly integrated with the sokol main repository (meaning that each
change to the sokol headers will update the bindings automatically).
The only missing part currently is that no test compilation happens in the CI
pipeline (that's also why the Jai bindings have no badge yet in the readme, I
think these things will have to wait until Jai leaves closed beta).
Many thanks to @colinbellino for creating the bindings scripts and preparing
the PR (https://github.com/floooh/sokol/pull/1090).
### 30-Jul-2024
Merged PR https://github.com/floooh/sokol/pull/1086 which adds Emscripten target platform
support for the Nim bindings. Please also see PR https://github.com/floooh/sokol-nim/pull/31
and the sokol-nim readme for details: https://github.com/floooh/sokol-nim
Many thanks to @Nazariglez for the PRs!
### 28-Jul-2024
sokol_gfx.h WebGL2: An important hotfix/workaround for a regression
in Chrome v127 on macOS and Safari Technology Preview 199 which broke all offscreen
rendering in sokol_gfx.h on WebGL2. The details are here https://github.com/floooh/sokol/issues/1085
and in this Chromium ticket: https://issues.chromium.org/issues/355605685.
The PR is here: https://github.com/floooh/sokol/pull/1087
It might take a little bit before the Chrome/Safari fix lands, and I fully expect
that the breakage will very slowly crawl through all sorts of other products
depending on Chromium (like VSCode, or the Qt WebView widget), so it made sense
to implement a workaround instead of waiting for the upstream fix to arrive.
The TL;DR is: A regression in the Chrome and Safari WebGL2 Metal backends
subtly breaks offscreen rendering for render target textures which have their
GL_TEXTURE_MAX_LEVEL set, but don't explicitly allocate texture storage
via the glTexStorage calls (this is entirely valid GL and WebGL2 though).
The breakage manifests as a 'stuck' offscreen rendering in Chrome, and as
a lost WebGL context in Safari Tech Preview (ok, that one isn't exactly 'subtle').
The workaround in the sokol_gfx.h GL backend is:
- on Emscripten only:
- for textures without initial data, explicitly allocate texture storage
via the glTexStorage functions
- and otherwise call the glTexImage functions as before
A better fix which I'll tackle later would be to rewrite the GL texture initialization
to generally use glTexStorage + glTexSubImage, but this will require a separate
fallback code path for macOS which doesn't have the glTexStorage calls because
GL on macOS is stuck at version 4.1, while glTexStorage has only been added in GL 4.2.
> NOTE: if you are affected by the breakage but cannot update to the most recent
sokol_gfx.h version, a simpler hotfix might be to just comment out this call
in `_sg_gl_create_image`, but this will only work for render target textures
with a single mip level (which is the common case though):
```c
glTexParameteri(img->gl.target, GL_TEXTURE_MAX_LEVEL, img->cmn.num_mipmaps - 1);
```
### 16-Jul-2024
sokol_app.h Linux: Fixed a long-standing issue on Linux where sokol-app key
up/down events were not keyboard layout independent. Instead the first keyboard
layout in the system settings would be used (this was responsible for why the
bug slipped through for so long, because on my Linux laptop I have a US layout
first in the list, followed by the German layout - this caused sokol-app key
codes to always be consistent with the US layout, even when the German layout
was selected, which is the intended behaviour. The bug only manifested itself
when moving the German layout into the top spot.
The fix has been adapted from GLFW by building a runtime-dynamic mapping table
from keyboard scan codes to sokol-app key codes at application start. As always,
big kudos to GLFW for investigating and implementing a fix after running into
the same issue before.
Also many thanks to GH user @marekmaskarinec for providing an initial PR
(https://github.com/floooh/sokol/pull/1078) which unfortunately couldn't be
used because it doesn't work on XWayland.
For more details see issue https://github.com/floooh/sokol/issues/1080 and
PR https://github.com/floooh/sokol/pull/1081.
### 04-Jul-2024
The public sokol_audio.h functions now have an assert to make sure that saudio_setup()
has already been called.
### 19-Jun-2024
Bugfix in the sokol_gfx.h D3D11 backend: calling `sg_update_image()` with a 3D texture
didn't take the 'depth pitch' into account which then caused invalid texture content
in small-ish textures. This happened at a specific size cutoff which seems to be GPU
specific (on my laptop with integrated Intel GPU only for textures smaller than
32x32xN).
Related ticket: https://github.com/floooh/sokol/issues/1063
...and PR: https://github.com/floooh/sokol/pull/1065
I also wrote a new sample for investigating the issue and to protect from
future regressions: https://floooh.github.io/sokol-html5/dyntex3d-sapp.html
### 01-Jun-2024
sokol_imgui.h is now officially supported in the [sokol-zig bindings](https://github.com/floooh/sokol-zig).
This caused a very minor breaking change in the sokol_imgui.h function
`simgui_add_key_event()`: previously this took a callback function pointer
which mapped the incoming key code to a Dear ImGui compatible keycode,
this is now expected to be performed by the caller before calling
`simgui_add_key_event()`.
Other than the minor API change there's an equally minor internal code cleanup:
The ImGuiIO method `SetKeyEventNativeData()` is no longer called. This change shouldn't
have any side effects.
For more details about the Zig sokol_imgui.h also see this example project:
https://github.com/floooh/sokol-zig-imgui-sample
### 14-May-2024
sokol_fetch.h: A minor breaking change in which hopefully doesn't affect anybody:
The function typedef `sfetch_callback_t` has been removed and the type signature
for the callback has been directly embedded in the `sfetch_request_t` struct. This
is a preparation for adding sokol_fetch.h to the language bindings (first in
sokol-zig, see this PR for details: https://github.com/floooh/sokol/pull/1048).
### 13-May-2024
Official bindings for the **D language** have been added, like the other official
bindings those will be automatically updated on commits to the main repository:
https://github.com/kassane/sokol-d
...this also includes a matching output format `sokol_d` in the sokol-shdc shader
compiler.
Also see PR https://github.com/floooh/sokol/pull/955.
Many thanks to @kassane for the hard work!
...and a couple minor texture format related fixes in the WebGPU backends in sokol_gfx.h and sokol_app.h:
- merged PR https://github.com/floooh/sokol/pull/1045, this sets 32-bit float textures
to filterable if supported (depending on `WGPUFeatureName_Float32Filterable`), many
thanks to @jdah!
- in sokol_app.h, the WebGPU feature detection code has been fixed:
- previously, BC and ETC2 texture compression support was mutually exclusive, which
was a bug (for instance on Apple Silicon, both formats are available)
- the missing ASTC texture compression detection has been added (sokol_gfx.h already
checked the WebGPU device for support of ASTC compression, but this code never
worked because the feature was not requested when the WebGPU device was created
in sokol_app.h
### 10-May-2024
A minor breaking change regarding ETC2/EAC pixel formats:
- `SG_PIXELFORMAT_ETC2_RG11` has been renamed to `SG_PIXELFORMAT_EAC_RG11`
- `SG_PIXELFORMAT_ETC2_RG11SN` has been renamed to `SG_PIXELFORMAT_EAC_RG11SN`
- the pixel formats `SG_PIXELFORMAT_EAC_R11` and `SG_PIXELFORMAT_EAC_R11SN` have been added
- fixed a pixel format mapping bug in WebGPU (the EAC RG11 formats were actually mapped to R11)
See ticket https://github.com/floooh/sokol/issues/1041, and PR https://github.com/floooh/sokol/pull/1044 for details.
### 09-May-2024
The 'storage buffer update'. sokol_gfx.h now has (readonly) storage buffer support, providing
a more flexible way to pass array-like random access data from the CPU to the GPU side.
Please see the following [blog post](https://floooh.github.io/2024/05/06/sokol-storage-buffers.html)
and the [associated PR #1007](https://github.com/floooh/sokol/pull/1007) for details.
Please also note the new documentation section `ON STORAGE BUFFERS` in sokol_gfx.h.
Also see the related [changes in sokol-shdc](https://github.com/floooh/sokol-tools/blob/master/CHANGELOG.md).
...and finally the following new samples (note that the demos are running on WebGPU and currently
require a recent Chrome on macOS or Windows):
- rendering without buffer bindings (this sample actually also runs on WebGL2):
- WebGPU: https://floooh.github.io/sokol-webgpu/triangle-bufferless-sapp.html
- WebGL2: https://floooh.github.io/sokol-html5/triangle-bufferless-sapp.html
- C source: https://github.com/floooh/sokol-samples/blob/master/sapp/triangle-bufferless-sapp.c
- GLSL source: https://github.com/floooh/sokol-samples/blob/master/sapp/triangle-bufferless-sapp.glsl
- vertex pulling from a storage buffer:
- WebGPU: https://floooh.github.io/sokol-webgpu/vertexpull-sapp.html
- C source: https://github.com/floooh/sokol-samples/tree/master/sapp/vertexpull-sapp.c
- GLSL source: https://github.com/floooh/sokol-samples/tree/master/sapp/vertexpull-sapp.glsl
- reading storage buffer content in fragment shader:
- WebGPU: https://floooh.github.io/sokol-webgpu/sbuftex-sapp.html
- C source: https://github.com/floooh/sokol-samples/tree/master/sapp/sbuftex-sapp.c
- GLSL source: https://github.com/floooh/sokol-samples/tree/master/sapp/sbuftex-sapp.glsl
- instanced rendering via storage buffer:
- WebGPU: https://floooh.github.io/sokol-webgpu/instancing-pull-sapp.html
- C source: https://github.com/floooh/sokol-samples/tree/master/sapp/instancing-pull-sapp.c
- GLSL source: https://github.com/floooh/sokol-samples/tree/master/sapp/instancing-pull-sapp.glsl
- skinned character rendering via storage buffers:
- WebGPU: https://floooh.github.io/sokol-webgpu/ozz-storagebuffer-sapp.html
- C source: https://github.com/floooh/sokol-samples/tree/master/sapp/ozz-storagebuffer-sapp.cc
- GLSL source: https://github.com/floooh/sokol-samples/tree/master/sapp/ozz-storagebuffer-sapp.glsl
Also see the following backend-specific samples which don't use sokol-shdc:
- D3D11: https://github.com/floooh/sokol-samples/blob/master/d3d11/vertexpulling-d3d11.c
- Metal: https://github.com/floooh/sokol-samples/blob/master/metal/vertexpulling-metal.c
- WebGPU: https://github.com/floooh/sokol-samples/blob/master/wgpu/vertexpulling-wgpu.c
- Desktop GL: https://github.com/floooh/sokol-samples/blob/master/glfw/vertexpulling-glfw.c
Storage buffer support is not available on the following platform/backend combos:
- macOS + GL (stuck at GL 4.1)
- iOS + GL (stuck at GLES 3.0)
- WebGL2 (stuck at GLES 3.0)
- Android (support may be implemented at a later time)
#### **BREAKING CHANGES**
- the config define `SOKOL_GLCORE33` has been renamed to `SOKOL_GLCORE`, this affects
the following headers:
- sokol_gfx.h
- sokol_app.h
- sokol_debugtext.h
- sokol_fontstash.h
- sokol_gl.h
- sokol_imgui.h
- sokol_nuklear.h
- sokol_spine.h
- likewise in the sokol_gfx.h enum `sg_backend` the enum item `SG_BACKEND_GLCORE33` has been
renamed to `SG_BACKEND_GLCORE`
- sokol_gfx.h now expects a minimal desktop GL version of 4.1 on macOS, and 4.3 on other
platforms (this only matters if you don't use sokol_app.h), storage buffer support is only
available on GL 4.3 contexts
- likewise, shaders passed into sokol_gfx.h when the desktop GL backend is active are now expected
to be `#version 410` or `#version 430` (`#version 330` may still work but is untested)
- likewise, by default sokol_app.h now creates a GL 4.1 context on macOS and a GL 4.3 context on other
desktop platforms when `SOKOL_GLCORE` is defined
- if you're passing WGSL shaders directly into sokol_gfx.h (instead of using sokol-shdc), please
be aware that the binding offsets for the different shader resource types have moved:
- vertex shader stage:
- textures: `@group(1) @binding(0..15)`
- samplers: `@group(1) @binding(16..31)`
- storage buffers: `@group(1) @binding(32..47)`
- fragment shader stage:
- textures: `@group(1) @binding(48..63)`
- samplers: `@group(1) @binding(64..79)`
- storage buffers `@group(1) @binding(80..95)`
#### **NON-BREAKING CHANGES**
- **sokol_app.h** learned two new functions to get the desktop GL version (note that on GLES
these return 0, this behaviour may change at a later time):
- `int sapp_gl_get_major_version(void)`
- `int sapp_gl_get_minor_version(void)`
- **sokol_gfx.h**:
- The enum `sg_buffer_type` has a new member `SG_BUFFERTYPE_STORAGEBUFFER`, used
in the `sg_make_buffer()` call to create a storage buffer
- The struct `sg_features` has a new member `bool storage_buffer`, used to indicate
that the current 3D backend supports storage buffers
- The stats struct `sg_frame_stats_metal_bindings` has a new member `num_set_fragment_buffer`
- There are various new error codes and validation checks related to storage buffers
- A new struct `sg_shader_storage_buffer_desc`, nested in `sg_shader_desc`.
This is used in the `sg_make_shader()` call to communicate to sokol_gfx.h
what storage buffer bind slots are used in a shader
- **sokol_gfx_imgui.h**: The debug UI panels have been updated to visualize the new
storage buffer related state
- in the following headers, the embedded shaders have been updated via the new
sokol-shdc version, switching the embedded GLSL shaders to `#version 410`
- sokol_debugtext.h
- sokol_fontstash.h
- sokol_gl.h
- sokol_imgui.h
- sokol_nuklear.h
- sokol_spine.h
### 03-May-2024:
- sokol_app.h win32: Merged PR https://github.com/floooh/sokol/pull/1034, this adds a NOAPI mode
to the sokol_app.h Windows backend by defining SOKOL_NOAPI before including the implementation.
Same thing as GLFW's NOAPI mode basically, to allow using the sokol_app.h windowing features
without setting up D3D11 or OpenGL. NOAPI implementations for other platforms will follow in the
future. Many thanks to @pplux and @castano!
### 13-Apr-2024:
- sokol_gfx.h d3d11: resource label strings are now communicated to D3D11 resource objects,
making it easier to identify those resources in tools like the Visual Studio Graphics Debugger
or RenderDoc. See PR https://github.com/floooh/sokol/pull/1025 for details. Many thanks to
@jakubtomsu for the PR!
- Odin bindings: merged https://github.com/floooh/sokol/pull/1023 (and related PR
https://github.com/floooh/sokol-odin/pull/11 in the actual bindings repo). This changes
the directory structure of the bindings to make them a bit friendlier to integrate
with Odin projects, and also adds a couple of smaller improvements and fixes.
Many thanks to @jakubtomsu for the PRs!
- Also a couple of smaller 'drive-by PRs' I merged over the last couple of days but didn't mention
yet in the changelog:
- https://github.com/floooh/sokol/pull/1029: exclude NUM enum items in Odin bindings, many thanks to @jakubtomsu
- https://github.com/floooh/sokol/pull/1028: in sokol_gfx.h fix GCC warnings in the d3d11 backend (when compiling
via mingw on Windows), many thanks @edubart
- https://github.com/floooh/sokol/pull/1026: in sokol_gfx.h increase the internal `_SG_STRING_SIZE` from
16 to 32, by @jakubtomsu
- https://github.com/floooh/sokol/pull/1021, https://github.com/floooh/sokol-odin/pull/10: re-enable Odin CI builds
for macOS (by linking against LLVM 17), also by @jakubtomsu
### 21-Mar-2024:
- sokol_imgui.h: merged PR https://github.com/floooh/sokol/pull/1010, this will automatically
re-create the sokol-gfx font texture resources in the `simgui_new_frame()` call
when the Dear ImGui texture atlas has changed. This is an alternative to calling the
functions `simgui_create_fonts_texture()` and `simgui_destroy_fonts_texture()` manually.
One important reason why you'd want to call those functions manually is to create the fonts texture
with custom texture sampler attributes (the new implicit re-creation inside `simgui_new_frame()`
calls `sg_make_sampler()` with default attributes).
Many thanks to @elloramir for the PR!
### 02-Mar-2024:
- sokol_app.h emscripten: two new flags in `sapp_desc` to configure the Emscripten main loop:
- `.html5_use_emsc_set_main_loop`: when this is true, the function `emscripten_set_main_loop()` will be used
to drive the sokol-app frame callback (otherwise `emscripten_request_animation_frame()` as before)
- `.html5_emsc_set_main_loop_simulate_infinite_loop`: this is passed as the `simulate_infinite_loop` parameter
into the `emscripten_set_main_loop()` function.
In general you should stick with sokol_app.h's default behaviour and only use those settings if you run
into specific problems, for instance as discussed here: https://github.com/floooh/sokol/issues/843
Related PR: https://github.com/floooh/sokol/pull/997
Many thanks to @Dvad for the PR, and also to @ambrusc for an alternative PR that hadn't been used, @voidware
for kicking off the discussion and all contributors!
The sample `texcube-sapp` has been updated to use the set-main-loop method:
https://floooh.github.io/sokol-html5/texcube-sapp.html
- sokol_imgui.h: PR https://github.com/floooh/sokol/pull/994 has been merged, this adds two
font management helper functions which drastically reduce boilerplate code
when injecting a custom font into Dear ImGui via sokol_imgui.h.
See the PR for details, and the updated sample https://floooh.github.io/sokol-html5/imgui-highdpi-sapp.html
via this PR: https://github.com/floooh/sokol-samples/pull/135
Many thanks to @Dvad for the PR!
### 01-Mar-2024:
Minor regression fix for yesterdays merge in the sokol_gfx.h Metal backend:
A swapchain render pass with an SG_PIXELFORMAT_DEPTH depth-buffer would try
to set a stencil surface (currently this only matters if you use your own
window system glue since sokol_app.h always creates a depth+stencil-buffer).
See https://github.com/floooh/sokol/issues/1004 for details.
The [Metal samples in the sokol-samples project](https://github.com/floooh/sokol-samples/tree/master/metal) have been updated to use all variants
of SG_PIXELFORMAT_NONE, SG_PIXELFORMAT_DEPTH and SG_PIXELFORMAT_DEPTH_STENCIL
now to catch similar regressions in the future.
Plus 2 minor drive-by fixes:
- fix the sokol_gfx.h WebGPU backend for a spec-fix in Chrome (see https://github.com/floooh/sokol/issues/1003)
- in the Emscripten backends of sokol_app.h and sokol_args.h, replace the deprecated JS helper function
`allocateUTF8OnStack` with its replacement `stringToUTF8OnStack` (see: https://github.com/floooh/sokol/commit/49a75e1476153cb2605d3b3ebd2f07e3eb0536d9)
### 29-Feb-2024:
**BREAKING CHANGES** in sokol_gfx.h, sokol_app.h, sokol_glue.h and sokol_gfx_imgui.h
(the 'big render pass cleanup').
- In sokol_gfx.h, the concepts of 'render contexts' and 'default render passes' have
been removed and replaced with a unified `sg_begin_pass()` which handles both
rendering into 'offscreen-passes' and 'swapchain-passes'.
[Please read this blog
post](https://floooh.github.io/2024/02/26/sokol-spring-cleaning-2024.html)
carefully for a detailed overview what has changed, why the changes make
sense, and how existing code needs to be updated.
Also see the related PR for further details: https://github.com/floooh/sokol/pull/985
- There are also minimal related changes in the sokol_app.h and a complete
rewrite of the sokol_glue.h APIs, also detailed in the above blog post.
- The namespace-prefix for the header sokol_gfx_imgui.h has been changed from
`sg_imgui_` to `sgimgui_`.
- In sokol_gfx.h with the Metal backend, a runtime configuration flag has been
added to `sg_desc` to create a Metal command buffer with
'retained-references'. See issue
[#981](https://github.com/floooh/sokol/issues/981) for details.
- Also in sokol_gfx.h, the struct item `sg_limits.gl_max_vertex_uniform_vectors` has been changed
to `sg_limits.gl_max_vertex_uniform_components` (note that there are 4x more 'components'
than 'vectors'). See issue [#714](https://github.com/floooh/sokol/issues/714) for details.
- All sampples, language binding examples and 'side projects' have been updated, see the above blog post
for links to the respective PRs.
### 27-Feb-2024:
- Merged PR https://github.com/floooh/sokol/pull/1001, this is a small fix for GLES3 to avoid
calling glInvalidateFramebuffer() on non-existing depth/stencil surfaces.
Many thanks to @danielchasehooper!
#### 26-Feb-2024:
- Minor fix in sokol_imgui.h: The drawing code now detects and skips the special
`ImDrawCallback_ResetRenderState` constant, not doing so would try to call a function
at address (-8) which then results in a crash.
See for what this is: https://github.com/ocornut/imgui/blob/277ae93c41314ba5f4c7444f37c4319cdf07e8cf/imgui.h#L2583-L2587
sokol_imgui.h doesn't have any handling for this special callback, it will just ignore it.
As a minor additional behaviour change, any user callback will now also cause `sg_reset_state_cache()`
to be called. This is just a precaution in case the user callback code calls any native 3D backend API
functions.
Related issue: https://github.com/floooh/sokol/issues/1000
#### 21-Feb-2024:
- PR https://github.com/floooh/sokol/pull/993 has been merged, this allows to inject
additional GL functions into the Win32 GL loader of sokol_gfx.h (TBH, it's a very specialized
feature for people who know what they're doing, but it also fixes a very specific problem
while at the same time resolving to 'nothing' when not used).
Many thanks for @kcbanner for the PR!
#### 31-Jan-2024:
- sokol_app.h macOS: merged a workaround for the application window not being focused
if the init callback takes a while (not reproducible on my M1 Mac with latest Sonoma,
but might fix the issue for older Macs, and the change seems harmless enough -
sokol_app.h essentially sends a focusEvent to itself)
Related issue: https://github.com/floooh/sokol/issues/757
Implemented in PR: https://github.com/floooh/sokol/pull/982
Many thanks to @zoo-3d for investigating the issue and the PR!
#### 28-Jan-2024:
- sokol_app.h web: the canvas resize callback is now unregistered on cleanup.
Related issue: https://github.com/floooh/sokol/issues/983 and PR: https://github.com/floooh/sokol/pull/984
Many thanks to @edubart!
#### 27-Jan-2024
- sokol_app.h web: The HTML5 event bubbling changes introduced in the 02-Jan-2024
update have been reverted because they introduced some undesired side effects.
By default, most input events now *don't* bubble up (which restores the
old behaviour), but it's now possible to enable bubbling for categories
of input events (mouse, touch, wheel, keys and chars) during sokol-app setup.
It's then possible to control bubbling of individual events by calling
`sapp_consume_event()` from within the sokol-app event callback.
See issue https://github.com/floooh/sokol/issues/972 for details and
PR https://github.com/floooh/sokol/pull/975 for the actual changes.
Also check out the new doc section `INPUT EVENT BUBBLING ON THE WEB PLATFORM`
in the sokol_app.h header documentation block.
- sokol_gfx.h metal: Merged PR https://github.com/floooh/sokol/pull/980. When
only the offset changes in a vertex buffer binding, only the buffer offset
is now updated (e.g. instead of the Metal method `setVertexBuffer:offset:atIndex`,
the leaner method `setVertexBufferOffset:atIndex` is called. Apart from the
actual PR I also removed a couple of actually unused items from the Metal
backend state cache. Many thanks to @staminajim for the PR!
Related issue: https://github.com/floooh/sokol/issues/979
#### 23-Jan-2024
- sokol_app.h android: Touch event coordinates are now using AMotionEvent_getX/Y() instead
of AMotionEvent_getRawX/Y(). The raw functions don't work well in multi-window
scenarios. See PR https://github.com/floooh/sokol/pull/974 for details.
Many thanks to GitHub user @Comanx!
#### 19-Jan-2024
- sokol_app.h wgpu: tiny fix for a breaking API change in webgpu.h in the Emscripten 3.1.52 SDK
- Merged PR https://github.com/floooh/sokol/pull/970 (many thanks to @waywardmonkeys) which
fixes a couple of strict-prototype warnings (e.g. C functions using func() instead of func(void)).
I also enabled `-Wstrict-prototypes` now in the CI tests for GCC and Clang, so such cases
should be caught in the future.
#### 18-Jan-2024
- sokol_gfx.h: added support for the following pixel formats:
- BC3_SRGBA
- BC7_SRGBA
- ETC2_SRGB8
- ETC2_SRGB8A8
- ASTC_4x4_RGBA
- ASTC_4x4_SRGBA
Related PR: https://github.com/floooh/sokol/pull/967
Many thanks to GH user @allcreater!
#### 07-Jan-2024
- sokol_app.h (macos+metal): window content no longer 'wobbles' during window resizing. Many
thanks to @Seb-degraff for picking up and investigating this longstanding issue
(https://github.com/floooh/sokol/issues/700), finding a fix for the remaining problem
and providing a really nice PR (https://github.com/floooh/sokol/pull/963)
#### 06-Jan-2024
> NOTE: if you use sokol_gfx.h and sokol_app.h together, make sure to update both. This is
because the pixel format enum in sokol_gfx.h has been shuffled around a bit, and as a result, some internal
pixel format constants in sokol_app.h had to move too!
- sokol_gfx.h: some minor new features (non-breaking):
- the struct `sg_pixel_format` has two new items:
- `bool compressed`: true if this is a hardware-compressed pixel format
- `int bytes_per_pixel`: as the name says, with the caveat that this is
zero for compressed pixel formats (because the smallest element in compressed formats is a block, not a pixel)
- two previously private helper functions have been exposed to help with size computations
for texture data, these may be useful when preparing image data for consumption by `sg_make_image()`
and `sg_update_image()`:
- `int sg_query_row_pitch(sg_pixel_format fmt, int width, int row_align_bytes)`:
Computes the number of bytes in a texture row for a given pixel format. A 'row' has
different meanings for uncompressed vs compressed formats: For uncompressed pixel
formats, a row is a single line of pixels, while for compressed formats, a row is
a line of 'compression blocks'. `width` is always in pixels.
- `int sg_query_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align_bytes)`:
Computes number of bytes in a texture surface (e.g. a single mipmap) for a given
pixel format. `width` and `height` are always in pixels.
The `row_align_bytes` parameter is for added flexibility. For image data that goes into
the `sg_make_image()` or `sg_update_image()` functions this should generally be 1, because these
functions take tightly packed image data as input no matter what alignment restrictions
exist in the backend 3D APIs.
- Related issue: https://github.com/floooh/sokol/issues/946, and PR: https://github.com/floooh/sokol/pull/962
#### 03-Jan-2024
- sokol_nuklear.h: `snk_handle_event()` now returns a bool to indicate whether the
event was handled by Nuklear (this allows an application to skip its own event
handling if Nuklear already handled the event). Issue link: https://github.com/floooh/sokol/issues/958,
fixed in PR: https://github.com/floooh/sokol/pull/959. Many thanks to @adamrt for the PR!
#### 02-Jan-2024
Happy New Year! A couple of input-related changes in the sokol_app.h Emscripten backend:
- Mouse and touch events now bubble up to the HTML document instead of being consumed, in some scenarios this
allows better integration with the surrounding web page. To prevent event bubbling,
call `sapp_consume_event()` from within the sokol_app.h event callback function.
- **NOTE**: wheel/scroll events behave as before and are always consumed. This prevents
an ugly "scroll bumping" effect when a wheel event bubbles up on a page where
scrolling shouldn't be possible.
- The hidden HTML text input field hack for text input on mobile browsers has been
removed. This idea never really worked across all browsers, and it actually
interfered with Dear ImGui text input fields because the hidden HTML text field
generated focus-in/out events which confused the Dear ImGui input handling code.
Those changes fix a couple of problem when trying to integrate sokol_app.h applications
into VSCode webview panels, see: https://marketplace.visualstudio.com/items?itemName=floooh.vscode-kcide
Related PR: https://github.com/floooh/sokol/pull/939
#### 10-Nov-2023
A small change in the sokol_gfx.h GL backend on Windows only:
PR https://github.com/floooh/sokol/pull/839 has been merged, in debug mode this creates
the GL context with WGL_CONTEXT_DEBUG_BIT_ARB. Thanks to @castano for the PR!
#### 06-Nov-2023
A bugfix in the sokol_gfx.h D3D11 backend, and some related cleanup when creating depth-stencil
render target images and resource views:
- fixed: render target images with format SG_PIXELFORMAT_DEPTH_STENCIL triggered a validation
error because the pixel format capabilities code marked them as non-renderable. Now
the SG_PIXELFORMAT_DEPTH_STENCIL pixel format is properly reported as renderable.
- the DXGIFormats for SG_PIXELFORMAT_DEPTH_STENCIL images are now as follows:
- D3D11 texture object: DXGI_FORMAT_R24G8_TYPELESS
- D3D11 shader-resource-view object: DXGI_FORMAT_R24_UNORM_X8_TYPELESS
- D3D11 depth-stencil-view object: DXGI_FORMAT_D24_UNORM_S8_UINT
Related PR: https://github.com/floooh/sokol/pull/937
#### 30-Oct-2023
Some sokol_gfx.h backend-specific updates and tweaks (very minor chance that this is breaking if you are injecting textures into the D3D11 backend).
- a new set of public API functions to access the native backend 3D-API resource objects of
sokol-gfx resource objects:
```
sg_[api]_[type]_info sg_[api]_query_[type]_info(sg_[type])
```
...where `[api]` is any of `[gl, d3d11, mtl, wgpu]` and `[type]` is any of `[buffer, image, sampler, shader, pipeline, pass]`.
This is mainly useful when mixing native 3D-API code with sokol-gfx code.
See issue https://github.com/floooh/sokol/issues/931 for details.
- WebGPU backend: `sg_make_image()` will no longer automatically create a WebGPU texture-view object when injecting a WebGPU texture object, instead
this must now be explicitly provided.
- D3D11 backend: `sg_make_image()` will no longer automatically create a
shader-resource-view object when injecting a D3D11 texture object, and
vice versa, a texture object will no longer be looked up from an injected
shader-resource-view object (e.g. the injection rules are now more straightforward and explicit). See issue https://github.com/floooh/sokol/issues/930 for details.
For the detailed changes, see PR https://github.com/floooh/sokol/pull/932.
#### 27-Oct-2023
Fix broken render-to-mipmap in the sokol_gfx.h GL backend.
There was a subtle bug / "feature gap" lurking in sokol_gfx.h GL backend: trying
to render to any mipmap except the top-level mipmap resulted in a black screen
because of an incomplete-framebuffer error. This is fixed now. The changes in detail:
- creating a texture in the GL backend now sets the GL_TEXTURE_MAX_LEVEL property
(this is the fix to make everything work)
- the framebuffer completeness check in the GL backend now has more detailed error logging
- in the validation layer, the requirement that a sampler that's used with a
single-mipmap-texture must use `.mipmap_filter = SG_FILTER_NONE` has been
relaxed (a later update will remove SG_FILTER_NONE entirely since it's not needed anymore
and the concept of a "none" mipmap filter only exists in GL and Metal, but not D3D, WebGPU
and Vulkan)
Ticket: https://github.com/floooh/sokol/issues/923
PR: https://github.com/floooh/sokol/pull/924
There's also a new render-to-mipmap sample which covers to close this 'feature gap':
https://floooh.github.io/sokol-html5/miprender-sapp.html
A couple of similar samples will follow over the next few days
(rendering to texture array layers and 3d texture slices).
#### 26-Oct-2023
- sokol_app.h gl: fix a regression introduced in https://github.com/floooh/sokol/pull/916
which could select the wrong framebuffer pixel format and break rendering
on some GL drivers (in my case: an older Intel GPU).
If you are using the GL backend on Windows, please make sure to upgrade!
#### 23-Oct-2023
- sokol_app.h gl: some further startup optimizations in the WGL code path
via PR https://github.com/floooh/sokol/pull/916
#### 21-Oct-2023
The major topic of this update is the 'finalized' WebGPU support in sokol_gfx.h and sokol_app.h.
- WebGPU samples are hosted here:
https://floooh.github.io/sokol-webgpu/
- WebGL2 samples remain hosted here:
https://floooh.github.io/sokol-html5/
- Please read the following blog post as introduction:
https://floooh.github.io/2023/10/16/sokol-webgpu.html
- ...and the changelog and updated documentation in the sokol-shdc repository:
https://github.com/floooh/sokol-tools
- You'll also need to update the sokol-shdc binaries:
https://github.com/floooh/sokol-tools-bin
- Please also read the following new or updated sections in the embedded sokol_gfx.h header documentation:
- `ON SHADER CREATION`
- `ON SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT AND SG_SAMPLERTYPE_NONFILTERING`
- `WEBGPU CAVEATS`
Please do this especially when using any of the following texture pixel formats, as you will most likely encounter new validation layer errors:
- `SG_PIXELFORMAT_R32F`
- `SG_PIXELFORMAT_RG32F`
- `SG_PIXELFORMAT_RGBA32F`
- There is a tiny breaking change in the sokol_gfx.h API (only requires action when not using sokol-shdc):
- the following `sg_sampler_type` enum items have been renamed to better match their WebGPU counterparts:
- SG_SAMPLERTYPE_SAMPLE => SG_SAMPLERTYPE_FILTERING
- SG_SAMPLERTYPE_COMPARE => SG_SAMPLERTYPE_COMPARISON
- the enum `sg_image_sample_type` gained a new item:
- SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT
- the enum `sg_sampler_type` gained a new item:
- SG_SAMPLERTYPE_NONFILTERING
- The sokol_gfx.h struct `sg_desc` has two new items:
- `.wgpu_bindgroups_cache_size` - must be power-of-2, default: 1024
- `.wgpu_disable_bindgroups_cache` - default: false
- sokol_gfx.h gained the following new public API functions to query per-frame information:
- `sg_frame_stats sg_query_frame_stats()`
- `void sg_enable_frame_stats(void)`
- `void sg_disable_frame_stats(void)`
- `bool sg_frame_stats_enabled(void)`
Frame statistics gathering is enabled after startup, but can be temporarily
disabled and enabled again via `sg_disable_frame_stats()` and `sg_enable_frame_stats`.
- The sokol_gfx.h validation layer has new validation checks in `sg_make_shader()`
regarding image/sampler pair compatibility (WebGPU is particularly strict about
this stuff).
- In sokol_app.h, the old wip WebGPU device and swapchain setup code is now implemented
in pure C code (previously this was a mix of Javascript and C).
- Also note that sokol_app.h currently only supports WebGPU in the Emscripten backend.
If you want to use sokol_gfx.h with the WebGPU backend in a native scenario, you'll have
to use a different window system glue library (like GLFW). The sokol-samples directory
has a handful of examples for using sokol_gfx.h + Dawn + GLFW.
- The following headers have been made compatible with the sokol_gfx.h WebGPU backend
(mainly by embedding WGSL shader code):
- sokol_debugtext.h
- sokol_fontstash.h
- sokol_gl.h
- sokol_spine.h
- sokol_imgui.h (also required some more changes for embedding `unfilterable-float`
textures, since these now require separate shader and pipeline objects)
- sokol_nuklear.h (works in WebGPU, but doesn't contain the work from sokol_imgui.h
to support `unfilterable-float` user textures)
- sokol_gfx_imgui.h gained a new function `sg_imgui_draw_menu()` which renders a
menu panel to show/hide all debug windows. Previously this had to be done
outside the header.
- sokol_gfx_imgui.h gained a new 'frame stats' window, which allows to peak into
sokol_gfx.h frame-rendering internals. This basically visualizes the struct
`sg_frame_stats` returned by the new sokol_gfx.h function `sg_query_frame_stats()`.
- The sokol-samples repository gained 3 new samples:
- cubemap-jpeg-sapp.c (load a cubemap from separate JPEG files)
- cubemaprt-sapp.c (render into cubemap faces - this demo actually existed a while but wasn't "official" so far)
- drawcallperf-sapp.c (a sample to explore the performance overhead of sg_apply_bindings, sg_apply_uniforms and sg_draw)
#### 03-Oct-2023
- sokol_app.h win/gl: PR https://github.com/floooh/sokol/pull/886 has been merged, this makes
GL context initialization on Windows slightly more efficient. Many thanks to @dtrebilco!
#### 25-Sep-2023
- The allocator callback functions in all headers that support custom allocators have been renamed
from `alloc` and `free` to `alloc_fn` and `free_fn`, this is because the symbol `free` is quite
likely to collide with a preprocessor macro of the same name if the standard C allocator is
replaced with a custom allocator.
This is a breaking change only if you've been providing your own allocator functions to
the sokol headers.
See issue https://github.com/floooh/sokol/issues/903 and PR https://github.com/floooh/sokol/pull/908
for details.
#### 23-Sep-2023
- sokol_gfx.h gl: Allow to inject an external GL framebuffer id into the sokol-gfx default
pass. See PR https://github.com/floooh/sokol/pull/899 and issue https://github.com/floooh/sokol/issues/892
for details. Many thanks to @danielchasehooper for the discussion and PR!
Further down the road I want to make the whole topic more flexible while at the same time
simplifying the sokol-gfx API, see here: https://github.com/floooh/sokol/issues/904
#### 22-Sep-2023
- sokol_gfx.h: Fixed a Metal validation error on Intel Macs when creating textures (Intel Macs
have unified memory, but don't support textures in shared storage mode). This was a regression
in the image/sampler split update in mid-July 2023. Fixes issue https://github.com/floooh/sokol/issues/905
via PR https://github.com/floooh/sokol/pull/907.
#### 19-Sep-2023
- sokol_fetch.h: fixed a minor issue where a request that was cancelled before it was dispatched
had an incomplete response state set in the response callback (the `finished`, `failed` and
`error_code` fields were not set). This fixes issue https://github.com/floooh/sokol/issues/882
via PR https://github.com/floooh/sokol/pull/898
#### 18-Sep-2023
- PR https://github.com/floooh/sokol/pull/893 has been merged, this fixes a minor issue
in the GL backend when using an injected texture as framebuffer attachment.
- Issue https://github.com/floooh/sokol/issues/884 has been fixed via PR https://github.com/floooh/sokol/pull/894,
this adds missing error code paths in the Metal backend when Metal object creation fails.
- Clarified `sapp_run()` behaviour in the sokol_app.h documentation header (search for `OPTIONAL: DON'T HIJACK main()`)
- sokol_args.h now fully supports "key-only args", see issue https://github.com/floooh/sokol/issues/876 for details,
fixed via PR https://github.com/floooh/sokol/pull/896
#### 17-Sep-2023
- The sokol-gfx Metal backend now adds debug labels to Metal resource objects and
also passes through the `sg_push/pop_debug_group()` calls. If you use the push/pop
debug group calls, please be aware of the following limitations:
- a push inside a render pass must have an associated pop inside the same render pass
- a push outside any render pass must have an associated pop outside any render pass
- Metal will ignore any push/pop calls outside render passes (this is because in Metal
these are MTLCommandEncoder methods)
Associated issue: https://github.com/floooh/sokol/issues/889, and PR: https://github.com/floooh/sokol/pull/890.
#### 09-Sep-2023
- a small PR has been merged which fixes a redundant glBindFramebuffer() in the GLES3 backend
in `sg_end_pass()` (see: https://github.com/floooh/sokol/pull/878), many thanks to @danielchasehooper
for catching that issue!
- sokol_imgui.h has been fixed for cimgui 1.89.9 (see https://github.com/floooh/sokol/issues/879)
#### 28-Aug-2023
**sokol_gfx.h metal**: A new attempt at fixing a rare Metal validation layer
error about MTKView swapchain resource lifetimes. See PR https://github.com/floooh/sokol/pull/873
for details.
#### 26-Jul-2023
**sokol_nuklear.h**: The same image+sampler support has been added as in sokol_imgui.h
three days ago:
- a new object type `snk_image_t` which wraps a sokol-gfx image and sampler
under a common handle
- new functions:
- snk_make_image()
- snk_destroy_image()
- snk_query_image_desc()
- snk_image_from_nkhandle()
- the function snk_nkhandle() now takes an snk_image_t handle instead of an sg_image handle
- the nuklear.h header needs to be included before the declaration (not just the implementation),
this was already required before, but now you get a proper error message if the include is missing
- the 'standard' logging- and error-reporting callback has been added as in the other sokol headers
(don't forget to add a logging callback in snk_setup(), otherwise sokol-nuklear will be silent)
- since sokol-nuklear now needs to allocate memory, an allocator can now be provided to the
snk_setup() call (otherwise malloc/free will be used)
Please also read the new documentation section `ON USER-PROVIDED IMAGES AND SAMPLERS`
in sokol_nuklear.h, and also check out the (rewritten) sample:
https://floooh.github.io/sokol-html5/nuklear-images-sapp.html
Associated PR: https://github.com/floooh/sokol/pull/862
#### 23-Jul-2023
**sokol_imgui.h**: Add proper support for injecting user-provided sokol-gfx
images and samplers into Dear ImGui UIs. With the introduction of separate
sampler objects in sokol_gfx.h there's a temporary feature regression in
sokol_imgui.h and sokol_nuklear.h in that user provided images had to use a
shared sampler that's hardwired into the respective headers. This update fixes
this problem for sokol_imgui.h, with a similar fix for sokol_nuklear.h coming
up next.
The sokol_imgui.h changes in detail are:
- a new object type `simgui_image_t` which wraps a sokol-gfx image and sampler
object under a common handle
- two new function `simgui_make_image()` and `simgui_destroy_image()` to
create and destroy such a new `simgui_image_t` object.
- the existing function `simgui_imtextureid()` has been changed to take
an `simgui_image_t`
- sokol_imgui.h now also uses the same error-handling and logging callback
as the other sokol headers (this was needed because creating an `simgui_image_t`
object may fail because the object pool is exhausted) - don't forget
to provide a logging callback (for instance via sokol_log.h), otherwise
sokol_imgui.h will be entirely silent in case of errors.
Please also read the new documentation section `ON USER-PROVIDED IMAGES AND SAMPLERS`
in sokol_imgui.h, and also check out the new sample:
https://floooh.github.io/sokol-html5/imgui-images-sapp.html
Associated PR: https://github.com/floooh/sokol/pull/861
#### 16-Jul-2023
**BREAKING CHANGES**
The main topic of this update is to separate sampler state from image state in
sokol_gfx.h which became possible after GLES2 support had been removed from
sokol_gfx.h.
This also causes some 'collateral changes' in shader authoring and
other sokol headers, but there was opportunity to fill a few feature gaps
in sokol_gfx.h as well:
- it's now possible to sample depth textures in shaders both with regular
samplers, and with 'comparison samplers' (which is mainly useful for shadow mapping)
- it's now possible to create render passes without color attachments for
'depth-only' rendering
See the new [shadows-depthtex-sapp](https://floooh.github.io/sokol-html5/shadows-depthtex-sapp.html) sample which demonstrates both features.
> NOTE: all related projects have a git tag `pre-separate-samplers` in case you are not ready yet to make the switch
> NOTE 2: if you use sokol-gfx with the sokol-shdc shader compiler, you'll also need
> to update the sokol-shdc binaries from https://github.com/floooh/sokol-tools-bin
##### **sokol_gfx.h**
- texture sampler state has been removed from `sg_image_desc`, instead you now
need to create separate sampler objects:
```c
sg_sampler smp = sg_make_sampler(&(sg_sampler_desc){
.min_filter = SG_FILTER_LINEAR,
.mag_filter = SG_FILTER_LINEAR,
.wrap_u = SG_WRAP_CLAMP_TO_EDGE,
.wrap_v = SG_WRAP_CLAMP_TO_EDGE
});
```
- texture filtering is now described by 3 separate filters:
- min_filter = SG_FILTER_NEAREST | SG_FILTER_LINEAR
- mag_filter = SG_FILTER_NEAREST | SG_FILTER_LINEAR
- mipmap_filter = SG_FILTER_NONE | SG_FILTER_NEAREST | SG_FILTER_LINEAR
...this basically switches from the esoteric GL convention to a convention
that's used by all other 3D APIs. There's still a limitation that's caused by
GL though: a sampler which is going to be used with an image that has a
`mipmap_count = 1` requires that `.mipmap_filter = SG_FILTER_NONE`.
- another new sampler state in `sg_sampler_desc` is `sg_compare_func compare;`,
this allows to create 'comparison samplers' for shadow mapping
- when calling `sg_apply_bindings()` the struct `sg_bindings` now has changed
to also include sampler objects, note that there is no 1:1 relationship
between images and samplers required:
```c
sg_apply_bindings(&(sg_bindings){
.vertex_buffers[0] = vbuf,
.fs = {
.images = {
[SLOT_tex0] = img0,
[SLOT_tex1] = img1,
[SLOT_tex2] = img2,
},
.samplers[SLOT_smp] = smp,
}
});
```
- if you use sokol-shdc, you need to rewrite your shaders from 'OpenGL GLSL style' (with
combined image samplers) to 'Vulkan GLSL style' (with separate textures and samplers):
E.g. the old GL-style shader with combined image samplers:
```glsl
uniform sampler2D tex;
void main() {
frag_color = texture(tex, uv);
}
```
...now needs to look like this:
```glsl
uniform texture2D tex;
uniform sampler smp;
void main() {
frag_color = texture(sampler2D(tex, smp), uv);
}
```
sokol-shdc will now throw an error if it encounters an 'old' shader using combined
image-samplers, this helps you to catch all places where a rewrite to separate
texture and sampler objects is required.
- If you *don't* use sokol-shdc and instead provide your own backend-specific
shaders, you need to provide more shader interface reflection info about the texture
and sampler usage in a shader when calling `sg_make_shader`.
Please see the new documentation block `ON SHADER CREATION` in sokol_gfx.h for more details!
Also refer to the updated 3D-backend-specific samples here:
- for GL: https://github.com/floooh/sokol-samples/tree/master/glfw
- for GLES3: https://github.com/floooh/sokol-samples/tree/master/html5
- for D3D11: https://github.com/floooh/sokol-samples/tree/master/d3d11
- for Metal: https://github.com/floooh/sokol-samples/tree/master/metal
- it's now possible to create `sg_pass` objects without color attachments to
enable depth-only rendering, see the new sample [shadows-depthtex-sapp](https://floooh.github.io/sokol-html5/shadows-depthtex-sapp.html) for details,
specifically be aware of the caveat that a depth-only-compatible `sg_pipeline` object
needs to 'deactivate' the first color target by setting its pixel format
to `NONE`:
```c
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
...
.colors[0].pixel_format = SG_PIXELFORMAT_NONE,
...
});
```
- the following struct names have been changed to be more in line with related
struct names, this also makes those names similar to WebGPU types:
- `sg_buffer_layout_desc` => `sg_vertex_buffer_layout_state`
- `sg_vertex_attr_desc` => `sg_vertex_attr_state`
- `sg_layout_desc` => `sg_vertex_layout_state`
- `sg_color_state` => `sg_color_target_state`
- bugfixes and under-the-hood changes
- `sg_begin_pass()` used the wrong framebuffer size when rendering to a mip-level != 0
- the Metal backend code started to use the `if (@available(...))` statement
to check for runtime-availability of macOS/iOS API features
- **NOTE:** this change (`if (@available(...))`) caused linking problems in
the Zig and Rust bindings on GH Actions (missing symbol
`___isPlatformVersionAtLeast`) which I could not reproduce locally on my
M1 Mac. On Zig this could be fixed by moving to the latest zig-0.11.0-dev
version, but for Rust this still needs to be fixed).
- on macOS the Metal backend now creates resources in Shared resource storage mode if
supported by the device
- on iOS the Metal backend now supports clamp-to-border-color if possible (depends on
iOS version and GPU family)
##### **sokol_gl.h**
- The function `sgl_texture(sg_image img)` has been changed to accept a sampler
object to `sgl_texture(sg_image img, sg_sampler smp)`. Passing an invalid image handle
will use the builtin default (white) texture, and passing an invalid sampler
handle will use the builtin default sampler.
##### **sokol_shape.h**
- Some sokol-shape functions have been renamed to match renamed structs in sokol-gfx:
- `sshape_buffer_layout_desc()` => `sshape_vertex_buffer_layout_state()`
- `sshape_position_attr_desc()` => `sshape_position_vertex_attr_state()`
- `sshape_normal_attr_desc()` => `sshape_normal_vertex_attr_state()`
- `sshape_texcoord_attr_desc()` => `sshape_texcoord_vertex_attr_state()`
- `sshape_color_attr_desc()` => `sshape_color_vertex_attr_state()`
##### **sokol_spine.h**
- A sokol-spine atlas object now allocates both an `sg_image` and `sg_sampler` handle
and expects the user code to initialize those handles to complete image and
sampler objects. Check the updated sokol-spine samples here for more details:
https://github.com/floooh/sokol-samples/tree/master/sapp
##### **sokol_imgui.h**
- sokol_imgui.h has a new public function to create an ImTextureID handle from
an `sg_image` handle which can be used like this:
```c
ImTextureID tex_id = simgui_imtextureid(img);
```
Note that sokol-imgui currently doesn't currently allow to pass user-provided `sg_sampler`
object with the user-provided image.
##### **sokol_nuklear.h**
- similar to sokol_imgui.h, there's a new public function `snk_nkhandle()`
which creates a Nuklear handle from a sokol-gfx image handle which can be
used like this to create a Nuklear image handle:
```c
nk_image nki = nk_image_handle(snk_nkhandle(img));
```
As with sokol_imgui.h, it's currently not possible to pass a user-provided `sg_sampler`
object with the image.
#### 20-May-2023
Some minor event-related cleanup in sokol_app.h and a touchscreen fix in sokol_imgui.h
- in the event `SAPP_EVENTTYPE_FILESDROPPED`:
- the `sapp_event.modifier` field now contains the active modifier keys
at the time of the file drop operations on the platforms macOS, Emscripten
and Win32 (on Linux I haven't figured out how this might work with the
Xlib API)
- on macOS, the `sapp_event.mouse_x/y` fields now contain the window-relative
mouse position where the drop happened (this already worked as expected on
the other desktop platforms)
- on macOS and Linux, the `sapp_event.mouse_dx/dy` fields are now set to zero
(this already was the case on Emscripten and Win32)
- in the events `SAPP_EVENTTYPE_MOUSE_ENTER` and `SAPP_EVENTTYPE_MOUSE_LEAVE`:
- the `sapp_event.mouse_dx/dy` fields are now set to zero, previously this
could be a very big value on some desktop platforms
Many thanks to @castano for the initial PR (https://github.com/floooh/sokol/pull/830)!
- In sokol_imgui.h, the new io.AddMouseSourceEvent() function in Dear ImGui 1.89.5
is called to differentiate between mouse- and touch-events, this makes ui tabs
work with a single tap (previously a double-tap on the tab was needed). The code
won't break if the ImGui version is older (in this case the function simply isn't called)
#### 19-May-2023
**BREAKING CHANGES**_ in sokol_gfx.h: Render passes are now more 'harmonized'
with Metal and WebGPU by exposing a 'store action', and making MSAA resolve attachments
explicit. The changes in detail:
- A new documentation section `ON RENDER PASSES` has been added to sokol_gfx.h, this
gives a much more detailed overview of the new render pass behaviour than this
changelog, please make sure to give it a read - especially when you are using
MSAA offscreen render passes in your code.
- `sg_action` has been renamed to `sg_load_action`.
- A new enum `sg_store_action` has been added.
- In `sg_pass_action`:
- `.action` has been renamed to `.load_action`.
- `.value` has been renamed to `.clear_value`.
- A new field `.store_action` has been added.
- An `sg_image` object with a sample count > 1 no longer contains a second implicit
texture for the msaa-resolve operation.
- When creating a pass object, there's now an array of `sg_image` objects
called `resolve_attachments[]`. When a resolve attachment image is set, the
color attachment at the same slot index must be an image with a sample count >
1, and an 'msaa-resolve' operation from the color attachment into the
resolve attachment will take place in `sg_end_pass()`.
- Pass attachments are now more flexible (there were a couple of gaps where specific
image types were not allowed as pass attachments, especially for the depth-stencil-
attachment - but this hadn't actually been checked by the validation layer).
- Some gaps in the validation layer around images and passes have been tightened up,
those usually don't work in one backend or another, but have been ignored so far
in the validation layer, mainly:
- MSAA images must have num_mipmaps = 1.
- 3D images cannot have a sample_count > 1.
- 3D images cannot have depth or depth-stencil image formats.
- It's not allowed to bind MSAA images as texture.
- It's not allowed to bind depth or depth-stencil images as texture.
- (I'll see if I can relax some of those restrictions after the WebGPU backend release)
- **A lot** of new tests have been added to cover validation layer checks when creating
image and pass objects.
Next up: WebGPU!
#### 30-Apr-2023
GLES2/WebGL1 support has been removed from the sokol headers (now that
all browsers support WebGL2, and WebGPU is around the corner I feel like it's finally
time to ditch GLES2.
This is a breaking API change in sokol_gfx.h and sokol_app.h.
Common changes across all headers:
- (breaking change) the `SOKOL_GLES2` config define is no longer accepted and will cause a compile error
(use `SOKOL_GLES3` instead)
- (breaking change) on Emscripten use the linker option `-s USE_WEBGL2=1`
- any embedded GLES shaders have been updated from glsl100 to glsl300es (but glsl100 shaders
still work fine with the GLES3 backend)
Changes in sokol_gfx.h:
- (breaking change) the following `sg_features` members have been removed (because those features
are no longer optional, but guaranteed across all backends):
- `sg_features.instancing`
- `sg_features.multiple_render_targets`
- `sg_features.msaa_render_targets`
- `sg_features.imagetype_3d`
- `sg_features.imagetype_array`
- (breaking change) the struct `sg_gl_context_desc` and its embedded instance `sg_desc.gl` have been removed
- `sg_image` objects with `SG_PIXELFORMAT_DEPTH` or `SG_PIXELFORMAT_DEPTH_STENCIL` with
a `sample_count == 1` are now regular textures in the GL backend (this is not true
for MSAA depth textures unfortunately, those are still GL render buffer objects)
- in the GL backend, `SG_PIXELFORMAT_DEPTH` now resolves to `GL_DEPTH_COMPONENT32F` (same
as in the other backends), previously it was `GL_DEPTH_COMPONENT16`
- in `sg_begin_pass()`, the GL backend now only uses the new `glClearBuffer*` functions, the
old GLES2 clear functions have been removed
- in `sg_end_pass()`, the GLES3 backend now invalidates MSAA render buffers after they have
been resolved (via `glInvalidateFramebuffer`) - more control over this will come soon-ish
when this ticket is implemented: https://github.com/floooh/sokol/issues/816
- the instanced rendering functions are no longer wrapped in C macros in the GL backend
Changes in sokol_app.h:
- (breaking) the config item `sapp_desc.gl_force_gles2` has been removed
- (breaking) the function `sapp_gles2()` has been removed
- any fallback logic from GLES3 to GLES2 has been removed (in the Emscripten, Android and
iOS backends)
- **20-Feb-2023**: sokol_gfx.h has a new set of functions to get a 'best-effort'
desc struct with the creation parameters of a specific resource object:
```c
sg_buffer_desc sg_query_buffer_desc(sg_buffer buf);
sg_image_desc sg_query_image_desc(sg_image img);
sg_shader_desc sg_query_shader_desc(sg_shader shd);
sg_pipeline_desc sg_query_pipeline_desc(sg_pipeline pip);
sg_pass_desc sg_query_pass_desc(sg_pass pass);
```
The returned structs will *not* be an exact copy of the desc struct that
was used for creation the resource object, instead:
- references to external data (like buffer and image content or
shader sources) will be zeroed
- any attributes that have not been kept around internally after
creation will be zeroed (the ```sg_shader_desc``` struct is most
affected by this, the other structs are fairly complete).
Calling the functions with an invalid or dangling resource handle
will return a completely zeroed struct (thus it may make sense
to first check the resource state via ```sg_query_*_state()```)
Nevertheless, those functions may be useful to get a partially filled out
'creation blueprint' for creating similar resources without the need
to keep and pass around the original desc structs.
>MINOR BREAKING CHANGE: the struct members ```sg_image_info.width``` and
```sg_image_info.height``` have been removed, this information is now
returned by ```sg_query_image_desc()```.
PR: https://github.com/floooh/sokol/pull/796, fixes: https://github.com/floooh/sokol/issues/568
- **17-Feb-2023**: sokol_app.h on macOS now has a proper fix for the problem
that macOS doesn't send key-up events while the Cmd key is held down.
Previously this was handled through a workaround of immediately sending a
key-up event after its key-down event if the Cmd key is currently held down
to prevent a 'stuck key'. The proper fix is now to install an "event monitor"
callback (many thanks to GLFW for finding and implementing the solution).
Unfortunately there's no such solution for the Emscripten code path, which
also don't send a key-up event while Cmd is pressed on macOS (the workaround
there to send a key-up event right on key-down while Cmd is held down to
prevent a stuck key is still in place) For more details, see:
https://github.com/floooh/sokol/issues/794
- **15-Feb-2023**: A fix in the sokol_gfx.h GL backend: due to a bug in the
state cache, the GL backend could only bind a total of
SG_MAX_SHADERSTAGE_IMAGES (= 12) when it actually should be twice that amount
(12 per shader stage). Note however that the total amount of texture bindings
is still internally limited by the GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
runtime variable (~~currently this is not exposed in sg_limits though~~). Many
thanks to @allcreater for PR https://github.com/floooh/sokol/pull/787.
PS: sg_limits now exposes GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS as
```sg_limits.gl_max_combined_texture_image_units```, and the
value can also be inspected via the debug UI in sokol_gfx_imgui.h.
- **13-Feb-2023**: The way logging works has been completely revamped in
the sokol headers. UWP support has been removed from sokol_audio.h
and sokol_app.h (this also means that the sokol headers no longer contain
any C++ code).
**REQUIRED ACTION**: Since the sokol headers are now completely silent
without a logging callback (explanation below), it is highly recommended
to use the standard logging callback provided by the new header ```sokol_log.h```.
For instance for sokol_gfx.h it looks like this:
```c
#include "sokol_log.h"
//...
sg_setup(&(sg_desc){
//...
.logger.func = slog_func,
});
```
All sokol samples have been updated to use sokol_log.h for logging.
The former logging callback is now a combined
logging- and error-reporting callback, and more information is available
to the logging function:
- a 'tag string' which identifies the sokol headers, this string
is identical with the API prefix (e.g. "sg" for sokol_gfx.h,
"sapp" for sokol_app.h etc...)
- a numeric log level: 0=panic, 1=error, 2=warning, 3=info
- a numeric 'log item id' (think of it as error code, but since
not only errors are reported I called it a log item id)
- a human readable error message
- a source file line number where the log item was reported
- the file path of the sokol header
Log level ```panic``` is special in that it terminates execution inside
the log function. When a sokol header issues a panic log message, it means
that the problem is so big that execution can not continue. By default,
the sokol headers and the standard log function in sokol_log.h call
```abort()``` when a panic log message is issued.
In debug mode (NDEBUG not defined, or SOKOL_DEBUG defined), a log message
(in this case from sokol_spine.h) will look like this:
```
[sspine][error][id:12] /Users/floh/projects/sokol/util/sokol_spine.h:3472:0:
SKELETON_DESC_NO_ATLAS: no atlas object provided in sspine_skeleton_desc.atlas
```
The information can be 'parsed' like this:
- ```[sspine]```: it's a message from sokol_spine.h
- ```[error]```: it's an error
- ```[id:12]```: the numeric log item id (associated with ```SKELETON_DESC_NO_ATLAS``` below)
- source file path and line number in a compiler-specific format - in some IDEs and terminals
this is a clickable link
- the line below is the human readable log item id and message
In release mode (NDEBUG is defined and SOKOL_DEBUG is not defined), log messages
are drastically reduced (the reason is to not bloat the executable with all the extra string data):
```
[sspine][error][id:12][line:3472]
```
...this reduced information still gives all the necessary information to identify the location and type of error.
A custom logging function must adhere to a few rules:
- must be re-entrant because it might be called from different threads
- must treat **all** provided string pointers as optional (can be null)
- don't store the string pointers, copy the string data instead
- must not return for log level panic
A new header ```sokol_log.h``` has been added to provide a standard logging callback implementation
which provides logging output on all platforms to stderr and/or platform specific logging
facilities. ```sokol_log.h``` only uses fputs() and platform specific logging function instead
of fprintf() to preserve some executable size.
**QUESTION**: Why are the sokol headers now silent, unless a logging callback is installed?
This is mainly because a standard logging function which does something meaningful on all
platforms (including Windows and Android) isn't trivial. E.g. printing to stderr is not
enough. It's better to move that stuff into a centralized place in a separate header,
but since the core sokol headers must not (statically) depend on other sokol headers
the only solution that made sense was to provide a standard logging function which must
be 'registered' as a callback.
- **26-Jan-2023**: Work on SRGB support in sokol_gfx.h has started, but
this requires more effort to be really usable. For now, only a new
pixel format has been added: SG_PIXELFORMAT_SRGB8A8 (see https://github.com/floooh/sokol/pull/758,
many thanks to @allcreater). The sokol-gfx GL backend has a temporary
workaround to align behaviour with D3D11 and Metal: automatic SRGB conversion
is enabled for offscreen render passes, but disabled for the default
framebuffer. A proper fix will require separate work on sokol_app.h to
support an SRGB default framebuffer and communicate to sokol-gfx
whether the default framebuffer is SRGB enabled or not.
- **24-Jan-2023**: sokol_gfx.h Metal: A minor inconsistency has been fixed in
the validation layer and an assert for the function ```sg_apply_uniforms()```
which checks the size of the incoming data against the uniform block size.
The validation layer and Metal backend did a ```<=``` test while the D3D11
and GL backends checked for an exact size match. Both the validation layer
and the Metal backend now also check for an exact match. Thanks to @nmr8acme
for noticing the issue and providing a PR! (https://github.com/floooh/sokol/pull/776)
- **23-Jan-2023**: A couple more sokol_audio.h updates:
- an AAudio backend has been added for Android, and made the default. This
means you now need to link with ```aaudio``` instead of ```OpenSLES``` when
using sokol_audio.h on Android. The OpenSLES backend code still exists (for
now), but must be explicitly selected by compiling the sokol_audio.h
implementation with the define ```SAUDIO_ANDROID_SLES``` (e.g. there is
no runtime fallback from AAudio to OpenSLES). AAudio is fully supported
since Android 8.1. Many thanks to @oviano for the initial AAudio PR
(https://github.com/floooh/sokol/pull/484)
- in the WebAudio backend, WebAudio is now properly activated on the first
input action again on Chrome for Android (at some point activating WebAudio
via a ```touchstart``` event stopped working and had to be moved to the
```touchend``` event, see https://github.com/floooh/sokol/issues/701)
- audio backend initialization on iOS and macOS is now a bit more fault-tolerant,
errors during initialization now properly set sokol_audio.h to 'silent mode'
instead of asserting (or in release mode ignoring the error)
- ...and some minor general code cleanup things in sokol_audio.h: backend-specific
functions now generally have a matching prefix (like ```_saudio_alsa_...()```)
for better searchability
- **16-Jan-2023**:
- sokol_audio.h android: https://github.com/floooh/sokol/pull/747 has been merged
which adds a couple more error checks at OpenSLES startup.
- sokol_gfx.h: support for half-float vertex formats has been added via
PR https://github.com/floooh/sokol/pull/745
- sokol_imgui.h: fixes for Dear ImGui 1.89 deprecations (via PR https://github.com/floooh/sokol/pull/761)
- **15-Jan-2023**: two bugfixes in sokol_app.h and sokol_gfx.h:
- sokol_app.h x11: Mouse button events now always return valid mouse
coordinates, also when no mouse movement happened yet
(fixes https://github.com/floooh/sokol/issues/770)
- sokol_gfx.h gl: The GL context is now configured with
GL_UNPACK_ALIGNMENT = 1, this should bring texture creation and updating
behaviour in line with the other backends for tightly packed texture
data that doesn't have a row-pitch with a multiple of 4
(fixes https://github.com/floooh/sokol/issues/767)
- **14-Jan-2023**: sokol_app.h x11: a drag'n'drop related bugfix, the
XdndFinished reply event was sent with the wrong window handle which
confused some apps where the drag operation originated
(see https://github.com/floooh/sokol/pull/765#issuecomment-1382750611)
- **16-Dec-2022**: In the sokol_gfx.h Metal backend: A fix for a Metal
validation layer error which I just discovered yesterday (seems to be new in
macOS 13). When the validation layer is active, and the application window
becomes fully obscured, the validation layer throws an error after a short
time (for details see: https://github.com/floooh/sokol/issues/762).
The reason appears to be that sokol_gfx.h creates a command buffer with
'unretained references' (e.g. the command buffer doesn't manage the
lifetime of resources used by the commands stored in the buffer). This
seems to clash with MTKView's and/or CAMetalLayer's expectations. I fixed
this now by creating a second command buffer with 'retained references',
which only holds the ```presentDrawable``` command. That way, regular
draw commands don't have the refcounting overhead (because they're stored
in an unretained-references cmdbuffer), while the drawable surface is
still properly lifetime managed (because it's used in a separate command
buffer with retained references).
- **15-Dec-2022**: A small but important update in sokol_imgui.h which fixes
touch input handling on mobile devices. Many thanks to GitHub user @Xadiant
for the bug investigation and [PR](https://github.com/floooh/sokol/pull/760).
- **25-Nov-2022**: Some code cleanup around resource creation and destruction in sokol_gfx.h:
- It's now safe to call the destroy, uninit and dealloc functions in any
resource state, in general, the functions will do the right thing without
assertions getting in the way (there are however new log warnings in some
cases though, such as attempting to call an ```sg_dealloc_*()``` function on
a resource object that's not in ALLOC state)
- A related **minor breaking change**: the ```sg_uninit_*()``` functions now return
void instead of bool, this is because ```sg_dealloc_*()``` no longer asserts
when called in the wrong resource state
- Related internal code cleanup in the backend-agnostic resource creation
and cleanup code, better or more consistent function names, etc...
- The validation layer can now be disabled in debug mode with a runtime
flag during setup: ```sg_desc.disable_validation```. This is mainly useful
for test code.
- Creating a pass object with invalid image objects now no longer asserts,
but instead results in a pass object in FAILED state. In debug mode,
the validation layer will still stop at this problem though (it's mostly
an 'undefined API behaviour' fix in release mode).
- Calling ```sg_shutdown()``` with existing resources in ALLOC state will
no longer print a log message about an 'active context mismatch'.
- A new header documentation blurb about the two-step resource creation
and destruction functions (search for RESOURCE CREATION AND DESTRUCTION IN DETAIL)
- **16-Nov-2022**: Render layer support has been added to sokol_debugtext.h,
same general changes as in sokol_gl.h with two new functions:
sdtx_layer(layer_id) to select the layer to record text into, and
sdtx_draw_layer(layer_id) to draw the recorded text in that layer inside a
sokol-gfx render pass. The new sample [debugtext-layers-sapp](https://floooh.github.io/sokol-html5/debugtext-layers-sapp) demonstrates the feature together with
sokol-gl.
- **11-Nov-2022**: sokol_gl.h has 2 new public API functions which enable
layered rendering: sgl_layer(), sgl_draw_layer() (technically it's three
functions: there's also sgl_context_draw_layer(), but that's just a variant of
sgl_draw_layer()). This allows to 'interleave' sokol-gl rendering
with other render operations. The [spine-layers-sapp](https://floooh.github.io/sokol-html5/spine-layers-sapp.html)
sample has been updated to use multiple sokol-gl layers.
- **09-Nov-2022**: sokol_gfx.h now allows to add 'commit listeners', these
are callback functions which are called from inside sg_commit(). This is
mainly useful for libraries which build on top of sokol-gfx to be notified
about the start/end point of a frame, which in turn may simplify the public
API, or the internal implementation, because the library no longer needs to
'guess' when a new frame starts.
For more details, search for 'COMMIT LISTENERS' in the sokol_gfx.h header.
This also results in a minor breaking change in sokol_spine.h: The function
```sspine_new_frame()``` has been removed and replaced with an internal commit
listener.
Likewise, sokol_gl.h now uses a commit listener in the implementation, but
without changing the public API (the feature will be important for an upcoming
sokol-gl feature to support rendering layers, and for this a 'new-frame-function'
would have been needed).
- **05-Nov-2022** A breaking change in sokol_fetch.h, and a minor change in
sokol_app.h which should only break for very few users:
- An ```sfetch_range_t``` ptr/size pair struct has been added to sokol_fetch.h,
and discrete ptr/size pairs have been replaced with sfetch_range_t
items. This affects the structs ```sfetch_request_t``` and ```sfetch_response_t```,
and the function ```sfetch_bind_buffer()```.
- The required changes in ```sfetch_response_t``` might be a bit non-obviois: To
access the fetched data, previous ```.buffer_ptr``` and ```.fetched_size```
was used. The fetched data is now accessible through an ```sfetch_range_t data```
item (```data.ptr``` and ```data.size```). The old ```.fetched_offset``` item
has been renamed to ```.data_offset``` to better conform with the new naming.
- The last two occurrences of discrete ptr/size pairs in sokol_app.h now have also
been replaced with ```sapp_range_t``` items, this only affects the structs
```sapp_html5_fetch_request``` and ```sapp_html5_fetch_response```.
- **03-Nov-2022** The language bindings generation has been updated for Zig 0.10.0,
and clang-14 (there was a minor change in the JSON ast-dump format).
Many thanks to GitHub user @kcbanner for the Zig PR!
- **02-Nov-2022** A new header sokol_spine.h (in the util dir), this is a
renderer and 'handle wrapper' around the spine-c runtime (Spine is a popular 2D
character anim system: http://esotericsoftware.com/). This turned out a much bigger
rabbit-hole than I initially expected, but the effort is justified by being a
experimentation testbed for a couple of things I want to add to other sokol
headers (for instance cleaned up handle pool code, a new logging- and error-reporting
system, render layers which will be useful for sokol_gl.h and sokol_debugtext.h).
- **22-Oct-2022** All sokol headers now allow to override logging with a
callback function (installed in the setup call) instead of defining a SOKOL_LOG
macro. Overriding SOKOL_LOG still works as default fallback, but this is no
longer documented, consider this deprecated. Many thanks to GitHub user
@Manuzor for the PR (see https://github.com/floooh/sokol/pull/721 for details)
- **21-Oct-2022** RGB9E5 pixel format support in sokol_gfx.h and a GLES2 related
bugfix in the sokol_app.h Android backend:
- sokol_gfx.h now supports RGB9E5 textures (3*9 bit RGB + 5 bit shared exponent),
this works in all backends except GLES2 and WebGL1 (use ```sg_query_pixelformat()```
to check for runtime support). Many thanks to GitHub user @allcreater for the PR!
- a bugfix in the sokol_app.h Android backend: when forcing a GLES2 context via
sapp_desc.gl_force_gles2, the Android backend correctly created a GLES2 context,
but then didn't communicate this through the function ```sapp_gles2()``` (which
still returned false in this case). This caused the sokol_gfx.h GL backend to
use the GLES3 code path instead GLES2 (which surprisingly seemed to have worked
fine, at least for the sokol samples which force GLES2).
- **19-Oct-2022** Some fixes in the embedded Javascript code blocks (via EM_JS)
in sokol_app.h, sokol_args.h, sokol_audio.h and sokol_fetch.h:
- the JS code has been 'modernized' (e.g. const and let instead of var,
```() => { ... }``` instead of ```function () { ... }``` for callbacks)
- false positives in the Closure static analysis have been suppressed
via inline hints
- **16-Oct-2022** The Odin bindings generator and the generated bindings have
been simplified (the Odin binding now don't have separate wrapper functions).
Requires the latest Odin release. Also note: On M1 Macs I'm currently seeing
what looks like an ABI problem (in functions which pass color values to the C
side as uint8_t, the colors come out wrong). This also happened with the
previous binding version, so it looks like a regression in Odin. Might be
related to this recent bugfix (which I haven't tested yet):
https://github.com/odin-lang/Odin/issues/2121 Many thanks to @thePHTest for the
PR! (https://github.com/floooh/sokol/pull/719)
- **15-Oct-2022**
- fixes for Emscripten 3.1.24: the sokol headers now use the new
**EM_JS_DEPS()** macro to declare 'indirect dependencies on JS library functions'.
This is a (much more robust) follow-up fix to the Emscripten related fixes from 10-Sep-2022.
The new Emscripten SDK also displays a couple of Javascript "static analyzer" warnings
by the Closure compiler (used in release mode to optimize and minify the generated
JS code). I fixed a couple of those warnings, but some warnings persist (all of them
false positives). Not sure yet if these can be fixed or need to be suppressed, but
that's for another time.
- the webkitAudioContext() fallback in sokol_audio.h's Emscripten backend
has been removed (only AudioContext is supported now), the fallback also
triggered a Closure warning, so it probably never worked as intended anyway.
- I also had to undo an older workaround in sokol_app.h on iOS (https://github.com/floooh/sokol/issues/645)
because this is now triggering a Metal validation layer error (https://github.com/floooh/sokol/issues/726).
The original case is no longer reproducible, so undoing the old workaround seems to
be a quick fix. Eventually I want to get rid of MTKView though, and go down to
CAMetalLayer.
- **08-Oct-2022** sokol_app.h Android backend: the ```sapp_touchpoint``` struct
now has a new item ```sapp_android_tooltype android_tooltype;```. This exposes the
result of the Android NDK function ```AMotionEvent_getToolType()```.
Many thanks to @Wertzui123 for the initial PR (https://github.com/floooh/sokol/pull/717).
- **25-Sep-2022**: sokol_app.h on Linux now optionally supports EGL instead of
GLX for the window system glue code and can create a GLES2 or GLES3 context
instead of a 'desktop GL' context.
To get EGL+GLES2/GLES3, just define SOKOL_GLES2 or SOKOL_GLES3 to compile the
implementation. To get EGL+GL, define SOKOL_GLCORE *and* SOKOL_FORCE_EGL.
By default, defining just SOKOL_GLCORE uses GLX for the window system glue
(just as before). Many thanks to GH user @billzez for the PR!
- **10-Sep-2022**: sokol_app.h and sokol_args.h has been fixed for Emscripten 3.21, those headers
used the Emscripten Javascript helper function ```ccall()``` which is now part of the
'legacy runtime' and causes linker errors. Instead of ```ccall()``` sokol_app.h and sokol_args.h
now drop down to a lower level set of Emscripten JS helper functions (which hopefully won't
go away anytime soon).
- **05-Aug-2022**: New officially supported and automatically updated language bindings for Odin:
https://github.com/floooh/sokol-odin (also see [gen_odin.py](https://github.com/floooh/sokol/blob/master/bindgen/gen_odin.py))
- **10-Jul-2022**: New features in sokol_app.h and sokol_imgui.h:
- In sokol_app.h it's now possible to set a mouse cursor type from a number of predefined
types via the new function ```sapp_set_mouse_cursor(sapp_mouse_cursor cursor)```. The
available cursor types are compatible with GLFW and Dear ImGui. Supported platforms
are: macOS, linux, win32, uwp and web.
- ```sapp_show_mouse(bool shown)``` now also works on the web platform.
- In sokol_app.h, the poorly defined 'user cursor' feature has been removed (```sapp_desc.user_cursor```
and ```SAPP_EVENTTYPE_UPDATE_CURSOR```). This was a hack to allow changing the mouse cursor and
only worked on Win32 and macOS (with different behaviour). Since setting the cursor type
is now 'properly supported, this hack was removed.
- sokol_imgui.h will now set the cursor type via ```sapp_set_mouse_cursor()```. This can be
disabled with the new ```simgui_desc_t``` item ```disable_set_mouse_cursor```.
- sokol_imgui.h now automatically enables resizing windows from edges (not just the bottom-right corner),
this behaviour can be disabled with the new ```simgui_desc_t``` item ```disable_windows_resize_from_edges```.
- sokol_imgui.h can now optionally write to the alpha channel (useful if you want to render the UI
into a separate render target, which is later composed onto the default framebuffer). The feature
is enabled with the new ```simgui_desc_t``` item ```write_alpha_channel```.
Many thanks to **@tomc1998** for the initial [Linux/X11 mouse cursor type PR](https://github.com/floooh/sokol/pull/678) and
**@luigi-rosso** for the [sokol_imgui.h alpha channel PR](https://github.com/floooh/sokol/pull/687)!
- **03-Jul-2022**: A new sokol_gfx.h function ```bool sg_query_buffer_will_overflow(sg_buffer buf, size_t size)```
which allows to check if a call to ```sg_append_buffer()``` would overflow the buffer. This
is an alternative to the ```sg_query_buffer_overflow()``` function which only reports
the overflow after the fact. Many thanks to @RandyGaul for the PR!
- **29-Jun-2022**: In sokol_app.h with the D3D11 backend, if SOKOL_DEBUG is
defined, and the D3D11 device creation fails, there's now a fallback code
path which tries to create the device again without the D3D11_CREATE_DEVICE_DEBUG
flag. Turns out the D3D11 debug support may suddenly stop working (just happened
to me, indicated by the Win10 "Graphics Tool" feature being silently uninstalled
and failing to install when asked to do so). This fix at least allows sokol_app.h
applications compiled in debug mode to run, even if the D3D11 debug layer doesn't
work.
- **29-May-2022**: The code generation scripts for the
[sokol-nim](https://github.com/floooh/sokol-nim) language bindings have been
revised and updated, many thanks to Gustav Olsson for the PR! (I'm planning to
spend a few more days integrating the bindings generation with GitHub Actions,
so that it's easier to publish new bindings after updates to the sokol headers).
- **26-May-2022**: The GL backend in sokol_app.h now allows to override the GL
context version via two new items in the ```sapp_desc``` struct:
```sapp_desc.gl_major_version``` and ```sapp_desc.gl_minor_version```. The
default GL context version remains at 3.2. Overriding the GL version might make
sense if you're not using sokol_app.h together with sokol_gfx.h, or otherwise
want to call GL functions directly. Note that this only works for the
'desktop GL' backends (Windows, Linux and macOS), but not for the GLES backends
(Android, iOS, web). Furthermore, on macOS only the GL versions 3.2 and 4.1
are available (plus the special config major=1 minor=0 creates an
NSOpenGLProfileVersionLegacy context). In general: use at your risk :) Many
thanks to GitHub user @pplux for the PR!
- **15-May-2022**: The way internal memory allocation can be overridden with
your own functions has been changed from global macros to callbacks
provided in the API setup call. For instance in sokol_gfx.h:
```c
void* my_malloc(size_t size, void* userdata) {
(void)userdata; // unused
return malloc(size);
}
void my_free(void* ptr, void* userdata) {
(void)userdata; // unused
free(ptr);
}
//...
sg_setup(&(sg_desc){
//...
.allocator = {
.alloc = my_malloc,
.free = my_free,
.user_data = ...,
}
});
```
sokol_gfx.h will now call ```my_malloc()``` and ```my_free()``` whenever it needs
to allocate or free memory (note however that allocations inside OS
functions or 3rd party libraries are not affected).
If no override functions are provided, the standard library functions ```malloc()``` and ```free()```
will be used, just as before.
This change breaks source compatibility in the following headers:
- **sokol_fontstash.h**: the function signature of ```sfons_create()``` has changed,
this now takes a pointer to a new ```sfons_desc_t``` struct instead of
individual parameters.
- **sokol_gfx_imgui.h** (NOT sokol_imgui.h!): likewise, the function signature of
```sg_imgui_init()``` has changed, this now takes an additional parameter
which is a pointer to a new ```sg_imgui_desc_t``` struct.
All affected headers also have a preprocessor check for the outdated
macros ```SOKOL_MALLOC```, ```SOKOL_CALLOC``` and ```SOKOL_FREE``` and throw
a compilation error if those macros are detected.
(if configuration through macros is still desired this could be added back in
the future, but I figured that the new way is more flexible in most situations).
The header sokol_memtrack.h and the sample [restart-sapp](https://floooh.github.io/sokol-html5/restart-sapp.html) have been updated accordingly.
Also search for ```MEMORY ALLOCATION OVERRIDE``` in the header documentation block
for more details.
- **14-May-2022**: added a helper function ```simgui_map_keycode()``` to
sokol_imgui.h to map sokol_app.h keycodes (```sapp_keycode```,
```SAPP_KEYCODE_*```) to Dear ImGui keycodes (```ImGuiKey```, ```ImGuiKey_*```).
If you're using Dear ImGui function to check for key input, you'll need to
update the code like this:
- Old:
```cpp
ImGui::IsKeyPressed(SAPP_KEYCODE_A);
```
- New:
```cpp
ImGui::IsKeyPressed(simgui_map_keycode(SAPP_KEYCODE_A));
```
This was basically 'fallout' from rewriting the input system in sokol_imgui.h
to the new evented IO system in Dear ImGui.
- **08-Feb-2022**: sokol_imgui.h has been updated for Dear ImGui 1.87:
- sokol_imgui.h's input code has been rewritten to use the new evented IO
system and extended virtual key codes in Dear ImGui
- on non-Emscripten platforms, mouse buttons are no longer "cancelled" when
the mouse leaves the window (since the native desktop platforms
automatically capture the mouse when mouse buttons are pressed, but mouse
capture is not supported in the sokol_app.h Emscripten backend)
- **28-Jan-2022**: some window size behaviour changes in sokol_app.h.
- Asking for a default-sized window (via sapp_desc.width/height = 0) now
behaves a bit differently on desktop platforms. Previously this set the
window size to 640x480, now a default window covers more screen area:
- on Windows CW_USEDEFAULT will be used for the size
- on macOS and Linux, the window size will be 4/5 of the
display size
- no behaviour changes on other platforms
- On Windows and Linux, the window is now centered (in a later update,
more control over the initial window position, and new functions for
positioning and sizing might be provided)
- On Windows, when toggling between windowed and fullscreen, the
window position and size will now be restored (on other platforms
this already happened automatically through the window system)
- On all desktop platforms if an application starts in fullscreen and
then is toggled back to windowed, the window will now be of the
expected size (provided in sapp_desc.width/height)
- **20-Jan-2022**:
- sokol_audio.h: A compatibility fix in the sokol_audio.h WASAPI backend (Windows): On
some configs the IAudioClient::Initialize() call could fail because
of a mismatch between the requested number of channels and speaker config.
See [#614](https://github.com/floooh/sokol/issues/614) for details.
- sokol_app.h D3D11/DXGI: Fix an (uncritical) COM interface leak warning for IDXGIAdapter and
IDXGIFactory at shutdown, introduced with the recent disabling of Alt-Enter.
- **18-Jan-2022**:
- sokol_app.h now has per-monitor DPI support on Windows and macOS: when
the application window is moved to a monitor with different DPI, the values
returned by sapp_dpi_scale(), sapp_width() and sapp_height() will update
accordingly (only if the application requested high-dpi rendering with
```sapp_desc.high_dpi=true```, otherwise the dpi scale value remains
fixed at 1.0f). The application will receive an SAPP_EVENTTYPE_RESIZED event
if the default framebuffer size has changed because of a DPI change.
On Windows this feature requires Win10 version 1703 or later (aka the
'Creators Update'), older Windows version simply behave as before.
Many thank to @tjachmann for the initial PR with the Windows implementation!
- sokol_app.h: DPI scale computation on macOS is now more robust using the
NSScreen.backingScaleFactor value
- sokol_app.h: the new frame timing code in sokol_app.h now detects if the display
refresh rate changes and adjusts itself accordingly (for instance if the
window is moved between displays with different refresh rate)
- sokol_app.h D3D11/DXGI: during window movement and resize, the frame is now
presented with DXGI_PRESENT_DO_NOT_WAIT, this fixes some window system
stuttering issues on Win10 configs with recent NVIDIA drivers.
- sokol_app.h D3D11/DXGI: the application will no longer appear to freeze for
0.5 seconds when the title bar is grabbed with the mouse for movement, but
then not moving the mouse.
- sokol_app.h D3D11/DXGI: DXGI's automatic windowed/fullscreen switching via
Alt-Enter has been disabled, because this switched to 'real' fullscreen mode,
while sokol_app.h's fullscreen mode uses a borderless window. Use the
programmatic fullscreen/window switching via ```sapp_toggle_fullscreen()```
instead.
- **BREAKING CHANGE** in sokol_imgui.h: because the applications' DPI scale
can now change at any time, the DPI scale value is now communicated to
sokol_imgui.h in the ```simgui_new_frame()``` function. This has been
changed to accept a pointer to a new ```simgui_frame_desc_t``` struct.
With C99, change the simgui_new_frame() call as follows (if also using
sokol_app.h):
```c
simgui_new_frame(&(simgui_frame_desc_t){
.width = sapp_width(),
.height = sapp_height(),
.delta_time = sapp_frame_duration(),
.dpi_scale = sapp_dpi_scale()
});
```
On C++ this works:
```c++
simgui_new_frame({ sapp_width(), sapp_height(), sapp_frame_duration(), sapp_dpi_scale() });
```
...or in C++20:
```c++
simgui_new_frame({
.width = sapp_width(),
.height = sapp_height(),
.delta_time = sapp_frame_duration(),
.dpi_scale = sapp_dpi_scale()
});
```
- **KNOWN ISSUE**: the recent change in sokol-audio's WASAPI backend to directly consume
float samples doesn't appear to work on some configs (see [#614](https://github.com/floooh/sokol/issues/614)),
investigation is underway
- **15-Jan-2022**:
- A bugfix in the GL backend for uniform arrays using the 'native' uniform block layout.
The bug was a regression in the recent 'uniform data handling' update. See
[PR #611](https://github.com/floooh/sokol/pull/611) for details, and this [new sample/test](https://github.com/floooh/sokol-samples/blob/master/glfw/uniformarrays-glfw.c).
Many thanks to @nmr8acme for the PR!
- **08-Jan-2022**: some enhancements and cleanup to uniform data handling in sokol_gfx.h
and the sokol-shdc shader compiler:
- *IMPORTANT*: when updating sokol_gfx.h (and you're using the sokol-shdc shader compiler),
don't forget to update the sokol-shdc binaries too!
- The GLSL uniform types int, ivec2, ivec3 and
ivec4 can now be used in shader code, those are exposed to the GL
backends with the new ```sg_uniform_type``` items
```SG_UNIFORM_TYPE_INT[2,3,4]```.
- A new enum ```sg_uniform_layout```, currently with the values SG_UNIFORMLAYOUT_NATIVE
and SG_UNIFORMLAYOUT_STD140. The enum is used in ```sg_shader_uniform_block_desc```
as a 'packing rule hint', so that the GL backend can properly locate the offset
of uniform block members. The default (SG_UNIFORMLAYOUT_NATIVE) keeps the same
behaviour, so existing code shouldn't need to be changed. With the packing
rule SG_UNIFORMLAYOUT_STD140 the uniform block interior is expected to be
laid out according to the OpenGL std140 packing rule.
- Note that the SG_UNIFORMLAYOUT_STD140 only allows a subset of the actual std140
packing rule: arrays are only allowed for the types vec4, int4 and mat4.
This is because the uniform data must still be compatible with
```glUniform()``` calls in the GL backends (which have different
'interior alignment' for arrays).
- The sokol-shdc compiler supports the new uniform types and will annotate the
code-generated sg_shader_desc structs with SG_UNIFORMLAYOUT_STD140,
and there are new errors to make sure that uniform blocks are compatible
with all sokol_gfx.h backends.
- Likewise, sokol_gfx.h has tighter validation for the ```sg_shader_uniform_block```
desc struct, but only when the GL backend is used (in general, the interior
layout of uniform blocks is only relevant for GL backends, on all other backends
sokol_gfx.h just passes the uniform data as an opaque block to the shader)
For more details see:
- [new sections in the sokol_gfx.h documentation](https://github.com/floooh/sokol/blob/ba64add0b67cac16fc86fb6b64d1da5f67e80c0f/sokol_gfx.h#L343-L450)
- [documentation of ```sg_uniform_layout```](https://github.com/floooh/sokol/blob/ba64add0b67cac16fc86fb6b64d1da5f67e80c0f/sokol_gfx.h#L1322-L1355)
- [enhanced sokol-shdc documentation](https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md#glsl-uniform-blocks-and-c-structs)
- [a new sample 'uniformtypes-sapp'](https://floooh.github.io/sokol-html5/uniformtypes-sapp.html)
PS: and an unrelated change: the frame latency on Win32+D3D11 has been slightly improved
via IDXGIDevice1::SetMaximumFrameLatency()
- **27-Dec-2021**: sokol_app.h frame timing improvements:
- A new function ```double sapp_frame_duration(void)``` which returns the frame
duration in seconds, averaged over the last 256 frames to smooth out
jittering spikes. If available, this uses platform/backend specific
functions of the swapchain API:
- On Windows: DXGI's GetFrameStatistics().SyncQPCTime.
- On Emscripten: the timestamp provided by the RAF callback, this will
still be clamped and jittered on some browsers, but averaged over
a number of frames yields a pretty accurate approximation
of the actual frame duration.
- On Metal, ```MTLDrawable addPresentedHandler + presentedTime```
doesn't appear to function correctly on macOS Monterey and/or M1 Macs, so
instead mach_absolute_time() is called at the start of the MTKView
frame callback.
- In all other situations, the same timing method is used as
in sokol_time.h.
- On macOS and iOS, sokol_app.h now queries the maximum display refresh rate
of the main display and uses this as base to compute the preferred frame
rate (by multiplying with ```sapp_desc.swap_interval```), previously the
preferred frame rate was hardwired to ```60 * swap_interval```. This means
that native macOS and iOS applications may now run at 120Hz instead of
60Hz depending on the device (I realize that this isn't ideal, there
will probably be a different way to hint the preferred interval at
which the frame callback is called, which would also support disabling
vsync and probably also adaptive vsync).
- **19-Dec-2021**: some sokol_audio.h changes:
- on Windows, sokol_audio.h no longer converts audio samples
from float to int16_t, but instead configures WASAPI to directly accept
float samples. Many thanks to GitHub user iOrange for the PR!
- sokol_audio.h has a new public function ```saudio_suspended()``` which
returns true if the audio device/context is currently in suspended mode.
On all backends except WebAudio this always returns false. This allows
to show a visual hint to the user that audio is muted until the first
input event is received.
- **18-Dec-2021**: the sokol_gfx.h ```sg_draw()``` function now uses the currently applied
pipeline object to decide if the GL or D3D11 backend's instanced drawing function
should be called instead of the ```num_instances``` argument. This fixes a
bug on some WebGL configs when instanced rendering is configured
but ```sg_draw()``` is called with an instance count of 1.
- **18-Nov-2021**: sokol_gl.h has a new function to control the point size for
point list rendering: ```void sgl_point_size(float size)```. Note that on D3D11
the point size is currently ignored (since D3D11 doesn't support a point size at
all, the feature will need to be emulated in sokol_gl.h when the D3D11 backend is active).
Also note that points cannot currently be textured, only colored.
- **08-Oct-2021**: texture compression support in sokol_gfx.h has been revisited:
- tighter validation checks on texture creation:
- content data validation now also happens in ```sg_make_image()``` (previously only in ```sg_update_image()```)
- validate that compressed textures are immutable
- separate "no data" validation checks for immutable vs dynamic/stream textures
- provided data size for creating or updating textures must match the expected surface sizes exactly
- fix PVRTC row and surface pitch computation according to the GL PVRTC extension spec
- better adhere to Metal documentation for the ```MTLTexture.replaceRegion``` parameters (when bytesPerImage is expected to be zero or not)
- **02-Sep-2021**: some minor non-breaking additions:
- sokol_app.h: new events FOCUSED and UNFOCUSED to indicate that the
window has gained or lost the focused state (Win32: WM_SETFOCUS/WM_KILLFOCUS,
macOS: windowDidBecomeKey/windowDidResignKey, X11: FocusIn/FocusOut,
HTML5: focus/blur).
- sokol_app.h Emscripten backend: the input event keycode is now extracted
from the HTML5 code string which yields the actual unmapped virtual key code.
- **21-Aug-2021**: some minor API tweaks in sokol_gl.h and sokol_debugtext.h,
one of them breaking (still minor though):
- sokol_gl.h has a new function ```sgl_default_context()``` which returns the
default context handle, it's the same as the global constant SGL_DEFAULT_CONTEXT,
but wrapping this in a function is better for language bindings
- ...and a similar function in sokol_debugtext.h: ```sdtx_default_context()```
- The sokol_gl.h function ```sgl_default_pipeline()``` has been renamed to
```sgl_load_default_pipeline()```. This fits better with the related
function ```sgl_load_pipeline()``` and doesn't 'semantically clash'
with the new function sgl_default_context(). The sgl_default_pipeline()
function is rarely used, so it's quite unlikely that this change breaks
your code.
- **19-Aug-2021**: sokol_gl.h gained rendering context support, this allows
sokol-gl to render into different sokol-gfx render passes. No changes are
needed for existing sokol-gl code. Check the updated
[header documentation](https://github.com/floooh/sokol/blob/master/util/sokol_gl.h)
and the new sample
[sgl-context-sapp](https://floooh.github.io/sokol-html5/sgl-context-sapp.html)
for details!
- **21-Jun-2021**: A new utility header sokol_color.h has been added, which adds
sokol_gfx.h-compatible named color constants and a handful initial utility
functions. See the [header documentation](https://github.com/floooh/sokol/blob/master/util/sokol_color.h)
for details. Many thanks to Stuart Adams (@nyalloc) for contributing the header!
- **12-Apr-2021**: Minor new feature in sokol_app.h: mouse buttons are now
also reported as modifier flags in most input events (similar to the
Ctrl-, Alt-, Shift- and Super-key modifiers). This lets you quickly check
what mouse buttons are currently pressed in any input event without having
to keep track of pressed mouse buttons yourself. This is implemented in the following
sokol_app.h backends: Win32, UWP, Emscripten, X11 and macOS. Example
code is in the [events-sapp.cc](https://floooh.github.io/sokol-html5/events-sapp.html) sample
- **10-Apr-2021**: followup fixes from yesterday: custom icon support on macOS
has been added (since macOS has no regular window icons, the dock icon is
updated instead), and a bugfix in the internal helper which select the
best matching candidate image (this actually always selected the first
candidate image)
- **09-Apr-2021**: sokol_app.h now allows to programmatically set the window
icon in the Win32, X11 and HTML5 backends. Search for "WINDOW ICON SUPPORT"
in sokol_app.h for documentation, and see the new
[icon sample](https://floooh.github.io/sokol-html5/icon-sapp.html) for example code.
- **01-Apr-2021**: some fixes in sokol_app.h's iOS backend:
- In the iOS Metal backend, high-dpi vs low-dpi works again. Some time
ago (around iOS 12.x) MTKView started to ignore the contentScaleFactor
property, which lead to sokol_app.h always setting up a HighDPI
framebuffer even when sapp_desc.high_dpi wasn't set. The fix is to set
the MTKView's drawableSize explicitly now.
- The iOS GL backend didn't support MSAA multisampling so far, this has
been fixed now, but only one MSAA mode (4x) is available, which will be
selected when sapp_desc.sample_count is greater than 1.
- **31-Mar-2021**: sokol_audio.h on macOS no longer includes system framework
headers (AudioToolbox/AudioToolbox.h), instead the necessary declarations
are embedded directly in sokol_audio.h (to get the old behaviour and
force inclusion of AudioToolbox/AudioToolbox.h, define
```SAUDIO_OSX_USE_SYSTEM_HEADERS``` before including the sokol_audio.h
implementation). This "fix" is both an experiment and an immediate workaround
for a current issue in Zig's HEAD version (what will eventually become
zig 0.8.0). See this issue for details: https://github.com/ziglang/zig/issues/8360).
The experiment is basically to see whether this approach generally makes sense
(replacing system headers with embedded declarations, so that the sokol headers
only depend on C standard library headers). This approach might
simplify cross-compilation and integration with other languages than C and C++.
- **20-Mar-2021**: The Windows-specific OpenGL loader, and the platform-specific
GL header includes have been moved from sokol_app.h to sokol_gfx.h. This means:
- In general, the sokol_gfx.h implementation can now simply be included
without having to include other headers which provide the GL API declarations
first (e.g. when sokol_gfx.h is used without sokol_app.h, you don't need to
use a GL loader, or include the system-specific GL headers yourself).
- When sokol_gfx.h is used together with sokol_app.h, the include order
for the implementations doesn't matter anymore (until now, the sokol_app.h
implementation had to be included before the sokol_gfx.h implementation).
- The only "downside" (not really a downside) is that sokol_gfx.h now has
platform detection ifdefs to include the correct GL headers for a given
platform. Until now this problem was "delegated" to the library user.
- The old macro **SOKOL_WIN32_NO_GL_LOADER** has been removed, and replaced
with a more general **SOKOL_EXTERNAL_GL_LOADER**. Define this before
including the sokol_gfx.h implementation if you are using your own GL
loader or provide the GL API declarations in any other way. In this case,
sokol_gfx.h will not include any platform GL headers, and the embedded
Win32 GL loader will be disabled.
- **22-Feb-2021**: Mouse input latency in sokol_app.h's macOS backend has been
quite significantly reduced, please see the detailed explanation [in this
PR](https://github.com/floooh/sokol/pull/483). Many thanks to @randrew for
the PR!
- **19-Feb-2021**: sokol_app.h learned some Windows-specific config options
to redirect stdout/stderr to the parent terminal or a separate console
window, and allow outputting UTF-8 encoded text. For details, search for
"WINDOWS CONSOLE OUTPUT" in
[sokol_app.h](https://github.com/floooh/sokol/blob/master/sokol_app.h). Many
thanks to @garettbass for the initial PR!
- **17-Feb-2021**: When compiled for iOS, the sokol_audio.h CoreAudio backend now
uses the **AVAudioSession** class to activate and deactivate audio output as needed.
This fixes sokol_audio.h for iPhones (so far, sokol_audio.h accidentally only worked
for iPads). Please see [this issue](https://github.com/floooh/sokol/issues/431) for details.
A somewhat unfortunate side effect of this fix is that sokol_audio.h must now be compiled
as Objective-C when targeting iOS, also note that a new framework must be linked: ```AVFoundation```.
Many thanks to @oviano for providing the PR!
- **14-Feb-2021**: The Dear ImGui rendering backend in [sokol_imgui.h](https://github.com/floooh/sokol/blob/master/util/sokol_imgui.h) has been rewritten to only do a single
buffer-update per frame each for vertex- and index-data. This addresses performance-problems
with sg_append_buffer() in the GL backend on some platforms (see [this issue](https://github.com/floooh/sokol/issues/399) for details.
- **13-Feb-2021**: A new utility header [sokol_nuklear.h](https://github.com/floooh/sokol/blob/master/util/sokol_nuklear.h)
has been added which implements a rendering backend for [Nuklear](https://github.com/Immediate-Mode-UI/Nuklear)
on top of sokol_gfx.h. Also see the new sample [nuklear-sapp](https://floooh.github.io/sokol-html5/nuklear-sapp.html).
Many thanks to **@wmerrifield** for the PR!
- **10-Feb-2021**: The breaking API-update has been merged (mainly sokol_gfx.h).
Please see [this blogpost](https://floooh.github.io/2021/02/07/sokol-api-overhaul.html)
and the updates [sokol samples](https://floooh.github.io/sokol-html5/) for details.
I also created a git tag named 'pre-feb2021-api-changes' which captures the previous
state in all related projects. Please also update the [sokol-tools-bin](https://github.com/floooh/sokol-tools-bin) if you're using the sokol-shdc shader compiler.
- **07-Feb-2021**: A PSA about upcoming breaking changes in (mainly) sokol_gfx.h: https://floooh.github.io/2021/02/07/sokol-api-overhaul.html
- **20-Dec-2020**: A couple of minor breaking changes in the sokol_gfx.h and
sokol_app.h APIs as preparation for the upcoming automatic language binding
generation:
- in **sokol_gfx.h** nested unions have been removed:
- **sg_image_desc.depth/.layers** has been renamed to **.num_slices**
- **sg_attachment_desc.face/.layer/.slice** has been unified to **.slice**
- in **sokol_app.h** the return value of **sapp_run()** has been changed from
**int** to **void** (the function always returned zero anyway)
Non-breaking (or at most potentially breaking) changes:
- expressions in enums have been replaced with integer literals (e.g. (1<<2) becomes 4)
- the value of **SAPP_MOUSEBUTTON_INVALID** has been changed from -1 to 0x100
For more information about the upcoming automatic language-bindings generation [see this bog post](https://floooh.github.io/2020/08/23/sokol-bindgen.html)
- **02-Dec-2020**: sokol_gfx.h has a couple new public API functions for
destroying resources in two steps:
- sg_uninit_buffer + sg_dealloc_buffer
- sg_uninit_image + sg_dealloc_image
- sg_uninit_shader + sg_dealloc_shader
- sg_uninit_pipeline + sg_dealloc_pipeline
- sg_uninit_pass + sg_dealloc_pass
Calling both functions in this order is identical with calling the
traditional sg_destroy_xxx() functions. See this PR for more details:
https://github.com/floooh/sokol/pull/435. Many thanks to @oviano for the
PR!
- **28-Nov-2020**: In addition to the generic SOKOL_API_DECL and SOKOL_IMPL
defines there are now header-specific versions SOKOL_xxx_API_DECL and
SOKOL_xxx_IMPL (for instance SOKOL_GFX_API_DECL and SOKOL_GFX_IMPL). The
original motivation for splitting the SOKOL_API_DECL defines up is described
here: https://github.com/floooh/sokol/issues/428). The same change for
SOKOL_IMPL also finally unifies the approach used in the utility headers (in
the ```util``` subdirectory), which exclusively used the SOKOL_xxx_IMPL
pattern with the core headers which exclusively used SOKOL_IMPL before (all
headers accept both patterns now). Many thanks to @iboB for providing the
API_DECL PR!
- **17-Nov-2020**: A new utility header **sokol_shape.h** to generate
vertices+indices for simple shapes (plane, box, sphere, cylinder and torus),
which seamlessly plug into the sokol_gfx.h resource creation functions. As
with most new utility headers, the initial functionality is a bit bare bones
and the public API shouldn't be considered stable yet. Check the sokol-samples
webpage for new and updates samples: https://floooh.github.io/sokol-html5/
- **08-Nov-2020** PSA: It appears that RenderDoc v1.10 chokes on the new
D3D11/DXGI swapchain code from 10-Oct-2020 in sokol_app.h. The current
RenderDoc Nightly Build works, so I guess in v1.11 everything will be fine.
- **03-Nov-2020**: sokol_app.h: the missing drag'n'drop support for HTML5/WASM
has been added. This adds two platform-specific functions
```sapp_html5_get_dropped_file_size()``` and
```sapp_html5_fetch_dropped_file()```. Please read the documentation
section in sokol_app.h under 'DRAG AND DROP SUPPORT' for additional
details and example code. Also consult the source code of the new
```droptest-sapp``` sample for an example of how to load the content
of dropped files on the web and native platforms:
https://floooh.github.io/sokol-html5/droptest-sapp.html
- **27-Oct-2020**: I committed a bugfix for a longstanding WebGL canvas id versus
css-selector confusion in the emscripten/WASM backend code in sokol_app.h.
I think the fix should not require any changes in your code (because if
you'd be using a canvas name different from the default "canvas" it wouldn't
have worked before anyway). See this bug for details: https://github.com/floooh/sokol/issues/407
- **22-Oct-2020**: sokol_app.h now has file drag'n'drop support on Win32,
macOS and Linux. WASM/HTML5 support will be added soon-ish. This will
work a bit differently because of security-related restrictions in the
HTML5 drag'n'drop API, but more on that later. For documentation,
search for 'DRAG AND DROP SUPPORT' in [sokol_app.h](https://github.com/floooh/sokol/blob/master/sokol_app.h).
Check out [events-sapp.c](https://github.com/floooh/sokol-samples/blob/master/sapp/events-sapp.cc)
for a simple usage example (I will also add a more real-world example to my
chips emulators once the WASM/HTML5 implementation is ready).
Many thanks for @prime31 and @hb3p8 for the initial PRs and valuable feature
discussions!
- **10-Oct-2020**: Improvements to the sokol_app.h Win32+D3D11 and UWP+D3D11 swapchain code:
- In the Win32+D3D11 backend and when running on Win10,
```DXGI_SWAP_EFFECT_FLIP_DISCARD``` is now used. This gets rid of a
deprecation warning in the debugger console and also should allow slightly
more efficient swaps in some situations. When running on Win7 or Win8, the
traditional ```DXGI_SWAP_EFFECT_DISCARD``` is used.
- The UWP backend now supports MSAA multisampling (the required fixes for
this are the same as in the Win32 backend with the new swap effect: a
separate MSAA texture and render-target-view is created where
rendering goes into, and this MSAA texture is resolved into the actual
swapchain surface before presentation).
- **07-Oct-2020**:
A fix in the ALSA/Linux backend initialization in sokol_audio.h: Previously,
initialization would fail if ALSA can't allocate the exact requested
buffer size. Instead sokol_audio.h let's now pick ALSA a suitable buffer
size. Also better log messages in the ALSA initialization code if something
goes wrong. Unfortunately I'm not able to reproduce the buffer allocation
problem on my Linux machine. Details are in this issue: https://github.com/floooh/sokol/issues/400
**NARRATOR**: the fix didn't work.
- **02-Oct-2020**:
The sokol_app.h Win32 backend can now render while moving and resizing
the window. NOTE that resizing the swapchain buffers (and receiving
SAPP_EVENTTYPE_RESIZED events) is deferred until the resizing finished.
Resizing the swapchain buffers each frame created a substantial temporary
memory spike of up to several hundred MBytes. I need to figure out a better
swapchain resizing strategy.
- **30-Sep-2020**:
sokol_audio.h now works on UWP, thanks again to Alberto Fustinoni
(@albertofustinoni) for the PR!
- **26-Sep-2020**:
sokol_app.h gained a new function sapp_set_window_title() to change
the window title on Windows, macOS and Linux. Many thanks to
@medvednikov for the initial PR!
- **23-Sep-2020**:
sokol_app.h now has initial UWP support using the C++/WinRT set of APIs.
Currently this requires "bleeding edge" tools: A recent VS2019 version,
and a very recent Windows SDK version (at least version 10.0.19041.0).
Furthermore the sokol_app.h implementation must be compiled as C++17
(this is a requirement of the C++/WinRT headers). Note that the Win32
backend will remain the primary and recommended backend on Windows. The UWP
backend should only be used when the Win32 backend is not an option.
The [sokol-samples](https://github.com/floooh/sokol-samples) project
has two new build configs ```sapp-uwp-vstudio-debug``` and
```sapp-uwp-vstudio-release``` to build the sokol-app samples for UWP.
Many thanks to Alberto Fustinoni (@albertofustinoni) for providing
the initial PR!
(also NOTE: UWP-related fixes in other sokol headers will follow)
- **22-Sep-2020**:
A small fix in sokol_app.h's Win32 backend: when a mouse button is pressed,
mouse input is now 'captured' by calling SetCapture(), and when the last
mouse button is released, ReleaseCapture() is called. This also provides
mouse events outside the window area as long as a mouse button is pressed,
which is useful for windowed UI applicactions (this is not the same as the
more 'rigorous' and explicit pointer-lock feature which is more useful for
camera-controls)
- **31-Aug-2020**:
Internal change: The D3D11/DXGI backend code in sokol_gfx.h and sokol_app.h
now use the D3D11 and DXGI C++-APIs when the implementation is compiled as
C++, and the C-APIs when the implementation is compiled as C (before, the C
API was also used when the implementation is compiled as C++). The new
behaviour is useful when another header *must* use the D3D11/DXGI C++ APIs
but should be included in the same compilation unit as sokol_gfx.h an
sokol_app.h (for example see this PR:
https://github.com/floooh/sokol/pull/351).
- **24-Aug-2020**:
The backend-specific callback functions that are provided to sokol_gfx.h
in the ```sg_setup()``` initialization call now have alternative
versions which accept a userdata-pointer argument. The userdata-free functions
still exist, so no changes are required for existing code.
- **02-Aug-2020**:
- sokol_app.h now has a mouse-lock feature (aka pointer-lock) via two
new functions ```void sapp_lock_mouse(bool lock)``` and ```bool sapp_mouse_locked(void)```.
For documentation, please search for 'MOUSE LOCK' in sokol_app.h.
The sokol-app samples [events-sapp](https://floooh.github.io/sokol-html5/events-sapp.html)
and [cgltf-sapp](https://floooh.github.io/sokol-html5/cgltf-sapp.html) have been
updated to demonstrate the feature.
- sokol_app.h Linux: mouse pointer visibility (via ```void sapp_show_mouse(bool show)```)
has been implemented for Linux/X11
- sokol_app.h WASM: mouse wheel scroll deltas are now 'normalized' between
the different scroll modes (pixels, lines, pages). See this issue:
https://github.com/floooh/sokol/issues/339. Many thanks to @bqqbarbhg for
investigating the issue and providing a solution!
- sokol_app.h now has [better documentation](https://github.com/floooh/sokol/blob/89a3bb8da0a2df843d6cc60a270ddc69f9aa69d6/sokol_app.h#L70)
what system libraries must be linked on the various platforms (and on Linux two additional libraries must be
linked now: Xcursor and Xi)
- **22-Jul-2020**: **PLEASE NOTE** cmake 3.18 breaks some of sokol samples when
compiling with the Visual Studio toolchain because some C files now actually
compile as C++ for some reason (see:
https://twitter.com/FlohOfWoe/status/1285996526117040128). Until this is
fixed, or I have come up with a workaround, please use an older cmake version
to build the sokol samples with the Visual Studio compiler.
(Update: I have added a workaround to fips: https://github.com/floooh/fips/commit/89997b8ebdca6fc9455a5cfe6145eecaa017df49
which fixes the issue at least for fips projects)
- **14-Jul-2020**:
- sapp_mouse_shown() has been implemented for macOS (thanks to @slmjkdbtl for
providing the initial PR!)
- On macOS, the lower-level functions CGDisplayShowCursor and CGDisplayHideCursor
are now used instead of the NSCursor class. This is in preparation for the
'pointer lock' feature which will also use CGDisplay* functions.
- Calling ```sapp_show_mouse(bool visible)``` no longer 'stacks' (e.g. there's
no 'hidden counter' underneath anymore, instead calling ```sapp_show_mouse(true)```
will always show the cursor and ```sapp_show_mouse(false)``` will always
hide it. This is a different behaviour than the underlying Win32 and
macOS functions ShowCursor() and CGDisplaShow/HideCursor()
- The mouse show/hide behaviour can now be tested in the ```events-sapp``` sample
(so far this only works on Windows and macOS).
- **13-Jul-2020**:
- On macOS and iOS, sokol_app.h and sokol_gfx.h can now be compiled with
ARC (Automatic Reference Counting) **disabled** (previously ARC had to be
enabled).
- Compiling with ARC enabled is still supported but with a little caveat:
if you're compiling sokol_app.h or sokol_gfx.h in ObjC mode (not ObjC++
mode) *AND* ARC is enabled, then the Xcode version must be more recent
than before (the language feature ```__has_feature(objc_arc_fields)```
must be supported, which I think has been added in Xcode 10.2, I couldn't
find this mentioned in any Xcode release notes though). Compiling with
ARC disabled should also work on older Xcode versions though.
- Various internal code cleanup things:
- sokol_app.h had the same 'structural cleanup' as sokol_gfx.h in
January, all internal state (including ObjC id's) has been merged into
a single big state structure. Backend specific struct declarations
have been moved closer together in the header, and
backend-specific structures and functions have been named more
consistently for better 'searchability'
- The 'mini GL' loader in the sokol_app.h Win32+WGL backend has been
rewritten to use X-Macros (less redundant lines of code)
- All macOS and iOS code has been revised and cleaned up
- On macOS a workaround for a (what looks like) post-Catalina
NSOpenGLView issue has been added: if the sokol_app.h window doesn't
fit on screen (and was thus 'clamped' by Cocoa) *AND* the
content-size was not set to native Retina resolution, the initial
content size was reported as if it was in Retina resolution. This
caused an empty screen to be rendered in the imgui-sapp demo. The
workaround is to hook into the NSOpenGLView reshape event at which
point the reported content size is correct.
- On macOS and iOS, the various 'view delegate' objects have been
removed, and rendering happens instead in the subclasses of MTKView,
GLKView and NSOpenGLView.
- On macOS and iOS, there's now proper cleanup code in the
applicationWillTerminate callback (although note that on iOS this
function isn't guaranteed to be called, because an application can
also simply be killed by the operating system.
- **22-Jun-2020**: The X11/GLX backend in sokol_app.h now has (soft-)fullscreen
support, bringing the feature on par with Windows and macOS. Many thanks to
@medvednikov for the PR!
- **20-Jun-2020**: Some work to better support older DX10-level GPUs in the
sokol_gfx.h D3D11 backend:
- sg_make_shader() now by default compiles HLSL shader code as shader model 4.0
(previously shader model 5.0 which caused problems with some older
Intel GPUs still in use, see this issue: https://github.com/floooh/sokol/issues/179)
- A new string item ```const char* d3d11_target``` in ```sg_shader_stage_desc``` now allows
to pass in the D3D shader model for compiling shaders. This defaults to
"vs_4_0" for the vertex shader stage and "ps_4_0" for the fragment shader stage.
The minimal DX shader model for use with the sokol_gfx.h D3D11 backend is
shader model 4.0, because that's the first shader model supporting
constant buffers.
- The *sokol-shdc* shader compiler tool has a new output option ```hlsl4```
to generate HLSL4 source code and shader model 4.0 byte code.
- All embedded D3D shader byte code in the sokol utility headers has been
changed from shader model 5.0 to 4.0
If you are using sokol_gfx.h with sokol-shdc, please update both at the same time
to avoid compilation errors caused by the new ```sg_shader_stage_desc.d3d11_target```
item. The sg_shader_desc initialization code in sokol-shdc has now been made more
robust to prevent similar problems in the future.
- **14-Jun-2020**: I have added a very simple utility header ```sokol_memtrack.h```
which allows to track memory allocations in sokol headers (number and overall
size of allocations) by overriding the macros SOKOL_MALLOC, SOKOL_CALLOC and
SOKOL_FREE. Simply include ```sokol_memtrack.h``` before the other sokol
header implementation includes to enable memory tracking in those headers
(but remember that the sokol_memtrack.h implementation must only be included
once in the whole project, so this only works when all other sokol header
implementations are included in the same compilation unit).
- **06-Jun-2020**: Some optimizations in the sokol_gfx.h GL backend to avoid
redundant GL calls in two areas: in the sg_begin_pass() calls when not
clearing the color- and depth-stencil-attachments, and in sg_apply_bindings()
when binding textures. Everything should behave exactly as before, but if
you notice any problems in those areas, please file a bug. Many thanks to
@edubart for the PRs!
- **01-Jun-2020**: sokol_app.h now allows to toggle to and from fullscreen
programmatically and to query the current fullscreen state via 2 new
functions: ```sapp_toggle_fullscreen()``` and ```sapp_is_fullscreen()```.
Currently this is only implemented for Windows and macOS (not Linux).
Thanks to @mattiasljungstrom for getting the feature started and providing
the Win32 implementation!
- **28-May-2020**: a small quality-of-life improvement for C++ coders: when the
sokol headers are included into C++, all public API functions which take a
pointer to a struct now have a C++ overload which instead takes a const-ref.
This allows to move the struct initialization right into the function call
just like in C99. For instance, in C99 one can write:
```c
sg_buffer buf = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(vertices),
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.content = vertices
});
```
In C++ it isn't possible to take the address of an 'adhoc-initialized'
struct like this, but with the new reference-wrapper functions (and C++20
designated initialization) this should work now:
```cpp
sg_buffer buf = sg_make_buffer({
.size = sizeof(vertices),
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.content = vertices
});
```
Many thanks to @garettbass for providing the PR!
- **27-May-2020**: a new utility header [sokol_debugtext.h](https://github.com/floooh/sokol/blob/master/util/sokol_debugtext.h)
for rendering simple ASCII text using vintage home computer fonts via sokol_gfx.h
- **13-May-2020**: a new function in sokol_time.h to round a measured frame time
to common display refresh rates: ```stm_round_to_common_refresh_rate()```.
See the header documentation for the motivation behind this function.
- **02-May-2020**: sokol_app.h: the 'programmatic quit' behaviour on the
web-platform is now more in line with other platforms: calling
```sapp_quit()``` will invoke the cleanup callback function, perform
platform-specific cleanup (like unregistering JS event handlers), and finally
exit the frame loop. In typical scenarios this isn't very useful (because
usually the user will simply close the tab, which doesn't allow to run
cleanup code), but it's useful for situations where the same
code needs to run repeatedly on a web page. Many thanks to @caiiiycuk
for providing the PR!
- **30-Apr-2020**: experimental WebGPU backend and a minor breaking change:
- sokol_gfx.h: a new WebGPU backend, expect frequent breakage for a while
because the WebGPU API is still in flux
- a new header sokol_glue.h, with interop helper functions when specific combinations
of sokol headers are used together
- changes in the way sokol_gfx.h is initialized via a new layout of the
sg_desc structure
- sokol_gfx.h: a new ```sg_sampler_type``` enum which is required for
shader creation to tell the WebGPU backend about the sampler data types
(float, signed int, or unsigned int) used in the shader
- sokol_app.h: a handful new functions to query default framebuffer attributes (color- and
depth-buffer pixel formats, and MSAA sample count)
- sokol_app.h: WebGPU device and swapchain initialization (currently only
in the emscripten code path)
- [sokol-shdc](https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md) has
been updated with WebGPU support (currently outputs SPIRV bytecode), and to output the new
```sg_sampler_type``` enum in ```sg_shader_image_desc```
- [sokol-samples](https://github.com/floooh/sokol-samples/) has a new set of
backend-specific WebGPU samples, and the other samples have been updated
for the new sokol-gfx initialization
- ```pre-webgpu``` tags have been added to the [sokol](https://github.com/floooh/sokol/releases/tag/pre-webgpu), [sokol-samples](https://github.com/floooh/sokol-samples/releases/tag/pre-webgpu), [sokol-tools](https://github.com/floooh/sokol-tools/releases/tag/pre-webgpu)
and [sokol-tools-bin](https://github.com/floooh/sokol-tools-bin/releases/tag/pre-webgpu) github repositories (in case you need to continue working with
the older versions)
- please see this [blog post](https://floooh.github.io/2020/04/26/sokol-spring-2020-update.html)
for more details
- **05-Apr-2020**: A bugfix in sokol_gl.h, the (fairly recent) optimization for
merging draw calls contained a bug that could be triggered in an "empty"
sgl_begin/sgl_end pair (with no vertices recorded inbetween). This could
lead to the following draw call being rendered with the wrong uniform data.
- **30-Jan-2020**: Some cleanup in sokol_gfx.h in the backend implementation code,
internal data structures and documentation comments. The public
API hasn't changed, so the change should be completely invisible
from the outside.
- **02-Dec-2019**: Initial clipboard support in sokol_app.h for Windows, macOS
and HTML5. This allows to read and write UTF-8 encoded strings from and
to the target platform's shared clipboard.
A 'real-world' example usage is in the [Visual6502 Remix project](https://github.com/floooh/v6502r).
Unfortunately clipboard support on the HTML5 platform comes with a lot of
platform-specific caveats which can't be solved in sokol_app.h alone
because of the restrictions the web platform puts on clipboard access and
different behaviours and support levels of the various HTML5 clipboard
APIs. I'm not really happy with the current HTML5 clipboard
implementation. It sorta works, but it sure ain't pretty :)
Maybe the situation will improve in a few years when all browsers agree
on and support the new [permission-based clipboard
API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API).
For documentation of the clipboard feature, search for CLIPBOARD SUPPORT
in sokol_app.h
- **08-Sep-2019**: sokol_gfx.h now supports clamp-to-border texture sampling:
- the enum ```sg_wrap``` has a new member ```SG_WRAP_CLAMP_TO_BORDER```
- there's a new enum ```sg_border_color```
- the struct ```sg_image_desc``` has a new member ```sg_border_color border_color```
- new feature flag in ```sg_features```: ```image_clamp_to_border```
Note the following caveats:
- clamp-to-border is only supported on a subset of platforms, support can
be checked at runtime via ```sg_query_features().image_clamp_to_border```
(D3D11, desktop-GL and macOS-Metal support clamp-to-border,
all other platforms don't)
- there are three hardwired border colors: transparent-black,
opaque-black and opaque-white (modern 3D APIs have moved away from
a freely programmable border color)
- if clamp-to-border is not supported, sampling will fall back to
clamp-to-edge without a validation warning
Many thanks to @martincohen for suggesting the feature and providing the initial
D3D11 implementation!
- **31-Aug-2019**: The header **sokol_gfx_cimgui.h** has been merged into
[**sokol_gfx_imgui.h**](https://github.com/floooh/sokol/blob/master/util/sokol_gfx_imgui.h).
Same idea as merging sokol_cimgui.h into sokol_imgui.h, the implementation
is now "bilingual", and can either be included into a C++ file or into a C file.
When included into a C++ file, the Dear ImGui C++ API will be called directly,
otherwise the C API bindings via cimgui.h
- **28-Aug-2019**: The header **sokol_cimgui.h** has been merged into
[**sokol_imgui.h**](https://github.com/floooh/sokol/blob/master/util/sokol_imgui.h).
The sokol_cimgui.h header had been created to implement Dear ImGui UIs from
pure C applications, instead of having to fall back to C++ just for the UI
code. However, there was a lot of code duplication between sokol_imgui.h and
sokol_cimgui.h, so that it made more sense to merge the two headers. The C vs
C++ code path will be selected automatically: When the implementation of
sokol_imgui.h is included into a C++ source file, the Dear ImGui C++ API will
be used. Otherwise, when the implementation is included into a C source file,
the C API via cimgui.h
- **27-Aug-2019**: [**sokol_audio.h**](https://github.com/floooh/sokol/blob/master/sokol_audio.h)
now has an OpenSLES backend for Android. Many thanks to Sepehr Taghdisian (@septag)
for the PR!
- **26-Aug-2019**: new utility header for text rendering, and fixes in sokol_gl.h:
- a new utility header [**sokol_fontstash.h**](https://github.com/floooh/sokol/blob/master/util/sokol_fontstash.h)
which implements a renderer for [fontstash.h](https://github.com/memononen/fontstash)
on top of sokol_gl.h
- **sokol_gl.h** updates:
- Optimization: If no relevant state between two begin/end pairs has
changed, draw commands will be merged into a single sokol-gfx draw
call. This is especially useful for text- and sprite-rendering (previously,
each begin/end pair would always result in one draw call).
- Bugfix: When calling sgl_disable_texture() the previously active
texture would still remain active which could lead to rendering
artefacts. This has been fixed.
- Feature: It's now possible to provide a custom shader in the
'desc' argument of *sgl_make_pipeline()*, as long as the shader
is "compatible" with sokol_gl.h, see the sokol_fontstash.h
header for an example. This feature isn't "advertised" in the
sokol_gl.h documentation because it's a bit brittle (for instance
if sokol_gl.h updates uniform block structures, custom shaders
would break), but it may still come in handy in some situations.
- **20-Aug-2019**: sokol_gfx.h has a couple new query functions to inspect the
default values of resource-creation desc structures:
```c
sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc);
sg_image_desc sg_query_image_defaults(const sg_image_desc* desc);
sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc);
sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc);
sg_pass_desc sg_query_pass_defaults(const sg_pass_desc* desc);
```
These functions take a pointer to a resource creation desc struct that
may contain zero-initialized values (to indicate default values) and
return a new struct where the zero-init values have been replaced with
concrete values. This is useful to inspect the actual creation attributes
of a resource.
- **18-Aug-2019**:
- Pixelformat and runtime capabilities modernization in sokol_gfx.h (breaking changes):
- The list of pixel formats supported in sokol_gfx.h has been modernized,
many new formats are available, and some formats have been removed. The
supported pixel formats are now identical with what WebGPU provides,
minus the SRGB formats (if SRGB conversion is needed it should be done
in the pixel shader)
- The pixel format list is now more "orthogonal":
- one, two or four color components (R, RG, RGBA)
- 8-, 16- or 32-bit component width
- unsigned-normalized (no postfix), signed-normalized (SN postfix),
unsigned-integer (UI postfix) and signed-integer (SI postfix)
and float (F postfix) component types.
- special pixel formats BGRA8 (default render target format on
Metal and D3D11), RGB10A2 and RG11B10F
- DXT compressed formats replaced with BC1 to BC7 (BC1 to BC3
are identical to the old DXT pixel formats)
- packed 16-bit formats (like RGBA4) have been removed
- packed 24-bit formats (RGB8) have been removed
- Use the new function ```sg_query_pixelformat()``` to get detailed
runtime capability information about a pixelformat (for instance
whether it is supported at all, can be used as render target etc...).
- Use the new function ```sg_query_limits()``` to query "numeric limits"
like maximum texture dimensions for different texture types.
- The enumeration ```sg_feature``` and the function ```sg_query_feature()```
has been replaced with the new function ```sg_query_features()```, which
returns a struct ```sg_features``` (this contains a bool for each
optional feature).
- The default pixelformat for render target images and pipeline objects
now depends on the backend:
- for GL backends, the default pixelformat stays the same: RGBA8
- for the Metal and D3D11 backends, the default pixelformat for
render target images is now BGRA8 (the reason is because
MTKView's pixelformat was always BGRA8 but this was "hidden"
through an internal hack, and a BGRA swapchain is more efficient
than RGBA in D3D11/DXGI)
- Because of the above RGBA/BGRA change, you may see pixelformat validation
errors in existing code if the code assumes that a render target image is
always created with a default pixelformat of RGBA8.
- Changes in sokol_app.h:
- The D3D11 backend now creates the DXGI swapchain with BGRA8 pixelformat
(previously: RGBA8), this allows more efficient presentation in some
situations since no format-conversion-blit needs to happen.
- **18-Jul-2019**:
- sokol_fetch.h has been fixed and can be used again :)
- **11-Jul-2019**:
- Don't use sokol_fetch.h for now, the current version assumes that
it is possible to obtain the content size of a file from the
HTTP server without downloading the entire file first. Turns out
that's not possible with vanilla HTTP when the web server serves
files compressed (in that case the Content-Length is the _compressed_
size, yet JS/WASM only has access to the uncompressed data).
Long story short, I need to go back to the drawing board :)
- **06-Jul-2019**:
- new header [sokol_fetch.h](https://github.com/floooh/sokol/blob/master/sokol_fetch.h) for asynchronously loading data.
- make sure to carefully read the embedded documentation
for making the best use of the header
- two new samples: [simple PNG file loadng with stb_image.h](https://floooh.github.io/sokol-html5/loadpng-sapp.html) and [MPEG1 streaming with pl_mpeg.h](https://floooh.github.io/sokol-html5/plmpeg-sapp.html)
- sokol_gfx.h: increased SG_MAX_SHADERSTAGE_BUFFERS configuration
constant from 4 to 8.
- **10-Jun-2019**: sokol_app.h now has proper "application quit handling":
- a pending quit can be intercepted, for instance to show a "Really Quit?" dialog box
- application code can now initiate a "soft quit" (interceptable) or
"hard quit" (not interceptable)
- on the web platform, the standard "Leave Site?" dialog box implemented
by browsers can be shown when the user leaves the site
- Android and iOS currently don't have any of those features (since the
operating system may decide to terminate mobile applications at any time
anyway, if similar features are added they will most likely have
similar limitations as the web platform)
For details, search for 'APPLICATION QUIT' in the sokol_app.h documentation
header: https://github.com/floooh/sokol/blob/master/sokol_app.h
The [imgui-highdpi-sapp](https://github.com/floooh/sokol-samples/tree/master/sapp)
contains sample code for all new quit-related features.
- **08-Jun-2019**: some new stuff in sokol_app.h:
- the ```sapp_event``` struct has a new field ```bool key_repeat```
which is true when a keyboard event is a key-repeat (for the
event types ```SAPP_EVENTTYPE_KEY_DOWN``` and ```SAPP_EVENTTYPE_CHAR```).
Many thanks to [Scott Lembcke](https://github.com/slembcke) for
the pull request!
- a new function to poll the internal frame counter:
```uint64_t sapp_frame_count(void)```, previously the frame counter
was only available via ```sapp_event```.
- also check out the new [event-inspector sample](https://floooh.github.io/sokol-html5/wasm/events-sapp.html)
- **04-Jun-2019**: All sokol headers now recognize a config-define ```SOKOL_DLL```
if sokol should be compiled into a DLL (when used with ```SOKOL_IMPL```)
or used as a DLL. On Windows, this will prepend the public function declarations
with ```__declspec(dllexport)``` or ```__declspec(dllimport)```.
- **31-May-2019**: if you're working with emscripten and fips, please note the
following changes:
https://github.com/floooh/fips#public-service-announcements
- **27-May-2019**: some D3D11 updates:
- The shader-cross-compiler can now generate D3D bytecode when
running on Windows, see the [sokol-shdc docs](https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md) for more
details.
- sokol_gfx.h no longer needs to be compiled with a
SOKOL_D3D11_SHADER_COMPILER define to enable shader compilation in the
D3D11 backend. Instead, the D3D shader compiler DLL (d3dcompiler_47.dll)
will be loaded on-demand when the first HLSL shader needs to be compiled.
If an application only uses D3D shader byte code, the compiler DLL won't
be loaded into the process.
- **24-May-2019** The shader-cross-compiler can now generate Metal byte code
for macOS or iOS when the build is running on macOS. This is enabled
automatically with the fips-integration files in [sokol-tools-bin](https://github.com/floooh/sokol-tools-bin),
see the [sokol-shdc docs](https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md) for more
details.
- **16-May-2019** two new utility headers: *sokol_cimgui.h* and *sokol_gfx_cimgui.h*,
those are the same as their counterparts sokol_imgui.h and sokol_gfx_imgui.h, but
use the [cimgui](https://github.com/cimgui/cimgui) C-API for Dear ImGui. This
is useful if you don't want to - or cannot - use C++ for creating Dear ImGui UIs.
Many thanks to @prime31 for contributing those!
sokol_cimgui.h [is used
here](https://floooh.github.io/sokol-html5/wasm/cimgui-sapp.html), and
sokol_gfx_cimgui.h is used for the [debugging UI
here](https://floooh.github.io/sokol-html5/wasm/sgl-microui-sapp-ui.html)
- **15-May-2019** there's now an optional shader-cross-compiler solution for
sokol_gfx.h: [see here for details](https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md).
This is "V1.0" with two notable features missing:
- an include-file feature for GLSL shaders
- compilation to Metal- and D3D-bytecode (currently
only source-code generation is supported)
The [sokol-app samples](https://floooh.github.io/sokol-html5/) have been
ported to the new shader-cross-compilation, follow the ```src``` and
```glsl``` links on the specific sample webpages to see the C- and GLSL-
source-code.
- **02-May-2019** sokol_gfx.h has a new function ```sg_query_backend()```, this
will return an enum ```sg_backend``` identifying the backend sokol-gfx is
currently running on, which is one of the following values:
- SG_BACKEND_GLCORE33
- SG_BACKEND_GLES2
- SG_BACKEND_GLES3
- SG_BACKEND_D3D11
- SG_BACKEND_METAL_MACOS
- SG_BACKEND_METAL_IOS
When compiled with SOKOL_GLES3, sg_query_backend() may return SG_BACKEND_GLES2
when the runtime platform doesn't support GLES3/WebGL2 and had to fallback
to GLES2/WebGL2.
When compiled with SOKOL_METAL, sg_query_backend() will return SG_BACKEND_METAL_MACOS
when the compile target is macOS, and SG_BACKEND_METAL_IOS when the target is iOS.
- **26-Apr-2019** Small but breaking change in **sokol_gfx.h** how the vertex
layout definition in sg_pipeline_desc works:
Vertex component names and semantics (needed by the GLES2 and D3D11 backends) have moved from ```sg_pipeline_desc``` into ```sg_shader_desc```.
This may seem like a rather pointless small detail to change, especially
for breaking existing code, but the whole thing will make a bit more
sense when the new shader-cross-compiler will be integrated which I'm
currently working on (here: https://github.com/floooh/sokol-tools).
While working on getting reflection data out of the shaders (e.g. what
uniform blocks and textures the shader uses), it occurred to me that
vertex-attribute-names and -semantics are actually part of the reflection
info and belong to the shader, not to the vertex layout in the pipeline
object (which only describes how the incoming vertex data maps to
vertex-component **slots**. Instead of (optionally) mapping this
association through a name, the pipeline's vertex layout is now always
strictly defined in terms of numeric 'bind slots' for **all** sokol_gfx.h
backends. For 3D APIs where the vertex component slot isn't explicitly
defined in the shader language (GLES2/WebGL, D3D11, and optionally
GLES3/GL), the shader merely offers a lookup table how vertex-layout
slot-indices map to names/semantics (and the underlying 3D API than maps
those names back to slot indices, which shows that Metal and GL made the
right choice defining the slots right in the shader).
Here's how the code changes (taken from the triangle-sapp.c sample):
**OLD**:
```c
/* create a shader */
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.vs.source = vs_src,
.fs.source = fs_src,
});
/* create a pipeline object (default render states are fine for triangle) */
pip = sg_make_pipeline(&(sg_pipeline_desc){
/* if the vertex layout doesn't have gaps, don't need to provide strides and offsets */
.shader = shd,
.layout = {
.attrs = {
[0] = { .name="position", .sem_name="POS", .format=SG_VERTEXFORMAT_FLOAT3 },
[1] = { .name="color0", .sem_name="COLOR", .format=SG_VERTEXFORMAT_FLOAT4 }
}
},
});
```
**NEW**:
```c
/* create a shader */
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.attrs = {
[0] = { .name="position", .sem_name="POS" },
[1] = { .name="color0", .sem_name="COLOR" }
},
.vs.source = vs_src,
.fs.source = fs_src,
});
/* create a pipeline object (default render states are fine for triangle) */
pip = sg_make_pipeline(&(sg_pipeline_desc){
/* if the vertex layout doesn't have gaps, don't need to provide strides and offsets */
.shader = shd,
.layout = {
.attrs = {
[0].format=SG_VERTEXFORMAT_FLOAT3,
[1].format=SG_VERTEXFORMAT_FLOAT4
}
},
});
```
```sg_shader_desc``` has a new embedded struct ```attrs``` which
contains a vertex attribute _name_ (for GLES2/WebGL) and
_sem_name/sem_index_ (for D3D11). For the Metal backend this struct is
ignored completely, and for GLES3/GL it is optional, and not required
when the vertex shader inputs are annotated with ```layout(location=N)```.
The remaining attribute description members in ```sg_pipeline_desc``` are:
- **.format**: the format of input vertex data (this can be different
from the vertex shader's inputs when data is extended during
vertex fetch (e.g. input can be vec3 while the vertex shader
expects vec4)
- **.offset**: optional offset of the vertex component data (not needed
when the input vertex has no gaps between the components)
- **.buffer**: the vertex buffer bind slot if the vertex data is coming
from different buffers
Also check out the various samples:
- for GLSL (explicit slots via ```layout(location=N)```): https://github.com/floooh/sokol-samples/tree/master/glfw
- for D3D11 (semantic names/indices): https://github.com/floooh/sokol-samples/tree/master/d3d11
- for GLES2: (vertex attribute names): https://github.com/floooh/sokol-samples/tree/master/html5
- for Metal: (explicit slots): https://github.com/floooh/sokol-samples/tree/master/metal
- ...and all of the above combined: https://github.com/floooh/sokol-samples/tree/master/sapp
- **19-Apr-2019** I have replaced the rather inflexible render-state handling
in **sokol_gl.h** with a *pipeline stack* (like the GL matrix stack, but with
pipeline-state-objects), along with a couple of other minor API tweaks.
These are the new pipeline-stack functions:
```c
sgl_pipeline sgl_make_pipeline(const sg_pipeline_desc* desc);
void sgl_destroy_pipeline(sgl_pipeline pip);
void sgl_default_pipeline(void);
void sgl_load_pipeline(sgl_pipeline pip);
void sgl_push_pipeline(void);
void sgl_pop_pipeline(void);
```
A pipeline object is created just like in sokol_gfx.h, but without shaders,
vertex layout, pixel formats, primitive-type and sample count (these details
are filled in by the ```sgl_make_pipeline()``` wrapper function. For instance
to create a pipeline object for additive transparency:
```c
sgl_pipeline additive_pip = sgl_make_pipeline(&(sg_pipeline_desc){
.blend = {
.enabled = true,
.src_factor_rgb = SG_BLENDFACTOR_ONE,
.dst_factor_rgb = SG_BLENDFACTOR_ONE
}
});
```
And to render with this, simply call ```sgl_load_pipeline()```:
```c
sgl_load_pipeline(additive_pip);
sgl_begin_triangles();
...
sgl_end();
```
Or to preserve and restore the previously active pipeline state:
```c
sgl_push_pipeline();
sgl_load_pipeline(additive_pip);
sgl_begin_quads();
...
sgl_end();
sgl_pop_pipeline();
```
You can also load the 'default pipeline' explicitly on the top of the
pipeline stack with ```sgl_default_pipeline()```.
The other API change is:
- ```sgl_state_texture(bool b)``` has been replaced with ```sgl_enable_texture()```
and ```sgl_disable_texture()```
The code samples have been updated accordingly:
- [sgl-sapp.c](https://github.com/floooh/sokol-samples/blob/master/sapp/sgl-sapp.c)
- [sgl-lines-sapp.c](https://github.com/floooh/sokol-samples/blob/master/sapp/sgl-lines-sapp.c)
- [sgl-microui-sapp.c](https://github.com/floooh/sokol-samples/blob/master/sapp/sgl-microui-sapp.c)
- **01-Apr-2019** (not an April Fool's joke): There's a new **sokol_gl.h**
util header which implements an 'OpenGL-1.x-in-spirit' rendering API on top
of sokol_gfx.h (vertex specification via begin/end, and a matrix stack). This is
only a small subset of OpenGL 1.x, mainly intended for debug-visualization or
simple tasks like 2D UI rendering. As always, sample code is in the
[sokol-samples](https://github.com/floooh/sokol-samples) project.
- **15-Mar-2019**: various Dear ImGui related changes:
- there's a new utility header sokol_imgui.h with a simple drop-in
renderer for Dear ImGui on top of sokol_gfx.h and sokol_app.h
(sokol_app.h is optional, and only used for input handling)
- the sokol_gfx_imgui.h debug inspection header no longer
depends on internal data structures and functions of sokol_gfx.h, as such
it is now a normal *utility header* and has been moved to the *utils*
directory
- the implementation macro for sokol_gfx_imgui.h has been changed
from SOKOL_IMPL to SOKOL_GFX_IMGUI_IMPL (so when you suddenly get
unresolved linker errors, that's the reason)
- all headers now have two preprocessor defines for the declaration
and implementation (for instance in sokol_gfx.h: SOKOL_GFX_INCLUDED
and SOKOL_GFX_IMPL_INCLUDED) these are checked in the utility-headers
to provide useful error message when dependent headers are missing
- **05-Mar-2019**: sokol_gfx.h now has a 'trace hook' API, and I have started
implementing optional debug-inspection-UI headers on top of Dear ImGui:
- sokol_gfx.h has a new function *sg_install_trace_hooks()*, this allows
you to install a callback function for each public sokol_gfx.h function
(and a couple of error callbacks). For more details, search for "TRACE HOOKS"
in sokol_gfx.h
- when creating sokol_gfx.h resources, you can now set a 'debug label'
in the desc structure, this is ignored by sokol_gfx.h itself, but is
useful for debuggers or profilers hooking in via the new trace hooks
- likewise, two new functions *sg_push_debug_group()* and *sg_pop_debug_group()*
can be used to group related drawing functions under a name, this
is also ignored by sokol_gfx.h itself and only useful when hooking
into the API calls
- I have started a new 'subproject' in the 'imgui' directory, this will
contain a slowly growing set of optional debug-inspection-UI headers
which allow to peek under the hood of the Sokol headers. The UIs are
implemented with [Dear ImGui](https://github.com/ocornut/imgui). Again,
see the README in the 'imgui' directory and the headers in there
for details, and check out the live demos on the [Sokol Sample Webpage](https://floooh.github.io/sokol-html5/)
(click on the little UI buttons in the top right corner of each thumbnail)
- **21-Feb-2019**: sokol_app.h and sokol_audio.h now have an alternative
set of callbacks with user_data arguments. This is useful if you don't
want or cannot store your own application state in global variables.
See the header documentation in sokol_app.h and sokol_audio.h for details,
and check out the samples *sapp/noentry-sapp.c* and *sapp/modplay-sapp.c*
in https://github.com/floooh/sokol-samples
- **19-Feb-2019**: sokol_app.h now has an alternative mode where it doesn't
"hijack" the platform's main() function. Search for SOKOL_NO_ENTRY in
sokol_app.h for details and documentation.
- **26-Jan-2019**: sokol_app.h now has an Android backend contributed by
[Gustav Olsson](https://github.com/gustavolsson)!
See the [sokol-samples readme](https://github.com/floooh/sokol-samples/blob/master/README.md)
for build instructions.
- **21-Jan-2019**: sokol_gfx.h - pool-slot-generation-counters and a dummy backend:
- Resource pool slots now have a generation-counter for the resource-id
unique-tag, instead of a single counter for the whole pool. This allows
to create many more unique handles.
- sokol_gfx.h now has a dummy backend, activated by defining SOKOL_DUMMY_BACKEND
(instead of SOKOL_METAL, SOKOL_D3D11, ...), this allows to write
'headless' tests (and there's now a sokol-gfx-test in the sokol-samples
repository which mainly tests the resource pool system)
- **12-Jan-2019**: sokol_gfx.h - setting the pipeline state and resource
bindings now happens in separate calls, specifically:
- *sg_apply_draw_state()* has been replaced with *sg_apply_pipeline()* and
*sg_apply_bindings()*
- the *sg_draw_state* struct has been replaced with *sg_bindings*
- *sg_apply_uniform_block()* has been renamed to *sg_apply_uniforms()*
All existing code will still work. See [this blog
post](https://floooh.github.io/2019/01/12/sokol-apply-pipeline.html) for
details.
- **29-Oct-2018**:
- sokol_gfx.h has a new function **sg_append_buffer()** which allows to
append new data to a buffer multiple times per frame and interleave this
with draw calls. This basically implements the
D3D11_MAP_WRITE_NO_OVERWRITE update strategy for buffer objects. For
example usage, see the updated Dear ImGui samples in the [sokol_samples
repo](https://github.com/floooh/sokol-samples)
- the GL state cache in sokol_gfx.h handles buffers bindings in a
more robust way, previously it might have happened that the
buffer binding gets confused when creating buffers or updating
buffer contents in the render loop
- **17-Oct-2018**: sokol_args.h added
- **26-Sep-2018**: sokol_audio.h ready for prime time :)
- **11-May-2018**: sokol_gfx.h now autodetects iOS vs MacOS in the Metal
backend during compilation using the standard define TARGET_OS_IPHONE defined
in the TargetConditionals.h system header, please replace the old
backend-selection defines SOKOL_METAL_MACOS and SOKOL_METAL_IOS with
**SOKOL_METAL**
- **20-Apr-2018**: 3 new context-switching functions have been added
to make it possible to use sokol together with applications that
use multiple GL contexts. On D3D11 and Metal, the functions are currently
empty. See the new section ```WORKING WITH CONTEXTS``` in the sokol_gfx.h
header documentation, and the new sample [multiwindow-glfw](https://github.com/floooh/sokol-samples/blob/master/glfw/multiwindow-glfw.c)
- **31-Jan-2018**: The vertex layout declaration in sg\_pipeline\_desc had
some fairly subtle flaws and has been changed to work like Metal or Vulkan.
The gist is that the vertex-buffer-layout properties (vertex stride,
vertex-step-rate and -step-function for instancing) is now defined in a
separate array from the vertex attributes. This removes some brittle backend
code which tries to guess the right vertex attribute slot if no attribute
names are given, and which was wrong for shader-code-generation pipelines
which reorder the vertex attributes (I stumbled over this when porting the
Oryol Gfx module over to sokol-gfx). Some code samples:
```c
// a complete vertex layout declaration with a single input buffer
// with two vertex attributes
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.buffers = {
[0] = {
.stride = 20,
.step_func = SG_VERTEXSTEP_PER_VERTEX,
.step_rate = 1
}
},
.attrs = {
[0] = {
.name = "pos",
.offset = 0,
.format = SG_VERTEXFORMAT_FLOAT3,
.buffer_index = 0
},
[1] = {
.name = "uv",
.offset = 12,
.format = SG_VERTEXFORMAT_FLOAT2,
.buffer_index = 0
}
}
},
...
});
// if the vertex layout has no gaps, we can get rid of the strides and offsets:
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.buffers = {
[0] = {
.step_func = SG_VERTEXSTEP_PER_VERTEX,
.step_rate=1
}
},
.attrs = {
[0] = {
.name = "pos",
.format = SG_VERTEXFORMAT_FLOAT3,
.buffer_index = 0
},
[1] = {
.name = "uv",
.format = SG_VERTEXFORMAT_FLOAT2,
.buffer_index = 0
}
}
},
...
});
// we can also get rid of the other default-values, which leaves buffers[0]
// as all-defaults, so it can disappear completely:
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[0] = { .name = "pos", .format = SG_VERTEXFORMAT_FLOAT3 },
[1] = { .name = "uv", .format = SG_VERTEXFORMAT_FLOAT2 }
}
},
...
});
// and finally on GL3.3 and Metal and we don't need the attribute names
// (on D3D11, a semantic name and index must be provided though)
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[0] = { .format = SG_VERTEXFORMAT_FLOAT3 },
[1] = { .format = SG_VERTEXFORMAT_FLOAT2 }
}
},
...
});
```
Please check the sample code in https://github.com/floooh/sokol-samples for
more examples!
Enjoy!
|
0 |
repos
|
repos/sokol/fips.yml
|
exports:
header-dirs: [ ".", "util" ]
|
0 |
repos
|
repos/sokol/sokol_time.h
|
#if defined(SOKOL_IMPL) && !defined(SOKOL_TIME_IMPL)
#define SOKOL_TIME_IMPL
#endif
#ifndef SOKOL_TIME_INCLUDED
/*
sokol_time.h -- simple cross-platform time measurement
Project URL: https://github.com/floooh/sokol
Do this:
#define SOKOL_IMPL or
#define SOKOL_TIME_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
Optionally provide the following defines with your own implementations:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_TIME_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_TIME_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
If sokol_time.h is compiled as a DLL, define the following before
including the declaration or implementation:
SOKOL_DLL
On Windows, SOKOL_DLL will define SOKOL_TIME_API_DECL as __declspec(dllexport)
or __declspec(dllimport) as needed.
void stm_setup();
Call once before any other functions to initialize sokol_time
(this calls for instance QueryPerformanceFrequency on Windows)
uint64_t stm_now();
Get current point in time in unspecified 'ticks'. The value that
is returned has no relation to the 'wall-clock' time and is
not in a specific time unit, it is only useful to compute
time differences.
uint64_t stm_diff(uint64_t new, uint64_t old);
Computes the time difference between new and old. This will always
return a positive, non-zero value.
uint64_t stm_since(uint64_t start);
Takes the current time, and returns the elapsed time since start
(this is a shortcut for "stm_diff(stm_now(), start)")
uint64_t stm_laptime(uint64_t* last_time);
This is useful for measuring frame time and other recurring
events. It takes the current time, returns the time difference
to the value in last_time, and stores the current time in
last_time for the next call. If the value in last_time is 0,
the return value will be zero (this usually happens on the
very first call).
uint64_t stm_round_to_common_refresh_rate(uint64_t duration)
This oddly named function takes a measured frame time and
returns the closest "nearby" common display refresh rate frame duration
in ticks. If the input duration isn't close to any common display
refresh rate, the input duration will be returned unchanged as a fallback.
The main purpose of this function is to remove jitter/inaccuracies from
measured frame times, and instead use the display refresh rate as
frame duration.
NOTE: for more robust frame timing, consider using the
sokol_app.h function sapp_frame_duration()
Use the following functions to convert a duration in ticks into
useful time units:
double stm_sec(uint64_t ticks);
double stm_ms(uint64_t ticks);
double stm_us(uint64_t ticks);
double stm_ns(uint64_t ticks);
Converts a tick value into seconds, milliseconds, microseconds
or nanoseconds. Note that not all platforms will have nanosecond
or even microsecond precision.
Uses the following time measurement functions under the hood:
Windows: QueryPerformanceFrequency() / QueryPerformanceCounter()
MacOS/iOS: mach_absolute_time()
emscripten: emscripten_get_now()
Linux+others: clock_gettime(CLOCK_MONOTONIC)
zlib/libpng license
Copyright (c) 2018 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define SOKOL_TIME_INCLUDED (1)
#include <stdint.h>
#if defined(SOKOL_API_DECL) && !defined(SOKOL_TIME_API_DECL)
#define SOKOL_TIME_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_TIME_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_TIME_IMPL)
#define SOKOL_TIME_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_TIME_API_DECL __declspec(dllimport)
#else
#define SOKOL_TIME_API_DECL extern
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
SOKOL_TIME_API_DECL void stm_setup(void);
SOKOL_TIME_API_DECL uint64_t stm_now(void);
SOKOL_TIME_API_DECL uint64_t stm_diff(uint64_t new_ticks, uint64_t old_ticks);
SOKOL_TIME_API_DECL uint64_t stm_since(uint64_t start_ticks);
SOKOL_TIME_API_DECL uint64_t stm_laptime(uint64_t* last_time);
SOKOL_TIME_API_DECL uint64_t stm_round_to_common_refresh_rate(uint64_t frame_ticks);
SOKOL_TIME_API_DECL double stm_sec(uint64_t ticks);
SOKOL_TIME_API_DECL double stm_ms(uint64_t ticks);
SOKOL_TIME_API_DECL double stm_us(uint64_t ticks);
SOKOL_TIME_API_DECL double stm_ns(uint64_t ticks);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif // SOKOL_TIME_INCLUDED
/*-- IMPLEMENTATION ----------------------------------------------------------*/
#ifdef SOKOL_TIME_IMPL
#define SOKOL_TIME_IMPL_INCLUDED (1)
#include <string.h> /* memset */
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
#ifndef SOKOL_ASSERT
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
#ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static
#else
#define _SOKOL_PRIVATE static
#endif
#endif
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
typedef struct {
uint32_t initialized;
LARGE_INTEGER freq;
LARGE_INTEGER start;
} _stm_state_t;
#elif defined(__APPLE__) && defined(__MACH__)
#include <mach/mach_time.h>
typedef struct {
uint32_t initialized;
mach_timebase_info_data_t timebase;
uint64_t start;
} _stm_state_t;
#elif defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h>
typedef struct {
uint32_t initialized;
double start;
} _stm_state_t;
#else /* anything else, this will need more care for non-Linux platforms */
#ifdef ESP8266
// On the ESP8266, clock_gettime ignores the first argument and CLOCK_MONOTONIC isn't defined
#define CLOCK_MONOTONIC 0
#endif
#include <time.h>
typedef struct {
uint32_t initialized;
uint64_t start;
} _stm_state_t;
#endif
static _stm_state_t _stm;
/* prevent 64-bit overflow when computing relative timestamp
see https://gist.github.com/jspohr/3dc4f00033d79ec5bdaf67bc46c813e3
*/
#if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__))
_SOKOL_PRIVATE int64_t _stm_int64_muldiv(int64_t value, int64_t numer, int64_t denom) {
int64_t q = value / denom;
int64_t r = value % denom;
return q * numer + r * numer / denom;
}
#endif
SOKOL_API_IMPL void stm_setup(void) {
memset(&_stm, 0, sizeof(_stm));
_stm.initialized = 0xABCDABCD;
#if defined(_WIN32)
QueryPerformanceFrequency(&_stm.freq);
QueryPerformanceCounter(&_stm.start);
#elif defined(__APPLE__) && defined(__MACH__)
mach_timebase_info(&_stm.timebase);
_stm.start = mach_absolute_time();
#elif defined(__EMSCRIPTEN__)
_stm.start = emscripten_get_now();
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
_stm.start = (uint64_t)ts.tv_sec*1000000000 + (uint64_t)ts.tv_nsec;
#endif
}
SOKOL_API_IMPL uint64_t stm_now(void) {
SOKOL_ASSERT(_stm.initialized == 0xABCDABCD);
uint64_t now;
#if defined(_WIN32)
LARGE_INTEGER qpc_t;
QueryPerformanceCounter(&qpc_t);
now = (uint64_t) _stm_int64_muldiv(qpc_t.QuadPart - _stm.start.QuadPart, 1000000000, _stm.freq.QuadPart);
#elif defined(__APPLE__) && defined(__MACH__)
const uint64_t mach_now = mach_absolute_time() - _stm.start;
now = (uint64_t) _stm_int64_muldiv((int64_t)mach_now, (int64_t)_stm.timebase.numer, (int64_t)_stm.timebase.denom);
#elif defined(__EMSCRIPTEN__)
double js_now = emscripten_get_now() - _stm.start;
now = (uint64_t) (js_now * 1000000.0);
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
now = ((uint64_t)ts.tv_sec*1000000000 + (uint64_t)ts.tv_nsec) - _stm.start;
#endif
return now;
}
SOKOL_API_IMPL uint64_t stm_diff(uint64_t new_ticks, uint64_t old_ticks) {
if (new_ticks > old_ticks) {
return new_ticks - old_ticks;
}
else {
return 1;
}
}
SOKOL_API_IMPL uint64_t stm_since(uint64_t start_ticks) {
return stm_diff(stm_now(), start_ticks);
}
SOKOL_API_IMPL uint64_t stm_laptime(uint64_t* last_time) {
SOKOL_ASSERT(last_time);
uint64_t dt = 0;
uint64_t now = stm_now();
if (0 != *last_time) {
dt = stm_diff(now, *last_time);
}
*last_time = now;
return dt;
}
// first number is frame duration in ns, second number is tolerance in ns,
// the resulting min/max values must not overlap!
static const uint64_t _stm_refresh_rates[][2] = {
{ 16666667, 1000000 }, // 60 Hz: 16.6667 +- 1ms
{ 13888889, 250000 }, // 72 Hz: 13.8889 +- 0.25ms
{ 13333333, 250000 }, // 75 Hz: 13.3333 +- 0.25ms
{ 11764706, 250000 }, // 85 Hz: 11.7647 +- 0.25
{ 11111111, 250000 }, // 90 Hz: 11.1111 +- 0.25ms
{ 10000000, 500000 }, // 100 Hz: 10.0000 +- 0.5ms
{ 8333333, 500000 }, // 120 Hz: 8.3333 +- 0.5ms
{ 6944445, 500000 }, // 144 Hz: 6.9445 +- 0.5ms
{ 4166667, 1000000 }, // 240 Hz: 4.1666 +- 1ms
{ 0, 0 }, // keep the last element always at zero
};
SOKOL_API_IMPL uint64_t stm_round_to_common_refresh_rate(uint64_t ticks) {
uint64_t ns;
int i = 0;
while (0 != (ns = _stm_refresh_rates[i][0])) {
uint64_t tol = _stm_refresh_rates[i][1];
if ((ticks > (ns - tol)) && (ticks < (ns + tol))) {
return ns;
}
i++;
}
// fallthrough: didn't fit into any buckets
return ticks;
}
SOKOL_API_IMPL double stm_sec(uint64_t ticks) {
return (double)ticks / 1000000000.0;
}
SOKOL_API_IMPL double stm_ms(uint64_t ticks) {
return (double)ticks / 1000000.0;
}
SOKOL_API_IMPL double stm_us(uint64_t ticks) {
return (double)ticks / 1000.0;
}
SOKOL_API_IMPL double stm_ns(uint64_t ticks) {
return (double)ticks;
}
#endif /* SOKOL_TIME_IMPL */
|
0 |
repos
|
repos/sokol/sokol_app.h
|
#if defined(SOKOL_IMPL) && !defined(SOKOL_APP_IMPL)
#define SOKOL_APP_IMPL
#endif
#ifndef SOKOL_APP_INCLUDED
/*
sokol_app.h -- cross-platform application wrapper
Project URL: https://github.com/floooh/sokol
Do this:
#define SOKOL_IMPL or
#define SOKOL_APP_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
In the same place define one of the following to select the 3D-API
which should be initialized by sokol_app.h (this must also match
the backend selected for sokol_gfx.h if both are used in the same
project):
#define SOKOL_GLCORE
#define SOKOL_GLES3
#define SOKOL_D3D11
#define SOKOL_METAL
#define SOKOL_WGPU
#define SOKOL_NOAPI
Optionally provide the following defines with your own implementations:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false))
SOKOL_WIN32_FORCE_MAIN - define this on Win32 to use a main() entry point instead of WinMain
SOKOL_NO_ENTRY - define this if sokol_app.h shouldn't "hijack" the main() function
SOKOL_APP_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_APP_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
Optionally define the following to force debug checks and validations
even in release mode:
SOKOL_DEBUG - by default this is defined if _DEBUG is defined
If sokol_app.h is compiled as a DLL, define the following before
including the declaration or implementation:
SOKOL_DLL
On Windows, SOKOL_DLL will define SOKOL_APP_API_DECL as __declspec(dllexport)
or __declspec(dllimport) as needed.
On Linux, SOKOL_GLCORE can use either GLX or EGL.
GLX is default, set SOKOL_FORCE_EGL to override.
For example code, see https://github.com/floooh/sokol-samples/tree/master/sapp
Portions of the Windows and Linux GL initialization, event-, icon- etc... code
have been taken from GLFW (http://www.glfw.org/).
iOS onscreen keyboard support 'inspired' by libgdx.
Link with the following system libraries:
- on macOS with Metal: Cocoa, QuartzCore, Metal, MetalKit
- on macOS with GL: Cocoa, QuartzCore, OpenGL
- on iOS with Metal: Foundation, UIKit, Metal, MetalKit
- on iOS with GL: Foundation, UIKit, OpenGLES, GLKit
- on Linux with EGL: X11, Xi, Xcursor, EGL, GL (or GLESv2), dl, pthread, m(?)
- on Linux with GLX: X11, Xi, Xcursor, GL, dl, pthread, m(?)
- on Android: GLESv3, EGL, log, android
- on Windows with the MSVC or Clang toolchains: no action needed, libs are defined in-source via pragma-comment-lib
- on Windows with MINGW/MSYS2 gcc: compile with '-mwin32' so that _WIN32 is defined
- link with the following libs: -lkernel32 -luser32 -lshell32
- additionally with the GL backend: -lgdi32
- additionally with the D3D11 backend: -ld3d11 -ldxgi
On Linux, you also need to use the -pthread compiler and linker option, otherwise weird
things will happen, see here for details: https://github.com/floooh/sokol/issues/376
On macOS and iOS, the implementation must be compiled as Objective-C.
FEATURE OVERVIEW
================
sokol_app.h provides a minimalistic cross-platform API which
implements the 'application-wrapper' parts of a 3D application:
- a common application entry function
- creates a window and 3D-API context/device with a 'default framebuffer'
- makes the rendered frame visible
- provides keyboard-, mouse- and low-level touch-events
- platforms: MacOS, iOS, HTML5, Win32, Linux/RaspberryPi, Android
- 3D-APIs: Metal, D3D11, GL4.1, GL4.3, GLES3, WebGL, WebGL2, NOAPI
FEATURE/PLATFORM MATRIX
=======================
| Windows | macOS | Linux | iOS | Android | HTML5
--------------------+---------+-------+-------+-------+---------+--------
gl 3.x | YES | YES | YES | --- | --- | ---
gles3/webgl2 | --- | --- | YES(2)| YES | YES | YES
metal | --- | YES | --- | YES | --- | ---
d3d11 | YES | --- | --- | --- | --- | ---
noapi | YES | TODO | TODO | --- | TODO | ---
KEY_DOWN | YES | YES | YES | SOME | TODO | YES
KEY_UP | YES | YES | YES | SOME | TODO | YES
CHAR | YES | YES | YES | YES | TODO | YES
MOUSE_DOWN | YES | YES | YES | --- | --- | YES
MOUSE_UP | YES | YES | YES | --- | --- | YES
MOUSE_SCROLL | YES | YES | YES | --- | --- | YES
MOUSE_MOVE | YES | YES | YES | --- | --- | YES
MOUSE_ENTER | YES | YES | YES | --- | --- | YES
MOUSE_LEAVE | YES | YES | YES | --- | --- | YES
TOUCHES_BEGAN | --- | --- | --- | YES | YES | YES
TOUCHES_MOVED | --- | --- | --- | YES | YES | YES
TOUCHES_ENDED | --- | --- | --- | YES | YES | YES
TOUCHES_CANCELLED | --- | --- | --- | YES | YES | YES
RESIZED | YES | YES | YES | YES | YES | YES
ICONIFIED | YES | YES | YES | --- | --- | ---
RESTORED | YES | YES | YES | --- | --- | ---
FOCUSED | YES | YES | YES | --- | --- | YES
UNFOCUSED | YES | YES | YES | --- | --- | YES
SUSPENDED | --- | --- | --- | YES | YES | TODO
RESUMED | --- | --- | --- | YES | YES | TODO
QUIT_REQUESTED | YES | YES | YES | --- | --- | YES
IME | TODO | TODO? | TODO | ??? | TODO | ???
key repeat flag | YES | YES | YES | --- | --- | YES
windowed | YES | YES | YES | --- | --- | YES
fullscreen | YES | YES | YES | YES | YES | ---
mouse hide | YES | YES | YES | --- | --- | YES
mouse lock | YES | YES | YES | --- | --- | YES
set cursor type | YES | YES | YES | --- | --- | YES
screen keyboard | --- | --- | --- | YES | TODO | YES
swap interval | YES | YES | YES | YES | TODO | YES
high-dpi | YES | YES | TODO | YES | YES | YES
clipboard | YES | YES | TODO | --- | --- | YES
MSAA | YES | YES | YES | YES | YES | YES
drag'n'drop | YES | YES | YES | --- | --- | YES
window icon | YES | YES(1)| YES | --- | --- | YES
(1) macOS has no regular window icons, instead the dock icon is changed
(2) supported with EGL only (not GLX)
STEP BY STEP
============
--- Add a sokol_main() function to your code which returns a sapp_desc structure
with initialization parameters and callback function pointers. This
function is called very early, usually at the start of the
platform's entry function (e.g. main or WinMain). You should do as
little as possible here, since the rest of your code might be called
from another thread (this depends on the platform):
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc) {
.width = 640,
.height = 480,
.init_cb = my_init_func,
.frame_cb = my_frame_func,
.cleanup_cb = my_cleanup_func,
.event_cb = my_event_func,
...
};
}
To get any logging output in case of errors you need to provide a log
callback. The easiest way is via sokol_log.h:
#include "sokol_log.h"
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc) {
...
.logger.func = slog_func,
};
}
There are many more setup parameters, but these are the most important.
For a complete list search for the sapp_desc structure declaration
below.
DO NOT call any sokol-app function from inside sokol_main(), since
sokol-app will not be initialized at this point.
The .width and .height parameters are the preferred size of the 3D
rendering canvas. The actual size may differ from this depending on
platform and other circumstances. Also the canvas size may change at
any time (for instance when the user resizes the application window,
or rotates the mobile device). You can just keep .width and .height
zero-initialized to open a default-sized window (what "default-size"
exactly means is platform-specific, but usually it's a size that covers
most of, but not all, of the display).
All provided function callbacks will be called from the same thread,
but this may be different from the thread where sokol_main() was called.
.init_cb (void (*)(void))
This function is called once after the application window,
3D rendering context and swap chain have been created. The
function takes no arguments and has no return value.
.frame_cb (void (*)(void))
This is the per-frame callback, which is usually called 60
times per second. This is where your application would update
most of its state and perform all rendering.
.cleanup_cb (void (*)(void))
The cleanup callback is called once right before the application
quits.
.event_cb (void (*)(const sapp_event* event))
The event callback is mainly for input handling, but is also
used to communicate other types of events to the application. Keep the
event_cb struct member zero-initialized if your application doesn't require
event handling.
As you can see, those 'standard callbacks' don't have a user_data
argument, so any data that needs to be preserved between callbacks
must live in global variables. If keeping state in global variables
is not an option, there's an alternative set of callbacks with
an additional user_data pointer argument:
.user_data (void*)
The user-data argument for the callbacks below
.init_userdata_cb (void (*)(void* user_data))
.frame_userdata_cb (void (*)(void* user_data))
.cleanup_userdata_cb (void (*)(void* user_data))
.event_userdata_cb (void(*)(const sapp_event* event, void* user_data))
The function sapp_userdata() can be used to query the user_data
pointer provided in the sapp_desc struct.
You can also call sapp_query_desc() to get a copy of the
original sapp_desc structure.
NOTE that there's also an alternative compile mode where sokol_app.h
doesn't "hijack" the main() function. Search below for SOKOL_NO_ENTRY.
--- Implement the initialization callback function (init_cb), this is called
once after the rendering surface, 3D API and swap chain have been
initialized by sokol_app. All sokol-app functions can be called
from inside the initialization callback, the most useful functions
at this point are:
int sapp_width(void)
int sapp_height(void)
Returns the current width and height of the default framebuffer in pixels,
this may change from one frame to the next, and it may be different
from the initial size provided in the sapp_desc struct.
float sapp_widthf(void)
float sapp_heightf(void)
These are alternatives to sapp_width() and sapp_height() which return
the default framebuffer size as float values instead of integer. This
may help to prevent casting back and forth between int and float
in more strongly typed languages than C and C++.
double sapp_frame_duration(void)
Returns the frame duration in seconds averaged over a number of
frames to smooth out any jittering spikes.
int sapp_color_format(void)
int sapp_depth_format(void)
The color and depth-stencil pixelformats of the default framebuffer,
as integer values which are compatible with sokol-gfx's
sg_pixel_format enum (so that they can be plugged directly in places
where sg_pixel_format is expected). Possible values are:
23 == SG_PIXELFORMAT_RGBA8
28 == SG_PIXELFORMAT_BGRA8
42 == SG_PIXELFORMAT_DEPTH
43 == SG_PIXELFORMAT_DEPTH_STENCIL
int sapp_sample_count(void)
Return the MSAA sample count of the default framebuffer.
const void* sapp_metal_get_device(void)
const void* sapp_metal_get_current_drawable(void)
const void* sapp_metal_get_depth_stencil_texture(void)
const void* sapp_metal_get_msaa_color_texture(void)
If the Metal backend has been selected, these functions return pointers
to various Metal API objects required for rendering, otherwise
they return a null pointer. These void pointers are actually
Objective-C ids converted with a (ARC) __bridge cast so that
the ids can be tunnel through C code. Also note that the returned
pointers to the renderpass-descriptor and drawable may change from one
frame to the next, only the Metal device object is guaranteed to
stay the same.
const void* sapp_macos_get_window(void)
On macOS, get the NSWindow object pointer, otherwise a null pointer.
Before being used as Objective-C object, the void* must be converted
back with a (ARC) __bridge cast.
const void* sapp_ios_get_window(void)
On iOS, get the UIWindow object pointer, otherwise a null pointer.
Before being used as Objective-C object, the void* must be converted
back with a (ARC) __bridge cast.
const void* sapp_d3d11_get_device(void)
const void* sapp_d3d11_get_device_context(void)
const void* sapp_d3d11_get_render_view(void)
const void* sapp_d3d11_get_resolve_view(void);
const void* sapp_d3d11_get_depth_stencil_view(void)
Similar to the sapp_metal_* functions, the sapp_d3d11_* functions
return pointers to D3D11 API objects required for rendering,
only if the D3D11 backend has been selected. Otherwise they
return a null pointer. Note that the returned pointers to the
render-target-view and depth-stencil-view may change from one
frame to the next!
const void* sapp_win32_get_hwnd(void)
On Windows, get the window's HWND, otherwise a null pointer. The
HWND has been cast to a void pointer in order to be tunneled
through code which doesn't include Windows.h.
const void* sapp_wgpu_get_device(void)
const void* sapp_wgpu_get_render_view(void)
const void* sapp_wgpu_get_resolve_view(void)
const void* sapp_wgpu_get_depth_stencil_view(void)
These are the WebGPU-specific functions to get the WebGPU
objects and values required for rendering. If sokol_app.h
is not compiled with SOKOL_WGPU, these functions return null.
uint32_t sapp_gl_get_framebuffer(void)
This returns the 'default framebuffer' of the GL context.
Typically this will be zero.
int sapp_gl_get_major_version(void)
int sapp_gl_get_minor_version(void)
Returns the major and minor version of the GL context
(only for SOKOL_GLCORE, all other backends return zero here, including SOKOL_GLES3)
const void* sapp_android_get_native_activity(void);
On Android, get the native activity ANativeActivity pointer, otherwise
a null pointer.
--- Implement the frame-callback function, this function will be called
on the same thread as the init callback, but might be on a different
thread than the sokol_main() function. Note that the size of
the rendering framebuffer might have changed since the frame callback
was called last. Call the functions sapp_width() and sapp_height()
each frame to get the current size.
--- Optionally implement the event-callback to handle input events.
sokol-app provides the following type of input events:
- a 'virtual key' was pressed down or released
- a single text character was entered (provided as UTF-32 code point)
- a mouse button was pressed down or released (left, right, middle)
- mouse-wheel or 2D scrolling events
- the mouse was moved
- the mouse has entered or left the application window boundaries
- low-level, portable multi-touch events (began, moved, ended, cancelled)
- the application window was resized, iconified or restored
- the application was suspended or restored (on mobile platforms)
- the user or application code has asked to quit the application
- a string was pasted to the system clipboard
- one or more files have been dropped onto the application window
To explicitly 'consume' an event and prevent that the event is
forwarded for further handling to the operating system, call
sapp_consume_event() from inside the event handler (NOTE that
this behaviour is currently only implemented for some HTML5
events, support for other platforms and event types will
be added as needed, please open a GitHub ticket and/or provide
a PR if needed).
NOTE: Do *not* call any 3D API rendering functions in the event
callback function, since the 3D API context may not be active when the
event callback is called (it may work on some platforms and 3D APIs,
but not others, and the exact behaviour may change between
sokol-app versions).
--- Implement the cleanup-callback function, this is called once
after the user quits the application (see the section
"APPLICATION QUIT" for detailed information on quitting
behaviour, and how to intercept a pending quit - for instance to show a
"Really Quit?" dialog box). Note that the cleanup-callback isn't
guaranteed to be called on the web and mobile platforms.
MOUSE CURSOR TYPE AND VISIBILITY
================================
You can show and hide the mouse cursor with
void sapp_show_mouse(bool show)
And to get the current shown status:
bool sapp_mouse_shown(void)
NOTE that hiding the mouse cursor is different and independent from
the MOUSE/POINTER LOCK feature which will also hide the mouse pointer when
active (MOUSE LOCK is described below).
To change the mouse cursor to one of several predefined types, call
the function:
void sapp_set_mouse_cursor(sapp_mouse_cursor cursor)
Setting the default mouse cursor SAPP_MOUSECURSOR_DEFAULT will restore
the standard look.
To get the currently active mouse cursor type, call:
sapp_mouse_cursor sapp_get_mouse_cursor(void)
MOUSE LOCK (AKA POINTER LOCK, AKA MOUSE CAPTURE)
================================================
In normal mouse mode, no mouse movement events are reported when the
mouse leaves the windows client area or hits the screen border (whether
it's one or the other depends on the platform), and the mouse move events
(SAPP_EVENTTYPE_MOUSE_MOVE) contain absolute mouse positions in
framebuffer pixels in the sapp_event items mouse_x and mouse_y, and
relative movement in framebuffer pixels in the sapp_event items mouse_dx
and mouse_dy.
To get continuous mouse movement (also when the mouse leaves the window
client area or hits the screen border), activate mouse-lock mode
by calling:
sapp_lock_mouse(true)
When mouse lock is activated, the mouse pointer is hidden, the
reported absolute mouse position (sapp_event.mouse_x/y) appears
frozen, and the relative mouse movement in sapp_event.mouse_dx/dy
no longer has a direct relation to framebuffer pixels but instead
uses "raw mouse input" (what "raw mouse input" exactly means also
differs by platform).
To deactivate mouse lock and return to normal mouse mode, call
sapp_lock_mouse(false)
And finally, to check if mouse lock is currently active, call
if (sapp_mouse_locked()) { ... }
On native platforms, the sapp_lock_mouse() and sapp_mouse_locked()
functions work as expected (mouse lock is activated or deactivated
immediately when sapp_lock_mouse() is called, and sapp_mouse_locked()
also immediately returns the new state after sapp_lock_mouse()
is called.
On the web platform, sapp_lock_mouse() and sapp_mouse_locked() behave
differently, as dictated by the limitations of the HTML5 Pointer Lock API:
- sapp_lock_mouse(true) can be called at any time, but it will
only take effect in a 'short-lived input event handler of a specific
type', meaning when one of the following events happens:
- SAPP_EVENTTYPE_MOUSE_DOWN
- SAPP_EVENTTYPE_MOUSE_UP
- SAPP_EVENTTYPE_MOUSE_SCROLL
- SAPP_EVENTTYPE_KEY_UP
- SAPP_EVENTTYPE_KEY_DOWN
- The mouse lock/unlock action on the web platform is asynchronous,
this means that sapp_mouse_locked() won't immediately return
the new status after calling sapp_lock_mouse(), instead the
reported status will only change when the pointer lock has actually
been activated or deactivated in the browser.
- On the web, mouse lock can be deactivated by the user at any time
by pressing the Esc key. When this happens, sokol_app.h behaves
the same as if sapp_lock_mouse(false) is called.
For things like camera manipulation it's most straightforward to lock
and unlock the mouse right from the sokol_app.h event handler, for
instance the following code enters and leaves mouse lock when the
left mouse button is pressed and released, and then uses the relative
movement information to manipulate a camera (taken from the
cgltf-sapp.c sample in the sokol-samples repository
at https://github.com/floooh/sokol-samples):
static void input(const sapp_event* ev) {
switch (ev->type) {
case SAPP_EVENTTYPE_MOUSE_DOWN:
if (ev->mouse_button == SAPP_MOUSEBUTTON_LEFT) {
sapp_lock_mouse(true);
}
break;
case SAPP_EVENTTYPE_MOUSE_UP:
if (ev->mouse_button == SAPP_MOUSEBUTTON_LEFT) {
sapp_lock_mouse(false);
}
break;
case SAPP_EVENTTYPE_MOUSE_MOVE:
if (sapp_mouse_locked()) {
cam_orbit(&state.camera, ev->mouse_dx * 0.25f, ev->mouse_dy * 0.25f);
}
break;
default:
break;
}
}
CLIPBOARD SUPPORT
=================
Applications can send and receive UTF-8 encoded text data from and to the
system clipboard. By default, clipboard support is disabled and
must be enabled at startup via the following sapp_desc struct
members:
sapp_desc.enable_clipboard - set to true to enable clipboard support
sapp_desc.clipboard_size - size of the internal clipboard buffer in bytes
Enabling the clipboard will dynamically allocate a clipboard buffer
for UTF-8 encoded text data of the requested size in bytes, the default
size is 8 KBytes. Strings that don't fit into the clipboard buffer
(including the terminating zero) will be silently clipped, so it's
important that you provide a big enough clipboard size for your
use case.
To send data to the clipboard, call sapp_set_clipboard_string() with
a pointer to an UTF-8 encoded, null-terminated C-string.
NOTE that on the HTML5 platform, sapp_set_clipboard_string() must be
called from inside a 'short-lived event handler', and there are a few
other HTML5-specific caveats to workaround. You'll basically have to
tinker until it works in all browsers :/ (maybe the situation will
improve when all browsers agree on and implement the new
HTML5 navigator.clipboard API).
To get data from the clipboard, check for the SAPP_EVENTTYPE_CLIPBOARD_PASTED
event in your event handler function, and then call sapp_get_clipboard_string()
to obtain the pasted UTF-8 encoded text.
NOTE that behaviour of sapp_get_clipboard_string() is slightly different
depending on platform:
- on the HTML5 platform, the internal clipboard buffer will only be updated
right before the SAPP_EVENTTYPE_CLIPBOARD_PASTED event is sent,
and sapp_get_clipboard_string() will simply return the current content
of the clipboard buffer
- on 'native' platforms, the call to sapp_get_clipboard_string() will
update the internal clipboard buffer with the most recent data
from the system clipboard
Portable code should check for the SAPP_EVENTTYPE_CLIPBOARD_PASTED event,
and then call sapp_get_clipboard_string() right in the event handler.
The SAPP_EVENTTYPE_CLIPBOARD_PASTED event will be generated by sokol-app
as follows:
- on macOS: when the Cmd+V key is pressed down
- on HTML5: when the browser sends a 'paste' event to the global 'window' object
- on all other platforms: when the Ctrl+V key is pressed down
DRAG AND DROP SUPPORT
=====================
PLEASE NOTE: the drag'n'drop feature works differently on WASM/HTML5
and on the native desktop platforms (Win32, Linux and macOS) because
of security-related restrictions in the HTML5 drag'n'drop API. The
WASM/HTML5 specifics are described at the end of this documentation
section:
Like clipboard support, drag'n'drop support must be explicitly enabled
at startup in the sapp_desc struct.
sapp_desc sokol_main(void) {
return (sapp_desc) {
.enable_dragndrop = true, // default is false
...
};
}
You can also adjust the maximum number of files that are accepted
in a drop operation, and the maximum path length in bytes if needed:
sapp_desc sokol_main(void) {
return (sapp_desc) {
.enable_dragndrop = true, // default is false
.max_dropped_files = 8, // default is 1
.max_dropped_file_path_length = 8192, // in bytes, default is 2048
...
};
}
When drag'n'drop is enabled, the event callback will be invoked with an
event of type SAPP_EVENTTYPE_FILES_DROPPED whenever the user drops files on
the application window.
After the SAPP_EVENTTYPE_FILES_DROPPED is received, you can query the
number of dropped files, and their absolute paths by calling separate
functions:
void on_event(const sapp_event* ev) {
if (ev->type == SAPP_EVENTTYPE_FILES_DROPPED) {
// the mouse position where the drop happened
float x = ev->mouse_x;
float y = ev->mouse_y;
// get the number of files and their paths like this:
const int num_dropped_files = sapp_get_num_dropped_files();
for (int i = 0; i < num_dropped_files; i++) {
const char* path = sapp_get_dropped_file_path(i);
...
}
}
}
The returned file paths are UTF-8 encoded strings.
You can call sapp_get_num_dropped_files() and sapp_get_dropped_file_path()
anywhere, also outside the event handler callback, but be aware that the
file path strings will be overwritten with the next drop operation.
In any case, sapp_get_dropped_file_path() will never return a null pointer,
instead an empty string "" will be returned if the drag'n'drop feature
hasn't been enabled, the last drop-operation failed, or the file path index
is out of range.
Drag'n'drop caveats:
- if more files are dropped in a single drop-action
than sapp_desc.max_dropped_files, the additional
files will be silently ignored
- if any of the file paths is longer than
sapp_desc.max_dropped_file_path_length (in number of bytes, after UTF-8
encoding) the entire drop operation will be silently ignored (this
needs some sort of error feedback in the future)
- no mouse positions are reported while the drag is in
process, this may change in the future
Drag'n'drop on HTML5/WASM:
The HTML5 drag'n'drop API doesn't return file paths, but instead
black-box 'file objects' which must be used to load the content
of dropped files. This is the reason why sokol_app.h adds two
HTML5-specific functions to the drag'n'drop API:
uint32_t sapp_html5_get_dropped_file_size(int index)
Returns the size in bytes of a dropped file.
void sapp_html5_fetch_dropped_file(const sapp_html5_fetch_request* request)
Asynchronously loads the content of a dropped file into a
provided memory buffer (which must be big enough to hold
the file content)
To start loading the first dropped file after an SAPP_EVENTTYPE_FILES_DROPPED
event is received:
sapp_html5_fetch_dropped_file(&(sapp_html5_fetch_request){
.dropped_file_index = 0,
.callback = fetch_cb
.buffer = {
.ptr = buf,
.size = sizeof(buf)
},
.user_data = ...
});
Make sure that the memory pointed to by 'buf' stays valid until the
callback function is called!
As result of the asynchronous loading operation (no matter if succeeded or
failed) the 'fetch_cb' function will be called:
void fetch_cb(const sapp_html5_fetch_response* response) {
// IMPORTANT: check if the loading operation actually succeeded:
if (response->succeeded) {
// the size of the loaded file:
const size_t num_bytes = response->data.size;
// and the pointer to the data (same as 'buf' in the fetch-call):
const void* ptr = response->data.ptr;
}
else {
// on error check the error code:
switch (response->error_code) {
case SAPP_HTML5_FETCH_ERROR_BUFFER_TOO_SMALL:
...
break;
case SAPP_HTML5_FETCH_ERROR_OTHER:
...
break;
}
}
}
Check the droptest-sapp example for a real-world example which works
both on native platforms and the web:
https://github.com/floooh/sokol-samples/blob/master/sapp/droptest-sapp.c
HIGH-DPI RENDERING
==================
You can set the sapp_desc.high_dpi flag during initialization to request
a full-resolution framebuffer on HighDPI displays. The default behaviour
is sapp_desc.high_dpi=false, this means that the application will
render to a lower-resolution framebuffer on HighDPI displays and the
rendered content will be upscaled by the window system composer.
In a HighDPI scenario, you still request the same window size during
sokol_main(), but the framebuffer sizes returned by sapp_width()
and sapp_height() will be scaled up according to the DPI scaling
ratio.
Note that on some platforms the DPI scaling factor may change at any
time (for instance when a window is moved from a high-dpi display
to a low-dpi display).
To query the current DPI scaling factor, call the function:
float sapp_dpi_scale(void);
For instance on a Retina Mac, returning the following sapp_desc
struct from sokol_main():
sapp_desc sokol_main(void) {
return (sapp_desc) {
.width = 640,
.height = 480,
.high_dpi = true,
...
};
}
...the functions the functions sapp_width(), sapp_height()
and sapp_dpi_scale() will return the following values:
sapp_width: 1280
sapp_height: 960
sapp_dpi_scale: 2.0
If the high_dpi flag is false, or you're not running on a Retina display,
the values would be:
sapp_width: 640
sapp_height: 480
sapp_dpi_scale: 1.0
If the window is moved from the Retina display to a low-dpi external display,
the values would change as follows:
sapp_width: 1280 => 640
sapp_height: 960 => 480
sapp_dpi_scale: 2.0 => 1.0
Currently there is no event associated with a DPI change, but an
SAPP_EVENTTYPE_RESIZED will be sent as a side effect of the
framebuffer size changing.
Per-monitor DPI is currently supported on macOS and Windows.
APPLICATION QUIT
================
Without special quit handling, a sokol_app.h application will quit
'gracefully' when the user clicks the window close-button unless a
platform's application model prevents this (e.g. on web or mobile).
'Graceful exit' means that the application-provided cleanup callback will
be called before the application quits.
On native desktop platforms sokol_app.h provides more control over the
application-quit-process. It's possible to initiate a 'programmatic quit'
from the application code, and a quit initiated by the application user can
be intercepted (for instance to show a custom dialog box).
This 'programmatic quit protocol' is implemented through 3 functions
and 1 event:
- sapp_quit(): This function simply quits the application without
giving the user a chance to intervene. Usually this might
be called when the user clicks the 'Ok' button in a 'Really Quit?'
dialog box
- sapp_request_quit(): Calling sapp_request_quit() will send the
event SAPP_EVENTTYPE_QUIT_REQUESTED to the applications event handler
callback, giving the user code a chance to intervene and cancel the
pending quit process (for instance to show a 'Really Quit?' dialog
box). If the event handler callback does nothing, the application
will be quit as usual. To prevent this, call the function
sapp_cancel_quit() from inside the event handler.
- sapp_cancel_quit(): Cancels a pending quit request, either initiated
by the user clicking the window close button, or programmatically
by calling sapp_request_quit(). The only place where calling this
function makes sense is from inside the event handler callback when
the SAPP_EVENTTYPE_QUIT_REQUESTED event has been received.
- SAPP_EVENTTYPE_QUIT_REQUESTED: this event is sent when the user
clicks the window's close button or application code calls the
sapp_request_quit() function. The event handler callback code can handle
this event by calling sapp_cancel_quit() to cancel the quit.
If the event is ignored, the application will quit as usual.
On the web platform, the quit behaviour differs from native platforms,
because of web-specific restrictions:
A `programmatic quit` initiated by calling sapp_quit() or
sapp_request_quit() will work as described above: the cleanup callback is
called, platform-specific cleanup is performed (on the web
this means that JS event handlers are unregistered), and then
the request-animation-loop will be exited. However that's all. The
web page itself will continue to exist (e.g. it's not possible to
programmatically close the browser tab).
On the web it's also not possible to run custom code when the user
closes a browser tab, so it's not possible to prevent this with a
fancy custom dialog box.
Instead the standard "Leave Site?" dialog box can be activated (or
deactivated) with the following function:
sapp_html5_ask_leave_site(bool ask);
The initial state of the associated internal flag can be provided
at startup via sapp_desc.html5_ask_leave_site.
This feature should only be used sparingly in critical situations - for
instance when the user would loose data - since popping up modal dialog
boxes is considered quite rude in the web world. Note that there's no way
to customize the content of this dialog box or run any code as a result
of the user's decision. Also note that the user must have interacted with
the site before the dialog box will appear. These are all security measures
to prevent fishing.
The Dear ImGui HighDPI sample contains example code of how to
implement a 'Really Quit?' dialog box with Dear ImGui (native desktop
platforms only), and for showing the hardwired "Leave Site?" dialog box
when running on the web platform:
https://floooh.github.io/sokol-html5/wasm/imgui-highdpi-sapp.html
FULLSCREEN
==========
If the sapp_desc.fullscreen flag is true, sokol-app will try to create
a fullscreen window on platforms with a 'proper' window system
(mobile devices will always use fullscreen). The implementation details
depend on the target platform, in general sokol-app will use a
'soft approach' which doesn't interfere too much with the platform's
window system (for instance borderless fullscreen window instead of
a 'real' fullscreen mode). Such details might change over time
as sokol-app is adapted for different needs.
The most important effect of fullscreen mode to keep in mind is that
the requested canvas width and height will be ignored for the initial
window size, calling sapp_width() and sapp_height() will instead return
the resolution of the fullscreen canvas (however the provided size
might still be used for the non-fullscreen window, in case the user can
switch back from fullscreen- to windowed-mode).
To toggle fullscreen mode programmatically, call sapp_toggle_fullscreen().
To check if the application window is currently in fullscreen mode,
call sapp_is_fullscreen().
WINDOW ICON SUPPORT
===================
Some sokol_app.h backends allow to change the window icon programmatically:
- on Win32: the small icon in the window's title bar, and the
bigger icon in the task bar
- on Linux: highly dependent on the used window manager, but usually
the window's title bar icon and/or the task bar icon
- on HTML5: the favicon shown in the page's browser tab
NOTE that it is not possible to set the actual application icon which is
displayed by the operating system on the desktop or 'home screen'. Those
icons must be provided 'traditionally' through operating-system-specific
resources which are associated with the application (sokol_app.h might
later support setting the window icon from platform specific resource data
though).
There are two ways to set the window icon:
- at application start in the sokol_main() function by initializing
the sapp_desc.icon nested struct
- or later by calling the function sapp_set_icon()
As a convenient shortcut, sokol_app.h comes with a builtin default-icon
(a rainbow-colored 'S', which at least looks a bit better than the Windows
default icon for applications), which can be activated like this:
At startup in sokol_main():
sapp_desc sokol_main(...) {
return (sapp_desc){
...
icon.sokol_default = true
};
}
Or later by calling:
sapp_set_icon(&(sapp_icon_desc){ .sokol_default = true });
NOTE that a completely zero-initialized sapp_icon_desc struct will not
update the window icon in any way. This is an 'escape hatch' so that you
can handle the window icon update yourself (or if you do this already,
sokol_app.h won't get in your way, in this case just leave the
sapp_desc.icon struct zero-initialized).
Providing your own icon images works exactly like in GLFW (down to the
data format):
You provide one or more 'candidate images' in different sizes, and the
sokol_app.h platform backends pick the best match for the specific backend
and icon type.
For each candidate image, you need to provide:
- the width in pixels
- the height in pixels
- and the actual pixel data in RGBA8 pixel format (e.g. 0xFFCC8844
on a little-endian CPU means: alpha=0xFF, blue=0xCC, green=0x88, red=0x44)
For instance, if you have 3 candidate images (small, medium, big) of
sizes 16x16, 32x32 and 64x64 the corresponding sapp_icon_desc struct is setup
like this:
// the actual pixel data (RGBA8, origin top-left)
const uint32_t small[16][16] = { ... };
const uint32_t medium[32][32] = { ... };
const uint32_t big[64][64] = { ... };
const sapp_icon_desc icon_desc = {
.images = {
{ .width = 16, .height = 16, .pixels = SAPP_RANGE(small) },
{ .width = 32, .height = 32, .pixels = SAPP_RANGE(medium) },
// ...or without the SAPP_RANGE helper macro:
{ .width = 64, .height = 64, .pixels = { .ptr=big, .size=sizeof(big) } }
}
};
An sapp_icon_desc struct initialized like this can then either be applied
at application start in sokol_main:
sapp_desc sokol_main(...) {
return (sapp_desc){
...
icon = icon_desc
};
}
...or later by calling sapp_set_icon():
sapp_set_icon(&icon_desc);
Some window icon caveats:
- once the window icon has been updated, there's no way to go back to
the platform's default icon, this is because some platforms (Linux
and HTML5) don't switch the icon visual back to the default even if
the custom icon is deleted or removed
- on HTML5, if the sokol_app.h icon doesn't show up in the browser
tab, check that there's no traditional favicon 'link' element
is defined in the page's index.html, sokol_app.h will only
append a new favicon link element, but not delete any manually
defined favicon in the page
For an example and test of the window icon feature, check out the
'icon-sapp' sample on the sokol-samples git repository.
ONSCREEN KEYBOARD
=================
On some platforms which don't provide a physical keyboard, sokol-app
can display the platform's integrated onscreen keyboard for text
input. To request that the onscreen keyboard is shown, call
sapp_show_keyboard(true);
Likewise, to hide the keyboard call:
sapp_show_keyboard(false);
Note that onscreen keyboard functionality is no longer supported
on the browser platform (the previous hacks and workarounds to make browser
keyboards work for on web applications that don't use HTML UIs
never really worked across browsers).
INPUT EVENT BUBBLING ON THE WEB PLATFORM
========================================
By default, input event bubbling on the web platform is configured in
a way that makes the most sense for 'full-canvas' apps that cover the
entire browser client window area:
- mouse, touch and wheel events do not bubble up, this prevents various
ugly side events, like:
- HTML text overlays being selected on double- or triple-click into
the canvas
- 'scroll bumping' even when the canvas covers the entire client area
- key_up/down events for 'character keys' *do* bubble up (otherwise
the browser will not generate UNICODE character events)
- all other key events *do not* bubble up by default (this prevents side effects
like F1 opening help, or F7 starting 'caret browsing')
- character events do no bubble up (although I haven't noticed any side effects
otherwise)
Event bubbling can be enabled for input event categories during initialization
in the sapp_desc struct:
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc){
//...
.html5_bubble_mouse_events = true,
.html5_bubble_touch_events = true,
.html5_bubble_wheel_events = true,
.html5_bubble_key_events = true,
.html5_bubble_char_events = true,
};
}
This basically opens the floodgates lets *all* input events bubble up to the browser.
To prevent individual events from bubbling, call sapp_consume_event() from within
the sokol_app.h event callback.
OPTIONAL: DON'T HIJACK main() (#define SOKOL_NO_ENTRY)
======================================================
NOTE: SOKOL_NO_ENTRY and sapp_run() is currently not supported on Android.
In its default configuration, sokol_app.h "hijacks" the platform's
standard main() function. This was done because different platforms
have different entry point conventions which are not compatible with
C's main() (for instance WinMain on Windows has completely different
arguments). However, this "main hijacking" posed a problem for
usage scenarios like integrating sokol_app.h with other languages than
C or C++, so an alternative SOKOL_NO_ENTRY mode has been added
in which the user code provides the platform's main function:
- define SOKOL_NO_ENTRY before including the sokol_app.h implementation
- do *not* provide a sokol_main() function
- instead provide the standard main() function of the platform
- from the main function, call the function ```sapp_run()``` which
takes a pointer to an ```sapp_desc``` structure.
- from here on```sapp_run()``` takes over control and calls the provided
init-, frame-, event- and cleanup-callbacks just like in the default model.
sapp_run() behaves differently across platforms:
- on some platforms, sapp_run() will return when the application quits
- on other platforms, sapp_run() will never return, even when the
application quits (the operating system is free to simply terminate
the application at any time)
- on Emscripten specifically, sapp_run() will return immediately while
the frame callback keeps being called
This different behaviour of sapp_run() essentially means that there shouldn't
be any code *after* sapp_run(), because that may either never be called, or in
case of Emscripten will be called at an unexpected time (at application start).
An application also should not depend on the cleanup-callback being called
when cross-platform compatibility is required.
Since sapp_run() returns immediately on Emscripten you shouldn't activate
the 'EXIT_RUNTIME' linker option (this is disabled by default when compiling
for the browser target), since the C/C++ exit runtime would be called immediately at
application start, causing any global objects to be destroyed and global
variables to be zeroed.
WINDOWS CONSOLE OUTPUT
======================
On Windows, regular windowed applications don't show any stdout/stderr text
output, which can be a bit of a hassle for printf() debugging or generally
logging text to the console. Also, console output by default uses a local
codepage setting and thus international UTF-8 encoded text is printed
as garbage.
To help with these issues, sokol_app.h can be configured at startup
via the following Windows-specific sapp_desc flags:
sapp_desc.win32_console_utf8 (default: false)
When set to true, the output console codepage will be switched
to UTF-8 (and restored to the original codepage on exit)
sapp_desc.win32_console_attach (default: false)
When set to true, stdout and stderr will be attached to the
console of the parent process (if the parent process actually
has a console). This means that if the application was started
in a command line window, stdout and stderr output will be printed
to the terminal, just like a regular command line program. But if
the application is started via double-click, it will behave like
a regular UI application, and stdout/stderr will not be visible.
sapp_desc.win32_console_create (default: false)
When set to true, a new console window will be created and
stdout/stderr will be redirected to that console window. It
doesn't matter if the application is started from the command
line or via double-click.
MEMORY ALLOCATION OVERRIDE
==========================
You can override the memory allocation functions at initialization time
like this:
void* my_alloc(size_t size, void* user_data) {
return malloc(size);
}
void my_free(void* ptr, void* user_data) {
free(ptr);
}
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc){
// ...
.allocator = {
.alloc_fn = my_alloc,
.free_fn = my_free,
.user_data = ...,
}
};
}
If no overrides are provided, malloc and free will be used.
This only affects memory allocation calls done by sokol_app.h
itself though, not any allocations in OS libraries.
ERROR REPORTING AND LOGGING
===========================
To get any logging information at all you need to provide a logging callback in the setup call
the easiest way is to use sokol_log.h:
#include "sokol_log.h"
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc) {
...
.logger.func = slog_func,
};
}
To override logging with your own callback, first write a logging function like this:
void my_log(const char* tag, // e.g. 'sapp'
uint32_t log_level, // 0=panic, 1=error, 2=warn, 3=info
uint32_t log_item_id, // SAPP_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_app.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data)
{
...
}
...and then setup sokol-app like this:
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc) {
...
.logger = {
.func = my_log,
.user_data = my_user_data,
}
};
}
The provided logging function must be reentrant (e.g. be callable from
different threads).
If you don't want to provide your own custom logger it is highly recommended to use
the standard logger in sokol_log.h instead, otherwise you won't see any warnings or
errors.
TEMP NOTE DUMP
==============
- sapp_desc needs a bool whether to initialize depth-stencil surface
- the Android implementation calls cleanup_cb() and destroys the egl context in onDestroy
at the latest but should do it earlier, in onStop, as an app is "killable" after onStop
on Android Honeycomb and later (it can't be done at the moment as the app may be started
again after onStop and the sokol lifecycle does not yet handle context teardown/bringup)
LICENSE
=======
zlib/libpng license
Copyright (c) 2018 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define SOKOL_APP_INCLUDED (1)
#include <stddef.h> // size_t
#include <stdint.h>
#include <stdbool.h>
#if defined(SOKOL_API_DECL) && !defined(SOKOL_APP_API_DECL)
#define SOKOL_APP_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_APP_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_APP_IMPL)
#define SOKOL_APP_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_APP_API_DECL __declspec(dllimport)
#else
#define SOKOL_APP_API_DECL extern
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* misc constants */
enum {
SAPP_MAX_TOUCHPOINTS = 8,
SAPP_MAX_MOUSEBUTTONS = 3,
SAPP_MAX_KEYCODES = 512,
SAPP_MAX_ICONIMAGES = 8,
};
/*
sapp_event_type
The type of event that's passed to the event handler callback
in the sapp_event.type field. These are not just "traditional"
input events, but also notify the application about state changes
or other user-invoked actions.
*/
typedef enum sapp_event_type {
SAPP_EVENTTYPE_INVALID,
SAPP_EVENTTYPE_KEY_DOWN,
SAPP_EVENTTYPE_KEY_UP,
SAPP_EVENTTYPE_CHAR,
SAPP_EVENTTYPE_MOUSE_DOWN,
SAPP_EVENTTYPE_MOUSE_UP,
SAPP_EVENTTYPE_MOUSE_SCROLL,
SAPP_EVENTTYPE_MOUSE_MOVE,
SAPP_EVENTTYPE_MOUSE_ENTER,
SAPP_EVENTTYPE_MOUSE_LEAVE,
SAPP_EVENTTYPE_TOUCHES_BEGAN,
SAPP_EVENTTYPE_TOUCHES_MOVED,
SAPP_EVENTTYPE_TOUCHES_ENDED,
SAPP_EVENTTYPE_TOUCHES_CANCELLED,
SAPP_EVENTTYPE_RESIZED,
SAPP_EVENTTYPE_ICONIFIED,
SAPP_EVENTTYPE_RESTORED,
SAPP_EVENTTYPE_FOCUSED,
SAPP_EVENTTYPE_UNFOCUSED,
SAPP_EVENTTYPE_SUSPENDED,
SAPP_EVENTTYPE_RESUMED,
SAPP_EVENTTYPE_QUIT_REQUESTED,
SAPP_EVENTTYPE_CLIPBOARD_PASTED,
SAPP_EVENTTYPE_FILES_DROPPED,
_SAPP_EVENTTYPE_NUM,
_SAPP_EVENTTYPE_FORCE_U32 = 0x7FFFFFFF
} sapp_event_type;
/*
sapp_keycode
The 'virtual keycode' of a KEY_DOWN or KEY_UP event in the
struct field sapp_event.key_code.
Note that the keycode values are identical with GLFW.
*/
typedef enum sapp_keycode {
SAPP_KEYCODE_INVALID = 0,
SAPP_KEYCODE_SPACE = 32,
SAPP_KEYCODE_APOSTROPHE = 39, /* ' */
SAPP_KEYCODE_COMMA = 44, /* , */
SAPP_KEYCODE_MINUS = 45, /* - */
SAPP_KEYCODE_PERIOD = 46, /* . */
SAPP_KEYCODE_SLASH = 47, /* / */
SAPP_KEYCODE_0 = 48,
SAPP_KEYCODE_1 = 49,
SAPP_KEYCODE_2 = 50,
SAPP_KEYCODE_3 = 51,
SAPP_KEYCODE_4 = 52,
SAPP_KEYCODE_5 = 53,
SAPP_KEYCODE_6 = 54,
SAPP_KEYCODE_7 = 55,
SAPP_KEYCODE_8 = 56,
SAPP_KEYCODE_9 = 57,
SAPP_KEYCODE_SEMICOLON = 59, /* ; */
SAPP_KEYCODE_EQUAL = 61, /* = */
SAPP_KEYCODE_A = 65,
SAPP_KEYCODE_B = 66,
SAPP_KEYCODE_C = 67,
SAPP_KEYCODE_D = 68,
SAPP_KEYCODE_E = 69,
SAPP_KEYCODE_F = 70,
SAPP_KEYCODE_G = 71,
SAPP_KEYCODE_H = 72,
SAPP_KEYCODE_I = 73,
SAPP_KEYCODE_J = 74,
SAPP_KEYCODE_K = 75,
SAPP_KEYCODE_L = 76,
SAPP_KEYCODE_M = 77,
SAPP_KEYCODE_N = 78,
SAPP_KEYCODE_O = 79,
SAPP_KEYCODE_P = 80,
SAPP_KEYCODE_Q = 81,
SAPP_KEYCODE_R = 82,
SAPP_KEYCODE_S = 83,
SAPP_KEYCODE_T = 84,
SAPP_KEYCODE_U = 85,
SAPP_KEYCODE_V = 86,
SAPP_KEYCODE_W = 87,
SAPP_KEYCODE_X = 88,
SAPP_KEYCODE_Y = 89,
SAPP_KEYCODE_Z = 90,
SAPP_KEYCODE_LEFT_BRACKET = 91, /* [ */
SAPP_KEYCODE_BACKSLASH = 92, /* \ */
SAPP_KEYCODE_RIGHT_BRACKET = 93, /* ] */
SAPP_KEYCODE_GRAVE_ACCENT = 96, /* ` */
SAPP_KEYCODE_WORLD_1 = 161, /* non-US #1 */
SAPP_KEYCODE_WORLD_2 = 162, /* non-US #2 */
SAPP_KEYCODE_ESCAPE = 256,
SAPP_KEYCODE_ENTER = 257,
SAPP_KEYCODE_TAB = 258,
SAPP_KEYCODE_BACKSPACE = 259,
SAPP_KEYCODE_INSERT = 260,
SAPP_KEYCODE_DELETE = 261,
SAPP_KEYCODE_RIGHT = 262,
SAPP_KEYCODE_LEFT = 263,
SAPP_KEYCODE_DOWN = 264,
SAPP_KEYCODE_UP = 265,
SAPP_KEYCODE_PAGE_UP = 266,
SAPP_KEYCODE_PAGE_DOWN = 267,
SAPP_KEYCODE_HOME = 268,
SAPP_KEYCODE_END = 269,
SAPP_KEYCODE_CAPS_LOCK = 280,
SAPP_KEYCODE_SCROLL_LOCK = 281,
SAPP_KEYCODE_NUM_LOCK = 282,
SAPP_KEYCODE_PRINT_SCREEN = 283,
SAPP_KEYCODE_PAUSE = 284,
SAPP_KEYCODE_F1 = 290,
SAPP_KEYCODE_F2 = 291,
SAPP_KEYCODE_F3 = 292,
SAPP_KEYCODE_F4 = 293,
SAPP_KEYCODE_F5 = 294,
SAPP_KEYCODE_F6 = 295,
SAPP_KEYCODE_F7 = 296,
SAPP_KEYCODE_F8 = 297,
SAPP_KEYCODE_F9 = 298,
SAPP_KEYCODE_F10 = 299,
SAPP_KEYCODE_F11 = 300,
SAPP_KEYCODE_F12 = 301,
SAPP_KEYCODE_F13 = 302,
SAPP_KEYCODE_F14 = 303,
SAPP_KEYCODE_F15 = 304,
SAPP_KEYCODE_F16 = 305,
SAPP_KEYCODE_F17 = 306,
SAPP_KEYCODE_F18 = 307,
SAPP_KEYCODE_F19 = 308,
SAPP_KEYCODE_F20 = 309,
SAPP_KEYCODE_F21 = 310,
SAPP_KEYCODE_F22 = 311,
SAPP_KEYCODE_F23 = 312,
SAPP_KEYCODE_F24 = 313,
SAPP_KEYCODE_F25 = 314,
SAPP_KEYCODE_KP_0 = 320,
SAPP_KEYCODE_KP_1 = 321,
SAPP_KEYCODE_KP_2 = 322,
SAPP_KEYCODE_KP_3 = 323,
SAPP_KEYCODE_KP_4 = 324,
SAPP_KEYCODE_KP_5 = 325,
SAPP_KEYCODE_KP_6 = 326,
SAPP_KEYCODE_KP_7 = 327,
SAPP_KEYCODE_KP_8 = 328,
SAPP_KEYCODE_KP_9 = 329,
SAPP_KEYCODE_KP_DECIMAL = 330,
SAPP_KEYCODE_KP_DIVIDE = 331,
SAPP_KEYCODE_KP_MULTIPLY = 332,
SAPP_KEYCODE_KP_SUBTRACT = 333,
SAPP_KEYCODE_KP_ADD = 334,
SAPP_KEYCODE_KP_ENTER = 335,
SAPP_KEYCODE_KP_EQUAL = 336,
SAPP_KEYCODE_LEFT_SHIFT = 340,
SAPP_KEYCODE_LEFT_CONTROL = 341,
SAPP_KEYCODE_LEFT_ALT = 342,
SAPP_KEYCODE_LEFT_SUPER = 343,
SAPP_KEYCODE_RIGHT_SHIFT = 344,
SAPP_KEYCODE_RIGHT_CONTROL = 345,
SAPP_KEYCODE_RIGHT_ALT = 346,
SAPP_KEYCODE_RIGHT_SUPER = 347,
SAPP_KEYCODE_MENU = 348,
} sapp_keycode;
/*
Android specific 'tool type' enum for touch events. This lets the
application check what type of input device was used for
touch events.
NOTE: the values must remain in sync with the corresponding
Android SDK type, so don't change those.
See https://developer.android.com/reference/android/view/MotionEvent#TOOL_TYPE_UNKNOWN
*/
typedef enum sapp_android_tooltype {
SAPP_ANDROIDTOOLTYPE_UNKNOWN = 0, // TOOL_TYPE_UNKNOWN
SAPP_ANDROIDTOOLTYPE_FINGER = 1, // TOOL_TYPE_FINGER
SAPP_ANDROIDTOOLTYPE_STYLUS = 2, // TOOL_TYPE_STYLUS
SAPP_ANDROIDTOOLTYPE_MOUSE = 3, // TOOL_TYPE_MOUSE
} sapp_android_tooltype;
/*
sapp_touchpoint
Describes a single touchpoint in a multitouch event (TOUCHES_BEGAN,
TOUCHES_MOVED, TOUCHES_ENDED).
Touch points are stored in the nested array sapp_event.touches[],
and the number of touches is stored in sapp_event.num_touches.
*/
typedef struct sapp_touchpoint {
uintptr_t identifier;
float pos_x;
float pos_y;
sapp_android_tooltype android_tooltype; // only valid on Android
bool changed;
} sapp_touchpoint;
/*
sapp_mousebutton
The currently pressed mouse button in the events MOUSE_DOWN
and MOUSE_UP, stored in the struct field sapp_event.mouse_button.
*/
typedef enum sapp_mousebutton {
SAPP_MOUSEBUTTON_LEFT = 0x0,
SAPP_MOUSEBUTTON_RIGHT = 0x1,
SAPP_MOUSEBUTTON_MIDDLE = 0x2,
SAPP_MOUSEBUTTON_INVALID = 0x100,
} sapp_mousebutton;
/*
These are currently pressed modifier keys (and mouse buttons) which are
passed in the event struct field sapp_event.modifiers.
*/
enum {
SAPP_MODIFIER_SHIFT = 0x1, // left or right shift key
SAPP_MODIFIER_CTRL = 0x2, // left or right control key
SAPP_MODIFIER_ALT = 0x4, // left or right alt key
SAPP_MODIFIER_SUPER = 0x8, // left or right 'super' key
SAPP_MODIFIER_LMB = 0x100, // left mouse button
SAPP_MODIFIER_RMB = 0x200, // right mouse button
SAPP_MODIFIER_MMB = 0x400, // middle mouse button
};
/*
sapp_event
This is an all-in-one event struct passed to the event handler
user callback function. Note that it depends on the event
type what struct fields actually contain useful values, so you
should first check the event type before reading other struct
fields.
*/
typedef struct sapp_event {
uint64_t frame_count; // current frame counter, always valid, useful for checking if two events were issued in the same frame
sapp_event_type type; // the event type, always valid
sapp_keycode key_code; // the virtual key code, only valid in KEY_UP, KEY_DOWN
uint32_t char_code; // the UTF-32 character code, only valid in CHAR events
bool key_repeat; // true if this is a key-repeat event, valid in KEY_UP, KEY_DOWN and CHAR
uint32_t modifiers; // current modifier keys, valid in all key-, char- and mouse-events
sapp_mousebutton mouse_button; // mouse button that was pressed or released, valid in MOUSE_DOWN, MOUSE_UP
float mouse_x; // current horizontal mouse position in pixels, always valid except during mouse lock
float mouse_y; // current vertical mouse position in pixels, always valid except during mouse lock
float mouse_dx; // relative horizontal mouse movement since last frame, always valid
float mouse_dy; // relative vertical mouse movement since last frame, always valid
float scroll_x; // horizontal mouse wheel scroll distance, valid in MOUSE_SCROLL events
float scroll_y; // vertical mouse wheel scroll distance, valid in MOUSE_SCROLL events
int num_touches; // number of valid items in the touches[] array
sapp_touchpoint touches[SAPP_MAX_TOUCHPOINTS]; // current touch points, valid in TOUCHES_BEGIN, TOUCHES_MOVED, TOUCHES_ENDED
int window_width; // current window- and framebuffer sizes in pixels, always valid
int window_height;
int framebuffer_width; // = window_width * dpi_scale
int framebuffer_height; // = window_height * dpi_scale
} sapp_event;
/*
sg_range
A general pointer/size-pair struct and constructor macros for passing binary blobs
into sokol_app.h.
*/
typedef struct sapp_range {
const void* ptr;
size_t size;
} sapp_range;
// disabling this for every includer isn't great, but the warnings are also quite pointless
#if defined(_MSC_VER)
#pragma warning(disable:4221) /* /W4 only: nonstandard extension used: 'x': cannot be initialized using address of automatic variable 'y' */
#pragma warning(disable:4204) /* VS2015: nonstandard extension used: non-constant aggregate initializer */
#endif
#if defined(__cplusplus)
#define SAPP_RANGE(x) sapp_range{ &x, sizeof(x) }
#else
#define SAPP_RANGE(x) (sapp_range){ &x, sizeof(x) }
#endif
/*
sapp_image_desc
This is used to describe image data to sokol_app.h (at first, window
icons, later maybe cursor images).
Note that the actual image pixel format depends on the use case:
- window icon pixels are RGBA8
*/
typedef struct sapp_image_desc {
int width;
int height;
sapp_range pixels;
} sapp_image_desc;
/*
sapp_icon_desc
An icon description structure for use in sapp_desc.icon and
sapp_set_icon().
When setting a custom image, the application can provide a number of
candidates differing in size, and sokol_app.h will pick the image(s)
closest to the size expected by the platform's window system.
To set sokol-app's default icon, set .sokol_default to true.
Otherwise provide candidate images of different sizes in the
images[] array.
If both the sokol_default flag is set to true, any image candidates
will be ignored and the sokol_app.h default icon will be set.
*/
typedef struct sapp_icon_desc {
bool sokol_default;
sapp_image_desc images[SAPP_MAX_ICONIMAGES];
} sapp_icon_desc;
/*
sapp_allocator
Used in sapp_desc to provide custom memory-alloc and -free functions
to sokol_app.h. If memory management should be overridden, both the
alloc_fn and free_fn function must be provided (e.g. it's not valid to
override one function but not the other).
*/
typedef struct sapp_allocator {
void* (*alloc_fn)(size_t size, void* user_data);
void (*free_fn)(void* ptr, void* user_data);
void* user_data;
} sapp_allocator;
/*
sapp_log_item
Log items are defined via X-Macros and expanded to an enum
'sapp_log_item', and in debug mode to corresponding
human readable error messages.
*/
#define _SAPP_LOG_ITEMS \
_SAPP_LOGITEM_XMACRO(OK, "Ok") \
_SAPP_LOGITEM_XMACRO(MALLOC_FAILED, "memory allocation failed") \
_SAPP_LOGITEM_XMACRO(MACOS_INVALID_NSOPENGL_PROFILE, "macos: invalid NSOpenGLProfile (valid choices are 1.0 and 4.1)") \
_SAPP_LOGITEM_XMACRO(WIN32_LOAD_OPENGL32_DLL_FAILED, "failed loading opengl32.dll") \
_SAPP_LOGITEM_XMACRO(WIN32_CREATE_HELPER_WINDOW_FAILED, "failed to create helper window") \
_SAPP_LOGITEM_XMACRO(WIN32_HELPER_WINDOW_GETDC_FAILED, "failed to get helper window DC") \
_SAPP_LOGITEM_XMACRO(WIN32_DUMMY_CONTEXT_SET_PIXELFORMAT_FAILED, "failed to set pixel format for dummy GL context") \
_SAPP_LOGITEM_XMACRO(WIN32_CREATE_DUMMY_CONTEXT_FAILED, "failed to create dummy GL context") \
_SAPP_LOGITEM_XMACRO(WIN32_DUMMY_CONTEXT_MAKE_CURRENT_FAILED, "failed to make dummy GL context current") \
_SAPP_LOGITEM_XMACRO(WIN32_GET_PIXELFORMAT_ATTRIB_FAILED, "failed to get WGL pixel format attribute") \
_SAPP_LOGITEM_XMACRO(WIN32_WGL_FIND_PIXELFORMAT_FAILED, "failed to find matching WGL pixel format") \
_SAPP_LOGITEM_XMACRO(WIN32_WGL_DESCRIBE_PIXELFORMAT_FAILED, "failed to get pixel format descriptor") \
_SAPP_LOGITEM_XMACRO(WIN32_WGL_SET_PIXELFORMAT_FAILED, "failed to set selected pixel format") \
_SAPP_LOGITEM_XMACRO(WIN32_WGL_ARB_CREATE_CONTEXT_REQUIRED, "ARB_create_context required") \
_SAPP_LOGITEM_XMACRO(WIN32_WGL_ARB_CREATE_CONTEXT_PROFILE_REQUIRED, "ARB_create_context_profile required") \
_SAPP_LOGITEM_XMACRO(WIN32_WGL_OPENGL_VERSION_NOT_SUPPORTED, "requested OpenGL version not supported by GL driver (ERROR_INVALID_VERSION_ARB)") \
_SAPP_LOGITEM_XMACRO(WIN32_WGL_OPENGL_PROFILE_NOT_SUPPORTED, "requested OpenGL profile not support by GL driver (ERROR_INVALID_PROFILE_ARB)") \
_SAPP_LOGITEM_XMACRO(WIN32_WGL_INCOMPATIBLE_DEVICE_CONTEXT, "CreateContextAttribsARB failed with ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB") \
_SAPP_LOGITEM_XMACRO(WIN32_WGL_CREATE_CONTEXT_ATTRIBS_FAILED_OTHER, "CreateContextAttribsARB failed for other reason") \
_SAPP_LOGITEM_XMACRO(WIN32_D3D11_CREATE_DEVICE_AND_SWAPCHAIN_WITH_DEBUG_FAILED, "D3D11CreateDeviceAndSwapChain() with D3D11_CREATE_DEVICE_DEBUG failed, retrying without debug flag.") \
_SAPP_LOGITEM_XMACRO(WIN32_D3D11_GET_IDXGIFACTORY_FAILED, "could not obtain IDXGIFactory object") \
_SAPP_LOGITEM_XMACRO(WIN32_D3D11_GET_IDXGIADAPTER_FAILED, "could not obtain IDXGIAdapter object") \
_SAPP_LOGITEM_XMACRO(WIN32_D3D11_QUERY_INTERFACE_IDXGIDEVICE1_FAILED, "could not obtain IDXGIDevice1 interface") \
_SAPP_LOGITEM_XMACRO(WIN32_REGISTER_RAW_INPUT_DEVICES_FAILED_MOUSE_LOCK, "RegisterRawInputDevices() failed (on mouse lock)") \
_SAPP_LOGITEM_XMACRO(WIN32_REGISTER_RAW_INPUT_DEVICES_FAILED_MOUSE_UNLOCK, "RegisterRawInputDevices() failed (on mouse unlock)") \
_SAPP_LOGITEM_XMACRO(WIN32_GET_RAW_INPUT_DATA_FAILED, "GetRawInputData() failed") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_LOAD_LIBGL_FAILED, "failed to load libGL") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_LOAD_ENTRY_POINTS_FAILED, "failed to load GLX entry points") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_EXTENSION_NOT_FOUND, "GLX extension not found") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_QUERY_VERSION_FAILED, "failed to query GLX version") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_VERSION_TOO_LOW, "GLX version too low (need at least 1.3)") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_NO_GLXFBCONFIGS, "glXGetFBConfigs() returned no configs") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_NO_SUITABLE_GLXFBCONFIG, "failed to find a suitable GLXFBConfig") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_GET_VISUAL_FROM_FBCONFIG_FAILED, "glXGetVisualFromFBConfig failed") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_REQUIRED_EXTENSIONS_MISSING, "GLX extensions ARB_create_context and ARB_create_context_profile missing") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_CREATE_CONTEXT_FAILED, "Failed to create GL context via glXCreateContextAttribsARB") \
_SAPP_LOGITEM_XMACRO(LINUX_GLX_CREATE_WINDOW_FAILED, "glXCreateWindow() failed") \
_SAPP_LOGITEM_XMACRO(LINUX_X11_CREATE_WINDOW_FAILED, "XCreateWindow() failed") \
_SAPP_LOGITEM_XMACRO(LINUX_EGL_BIND_OPENGL_API_FAILED, "eglBindAPI(EGL_OPENGL_API) failed") \
_SAPP_LOGITEM_XMACRO(LINUX_EGL_BIND_OPENGL_ES_API_FAILED, "eglBindAPI(EGL_OPENGL_ES_API) failed") \
_SAPP_LOGITEM_XMACRO(LINUX_EGL_GET_DISPLAY_FAILED, "eglGetDisplay() failed") \
_SAPP_LOGITEM_XMACRO(LINUX_EGL_INITIALIZE_FAILED, "eglInitialize() failed") \
_SAPP_LOGITEM_XMACRO(LINUX_EGL_NO_CONFIGS, "eglChooseConfig() returned no configs") \
_SAPP_LOGITEM_XMACRO(LINUX_EGL_NO_NATIVE_VISUAL, "eglGetConfigAttrib() for EGL_NATIVE_VISUAL_ID failed") \
_SAPP_LOGITEM_XMACRO(LINUX_EGL_GET_VISUAL_INFO_FAILED, "XGetVisualInfo() failed") \
_SAPP_LOGITEM_XMACRO(LINUX_EGL_CREATE_WINDOW_SURFACE_FAILED, "eglCreateWindowSurface() failed") \
_SAPP_LOGITEM_XMACRO(LINUX_EGL_CREATE_CONTEXT_FAILED, "eglCreateContext() failed") \
_SAPP_LOGITEM_XMACRO(LINUX_EGL_MAKE_CURRENT_FAILED, "eglMakeCurrent() failed") \
_SAPP_LOGITEM_XMACRO(LINUX_X11_OPEN_DISPLAY_FAILED, "XOpenDisplay() failed") \
_SAPP_LOGITEM_XMACRO(LINUX_X11_QUERY_SYSTEM_DPI_FAILED, "failed to query system dpi value, assuming default 96.0") \
_SAPP_LOGITEM_XMACRO(LINUX_X11_DROPPED_FILE_URI_WRONG_SCHEME, "dropped file URL doesn't start with 'file://'") \
_SAPP_LOGITEM_XMACRO(ANDROID_UNSUPPORTED_INPUT_EVENT_INPUT_CB, "unsupported input event encountered in _sapp_android_input_cb()") \
_SAPP_LOGITEM_XMACRO(ANDROID_UNSUPPORTED_INPUT_EVENT_MAIN_CB, "unsupported input event encountered in _sapp_android_main_cb()") \
_SAPP_LOGITEM_XMACRO(ANDROID_READ_MSG_FAILED, "failed to read message in _sapp_android_main_cb()") \
_SAPP_LOGITEM_XMACRO(ANDROID_WRITE_MSG_FAILED, "failed to write message in _sapp_android_msg") \
_SAPP_LOGITEM_XMACRO(ANDROID_MSG_CREATE, "MSG_CREATE") \
_SAPP_LOGITEM_XMACRO(ANDROID_MSG_RESUME, "MSG_RESUME") \
_SAPP_LOGITEM_XMACRO(ANDROID_MSG_PAUSE, "MSG_PAUSE") \
_SAPP_LOGITEM_XMACRO(ANDROID_MSG_FOCUS, "MSG_FOCUS") \
_SAPP_LOGITEM_XMACRO(ANDROID_MSG_NO_FOCUS, "MSG_NO_FOCUS") \
_SAPP_LOGITEM_XMACRO(ANDROID_MSG_SET_NATIVE_WINDOW, "MSG_SET_NATIVE_WINDOW") \
_SAPP_LOGITEM_XMACRO(ANDROID_MSG_SET_INPUT_QUEUE, "MSG_SET_INPUT_QUEUE") \
_SAPP_LOGITEM_XMACRO(ANDROID_MSG_DESTROY, "MSG_DESTROY") \
_SAPP_LOGITEM_XMACRO(ANDROID_UNKNOWN_MSG, "unknown msg type received") \
_SAPP_LOGITEM_XMACRO(ANDROID_LOOP_THREAD_STARTED, "loop thread started") \
_SAPP_LOGITEM_XMACRO(ANDROID_LOOP_THREAD_DONE, "loop thread done") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONSTART, "NativeActivity onStart()") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONRESUME, "NativeActivity onResume") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONSAVEINSTANCESTATE, "NativeActivity onSaveInstanceState") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONWINDOWFOCUSCHANGED, "NativeActivity onWindowFocusChanged") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONPAUSE, "NativeActivity onPause") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONSTOP, "NativeActivity onStop()") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONNATIVEWINDOWCREATED, "NativeActivity onNativeWindowCreated") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONNATIVEWINDOWDESTROYED, "NativeActivity onNativeWindowDestroyed") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONINPUTQUEUECREATED, "NativeActivity onInputQueueCreated") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONINPUTQUEUEDESTROYED, "NativeActivity onInputQueueDestroyed") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONCONFIGURATIONCHANGED, "NativeActivity onConfigurationChanged") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONLOWMEMORY, "NativeActivity onLowMemory") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONDESTROY, "NativeActivity onDestroy") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_DONE, "NativeActivity done") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONCREATE, "NativeActivity onCreate") \
_SAPP_LOGITEM_XMACRO(ANDROID_CREATE_THREAD_PIPE_FAILED, "failed to create thread pipe") \
_SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_CREATE_SUCCESS, "NativeActivity successfully created") \
_SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_SURFACE_FAILED, "wgpu: failed to create surface for swapchain") \
_SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_SWAPCHAIN_FAILED, "wgpu: failed to create swapchain object") \
_SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_DEPTH_STENCIL_TEXTURE_FAILED, "wgpu: failed to create depth-stencil texture for swapchain") \
_SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_DEPTH_STENCIL_VIEW_FAILED, "wgpu: failed to create view object for swapchain depth-stencil texture") \
_SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_MSAA_TEXTURE_FAILED, "wgpu: failed to create msaa texture for swapchain") \
_SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_MSAA_VIEW_FAILED, "wgpu: failed to create view object for swapchain msaa texture") \
_SAPP_LOGITEM_XMACRO(WGPU_REQUEST_DEVICE_STATUS_ERROR, "wgpu: requesting device failed with status 'error'") \
_SAPP_LOGITEM_XMACRO(WGPU_REQUEST_DEVICE_STATUS_UNKNOWN, "wgpu: requesting device failed with status 'unknown'") \
_SAPP_LOGITEM_XMACRO(WGPU_REQUEST_ADAPTER_STATUS_UNAVAILABLE, "wgpu: requesting adapter failed with 'unavailable'") \
_SAPP_LOGITEM_XMACRO(WGPU_REQUEST_ADAPTER_STATUS_ERROR, "wgpu: requesting adapter failed with status 'error'") \
_SAPP_LOGITEM_XMACRO(WGPU_REQUEST_ADAPTER_STATUS_UNKNOWN, "wgpu: requesting adapter failed with status 'unknown'") \
_SAPP_LOGITEM_XMACRO(WGPU_CREATE_INSTANCE_FAILED, "wgpu: failed to create instance") \
_SAPP_LOGITEM_XMACRO(IMAGE_DATA_SIZE_MISMATCH, "image data size mismatch (must be width*height*4 bytes)") \
_SAPP_LOGITEM_XMACRO(DROPPED_FILE_PATH_TOO_LONG, "dropped file path too long (sapp_desc.max_dropped_filed_path_length)") \
_SAPP_LOGITEM_XMACRO(CLIPBOARD_STRING_TOO_BIG, "clipboard string didn't fit into clipboard buffer") \
#define _SAPP_LOGITEM_XMACRO(item,msg) SAPP_LOGITEM_##item,
typedef enum sapp_log_item {
_SAPP_LOG_ITEMS
} sapp_log_item;
#undef _SAPP_LOGITEM_XMACRO
/*
sapp_logger
Used in sapp_desc to provide a logging function. Please be aware that
without logging function, sokol-app will be completely silent, e.g. it will
not report errors or warnings. For maximum error verbosity, compile in
debug mode (e.g. NDEBUG *not* defined) and install a logger (for instance
the standard logging function from sokol_log.h).
*/
typedef struct sapp_logger {
void (*func)(
const char* tag, // always "sapp"
uint32_t log_level, // 0=panic, 1=error, 2=warning, 3=info
uint32_t log_item_id, // SAPP_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_app.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data);
void* user_data;
} sapp_logger;
typedef struct sapp_desc {
void (*init_cb)(void); // these are the user-provided callbacks without user data
void (*frame_cb)(void);
void (*cleanup_cb)(void);
void (*event_cb)(const sapp_event*);
void* user_data; // these are the user-provided callbacks with user data
void (*init_userdata_cb)(void*);
void (*frame_userdata_cb)(void*);
void (*cleanup_userdata_cb)(void*);
void (*event_userdata_cb)(const sapp_event*, void*);
int width; // the preferred width of the window / canvas
int height; // the preferred height of the window / canvas
int sample_count; // MSAA sample count
int swap_interval; // the preferred swap interval (ignored on some platforms)
bool high_dpi; // whether the rendering canvas is full-resolution on HighDPI displays
bool fullscreen; // whether the window should be created in fullscreen mode
bool alpha; // whether the framebuffer should have an alpha channel (ignored on some platforms)
const char* window_title; // the window title as UTF-8 encoded string
bool enable_clipboard; // enable clipboard access, default is false
int clipboard_size; // max size of clipboard content in bytes
bool enable_dragndrop; // enable file dropping (drag'n'drop), default is false
int max_dropped_files; // max number of dropped files to process (default: 1)
int max_dropped_file_path_length; // max length in bytes of a dropped UTF-8 file path (default: 2048)
sapp_icon_desc icon; // the initial window icon to set
sapp_allocator allocator; // optional memory allocation overrides (default: malloc/free)
sapp_logger logger; // logging callback override (default: NO LOGGING!)
// backend-specific options
int gl_major_version; // override GL major and minor version (the default GL version is 4.1 on macOS, 4.3 elsewhere)
int gl_minor_version;
bool win32_console_utf8; // if true, set the output console codepage to UTF-8
bool win32_console_create; // if true, attach stdout/stderr to a new console window
bool win32_console_attach; // if true, attach stdout/stderr to parent process
const char* html5_canvas_name; // the name (id) of the HTML5 canvas element, default is "canvas"
bool html5_canvas_resize; // if true, the HTML5 canvas size is set to sapp_desc.width/height, otherwise canvas size is tracked
bool html5_preserve_drawing_buffer; // HTML5 only: whether to preserve default framebuffer content between frames
bool html5_premultiplied_alpha; // HTML5 only: whether the rendered pixels use premultiplied alpha convention
bool html5_ask_leave_site; // initial state of the internal html5_ask_leave_site flag (see sapp_html5_ask_leave_site())
bool html5_bubble_mouse_events; // if true, mouse events will bubble up to the web page
bool html5_bubble_touch_events; // same for touch events
bool html5_bubble_wheel_events; // same for wheel events
bool html5_bubble_key_events; // if true, bubble up *all* key events to browser, not just key events that represent characters
bool html5_bubble_char_events; // if true, bubble up character events to browser
bool html5_use_emsc_set_main_loop; // if true, use emscripten_set_main_loop() instead of emscripten_request_animation_frame_loop()
bool html5_emsc_set_main_loop_simulate_infinite_loop; // this will be passed as the simulate_infinite_loop arg to emscripten_set_main_loop()
bool ios_keyboard_resizes_canvas; // if true, showing the iOS keyboard shrinks the canvas
} sapp_desc;
/* HTML5 specific: request and response structs for
asynchronously loading dropped-file content.
*/
typedef enum sapp_html5_fetch_error {
SAPP_HTML5_FETCH_ERROR_NO_ERROR,
SAPP_HTML5_FETCH_ERROR_BUFFER_TOO_SMALL,
SAPP_HTML5_FETCH_ERROR_OTHER,
} sapp_html5_fetch_error;
typedef struct sapp_html5_fetch_response {
bool succeeded; // true if the loading operation has succeeded
sapp_html5_fetch_error error_code;
int file_index; // index of the dropped file (0..sapp_get_num_dropped_filed()-1)
sapp_range data; // pointer and size of the fetched data (data.ptr == buffer.ptr, data.size <= buffer.size)
sapp_range buffer; // the user-provided buffer ptr/size pair (buffer.ptr == data.ptr, buffer.size >= data.size)
void* user_data; // user-provided user data pointer
} sapp_html5_fetch_response;
typedef struct sapp_html5_fetch_request {
int dropped_file_index; // 0..sapp_get_num_dropped_files()-1
void (*callback)(const sapp_html5_fetch_response*); // response callback function pointer (required)
sapp_range buffer; // ptr/size of a memory buffer to load the data into
void* user_data; // optional userdata pointer
} sapp_html5_fetch_request;
/*
sapp_mouse_cursor
Predefined cursor image definitions, set with sapp_set_mouse_cursor(sapp_mouse_cursor cursor)
*/
typedef enum sapp_mouse_cursor {
SAPP_MOUSECURSOR_DEFAULT = 0, // equivalent with system default cursor
SAPP_MOUSECURSOR_ARROW,
SAPP_MOUSECURSOR_IBEAM,
SAPP_MOUSECURSOR_CROSSHAIR,
SAPP_MOUSECURSOR_POINTING_HAND,
SAPP_MOUSECURSOR_RESIZE_EW,
SAPP_MOUSECURSOR_RESIZE_NS,
SAPP_MOUSECURSOR_RESIZE_NWSE,
SAPP_MOUSECURSOR_RESIZE_NESW,
SAPP_MOUSECURSOR_RESIZE_ALL,
SAPP_MOUSECURSOR_NOT_ALLOWED,
_SAPP_MOUSECURSOR_NUM,
} sapp_mouse_cursor;
/* user-provided functions */
extern sapp_desc sokol_main(int argc, char* argv[]);
/* returns true after sokol-app has been initialized */
SOKOL_APP_API_DECL bool sapp_isvalid(void);
/* returns the current framebuffer width in pixels */
SOKOL_APP_API_DECL int sapp_width(void);
/* same as sapp_width(), but returns float */
SOKOL_APP_API_DECL float sapp_widthf(void);
/* returns the current framebuffer height in pixels */
SOKOL_APP_API_DECL int sapp_height(void);
/* same as sapp_height(), but returns float */
SOKOL_APP_API_DECL float sapp_heightf(void);
/* get default framebuffer color pixel format */
SOKOL_APP_API_DECL int sapp_color_format(void);
/* get default framebuffer depth pixel format */
SOKOL_APP_API_DECL int sapp_depth_format(void);
/* get default framebuffer sample count */
SOKOL_APP_API_DECL int sapp_sample_count(void);
/* returns true when high_dpi was requested and actually running in a high-dpi scenario */
SOKOL_APP_API_DECL bool sapp_high_dpi(void);
/* returns the dpi scaling factor (window pixels to framebuffer pixels) */
SOKOL_APP_API_DECL float sapp_dpi_scale(void);
/* show or hide the mobile device onscreen keyboard */
SOKOL_APP_API_DECL void sapp_show_keyboard(bool show);
/* return true if the mobile device onscreen keyboard is currently shown */
SOKOL_APP_API_DECL bool sapp_keyboard_shown(void);
/* query fullscreen mode */
SOKOL_APP_API_DECL bool sapp_is_fullscreen(void);
/* toggle fullscreen mode */
SOKOL_APP_API_DECL void sapp_toggle_fullscreen(void);
/* show or hide the mouse cursor */
SOKOL_APP_API_DECL void sapp_show_mouse(bool show);
/* show or hide the mouse cursor */
SOKOL_APP_API_DECL bool sapp_mouse_shown(void);
/* enable/disable mouse-pointer-lock mode */
SOKOL_APP_API_DECL void sapp_lock_mouse(bool lock);
/* return true if in mouse-pointer-lock mode (this may toggle a few frames later) */
SOKOL_APP_API_DECL bool sapp_mouse_locked(void);
/* set mouse cursor type */
SOKOL_APP_API_DECL void sapp_set_mouse_cursor(sapp_mouse_cursor cursor);
/* get current mouse cursor type */
SOKOL_APP_API_DECL sapp_mouse_cursor sapp_get_mouse_cursor(void);
/* return the userdata pointer optionally provided in sapp_desc */
SOKOL_APP_API_DECL void* sapp_userdata(void);
/* return a copy of the sapp_desc structure */
SOKOL_APP_API_DECL sapp_desc sapp_query_desc(void);
/* initiate a "soft quit" (sends SAPP_EVENTTYPE_QUIT_REQUESTED) */
SOKOL_APP_API_DECL void sapp_request_quit(void);
/* cancel a pending quit (when SAPP_EVENTTYPE_QUIT_REQUESTED has been received) */
SOKOL_APP_API_DECL void sapp_cancel_quit(void);
/* initiate a "hard quit" (quit application without sending SAPP_EVENTTYPE_QUIT_REQUESTED) */
SOKOL_APP_API_DECL void sapp_quit(void);
/* call from inside event callback to consume the current event (don't forward to platform) */
SOKOL_APP_API_DECL void sapp_consume_event(void);
/* get the current frame counter (for comparison with sapp_event.frame_count) */
SOKOL_APP_API_DECL uint64_t sapp_frame_count(void);
/* get an averaged/smoothed frame duration in seconds */
SOKOL_APP_API_DECL double sapp_frame_duration(void);
/* write string into clipboard */
SOKOL_APP_API_DECL void sapp_set_clipboard_string(const char* str);
/* read string from clipboard (usually during SAPP_EVENTTYPE_CLIPBOARD_PASTED) */
SOKOL_APP_API_DECL const char* sapp_get_clipboard_string(void);
/* set the window title (only on desktop platforms) */
SOKOL_APP_API_DECL void sapp_set_window_title(const char* str);
/* set the window icon (only on Windows and Linux) */
SOKOL_APP_API_DECL void sapp_set_icon(const sapp_icon_desc* icon_desc);
/* gets the total number of dropped files (after an SAPP_EVENTTYPE_FILES_DROPPED event) */
SOKOL_APP_API_DECL int sapp_get_num_dropped_files(void);
/* gets the dropped file paths */
SOKOL_APP_API_DECL const char* sapp_get_dropped_file_path(int index);
/* special run-function for SOKOL_NO_ENTRY (in standard mode this is an empty stub) */
SOKOL_APP_API_DECL void sapp_run(const sapp_desc* desc);
/* EGL: get EGLDisplay object */
SOKOL_APP_API_DECL const void* sapp_egl_get_display(void);
/* EGL: get EGLContext object */
SOKOL_APP_API_DECL const void* sapp_egl_get_context(void);
/* HTML5: enable or disable the hardwired "Leave Site?" dialog box */
SOKOL_APP_API_DECL void sapp_html5_ask_leave_site(bool ask);
/* HTML5: get byte size of a dropped file */
SOKOL_APP_API_DECL uint32_t sapp_html5_get_dropped_file_size(int index);
/* HTML5: asynchronously load the content of a dropped file */
SOKOL_APP_API_DECL void sapp_html5_fetch_dropped_file(const sapp_html5_fetch_request* request);
/* Metal: get bridged pointer to Metal device object */
SOKOL_APP_API_DECL const void* sapp_metal_get_device(void);
/* Metal: get bridged pointer to MTKView's current drawable of type CAMetalDrawable */
SOKOL_APP_API_DECL const void* sapp_metal_get_current_drawable(void);
/* Metal: get bridged pointer to MTKView's depth-stencil texture of type MTLTexture */
SOKOL_APP_API_DECL const void* sapp_metal_get_depth_stencil_texture(void);
/* Metal: get bridged pointer to MTKView's msaa-color-texture of type MTLTexture (may be null) */
SOKOL_APP_API_DECL const void* sapp_metal_get_msaa_color_texture(void);
/* macOS: get bridged pointer to macOS NSWindow */
SOKOL_APP_API_DECL const void* sapp_macos_get_window(void);
/* iOS: get bridged pointer to iOS UIWindow */
SOKOL_APP_API_DECL const void* sapp_ios_get_window(void);
/* D3D11: get pointer to ID3D11Device object */
SOKOL_APP_API_DECL const void* sapp_d3d11_get_device(void);
/* D3D11: get pointer to ID3D11DeviceContext object */
SOKOL_APP_API_DECL const void* sapp_d3d11_get_device_context(void);
/* D3D11: get pointer to IDXGISwapChain object */
SOKOL_APP_API_DECL const void* sapp_d3d11_get_swap_chain(void);
/* D3D11: get pointer to ID3D11RenderTargetView object for rendering */
SOKOL_APP_API_DECL const void* sapp_d3d11_get_render_view(void);
/* D3D11: get pointer ID3D11RenderTargetView object for msaa-resolve (may return null) */
SOKOL_APP_API_DECL const void* sapp_d3d11_get_resolve_view(void);
/* D3D11: get pointer ID3D11DepthStencilView */
SOKOL_APP_API_DECL const void* sapp_d3d11_get_depth_stencil_view(void);
/* Win32: get the HWND window handle */
SOKOL_APP_API_DECL const void* sapp_win32_get_hwnd(void);
/* WebGPU: get WGPUDevice handle */
SOKOL_APP_API_DECL const void* sapp_wgpu_get_device(void);
/* WebGPU: get swapchain's WGPUTextureView handle for rendering */
SOKOL_APP_API_DECL const void* sapp_wgpu_get_render_view(void);
/* WebGPU: get swapchain's MSAA-resolve WGPUTextureView (may return null) */
SOKOL_APP_API_DECL const void* sapp_wgpu_get_resolve_view(void);
/* WebGPU: get swapchain's WGPUTextureView for the depth-stencil surface */
SOKOL_APP_API_DECL const void* sapp_wgpu_get_depth_stencil_view(void);
/* GL: get framebuffer object */
SOKOL_APP_API_DECL uint32_t sapp_gl_get_framebuffer(void);
/* GL: get major version (only valid for desktop GL) */
SOKOL_APP_API_DECL int sapp_gl_get_major_version(void);
/* GL: get minor version (only valid for desktop GL) */
SOKOL_APP_API_DECL int sapp_gl_get_minor_version(void);
/* Android: get native activity handle */
SOKOL_APP_API_DECL const void* sapp_android_get_native_activity(void);
#ifdef __cplusplus
} /* extern "C" */
/* reference-based equivalents for C++ */
inline void sapp_run(const sapp_desc& desc) { return sapp_run(&desc); }
#endif
// this WinRT specific hack is required when wWinMain is in a static library
#if defined(_MSC_VER) && defined(UNICODE)
#include <winapifamily.h>
#if defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
#pragma comment(linker, "/include:wWinMain")
#endif
#endif
#endif // SOKOL_APP_INCLUDED
// ██ ███ ███ ██████ ██ ███████ ███ ███ ███████ ███ ██ ████████ █████ ████████ ██ ██████ ███ ██
// ██ ████ ████ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
// ██ ██ ████ ██ ██████ ██ █████ ██ ████ ██ █████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ███████ ███████ ██ ██ ███████ ██ ████ ██ ██ ██ ██ ██ ██████ ██ ████
//
// >>implementation
#ifdef SOKOL_APP_IMPL
#define SOKOL_APP_IMPL_INCLUDED (1)
#if defined(SOKOL_MALLOC) || defined(SOKOL_CALLOC) || defined(SOKOL_FREE)
#error "SOKOL_MALLOC/CALLOC/FREE macros are no longer supported, please use sapp_desc.allocator to override memory allocation functions"
#endif
#include <stdlib.h> // malloc, free
#include <string.h> // memset, strncmp
#include <stddef.h> // size_t
#include <math.h> // roundf
// helper macros
#define _sapp_def(val, def) (((val) == 0) ? (def) : (val))
#define _sapp_absf(a) (((a)<0.0f)?-(a):(a))
#define _SAPP_MAX_TITLE_LENGTH (128)
#define _SAPP_FALLBACK_DEFAULT_WINDOW_WIDTH (640)
#define _SAPP_FALLBACK_DEFAULT_WINDOW_HEIGHT (480)
// NOTE: the pixel format values *must* be compatible with sg_pixel_format
#define _SAPP_PIXELFORMAT_RGBA8 (23)
#define _SAPP_PIXELFORMAT_BGRA8 (28)
#define _SAPP_PIXELFORMAT_DEPTH (43)
#define _SAPP_PIXELFORMAT_DEPTH_STENCIL (44)
// check if the config defines are alright
#if defined(__APPLE__)
// see https://clang.llvm.org/docs/LanguageExtensions.html#automatic-reference-counting
#if !defined(__cplusplus)
#if __has_feature(objc_arc) && !__has_feature(objc_arc_fields)
#error "sokol_app.h requires __has_feature(objc_arc_field) if ARC is enabled (use a more recent compiler version)"
#endif
#endif
#define _SAPP_APPLE (1)
#include <TargetConditionals.h>
#if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE
/* MacOS */
#define _SAPP_MACOS (1)
#if !defined(SOKOL_METAL) && !defined(SOKOL_GLCORE)
#error("sokol_app.h: unknown 3D API selected for MacOS, must be SOKOL_METAL or SOKOL_GLCORE")
#endif
#else
/* iOS or iOS Simulator */
#define _SAPP_IOS (1)
#if !defined(SOKOL_METAL) && !defined(SOKOL_GLES3)
#error("sokol_app.h: unknown 3D API selected for iOS, must be SOKOL_METAL or SOKOL_GLES3")
#endif
#endif
#elif defined(__EMSCRIPTEN__)
/* emscripten (asm.js or wasm) */
#define _SAPP_EMSCRIPTEN (1)
#if !defined(SOKOL_GLES3) && !defined(SOKOL_WGPU)
#error("sokol_app.h: unknown 3D API selected for emscripten, must be SOKOL_GLES3 or SOKOL_WGPU")
#endif
#elif defined(_WIN32)
/* Windows (D3D11 or GL) */
#define _SAPP_WIN32 (1)
#if !defined(SOKOL_D3D11) && !defined(SOKOL_GLCORE) && !defined(SOKOL_NOAPI)
#error("sokol_app.h: unknown 3D API selected for Win32, must be SOKOL_D3D11, SOKOL_GLCORE or SOKOL_NOAPI")
#endif
#elif defined(__ANDROID__)
/* Android */
#define _SAPP_ANDROID (1)
#if !defined(SOKOL_GLES3)
#error("sokol_app.h: unknown 3D API selected for Android, must be SOKOL_GLES3")
#endif
#if defined(SOKOL_NO_ENTRY)
#error("sokol_app.h: SOKOL_NO_ENTRY is not supported on Android")
#endif
#elif defined(__linux__) || defined(__unix__)
/* Linux */
#define _SAPP_LINUX (1)
#if defined(SOKOL_GLCORE)
#if !defined(SOKOL_FORCE_EGL)
#define _SAPP_GLX (1)
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#elif defined(SOKOL_GLES3)
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#else
#error("sokol_app.h: unknown 3D API selected for Linux, must be SOKOL_GLCORE, SOKOL_GLES3")
#endif
#else
#error "sokol_app.h: Unknown platform"
#endif
#if defined(SOKOL_GLCORE) || defined(SOKOL_GLES3)
#define _SAPP_ANY_GL (1)
#endif
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
#ifndef SOKOL_DEBUG
#ifndef NDEBUG
#define SOKOL_DEBUG
#endif
#endif
#ifndef SOKOL_ASSERT
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
#ifndef SOKOL_UNREACHABLE
#define SOKOL_UNREACHABLE SOKOL_ASSERT(false)
#endif
#ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static
#else
#define _SOKOL_PRIVATE static
#endif
#endif
#ifndef _SOKOL_UNUSED
#define _SOKOL_UNUSED(x) (void)(x)
#endif
#if defined(_SAPP_APPLE)
#if defined(SOKOL_METAL)
#import <Metal/Metal.h>
#import <MetalKit/MetalKit.h>
#endif
#if defined(_SAPP_MACOS)
#if defined(_SAPP_ANY_GL)
#ifndef GL_SILENCE_DEPRECATION
#define GL_SILENCE_DEPRECATION
#endif
#include <Cocoa/Cocoa.h>
#include <OpenGL/gl3.h>
#endif
#elif defined(_SAPP_IOS)
#import <UIKit/UIKit.h>
#if defined(_SAPP_ANY_GL)
#import <GLKit/GLKit.h>
#include <OpenGLES/ES3/gl.h>
#endif
#endif
#include <AvailabilityMacros.h>
#include <mach/mach_time.h>
#elif defined(_SAPP_EMSCRIPTEN)
#if defined(SOKOL_WGPU)
#include <webgpu/webgpu.h>
#endif
#if defined(SOKOL_GLES3)
#include <GLES3/gl3.h>
#endif
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#elif defined(_SAPP_WIN32)
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4201) /* nonstandard extension used: nameless struct/union */
#pragma warning(disable:4204) /* nonstandard extension used: non-constant aggregate initializer */
#pragma warning(disable:4054) /* 'type cast': from function pointer */
#pragma warning(disable:4055) /* 'type cast': from data pointer */
#pragma warning(disable:4505) /* unreferenced local function has been removed */
#pragma warning(disable:4115) /* /W4: 'ID3D11ModuleInstance': named type definition in parentheses (in d3d11.h) */
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <windowsx.h>
#include <shellapi.h>
#if !defined(SOKOL_NO_ENTRY) // if SOKOL_NO_ENTRY is defined, it's the applications' responsibility to use the right subsystem
#if defined(SOKOL_WIN32_FORCE_MAIN)
#pragma comment (linker, "/subsystem:console")
#else
#pragma comment (linker, "/subsystem:windows")
#endif
#endif
#include <stdio.h> /* freopen_s() */
#include <wchar.h> /* wcslen() */
#pragma comment (lib, "kernel32")
#pragma comment (lib, "user32")
#pragma comment (lib, "shell32") /* CommandLineToArgvW, DragQueryFileW, DragFinished */
#pragma comment (lib, "gdi32")
#if defined(SOKOL_D3D11)
#pragma comment (lib, "dxgi")
#pragma comment (lib, "d3d11")
#endif
#if defined(SOKOL_D3D11)
#ifndef D3D11_NO_HELPERS
#define D3D11_NO_HELPERS
#endif
#include <d3d11.h>
#include <dxgi.h>
// DXGI_SWAP_EFFECT_FLIP_DISCARD is only defined in newer Windows SDKs, so don't depend on it
#define _SAPP_DXGI_SWAP_EFFECT_FLIP_DISCARD (4)
#endif
#ifndef WM_MOUSEHWHEEL /* see https://github.com/floooh/sokol/issues/138 */
#define WM_MOUSEHWHEEL (0x020E)
#endif
#ifndef WM_DPICHANGED
#define WM_DPICHANGED (0x02E0)
#endif
#elif defined(_SAPP_ANDROID)
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <android/native_activity.h>
#include <android/looper.h>
#include <EGL/egl.h>
#include <GLES3/gl3.h>
#elif defined(_SAPP_LINUX)
#define GL_GLEXT_PROTOTYPES
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/keysym.h>
#include <X11/Xresource.h>
#include <X11/Xatom.h>
#include <X11/extensions/XInput2.h>
#include <X11/Xcursor/Xcursor.h>
#include <X11/cursorfont.h> /* XC_* font cursors */
#include <X11/Xmd.h> /* CARD32 */
#if !defined(_SAPP_GLX)
#include <EGL/egl.h>
#endif
#include <dlfcn.h> /* dlopen, dlsym, dlclose */
#include <limits.h> /* LONG_MAX */
#include <pthread.h> /* only used a linker-guard, search for _sapp_linux_run() and see first comment */
#include <time.h>
#endif
#if defined(_SAPP_APPLE)
// this is ARC compatible
#if defined(__cplusplus)
#define _SAPP_CLEAR_ARC_STRUCT(type, item) { item = type(); }
#else
#define _SAPP_CLEAR_ARC_STRUCT(type, item) { item = (type) { 0 }; }
#endif
#else
#define _SAPP_CLEAR_ARC_STRUCT(type, item) { _sapp_clear(&item, sizeof(item)); }
#endif
// ███████ ██████ █████ ███ ███ ███████ ████████ ██ ███ ███ ██ ███ ██ ██████
// ██ ██ ██ ██ ██ ████ ████ ██ ██ ██ ████ ████ ██ ████ ██ ██
// █████ ██████ ███████ ██ ████ ██ █████ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ███
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ████ ██████
//
// >>frame timing
#define _SAPP_RING_NUM_SLOTS (256)
typedef struct {
int head;
int tail;
double buf[_SAPP_RING_NUM_SLOTS];
} _sapp_ring_t;
_SOKOL_PRIVATE int _sapp_ring_idx(int i) {
return i % _SAPP_RING_NUM_SLOTS;
}
_SOKOL_PRIVATE void _sapp_ring_init(_sapp_ring_t* ring) {
ring->head = 0;
ring->tail = 0;
}
_SOKOL_PRIVATE bool _sapp_ring_full(_sapp_ring_t* ring) {
return _sapp_ring_idx(ring->head + 1) == ring->tail;
}
_SOKOL_PRIVATE bool _sapp_ring_empty(_sapp_ring_t* ring) {
return ring->head == ring->tail;
}
_SOKOL_PRIVATE int _sapp_ring_count(_sapp_ring_t* ring) {
int count;
if (ring->head >= ring->tail) {
count = ring->head - ring->tail;
}
else {
count = (ring->head + _SAPP_RING_NUM_SLOTS) - ring->tail;
}
SOKOL_ASSERT((count >= 0) && (count < _SAPP_RING_NUM_SLOTS));
return count;
}
_SOKOL_PRIVATE void _sapp_ring_enqueue(_sapp_ring_t* ring, double val) {
SOKOL_ASSERT(!_sapp_ring_full(ring));
ring->buf[ring->head] = val;
ring->head = _sapp_ring_idx(ring->head + 1);
}
_SOKOL_PRIVATE double _sapp_ring_dequeue(_sapp_ring_t* ring) {
SOKOL_ASSERT(!_sapp_ring_empty(ring));
double val = ring->buf[ring->tail];
ring->tail = _sapp_ring_idx(ring->tail + 1);
return val;
}
/*
NOTE:
Q: Why not use CAMetalDrawable.presentedTime on macOS and iOS?
A: The value appears to be highly unstable during the first few
seconds, sometimes several frames are dropped in sequence, or
switch between 120 and 60 Hz for a few frames. Simply measuring
and averaging the frame time yielded a more stable frame duration.
Maybe switching to CVDisplayLink would yield better results.
Until then just measure the time.
*/
typedef struct {
#if defined(_SAPP_APPLE)
struct {
mach_timebase_info_data_t timebase;
uint64_t start;
} mach;
#elif defined(_SAPP_EMSCRIPTEN)
// empty
#elif defined(_SAPP_WIN32)
struct {
LARGE_INTEGER freq;
LARGE_INTEGER start;
} win;
#else // Linux, Android, ...
#ifdef CLOCK_MONOTONIC
#define _SAPP_CLOCK_MONOTONIC CLOCK_MONOTONIC
#else
// on some embedded platforms, CLOCK_MONOTONIC isn't defined
#define _SAPP_CLOCK_MONOTONIC (1)
#endif
struct {
uint64_t start;
} posix;
#endif
} _sapp_timestamp_t;
_SOKOL_PRIVATE int64_t _sapp_int64_muldiv(int64_t value, int64_t numer, int64_t denom) {
int64_t q = value / denom;
int64_t r = value % denom;
return q * numer + r * numer / denom;
}
_SOKOL_PRIVATE void _sapp_timestamp_init(_sapp_timestamp_t* ts) {
#if defined(_SAPP_APPLE)
mach_timebase_info(&ts->mach.timebase);
ts->mach.start = mach_absolute_time();
#elif defined(_SAPP_EMSCRIPTEN)
(void)ts;
#elif defined(_SAPP_WIN32)
QueryPerformanceFrequency(&ts->win.freq);
QueryPerformanceCounter(&ts->win.start);
#else
struct timespec tspec;
clock_gettime(_SAPP_CLOCK_MONOTONIC, &tspec);
ts->posix.start = (uint64_t)tspec.tv_sec*1000000000 + (uint64_t)tspec.tv_nsec;
#endif
}
_SOKOL_PRIVATE double _sapp_timestamp_now(_sapp_timestamp_t* ts) {
#if defined(_SAPP_APPLE)
const uint64_t traw = mach_absolute_time() - ts->mach.start;
const uint64_t now = (uint64_t) _sapp_int64_muldiv((int64_t)traw, (int64_t)ts->mach.timebase.numer, (int64_t)ts->mach.timebase.denom);
return (double)now / 1000000000.0;
#elif defined(_SAPP_EMSCRIPTEN)
(void)ts;
SOKOL_ASSERT(false);
return 0.0;
#elif defined(_SAPP_WIN32)
LARGE_INTEGER qpc;
QueryPerformanceCounter(&qpc);
const uint64_t now = (uint64_t)_sapp_int64_muldiv(qpc.QuadPart - ts->win.start.QuadPart, 1000000000, ts->win.freq.QuadPart);
return (double)now / 1000000000.0;
#else
struct timespec tspec;
clock_gettime(_SAPP_CLOCK_MONOTONIC, &tspec);
const uint64_t now = ((uint64_t)tspec.tv_sec*1000000000 + (uint64_t)tspec.tv_nsec) - ts->posix.start;
return (double)now / 1000000000.0;
#endif
}
typedef struct {
double last;
double accum;
double avg;
int spike_count;
int num;
_sapp_timestamp_t timestamp;
_sapp_ring_t ring;
} _sapp_timing_t;
_SOKOL_PRIVATE void _sapp_timing_reset(_sapp_timing_t* t) {
t->last = 0.0;
t->accum = 0.0;
t->spike_count = 0;
t->num = 0;
_sapp_ring_init(&t->ring);
}
_SOKOL_PRIVATE void _sapp_timing_init(_sapp_timing_t* t) {
t->avg = 1.0 / 60.0; // dummy value until first actual value is available
_sapp_timing_reset(t);
_sapp_timestamp_init(&t->timestamp);
}
_SOKOL_PRIVATE void _sapp_timing_put(_sapp_timing_t* t, double dur) {
// arbitrary upper limit to ignore outliers (e.g. during window resizing, or debugging)
double min_dur = 0.0;
double max_dur = 0.1;
// if we have enough samples for a useful average, use a much tighter 'valid window'
if (_sapp_ring_full(&t->ring)) {
min_dur = t->avg * 0.8;
max_dur = t->avg * 1.2;
}
if ((dur < min_dur) || (dur > max_dur)) {
t->spike_count++;
// if there have been many spikes in a row, the display refresh rate
// might have changed, so a timing reset is needed
if (t->spike_count > 20) {
_sapp_timing_reset(t);
}
return;
}
if (_sapp_ring_full(&t->ring)) {
double old_val = _sapp_ring_dequeue(&t->ring);
t->accum -= old_val;
t->num -= 1;
}
_sapp_ring_enqueue(&t->ring, dur);
t->accum += dur;
t->num += 1;
SOKOL_ASSERT(t->num > 0);
t->avg = t->accum / t->num;
t->spike_count = 0;
}
_SOKOL_PRIVATE void _sapp_timing_discontinuity(_sapp_timing_t* t) {
t->last = 0.0;
}
_SOKOL_PRIVATE void _sapp_timing_measure(_sapp_timing_t* t) {
const double now = _sapp_timestamp_now(&t->timestamp);
if (t->last > 0.0) {
double dur = now - t->last;
_sapp_timing_put(t, dur);
}
t->last = now;
}
_SOKOL_PRIVATE void _sapp_timing_external(_sapp_timing_t* t, double now) {
if (t->last > 0.0) {
double dur = now - t->last;
_sapp_timing_put(t, dur);
}
t->last = now;
}
_SOKOL_PRIVATE double _sapp_timing_get_avg(_sapp_timing_t* t) {
return t->avg;
}
// ███████ ████████ ██████ ██ ██ ██████ ████████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██████ ██ ██ ██ ██ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██ ██ ██████ ██████ ██ ███████
//
// >> structs
#if defined(_SAPP_MACOS)
@interface _sapp_macos_app_delegate : NSObject<NSApplicationDelegate>
@end
@interface _sapp_macos_window : NSWindow
@end
@interface _sapp_macos_window_delegate : NSObject<NSWindowDelegate>
@end
#if defined(SOKOL_METAL)
@interface _sapp_macos_view : MTKView
@end
#elif defined(SOKOL_GLCORE)
@interface _sapp_macos_view : NSOpenGLView
- (void)timerFired:(id)sender;
@end
#endif // SOKOL_GLCORE
typedef struct {
uint32_t flags_changed_store;
uint8_t mouse_buttons;
NSWindow* window;
NSTrackingArea* tracking_area;
id keyup_monitor;
_sapp_macos_app_delegate* app_dlg;
_sapp_macos_window_delegate* win_dlg;
_sapp_macos_view* view;
NSCursor* cursors[_SAPP_MOUSECURSOR_NUM];
#if defined(SOKOL_METAL)
id<MTLDevice> mtl_device;
#endif
} _sapp_macos_t;
#endif // _SAPP_MACOS
#if defined(_SAPP_IOS)
@interface _sapp_app_delegate : NSObject<UIApplicationDelegate>
@end
@interface _sapp_textfield_dlg : NSObject<UITextFieldDelegate>
- (void)keyboardWasShown:(NSNotification*)notif;
- (void)keyboardWillBeHidden:(NSNotification*)notif;
- (void)keyboardDidChangeFrame:(NSNotification*)notif;
@end
#if defined(SOKOL_METAL)
@interface _sapp_ios_view : MTKView;
@end
#else
@interface _sapp_ios_view : GLKView
@end
#endif
typedef struct {
UIWindow* window;
_sapp_ios_view* view;
UITextField* textfield;
_sapp_textfield_dlg* textfield_dlg;
#if defined(SOKOL_METAL)
UIViewController* view_ctrl;
id<MTLDevice> mtl_device;
#else
GLKViewController* view_ctrl;
EAGLContext* eagl_ctx;
#endif
bool suspended;
} _sapp_ios_t;
#endif // _SAPP_IOS
#if defined(_SAPP_EMSCRIPTEN)
#if defined(SOKOL_WGPU)
typedef struct {
WGPUInstance instance;
WGPUAdapter adapter;
WGPUDevice device;
WGPUTextureFormat render_format;
WGPUSurface surface;
WGPUSwapChain swapchain;
WGPUTexture msaa_tex;
WGPUTextureView msaa_view;
WGPUTexture depth_stencil_tex;
WGPUTextureView depth_stencil_view;
WGPUTextureView swapchain_view;
bool async_init_done;
} _sapp_wgpu_t;
#endif
typedef struct {
bool mouse_lock_requested;
uint16_t mouse_buttons;
} _sapp_emsc_t;
#endif // _SAPP_EMSCRIPTEN
#if defined(SOKOL_D3D11) && defined(_SAPP_WIN32)
typedef struct {
ID3D11Device* device;
ID3D11DeviceContext* device_context;
ID3D11Texture2D* rt;
ID3D11RenderTargetView* rtv;
ID3D11Texture2D* msaa_rt;
ID3D11RenderTargetView* msaa_rtv;
ID3D11Texture2D* ds;
ID3D11DepthStencilView* dsv;
DXGI_SWAP_CHAIN_DESC swap_chain_desc;
IDXGISwapChain* swap_chain;
IDXGIDevice1* dxgi_device;
bool use_dxgi_frame_stats;
UINT sync_refresh_count;
} _sapp_d3d11_t;
#endif
#if defined(_SAPP_WIN32)
#ifndef DPI_ENUMS_DECLARED
typedef enum PROCESS_DPI_AWARENESS
{
PROCESS_DPI_UNAWARE = 0,
PROCESS_SYSTEM_DPI_AWARE = 1,
PROCESS_PER_MONITOR_DPI_AWARE = 2
} PROCESS_DPI_AWARENESS;
typedef enum MONITOR_DPI_TYPE {
MDT_EFFECTIVE_DPI = 0,
MDT_ANGULAR_DPI = 1,
MDT_RAW_DPI = 2,
MDT_DEFAULT = MDT_EFFECTIVE_DPI
} MONITOR_DPI_TYPE;
#endif // DPI_ENUMS_DECLARED
typedef struct {
bool aware;
float content_scale;
float window_scale;
float mouse_scale;
} _sapp_win32_dpi_t;
typedef struct {
HWND hwnd;
HMONITOR hmonitor;
HDC dc;
HICON big_icon;
HICON small_icon;
HCURSOR cursors[_SAPP_MOUSECURSOR_NUM];
UINT orig_codepage;
LONG mouse_locked_x, mouse_locked_y;
RECT stored_window_rect; // used to restore window pos/size when toggling fullscreen => windowed
bool is_win10_or_greater;
bool in_create_window;
bool iconified;
bool mouse_tracked;
uint8_t mouse_capture_mask;
_sapp_win32_dpi_t dpi;
bool raw_input_mousepos_valid;
LONG raw_input_mousepos_x;
LONG raw_input_mousepos_y;
uint8_t raw_input_data[256];
} _sapp_win32_t;
#if defined(SOKOL_GLCORE)
#define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
#define WGL_SUPPORT_OPENGL_ARB 0x2010
#define WGL_DRAW_TO_WINDOW_ARB 0x2001
#define WGL_PIXEL_TYPE_ARB 0x2013
#define WGL_TYPE_RGBA_ARB 0x202b
#define WGL_ACCELERATION_ARB 0x2003
#define WGL_NO_ACCELERATION_ARB 0x2025
#define WGL_RED_BITS_ARB 0x2015
#define WGL_GREEN_BITS_ARB 0x2017
#define WGL_BLUE_BITS_ARB 0x2019
#define WGL_ALPHA_BITS_ARB 0x201b
#define WGL_DEPTH_BITS_ARB 0x2022
#define WGL_STENCIL_BITS_ARB 0x2023
#define WGL_DOUBLE_BUFFER_ARB 0x2011
#define WGL_SAMPLES_ARB 0x2042
#define WGL_CONTEXT_DEBUG_BIT_ARB 0x00000001
#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
#define WGL_CONTEXT_FLAGS_ARB 0x2094
#define ERROR_INVALID_VERSION_ARB 0x2095
#define ERROR_INVALID_PROFILE_ARB 0x2096
#define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054
typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC)(int);
typedef BOOL (WINAPI * PFNWGLGETPIXELFORMATATTRIBIVARBPROC)(HDC,int,int,UINT,const int*,int*);
typedef const char* (WINAPI * PFNWGLGETEXTENSIONSSTRINGEXTPROC)(void);
typedef const char* (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC)(HDC);
typedef HGLRC (WINAPI * PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC,HGLRC,const int*);
typedef HGLRC (WINAPI * PFN_wglCreateContext)(HDC);
typedef BOOL (WINAPI * PFN_wglDeleteContext)(HGLRC);
typedef PROC (WINAPI * PFN_wglGetProcAddress)(LPCSTR);
typedef HDC (WINAPI * PFN_wglGetCurrentDC)(void);
typedef BOOL (WINAPI * PFN_wglMakeCurrent)(HDC,HGLRC);
typedef struct {
HINSTANCE opengl32;
HGLRC gl_ctx;
PFN_wglCreateContext CreateContext;
PFN_wglDeleteContext DeleteContext;
PFN_wglGetProcAddress GetProcAddress;
PFN_wglGetCurrentDC GetCurrentDC;
PFN_wglMakeCurrent MakeCurrent;
PFNWGLSWAPINTERVALEXTPROC SwapIntervalEXT;
PFNWGLGETPIXELFORMATATTRIBIVARBPROC GetPixelFormatAttribivARB;
PFNWGLGETEXTENSIONSSTRINGEXTPROC GetExtensionsStringEXT;
PFNWGLGETEXTENSIONSSTRINGARBPROC GetExtensionsStringARB;
PFNWGLCREATECONTEXTATTRIBSARBPROC CreateContextAttribsARB;
// special case glGetIntegerv
void (WINAPI *GetIntegerv)(uint32_t pname, int32_t* data);
bool ext_swap_control;
bool arb_multisample;
bool arb_pixel_format;
bool arb_create_context;
bool arb_create_context_profile;
HWND msg_hwnd;
HDC msg_dc;
} _sapp_wgl_t;
#endif // SOKOL_GLCORE
#endif // _SAPP_WIN32
#if defined(_SAPP_ANDROID)
typedef enum {
_SOKOL_ANDROID_MSG_CREATE,
_SOKOL_ANDROID_MSG_RESUME,
_SOKOL_ANDROID_MSG_PAUSE,
_SOKOL_ANDROID_MSG_FOCUS,
_SOKOL_ANDROID_MSG_NO_FOCUS,
_SOKOL_ANDROID_MSG_SET_NATIVE_WINDOW,
_SOKOL_ANDROID_MSG_SET_INPUT_QUEUE,
_SOKOL_ANDROID_MSG_DESTROY,
} _sapp_android_msg_t;
typedef struct {
pthread_t thread;
pthread_mutex_t mutex;
pthread_cond_t cond;
int read_from_main_fd;
int write_from_main_fd;
} _sapp_android_pt_t;
typedef struct {
ANativeWindow* window;
AInputQueue* input;
} _sapp_android_resources_t;
typedef struct {
ANativeActivity* activity;
_sapp_android_pt_t pt;
_sapp_android_resources_t pending;
_sapp_android_resources_t current;
ALooper* looper;
bool is_thread_started;
bool is_thread_stopping;
bool is_thread_stopped;
bool has_created;
bool has_resumed;
bool has_focus;
EGLConfig config;
EGLDisplay display;
EGLContext context;
EGLSurface surface;
} _sapp_android_t;
#endif // _SAPP_ANDROID
#if defined(_SAPP_LINUX)
#define _SAPP_X11_XDND_VERSION (5)
#define _SAPP_X11_MAX_X11_KEYCODES (256)
#define GLX_VENDOR 1
#define GLX_RGBA_BIT 0x00000001
#define GLX_WINDOW_BIT 0x00000001
#define GLX_DRAWABLE_TYPE 0x8010
#define GLX_RENDER_TYPE 0x8011
#define GLX_DOUBLEBUFFER 5
#define GLX_RED_SIZE 8
#define GLX_GREEN_SIZE 9
#define GLX_BLUE_SIZE 10
#define GLX_ALPHA_SIZE 11
#define GLX_DEPTH_SIZE 12
#define GLX_STENCIL_SIZE 13
#define GLX_SAMPLES 0x186a1
#define GLX_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
#define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
#define GLX_CONTEXT_FLAGS_ARB 0x2094
typedef XID GLXWindow;
typedef XID GLXDrawable;
typedef struct __GLXFBConfig* GLXFBConfig;
typedef struct __GLXcontext* GLXContext;
typedef void (*__GLXextproc)(void);
typedef int (*PFNGLXGETFBCONFIGATTRIBPROC)(Display*,GLXFBConfig,int,int*);
typedef const char* (*PFNGLXGETCLIENTSTRINGPROC)(Display*,int);
typedef Bool (*PFNGLXQUERYEXTENSIONPROC)(Display*,int*,int*);
typedef Bool (*PFNGLXQUERYVERSIONPROC)(Display*,int*,int*);
typedef void (*PFNGLXDESTROYCONTEXTPROC)(Display*,GLXContext);
typedef Bool (*PFNGLXMAKECURRENTPROC)(Display*,GLXDrawable,GLXContext);
typedef void (*PFNGLXSWAPBUFFERSPROC)(Display*,GLXDrawable);
typedef const char* (*PFNGLXQUERYEXTENSIONSSTRINGPROC)(Display*,int);
typedef GLXFBConfig* (*PFNGLXGETFBCONFIGSPROC)(Display*,int,int*);
typedef __GLXextproc (* PFNGLXGETPROCADDRESSPROC)(const char *procName);
typedef void (*PFNGLXSWAPINTERVALEXTPROC)(Display*,GLXDrawable,int);
typedef XVisualInfo* (*PFNGLXGETVISUALFROMFBCONFIGPROC)(Display*,GLXFBConfig);
typedef GLXWindow (*PFNGLXCREATEWINDOWPROC)(Display*,GLXFBConfig,Window,const int*);
typedef void (*PFNGLXDESTROYWINDOWPROC)(Display*,GLXWindow);
typedef int (*PFNGLXSWAPINTERVALMESAPROC)(int);
typedef GLXContext (*PFNGLXCREATECONTEXTATTRIBSARBPROC)(Display*,GLXFBConfig,GLXContext,Bool,const int*);
typedef struct {
bool available;
int major_opcode;
int event_base;
int error_base;
int major;
int minor;
} _sapp_xi_t;
typedef struct {
int version;
Window source;
Atom format;
Atom XdndAware;
Atom XdndEnter;
Atom XdndPosition;
Atom XdndStatus;
Atom XdndActionCopy;
Atom XdndDrop;
Atom XdndFinished;
Atom XdndSelection;
Atom XdndTypeList;
Atom text_uri_list;
} _sapp_xdnd_t;
typedef struct {
uint8_t mouse_buttons;
Display* display;
int screen;
Window root;
Colormap colormap;
Window window;
Cursor hidden_cursor;
Cursor cursors[_SAPP_MOUSECURSOR_NUM];
int window_state;
float dpi;
unsigned char error_code;
Atom UTF8_STRING;
Atom WM_PROTOCOLS;
Atom WM_DELETE_WINDOW;
Atom WM_STATE;
Atom NET_WM_NAME;
Atom NET_WM_ICON_NAME;
Atom NET_WM_ICON;
Atom NET_WM_STATE;
Atom NET_WM_STATE_FULLSCREEN;
_sapp_xi_t xi;
_sapp_xdnd_t xdnd;
// XLib manual says keycodes are in the range [8, 255] inclusive.
// https://tronche.com/gui/x/xlib/input/keyboard-encoding.html
bool key_repeat[_SAPP_X11_MAX_X11_KEYCODES];
} _sapp_x11_t;
#if defined(_SAPP_GLX)
typedef struct {
void* libgl;
int major;
int minor;
int event_base;
int error_base;
GLXContext ctx;
GLXWindow window;
// GLX 1.3 functions
PFNGLXGETFBCONFIGSPROC GetFBConfigs;
PFNGLXGETFBCONFIGATTRIBPROC GetFBConfigAttrib;
PFNGLXGETCLIENTSTRINGPROC GetClientString;
PFNGLXQUERYEXTENSIONPROC QueryExtension;
PFNGLXQUERYVERSIONPROC QueryVersion;
PFNGLXDESTROYCONTEXTPROC DestroyContext;
PFNGLXMAKECURRENTPROC MakeCurrent;
PFNGLXSWAPBUFFERSPROC SwapBuffers;
PFNGLXQUERYEXTENSIONSSTRINGPROC QueryExtensionsString;
PFNGLXGETVISUALFROMFBCONFIGPROC GetVisualFromFBConfig;
PFNGLXCREATEWINDOWPROC CreateWindow;
PFNGLXDESTROYWINDOWPROC DestroyWindow;
// GLX 1.4 and extension functions
PFNGLXGETPROCADDRESSPROC GetProcAddress;
PFNGLXGETPROCADDRESSPROC GetProcAddressARB;
PFNGLXSWAPINTERVALEXTPROC SwapIntervalEXT;
PFNGLXSWAPINTERVALMESAPROC SwapIntervalMESA;
PFNGLXCREATECONTEXTATTRIBSARBPROC CreateContextAttribsARB;
// special case glGetIntegerv
void (*GetIntegerv)(uint32_t pname, int32_t* data);
// extension availability
bool EXT_swap_control;
bool MESA_swap_control;
bool ARB_multisample;
bool ARB_create_context;
bool ARB_create_context_profile;
} _sapp_glx_t;
#else
typedef struct {
EGLDisplay display;
EGLContext context;
EGLSurface surface;
} _sapp_egl_t;
#endif // _SAPP_GLX
#endif // _SAPP_LINUX
#if defined(_SAPP_ANY_GL)
typedef struct {
uint32_t framebuffer;
} _sapp_gl_t;
#endif
typedef struct {
bool enabled;
int buf_size;
char* buffer;
} _sapp_clipboard_t;
typedef struct {
bool enabled;
int max_files;
int max_path_length;
int num_files;
int buf_size;
char* buffer;
} _sapp_drop_t;
typedef struct {
float x, y;
float dx, dy;
bool shown;
bool locked;
bool pos_valid;
sapp_mouse_cursor current_cursor;
} _sapp_mouse_t;
typedef struct {
sapp_desc desc;
bool valid;
bool fullscreen;
bool first_frame;
bool init_called;
bool cleanup_called;
bool quit_requested;
bool quit_ordered;
bool event_consumed;
bool html5_ask_leave_site;
bool onscreen_keyboard_shown;
int window_width;
int window_height;
int framebuffer_width;
int framebuffer_height;
int sample_count;
int swap_interval;
float dpi_scale;
uint64_t frame_count;
_sapp_timing_t timing;
sapp_event event;
_sapp_mouse_t mouse;
_sapp_clipboard_t clipboard;
_sapp_drop_t drop;
sapp_icon_desc default_icon_desc;
uint32_t* default_icon_pixels;
#if defined(_SAPP_MACOS)
_sapp_macos_t macos;
#elif defined(_SAPP_IOS)
_sapp_ios_t ios;
#elif defined(_SAPP_EMSCRIPTEN)
_sapp_emsc_t emsc;
#if defined(SOKOL_WGPU)
_sapp_wgpu_t wgpu;
#endif
#elif defined(_SAPP_WIN32)
_sapp_win32_t win32;
#if defined(SOKOL_D3D11)
_sapp_d3d11_t d3d11;
#elif defined(SOKOL_GLCORE)
_sapp_wgl_t wgl;
#endif
#elif defined(_SAPP_ANDROID)
_sapp_android_t android;
#elif defined(_SAPP_LINUX)
_sapp_x11_t x11;
#if defined(_SAPP_GLX)
_sapp_glx_t glx;
#else
_sapp_egl_t egl;
#endif
#endif
#if defined(_SAPP_ANY_GL)
_sapp_gl_t gl;
#endif
char html5_canvas_selector[_SAPP_MAX_TITLE_LENGTH];
char window_title[_SAPP_MAX_TITLE_LENGTH]; // UTF-8
wchar_t window_title_wide[_SAPP_MAX_TITLE_LENGTH]; // UTF-32 or UCS-2 */
sapp_keycode keycodes[SAPP_MAX_KEYCODES];
} _sapp_t;
static _sapp_t _sapp;
// ██ ██████ ██████ ██████ ██ ███ ██ ██████
// ██ ██ ██ ██ ██ ██ ████ ██ ██
// ██ ██ ██ ██ ███ ██ ███ ██ ██ ██ ██ ██ ███
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██████ ██████ ██████ ██ ██ ████ ██████
//
// >>logging
#if defined(SOKOL_DEBUG)
#define _SAPP_LOGITEM_XMACRO(item,msg) #item ": " msg,
static const char* _sapp_log_messages[] = {
_SAPP_LOG_ITEMS
};
#undef _SAPP_LOGITEM_XMACRO
#endif // SOKOL_DEBUG
#define _SAPP_PANIC(code) _sapp_log(SAPP_LOGITEM_ ##code, 0, 0, __LINE__)
#define _SAPP_ERROR(code) _sapp_log(SAPP_LOGITEM_ ##code, 1, 0, __LINE__)
#define _SAPP_WARN(code) _sapp_log(SAPP_LOGITEM_ ##code, 2, 0, __LINE__)
#define _SAPP_INFO(code) _sapp_log(SAPP_LOGITEM_ ##code, 3, 0, __LINE__)
static void _sapp_log(sapp_log_item log_item, uint32_t log_level, const char* msg, uint32_t line_nr) {
if (_sapp.desc.logger.func) {
const char* filename = 0;
#if defined(SOKOL_DEBUG)
filename = __FILE__;
if (0 == msg) {
msg = _sapp_log_messages[log_item];
}
#endif
_sapp.desc.logger.func("sapp", log_level, log_item, msg, line_nr, filename, _sapp.desc.logger.user_data);
}
else {
// for log level PANIC it would be 'undefined behaviour' to continue
if (log_level == 0) {
abort();
}
}
}
// ███ ███ ███████ ███ ███ ██████ ██████ ██ ██
// ████ ████ ██ ████ ████ ██ ██ ██ ██ ██ ██
// ██ ████ ██ █████ ██ ████ ██ ██ ██ ██████ ████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ██ ██ ██████ ██ ██ ██
//
// >>memory
_SOKOL_PRIVATE void _sapp_clear(void* ptr, size_t size) {
SOKOL_ASSERT(ptr && (size > 0));
memset(ptr, 0, size);
}
_SOKOL_PRIVATE void* _sapp_malloc(size_t size) {
SOKOL_ASSERT(size > 0);
void* ptr;
if (_sapp.desc.allocator.alloc_fn) {
ptr = _sapp.desc.allocator.alloc_fn(size, _sapp.desc.allocator.user_data);
} else {
ptr = malloc(size);
}
if (0 == ptr) {
_SAPP_PANIC(MALLOC_FAILED);
}
return ptr;
}
_SOKOL_PRIVATE void* _sapp_malloc_clear(size_t size) {
void* ptr = _sapp_malloc(size);
_sapp_clear(ptr, size);
return ptr;
}
_SOKOL_PRIVATE void _sapp_free(void* ptr) {
if (_sapp.desc.allocator.free_fn) {
_sapp.desc.allocator.free_fn(ptr, _sapp.desc.allocator.user_data);
}
else {
free(ptr);
}
}
// ██ ██ ███████ ██ ██████ ███████ ██████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ █████ ██ ██████ █████ ██████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ███████ ██ ███████ ██ ██ ███████
//
// >>helpers
_SOKOL_PRIVATE void _sapp_call_init(void) {
if (_sapp.desc.init_cb) {
_sapp.desc.init_cb();
}
else if (_sapp.desc.init_userdata_cb) {
_sapp.desc.init_userdata_cb(_sapp.desc.user_data);
}
_sapp.init_called = true;
}
_SOKOL_PRIVATE void _sapp_call_frame(void) {
if (_sapp.init_called && !_sapp.cleanup_called) {
if (_sapp.desc.frame_cb) {
_sapp.desc.frame_cb();
}
else if (_sapp.desc.frame_userdata_cb) {
_sapp.desc.frame_userdata_cb(_sapp.desc.user_data);
}
}
}
_SOKOL_PRIVATE void _sapp_call_cleanup(void) {
if (!_sapp.cleanup_called) {
if (_sapp.desc.cleanup_cb) {
_sapp.desc.cleanup_cb();
}
else if (_sapp.desc.cleanup_userdata_cb) {
_sapp.desc.cleanup_userdata_cb(_sapp.desc.user_data);
}
_sapp.cleanup_called = true;
}
}
_SOKOL_PRIVATE bool _sapp_call_event(const sapp_event* e) {
if (!_sapp.cleanup_called) {
if (_sapp.desc.event_cb) {
_sapp.desc.event_cb(e);
}
else if (_sapp.desc.event_userdata_cb) {
_sapp.desc.event_userdata_cb(e, _sapp.desc.user_data);
}
}
if (_sapp.event_consumed) {
_sapp.event_consumed = false;
return true;
}
else {
return false;
}
}
_SOKOL_PRIVATE char* _sapp_dropped_file_path_ptr(int index) {
SOKOL_ASSERT(_sapp.drop.buffer);
SOKOL_ASSERT((index >= 0) && (index <= _sapp.drop.max_files));
int offset = index * _sapp.drop.max_path_length;
SOKOL_ASSERT(offset < _sapp.drop.buf_size);
return &_sapp.drop.buffer[offset];
}
/* Copy a string into a fixed size buffer with guaranteed zero-
termination.
Return false if the string didn't fit into the buffer and had to be clamped.
FIXME: Currently UTF-8 strings might become invalid if the string
is clamped, because the last zero-byte might be written into
the middle of a multi-byte sequence.
*/
_SOKOL_PRIVATE bool _sapp_strcpy(const char* src, char* dst, int max_len) {
SOKOL_ASSERT(src && dst && (max_len > 0));
char* const end = &(dst[max_len-1]);
char c = 0;
for (int i = 0; i < max_len; i++) {
c = *src;
if (c != 0) {
src++;
}
*dst++ = c;
}
/* truncated? */
if (c != 0) {
*end = 0;
return false;
}
else {
return true;
}
}
_SOKOL_PRIVATE sapp_desc _sapp_desc_defaults(const sapp_desc* desc) {
SOKOL_ASSERT((desc->allocator.alloc_fn && desc->allocator.free_fn) || (!desc->allocator.alloc_fn && !desc->allocator.free_fn));
sapp_desc res = *desc;
res.sample_count = _sapp_def(res.sample_count, 1);
res.swap_interval = _sapp_def(res.swap_interval, 1);
// NOTE: can't patch the default for gl_major_version and gl_minor_version
// independently, because a desired version 4.0 would be patched to 4.2
// (or expressed differently: zero is a valid value for gl_minor_version
// and can't be used to indicate 'default')
if (0 == res.gl_major_version) {
#if defined(_SAPP_APPLE)
res.gl_major_version = 4;
res.gl_minor_version = 1;
#else
res.gl_major_version = 4;
res.gl_minor_version = 3;
#endif
}
res.html5_canvas_name = _sapp_def(res.html5_canvas_name, "canvas");
res.clipboard_size = _sapp_def(res.clipboard_size, 8192);
res.max_dropped_files = _sapp_def(res.max_dropped_files, 1);
res.max_dropped_file_path_length = _sapp_def(res.max_dropped_file_path_length, 2048);
res.window_title = _sapp_def(res.window_title, "sokol_app");
return res;
}
_SOKOL_PRIVATE void _sapp_init_state(const sapp_desc* desc) {
SOKOL_ASSERT(desc);
SOKOL_ASSERT(desc->width >= 0);
SOKOL_ASSERT(desc->height >= 0);
SOKOL_ASSERT(desc->sample_count >= 0);
SOKOL_ASSERT(desc->swap_interval >= 0);
SOKOL_ASSERT(desc->clipboard_size >= 0);
SOKOL_ASSERT(desc->max_dropped_files >= 0);
SOKOL_ASSERT(desc->max_dropped_file_path_length >= 0);
_SAPP_CLEAR_ARC_STRUCT(_sapp_t, _sapp);
_sapp.desc = _sapp_desc_defaults(desc);
_sapp.first_frame = true;
// NOTE: _sapp.desc.width/height may be 0! Platform backends need to deal with this
_sapp.window_width = _sapp.desc.width;
_sapp.window_height = _sapp.desc.height;
_sapp.framebuffer_width = _sapp.window_width;
_sapp.framebuffer_height = _sapp.window_height;
_sapp.sample_count = _sapp.desc.sample_count;
_sapp.swap_interval = _sapp.desc.swap_interval;
_sapp.html5_canvas_selector[0] = '#';
_sapp_strcpy(_sapp.desc.html5_canvas_name, &_sapp.html5_canvas_selector[1], sizeof(_sapp.html5_canvas_selector) - 1);
_sapp.desc.html5_canvas_name = &_sapp.html5_canvas_selector[1];
_sapp.html5_ask_leave_site = _sapp.desc.html5_ask_leave_site;
_sapp.clipboard.enabled = _sapp.desc.enable_clipboard;
if (_sapp.clipboard.enabled) {
_sapp.clipboard.buf_size = _sapp.desc.clipboard_size;
_sapp.clipboard.buffer = (char*) _sapp_malloc_clear((size_t)_sapp.clipboard.buf_size);
}
_sapp.drop.enabled = _sapp.desc.enable_dragndrop;
if (_sapp.drop.enabled) {
_sapp.drop.max_files = _sapp.desc.max_dropped_files;
_sapp.drop.max_path_length = _sapp.desc.max_dropped_file_path_length;
_sapp.drop.buf_size = _sapp.drop.max_files * _sapp.drop.max_path_length;
_sapp.drop.buffer = (char*) _sapp_malloc_clear((size_t)_sapp.drop.buf_size);
}
_sapp_strcpy(_sapp.desc.window_title, _sapp.window_title, sizeof(_sapp.window_title));
_sapp.desc.window_title = _sapp.window_title;
_sapp.dpi_scale = 1.0f;
_sapp.fullscreen = _sapp.desc.fullscreen;
_sapp.mouse.shown = true;
_sapp_timing_init(&_sapp.timing);
}
_SOKOL_PRIVATE void _sapp_discard_state(void) {
if (_sapp.clipboard.enabled) {
SOKOL_ASSERT(_sapp.clipboard.buffer);
_sapp_free((void*)_sapp.clipboard.buffer);
}
if (_sapp.drop.enabled) {
SOKOL_ASSERT(_sapp.drop.buffer);
_sapp_free((void*)_sapp.drop.buffer);
}
if (_sapp.default_icon_pixels) {
_sapp_free((void*)_sapp.default_icon_pixels);
}
_SAPP_CLEAR_ARC_STRUCT(_sapp_t, _sapp);
}
_SOKOL_PRIVATE void _sapp_init_event(sapp_event_type type) {
_sapp_clear(&_sapp.event, sizeof(_sapp.event));
_sapp.event.type = type;
_sapp.event.frame_count = _sapp.frame_count;
_sapp.event.mouse_button = SAPP_MOUSEBUTTON_INVALID;
_sapp.event.window_width = _sapp.window_width;
_sapp.event.window_height = _sapp.window_height;
_sapp.event.framebuffer_width = _sapp.framebuffer_width;
_sapp.event.framebuffer_height = _sapp.framebuffer_height;
_sapp.event.mouse_x = _sapp.mouse.x;
_sapp.event.mouse_y = _sapp.mouse.y;
_sapp.event.mouse_dx = _sapp.mouse.dx;
_sapp.event.mouse_dy = _sapp.mouse.dy;
}
_SOKOL_PRIVATE bool _sapp_events_enabled(void) {
/* only send events when an event callback is set, and the init function was called */
return (_sapp.desc.event_cb || _sapp.desc.event_userdata_cb) && _sapp.init_called;
}
_SOKOL_PRIVATE sapp_keycode _sapp_translate_key(int scan_code) {
if ((scan_code >= 0) && (scan_code < SAPP_MAX_KEYCODES)) {
return _sapp.keycodes[scan_code];
}
else {
return SAPP_KEYCODE_INVALID;
}
}
_SOKOL_PRIVATE void _sapp_clear_drop_buffer(void) {
if (_sapp.drop.enabled) {
SOKOL_ASSERT(_sapp.drop.buffer);
_sapp_clear(_sapp.drop.buffer, (size_t)_sapp.drop.buf_size);
}
}
_SOKOL_PRIVATE void _sapp_frame(void) {
if (_sapp.first_frame) {
_sapp.first_frame = false;
_sapp_call_init();
}
_sapp_call_frame();
_sapp.frame_count++;
}
_SOKOL_PRIVATE bool _sapp_image_validate(const sapp_image_desc* desc) {
SOKOL_ASSERT(desc->width > 0);
SOKOL_ASSERT(desc->height > 0);
SOKOL_ASSERT(desc->pixels.ptr != 0);
SOKOL_ASSERT(desc->pixels.size > 0);
const size_t wh_size = (size_t)(desc->width * desc->height) * sizeof(uint32_t);
if (wh_size != desc->pixels.size) {
_SAPP_ERROR(IMAGE_DATA_SIZE_MISMATCH);
return false;
}
return true;
}
_SOKOL_PRIVATE int _sapp_image_bestmatch(const sapp_image_desc image_descs[], int num_images, int width, int height) {
int least_diff = 0x7FFFFFFF;
int least_index = 0;
for (int i = 0; i < num_images; i++) {
int diff = (image_descs[i].width * image_descs[i].height) - (width * height);
if (diff < 0) {
diff = -diff;
}
if (diff < least_diff) {
least_diff = diff;
least_index = i;
}
}
return least_index;
}
_SOKOL_PRIVATE int _sapp_icon_num_images(const sapp_icon_desc* desc) {
int index = 0;
for (; index < SAPP_MAX_ICONIMAGES; index++) {
if (0 == desc->images[index].pixels.ptr) {
break;
}
}
return index;
}
_SOKOL_PRIVATE bool _sapp_validate_icon_desc(const sapp_icon_desc* desc, int num_images) {
SOKOL_ASSERT(num_images <= SAPP_MAX_ICONIMAGES);
for (int i = 0; i < num_images; i++) {
const sapp_image_desc* img_desc = &desc->images[i];
if (!_sapp_image_validate(img_desc)) {
return false;
}
}
return true;
}
_SOKOL_PRIVATE void _sapp_setup_default_icon(void) {
SOKOL_ASSERT(0 == _sapp.default_icon_pixels);
const int num_icons = 3;
const int icon_sizes[3] = { 16, 32, 64 }; // must be multiple of 8!
// allocate a pixel buffer for all icon pixels
int all_num_pixels = 0;
for (int i = 0; i < num_icons; i++) {
all_num_pixels += icon_sizes[i] * icon_sizes[i];
}
_sapp.default_icon_pixels = (uint32_t*) _sapp_malloc_clear((size_t)all_num_pixels * sizeof(uint32_t));
// initialize default_icon_desc struct
uint32_t* dst = _sapp.default_icon_pixels;
const uint32_t* dst_end = dst + all_num_pixels;
(void)dst_end; // silence unused warning in release mode
for (int i = 0; i < num_icons; i++) {
const int dim = (int) icon_sizes[i];
const int num_pixels = dim * dim;
sapp_image_desc* img_desc = &_sapp.default_icon_desc.images[i];
img_desc->width = dim;
img_desc->height = dim;
img_desc->pixels.ptr = dst;
img_desc->pixels.size = (size_t)num_pixels * sizeof(uint32_t);
dst += num_pixels;
}
SOKOL_ASSERT(dst == dst_end);
// Amstrad CPC font 'S'
const uint8_t tile[8] = {
0x3C,
0x66,
0x60,
0x3C,
0x06,
0x66,
0x3C,
0x00,
};
// rainbow colors
const uint32_t colors[8] = {
0xFF4370FF,
0xFF26A7FF,
0xFF58EEFF,
0xFF57E1D4,
0xFF65CC9C,
0xFF6ABB66,
0xFFF5A542,
0xFFC2577E,
};
dst = _sapp.default_icon_pixels;
const uint32_t blank = 0x00FFFFFF;
const uint32_t shadow = 0xFF000000;
for (int i = 0; i < num_icons; i++) {
const int dim = icon_sizes[i];
SOKOL_ASSERT((dim % 8) == 0);
const int scale = dim / 8;
for (int ty = 0, y = 0; ty < 8; ty++) {
const uint32_t color = colors[ty];
for (int sy = 0; sy < scale; sy++, y++) {
uint8_t bits = tile[ty];
for (int tx = 0, x = 0; tx < 8; tx++, bits<<=1) {
uint32_t pixel = (0 == (bits & 0x80)) ? blank : color;
for (int sx = 0; sx < scale; sx++, x++) {
SOKOL_ASSERT(dst < dst_end);
*dst++ = pixel;
}
}
}
}
}
SOKOL_ASSERT(dst == dst_end);
// right shadow
dst = _sapp.default_icon_pixels;
for (int i = 0; i < num_icons; i++) {
const int dim = icon_sizes[i];
for (int y = 0; y < dim; y++) {
uint32_t prev_color = blank;
for (int x = 0; x < dim; x++) {
const int dst_index = y * dim + x;
const uint32_t cur_color = dst[dst_index];
if ((cur_color == blank) && (prev_color != blank)) {
dst[dst_index] = shadow;
}
prev_color = cur_color;
}
}
dst += dim * dim;
}
SOKOL_ASSERT(dst == dst_end);
// bottom shadow
dst = _sapp.default_icon_pixels;
for (int i = 0; i < num_icons; i++) {
const int dim = icon_sizes[i];
for (int x = 0; x < dim; x++) {
uint32_t prev_color = blank;
for (int y = 0; y < dim; y++) {
const int dst_index = y * dim + x;
const uint32_t cur_color = dst[dst_index];
if ((cur_color == blank) && (prev_color != blank)) {
dst[dst_index] = shadow;
}
prev_color = cur_color;
}
}
dst += dim * dim;
}
SOKOL_ASSERT(dst == dst_end);
}
// █████ ██████ ██████ ██ ███████
// ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██████ ██████ ██ █████
// ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ███████ ███████
//
// >>apple
#if defined(_SAPP_APPLE)
#if __has_feature(objc_arc)
#define _SAPP_OBJC_RELEASE(obj) { obj = nil; }
#else
#define _SAPP_OBJC_RELEASE(obj) { [obj release]; obj = nil; }
#endif
// ███ ███ █████ ██████ ██████ ███████
// ████ ████ ██ ██ ██ ██ ██ ██
// ██ ████ ██ ███████ ██ ██ ██ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██████ ██████ ███████
//
// >>macos
#if defined(_SAPP_MACOS)
_SOKOL_PRIVATE void _sapp_macos_init_keytable(void) {
_sapp.keycodes[0x1D] = SAPP_KEYCODE_0;
_sapp.keycodes[0x12] = SAPP_KEYCODE_1;
_sapp.keycodes[0x13] = SAPP_KEYCODE_2;
_sapp.keycodes[0x14] = SAPP_KEYCODE_3;
_sapp.keycodes[0x15] = SAPP_KEYCODE_4;
_sapp.keycodes[0x17] = SAPP_KEYCODE_5;
_sapp.keycodes[0x16] = SAPP_KEYCODE_6;
_sapp.keycodes[0x1A] = SAPP_KEYCODE_7;
_sapp.keycodes[0x1C] = SAPP_KEYCODE_8;
_sapp.keycodes[0x19] = SAPP_KEYCODE_9;
_sapp.keycodes[0x00] = SAPP_KEYCODE_A;
_sapp.keycodes[0x0B] = SAPP_KEYCODE_B;
_sapp.keycodes[0x08] = SAPP_KEYCODE_C;
_sapp.keycodes[0x02] = SAPP_KEYCODE_D;
_sapp.keycodes[0x0E] = SAPP_KEYCODE_E;
_sapp.keycodes[0x03] = SAPP_KEYCODE_F;
_sapp.keycodes[0x05] = SAPP_KEYCODE_G;
_sapp.keycodes[0x04] = SAPP_KEYCODE_H;
_sapp.keycodes[0x22] = SAPP_KEYCODE_I;
_sapp.keycodes[0x26] = SAPP_KEYCODE_J;
_sapp.keycodes[0x28] = SAPP_KEYCODE_K;
_sapp.keycodes[0x25] = SAPP_KEYCODE_L;
_sapp.keycodes[0x2E] = SAPP_KEYCODE_M;
_sapp.keycodes[0x2D] = SAPP_KEYCODE_N;
_sapp.keycodes[0x1F] = SAPP_KEYCODE_O;
_sapp.keycodes[0x23] = SAPP_KEYCODE_P;
_sapp.keycodes[0x0C] = SAPP_KEYCODE_Q;
_sapp.keycodes[0x0F] = SAPP_KEYCODE_R;
_sapp.keycodes[0x01] = SAPP_KEYCODE_S;
_sapp.keycodes[0x11] = SAPP_KEYCODE_T;
_sapp.keycodes[0x20] = SAPP_KEYCODE_U;
_sapp.keycodes[0x09] = SAPP_KEYCODE_V;
_sapp.keycodes[0x0D] = SAPP_KEYCODE_W;
_sapp.keycodes[0x07] = SAPP_KEYCODE_X;
_sapp.keycodes[0x10] = SAPP_KEYCODE_Y;
_sapp.keycodes[0x06] = SAPP_KEYCODE_Z;
_sapp.keycodes[0x27] = SAPP_KEYCODE_APOSTROPHE;
_sapp.keycodes[0x2A] = SAPP_KEYCODE_BACKSLASH;
_sapp.keycodes[0x2B] = SAPP_KEYCODE_COMMA;
_sapp.keycodes[0x18] = SAPP_KEYCODE_EQUAL;
_sapp.keycodes[0x32] = SAPP_KEYCODE_GRAVE_ACCENT;
_sapp.keycodes[0x21] = SAPP_KEYCODE_LEFT_BRACKET;
_sapp.keycodes[0x1B] = SAPP_KEYCODE_MINUS;
_sapp.keycodes[0x2F] = SAPP_KEYCODE_PERIOD;
_sapp.keycodes[0x1E] = SAPP_KEYCODE_RIGHT_BRACKET;
_sapp.keycodes[0x29] = SAPP_KEYCODE_SEMICOLON;
_sapp.keycodes[0x2C] = SAPP_KEYCODE_SLASH;
_sapp.keycodes[0x0A] = SAPP_KEYCODE_WORLD_1;
_sapp.keycodes[0x33] = SAPP_KEYCODE_BACKSPACE;
_sapp.keycodes[0x39] = SAPP_KEYCODE_CAPS_LOCK;
_sapp.keycodes[0x75] = SAPP_KEYCODE_DELETE;
_sapp.keycodes[0x7D] = SAPP_KEYCODE_DOWN;
_sapp.keycodes[0x77] = SAPP_KEYCODE_END;
_sapp.keycodes[0x24] = SAPP_KEYCODE_ENTER;
_sapp.keycodes[0x35] = SAPP_KEYCODE_ESCAPE;
_sapp.keycodes[0x7A] = SAPP_KEYCODE_F1;
_sapp.keycodes[0x78] = SAPP_KEYCODE_F2;
_sapp.keycodes[0x63] = SAPP_KEYCODE_F3;
_sapp.keycodes[0x76] = SAPP_KEYCODE_F4;
_sapp.keycodes[0x60] = SAPP_KEYCODE_F5;
_sapp.keycodes[0x61] = SAPP_KEYCODE_F6;
_sapp.keycodes[0x62] = SAPP_KEYCODE_F7;
_sapp.keycodes[0x64] = SAPP_KEYCODE_F8;
_sapp.keycodes[0x65] = SAPP_KEYCODE_F9;
_sapp.keycodes[0x6D] = SAPP_KEYCODE_F10;
_sapp.keycodes[0x67] = SAPP_KEYCODE_F11;
_sapp.keycodes[0x6F] = SAPP_KEYCODE_F12;
_sapp.keycodes[0x69] = SAPP_KEYCODE_F13;
_sapp.keycodes[0x6B] = SAPP_KEYCODE_F14;
_sapp.keycodes[0x71] = SAPP_KEYCODE_F15;
_sapp.keycodes[0x6A] = SAPP_KEYCODE_F16;
_sapp.keycodes[0x40] = SAPP_KEYCODE_F17;
_sapp.keycodes[0x4F] = SAPP_KEYCODE_F18;
_sapp.keycodes[0x50] = SAPP_KEYCODE_F19;
_sapp.keycodes[0x5A] = SAPP_KEYCODE_F20;
_sapp.keycodes[0x73] = SAPP_KEYCODE_HOME;
_sapp.keycodes[0x72] = SAPP_KEYCODE_INSERT;
_sapp.keycodes[0x7B] = SAPP_KEYCODE_LEFT;
_sapp.keycodes[0x3A] = SAPP_KEYCODE_LEFT_ALT;
_sapp.keycodes[0x3B] = SAPP_KEYCODE_LEFT_CONTROL;
_sapp.keycodes[0x38] = SAPP_KEYCODE_LEFT_SHIFT;
_sapp.keycodes[0x37] = SAPP_KEYCODE_LEFT_SUPER;
_sapp.keycodes[0x6E] = SAPP_KEYCODE_MENU;
_sapp.keycodes[0x47] = SAPP_KEYCODE_NUM_LOCK;
_sapp.keycodes[0x79] = SAPP_KEYCODE_PAGE_DOWN;
_sapp.keycodes[0x74] = SAPP_KEYCODE_PAGE_UP;
_sapp.keycodes[0x7C] = SAPP_KEYCODE_RIGHT;
_sapp.keycodes[0x3D] = SAPP_KEYCODE_RIGHT_ALT;
_sapp.keycodes[0x3E] = SAPP_KEYCODE_RIGHT_CONTROL;
_sapp.keycodes[0x3C] = SAPP_KEYCODE_RIGHT_SHIFT;
_sapp.keycodes[0x36] = SAPP_KEYCODE_RIGHT_SUPER;
_sapp.keycodes[0x31] = SAPP_KEYCODE_SPACE;
_sapp.keycodes[0x30] = SAPP_KEYCODE_TAB;
_sapp.keycodes[0x7E] = SAPP_KEYCODE_UP;
_sapp.keycodes[0x52] = SAPP_KEYCODE_KP_0;
_sapp.keycodes[0x53] = SAPP_KEYCODE_KP_1;
_sapp.keycodes[0x54] = SAPP_KEYCODE_KP_2;
_sapp.keycodes[0x55] = SAPP_KEYCODE_KP_3;
_sapp.keycodes[0x56] = SAPP_KEYCODE_KP_4;
_sapp.keycodes[0x57] = SAPP_KEYCODE_KP_5;
_sapp.keycodes[0x58] = SAPP_KEYCODE_KP_6;
_sapp.keycodes[0x59] = SAPP_KEYCODE_KP_7;
_sapp.keycodes[0x5B] = SAPP_KEYCODE_KP_8;
_sapp.keycodes[0x5C] = SAPP_KEYCODE_KP_9;
_sapp.keycodes[0x45] = SAPP_KEYCODE_KP_ADD;
_sapp.keycodes[0x41] = SAPP_KEYCODE_KP_DECIMAL;
_sapp.keycodes[0x4B] = SAPP_KEYCODE_KP_DIVIDE;
_sapp.keycodes[0x4C] = SAPP_KEYCODE_KP_ENTER;
_sapp.keycodes[0x51] = SAPP_KEYCODE_KP_EQUAL;
_sapp.keycodes[0x43] = SAPP_KEYCODE_KP_MULTIPLY;
_sapp.keycodes[0x4E] = SAPP_KEYCODE_KP_SUBTRACT;
}
_SOKOL_PRIVATE void _sapp_macos_discard_state(void) {
// NOTE: it's safe to call [release] on a nil object
if (_sapp.macos.keyup_monitor != nil) {
[NSEvent removeMonitor:_sapp.macos.keyup_monitor];
// NOTE: removeMonitor also releases the object
_sapp.macos.keyup_monitor = nil;
}
_SAPP_OBJC_RELEASE(_sapp.macos.tracking_area);
_SAPP_OBJC_RELEASE(_sapp.macos.app_dlg);
_SAPP_OBJC_RELEASE(_sapp.macos.win_dlg);
_SAPP_OBJC_RELEASE(_sapp.macos.view);
#if defined(SOKOL_METAL)
_SAPP_OBJC_RELEASE(_sapp.macos.mtl_device);
#endif
_SAPP_OBJC_RELEASE(_sapp.macos.window);
}
// undocumented methods for creating cursors (see GLFW 3.4 and imgui_impl_osx.mm)
@interface NSCursor()
+ (id)_windowResizeNorthWestSouthEastCursor;
+ (id)_windowResizeNorthEastSouthWestCursor;
+ (id)_windowResizeNorthSouthCursor;
+ (id)_windowResizeEastWestCursor;
@end
_SOKOL_PRIVATE void _sapp_macos_init_cursors(void) {
_sapp.macos.cursors[SAPP_MOUSECURSOR_DEFAULT] = nil; // not a bug
_sapp.macos.cursors[SAPP_MOUSECURSOR_ARROW] = [NSCursor arrowCursor];
_sapp.macos.cursors[SAPP_MOUSECURSOR_IBEAM] = [NSCursor IBeamCursor];
_sapp.macos.cursors[SAPP_MOUSECURSOR_CROSSHAIR] = [NSCursor crosshairCursor];
_sapp.macos.cursors[SAPP_MOUSECURSOR_POINTING_HAND] = [NSCursor pointingHandCursor];
_sapp.macos.cursors[SAPP_MOUSECURSOR_RESIZE_EW] = [NSCursor respondsToSelector:@selector(_windowResizeEastWestCursor)] ? [NSCursor _windowResizeEastWestCursor] : [NSCursor resizeLeftRightCursor];
_sapp.macos.cursors[SAPP_MOUSECURSOR_RESIZE_NS] = [NSCursor respondsToSelector:@selector(_windowResizeNorthSouthCursor)] ? [NSCursor _windowResizeNorthSouthCursor] : [NSCursor resizeUpDownCursor];
_sapp.macos.cursors[SAPP_MOUSECURSOR_RESIZE_NWSE] = [NSCursor respondsToSelector:@selector(_windowResizeNorthWestSouthEastCursor)] ? [NSCursor _windowResizeNorthWestSouthEastCursor] : [NSCursor closedHandCursor];
_sapp.macos.cursors[SAPP_MOUSECURSOR_RESIZE_NESW] = [NSCursor respondsToSelector:@selector(_windowResizeNorthEastSouthWestCursor)] ? [NSCursor _windowResizeNorthEastSouthWestCursor] : [NSCursor closedHandCursor];
_sapp.macos.cursors[SAPP_MOUSECURSOR_RESIZE_ALL] = [NSCursor closedHandCursor];
_sapp.macos.cursors[SAPP_MOUSECURSOR_NOT_ALLOWED] = [NSCursor operationNotAllowedCursor];
}
_SOKOL_PRIVATE void _sapp_macos_run(const sapp_desc* desc) {
_sapp_init_state(desc);
_sapp_macos_init_keytable();
[NSApplication sharedApplication];
// set the application dock icon as early as possible, otherwise
// the dummy icon will be visible for a short time
sapp_set_icon(&_sapp.desc.icon);
_sapp.macos.app_dlg = [[_sapp_macos_app_delegate alloc] init];
NSApp.delegate = _sapp.macos.app_dlg;
// workaround for "no key-up sent while Cmd is pressed" taken from GLFW:
NSEvent* (^keyup_monitor)(NSEvent*) = ^NSEvent* (NSEvent* event) {
if ([event modifierFlags] & NSEventModifierFlagCommand) {
[[NSApp keyWindow] sendEvent:event];
}
return event;
};
_sapp.macos.keyup_monitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyUp handler:keyup_monitor];
[NSApp run];
// NOTE: [NSApp run] never returns, instead cleanup code
// must be put into applicationWillTerminate
}
/* MacOS entry function */
#if !defined(SOKOL_NO_ENTRY)
int main(int argc, char* argv[]) {
sapp_desc desc = sokol_main(argc, argv);
_sapp_macos_run(&desc);
return 0;
}
#endif /* SOKOL_NO_ENTRY */
_SOKOL_PRIVATE uint32_t _sapp_macos_mods(NSEvent* ev) {
const NSEventModifierFlags f = (ev == nil) ? NSEvent.modifierFlags : ev.modifierFlags;
const NSUInteger b = NSEvent.pressedMouseButtons;
uint32_t m = 0;
if (f & NSEventModifierFlagShift) {
m |= SAPP_MODIFIER_SHIFT;
}
if (f & NSEventModifierFlagControl) {
m |= SAPP_MODIFIER_CTRL;
}
if (f & NSEventModifierFlagOption) {
m |= SAPP_MODIFIER_ALT;
}
if (f & NSEventModifierFlagCommand) {
m |= SAPP_MODIFIER_SUPER;
}
if (0 != (b & (1<<0))) {
m |= SAPP_MODIFIER_LMB;
}
if (0 != (b & (1<<1))) {
m |= SAPP_MODIFIER_RMB;
}
if (0 != (b & (1<<2))) {
m |= SAPP_MODIFIER_MMB;
}
return m;
}
_SOKOL_PRIVATE void _sapp_macos_mouse_event(sapp_event_type type, sapp_mousebutton btn, uint32_t mod) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
_sapp.event.mouse_button = btn;
_sapp.event.modifiers = mod;
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE void _sapp_macos_key_event(sapp_event_type type, sapp_keycode key, bool repeat, uint32_t mod) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
_sapp.event.key_code = key;
_sapp.event.key_repeat = repeat;
_sapp.event.modifiers = mod;
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE void _sapp_macos_app_event(sapp_event_type type) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
_sapp_call_event(&_sapp.event);
}
}
/* NOTE: unlike the iOS version of this function, the macOS version
can dynamically update the DPI scaling factor when a window is moved
between HighDPI / LowDPI screens.
*/
_SOKOL_PRIVATE void _sapp_macos_update_dimensions(void) {
if (_sapp.desc.high_dpi) {
_sapp.dpi_scale = [_sapp.macos.window screen].backingScaleFactor;
}
else {
_sapp.dpi_scale = 1.0f;
}
_sapp.macos.view.layer.contentsScale = _sapp.dpi_scale; // NOTE: needed because we set layerContentsPlacement to a non-scaling value in windowWillStartLiveResize.
const NSRect bounds = [_sapp.macos.view bounds];
_sapp.window_width = (int)roundf(bounds.size.width);
_sapp.window_height = (int)roundf(bounds.size.height);
#if defined(SOKOL_METAL)
_sapp.framebuffer_width = (int)roundf(bounds.size.width * _sapp.dpi_scale);
_sapp.framebuffer_height = (int)roundf(bounds.size.height * _sapp.dpi_scale);
const CGSize fb_size = _sapp.macos.view.drawableSize;
const int cur_fb_width = (int)roundf(fb_size.width);
const int cur_fb_height = (int)roundf(fb_size.height);
const bool dim_changed = (_sapp.framebuffer_width != cur_fb_width) ||
(_sapp.framebuffer_height != cur_fb_height);
#elif defined(SOKOL_GLCORE)
const int cur_fb_width = (int)roundf(bounds.size.width * _sapp.dpi_scale);
const int cur_fb_height = (int)roundf(bounds.size.height * _sapp.dpi_scale);
const bool dim_changed = (_sapp.framebuffer_width != cur_fb_width) ||
(_sapp.framebuffer_height != cur_fb_height);
_sapp.framebuffer_width = cur_fb_width;
_sapp.framebuffer_height = cur_fb_height;
#endif
if (_sapp.framebuffer_width == 0) {
_sapp.framebuffer_width = 1;
}
if (_sapp.framebuffer_height == 0) {
_sapp.framebuffer_height = 1;
}
if (_sapp.window_width == 0) {
_sapp.window_width = 1;
}
if (_sapp.window_height == 0) {
_sapp.window_height = 1;
}
if (dim_changed) {
#if defined(SOKOL_METAL)
CGSize drawable_size = { (CGFloat) _sapp.framebuffer_width, (CGFloat) _sapp.framebuffer_height };
_sapp.macos.view.drawableSize = drawable_size;
#else
// nothing to do for GL?
#endif
if (!_sapp.first_frame) {
_sapp_macos_app_event(SAPP_EVENTTYPE_RESIZED);
}
}
}
_SOKOL_PRIVATE void _sapp_macos_toggle_fullscreen(void) {
/* NOTE: the _sapp.fullscreen flag is also notified by the
windowDidEnterFullscreen / windowDidExitFullscreen
event handlers
*/
_sapp.fullscreen = !_sapp.fullscreen;
[_sapp.macos.window toggleFullScreen:nil];
}
_SOKOL_PRIVATE void _sapp_macos_set_clipboard_string(const char* str) {
@autoreleasepool {
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
[pasteboard declareTypes:@[NSPasteboardTypeString] owner:nil];
[pasteboard setString:@(str) forType:NSPasteboardTypeString];
}
}
_SOKOL_PRIVATE const char* _sapp_macos_get_clipboard_string(void) {
SOKOL_ASSERT(_sapp.clipboard.buffer);
@autoreleasepool {
_sapp.clipboard.buffer[0] = 0;
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
if (![[pasteboard types] containsObject:NSPasteboardTypeString]) {
return _sapp.clipboard.buffer;
}
NSString* str = [pasteboard stringForType:NSPasteboardTypeString];
if (!str) {
return _sapp.clipboard.buffer;
}
_sapp_strcpy([str UTF8String], _sapp.clipboard.buffer, _sapp.clipboard.buf_size);
}
return _sapp.clipboard.buffer;
}
_SOKOL_PRIVATE void _sapp_macos_update_window_title(void) {
[_sapp.macos.window setTitle: [NSString stringWithUTF8String:_sapp.window_title]];
}
_SOKOL_PRIVATE void _sapp_macos_mouse_update_from_nspoint(NSPoint mouse_pos, bool clear_dxdy) {
if (!_sapp.mouse.locked) {
float new_x = mouse_pos.x * _sapp.dpi_scale;
float new_y = _sapp.framebuffer_height - (mouse_pos.y * _sapp.dpi_scale) - 1;
if (clear_dxdy) {
_sapp.mouse.dx = 0.0f;
_sapp.mouse.dy = 0.0f;
}
else if (_sapp.mouse.pos_valid) {
// don't update dx/dy in the very first update
_sapp.mouse.dx = new_x - _sapp.mouse.x;
_sapp.mouse.dy = new_y - _sapp.mouse.y;
}
_sapp.mouse.x = new_x;
_sapp.mouse.y = new_y;
_sapp.mouse.pos_valid = true;
}
}
_SOKOL_PRIVATE void _sapp_macos_mouse_update_from_nsevent(NSEvent* event, bool clear_dxdy) {
_sapp_macos_mouse_update_from_nspoint(event.locationInWindow, clear_dxdy);
}
_SOKOL_PRIVATE void _sapp_macos_show_mouse(bool visible) {
/* NOTE: this function is only called when the mouse visibility actually changes */
if (visible) {
CGDisplayShowCursor(kCGDirectMainDisplay);
}
else {
CGDisplayHideCursor(kCGDirectMainDisplay);
}
}
_SOKOL_PRIVATE void _sapp_macos_lock_mouse(bool lock) {
if (lock == _sapp.mouse.locked) {
return;
}
_sapp.mouse.dx = 0.0f;
_sapp.mouse.dy = 0.0f;
_sapp.mouse.locked = lock;
/*
NOTE that this code doesn't warp the mouse cursor to the window
center as everybody else does it. This lead to a spike in the
*second* mouse-moved event after the warp happened. The
mouse centering doesn't seem to be required (mouse-moved events
are reported correctly even when the cursor is at an edge of the screen).
NOTE also that the hide/show of the mouse cursor should properly
stack with calls to sapp_show_mouse()
*/
if (_sapp.mouse.locked) {
CGAssociateMouseAndMouseCursorPosition(NO);
[NSCursor hide];
}
else {
[NSCursor unhide];
CGAssociateMouseAndMouseCursorPosition(YES);
}
}
_SOKOL_PRIVATE void _sapp_macos_update_cursor(sapp_mouse_cursor cursor, bool shown) {
// show/hide cursor only if visibility status has changed (required because show/hide stacks)
if (shown != _sapp.mouse.shown) {
if (shown) {
[NSCursor unhide];
}
else {
[NSCursor hide];
}
}
// update cursor type
SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM));
if (_sapp.macos.cursors[cursor]) {
[_sapp.macos.cursors[cursor] set];
}
else {
[[NSCursor arrowCursor] set];
}
}
_SOKOL_PRIVATE void _sapp_macos_set_icon(const sapp_icon_desc* icon_desc, int num_images) {
NSDockTile* dock_tile = NSApp.dockTile;
const int wanted_width = (int) dock_tile.size.width;
const int wanted_height = (int) dock_tile.size.height;
const int img_index = _sapp_image_bestmatch(icon_desc->images, num_images, wanted_width, wanted_height);
const sapp_image_desc* img_desc = &icon_desc->images[img_index];
CGColorSpaceRef cg_color_space = CGColorSpaceCreateDeviceRGB();
CFDataRef cf_data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)img_desc->pixels.ptr, (CFIndex)img_desc->pixels.size);
CGDataProviderRef cg_data_provider = CGDataProviderCreateWithCFData(cf_data);
CGImageRef cg_img = CGImageCreate(
(size_t)img_desc->width, // width
(size_t)img_desc->height, // height
8, // bitsPerComponent
32, // bitsPerPixel
(size_t)img_desc->width * 4,// bytesPerRow
cg_color_space, // space
kCGImageAlphaLast | kCGImageByteOrderDefault, // bitmapInfo
cg_data_provider, // provider
NULL, // decode
false, // shouldInterpolate
kCGRenderingIntentDefault);
CFRelease(cf_data);
CGDataProviderRelease(cg_data_provider);
CGColorSpaceRelease(cg_color_space);
NSImage* ns_image = [[NSImage alloc] initWithCGImage:cg_img size:dock_tile.size];
dock_tile.contentView = [NSImageView imageViewWithImage:ns_image];
[dock_tile display];
_SAPP_OBJC_RELEASE(ns_image);
CGImageRelease(cg_img);
}
_SOKOL_PRIVATE void _sapp_macos_frame(void) {
_sapp_frame();
if (_sapp.quit_requested || _sapp.quit_ordered) {
[_sapp.macos.window performClose:nil];
}
}
@implementation _sapp_macos_app_delegate
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
_SOKOL_UNUSED(aNotification);
_sapp_macos_init_cursors();
if ((_sapp.window_width == 0) || (_sapp.window_height == 0)) {
// use 4/5 of screen size as default size
NSRect screen_rect = NSScreen.mainScreen.frame;
if (_sapp.window_width == 0) {
_sapp.window_width = (int)roundf((screen_rect.size.width * 4.0f) / 5.0f);
}
if (_sapp.window_height == 0) {
_sapp.window_height = (int)roundf((screen_rect.size.height * 4.0f) / 5.0f);
}
}
const NSUInteger style =
NSWindowStyleMaskTitled |
NSWindowStyleMaskClosable |
NSWindowStyleMaskMiniaturizable |
NSWindowStyleMaskResizable;
NSRect window_rect = NSMakeRect(0, 0, _sapp.window_width, _sapp.window_height);
_sapp.macos.window = [[_sapp_macos_window alloc]
initWithContentRect:window_rect
styleMask:style
backing:NSBackingStoreBuffered
defer:NO];
_sapp.macos.window.releasedWhenClosed = NO; // this is necessary for proper cleanup in applicationWillTerminate
_sapp.macos.window.title = [NSString stringWithUTF8String:_sapp.window_title];
_sapp.macos.window.acceptsMouseMovedEvents = YES;
_sapp.macos.window.restorable = YES;
_sapp.macos.win_dlg = [[_sapp_macos_window_delegate alloc] init];
_sapp.macos.window.delegate = _sapp.macos.win_dlg;
#if defined(SOKOL_METAL)
NSInteger max_fps = 60;
#if (__MAC_OS_X_VERSION_MAX_ALLOWED >= 120000)
if (@available(macOS 12.0, *)) {
max_fps = [NSScreen.mainScreen maximumFramesPerSecond];
}
#endif
_sapp.macos.mtl_device = MTLCreateSystemDefaultDevice();
_sapp.macos.view = [[_sapp_macos_view alloc] init];
[_sapp.macos.view updateTrackingAreas];
_sapp.macos.view.preferredFramesPerSecond = max_fps / _sapp.swap_interval;
_sapp.macos.view.device = _sapp.macos.mtl_device;
_sapp.macos.view.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
_sapp.macos.view.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
_sapp.macos.view.sampleCount = (NSUInteger) _sapp.sample_count;
_sapp.macos.view.autoResizeDrawable = false;
_sapp.macos.window.contentView = _sapp.macos.view;
[_sapp.macos.window makeFirstResponder:_sapp.macos.view];
_sapp.macos.view.layer.magnificationFilter = kCAFilterNearest;
#elif defined(SOKOL_GLCORE)
NSOpenGLPixelFormatAttribute attrs[32];
int i = 0;
attrs[i++] = NSOpenGLPFAAccelerated;
attrs[i++] = NSOpenGLPFADoubleBuffer;
attrs[i++] = NSOpenGLPFAOpenGLProfile;
const int glVersion = _sapp.desc.gl_major_version * 10 + _sapp.desc.gl_minor_version;
switch(glVersion) {
case 10: attrs[i++] = NSOpenGLProfileVersionLegacy; break;
case 32: attrs[i++] = NSOpenGLProfileVersion3_2Core; break;
case 41: attrs[i++] = NSOpenGLProfileVersion4_1Core; break;
default:
_SAPP_PANIC(MACOS_INVALID_NSOPENGL_PROFILE);
}
attrs[i++] = NSOpenGLPFAColorSize; attrs[i++] = 24;
attrs[i++] = NSOpenGLPFAAlphaSize; attrs[i++] = 8;
attrs[i++] = NSOpenGLPFADepthSize; attrs[i++] = 24;
attrs[i++] = NSOpenGLPFAStencilSize; attrs[i++] = 8;
if (_sapp.sample_count > 1) {
attrs[i++] = NSOpenGLPFAMultisample;
attrs[i++] = NSOpenGLPFASampleBuffers; attrs[i++] = 1;
attrs[i++] = NSOpenGLPFASamples; attrs[i++] = (NSOpenGLPixelFormatAttribute)_sapp.sample_count;
}
else {
attrs[i++] = NSOpenGLPFASampleBuffers; attrs[i++] = 0;
}
attrs[i++] = 0;
NSOpenGLPixelFormat* glpixelformat_obj = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
SOKOL_ASSERT(glpixelformat_obj != nil);
_sapp.macos.view = [[_sapp_macos_view alloc]
initWithFrame:window_rect
pixelFormat:glpixelformat_obj];
_SAPP_OBJC_RELEASE(glpixelformat_obj);
[_sapp.macos.view updateTrackingAreas];
if (_sapp.desc.high_dpi) {
[_sapp.macos.view setWantsBestResolutionOpenGLSurface:YES];
}
else {
[_sapp.macos.view setWantsBestResolutionOpenGLSurface:NO];
}
_sapp.macos.window.contentView = _sapp.macos.view;
[_sapp.macos.window makeFirstResponder:_sapp.macos.view];
NSTimer* timer_obj = [NSTimer timerWithTimeInterval:0.001
target:_sapp.macos.view
selector:@selector(timerFired:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer_obj forMode:NSDefaultRunLoopMode];
timer_obj = nil;
#endif
[_sapp.macos.window center];
_sapp.valid = true;
if (_sapp.fullscreen) {
/* ^^^ on GL, this already toggles a rendered frame, so set the valid flag before */
[_sapp.macos.window toggleFullScreen:self];
}
NSApp.activationPolicy = NSApplicationActivationPolicyRegular;
[NSApp activateIgnoringOtherApps:YES];
[_sapp.macos.window makeKeyAndOrderFront:nil];
_sapp_macos_update_dimensions();
[NSEvent setMouseCoalescingEnabled:NO];
// workaround for window not being focused during a long init callback
// for details see: https://github.com/floooh/sokol/pull/982
// also see: https://gitlab.gnome.org/GNOME/gtk/-/issues/2342
NSEvent *focusevent = [NSEvent otherEventWithType:NSEventTypeAppKitDefined
location:NSZeroPoint
modifierFlags:0x40
timestamp:0
windowNumber:0
context:nil
subtype:NSEventSubtypeApplicationActivated
data1:0
data2:0];
[NSApp postEvent:focusevent atStart:YES];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender {
_SOKOL_UNUSED(sender);
return YES;
}
- (void)applicationWillTerminate:(NSNotification*)notification {
_SOKOL_UNUSED(notification);
_sapp_call_cleanup();
_sapp_macos_discard_state();
_sapp_discard_state();
}
@end
@implementation _sapp_macos_window_delegate
- (BOOL)windowShouldClose:(id)sender {
_SOKOL_UNUSED(sender);
/* only give user-code a chance to intervene when sapp_quit() wasn't already called */
if (!_sapp.quit_ordered) {
/* if window should be closed and event handling is enabled, give user code
a chance to intervene via sapp_cancel_quit()
*/
_sapp.quit_requested = true;
_sapp_macos_app_event(SAPP_EVENTTYPE_QUIT_REQUESTED);
/* user code hasn't intervened, quit the app */
if (_sapp.quit_requested) {
_sapp.quit_ordered = true;
}
}
if (_sapp.quit_ordered) {
return YES;
}
else {
return NO;
}
}
#if defined(SOKOL_METAL)
- (void)windowWillStartLiveResize:(NSNotification *)notification {
// Work around the MTKView resizing glitch by "anchoring" the layer to the window corner opposite
// to the currently manipulated corner (or edge). This prevents the content stretching back and
// forth during resizing. This is a workaround for this issue: https://github.com/floooh/sokol/issues/700
// Can be removed if/when migrating to CAMetalLayer: https://github.com/floooh/sokol/issues/727
bool resizing_from_left = _sapp.mouse.x < _sapp.window_width/2;
bool resizing_from_top = _sapp.mouse.y < _sapp.window_height/2;
NSViewLayerContentsPlacement placement;
if (resizing_from_left) {
placement = resizing_from_top ? NSViewLayerContentsPlacementBottomRight : NSViewLayerContentsPlacementTopRight;
} else {
placement = resizing_from_top ? NSViewLayerContentsPlacementBottomLeft : NSViewLayerContentsPlacementTopLeft;
}
_sapp.macos.view.layerContentsPlacement = placement;
}
#endif
- (void)windowDidResize:(NSNotification*)notification {
_SOKOL_UNUSED(notification);
_sapp_macos_update_dimensions();
}
- (void)windowDidChangeScreen:(NSNotification*)notification {
_SOKOL_UNUSED(notification);
_sapp_timing_reset(&_sapp.timing);
_sapp_macos_update_dimensions();
}
- (void)windowDidMiniaturize:(NSNotification*)notification {
_SOKOL_UNUSED(notification);
_sapp_macos_app_event(SAPP_EVENTTYPE_ICONIFIED);
}
- (void)windowDidDeminiaturize:(NSNotification*)notification {
_SOKOL_UNUSED(notification);
_sapp_macos_app_event(SAPP_EVENTTYPE_RESTORED);
}
- (void)windowDidBecomeKey:(NSNotification*)notification {
_SOKOL_UNUSED(notification);
_sapp_macos_app_event(SAPP_EVENTTYPE_FOCUSED);
}
- (void)windowDidResignKey:(NSNotification*)notification {
_SOKOL_UNUSED(notification);
_sapp_macos_app_event(SAPP_EVENTTYPE_UNFOCUSED);
}
- (void)windowDidEnterFullScreen:(NSNotification*)notification {
_SOKOL_UNUSED(notification);
_sapp.fullscreen = true;
}
- (void)windowDidExitFullScreen:(NSNotification*)notification {
_SOKOL_UNUSED(notification);
_sapp.fullscreen = false;
}
@end
@implementation _sapp_macos_window
- (instancetype)initWithContentRect:(NSRect)contentRect
styleMask:(NSWindowStyleMask)style
backing:(NSBackingStoreType)backingStoreType
defer:(BOOL)flag {
if (self = [super initWithContentRect:contentRect styleMask:style backing:backingStoreType defer:flag]) {
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
[self registerForDraggedTypes:[NSArray arrayWithObject:NSPasteboardTypeFileURL]];
#endif
}
return self;
}
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
return NSDragOperationCopy;
}
- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
return NSDragOperationCopy;
}
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
NSPasteboard *pboard = [sender draggingPasteboard];
if ([pboard.types containsObject:NSPasteboardTypeFileURL]) {
_sapp_clear_drop_buffer();
_sapp.drop.num_files = ((int)pboard.pasteboardItems.count > _sapp.drop.max_files) ? _sapp.drop.max_files : (int)pboard.pasteboardItems.count;
bool drop_failed = false;
for (int i = 0; i < _sapp.drop.num_files; i++) {
NSURL *fileUrl = [NSURL fileURLWithPath:[pboard.pasteboardItems[(NSUInteger)i] stringForType:NSPasteboardTypeFileURL]];
if (!_sapp_strcpy(fileUrl.standardizedURL.path.UTF8String, _sapp_dropped_file_path_ptr(i), _sapp.drop.max_path_length)) {
_SAPP_ERROR(DROPPED_FILE_PATH_TOO_LONG);
drop_failed = true;
break;
}
}
if (!drop_failed) {
if (_sapp_events_enabled()) {
_sapp_macos_mouse_update_from_nspoint(sender.draggingLocation, true);
_sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED);
_sapp.event.modifiers = _sapp_macos_mods(nil);
_sapp_call_event(&_sapp.event);
}
}
else {
_sapp_clear_drop_buffer();
_sapp.drop.num_files = 0;
}
return YES;
}
#endif
return NO;
}
@end
@implementation _sapp_macos_view
#if defined(SOKOL_GLCORE)
- (void)timerFired:(id)sender {
_SOKOL_UNUSED(sender);
[self setNeedsDisplay:YES];
}
- (void)prepareOpenGL {
[super prepareOpenGL];
GLint swapInt = 1;
NSOpenGLContext* ctx = [_sapp.macos.view openGLContext];
[ctx setValues:&swapInt forParameter:NSOpenGLContextParameterSwapInterval];
[ctx makeCurrentContext];
}
#endif
_SOKOL_PRIVATE void _sapp_macos_poll_input_events(void) {
/*
NOTE: late event polling temporarily out-commented to check if this
causes infrequent and almost impossible to reproduce problems with the
window close events, see:
https://github.com/floooh/sokol/pull/483#issuecomment-805148815
const NSEventMask mask = NSEventMaskLeftMouseDown |
NSEventMaskLeftMouseUp|
NSEventMaskRightMouseDown |
NSEventMaskRightMouseUp |
NSEventMaskMouseMoved |
NSEventMaskLeftMouseDragged |
NSEventMaskRightMouseDragged |
NSEventMaskMouseEntered |
NSEventMaskMouseExited |
NSEventMaskKeyDown |
NSEventMaskKeyUp |
NSEventMaskCursorUpdate |
NSEventMaskScrollWheel |
NSEventMaskTabletPoint |
NSEventMaskTabletProximity |
NSEventMaskOtherMouseDown |
NSEventMaskOtherMouseUp |
NSEventMaskOtherMouseDragged |
NSEventMaskPressure |
NSEventMaskDirectTouch;
@autoreleasepool {
for (;;) {
// NOTE: using NSDefaultRunLoopMode here causes stuttering in the GL backend,
// see: https://github.com/floooh/sokol/issues/486
NSEvent* event = [NSApp nextEventMatchingMask:mask untilDate:nil inMode:NSEventTrackingRunLoopMode dequeue:YES];
if (event == nil) {
break;
}
[NSApp sendEvent:event];
}
}
*/
}
- (void)drawRect:(NSRect)rect {
_SOKOL_UNUSED(rect);
#if defined(_SAPP_ANY_GL)
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer);
#endif
_sapp_timing_measure(&_sapp.timing);
/* Catch any last-moment input events */
_sapp_macos_poll_input_events();
@autoreleasepool {
_sapp_macos_frame();
}
#if defined(_SAPP_ANY_GL)
[[_sapp.macos.view openGLContext] flushBuffer];
#endif
}
- (BOOL)isOpaque {
return YES;
}
- (BOOL)canBecomeKeyView {
return YES;
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)updateTrackingAreas {
if (_sapp.macos.tracking_area != nil) {
[self removeTrackingArea:_sapp.macos.tracking_area];
_SAPP_OBJC_RELEASE(_sapp.macos.tracking_area);
}
const NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited |
NSTrackingActiveInKeyWindow |
NSTrackingEnabledDuringMouseDrag |
NSTrackingCursorUpdate |
NSTrackingInVisibleRect |
NSTrackingAssumeInside;
_sapp.macos.tracking_area = [[NSTrackingArea alloc] initWithRect:[self bounds] options:options owner:self userInfo:nil];
[self addTrackingArea:_sapp.macos.tracking_area];
[super updateTrackingAreas];
}
// helper function to make GL context active
static void _sapp_gl_make_current(void) {
#if defined(SOKOL_GLCORE)
[[_sapp.macos.view openGLContext] makeCurrentContext];
#endif
}
- (void)mouseEntered:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, true);
/* don't send mouse enter/leave while dragging (so that it behaves the same as
on Windows while SetCapture is active
*/
if (0 == _sapp.macos.mouse_buttons) {
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_ENTER, SAPP_MOUSEBUTTON_INVALID, _sapp_macos_mods(event));
}
}
- (void)mouseExited:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, true);
if (0 == _sapp.macos.mouse_buttons) {
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_LEAVE, SAPP_MOUSEBUTTON_INVALID, _sapp_macos_mods(event));
}
}
- (void)mouseDown:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, false);
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_LEFT, _sapp_macos_mods(event));
_sapp.macos.mouse_buttons |= (1<<SAPP_MOUSEBUTTON_LEFT);
}
- (void)mouseUp:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, false);
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_LEFT, _sapp_macos_mods(event));
_sapp.macos.mouse_buttons &= ~(1<<SAPP_MOUSEBUTTON_LEFT);
}
- (void)rightMouseDown:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, false);
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_RIGHT, _sapp_macos_mods(event));
_sapp.macos.mouse_buttons |= (1<<SAPP_MOUSEBUTTON_RIGHT);
}
- (void)rightMouseUp:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, false);
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_RIGHT, _sapp_macos_mods(event));
_sapp.macos.mouse_buttons &= ~(1<<SAPP_MOUSEBUTTON_RIGHT);
}
- (void)otherMouseDown:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, false);
if (2 == event.buttonNumber) {
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_MIDDLE, _sapp_macos_mods(event));
_sapp.macos.mouse_buttons |= (1<<SAPP_MOUSEBUTTON_MIDDLE);
}
}
- (void)otherMouseUp:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, false);
if (2 == event.buttonNumber) {
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_MIDDLE, _sapp_macos_mods(event));
_sapp.macos.mouse_buttons &= (1<<SAPP_MOUSEBUTTON_MIDDLE);
}
}
- (void)otherMouseDragged:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, false);
if (2 == event.buttonNumber) {
if (_sapp.mouse.locked) {
_sapp.mouse.dx = [event deltaX];
_sapp.mouse.dy = [event deltaY];
}
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_macos_mods(event));
}
}
- (void)mouseMoved:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, false);
if (_sapp.mouse.locked) {
_sapp.mouse.dx = [event deltaX];
_sapp.mouse.dy = [event deltaY];
}
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID , _sapp_macos_mods(event));
}
- (void)mouseDragged:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, false);
if (_sapp.mouse.locked) {
_sapp.mouse.dx = [event deltaX];
_sapp.mouse.dy = [event deltaY];
}
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID , _sapp_macos_mods(event));
}
- (void)rightMouseDragged:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, false);
if (_sapp.mouse.locked) {
_sapp.mouse.dx = [event deltaX];
_sapp.mouse.dy = [event deltaY];
}
_sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_macos_mods(event));
}
- (void)scrollWheel:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_mouse_update_from_nsevent(event, true);
if (_sapp_events_enabled()) {
float dx = (float) event.scrollingDeltaX;
float dy = (float) event.scrollingDeltaY;
if (event.hasPreciseScrollingDeltas) {
dx *= 0.1;
dy *= 0.1;
}
if ((_sapp_absf(dx) > 0.0f) || (_sapp_absf(dy) > 0.0f)) {
_sapp_init_event(SAPP_EVENTTYPE_MOUSE_SCROLL);
_sapp.event.modifiers = _sapp_macos_mods(event);
_sapp.event.scroll_x = dx;
_sapp.event.scroll_y = dy;
_sapp_call_event(&_sapp.event);
}
}
}
- (void)keyDown:(NSEvent*)event {
if (_sapp_events_enabled()) {
_sapp_gl_make_current();
const uint32_t mods = _sapp_macos_mods(event);
const sapp_keycode key_code = _sapp_translate_key(event.keyCode);
_sapp_macos_key_event(SAPP_EVENTTYPE_KEY_DOWN, key_code, event.isARepeat, mods);
const NSString* chars = event.characters;
const NSUInteger len = chars.length;
if (len > 0) {
_sapp_init_event(SAPP_EVENTTYPE_CHAR);
_sapp.event.modifiers = mods;
for (NSUInteger i = 0; i < len; i++) {
const unichar codepoint = [chars characterAtIndex:i];
if ((codepoint & 0xFF00) == 0xF700) {
continue;
}
_sapp.event.char_code = codepoint;
_sapp.event.key_repeat = event.isARepeat;
_sapp_call_event(&_sapp.event);
}
}
/* if this is a Cmd+V (paste), also send a CLIPBOARD_PASTE event */
if (_sapp.clipboard.enabled && (mods == SAPP_MODIFIER_SUPER) && (key_code == SAPP_KEYCODE_V)) {
_sapp_init_event(SAPP_EVENTTYPE_CLIPBOARD_PASTED);
_sapp_call_event(&_sapp.event);
}
}
}
- (void)keyUp:(NSEvent*)event {
_sapp_gl_make_current();
_sapp_macos_key_event(SAPP_EVENTTYPE_KEY_UP,
_sapp_translate_key(event.keyCode),
event.isARepeat,
_sapp_macos_mods(event));
}
- (void)flagsChanged:(NSEvent*)event {
const uint32_t old_f = _sapp.macos.flags_changed_store;
const uint32_t new_f = (uint32_t)event.modifierFlags;
_sapp.macos.flags_changed_store = new_f;
sapp_keycode key_code = SAPP_KEYCODE_INVALID;
bool down = false;
if ((new_f ^ old_f) & NSEventModifierFlagShift) {
key_code = SAPP_KEYCODE_LEFT_SHIFT;
down = 0 != (new_f & NSEventModifierFlagShift);
}
if ((new_f ^ old_f) & NSEventModifierFlagControl) {
key_code = SAPP_KEYCODE_LEFT_CONTROL;
down = 0 != (new_f & NSEventModifierFlagControl);
}
if ((new_f ^ old_f) & NSEventModifierFlagOption) {
key_code = SAPP_KEYCODE_LEFT_ALT;
down = 0 != (new_f & NSEventModifierFlagOption);
}
if ((new_f ^ old_f) & NSEventModifierFlagCommand) {
key_code = SAPP_KEYCODE_LEFT_SUPER;
down = 0 != (new_f & NSEventModifierFlagCommand);
}
if (key_code != SAPP_KEYCODE_INVALID) {
_sapp_macos_key_event(down ? SAPP_EVENTTYPE_KEY_DOWN : SAPP_EVENTTYPE_KEY_UP,
key_code,
false,
_sapp_macos_mods(event));
}
}
@end
#endif // macOS
// ██ ██████ ███████
// ██ ██ ██ ██
// ██ ██ ██ ███████
// ██ ██ ██ ██
// ██ ██████ ███████
//
// >>ios
#if defined(_SAPP_IOS)
_SOKOL_PRIVATE void _sapp_ios_discard_state(void) {
// NOTE: it's safe to call [release] on a nil object
_SAPP_OBJC_RELEASE(_sapp.ios.textfield_dlg);
_SAPP_OBJC_RELEASE(_sapp.ios.textfield);
#if defined(SOKOL_METAL)
_SAPP_OBJC_RELEASE(_sapp.ios.view_ctrl);
_SAPP_OBJC_RELEASE(_sapp.ios.mtl_device);
#else
_SAPP_OBJC_RELEASE(_sapp.ios.view_ctrl);
_SAPP_OBJC_RELEASE(_sapp.ios.eagl_ctx);
#endif
_SAPP_OBJC_RELEASE(_sapp.ios.view);
_SAPP_OBJC_RELEASE(_sapp.ios.window);
}
_SOKOL_PRIVATE void _sapp_ios_run(const sapp_desc* desc) {
_sapp_init_state(desc);
static int argc = 1;
static char* argv[] = { (char*)"sokol_app" };
UIApplicationMain(argc, argv, nil, NSStringFromClass([_sapp_app_delegate class]));
}
/* iOS entry function */
#if !defined(SOKOL_NO_ENTRY)
int main(int argc, char* argv[]) {
sapp_desc desc = sokol_main(argc, argv);
_sapp_ios_run(&desc);
return 0;
}
#endif /* SOKOL_NO_ENTRY */
_SOKOL_PRIVATE void _sapp_ios_app_event(sapp_event_type type) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE void _sapp_ios_touch_event(sapp_event_type type, NSSet<UITouch *>* touches, UIEvent* event) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
NSEnumerator* enumerator = event.allTouches.objectEnumerator;
UITouch* ios_touch;
while ((ios_touch = [enumerator nextObject])) {
if ((_sapp.event.num_touches + 1) < SAPP_MAX_TOUCHPOINTS) {
CGPoint ios_pos = [ios_touch locationInView:_sapp.ios.view];
sapp_touchpoint* cur_point = &_sapp.event.touches[_sapp.event.num_touches++];
cur_point->identifier = (uintptr_t) ios_touch;
cur_point->pos_x = ios_pos.x * _sapp.dpi_scale;
cur_point->pos_y = ios_pos.y * _sapp.dpi_scale;
cur_point->changed = [touches containsObject:ios_touch];
}
}
if (_sapp.event.num_touches > 0) {
_sapp_call_event(&_sapp.event);
}
}
}
_SOKOL_PRIVATE void _sapp_ios_update_dimensions(void) {
CGRect screen_rect = UIScreen.mainScreen.bounds;
_sapp.framebuffer_width = (int)roundf(screen_rect.size.width * _sapp.dpi_scale);
_sapp.framebuffer_height = (int)roundf(screen_rect.size.height * _sapp.dpi_scale);
_sapp.window_width = (int)roundf(screen_rect.size.width);
_sapp.window_height = (int)roundf(screen_rect.size.height);
int cur_fb_width, cur_fb_height;
#if defined(SOKOL_METAL)
const CGSize fb_size = _sapp.ios.view.drawableSize;
cur_fb_width = (int)roundf(fb_size.width);
cur_fb_height = (int)roundf(fb_size.height);
#else
cur_fb_width = (int)roundf(_sapp.ios.view.drawableWidth);
cur_fb_height = (int)roundf(_sapp.ios.view.drawableHeight);
#endif
const bool dim_changed = (_sapp.framebuffer_width != cur_fb_width) ||
(_sapp.framebuffer_height != cur_fb_height);
if (dim_changed) {
#if defined(SOKOL_METAL)
const CGSize drawable_size = { (CGFloat) _sapp.framebuffer_width, (CGFloat) _sapp.framebuffer_height };
_sapp.ios.view.drawableSize = drawable_size;
#else
// nothing to do here, GLKView correctly respects the view's contentScaleFactor
#endif
if (!_sapp.first_frame) {
_sapp_ios_app_event(SAPP_EVENTTYPE_RESIZED);
}
}
}
_SOKOL_PRIVATE void _sapp_ios_frame(void) {
_sapp_ios_update_dimensions();
_sapp_frame();
}
_SOKOL_PRIVATE void _sapp_ios_show_keyboard(bool shown) {
/* if not happened yet, create an invisible text field */
if (nil == _sapp.ios.textfield) {
_sapp.ios.textfield_dlg = [[_sapp_textfield_dlg alloc] init];
_sapp.ios.textfield = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
_sapp.ios.textfield.keyboardType = UIKeyboardTypeDefault;
_sapp.ios.textfield.returnKeyType = UIReturnKeyDefault;
_sapp.ios.textfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
_sapp.ios.textfield.autocorrectionType = UITextAutocorrectionTypeNo;
_sapp.ios.textfield.spellCheckingType = UITextSpellCheckingTypeNo;
_sapp.ios.textfield.hidden = YES;
_sapp.ios.textfield.text = @"x";
_sapp.ios.textfield.delegate = _sapp.ios.textfield_dlg;
[_sapp.ios.view_ctrl.view addSubview:_sapp.ios.textfield];
[[NSNotificationCenter defaultCenter] addObserver:_sapp.ios.textfield_dlg
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:_sapp.ios.textfield_dlg
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:_sapp.ios.textfield_dlg
selector:@selector(keyboardDidChangeFrame:)
name:UIKeyboardDidChangeFrameNotification object:nil];
}
if (shown) {
/* setting the text field as first responder brings up the onscreen keyboard */
[_sapp.ios.textfield becomeFirstResponder];
}
else {
[_sapp.ios.textfield resignFirstResponder];
}
}
@implementation _sapp_app_delegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
CGRect screen_rect = UIScreen.mainScreen.bounds;
_sapp.ios.window = [[UIWindow alloc] initWithFrame:screen_rect];
_sapp.window_width = (int)roundf(screen_rect.size.width);
_sapp.window_height = (int)roundf(screen_rect.size.height);
if (_sapp.desc.high_dpi) {
_sapp.dpi_scale = (float) UIScreen.mainScreen.nativeScale;
}
else {
_sapp.dpi_scale = 1.0f;
}
_sapp.framebuffer_width = (int)roundf(_sapp.window_width * _sapp.dpi_scale);
_sapp.framebuffer_height = (int)roundf(_sapp.window_height * _sapp.dpi_scale);
NSInteger max_fps = UIScreen.mainScreen.maximumFramesPerSecond;
#if defined(SOKOL_METAL)
_sapp.ios.mtl_device = MTLCreateSystemDefaultDevice();
_sapp.ios.view = [[_sapp_ios_view alloc] init];
_sapp.ios.view.preferredFramesPerSecond = max_fps / _sapp.swap_interval;
_sapp.ios.view.device = _sapp.ios.mtl_device;
_sapp.ios.view.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
_sapp.ios.view.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
_sapp.ios.view.sampleCount = (NSUInteger)_sapp.sample_count;
/* NOTE: iOS MTKView seems to ignore thew view's contentScaleFactor
and automatically renders at Retina resolution. We'll disable
autoResize and instead do the resizing in _sapp_ios_update_dimensions()
*/
_sapp.ios.view.autoResizeDrawable = false;
_sapp.ios.view.userInteractionEnabled = YES;
_sapp.ios.view.multipleTouchEnabled = YES;
_sapp.ios.view_ctrl = [[UIViewController alloc] init];
_sapp.ios.view_ctrl.modalPresentationStyle = UIModalPresentationFullScreen;
_sapp.ios.view_ctrl.view = _sapp.ios.view;
_sapp.ios.window.rootViewController = _sapp.ios.view_ctrl;
#else
_sapp.ios.eagl_ctx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
_sapp.ios.view = [[_sapp_ios_view alloc] initWithFrame:screen_rect];
_sapp.ios.view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
_sapp.ios.view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
_sapp.ios.view.drawableStencilFormat = GLKViewDrawableStencilFormatNone;
GLKViewDrawableMultisample msaa = _sapp.sample_count > 1 ? GLKViewDrawableMultisample4X : GLKViewDrawableMultisampleNone;
_sapp.ios.view.drawableMultisample = msaa;
_sapp.ios.view.context = _sapp.ios.eagl_ctx;
_sapp.ios.view.enableSetNeedsDisplay = NO;
_sapp.ios.view.userInteractionEnabled = YES;
_sapp.ios.view.multipleTouchEnabled = YES;
// on GLKView, contentScaleFactor appears to work just fine!
if (_sapp.desc.high_dpi) {
_sapp.ios.view.contentScaleFactor = _sapp.dpi_scale;
}
else {
_sapp.ios.view.contentScaleFactor = 1.0;
}
_sapp.ios.view_ctrl = [[GLKViewController alloc] init];
_sapp.ios.view_ctrl.view = _sapp.ios.view;
_sapp.ios.view_ctrl.preferredFramesPerSecond = max_fps / _sapp.swap_interval;
_sapp.ios.window.rootViewController = _sapp.ios.view_ctrl;
#endif
[_sapp.ios.window makeKeyAndVisible];
_sapp.valid = true;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
if (!_sapp.ios.suspended) {
_sapp.ios.suspended = true;
_sapp_ios_app_event(SAPP_EVENTTYPE_SUSPENDED);
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (_sapp.ios.suspended) {
_sapp.ios.suspended = false;
_sapp_ios_app_event(SAPP_EVENTTYPE_RESUMED);
}
}
/* NOTE: this method will rarely ever be called, iOS application
which are terminated by the user are usually killed via signal 9
by the operating system.
*/
- (void)applicationWillTerminate:(UIApplication *)application {
_SOKOL_UNUSED(application);
_sapp_call_cleanup();
_sapp_ios_discard_state();
_sapp_discard_state();
}
@end
@implementation _sapp_textfield_dlg
- (void)keyboardWasShown:(NSNotification*)notif {
_sapp.onscreen_keyboard_shown = true;
/* query the keyboard's size, and modify the content view's size */
if (_sapp.desc.ios_keyboard_resizes_canvas) {
NSDictionary* info = notif.userInfo;
CGFloat kbd_h = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGRect view_frame = UIScreen.mainScreen.bounds;
view_frame.size.height -= kbd_h;
_sapp.ios.view.frame = view_frame;
}
}
- (void)keyboardWillBeHidden:(NSNotification*)notif {
_sapp.onscreen_keyboard_shown = false;
if (_sapp.desc.ios_keyboard_resizes_canvas) {
_sapp.ios.view.frame = UIScreen.mainScreen.bounds;
}
}
- (void)keyboardDidChangeFrame:(NSNotification*)notif {
/* this is for the case when the screen rotation changes while the keyboard is open */
if (_sapp.onscreen_keyboard_shown && _sapp.desc.ios_keyboard_resizes_canvas) {
NSDictionary* info = notif.userInfo;
CGFloat kbd_h = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGRect view_frame = UIScreen.mainScreen.bounds;
view_frame.size.height -= kbd_h;
_sapp.ios.view.frame = view_frame;
}
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
if (_sapp_events_enabled()) {
const NSUInteger len = string.length;
if (len > 0) {
for (NSUInteger i = 0; i < len; i++) {
unichar c = [string characterAtIndex:i];
if (c >= 32) {
/* ignore surrogates for now */
if ((c < 0xD800) || (c > 0xDFFF)) {
_sapp_init_event(SAPP_EVENTTYPE_CHAR);
_sapp.event.char_code = c;
_sapp_call_event(&_sapp.event);
}
}
if (c <= 32) {
sapp_keycode k = SAPP_KEYCODE_INVALID;
switch (c) {
case 10: k = SAPP_KEYCODE_ENTER; break;
case 32: k = SAPP_KEYCODE_SPACE; break;
default: break;
}
if (k != SAPP_KEYCODE_INVALID) {
_sapp_init_event(SAPP_EVENTTYPE_KEY_DOWN);
_sapp.event.key_code = k;
_sapp_call_event(&_sapp.event);
_sapp_init_event(SAPP_EVENTTYPE_KEY_UP);
_sapp.event.key_code = k;
_sapp_call_event(&_sapp.event);
}
}
}
}
else {
/* this was a backspace */
_sapp_init_event(SAPP_EVENTTYPE_KEY_DOWN);
_sapp.event.key_code = SAPP_KEYCODE_BACKSPACE;
_sapp_call_event(&_sapp.event);
_sapp_init_event(SAPP_EVENTTYPE_KEY_UP);
_sapp.event.key_code = SAPP_KEYCODE_BACKSPACE;
_sapp_call_event(&_sapp.event);
}
}
return NO;
}
@end
@implementation _sapp_ios_view
- (void)drawRect:(CGRect)rect {
_SOKOL_UNUSED(rect);
#if defined(_SAPP_ANY_GL)
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer);
#endif
_sapp_timing_measure(&_sapp.timing);
@autoreleasepool {
_sapp_ios_frame();
}
}
- (BOOL)isOpaque {
return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent*)event {
_sapp_ios_touch_event(SAPP_EVENTTYPE_TOUCHES_BEGAN, touches, event);
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent*)event {
_sapp_ios_touch_event(SAPP_EVENTTYPE_TOUCHES_MOVED, touches, event);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent*)event {
_sapp_ios_touch_event(SAPP_EVENTTYPE_TOUCHES_ENDED, touches, event);
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent*)event {
_sapp_ios_touch_event(SAPP_EVENTTYPE_TOUCHES_CANCELLED, touches, event);
}
@end
#endif /* TARGET_OS_IPHONE */
#endif /* _SAPP_APPLE */
// ███████ ███ ███ ███████ ██████ ██████ ██ ██████ ████████ ███████ ███ ██
// ██ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
// █████ ██ ████ ██ ███████ ██ ██████ ██ ██████ ██ █████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██ ███████ ██████ ██ ██ ██ ██ ██ ███████ ██ ████
//
// >>emscripten
#if defined(_SAPP_EMSCRIPTEN)
#if defined(EM_JS_DEPS)
EM_JS_DEPS(sokol_app, "$withStackSave,$stringToUTF8OnStack");
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*_sapp_html5_fetch_callback) (const sapp_html5_fetch_response*);
EMSCRIPTEN_KEEPALIVE void _sapp_emsc_onpaste(const char* str) {
if (_sapp.clipboard.enabled) {
_sapp_strcpy(str, _sapp.clipboard.buffer, _sapp.clipboard.buf_size);
if (_sapp_events_enabled()) {
_sapp_init_event(SAPP_EVENTTYPE_CLIPBOARD_PASTED);
_sapp_call_event(&_sapp.event);
}
}
}
/* https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload */
EMSCRIPTEN_KEEPALIVE int _sapp_html5_get_ask_leave_site(void) {
return _sapp.html5_ask_leave_site ? 1 : 0;
}
EMSCRIPTEN_KEEPALIVE void _sapp_emsc_begin_drop(int num) {
if (!_sapp.drop.enabled) {
return;
}
if (num < 0) {
num = 0;
}
if (num > _sapp.drop.max_files) {
num = _sapp.drop.max_files;
}
_sapp.drop.num_files = num;
_sapp_clear_drop_buffer();
}
EMSCRIPTEN_KEEPALIVE void _sapp_emsc_drop(int i, const char* name) {
/* NOTE: name is only the filename part, not a path */
if (!_sapp.drop.enabled) {
return;
}
if (0 == name) {
return;
}
SOKOL_ASSERT(_sapp.drop.num_files <= _sapp.drop.max_files);
if ((i < 0) || (i >= _sapp.drop.num_files)) {
return;
}
if (!_sapp_strcpy(name, _sapp_dropped_file_path_ptr(i), _sapp.drop.max_path_length)) {
_SAPP_ERROR(DROPPED_FILE_PATH_TOO_LONG);
_sapp.drop.num_files = 0;
}
}
EMSCRIPTEN_KEEPALIVE void _sapp_emsc_end_drop(int x, int y, int mods) {
if (!_sapp.drop.enabled) {
return;
}
if (0 == _sapp.drop.num_files) {
/* there was an error copying the filenames */
_sapp_clear_drop_buffer();
return;
}
if (_sapp_events_enabled()) {
_sapp.mouse.x = (float)x * _sapp.dpi_scale;
_sapp.mouse.y = (float)y * _sapp.dpi_scale;
_sapp.mouse.dx = 0.0f;
_sapp.mouse.dy = 0.0f;
_sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED);
// see sapp_js_add_dragndrop_listeners for mods constants
if (mods & 1) { _sapp.event.modifiers |= SAPP_MODIFIER_SHIFT; }
if (mods & 2) { _sapp.event.modifiers |= SAPP_MODIFIER_CTRL; }
if (mods & 4) { _sapp.event.modifiers |= SAPP_MODIFIER_ALT; }
if (mods & 8) { _sapp.event.modifiers |= SAPP_MODIFIER_SUPER; }
_sapp_call_event(&_sapp.event);
}
}
EMSCRIPTEN_KEEPALIVE void _sapp_emsc_invoke_fetch_cb(int index, int success, int error_code, _sapp_html5_fetch_callback callback, uint32_t fetched_size, void* buf_ptr, uint32_t buf_size, void* user_data) {
sapp_html5_fetch_response response;
_sapp_clear(&response, sizeof(response));
response.succeeded = (0 != success);
response.error_code = (sapp_html5_fetch_error) error_code;
response.file_index = index;
response.data.ptr = buf_ptr;
response.data.size = fetched_size;
response.buffer.ptr = buf_ptr;
response.buffer.size = buf_size;
response.user_data = user_data;
callback(&response);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
EM_JS(void, sapp_js_add_beforeunload_listener, (void), {
Module.sokol_beforeunload = (event) => {
if (__sapp_html5_get_ask_leave_site() != 0) {
event.preventDefault();
event.returnValue = ' ';
}
};
window.addEventListener('beforeunload', Module.sokol_beforeunload);
});
EM_JS(void, sapp_js_remove_beforeunload_listener, (void), {
window.removeEventListener('beforeunload', Module.sokol_beforeunload);
});
EM_JS(void, sapp_js_add_clipboard_listener, (void), {
Module.sokol_paste = (event) => {
const pasted_str = event.clipboardData.getData('text');
withStackSave(() => {
const cstr = stringToUTF8OnStack(pasted_str);
__sapp_emsc_onpaste(cstr);
});
};
window.addEventListener('paste', Module.sokol_paste);
});
EM_JS(void, sapp_js_remove_clipboard_listener, (void), {
window.removeEventListener('paste', Module.sokol_paste);
});
EM_JS(void, sapp_js_write_clipboard, (const char* c_str), {
const str = UTF8ToString(c_str);
const ta = document.createElement('textarea');
ta.setAttribute('autocomplete', 'off');
ta.setAttribute('autocorrect', 'off');
ta.setAttribute('autocapitalize', 'off');
ta.setAttribute('spellcheck', 'false');
ta.style.left = -100 + 'px';
ta.style.top = -100 + 'px';
ta.style.height = 1;
ta.style.width = 1;
ta.value = str;
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
});
_SOKOL_PRIVATE void _sapp_emsc_set_clipboard_string(const char* str) {
sapp_js_write_clipboard(str);
}
EM_JS(void, sapp_js_add_dragndrop_listeners, (const char* canvas_name_cstr), {
Module.sokol_drop_files = [];
const canvas_name = UTF8ToString(canvas_name_cstr);
const canvas = document.getElementById(canvas_name);
Module.sokol_dragenter = (event) => {
event.stopPropagation();
event.preventDefault();
};
Module.sokol_dragleave = (event) => {
event.stopPropagation();
event.preventDefault();
};
Module.sokol_dragover = (event) => {
event.stopPropagation();
event.preventDefault();
};
Module.sokol_drop = (event) => {
event.stopPropagation();
event.preventDefault();
const files = event.dataTransfer.files;
Module.sokol_dropped_files = files;
__sapp_emsc_begin_drop(files.length);
for (let i = 0; i < files.length; i++) {
withStackSave(() => {
const cstr = stringToUTF8OnStack(files[i].name);
__sapp_emsc_drop(i, cstr);
});
}
let mods = 0;
if (event.shiftKey) { mods |= 1; }
if (event.ctrlKey) { mods |= 2; }
if (event.altKey) { mods |= 4; }
if (event.metaKey) { mods |= 8; }
// FIXME? see computation of targetX/targetY in emscripten via getClientBoundingRect
__sapp_emsc_end_drop(event.clientX, event.clientY, mods);
};
canvas.addEventListener('dragenter', Module.sokol_dragenter, false);
canvas.addEventListener('dragleave', Module.sokol_dragleave, false);
canvas.addEventListener('dragover', Module.sokol_dragover, false);
canvas.addEventListener('drop', Module.sokol_drop, false);
});
EM_JS(uint32_t, sapp_js_dropped_file_size, (int index), {
\x2F\x2A\x2A @suppress {missingProperties} \x2A\x2F
const files = Module.sokol_dropped_files;
if ((index < 0) || (index >= files.length)) {
return 0;
}
else {
return files[index].size;
}
});
EM_JS(void, sapp_js_fetch_dropped_file, (int index, _sapp_html5_fetch_callback callback, void* buf_ptr, uint32_t buf_size, void* user_data), {
const reader = new FileReader();
reader.onload = (loadEvent) => {
const content = loadEvent.target.result;
if (content.byteLength > buf_size) {
// SAPP_HTML5_FETCH_ERROR_BUFFER_TOO_SMALL
__sapp_emsc_invoke_fetch_cb(index, 0, 1, callback, 0, buf_ptr, buf_size, user_data);
}
else {
HEAPU8.set(new Uint8Array(content), buf_ptr);
__sapp_emsc_invoke_fetch_cb(index, 1, 0, callback, content.byteLength, buf_ptr, buf_size, user_data);
}
};
reader.onerror = () => {
// SAPP_HTML5_FETCH_ERROR_OTHER
__sapp_emsc_invoke_fetch_cb(index, 0, 2, callback, 0, buf_ptr, buf_size, user_data);
};
\x2F\x2A\x2A @suppress {missingProperties} \x2A\x2F
const files = Module.sokol_dropped_files;
reader.readAsArrayBuffer(files[index]);
});
EM_JS(void, sapp_js_remove_dragndrop_listeners, (const char* canvas_name_cstr), {
const canvas_name = UTF8ToString(canvas_name_cstr);
const canvas = document.getElementById(canvas_name);
canvas.removeEventListener('dragenter', Module.sokol_dragenter);
canvas.removeEventListener('dragleave', Module.sokol_dragleave);
canvas.removeEventListener('dragover', Module.sokol_dragover);
canvas.removeEventListener('drop', Module.sokol_drop);
});
EM_JS(void, sapp_js_init, (const char* c_str_target), {
// lookup and store canvas object by name
const target_str = UTF8ToString(c_str_target);
Module.sapp_emsc_target = document.getElementById(target_str);
if (!Module.sapp_emsc_target) {
console.log("sokol_app.h: invalid target:" + target_str);
}
if (!Module.sapp_emsc_target.requestPointerLock) {
console.log("sokol_app.h: target doesn't support requestPointerLock:" + target_str);
}
});
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_pointerlockchange_cb(int emsc_type, const EmscriptenPointerlockChangeEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(emsc_type);
_SOKOL_UNUSED(user_data);
_sapp.mouse.locked = emsc_event->isActive;
return EM_TRUE;
}
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_pointerlockerror_cb(int emsc_type, const void* reserved, void* user_data) {
_SOKOL_UNUSED(emsc_type);
_SOKOL_UNUSED(reserved);
_SOKOL_UNUSED(user_data);
_sapp.mouse.locked = false;
_sapp.emsc.mouse_lock_requested = false;
return true;
}
EM_JS(void, sapp_js_request_pointerlock, (void), {
if (Module.sapp_emsc_target) {
if (Module.sapp_emsc_target.requestPointerLock) {
Module.sapp_emsc_target.requestPointerLock();
}
}
});
EM_JS(void, sapp_js_exit_pointerlock, (void), {
if (document.exitPointerLock) {
document.exitPointerLock();
}
});
_SOKOL_PRIVATE void _sapp_emsc_lock_mouse(bool lock) {
if (lock) {
/* request mouse-lock during event handler invocation (see _sapp_emsc_update_mouse_lock_state) */
_sapp.emsc.mouse_lock_requested = true;
}
else {
/* NOTE: the _sapp.mouse_locked state will be set in the pointerlockchange callback */
_sapp.emsc.mouse_lock_requested = false;
sapp_js_exit_pointerlock();
}
}
/* called from inside event handlers to check if mouse lock had been requested,
and if yes, actually enter mouse lock.
*/
_SOKOL_PRIVATE void _sapp_emsc_update_mouse_lock_state(void) {
if (_sapp.emsc.mouse_lock_requested) {
_sapp.emsc.mouse_lock_requested = false;
sapp_js_request_pointerlock();
}
}
// set mouse cursor type
EM_JS(void, sapp_js_set_cursor, (int cursor_type, int shown), {
if (Module.sapp_emsc_target) {
let cursor;
if (shown === 0) {
cursor = "none";
}
else switch (cursor_type) {
case 0: cursor = "auto"; break; // SAPP_MOUSECURSOR_DEFAULT
case 1: cursor = "default"; break; // SAPP_MOUSECURSOR_ARROW
case 2: cursor = "text"; break; // SAPP_MOUSECURSOR_IBEAM
case 3: cursor = "crosshair"; break; // SAPP_MOUSECURSOR_CROSSHAIR
case 4: cursor = "pointer"; break; // SAPP_MOUSECURSOR_POINTING_HAND
case 5: cursor = "ew-resize"; break; // SAPP_MOUSECURSOR_RESIZE_EW
case 6: cursor = "ns-resize"; break; // SAPP_MOUSECURSOR_RESIZE_NS
case 7: cursor = "nwse-resize"; break; // SAPP_MOUSECURSOR_RESIZE_NWSE
case 8: cursor = "nesw-resize"; break; // SAPP_MOUSECURSOR_RESIZE_NESW
case 9: cursor = "all-scroll"; break; // SAPP_MOUSECURSOR_RESIZE_ALL
case 10: cursor = "not-allowed"; break; // SAPP_MOUSECURSOR_NOT_ALLOWED
default: cursor = "auto"; break;
}
Module.sapp_emsc_target.style.cursor = cursor;
}
});
_SOKOL_PRIVATE void _sapp_emsc_update_cursor(sapp_mouse_cursor cursor, bool shown) {
SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM));
sapp_js_set_cursor((int)cursor, shown ? 1 : 0);
}
/* JS helper functions to update browser tab favicon */
EM_JS(void, sapp_js_clear_favicon, (void), {
const link = document.getElementById('sokol-app-favicon');
if (link) {
document.head.removeChild(link);
}
});
EM_JS(void, sapp_js_set_favicon, (int w, int h, const uint8_t* pixels), {
const canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
const ctx = canvas.getContext('2d');
const img_data = ctx.createImageData(w, h);
img_data.data.set(HEAPU8.subarray(pixels, pixels + w*h*4));
ctx.putImageData(img_data, 0, 0);
const new_link = document.createElement('link');
new_link.id = 'sokol-app-favicon';
new_link.rel = 'shortcut icon';
new_link.href = canvas.toDataURL();
document.head.appendChild(new_link);
});
_SOKOL_PRIVATE void _sapp_emsc_set_icon(const sapp_icon_desc* icon_desc, int num_images) {
SOKOL_ASSERT((num_images > 0) && (num_images <= SAPP_MAX_ICONIMAGES));
sapp_js_clear_favicon();
// find the best matching image candidate for 16x16 pixels
int img_index = _sapp_image_bestmatch(icon_desc->images, num_images, 16, 16);
const sapp_image_desc* img_desc = &icon_desc->images[img_index];
sapp_js_set_favicon(img_desc->width, img_desc->height, (const uint8_t*) img_desc->pixels.ptr);
}
_SOKOL_PRIVATE uint32_t _sapp_emsc_mouse_button_mods(uint16_t buttons) {
uint32_t m = 0;
if (0 != (buttons & (1<<0))) { m |= SAPP_MODIFIER_LMB; }
if (0 != (buttons & (1<<1))) { m |= SAPP_MODIFIER_RMB; } // not a bug
if (0 != (buttons & (1<<2))) { m |= SAPP_MODIFIER_MMB; } // not a bug
return m;
}
_SOKOL_PRIVATE uint32_t _sapp_emsc_mouse_event_mods(const EmscriptenMouseEvent* ev) {
uint32_t m = 0;
if (ev->ctrlKey) { m |= SAPP_MODIFIER_CTRL; }
if (ev->shiftKey) { m |= SAPP_MODIFIER_SHIFT; }
if (ev->altKey) { m |= SAPP_MODIFIER_ALT; }
if (ev->metaKey) { m |= SAPP_MODIFIER_SUPER; }
m |= _sapp_emsc_mouse_button_mods(_sapp.emsc.mouse_buttons);
return m;
}
_SOKOL_PRIVATE uint32_t _sapp_emsc_key_event_mods(const EmscriptenKeyboardEvent* ev) {
uint32_t m = 0;
if (ev->ctrlKey) { m |= SAPP_MODIFIER_CTRL; }
if (ev->shiftKey) { m |= SAPP_MODIFIER_SHIFT; }
if (ev->altKey) { m |= SAPP_MODIFIER_ALT; }
if (ev->metaKey) { m |= SAPP_MODIFIER_SUPER; }
m |= _sapp_emsc_mouse_button_mods(_sapp.emsc.mouse_buttons);
return m;
}
_SOKOL_PRIVATE uint32_t _sapp_emsc_touch_event_mods(const EmscriptenTouchEvent* ev) {
uint32_t m = 0;
if (ev->ctrlKey) { m |= SAPP_MODIFIER_CTRL; }
if (ev->shiftKey) { m |= SAPP_MODIFIER_SHIFT; }
if (ev->altKey) { m |= SAPP_MODIFIER_ALT; }
if (ev->metaKey) { m |= SAPP_MODIFIER_SUPER; }
m |= _sapp_emsc_mouse_button_mods(_sapp.emsc.mouse_buttons);
return m;
}
#if defined(SOKOL_WGPU)
_SOKOL_PRIVATE void _sapp_emsc_wgpu_size_changed(void);
#endif
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_size_changed(int event_type, const EmscriptenUiEvent* ui_event, void* user_data) {
_SOKOL_UNUSED(event_type);
_SOKOL_UNUSED(user_data);
double w, h;
emscripten_get_element_css_size(_sapp.html5_canvas_selector, &w, &h);
/* The above method might report zero when toggling HTML5 fullscreen,
in that case use the window's inner width reported by the
emscripten event. This works ok when toggling *into* fullscreen
but doesn't properly restore the previous canvas size when switching
back from fullscreen.
In general, due to the HTML5's fullscreen API's flaky nature it is
recommended to use 'soft fullscreen' (stretching the WebGL canvas
over the browser windows client rect) with a CSS definition like this:
position: absolute;
top: 0px;
left: 0px;
margin: 0px;
border: 0;
width: 100%;
height: 100%;
overflow: hidden;
display: block;
*/
if (w < 1.0) {
w = ui_event->windowInnerWidth;
}
else {
_sapp.window_width = (int)roundf(w);
}
if (h < 1.0) {
h = ui_event->windowInnerHeight;
}
else {
_sapp.window_height = (int)roundf(h);
}
if (_sapp.desc.high_dpi) {
_sapp.dpi_scale = emscripten_get_device_pixel_ratio();
}
_sapp.framebuffer_width = (int)roundf(w * _sapp.dpi_scale);
_sapp.framebuffer_height = (int)roundf(h * _sapp.dpi_scale);
SOKOL_ASSERT((_sapp.framebuffer_width > 0) && (_sapp.framebuffer_height > 0));
emscripten_set_canvas_element_size(_sapp.html5_canvas_selector, _sapp.framebuffer_width, _sapp.framebuffer_height);
#if defined(SOKOL_WGPU)
// on WebGPU: recreate size-dependent rendering surfaces
_sapp_emsc_wgpu_size_changed();
#endif
if (_sapp_events_enabled()) {
_sapp_init_event(SAPP_EVENTTYPE_RESIZED);
_sapp_call_event(&_sapp.event);
}
return true;
}
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_mouse_cb(int emsc_type, const EmscriptenMouseEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(user_data);
bool consume_event = !_sapp.desc.html5_bubble_mouse_events;
_sapp.emsc.mouse_buttons = emsc_event->buttons;
if (_sapp.mouse.locked) {
_sapp.mouse.dx = (float) emsc_event->movementX;
_sapp.mouse.dy = (float) emsc_event->movementY;
} else {
float new_x = emsc_event->targetX * _sapp.dpi_scale;
float new_y = emsc_event->targetY * _sapp.dpi_scale;
if (_sapp.mouse.pos_valid) {
_sapp.mouse.dx = new_x - _sapp.mouse.x;
_sapp.mouse.dy = new_y - _sapp.mouse.y;
}
_sapp.mouse.x = new_x;
_sapp.mouse.y = new_y;
_sapp.mouse.pos_valid = true;
}
if (_sapp_events_enabled() && (emsc_event->button >= 0) && (emsc_event->button < SAPP_MAX_MOUSEBUTTONS)) {
sapp_event_type type;
bool is_button_event = false;
bool clear_dxdy = false;
switch (emsc_type) {
case EMSCRIPTEN_EVENT_MOUSEDOWN:
type = SAPP_EVENTTYPE_MOUSE_DOWN;
is_button_event = true;
break;
case EMSCRIPTEN_EVENT_MOUSEUP:
type = SAPP_EVENTTYPE_MOUSE_UP;
is_button_event = true;
break;
case EMSCRIPTEN_EVENT_MOUSEMOVE:
type = SAPP_EVENTTYPE_MOUSE_MOVE;
break;
case EMSCRIPTEN_EVENT_MOUSEENTER:
type = SAPP_EVENTTYPE_MOUSE_ENTER;
clear_dxdy = true;
break;
case EMSCRIPTEN_EVENT_MOUSELEAVE:
type = SAPP_EVENTTYPE_MOUSE_LEAVE;
clear_dxdy = true;
break;
default:
type = SAPP_EVENTTYPE_INVALID;
break;
}
if (clear_dxdy) {
_sapp.mouse.dx = 0.0f;
_sapp.mouse.dy = 0.0f;
}
if (type != SAPP_EVENTTYPE_INVALID) {
_sapp_init_event(type);
_sapp.event.modifiers = _sapp_emsc_mouse_event_mods(emsc_event);
if (is_button_event) {
switch (emsc_event->button) {
case 0: _sapp.event.mouse_button = SAPP_MOUSEBUTTON_LEFT; break;
case 1: _sapp.event.mouse_button = SAPP_MOUSEBUTTON_MIDDLE; break;
case 2: _sapp.event.mouse_button = SAPP_MOUSEBUTTON_RIGHT; break;
default: _sapp.event.mouse_button = (sapp_mousebutton)emsc_event->button; break;
}
} else {
_sapp.event.mouse_button = SAPP_MOUSEBUTTON_INVALID;
}
consume_event |= _sapp_call_event(&_sapp.event);
}
// mouse lock can only be activated in mouse button events (not in move, enter or leave)
if (is_button_event) {
_sapp_emsc_update_mouse_lock_state();
}
}
return consume_event;
}
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_wheel_cb(int emsc_type, const EmscriptenWheelEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(emsc_type);
_SOKOL_UNUSED(user_data);
bool consume_event = !_sapp.desc.html5_bubble_wheel_events;
_sapp.emsc.mouse_buttons = emsc_event->mouse.buttons;
if (_sapp_events_enabled()) {
_sapp_init_event(SAPP_EVENTTYPE_MOUSE_SCROLL);
_sapp.event.modifiers = _sapp_emsc_mouse_event_mods(&emsc_event->mouse);
/* see https://github.com/floooh/sokol/issues/339 */
float scale;
switch (emsc_event->deltaMode) {
case DOM_DELTA_PIXEL: scale = -0.04f; break;
case DOM_DELTA_LINE: scale = -1.33f; break;
case DOM_DELTA_PAGE: scale = -10.0f; break; // FIXME: this is a guess
default: scale = -0.1f; break; // shouldn't happen
}
_sapp.event.scroll_x = scale * (float)emsc_event->deltaX;
_sapp.event.scroll_y = scale * (float)emsc_event->deltaY;
consume_event |= _sapp_call_event(&_sapp.event);
}
_sapp_emsc_update_mouse_lock_state();
return consume_event;
}
static struct {
const char* str;
sapp_keycode code;
} _sapp_emsc_keymap[] = {
{ "Backspace", SAPP_KEYCODE_BACKSPACE },
{ "Tab", SAPP_KEYCODE_TAB },
{ "Enter", SAPP_KEYCODE_ENTER },
{ "ShiftLeft", SAPP_KEYCODE_LEFT_SHIFT },
{ "ShiftRight", SAPP_KEYCODE_RIGHT_SHIFT },
{ "ControlLeft", SAPP_KEYCODE_LEFT_CONTROL },
{ "ControlRight", SAPP_KEYCODE_RIGHT_CONTROL },
{ "AltLeft", SAPP_KEYCODE_LEFT_ALT },
{ "AltRight", SAPP_KEYCODE_RIGHT_ALT },
{ "Pause", SAPP_KEYCODE_PAUSE },
{ "CapsLock", SAPP_KEYCODE_CAPS_LOCK },
{ "Escape", SAPP_KEYCODE_ESCAPE },
{ "Space", SAPP_KEYCODE_SPACE },
{ "PageUp", SAPP_KEYCODE_PAGE_UP },
{ "PageDown", SAPP_KEYCODE_PAGE_DOWN },
{ "End", SAPP_KEYCODE_END },
{ "Home", SAPP_KEYCODE_HOME },
{ "ArrowLeft", SAPP_KEYCODE_LEFT },
{ "ArrowUp", SAPP_KEYCODE_UP },
{ "ArrowRight", SAPP_KEYCODE_RIGHT },
{ "ArrowDown", SAPP_KEYCODE_DOWN },
{ "PrintScreen", SAPP_KEYCODE_PRINT_SCREEN },
{ "Insert", SAPP_KEYCODE_INSERT },
{ "Delete", SAPP_KEYCODE_DELETE },
{ "Digit0", SAPP_KEYCODE_0 },
{ "Digit1", SAPP_KEYCODE_1 },
{ "Digit2", SAPP_KEYCODE_2 },
{ "Digit3", SAPP_KEYCODE_3 },
{ "Digit4", SAPP_KEYCODE_4 },
{ "Digit5", SAPP_KEYCODE_5 },
{ "Digit6", SAPP_KEYCODE_6 },
{ "Digit7", SAPP_KEYCODE_7 },
{ "Digit8", SAPP_KEYCODE_8 },
{ "Digit9", SAPP_KEYCODE_9 },
{ "KeyA", SAPP_KEYCODE_A },
{ "KeyB", SAPP_KEYCODE_B },
{ "KeyC", SAPP_KEYCODE_C },
{ "KeyD", SAPP_KEYCODE_D },
{ "KeyE", SAPP_KEYCODE_E },
{ "KeyF", SAPP_KEYCODE_F },
{ "KeyG", SAPP_KEYCODE_G },
{ "KeyH", SAPP_KEYCODE_H },
{ "KeyI", SAPP_KEYCODE_I },
{ "KeyJ", SAPP_KEYCODE_J },
{ "KeyK", SAPP_KEYCODE_K },
{ "KeyL", SAPP_KEYCODE_L },
{ "KeyM", SAPP_KEYCODE_M },
{ "KeyN", SAPP_KEYCODE_N },
{ "KeyO", SAPP_KEYCODE_O },
{ "KeyP", SAPP_KEYCODE_P },
{ "KeyQ", SAPP_KEYCODE_Q },
{ "KeyR", SAPP_KEYCODE_R },
{ "KeyS", SAPP_KEYCODE_S },
{ "KeyT", SAPP_KEYCODE_T },
{ "KeyU", SAPP_KEYCODE_U },
{ "KeyV", SAPP_KEYCODE_V },
{ "KeyW", SAPP_KEYCODE_W },
{ "KeyX", SAPP_KEYCODE_X },
{ "KeyY", SAPP_KEYCODE_Y },
{ "KeyZ", SAPP_KEYCODE_Z },
{ "MetaLeft", SAPP_KEYCODE_LEFT_SUPER },
{ "MetaRight", SAPP_KEYCODE_RIGHT_SUPER },
{ "Numpad0", SAPP_KEYCODE_KP_0 },
{ "Numpad1", SAPP_KEYCODE_KP_1 },
{ "Numpad2", SAPP_KEYCODE_KP_2 },
{ "Numpad3", SAPP_KEYCODE_KP_3 },
{ "Numpad4", SAPP_KEYCODE_KP_4 },
{ "Numpad5", SAPP_KEYCODE_KP_5 },
{ "Numpad6", SAPP_KEYCODE_KP_6 },
{ "Numpad7", SAPP_KEYCODE_KP_7 },
{ "Numpad8", SAPP_KEYCODE_KP_8 },
{ "Numpad9", SAPP_KEYCODE_KP_9 },
{ "NumpadMultiply", SAPP_KEYCODE_KP_MULTIPLY },
{ "NumpadAdd", SAPP_KEYCODE_KP_ADD },
{ "NumpadSubtract", SAPP_KEYCODE_KP_SUBTRACT },
{ "NumpadDecimal", SAPP_KEYCODE_KP_DECIMAL },
{ "NumpadDivide", SAPP_KEYCODE_KP_DIVIDE },
{ "F1", SAPP_KEYCODE_F1 },
{ "F2", SAPP_KEYCODE_F2 },
{ "F3", SAPP_KEYCODE_F3 },
{ "F4", SAPP_KEYCODE_F4 },
{ "F5", SAPP_KEYCODE_F5 },
{ "F6", SAPP_KEYCODE_F6 },
{ "F7", SAPP_KEYCODE_F7 },
{ "F8", SAPP_KEYCODE_F8 },
{ "F9", SAPP_KEYCODE_F9 },
{ "F10", SAPP_KEYCODE_F10 },
{ "F11", SAPP_KEYCODE_F11 },
{ "F12", SAPP_KEYCODE_F12 },
{ "NumLock", SAPP_KEYCODE_NUM_LOCK },
{ "ScrollLock", SAPP_KEYCODE_SCROLL_LOCK },
{ "Semicolon", SAPP_KEYCODE_SEMICOLON },
{ "Equal", SAPP_KEYCODE_EQUAL },
{ "Comma", SAPP_KEYCODE_COMMA },
{ "Minus", SAPP_KEYCODE_MINUS },
{ "Period", SAPP_KEYCODE_PERIOD },
{ "Slash", SAPP_KEYCODE_SLASH },
{ "Backquote", SAPP_KEYCODE_GRAVE_ACCENT },
{ "BracketLeft", SAPP_KEYCODE_LEFT_BRACKET },
{ "Backslash", SAPP_KEYCODE_BACKSLASH },
{ "BracketRight", SAPP_KEYCODE_RIGHT_BRACKET },
{ "Quote", SAPP_KEYCODE_GRAVE_ACCENT }, // FIXME: ???
{ 0, SAPP_KEYCODE_INVALID },
};
_SOKOL_PRIVATE sapp_keycode _sapp_emsc_translate_key(const char* str) {
int i = 0;
const char* keystr;
while (( keystr = _sapp_emsc_keymap[i].str )) {
if (0 == strcmp(str, keystr)) {
return _sapp_emsc_keymap[i].code;
}
i += 1;
}
return SAPP_KEYCODE_INVALID;
}
// returns true if the key code is a 'character key', this is used to decide
// if a key event needs to bubble up to create a char event
_SOKOL_PRIVATE bool _sapp_emsc_is_char_key(sapp_keycode key_code) {
return key_code < SAPP_KEYCODE_WORLD_1;
}
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboardEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(user_data);
bool consume_event = false;
if (_sapp_events_enabled()) {
sapp_event_type type;
switch (emsc_type) {
case EMSCRIPTEN_EVENT_KEYDOWN:
type = SAPP_EVENTTYPE_KEY_DOWN;
break;
case EMSCRIPTEN_EVENT_KEYUP:
type = SAPP_EVENTTYPE_KEY_UP;
break;
case EMSCRIPTEN_EVENT_KEYPRESS:
type = SAPP_EVENTTYPE_CHAR;
break;
default:
type = SAPP_EVENTTYPE_INVALID;
break;
}
if (type != SAPP_EVENTTYPE_INVALID) {
bool send_keyup_followup = false;
_sapp_init_event(type);
_sapp.event.key_repeat = emsc_event->repeat;
_sapp.event.modifiers = _sapp_emsc_key_event_mods(emsc_event);
if (type == SAPP_EVENTTYPE_CHAR) {
// NOTE: charCode doesn't appear to be supported on Android Chrome
_sapp.event.char_code = emsc_event->charCode;
consume_event |= !_sapp.desc.html5_bubble_char_events;
} else {
if (0 != emsc_event->code[0]) {
// This code path is for desktop browsers which send untranslated 'physical' key code strings
// (which is what we actually want for key events)
_sapp.event.key_code = _sapp_emsc_translate_key(emsc_event->code);
} else {
// This code path is for mobile browsers which only send localized key code
// strings. Note that the translation will only work for a small subset
// of localization-agnostic keys (like Enter, arrow keys, etc...), but
// regular alpha-numeric keys will all result in an SAPP_KEYCODE_INVALID)
_sapp.event.key_code = _sapp_emsc_translate_key(emsc_event->key);
}
// Special hack for macOS: if the Super key is pressed, macOS doesn't
// send keyUp events. As a workaround, to prevent keys from
// "sticking", we'll send a keyup event following a keydown
// when the SUPER key is pressed
if ((type == SAPP_EVENTTYPE_KEY_DOWN) &&
(_sapp.event.key_code != SAPP_KEYCODE_LEFT_SUPER) &&
(_sapp.event.key_code != SAPP_KEYCODE_RIGHT_SUPER) &&
(_sapp.event.modifiers & SAPP_MODIFIER_SUPER))
{
send_keyup_followup = true;
}
// 'character key events' will always need to bubble up, otherwise the browser
// wouldn't be able to generate character events.
if (!_sapp_emsc_is_char_key(_sapp.event.key_code)) {
consume_event |= !_sapp.desc.html5_bubble_key_events;
}
}
consume_event |= _sapp_call_event(&_sapp.event);
if (send_keyup_followup) {
_sapp.event.type = SAPP_EVENTTYPE_KEY_UP;
consume_event |= _sapp_call_event(&_sapp.event);
}
}
}
_sapp_emsc_update_mouse_lock_state();
return consume_event;
}
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_touch_cb(int emsc_type, const EmscriptenTouchEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(user_data);
bool consume_event = !_sapp.desc.html5_bubble_touch_events;
if (_sapp_events_enabled()) {
sapp_event_type type;
switch (emsc_type) {
case EMSCRIPTEN_EVENT_TOUCHSTART:
type = SAPP_EVENTTYPE_TOUCHES_BEGAN;
break;
case EMSCRIPTEN_EVENT_TOUCHMOVE:
type = SAPP_EVENTTYPE_TOUCHES_MOVED;
break;
case EMSCRIPTEN_EVENT_TOUCHEND:
type = SAPP_EVENTTYPE_TOUCHES_ENDED;
break;
case EMSCRIPTEN_EVENT_TOUCHCANCEL:
type = SAPP_EVENTTYPE_TOUCHES_CANCELLED;
break;
default:
type = SAPP_EVENTTYPE_INVALID;
break;
}
if (type != SAPP_EVENTTYPE_INVALID) {
_sapp_init_event(type);
_sapp.event.modifiers = _sapp_emsc_touch_event_mods(emsc_event);
_sapp.event.num_touches = emsc_event->numTouches;
if (_sapp.event.num_touches > SAPP_MAX_TOUCHPOINTS) {
_sapp.event.num_touches = SAPP_MAX_TOUCHPOINTS;
}
for (int i = 0; i < _sapp.event.num_touches; i++) {
const EmscriptenTouchPoint* src = &emsc_event->touches[i];
sapp_touchpoint* dst = &_sapp.event.touches[i];
dst->identifier = (uintptr_t)src->identifier;
dst->pos_x = src->targetX * _sapp.dpi_scale;
dst->pos_y = src->targetY * _sapp.dpi_scale;
dst->changed = src->isChanged;
}
consume_event |= _sapp_call_event(&_sapp.event);
}
}
return consume_event;
}
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_focus_cb(int emsc_type, const EmscriptenFocusEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(emsc_type);
_SOKOL_UNUSED(emsc_event);
_SOKOL_UNUSED(user_data);
if (_sapp_events_enabled()) {
_sapp_init_event(SAPP_EVENTTYPE_FOCUSED);
_sapp_call_event(&_sapp.event);
}
return true;
}
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_blur_cb(int emsc_type, const EmscriptenFocusEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(emsc_type);
_SOKOL_UNUSED(emsc_event);
_SOKOL_UNUSED(user_data);
if (_sapp_events_enabled()) {
_sapp_init_event(SAPP_EVENTTYPE_UNFOCUSED);
_sapp_call_event(&_sapp.event);
}
return true;
}
#if defined(SOKOL_GLES3)
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_webgl_context_cb(int emsc_type, const void* reserved, void* user_data) {
_SOKOL_UNUSED(reserved);
_SOKOL_UNUSED(user_data);
sapp_event_type type;
switch (emsc_type) {
case EMSCRIPTEN_EVENT_WEBGLCONTEXTLOST: type = SAPP_EVENTTYPE_SUSPENDED; break;
case EMSCRIPTEN_EVENT_WEBGLCONTEXTRESTORED: type = SAPP_EVENTTYPE_RESUMED; break;
default: type = SAPP_EVENTTYPE_INVALID; break;
}
if (_sapp_events_enabled() && (SAPP_EVENTTYPE_INVALID != type)) {
_sapp_init_event(type);
_sapp_call_event(&_sapp.event);
}
return true;
}
_SOKOL_PRIVATE void _sapp_emsc_webgl_init(void) {
EmscriptenWebGLContextAttributes attrs;
emscripten_webgl_init_context_attributes(&attrs);
attrs.alpha = _sapp.desc.alpha;
attrs.depth = true;
attrs.stencil = true;
attrs.antialias = _sapp.sample_count > 1;
attrs.premultipliedAlpha = _sapp.desc.html5_premultiplied_alpha;
attrs.preserveDrawingBuffer = _sapp.desc.html5_preserve_drawing_buffer;
attrs.enableExtensionsByDefault = true;
attrs.majorVersion = 2;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context(_sapp.html5_canvas_selector, &attrs);
// FIXME: error message?
emscripten_webgl_make_context_current(ctx);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer);
// FIXME: remove PVRTC support here and in sokol-gfx at some point
// some WebGL extension are not enabled automatically by emscripten
emscripten_webgl_enable_extension(ctx, "WEBKIT_WEBGL_compressed_texture_pvrtc");
}
#endif
#if defined(SOKOL_WGPU)
_SOKOL_PRIVATE void _sapp_emsc_wgpu_create_swapchain(void) {
SOKOL_ASSERT(_sapp.wgpu.instance);
SOKOL_ASSERT(_sapp.wgpu.device);
SOKOL_ASSERT(0 == _sapp.wgpu.surface);
SOKOL_ASSERT(0 == _sapp.wgpu.swapchain);
SOKOL_ASSERT(0 == _sapp.wgpu.msaa_tex);
SOKOL_ASSERT(0 == _sapp.wgpu.msaa_view);
SOKOL_ASSERT(0 == _sapp.wgpu.depth_stencil_tex);
SOKOL_ASSERT(0 == _sapp.wgpu.depth_stencil_view);
SOKOL_ASSERT(0 == _sapp.wgpu.swapchain_view);
WGPUSurfaceDescriptorFromCanvasHTMLSelector canvas_desc;
_sapp_clear(&canvas_desc, sizeof(canvas_desc));
canvas_desc.chain.sType = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector;
canvas_desc.selector = _sapp.html5_canvas_selector;
WGPUSurfaceDescriptor surf_desc;
_sapp_clear(&surf_desc, sizeof(surf_desc));
surf_desc.nextInChain = &canvas_desc.chain;
_sapp.wgpu.surface = wgpuInstanceCreateSurface(_sapp.wgpu.instance, &surf_desc);
if (0 == _sapp.wgpu.surface) {
_SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_SURFACE_FAILED);
}
_sapp.wgpu.render_format = wgpuSurfaceGetPreferredFormat(_sapp.wgpu.surface, _sapp.wgpu.adapter);
WGPUSwapChainDescriptor sc_desc;
_sapp_clear(&sc_desc, sizeof(sc_desc));
sc_desc.usage = WGPUTextureUsage_RenderAttachment;
sc_desc.format = _sapp.wgpu.render_format;
sc_desc.width = (uint32_t)_sapp.framebuffer_width;
sc_desc.height = (uint32_t)_sapp.framebuffer_height;
sc_desc.presentMode = WGPUPresentMode_Fifo;
_sapp.wgpu.swapchain = wgpuDeviceCreateSwapChain(_sapp.wgpu.device, _sapp.wgpu.surface, &sc_desc);
if (0 == _sapp.wgpu.swapchain) {
_SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_SWAPCHAIN_FAILED);
}
WGPUTextureDescriptor ds_desc;
_sapp_clear(&ds_desc, sizeof(ds_desc));
ds_desc.usage = WGPUTextureUsage_RenderAttachment;
ds_desc.dimension = WGPUTextureDimension_2D;
ds_desc.size.width = (uint32_t)_sapp.framebuffer_width;
ds_desc.size.height = (uint32_t)_sapp.framebuffer_height;
ds_desc.size.depthOrArrayLayers = 1;
ds_desc.format = WGPUTextureFormat_Depth32FloatStencil8;
ds_desc.mipLevelCount = 1;
ds_desc.sampleCount = (uint32_t)_sapp.sample_count;
_sapp.wgpu.depth_stencil_tex = wgpuDeviceCreateTexture(_sapp.wgpu.device, &ds_desc);
if (0 == _sapp.wgpu.depth_stencil_tex) {
_SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_DEPTH_STENCIL_TEXTURE_FAILED);
}
_sapp.wgpu.depth_stencil_view = wgpuTextureCreateView(_sapp.wgpu.depth_stencil_tex, 0);
if (0 == _sapp.wgpu.depth_stencil_view) {
_SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_DEPTH_STENCIL_VIEW_FAILED);
}
if (_sapp.sample_count > 1) {
WGPUTextureDescriptor msaa_desc;
_sapp_clear(&msaa_desc, sizeof(msaa_desc));
msaa_desc.usage = WGPUTextureUsage_RenderAttachment;
msaa_desc.dimension = WGPUTextureDimension_2D;
msaa_desc.size.width = (uint32_t)_sapp.framebuffer_width;
msaa_desc.size.height = (uint32_t)_sapp.framebuffer_height;
msaa_desc.size.depthOrArrayLayers = 1;
msaa_desc.format = _sapp.wgpu.render_format;
msaa_desc.mipLevelCount = 1;
msaa_desc.sampleCount = (uint32_t)_sapp.sample_count;
_sapp.wgpu.msaa_tex = wgpuDeviceCreateTexture(_sapp.wgpu.device, &msaa_desc);
if (0 == _sapp.wgpu.msaa_tex) {
_SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_MSAA_TEXTURE_FAILED);
}
_sapp.wgpu.msaa_view = wgpuTextureCreateView(_sapp.wgpu.msaa_tex, 0);
if (0 == _sapp.wgpu.msaa_view) {
_SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_MSAA_VIEW_FAILED);
}
}
}
_SOKOL_PRIVATE void _sapp_emsc_wgpu_discard_swapchain(void) {
if (_sapp.wgpu.msaa_view) {
wgpuTextureViewRelease(_sapp.wgpu.msaa_view);
_sapp.wgpu.msaa_view = 0;
}
if (_sapp.wgpu.msaa_tex) {
wgpuTextureRelease(_sapp.wgpu.msaa_tex);
_sapp.wgpu.msaa_tex = 0;
}
if (_sapp.wgpu.depth_stencil_view) {
wgpuTextureViewRelease(_sapp.wgpu.depth_stencil_view);
_sapp.wgpu.depth_stencil_view = 0;
}
if (_sapp.wgpu.depth_stencil_tex) {
wgpuTextureRelease(_sapp.wgpu.depth_stencil_tex);
_sapp.wgpu.depth_stencil_tex = 0;
}
if (_sapp.wgpu.swapchain) {
wgpuSwapChainRelease(_sapp.wgpu.swapchain);
_sapp.wgpu.swapchain = 0;
}
if (_sapp.wgpu.surface) {
wgpuSurfaceRelease(_sapp.wgpu.surface);
_sapp.wgpu.surface = 0;
}
}
_SOKOL_PRIVATE void _sapp_emsc_wgpu_size_changed(void) {
_sapp_emsc_wgpu_discard_swapchain();
_sapp_emsc_wgpu_create_swapchain();
}
_SOKOL_PRIVATE void _sapp_emsc_wgpu_request_device_cb(WGPURequestDeviceStatus status, WGPUDevice device, const char* msg, void* userdata) {
_SOKOL_UNUSED(msg);
_SOKOL_UNUSED(userdata);
SOKOL_ASSERT(!_sapp.wgpu.async_init_done);
if (status != WGPURequestDeviceStatus_Success) {
if (status == WGPURequestDeviceStatus_Error) {
_SAPP_PANIC(WGPU_REQUEST_DEVICE_STATUS_ERROR);
} else {
_SAPP_PANIC(WGPU_REQUEST_DEVICE_STATUS_UNKNOWN);
}
}
SOKOL_ASSERT(device);
_sapp.wgpu.device = device;
_sapp_emsc_wgpu_create_swapchain();
_sapp.wgpu.async_init_done = true;
}
_SOKOL_PRIVATE void _sapp_emsc_wgpu_request_adapter_cb(WGPURequestAdapterStatus status, WGPUAdapter adapter, const char* msg, void* userdata) {
_SOKOL_UNUSED(msg);
_SOKOL_UNUSED(userdata);
if (status != WGPURequestAdapterStatus_Success) {
switch (status) {
case WGPURequestAdapterStatus_Unavailable: _SAPP_PANIC(WGPU_REQUEST_ADAPTER_STATUS_UNAVAILABLE); break;
case WGPURequestAdapterStatus_Error: _SAPP_PANIC(WGPU_REQUEST_ADAPTER_STATUS_ERROR); break;
default: _SAPP_PANIC(WGPU_REQUEST_ADAPTER_STATUS_UNKNOWN); break;
}
}
SOKOL_ASSERT(adapter);
_sapp.wgpu.adapter = adapter;
size_t cur_feature_index = 1;
#define _SAPP_WGPU_MAX_REQUESTED_FEATURES (8)
WGPUFeatureName requiredFeatures[_SAPP_WGPU_MAX_REQUESTED_FEATURES] = {
WGPUFeatureName_Depth32FloatStencil8,
};
// check for optional features we're interested in
if (wgpuAdapterHasFeature(adapter, WGPUFeatureName_TextureCompressionBC)) {
SOKOL_ASSERT(cur_feature_index < _SAPP_WGPU_MAX_REQUESTED_FEATURES);
requiredFeatures[cur_feature_index++] = WGPUFeatureName_TextureCompressionBC;
}
if (wgpuAdapterHasFeature(adapter, WGPUFeatureName_TextureCompressionETC2)) {
SOKOL_ASSERT(cur_feature_index < _SAPP_WGPU_MAX_REQUESTED_FEATURES);
requiredFeatures[cur_feature_index++] = WGPUFeatureName_TextureCompressionETC2;
}
if (wgpuAdapterHasFeature(adapter, WGPUFeatureName_TextureCompressionASTC)) {
SOKOL_ASSERT(cur_feature_index < _SAPP_WGPU_MAX_REQUESTED_FEATURES);
requiredFeatures[cur_feature_index++] = WGPUFeatureName_TextureCompressionASTC;
}
if (wgpuAdapterHasFeature(adapter, WGPUFeatureName_Float32Filterable)) {
SOKOL_ASSERT(cur_feature_index < _SAPP_WGPU_MAX_REQUESTED_FEATURES);
requiredFeatures[cur_feature_index++] = WGPUFeatureName_Float32Filterable;
}
#undef _SAPP_WGPU_MAX_REQUESTED_FEATURES
WGPUDeviceDescriptor dev_desc;
_sapp_clear(&dev_desc, sizeof(dev_desc));
dev_desc.requiredFeatureCount = cur_feature_index;
dev_desc.requiredFeatures = requiredFeatures,
wgpuAdapterRequestDevice(adapter, &dev_desc, _sapp_emsc_wgpu_request_device_cb, 0);
}
_SOKOL_PRIVATE void _sapp_emsc_wgpu_init(void) {
SOKOL_ASSERT(0 == _sapp.wgpu.instance);
SOKOL_ASSERT(!_sapp.wgpu.async_init_done);
_sapp.wgpu.instance = wgpuCreateInstance(0);
if (0 == _sapp.wgpu.instance) {
_SAPP_PANIC(WGPU_CREATE_INSTANCE_FAILED);
}
// FIXME: power preference?
wgpuInstanceRequestAdapter(_sapp.wgpu.instance, 0, _sapp_emsc_wgpu_request_adapter_cb, 0);
}
_SOKOL_PRIVATE void _sapp_emsc_wgpu_frame(void) {
if (_sapp.wgpu.async_init_done) {
_sapp.wgpu.swapchain_view = wgpuSwapChainGetCurrentTextureView(_sapp.wgpu.swapchain);
_sapp_frame();
wgpuTextureViewRelease(_sapp.wgpu.swapchain_view);
_sapp.wgpu.swapchain_view = 0;
}
}
#endif // SOKOL_WGPU
_SOKOL_PRIVATE void _sapp_emsc_register_eventhandlers(void) {
// NOTE: HTML canvas doesn't receive input focus, this is why key event handlers are added
// to the window object (this could be worked around by adding a "tab index" to the
// canvas)
emscripten_set_mousedown_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_mouse_cb);
emscripten_set_mouseup_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_mouse_cb);
emscripten_set_mousemove_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_mouse_cb);
emscripten_set_mouseenter_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_mouse_cb);
emscripten_set_mouseleave_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_mouse_cb);
emscripten_set_wheel_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_wheel_cb);
emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, _sapp_emsc_key_cb);
emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, _sapp_emsc_key_cb);
emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, _sapp_emsc_key_cb);
emscripten_set_touchstart_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_touch_cb);
emscripten_set_touchmove_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_touch_cb);
emscripten_set_touchend_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_touch_cb);
emscripten_set_touchcancel_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_touch_cb);
emscripten_set_pointerlockchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, true, _sapp_emsc_pointerlockchange_cb);
emscripten_set_pointerlockerror_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, true, _sapp_emsc_pointerlockerror_cb);
emscripten_set_focus_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, _sapp_emsc_focus_cb);
emscripten_set_blur_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, _sapp_emsc_blur_cb);
sapp_js_add_beforeunload_listener();
if (_sapp.clipboard.enabled) {
sapp_js_add_clipboard_listener();
}
if (_sapp.drop.enabled) {
sapp_js_add_dragndrop_listeners(&_sapp.html5_canvas_selector[1]);
}
#if defined(SOKOL_GLES3)
emscripten_set_webglcontextlost_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_webgl_context_cb);
emscripten_set_webglcontextrestored_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_webgl_context_cb);
#endif
}
_SOKOL_PRIVATE void _sapp_emsc_unregister_eventhandlers(void) {
emscripten_set_mousedown_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_mouseup_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_mousemove_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_mouseenter_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_mouseleave_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_wheel_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0);
emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0);
emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0);
emscripten_set_touchstart_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_touchmove_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_touchend_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_touchcancel_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_pointerlockchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, true, 0);
emscripten_set_pointerlockerror_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, true, 0);
emscripten_set_focus_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0);
emscripten_set_blur_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0);
if (!_sapp.desc.html5_canvas_resize) {
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0);
}
sapp_js_remove_beforeunload_listener();
if (_sapp.clipboard.enabled) {
sapp_js_remove_clipboard_listener();
}
if (_sapp.drop.enabled) {
sapp_js_remove_dragndrop_listeners(&_sapp.html5_canvas_selector[1]);
}
#if defined(SOKOL_GLES3)
emscripten_set_webglcontextlost_callback(_sapp.html5_canvas_selector, 0, true, 0);
emscripten_set_webglcontextrestored_callback(_sapp.html5_canvas_selector, 0, true, 0);
#endif
}
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_frame_animation_loop(double time, void* userData) {
_SOKOL_UNUSED(userData);
_sapp_timing_external(&_sapp.timing, time / 1000.0);
#if defined(SOKOL_WGPU)
_sapp_emsc_wgpu_frame();
#else
_sapp_frame();
#endif
// quit-handling
if (_sapp.quit_requested) {
_sapp_init_event(SAPP_EVENTTYPE_QUIT_REQUESTED);
_sapp_call_event(&_sapp.event);
if (_sapp.quit_requested) {
_sapp.quit_ordered = true;
}
}
if (_sapp.quit_ordered) {
_sapp_emsc_unregister_eventhandlers();
_sapp_call_cleanup();
_sapp_discard_state();
return EM_FALSE;
}
return EM_TRUE;
}
_SOKOL_PRIVATE void _sapp_emsc_frame_main_loop(void) {
const double time = emscripten_performance_now();
if (!_sapp_emsc_frame_animation_loop(time, 0)) {
emscripten_cancel_main_loop();
}
}
_SOKOL_PRIVATE void _sapp_emsc_run(const sapp_desc* desc) {
_sapp_init_state(desc);
sapp_js_init(&_sapp.html5_canvas_selector[1]);
double w, h;
if (_sapp.desc.html5_canvas_resize) {
w = (double) _sapp_def(_sapp.desc.width, _SAPP_FALLBACK_DEFAULT_WINDOW_WIDTH);
h = (double) _sapp_def(_sapp.desc.height, _SAPP_FALLBACK_DEFAULT_WINDOW_HEIGHT);
}
else {
emscripten_get_element_css_size(_sapp.html5_canvas_selector, &w, &h);
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, false, _sapp_emsc_size_changed);
}
if (_sapp.desc.high_dpi) {
_sapp.dpi_scale = emscripten_get_device_pixel_ratio();
}
_sapp.window_width = (int)roundf(w);
_sapp.window_height = (int)roundf(h);
_sapp.framebuffer_width = (int)roundf(w * _sapp.dpi_scale);
_sapp.framebuffer_height = (int)roundf(h * _sapp.dpi_scale);
emscripten_set_canvas_element_size(_sapp.html5_canvas_selector, _sapp.framebuffer_width, _sapp.framebuffer_height);
#if defined(SOKOL_GLES3)
_sapp_emsc_webgl_init();
#elif defined(SOKOL_WGPU)
_sapp_emsc_wgpu_init();
#endif
_sapp.valid = true;
_sapp_emsc_register_eventhandlers();
sapp_set_icon(&desc->icon);
// start the frame loop
if (_sapp.desc.html5_use_emsc_set_main_loop) {
emscripten_set_main_loop(_sapp_emsc_frame_main_loop, 0, _sapp.desc.html5_emsc_set_main_loop_simulate_infinite_loop);
} else {
emscripten_request_animation_frame_loop(_sapp_emsc_frame_animation_loop, 0);
}
// NOT A BUG: do not call _sapp_discard_state() here, instead this is
// called in _sapp_emsc_frame() when the application is ordered to quit
}
#if !defined(SOKOL_NO_ENTRY)
int main(int argc, char* argv[]) {
sapp_desc desc = sokol_main(argc, argv);
_sapp_emsc_run(&desc);
return 0;
}
#endif /* SOKOL_NO_ENTRY */
#endif /* _SAPP_EMSCRIPTEN */
// ██████ ██ ██ ██ ███████ ██ ██████ ███████ ██████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ███ ██ ███████ █████ ██ ██████ █████ ██████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ███████ ██ ██ ███████ ███████ ██ ███████ ██ ██ ███████
//
// >>gl helpers
#if defined(SOKOL_GLCORE)
typedef struct {
int red_bits;
int green_bits;
int blue_bits;
int alpha_bits;
int depth_bits;
int stencil_bits;
int samples;
bool doublebuffer;
uintptr_t handle;
} _sapp_gl_fbconfig;
_SOKOL_PRIVATE void _sapp_gl_init_fbconfig(_sapp_gl_fbconfig* fbconfig) {
_sapp_clear(fbconfig, sizeof(_sapp_gl_fbconfig));
/* -1 means "don't care" */
fbconfig->red_bits = -1;
fbconfig->green_bits = -1;
fbconfig->blue_bits = -1;
fbconfig->alpha_bits = -1;
fbconfig->depth_bits = -1;
fbconfig->stencil_bits = -1;
fbconfig->samples = -1;
}
typedef struct {
int least_missing;
int least_color_diff;
int least_extra_diff;
bool best_match;
} _sapp_gl_fbselect;
_SOKOL_PRIVATE void _sapp_gl_init_fbselect(_sapp_gl_fbselect* fbselect) {
_sapp_clear(fbselect, sizeof(_sapp_gl_fbselect));
fbselect->least_missing = 1000000;
fbselect->least_color_diff = 10000000;
fbselect->least_extra_diff = 10000000;
fbselect->best_match = false;
}
// NOTE: this is used only in the WGL code path
_SOKOL_PRIVATE bool _sapp_gl_select_fbconfig(_sapp_gl_fbselect* fbselect, const _sapp_gl_fbconfig* desired, const _sapp_gl_fbconfig* current) {
int missing = 0;
if (desired->doublebuffer != current->doublebuffer) {
return false;
}
if ((desired->alpha_bits > 0) && (current->alpha_bits == 0)) {
missing++;
}
if ((desired->depth_bits > 0) && (current->depth_bits == 0)) {
missing++;
}
if ((desired->stencil_bits > 0) && (current->stencil_bits == 0)) {
missing++;
}
if ((desired->samples > 0) && (current->samples == 0)) {
/* Technically, several multisampling buffers could be
involved, but that's a lower level implementation detail and
not important to us here, so we count them as one
*/
missing++;
}
/* These polynomials make many small channel size differences matter
less than one large channel size difference
Calculate color channel size difference value
*/
int color_diff = 0;
if (desired->red_bits != -1) {
color_diff += (desired->red_bits - current->red_bits) * (desired->red_bits - current->red_bits);
}
if (desired->green_bits != -1) {
color_diff += (desired->green_bits - current->green_bits) * (desired->green_bits - current->green_bits);
}
if (desired->blue_bits != -1) {
color_diff += (desired->blue_bits - current->blue_bits) * (desired->blue_bits - current->blue_bits);
}
/* Calculate non-color channel size difference value */
int extra_diff = 0;
if (desired->alpha_bits != -1) {
extra_diff += (desired->alpha_bits - current->alpha_bits) * (desired->alpha_bits - current->alpha_bits);
}
if (desired->depth_bits != -1) {
extra_diff += (desired->depth_bits - current->depth_bits) * (desired->depth_bits - current->depth_bits);
}
if (desired->stencil_bits != -1) {
extra_diff += (desired->stencil_bits - current->stencil_bits) * (desired->stencil_bits - current->stencil_bits);
}
if (desired->samples != -1) {
extra_diff += (desired->samples - current->samples) * (desired->samples - current->samples);
}
/* Figure out if the current one is better than the best one found so far
Least number of missing buffers is the most important heuristic,
then color buffer size match and lastly size match for other buffers
*/
bool new_closest = false;
if (missing < fbselect->least_missing) {
new_closest = true;
} else if (missing == fbselect->least_missing) {
if ((color_diff < fbselect->least_color_diff) ||
((color_diff == fbselect->least_color_diff) && (extra_diff < fbselect->least_extra_diff)))
{
new_closest = true;
}
}
if (new_closest) {
fbselect->least_missing = missing;
fbselect->least_color_diff = color_diff;
fbselect->least_extra_diff = extra_diff;
fbselect->best_match = (missing | color_diff | extra_diff) == 0;
}
return new_closest;
}
// NOTE: this is used only in the GLX code path
_SOKOL_PRIVATE const _sapp_gl_fbconfig* _sapp_gl_choose_fbconfig(const _sapp_gl_fbconfig* desired, const _sapp_gl_fbconfig* alternatives, int count) {
int missing, least_missing = 1000000;
int color_diff, least_color_diff = 10000000;
int extra_diff, least_extra_diff = 10000000;
const _sapp_gl_fbconfig* current;
const _sapp_gl_fbconfig* closest = 0;
for (int i = 0; i < count; i++) {
current = alternatives + i;
if (desired->doublebuffer != current->doublebuffer) {
continue;
}
missing = 0;
if (desired->alpha_bits > 0 && current->alpha_bits == 0) {
missing++;
}
if (desired->depth_bits > 0 && current->depth_bits == 0) {
missing++;
}
if (desired->stencil_bits > 0 && current->stencil_bits == 0) {
missing++;
}
if (desired->samples > 0 && current->samples == 0) {
/* Technically, several multisampling buffers could be
involved, but that's a lower level implementation detail and
not important to us here, so we count them as one
*/
missing++;
}
/* These polynomials make many small channel size differences matter
less than one large channel size difference
Calculate color channel size difference value
*/
color_diff = 0;
if (desired->red_bits != -1) {
color_diff += (desired->red_bits - current->red_bits) * (desired->red_bits - current->red_bits);
}
if (desired->green_bits != -1) {
color_diff += (desired->green_bits - current->green_bits) * (desired->green_bits - current->green_bits);
}
if (desired->blue_bits != -1) {
color_diff += (desired->blue_bits - current->blue_bits) * (desired->blue_bits - current->blue_bits);
}
/* Calculate non-color channel size difference value */
extra_diff = 0;
if (desired->alpha_bits != -1) {
extra_diff += (desired->alpha_bits - current->alpha_bits) * (desired->alpha_bits - current->alpha_bits);
}
if (desired->depth_bits != -1) {
extra_diff += (desired->depth_bits - current->depth_bits) * (desired->depth_bits - current->depth_bits);
}
if (desired->stencil_bits != -1) {
extra_diff += (desired->stencil_bits - current->stencil_bits) * (desired->stencil_bits - current->stencil_bits);
}
if (desired->samples != -1) {
extra_diff += (desired->samples - current->samples) * (desired->samples - current->samples);
}
/* Figure out if the current one is better than the best one found so far
Least number of missing buffers is the most important heuristic,
then color buffer size match and lastly size match for other buffers
*/
if (missing < least_missing) {
closest = current;
}
else if (missing == least_missing) {
if ((color_diff < least_color_diff) ||
(color_diff == least_color_diff && extra_diff < least_extra_diff))
{
closest = current;
}
}
if (current == closest) {
least_missing = missing;
least_color_diff = color_diff;
least_extra_diff = extra_diff;
}
}
return closest;
}
#endif
// ██ ██ ██ ███ ██ ██████ ██████ ██ ██ ███████
// ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██
// ██ █ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █ ██ ███████
// ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██
// ███ ███ ██ ██ ████ ██████ ██████ ███ ███ ███████
//
// >>windows
#if defined(_SAPP_WIN32)
_SOKOL_PRIVATE bool _sapp_win32_utf8_to_wide(const char* src, wchar_t* dst, int dst_num_bytes) {
SOKOL_ASSERT(src && dst && (dst_num_bytes > 1));
_sapp_clear(dst, (size_t)dst_num_bytes);
const int dst_chars = dst_num_bytes / (int)sizeof(wchar_t);
const int dst_needed = MultiByteToWideChar(CP_UTF8, 0, src, -1, 0, 0);
if ((dst_needed > 0) && (dst_needed < dst_chars)) {
MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, dst_chars);
return true;
}
else {
/* input string doesn't fit into destination buffer */
return false;
}
}
_SOKOL_PRIVATE void _sapp_win32_app_event(sapp_event_type type) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE void _sapp_win32_init_keytable(void) {
/* same as GLFW */
_sapp.keycodes[0x00B] = SAPP_KEYCODE_0;
_sapp.keycodes[0x002] = SAPP_KEYCODE_1;
_sapp.keycodes[0x003] = SAPP_KEYCODE_2;
_sapp.keycodes[0x004] = SAPP_KEYCODE_3;
_sapp.keycodes[0x005] = SAPP_KEYCODE_4;
_sapp.keycodes[0x006] = SAPP_KEYCODE_5;
_sapp.keycodes[0x007] = SAPP_KEYCODE_6;
_sapp.keycodes[0x008] = SAPP_KEYCODE_7;
_sapp.keycodes[0x009] = SAPP_KEYCODE_8;
_sapp.keycodes[0x00A] = SAPP_KEYCODE_9;
_sapp.keycodes[0x01E] = SAPP_KEYCODE_A;
_sapp.keycodes[0x030] = SAPP_KEYCODE_B;
_sapp.keycodes[0x02E] = SAPP_KEYCODE_C;
_sapp.keycodes[0x020] = SAPP_KEYCODE_D;
_sapp.keycodes[0x012] = SAPP_KEYCODE_E;
_sapp.keycodes[0x021] = SAPP_KEYCODE_F;
_sapp.keycodes[0x022] = SAPP_KEYCODE_G;
_sapp.keycodes[0x023] = SAPP_KEYCODE_H;
_sapp.keycodes[0x017] = SAPP_KEYCODE_I;
_sapp.keycodes[0x024] = SAPP_KEYCODE_J;
_sapp.keycodes[0x025] = SAPP_KEYCODE_K;
_sapp.keycodes[0x026] = SAPP_KEYCODE_L;
_sapp.keycodes[0x032] = SAPP_KEYCODE_M;
_sapp.keycodes[0x031] = SAPP_KEYCODE_N;
_sapp.keycodes[0x018] = SAPP_KEYCODE_O;
_sapp.keycodes[0x019] = SAPP_KEYCODE_P;
_sapp.keycodes[0x010] = SAPP_KEYCODE_Q;
_sapp.keycodes[0x013] = SAPP_KEYCODE_R;
_sapp.keycodes[0x01F] = SAPP_KEYCODE_S;
_sapp.keycodes[0x014] = SAPP_KEYCODE_T;
_sapp.keycodes[0x016] = SAPP_KEYCODE_U;
_sapp.keycodes[0x02F] = SAPP_KEYCODE_V;
_sapp.keycodes[0x011] = SAPP_KEYCODE_W;
_sapp.keycodes[0x02D] = SAPP_KEYCODE_X;
_sapp.keycodes[0x015] = SAPP_KEYCODE_Y;
_sapp.keycodes[0x02C] = SAPP_KEYCODE_Z;
_sapp.keycodes[0x028] = SAPP_KEYCODE_APOSTROPHE;
_sapp.keycodes[0x02B] = SAPP_KEYCODE_BACKSLASH;
_sapp.keycodes[0x033] = SAPP_KEYCODE_COMMA;
_sapp.keycodes[0x00D] = SAPP_KEYCODE_EQUAL;
_sapp.keycodes[0x029] = SAPP_KEYCODE_GRAVE_ACCENT;
_sapp.keycodes[0x01A] = SAPP_KEYCODE_LEFT_BRACKET;
_sapp.keycodes[0x00C] = SAPP_KEYCODE_MINUS;
_sapp.keycodes[0x034] = SAPP_KEYCODE_PERIOD;
_sapp.keycodes[0x01B] = SAPP_KEYCODE_RIGHT_BRACKET;
_sapp.keycodes[0x027] = SAPP_KEYCODE_SEMICOLON;
_sapp.keycodes[0x035] = SAPP_KEYCODE_SLASH;
_sapp.keycodes[0x056] = SAPP_KEYCODE_WORLD_2;
_sapp.keycodes[0x00E] = SAPP_KEYCODE_BACKSPACE;
_sapp.keycodes[0x153] = SAPP_KEYCODE_DELETE;
_sapp.keycodes[0x14F] = SAPP_KEYCODE_END;
_sapp.keycodes[0x01C] = SAPP_KEYCODE_ENTER;
_sapp.keycodes[0x001] = SAPP_KEYCODE_ESCAPE;
_sapp.keycodes[0x147] = SAPP_KEYCODE_HOME;
_sapp.keycodes[0x152] = SAPP_KEYCODE_INSERT;
_sapp.keycodes[0x15D] = SAPP_KEYCODE_MENU;
_sapp.keycodes[0x151] = SAPP_KEYCODE_PAGE_DOWN;
_sapp.keycodes[0x149] = SAPP_KEYCODE_PAGE_UP;
_sapp.keycodes[0x045] = SAPP_KEYCODE_PAUSE;
_sapp.keycodes[0x146] = SAPP_KEYCODE_PAUSE;
_sapp.keycodes[0x039] = SAPP_KEYCODE_SPACE;
_sapp.keycodes[0x00F] = SAPP_KEYCODE_TAB;
_sapp.keycodes[0x03A] = SAPP_KEYCODE_CAPS_LOCK;
_sapp.keycodes[0x145] = SAPP_KEYCODE_NUM_LOCK;
_sapp.keycodes[0x046] = SAPP_KEYCODE_SCROLL_LOCK;
_sapp.keycodes[0x03B] = SAPP_KEYCODE_F1;
_sapp.keycodes[0x03C] = SAPP_KEYCODE_F2;
_sapp.keycodes[0x03D] = SAPP_KEYCODE_F3;
_sapp.keycodes[0x03E] = SAPP_KEYCODE_F4;
_sapp.keycodes[0x03F] = SAPP_KEYCODE_F5;
_sapp.keycodes[0x040] = SAPP_KEYCODE_F6;
_sapp.keycodes[0x041] = SAPP_KEYCODE_F7;
_sapp.keycodes[0x042] = SAPP_KEYCODE_F8;
_sapp.keycodes[0x043] = SAPP_KEYCODE_F9;
_sapp.keycodes[0x044] = SAPP_KEYCODE_F10;
_sapp.keycodes[0x057] = SAPP_KEYCODE_F11;
_sapp.keycodes[0x058] = SAPP_KEYCODE_F12;
_sapp.keycodes[0x064] = SAPP_KEYCODE_F13;
_sapp.keycodes[0x065] = SAPP_KEYCODE_F14;
_sapp.keycodes[0x066] = SAPP_KEYCODE_F15;
_sapp.keycodes[0x067] = SAPP_KEYCODE_F16;
_sapp.keycodes[0x068] = SAPP_KEYCODE_F17;
_sapp.keycodes[0x069] = SAPP_KEYCODE_F18;
_sapp.keycodes[0x06A] = SAPP_KEYCODE_F19;
_sapp.keycodes[0x06B] = SAPP_KEYCODE_F20;
_sapp.keycodes[0x06C] = SAPP_KEYCODE_F21;
_sapp.keycodes[0x06D] = SAPP_KEYCODE_F22;
_sapp.keycodes[0x06E] = SAPP_KEYCODE_F23;
_sapp.keycodes[0x076] = SAPP_KEYCODE_F24;
_sapp.keycodes[0x038] = SAPP_KEYCODE_LEFT_ALT;
_sapp.keycodes[0x01D] = SAPP_KEYCODE_LEFT_CONTROL;
_sapp.keycodes[0x02A] = SAPP_KEYCODE_LEFT_SHIFT;
_sapp.keycodes[0x15B] = SAPP_KEYCODE_LEFT_SUPER;
_sapp.keycodes[0x137] = SAPP_KEYCODE_PRINT_SCREEN;
_sapp.keycodes[0x138] = SAPP_KEYCODE_RIGHT_ALT;
_sapp.keycodes[0x11D] = SAPP_KEYCODE_RIGHT_CONTROL;
_sapp.keycodes[0x036] = SAPP_KEYCODE_RIGHT_SHIFT;
_sapp.keycodes[0x136] = SAPP_KEYCODE_RIGHT_SHIFT;
_sapp.keycodes[0x15C] = SAPP_KEYCODE_RIGHT_SUPER;
_sapp.keycodes[0x150] = SAPP_KEYCODE_DOWN;
_sapp.keycodes[0x14B] = SAPP_KEYCODE_LEFT;
_sapp.keycodes[0x14D] = SAPP_KEYCODE_RIGHT;
_sapp.keycodes[0x148] = SAPP_KEYCODE_UP;
_sapp.keycodes[0x052] = SAPP_KEYCODE_KP_0;
_sapp.keycodes[0x04F] = SAPP_KEYCODE_KP_1;
_sapp.keycodes[0x050] = SAPP_KEYCODE_KP_2;
_sapp.keycodes[0x051] = SAPP_KEYCODE_KP_3;
_sapp.keycodes[0x04B] = SAPP_KEYCODE_KP_4;
_sapp.keycodes[0x04C] = SAPP_KEYCODE_KP_5;
_sapp.keycodes[0x04D] = SAPP_KEYCODE_KP_6;
_sapp.keycodes[0x047] = SAPP_KEYCODE_KP_7;
_sapp.keycodes[0x048] = SAPP_KEYCODE_KP_8;
_sapp.keycodes[0x049] = SAPP_KEYCODE_KP_9;
_sapp.keycodes[0x04E] = SAPP_KEYCODE_KP_ADD;
_sapp.keycodes[0x053] = SAPP_KEYCODE_KP_DECIMAL;
_sapp.keycodes[0x135] = SAPP_KEYCODE_KP_DIVIDE;
_sapp.keycodes[0x11C] = SAPP_KEYCODE_KP_ENTER;
_sapp.keycodes[0x037] = SAPP_KEYCODE_KP_MULTIPLY;
_sapp.keycodes[0x04A] = SAPP_KEYCODE_KP_SUBTRACT;
}
#endif // _SAPP_WIN32
#if defined(_SAPP_WIN32)
#if defined(SOKOL_D3D11)
#if defined(__cplusplus)
#define _sapp_d3d11_Release(self) (self)->Release()
#define _sapp_win32_refiid(iid) iid
#else
#define _sapp_d3d11_Release(self) (self)->lpVtbl->Release(self)
#define _sapp_win32_refiid(iid) &iid
#endif
#define _SAPP_SAFE_RELEASE(obj) if (obj) { _sapp_d3d11_Release(obj); obj=0; }
static const IID _sapp_IID_ID3D11Texture2D = { 0x6f15aaf2,0xd208,0x4e89, {0x9a,0xb4,0x48,0x95,0x35,0xd3,0x4f,0x9c} };
static const IID _sapp_IID_IDXGIDevice1 = { 0x77db970f,0x6276,0x48ba, {0xba,0x28,0x07,0x01,0x43,0xb4,0x39,0x2c} };
static const IID _sapp_IID_IDXGIFactory = { 0x7b7166ec,0x21c7,0x44ae, {0xb2,0x1a,0xc9,0xae,0x32,0x1a,0xe3,0x69} };
static inline HRESULT _sapp_dxgi_GetBuffer(IDXGISwapChain* self, UINT Buffer, REFIID riid, void** ppSurface) {
#if defined(__cplusplus)
return self->GetBuffer(Buffer, riid, ppSurface);
#else
return self->lpVtbl->GetBuffer(self, Buffer, riid, ppSurface);
#endif
}
static inline HRESULT _sapp_d3d11_QueryInterface(ID3D11Device* self, REFIID riid, void** ppvObject) {
#if defined(__cplusplus)
return self->QueryInterface(riid, ppvObject);
#else
return self->lpVtbl->QueryInterface(self, riid, ppvObject);
#endif
}
static inline HRESULT _sapp_d3d11_CreateRenderTargetView(ID3D11Device* self, ID3D11Resource *pResource, const D3D11_RENDER_TARGET_VIEW_DESC* pDesc, ID3D11RenderTargetView** ppRTView) {
#if defined(__cplusplus)
return self->CreateRenderTargetView(pResource, pDesc, ppRTView);
#else
return self->lpVtbl->CreateRenderTargetView(self, pResource, pDesc, ppRTView);
#endif
}
static inline HRESULT _sapp_d3d11_CreateTexture2D(ID3D11Device* self, const D3D11_TEXTURE2D_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Texture2D** ppTexture2D) {
#if defined(__cplusplus)
return self->CreateTexture2D(pDesc, pInitialData, ppTexture2D);
#else
return self->lpVtbl->CreateTexture2D(self, pDesc, pInitialData, ppTexture2D);
#endif
}
static inline HRESULT _sapp_d3d11_CreateDepthStencilView(ID3D11Device* self, ID3D11Resource* pResource, const D3D11_DEPTH_STENCIL_VIEW_DESC* pDesc, ID3D11DepthStencilView** ppDepthStencilView) {
#if defined(__cplusplus)
return self->CreateDepthStencilView(pResource, pDesc, ppDepthStencilView);
#else
return self->lpVtbl->CreateDepthStencilView(self, pResource, pDesc, ppDepthStencilView);
#endif
}
static inline HRESULT _sapp_dxgi_ResizeBuffers(IDXGISwapChain* self, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags) {
#if defined(__cplusplus)
return self->ResizeBuffers(BufferCount, Width, Height, NewFormat, SwapChainFlags);
#else
return self->lpVtbl->ResizeBuffers(self, BufferCount, Width, Height, NewFormat, SwapChainFlags);
#endif
}
static inline HRESULT _sapp_dxgi_Present(IDXGISwapChain* self, UINT SyncInterval, UINT Flags) {
#if defined(__cplusplus)
return self->Present(SyncInterval, Flags);
#else
return self->lpVtbl->Present(self, SyncInterval, Flags);
#endif
}
static inline HRESULT _sapp_dxgi_GetFrameStatistics(IDXGISwapChain* self, DXGI_FRAME_STATISTICS* pStats) {
#if defined(__cplusplus)
return self->GetFrameStatistics(pStats);
#else
return self->lpVtbl->GetFrameStatistics(self, pStats);
#endif
}
static inline HRESULT _sapp_dxgi_SetMaximumFrameLatency(IDXGIDevice1* self, UINT MaxLatency) {
#if defined(__cplusplus)
return self->SetMaximumFrameLatency(MaxLatency);
#else
return self->lpVtbl->SetMaximumFrameLatency(self, MaxLatency);
#endif
}
static inline HRESULT _sapp_dxgi_GetAdapter(IDXGIDevice1* self, IDXGIAdapter** pAdapter) {
#if defined(__cplusplus)
return self->GetAdapter(pAdapter);
#else
return self->lpVtbl->GetAdapter(self, pAdapter);
#endif
}
static inline HRESULT _sapp_dxgi_GetParent(IDXGIObject* self, REFIID riid, void** ppParent) {
#if defined(__cplusplus)
return self->GetParent(riid, ppParent);
#else
return self->lpVtbl->GetParent(self, riid, ppParent);
#endif
}
static inline HRESULT _sapp_dxgi_MakeWindowAssociation(IDXGIFactory* self, HWND WindowHandle, UINT Flags) {
#if defined(__cplusplus)
return self->MakeWindowAssociation(WindowHandle, Flags);
#else
return self->lpVtbl->MakeWindowAssociation(self, WindowHandle, Flags);
#endif
}
_SOKOL_PRIVATE void _sapp_d3d11_create_device_and_swapchain(void) {
DXGI_SWAP_CHAIN_DESC* sc_desc = &_sapp.d3d11.swap_chain_desc;
sc_desc->BufferDesc.Width = (UINT)_sapp.framebuffer_width;
sc_desc->BufferDesc.Height = (UINT)_sapp.framebuffer_height;
sc_desc->BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
sc_desc->BufferDesc.RefreshRate.Numerator = 60;
sc_desc->BufferDesc.RefreshRate.Denominator = 1;
sc_desc->OutputWindow = _sapp.win32.hwnd;
sc_desc->Windowed = true;
if (_sapp.win32.is_win10_or_greater) {
sc_desc->BufferCount = 2;
sc_desc->SwapEffect = (DXGI_SWAP_EFFECT) _SAPP_DXGI_SWAP_EFFECT_FLIP_DISCARD;
_sapp.d3d11.use_dxgi_frame_stats = true;
}
else {
sc_desc->BufferCount = 1;
sc_desc->SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
_sapp.d3d11.use_dxgi_frame_stats = false;
}
sc_desc->SampleDesc.Count = 1;
sc_desc->SampleDesc.Quality = 0;
sc_desc->BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
UINT create_flags = D3D11_CREATE_DEVICE_SINGLETHREADED | D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if defined(SOKOL_DEBUG)
create_flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_FEATURE_LEVEL feature_level;
HRESULT hr = D3D11CreateDeviceAndSwapChain(
NULL, /* pAdapter (use default) */
D3D_DRIVER_TYPE_HARDWARE, /* DriverType */
NULL, /* Software */
create_flags, /* Flags */
NULL, /* pFeatureLevels */
0, /* FeatureLevels */
D3D11_SDK_VERSION, /* SDKVersion */
sc_desc, /* pSwapChainDesc */
&_sapp.d3d11.swap_chain, /* ppSwapChain */
&_sapp.d3d11.device, /* ppDevice */
&feature_level, /* pFeatureLevel */
&_sapp.d3d11.device_context); /* ppImmediateContext */
_SOKOL_UNUSED(hr);
#if defined(SOKOL_DEBUG)
if (!SUCCEEDED(hr)) {
// if initialization with D3D11_CREATE_DEVICE_DEBUG fails, this could be because the
// 'D3D11 debug layer' stopped working, indicated by the error message:
// ===
// D3D11CreateDevice: Flags (0x2) were specified which require the D3D11 SDK Layers for Windows 10, but they are not present on the system.
// These flags must be removed, or the Windows 10 SDK must be installed.
// Flags include: D3D11_CREATE_DEVICE_DEBUG
// ===
//
// ...just retry with the DEBUG flag switched off
_SAPP_ERROR(WIN32_D3D11_CREATE_DEVICE_AND_SWAPCHAIN_WITH_DEBUG_FAILED);
create_flags &= ~D3D11_CREATE_DEVICE_DEBUG;
hr = D3D11CreateDeviceAndSwapChain(
NULL, /* pAdapter (use default) */
D3D_DRIVER_TYPE_HARDWARE, /* DriverType */
NULL, /* Software */
create_flags, /* Flags */
NULL, /* pFeatureLevels */
0, /* FeatureLevels */
D3D11_SDK_VERSION, /* SDKVersion */
sc_desc, /* pSwapChainDesc */
&_sapp.d3d11.swap_chain, /* ppSwapChain */
&_sapp.d3d11.device, /* ppDevice */
&feature_level, /* pFeatureLevel */
&_sapp.d3d11.device_context); /* ppImmediateContext */
}
#endif
SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.swap_chain && _sapp.d3d11.device && _sapp.d3d11.device_context);
// minimize frame latency, disable Alt-Enter
hr = _sapp_d3d11_QueryInterface(_sapp.d3d11.device, _sapp_win32_refiid(_sapp_IID_IDXGIDevice1), (void**)&_sapp.d3d11.dxgi_device);
if (SUCCEEDED(hr) && _sapp.d3d11.dxgi_device) {
_sapp_dxgi_SetMaximumFrameLatency(_sapp.d3d11.dxgi_device, 1);
IDXGIAdapter* dxgi_adapter = 0;
hr = _sapp_dxgi_GetAdapter(_sapp.d3d11.dxgi_device, &dxgi_adapter);
if (SUCCEEDED(hr) && dxgi_adapter) {
IDXGIFactory* dxgi_factory = 0;
hr = _sapp_dxgi_GetParent((IDXGIObject*)dxgi_adapter, _sapp_win32_refiid(_sapp_IID_IDXGIFactory), (void**)&dxgi_factory);
if (SUCCEEDED(hr)) {
_sapp_dxgi_MakeWindowAssociation(dxgi_factory, _sapp.win32.hwnd, DXGI_MWA_NO_ALT_ENTER|DXGI_MWA_NO_PRINT_SCREEN);
_SAPP_SAFE_RELEASE(dxgi_factory);
}
else {
_SAPP_ERROR(WIN32_D3D11_GET_IDXGIFACTORY_FAILED);
}
_SAPP_SAFE_RELEASE(dxgi_adapter);
}
else {
_SAPP_ERROR(WIN32_D3D11_GET_IDXGIADAPTER_FAILED);
}
}
else {
_SAPP_PANIC(WIN32_D3D11_QUERY_INTERFACE_IDXGIDEVICE1_FAILED);
}
}
_SOKOL_PRIVATE void _sapp_d3d11_destroy_device_and_swapchain(void) {
_SAPP_SAFE_RELEASE(_sapp.d3d11.swap_chain);
_SAPP_SAFE_RELEASE(_sapp.d3d11.dxgi_device);
_SAPP_SAFE_RELEASE(_sapp.d3d11.device_context);
_SAPP_SAFE_RELEASE(_sapp.d3d11.device);
}
_SOKOL_PRIVATE void _sapp_d3d11_create_default_render_target(void) {
SOKOL_ASSERT(0 == _sapp.d3d11.rt);
SOKOL_ASSERT(0 == _sapp.d3d11.rtv);
SOKOL_ASSERT(0 == _sapp.d3d11.msaa_rt);
SOKOL_ASSERT(0 == _sapp.d3d11.msaa_rtv);
SOKOL_ASSERT(0 == _sapp.d3d11.ds);
SOKOL_ASSERT(0 == _sapp.d3d11.dsv);
HRESULT hr;
/* view for the swapchain-created framebuffer */
hr = _sapp_dxgi_GetBuffer(_sapp.d3d11.swap_chain, 0, _sapp_win32_refiid(_sapp_IID_ID3D11Texture2D), (void**)&_sapp.d3d11.rt);
SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.rt);
hr = _sapp_d3d11_CreateRenderTargetView(_sapp.d3d11.device, (ID3D11Resource*)_sapp.d3d11.rt, NULL, &_sapp.d3d11.rtv);
SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.rtv);
/* common desc for MSAA and depth-stencil texture */
D3D11_TEXTURE2D_DESC tex_desc;
_sapp_clear(&tex_desc, sizeof(tex_desc));
tex_desc.Width = (UINT)_sapp.framebuffer_width;
tex_desc.Height = (UINT)_sapp.framebuffer_height;
tex_desc.MipLevels = 1;
tex_desc.ArraySize = 1;
tex_desc.Usage = D3D11_USAGE_DEFAULT;
tex_desc.BindFlags = D3D11_BIND_RENDER_TARGET;
tex_desc.SampleDesc.Count = (UINT) _sapp.sample_count;
tex_desc.SampleDesc.Quality = (UINT) (_sapp.sample_count > 1 ? D3D11_STANDARD_MULTISAMPLE_PATTERN : 0);
/* create MSAA texture and view if antialiasing requested */
if (_sapp.sample_count > 1) {
tex_desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
hr = _sapp_d3d11_CreateTexture2D(_sapp.d3d11.device, &tex_desc, NULL, &_sapp.d3d11.msaa_rt);
SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.msaa_rt);
hr = _sapp_d3d11_CreateRenderTargetView(_sapp.d3d11.device, (ID3D11Resource*)_sapp.d3d11.msaa_rt, NULL, &_sapp.d3d11.msaa_rtv);
SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.msaa_rtv);
}
/* texture and view for the depth-stencil-surface */
tex_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
tex_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
hr = _sapp_d3d11_CreateTexture2D(_sapp.d3d11.device, &tex_desc, NULL, &_sapp.d3d11.ds);
SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.ds);
hr = _sapp_d3d11_CreateDepthStencilView(_sapp.d3d11.device, (ID3D11Resource*)_sapp.d3d11.ds, NULL, &_sapp.d3d11.dsv);
SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.dsv);
}
_SOKOL_PRIVATE void _sapp_d3d11_destroy_default_render_target(void) {
_SAPP_SAFE_RELEASE(_sapp.d3d11.rt);
_SAPP_SAFE_RELEASE(_sapp.d3d11.rtv);
_SAPP_SAFE_RELEASE(_sapp.d3d11.msaa_rt);
_SAPP_SAFE_RELEASE(_sapp.d3d11.msaa_rtv);
_SAPP_SAFE_RELEASE(_sapp.d3d11.ds);
_SAPP_SAFE_RELEASE(_sapp.d3d11.dsv);
}
_SOKOL_PRIVATE void _sapp_d3d11_resize_default_render_target(void) {
if (_sapp.d3d11.swap_chain) {
_sapp_d3d11_destroy_default_render_target();
_sapp_dxgi_ResizeBuffers(_sapp.d3d11.swap_chain, _sapp.d3d11.swap_chain_desc.BufferCount, (UINT)_sapp.framebuffer_width, (UINT)_sapp.framebuffer_height, DXGI_FORMAT_B8G8R8A8_UNORM, 0);
_sapp_d3d11_create_default_render_target();
}
}
_SOKOL_PRIVATE void _sapp_d3d11_present(bool do_not_wait) {
UINT flags = 0;
if (_sapp.win32.is_win10_or_greater && do_not_wait) {
/* this hack/workaround somewhat improves window-movement and -sizing
responsiveness when rendering is controlled via WM_TIMER during window
move and resize on NVIDIA cards on Win10 with recent drivers.
*/
flags = DXGI_PRESENT_DO_NOT_WAIT;
}
_sapp_dxgi_Present(_sapp.d3d11.swap_chain, (UINT)_sapp.swap_interval, flags);
}
#endif /* SOKOL_D3D11 */
#if defined(SOKOL_GLCORE)
_SOKOL_PRIVATE void _sapp_wgl_init(void) {
_sapp.wgl.opengl32 = LoadLibraryA("opengl32.dll");
if (!_sapp.wgl.opengl32) {
_SAPP_PANIC(WIN32_LOAD_OPENGL32_DLL_FAILED);
}
SOKOL_ASSERT(_sapp.wgl.opengl32);
_sapp.wgl.CreateContext = (PFN_wglCreateContext)(void*) GetProcAddress(_sapp.wgl.opengl32, "wglCreateContext");
SOKOL_ASSERT(_sapp.wgl.CreateContext);
_sapp.wgl.DeleteContext = (PFN_wglDeleteContext)(void*) GetProcAddress(_sapp.wgl.opengl32, "wglDeleteContext");
SOKOL_ASSERT(_sapp.wgl.DeleteContext);
_sapp.wgl.GetProcAddress = (PFN_wglGetProcAddress)(void*) GetProcAddress(_sapp.wgl.opengl32, "wglGetProcAddress");
SOKOL_ASSERT(_sapp.wgl.GetProcAddress);
_sapp.wgl.GetCurrentDC = (PFN_wglGetCurrentDC)(void*) GetProcAddress(_sapp.wgl.opengl32, "wglGetCurrentDC");
SOKOL_ASSERT(_sapp.wgl.GetCurrentDC);
_sapp.wgl.MakeCurrent = (PFN_wglMakeCurrent)(void*) GetProcAddress(_sapp.wgl.opengl32, "wglMakeCurrent");
SOKOL_ASSERT(_sapp.wgl.MakeCurrent);
_sapp.wgl.GetIntegerv = (void(WINAPI*)(uint32_t, int32_t*)) GetProcAddress(_sapp.wgl.opengl32, "glGetIntegerv");
SOKOL_ASSERT(_sapp.wgl.GetIntegerv);
_sapp.wgl.msg_hwnd = CreateWindowExW(WS_EX_OVERLAPPEDWINDOW,
L"SOKOLAPP",
L"sokol-app message window",
WS_CLIPSIBLINGS|WS_CLIPCHILDREN,
0, 0, 1, 1,
NULL, NULL,
GetModuleHandleW(NULL),
NULL);
if (!_sapp.wgl.msg_hwnd) {
_SAPP_PANIC(WIN32_CREATE_HELPER_WINDOW_FAILED);
}
SOKOL_ASSERT(_sapp.wgl.msg_hwnd);
ShowWindow(_sapp.wgl.msg_hwnd, SW_HIDE);
MSG msg;
while (PeekMessageW(&msg, _sapp.wgl.msg_hwnd, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
_sapp.wgl.msg_dc = GetDC(_sapp.wgl.msg_hwnd);
if (!_sapp.wgl.msg_dc) {
_SAPP_PANIC(WIN32_HELPER_WINDOW_GETDC_FAILED);
}
}
_SOKOL_PRIVATE void _sapp_wgl_shutdown(void) {
SOKOL_ASSERT(_sapp.wgl.opengl32 && _sapp.wgl.msg_hwnd);
DestroyWindow(_sapp.wgl.msg_hwnd); _sapp.wgl.msg_hwnd = 0;
FreeLibrary(_sapp.wgl.opengl32); _sapp.wgl.opengl32 = 0;
}
_SOKOL_PRIVATE bool _sapp_wgl_has_ext(const char* ext, const char* extensions) {
SOKOL_ASSERT(ext && extensions);
const char* start = extensions;
while (true) {
const char* where = strstr(start, ext);
if (!where) {
return false;
}
const char* terminator = where + strlen(ext);
if ((where == start) || (*(where - 1) == ' ')) {
if (*terminator == ' ' || *terminator == '\0') {
break;
}
}
start = terminator;
}
return true;
}
_SOKOL_PRIVATE bool _sapp_wgl_ext_supported(const char* ext) {
SOKOL_ASSERT(ext);
if (_sapp.wgl.GetExtensionsStringEXT) {
const char* extensions = _sapp.wgl.GetExtensionsStringEXT();
if (extensions) {
if (_sapp_wgl_has_ext(ext, extensions)) {
return true;
}
}
}
if (_sapp.wgl.GetExtensionsStringARB) {
const char* extensions = _sapp.wgl.GetExtensionsStringARB(_sapp.wgl.GetCurrentDC());
if (extensions) {
if (_sapp_wgl_has_ext(ext, extensions)) {
return true;
}
}
}
return false;
}
_SOKOL_PRIVATE void _sapp_wgl_load_extensions(void) {
SOKOL_ASSERT(_sapp.wgl.msg_dc);
PIXELFORMATDESCRIPTOR pfd;
_sapp_clear(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
if (!SetPixelFormat(_sapp.wgl.msg_dc, ChoosePixelFormat(_sapp.wgl.msg_dc, &pfd), &pfd)) {
_SAPP_PANIC(WIN32_DUMMY_CONTEXT_SET_PIXELFORMAT_FAILED);
}
HGLRC rc = _sapp.wgl.CreateContext(_sapp.wgl.msg_dc);
if (!rc) {
_SAPP_PANIC(WIN32_CREATE_DUMMY_CONTEXT_FAILED);
}
if (!_sapp.wgl.MakeCurrent(_sapp.wgl.msg_dc, rc)) {
_SAPP_PANIC(WIN32_DUMMY_CONTEXT_MAKE_CURRENT_FAILED);
}
_sapp.wgl.GetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC)(void*) _sapp.wgl.GetProcAddress("wglGetExtensionsStringEXT");
_sapp.wgl.GetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)(void*) _sapp.wgl.GetProcAddress("wglGetExtensionsStringARB");
_sapp.wgl.CreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)(void*) _sapp.wgl.GetProcAddress("wglCreateContextAttribsARB");
_sapp.wgl.SwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)(void*) _sapp.wgl.GetProcAddress("wglSwapIntervalEXT");
_sapp.wgl.GetPixelFormatAttribivARB = (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)(void*) _sapp.wgl.GetProcAddress("wglGetPixelFormatAttribivARB");
_sapp.wgl.arb_multisample = _sapp_wgl_ext_supported("WGL_ARB_multisample");
_sapp.wgl.arb_create_context = _sapp_wgl_ext_supported("WGL_ARB_create_context");
_sapp.wgl.arb_create_context_profile = _sapp_wgl_ext_supported("WGL_ARB_create_context_profile");
_sapp.wgl.ext_swap_control = _sapp_wgl_ext_supported("WGL_EXT_swap_control");
_sapp.wgl.arb_pixel_format = _sapp_wgl_ext_supported("WGL_ARB_pixel_format");
_sapp.wgl.MakeCurrent(_sapp.wgl.msg_dc, 0);
_sapp.wgl.DeleteContext(rc);
}
_SOKOL_PRIVATE int _sapp_wgl_attrib(int pixel_format, int attrib) {
SOKOL_ASSERT(_sapp.wgl.arb_pixel_format);
int value = 0;
if (!_sapp.wgl.GetPixelFormatAttribivARB(_sapp.win32.dc, pixel_format, 0, 1, &attrib, &value)) {
_SAPP_PANIC(WIN32_GET_PIXELFORMAT_ATTRIB_FAILED);
}
return value;
}
_SOKOL_PRIVATE void _sapp_wgl_attribiv(int pixel_format, int num_attribs, const int* attribs, int* results) {
SOKOL_ASSERT(_sapp.wgl.arb_pixel_format);
if (!_sapp.wgl.GetPixelFormatAttribivARB(_sapp.win32.dc, pixel_format, 0, num_attribs, attribs, results)) {
_SAPP_PANIC(WIN32_GET_PIXELFORMAT_ATTRIB_FAILED);
}
}
_SOKOL_PRIVATE int _sapp_wgl_find_pixel_format(void) {
SOKOL_ASSERT(_sapp.win32.dc);
SOKOL_ASSERT(_sapp.wgl.arb_pixel_format);
#define _sapp_wgl_num_query_tags (12)
const int query_tags[_sapp_wgl_num_query_tags] = {
WGL_SUPPORT_OPENGL_ARB,
WGL_DRAW_TO_WINDOW_ARB,
WGL_PIXEL_TYPE_ARB,
WGL_ACCELERATION_ARB,
WGL_DOUBLE_BUFFER_ARB,
WGL_RED_BITS_ARB,
WGL_GREEN_BITS_ARB,
WGL_BLUE_BITS_ARB,
WGL_ALPHA_BITS_ARB,
WGL_DEPTH_BITS_ARB,
WGL_STENCIL_BITS_ARB,
WGL_SAMPLES_ARB,
};
const int result_support_opengl_index = 0;
const int result_draw_to_window_index = 1;
const int result_pixel_type_index = 2;
const int result_acceleration_index = 3;
const int result_double_buffer_index = 4;
const int result_red_bits_index = 5;
const int result_green_bits_index = 6;
const int result_blue_bits_index = 7;
const int result_alpha_bits_index = 8;
const int result_depth_bits_index = 9;
const int result_stencil_bits_index = 10;
const int result_samples_index = 11;
int query_results[_sapp_wgl_num_query_tags] = {0};
// Drop the last item if multisample extension is not supported.
// If in future querying with multiple extensions, will have to shuffle index values to have active extensions on the end.
int query_count = _sapp_wgl_num_query_tags;
if (!_sapp.wgl.arb_multisample) {
query_count = _sapp_wgl_num_query_tags - 1;
}
int native_count = _sapp_wgl_attrib(1, WGL_NUMBER_PIXEL_FORMATS_ARB);
_sapp_gl_fbconfig desired;
_sapp_gl_init_fbconfig(&desired);
desired.red_bits = 8;
desired.green_bits = 8;
desired.blue_bits = 8;
desired.alpha_bits = 8;
desired.depth_bits = 24;
desired.stencil_bits = 8;
desired.doublebuffer = true;
desired.samples = (_sapp.sample_count > 1) ? _sapp.sample_count : 0;
int pixel_format = 0;
_sapp_gl_fbselect fbselect;
_sapp_gl_init_fbselect(&fbselect);
for (int i = 0; i < native_count; i++) {
const int n = i + 1;
_sapp_wgl_attribiv(n, query_count, query_tags, query_results);
if (query_results[result_support_opengl_index] == 0
|| query_results[result_draw_to_window_index] == 0
|| query_results[result_pixel_type_index] != WGL_TYPE_RGBA_ARB
|| query_results[result_acceleration_index] == WGL_NO_ACCELERATION_ARB)
{
continue;
}
_sapp_gl_fbconfig u;
_sapp_clear(&u, sizeof(u));
u.red_bits = query_results[result_red_bits_index];
u.green_bits = query_results[result_green_bits_index];
u.blue_bits = query_results[result_blue_bits_index];
u.alpha_bits = query_results[result_alpha_bits_index];
u.depth_bits = query_results[result_depth_bits_index];
u.stencil_bits = query_results[result_stencil_bits_index];
u.doublebuffer = 0 != query_results[result_double_buffer_index];
u.samples = query_results[result_samples_index]; // NOTE: If arb_multisample is not supported - just takes the default 0
// Test if this pixel format is better than the previous one
if (_sapp_gl_select_fbconfig(&fbselect, &desired, &u)) {
pixel_format = (uintptr_t)n;
// Early exit if matching as good as possible
if (fbselect.best_match) {
break;
}
}
}
return pixel_format;
}
_SOKOL_PRIVATE void _sapp_wgl_create_context(void) {
int pixel_format = _sapp_wgl_find_pixel_format();
if (0 == pixel_format) {
_SAPP_PANIC(WIN32_WGL_FIND_PIXELFORMAT_FAILED);
}
PIXELFORMATDESCRIPTOR pfd;
if (!DescribePixelFormat(_sapp.win32.dc, pixel_format, sizeof(pfd), &pfd)) {
_SAPP_PANIC(WIN32_WGL_DESCRIBE_PIXELFORMAT_FAILED);
}
if (!SetPixelFormat(_sapp.win32.dc, pixel_format, &pfd)) {
_SAPP_PANIC(WIN32_WGL_SET_PIXELFORMAT_FAILED);
}
if (!_sapp.wgl.arb_create_context) {
_SAPP_PANIC(WIN32_WGL_ARB_CREATE_CONTEXT_REQUIRED);
}
if (!_sapp.wgl.arb_create_context_profile) {
_SAPP_PANIC(WIN32_WGL_ARB_CREATE_CONTEXT_PROFILE_REQUIRED);
}
const int attrs[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, _sapp.desc.gl_major_version,
WGL_CONTEXT_MINOR_VERSION_ARB, _sapp.desc.gl_minor_version,
#if defined(SOKOL_DEBUG)
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB | WGL_CONTEXT_DEBUG_BIT_ARB,
#else
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
#endif
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0, 0
};
_sapp.wgl.gl_ctx = _sapp.wgl.CreateContextAttribsARB(_sapp.win32.dc, 0, attrs);
if (!_sapp.wgl.gl_ctx) {
const DWORD err = GetLastError();
if (err == (0xc0070000 | ERROR_INVALID_VERSION_ARB)) {
_SAPP_PANIC(WIN32_WGL_OPENGL_VERSION_NOT_SUPPORTED);
}
else if (err == (0xc0070000 | ERROR_INVALID_PROFILE_ARB)) {
_SAPP_PANIC(WIN32_WGL_OPENGL_PROFILE_NOT_SUPPORTED);
}
else if (err == (0xc0070000 | ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB)) {
_SAPP_PANIC(WIN32_WGL_INCOMPATIBLE_DEVICE_CONTEXT);
}
else {
_SAPP_PANIC(WIN32_WGL_CREATE_CONTEXT_ATTRIBS_FAILED_OTHER);
}
}
_sapp.wgl.MakeCurrent(_sapp.win32.dc, _sapp.wgl.gl_ctx);
if (_sapp.wgl.ext_swap_control) {
/* FIXME: DwmIsCompositionEnabled() (see GLFW) */
_sapp.wgl.SwapIntervalEXT(_sapp.swap_interval);
}
const uint32_t gl_framebuffer_binding = 0x8CA6;
_sapp.wgl.GetIntegerv(gl_framebuffer_binding, (int32_t*)&_sapp.gl.framebuffer);
}
_SOKOL_PRIVATE void _sapp_wgl_destroy_context(void) {
SOKOL_ASSERT(_sapp.wgl.gl_ctx);
_sapp.wgl.DeleteContext(_sapp.wgl.gl_ctx);
_sapp.wgl.gl_ctx = 0;
}
_SOKOL_PRIVATE void _sapp_wgl_swap_buffers(void) {
SOKOL_ASSERT(_sapp.win32.dc);
/* FIXME: DwmIsCompositionEnabled? (see GLFW) */
SwapBuffers(_sapp.win32.dc);
}
#endif /* SOKOL_GLCORE */
_SOKOL_PRIVATE bool _sapp_win32_wide_to_utf8(const wchar_t* src, char* dst, int dst_num_bytes) {
SOKOL_ASSERT(src && dst && (dst_num_bytes > 1));
_sapp_clear(dst, (size_t)dst_num_bytes);
const int bytes_needed = WideCharToMultiByte(CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL);
if (bytes_needed <= dst_num_bytes) {
WideCharToMultiByte(CP_UTF8, 0, src, -1, dst, dst_num_bytes, NULL, NULL);
return true;
}
else {
return false;
}
}
/* updates current window and framebuffer size from the window's client rect, returns true if size has changed */
_SOKOL_PRIVATE bool _sapp_win32_update_dimensions(void) {
RECT rect;
if (GetClientRect(_sapp.win32.hwnd, &rect)) {
float window_width = (float)(rect.right - rect.left) / _sapp.win32.dpi.window_scale;
float window_height = (float)(rect.bottom - rect.top) / _sapp.win32.dpi.window_scale;
_sapp.window_width = (int)roundf(window_width);
_sapp.window_height = (int)roundf(window_height);
int fb_width = (int)roundf(window_width * _sapp.win32.dpi.content_scale);
int fb_height = (int)roundf(window_height * _sapp.win32.dpi.content_scale);
/* prevent a framebuffer size of 0 when window is minimized */
if (0 == fb_width) {
fb_width = 1;
}
if (0 == fb_height) {
fb_height = 1;
}
if ((fb_width != _sapp.framebuffer_width) || (fb_height != _sapp.framebuffer_height)) {
_sapp.framebuffer_width = fb_width;
_sapp.framebuffer_height = fb_height;
return true;
}
}
else {
_sapp.window_width = _sapp.window_height = 1;
_sapp.framebuffer_width = _sapp.framebuffer_height = 1;
}
return false;
}
_SOKOL_PRIVATE void _sapp_win32_set_fullscreen(bool fullscreen, UINT swp_flags) {
HMONITOR monitor = MonitorFromWindow(_sapp.win32.hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO minfo;
_sapp_clear(&minfo, sizeof(minfo));
minfo.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &minfo);
const RECT mr = minfo.rcMonitor;
const int monitor_w = mr.right - mr.left;
const int monitor_h = mr.bottom - mr.top;
const DWORD win_ex_style = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
DWORD win_style;
RECT rect = { 0, 0, 0, 0 };
_sapp.fullscreen = fullscreen;
if (!_sapp.fullscreen) {
win_style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX;
rect = _sapp.win32.stored_window_rect;
}
else {
GetWindowRect(_sapp.win32.hwnd, &_sapp.win32.stored_window_rect);
win_style = WS_POPUP | WS_SYSMENU | WS_VISIBLE;
rect.left = mr.left;
rect.top = mr.top;
rect.right = rect.left + monitor_w;
rect.bottom = rect.top + monitor_h;
AdjustWindowRectEx(&rect, win_style, FALSE, win_ex_style);
}
const int win_w = rect.right - rect.left;
const int win_h = rect.bottom - rect.top;
const int win_x = rect.left;
const int win_y = rect.top;
SetWindowLongPtr(_sapp.win32.hwnd, GWL_STYLE, win_style);
SetWindowPos(_sapp.win32.hwnd, HWND_TOP, win_x, win_y, win_w, win_h, swp_flags | SWP_FRAMECHANGED);
}
_SOKOL_PRIVATE void _sapp_win32_toggle_fullscreen(void) {
_sapp_win32_set_fullscreen(!_sapp.fullscreen, SWP_SHOWWINDOW);
}
_SOKOL_PRIVATE void _sapp_win32_init_cursor(sapp_mouse_cursor cursor) {
SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM));
// NOTE: the OCR_* constants are only defined if OEMRESOURCE is defined
// before windows.h is included, but we can't guarantee that because
// the sokol_app.h implementation may be included with other implementations
// in the same compilation unit
int id = 0;
switch (cursor) {
case SAPP_MOUSECURSOR_ARROW: id = 32512; break; // OCR_NORMAL
case SAPP_MOUSECURSOR_IBEAM: id = 32513; break; // OCR_IBEAM
case SAPP_MOUSECURSOR_CROSSHAIR: id = 32515; break; // OCR_CROSS
case SAPP_MOUSECURSOR_POINTING_HAND: id = 32649; break; // OCR_HAND
case SAPP_MOUSECURSOR_RESIZE_EW: id = 32644; break; // OCR_SIZEWE
case SAPP_MOUSECURSOR_RESIZE_NS: id = 32645; break; // OCR_SIZENS
case SAPP_MOUSECURSOR_RESIZE_NWSE: id = 32642; break; // OCR_SIZENWSE
case SAPP_MOUSECURSOR_RESIZE_NESW: id = 32643; break; // OCR_SIZENESW
case SAPP_MOUSECURSOR_RESIZE_ALL: id = 32646; break; // OCR_SIZEALL
case SAPP_MOUSECURSOR_NOT_ALLOWED: id = 32648; break; // OCR_NO
default: break;
}
if (id != 0) {
_sapp.win32.cursors[cursor] = (HCURSOR)LoadImageW(NULL, MAKEINTRESOURCEW(id), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE|LR_SHARED);
}
// fallback: default cursor
if (0 == _sapp.win32.cursors[cursor]) {
// 32512 => IDC_ARROW
_sapp.win32.cursors[cursor] = LoadCursorW(NULL, MAKEINTRESOURCEW(32512));
}
SOKOL_ASSERT(0 != _sapp.win32.cursors[cursor]);
}
_SOKOL_PRIVATE void _sapp_win32_init_cursors(void) {
for (int i = 0; i < _SAPP_MOUSECURSOR_NUM; i++) {
_sapp_win32_init_cursor((sapp_mouse_cursor)i);
}
}
_SOKOL_PRIVATE bool _sapp_win32_cursor_in_content_area(void) {
POINT pos;
if (!GetCursorPos(&pos)) {
return false;
}
if (WindowFromPoint(pos) != _sapp.win32.hwnd) {
return false;
}
RECT area;
GetClientRect(_sapp.win32.hwnd, &area);
ClientToScreen(_sapp.win32.hwnd, (POINT*)&area.left);
ClientToScreen(_sapp.win32.hwnd, (POINT*)&area.right);
return PtInRect(&area, pos) == TRUE;
}
_SOKOL_PRIVATE void _sapp_win32_update_cursor(sapp_mouse_cursor cursor, bool shown, bool skip_area_test) {
// NOTE: when called from WM_SETCURSOR, the area test would be redundant
if (!skip_area_test) {
if (!_sapp_win32_cursor_in_content_area()) {
return;
}
}
if (!shown) {
SetCursor(NULL);
}
else {
SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM));
SOKOL_ASSERT(0 != _sapp.win32.cursors[cursor]);
SetCursor(_sapp.win32.cursors[cursor]);
}
}
_SOKOL_PRIVATE void _sapp_win32_capture_mouse(uint8_t btn_mask) {
if (0 == _sapp.win32.mouse_capture_mask) {
SetCapture(_sapp.win32.hwnd);
}
_sapp.win32.mouse_capture_mask |= btn_mask;
}
_SOKOL_PRIVATE void _sapp_win32_release_mouse(uint8_t btn_mask) {
if (0 != _sapp.win32.mouse_capture_mask) {
_sapp.win32.mouse_capture_mask &= ~btn_mask;
if (0 == _sapp.win32.mouse_capture_mask) {
ReleaseCapture();
}
}
}
_SOKOL_PRIVATE void _sapp_win32_lock_mouse(bool lock) {
if (lock == _sapp.mouse.locked) {
return;
}
_sapp.mouse.dx = 0.0f;
_sapp.mouse.dy = 0.0f;
_sapp.mouse.locked = lock;
_sapp_win32_release_mouse(0xFF);
if (_sapp.mouse.locked) {
/* store the current mouse position, so it can be restored when unlocked */
POINT pos;
BOOL res = GetCursorPos(&pos);
SOKOL_ASSERT(res); _SOKOL_UNUSED(res);
_sapp.win32.mouse_locked_x = pos.x;
_sapp.win32.mouse_locked_y = pos.y;
/* while the mouse is locked, make the mouse cursor invisible and
confine the mouse movement to a small rectangle inside our window
(so that we don't miss any mouse up events)
*/
RECT client_rect = {
_sapp.win32.mouse_locked_x,
_sapp.win32.mouse_locked_y,
_sapp.win32.mouse_locked_x,
_sapp.win32.mouse_locked_y
};
ClipCursor(&client_rect);
/* make the mouse cursor invisible, this will stack with sapp_show_mouse() */
ShowCursor(FALSE);
/* enable raw input for mouse, starts sending WM_INPUT messages to WinProc (see GLFW) */
const RAWINPUTDEVICE rid = {
0x01, // usUsagePage: HID_USAGE_PAGE_GENERIC
0x02, // usUsage: HID_USAGE_GENERIC_MOUSE
0, // dwFlags
_sapp.win32.hwnd // hwndTarget
};
if (!RegisterRawInputDevices(&rid, 1, sizeof(rid))) {
_SAPP_ERROR(WIN32_REGISTER_RAW_INPUT_DEVICES_FAILED_MOUSE_LOCK);
}
/* in case the raw mouse device only supports absolute position reporting,
we need to skip the dx/dy compution for the first WM_INPUT event
*/
_sapp.win32.raw_input_mousepos_valid = false;
}
else {
/* disable raw input for mouse */
const RAWINPUTDEVICE rid = { 0x01, 0x02, RIDEV_REMOVE, NULL };
if (!RegisterRawInputDevices(&rid, 1, sizeof(rid))) {
_SAPP_ERROR(WIN32_REGISTER_RAW_INPUT_DEVICES_FAILED_MOUSE_UNLOCK);
}
/* let the mouse roam freely again */
ClipCursor(NULL);
ShowCursor(TRUE);
/* restore the 'pre-locked' mouse position */
BOOL res = SetCursorPos(_sapp.win32.mouse_locked_x, _sapp.win32.mouse_locked_y);
SOKOL_ASSERT(res); _SOKOL_UNUSED(res);
}
}
_SOKOL_PRIVATE bool _sapp_win32_update_monitor(void) {
const HMONITOR cur_monitor = MonitorFromWindow(_sapp.win32.hwnd, MONITOR_DEFAULTTONULL);
if (cur_monitor != _sapp.win32.hmonitor) {
_sapp.win32.hmonitor = cur_monitor;
return true;
}
else {
return false;
}
}
_SOKOL_PRIVATE uint32_t _sapp_win32_mods(void) {
uint32_t mods = 0;
if (GetKeyState(VK_SHIFT) & (1<<15)) {
mods |= SAPP_MODIFIER_SHIFT;
}
if (GetKeyState(VK_CONTROL) & (1<<15)) {
mods |= SAPP_MODIFIER_CTRL;
}
if (GetKeyState(VK_MENU) & (1<<15)) {
mods |= SAPP_MODIFIER_ALT;
}
if ((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & (1<<15)) {
mods |= SAPP_MODIFIER_SUPER;
}
const bool swapped = (TRUE == GetSystemMetrics(SM_SWAPBUTTON));
if (GetAsyncKeyState(VK_LBUTTON)) {
mods |= swapped ? SAPP_MODIFIER_RMB : SAPP_MODIFIER_LMB;
}
if (GetAsyncKeyState(VK_RBUTTON)) {
mods |= swapped ? SAPP_MODIFIER_LMB : SAPP_MODIFIER_RMB;
}
if (GetAsyncKeyState(VK_MBUTTON)) {
mods |= SAPP_MODIFIER_MMB;
}
return mods;
}
_SOKOL_PRIVATE void _sapp_win32_mouse_update(LPARAM lParam) {
if (!_sapp.mouse.locked) {
const float new_x = (float)GET_X_LPARAM(lParam) * _sapp.win32.dpi.mouse_scale;
const float new_y = (float)GET_Y_LPARAM(lParam) * _sapp.win32.dpi.mouse_scale;
if (_sapp.mouse.pos_valid) {
// don't update dx/dy in the very first event
_sapp.mouse.dx = new_x - _sapp.mouse.x;
_sapp.mouse.dy = new_y - _sapp.mouse.y;
}
_sapp.mouse.x = new_x;
_sapp.mouse.y = new_y;
_sapp.mouse.pos_valid = true;
}
}
_SOKOL_PRIVATE void _sapp_win32_mouse_event(sapp_event_type type, sapp_mousebutton btn) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
_sapp.event.modifiers = _sapp_win32_mods();
_sapp.event.mouse_button = btn;
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE void _sapp_win32_scroll_event(float x, float y) {
if (_sapp_events_enabled()) {
_sapp_init_event(SAPP_EVENTTYPE_MOUSE_SCROLL);
_sapp.event.modifiers = _sapp_win32_mods();
_sapp.event.scroll_x = -x / 30.0f;
_sapp.event.scroll_y = y / 30.0f;
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE void _sapp_win32_key_event(sapp_event_type type, int vk, bool repeat) {
if (_sapp_events_enabled() && (vk < SAPP_MAX_KEYCODES)) {
_sapp_init_event(type);
_sapp.event.modifiers = _sapp_win32_mods();
_sapp.event.key_code = _sapp.keycodes[vk];
_sapp.event.key_repeat = repeat;
_sapp_call_event(&_sapp.event);
/* check if a CLIPBOARD_PASTED event must be sent too */
if (_sapp.clipboard.enabled &&
(type == SAPP_EVENTTYPE_KEY_DOWN) &&
(_sapp.event.modifiers == SAPP_MODIFIER_CTRL) &&
(_sapp.event.key_code == SAPP_KEYCODE_V))
{
_sapp_init_event(SAPP_EVENTTYPE_CLIPBOARD_PASTED);
_sapp_call_event(&_sapp.event);
}
}
}
_SOKOL_PRIVATE void _sapp_win32_char_event(uint32_t c, bool repeat) {
if (_sapp_events_enabled() && (c >= 32)) {
_sapp_init_event(SAPP_EVENTTYPE_CHAR);
_sapp.event.modifiers = _sapp_win32_mods();
_sapp.event.char_code = c;
_sapp.event.key_repeat = repeat;
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE void _sapp_win32_dpi_changed(HWND hWnd, LPRECT proposed_win_rect) {
/* called on WM_DPICHANGED, which will only be sent to the application
if sapp_desc.high_dpi is true and the Windows version is recent enough
to support DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
*/
SOKOL_ASSERT(_sapp.desc.high_dpi);
HINSTANCE user32 = LoadLibraryA("user32.dll");
if (!user32) {
return;
}
typedef UINT(WINAPI * GETDPIFORWINDOW_T)(HWND hwnd);
GETDPIFORWINDOW_T fn_getdpiforwindow = (GETDPIFORWINDOW_T)(void*)GetProcAddress(user32, "GetDpiForWindow");
if (fn_getdpiforwindow) {
UINT dpix = fn_getdpiforwindow(_sapp.win32.hwnd);
// NOTE: for high-dpi apps, mouse_scale remains one
_sapp.win32.dpi.window_scale = (float)dpix / 96.0f;
_sapp.win32.dpi.content_scale = _sapp.win32.dpi.window_scale;
_sapp.dpi_scale = _sapp.win32.dpi.window_scale;
SetWindowPos(hWnd, 0,
proposed_win_rect->left,
proposed_win_rect->top,
proposed_win_rect->right - proposed_win_rect->left,
proposed_win_rect->bottom - proposed_win_rect->top,
SWP_NOZORDER | SWP_NOACTIVATE);
}
FreeLibrary(user32);
}
_SOKOL_PRIVATE void _sapp_win32_files_dropped(HDROP hdrop) {
if (!_sapp.drop.enabled) {
return;
}
_sapp_clear_drop_buffer();
bool drop_failed = false;
const int count = (int) DragQueryFileW(hdrop, 0xffffffff, NULL, 0);
_sapp.drop.num_files = (count > _sapp.drop.max_files) ? _sapp.drop.max_files : count;
for (UINT i = 0; i < (UINT)_sapp.drop.num_files; i++) {
const UINT num_chars = DragQueryFileW(hdrop, i, NULL, 0) + 1;
WCHAR* buffer = (WCHAR*) _sapp_malloc_clear(num_chars * sizeof(WCHAR));
DragQueryFileW(hdrop, i, buffer, num_chars);
if (!_sapp_win32_wide_to_utf8(buffer, _sapp_dropped_file_path_ptr((int)i), _sapp.drop.max_path_length)) {
_SAPP_ERROR(DROPPED_FILE_PATH_TOO_LONG);
drop_failed = true;
}
_sapp_free(buffer);
}
DragFinish(hdrop);
if (!drop_failed) {
if (_sapp_events_enabled()) {
_sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED);
_sapp.event.modifiers = _sapp_win32_mods();
_sapp_call_event(&_sapp.event);
}
}
else {
_sapp_clear_drop_buffer();
_sapp.drop.num_files = 0;
}
}
_SOKOL_PRIVATE void _sapp_win32_timing_measure(void) {
#if defined(SOKOL_D3D11)
// on D3D11, use the more precise DXGI timestamp
if (_sapp.d3d11.use_dxgi_frame_stats) {
DXGI_FRAME_STATISTICS dxgi_stats;
_sapp_clear(&dxgi_stats, sizeof(dxgi_stats));
HRESULT hr = _sapp_dxgi_GetFrameStatistics(_sapp.d3d11.swap_chain, &dxgi_stats);
if (SUCCEEDED(hr)) {
if (dxgi_stats.SyncRefreshCount != _sapp.d3d11.sync_refresh_count) {
if ((_sapp.d3d11.sync_refresh_count + 1) != dxgi_stats.SyncRefreshCount) {
_sapp_timing_discontinuity(&_sapp.timing);
}
_sapp.d3d11.sync_refresh_count = dxgi_stats.SyncRefreshCount;
LARGE_INTEGER qpc = dxgi_stats.SyncQPCTime;
const uint64_t now = (uint64_t)_sapp_int64_muldiv(qpc.QuadPart - _sapp.timing.timestamp.win.start.QuadPart, 1000000000, _sapp.timing.timestamp.win.freq.QuadPart);
_sapp_timing_external(&_sapp.timing, (double)now / 1000000000.0);
}
return;
}
}
// fallback if swap model isn't "flip-discard" or GetFrameStatistics failed for another reason
_sapp_timing_measure(&_sapp.timing);
#endif
#if defined(SOKOL_GLCORE)
_sapp_timing_measure(&_sapp.timing);
#endif
#if defined(SOKOL_NOAPI)
_sapp_timing_measure(&_sapp.timing);
#endif
}
_SOKOL_PRIVATE LRESULT CALLBACK _sapp_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
if (!_sapp.win32.in_create_window) {
switch (uMsg) {
case WM_CLOSE:
/* only give user a chance to intervene when sapp_quit() wasn't already called */
if (!_sapp.quit_ordered) {
/* if window should be closed and event handling is enabled, give user code
a change to intervene via sapp_cancel_quit()
*/
_sapp.quit_requested = true;
_sapp_win32_app_event(SAPP_EVENTTYPE_QUIT_REQUESTED);
/* if user code hasn't intervened, quit the app */
if (_sapp.quit_requested) {
_sapp.quit_ordered = true;
}
}
if (_sapp.quit_ordered) {
PostQuitMessage(0);
}
return 0;
case WM_SYSCOMMAND:
switch (wParam & 0xFFF0) {
case SC_SCREENSAVE:
case SC_MONITORPOWER:
if (_sapp.fullscreen) {
/* disable screen saver and blanking in fullscreen mode */
return 0;
}
break;
case SC_KEYMENU:
/* user trying to access menu via ALT */
return 0;
}
break;
case WM_ERASEBKGND:
return 1;
case WM_SIZE:
{
const bool iconified = wParam == SIZE_MINIMIZED;
if (iconified != _sapp.win32.iconified) {
_sapp.win32.iconified = iconified;
if (iconified) {
_sapp_win32_app_event(SAPP_EVENTTYPE_ICONIFIED);
}
else {
_sapp_win32_app_event(SAPP_EVENTTYPE_RESTORED);
}
}
}
break;
case WM_SETFOCUS:
_sapp_win32_app_event(SAPP_EVENTTYPE_FOCUSED);
break;
case WM_KILLFOCUS:
/* if focus is lost for any reason, and we're in mouse locked mode, disable mouse lock */
if (_sapp.mouse.locked) {
_sapp_win32_lock_mouse(false);
}
_sapp_win32_app_event(SAPP_EVENTTYPE_UNFOCUSED);
break;
case WM_SETCURSOR:
if (LOWORD(lParam) == HTCLIENT) {
_sapp_win32_update_cursor(_sapp.mouse.current_cursor, _sapp.mouse.shown, true);
return TRUE;
}
break;
case WM_DPICHANGED:
{
/* Update window's DPI and size if its moved to another monitor with a different DPI
Only sent if DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 is used.
*/
_sapp_win32_dpi_changed(hWnd, (LPRECT)lParam);
break;
}
case WM_LBUTTONDOWN:
_sapp_win32_mouse_update(lParam);
_sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_LEFT);
_sapp_win32_capture_mouse(1<<SAPP_MOUSEBUTTON_LEFT);
break;
case WM_RBUTTONDOWN:
_sapp_win32_mouse_update(lParam);
_sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_RIGHT);
_sapp_win32_capture_mouse(1<<SAPP_MOUSEBUTTON_RIGHT);
break;
case WM_MBUTTONDOWN:
_sapp_win32_mouse_update(lParam);
_sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_MIDDLE);
_sapp_win32_capture_mouse(1<<SAPP_MOUSEBUTTON_MIDDLE);
break;
case WM_LBUTTONUP:
_sapp_win32_mouse_update(lParam);
_sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_LEFT);
_sapp_win32_release_mouse(1<<SAPP_MOUSEBUTTON_LEFT);
break;
case WM_RBUTTONUP:
_sapp_win32_mouse_update(lParam);
_sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_RIGHT);
_sapp_win32_release_mouse(1<<SAPP_MOUSEBUTTON_RIGHT);
break;
case WM_MBUTTONUP:
_sapp_win32_mouse_update(lParam);
_sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_MIDDLE);
_sapp_win32_release_mouse(1<<SAPP_MOUSEBUTTON_MIDDLE);
break;
case WM_MOUSEMOVE:
if (!_sapp.mouse.locked) {
_sapp_win32_mouse_update(lParam);
if (!_sapp.win32.mouse_tracked) {
_sapp.win32.mouse_tracked = true;
TRACKMOUSEEVENT tme;
_sapp_clear(&tme, sizeof(tme));
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = _sapp.win32.hwnd;
TrackMouseEvent(&tme);
_sapp.mouse.dx = 0.0f;
_sapp.mouse.dy = 0.0f;
_sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_ENTER, SAPP_MOUSEBUTTON_INVALID);
}
_sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID);
}
break;
case WM_INPUT:
/* raw mouse input during mouse-lock */
if (_sapp.mouse.locked) {
HRAWINPUT ri = (HRAWINPUT) lParam;
UINT size = sizeof(_sapp.win32.raw_input_data);
// see: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getrawinputdata
if ((UINT)-1 == GetRawInputData(ri, RID_INPUT, &_sapp.win32.raw_input_data, &size, sizeof(RAWINPUTHEADER))) {
_SAPP_ERROR(WIN32_GET_RAW_INPUT_DATA_FAILED);
break;
}
const RAWINPUT* raw_mouse_data = (const RAWINPUT*) &_sapp.win32.raw_input_data;
if (raw_mouse_data->data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE) {
/* mouse only reports absolute position
NOTE: This code is untested and will most likely behave wrong in Remote Desktop sessions.
(such remote desktop sessions are setting the MOUSE_MOVE_ABSOLUTE flag).
See: https://github.com/floooh/sokol/issues/806 and
https://github.com/microsoft/DirectXTK/commit/ef56b63f3739381e451f7a5a5bd2c9779d2a7555)
*/
LONG new_x = raw_mouse_data->data.mouse.lLastX;
LONG new_y = raw_mouse_data->data.mouse.lLastY;
if (_sapp.win32.raw_input_mousepos_valid) {
_sapp.mouse.dx = (float) (new_x - _sapp.win32.raw_input_mousepos_x);
_sapp.mouse.dy = (float) (new_y - _sapp.win32.raw_input_mousepos_y);
}
_sapp.win32.raw_input_mousepos_x = new_x;
_sapp.win32.raw_input_mousepos_y = new_y;
_sapp.win32.raw_input_mousepos_valid = true;
}
else {
/* mouse reports movement delta (this seems to be the common case) */
_sapp.mouse.dx = (float) raw_mouse_data->data.mouse.lLastX;
_sapp.mouse.dy = (float) raw_mouse_data->data.mouse.lLastY;
}
_sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID);
}
break;
case WM_MOUSELEAVE:
if (!_sapp.mouse.locked) {
_sapp.mouse.dx = 0.0f;
_sapp.mouse.dy = 0.0f;
_sapp.win32.mouse_tracked = false;
_sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_LEAVE, SAPP_MOUSEBUTTON_INVALID);
}
break;
case WM_MOUSEWHEEL:
_sapp_win32_scroll_event(0.0f, (float)((SHORT)HIWORD(wParam)));
break;
case WM_MOUSEHWHEEL:
_sapp_win32_scroll_event((float)((SHORT)HIWORD(wParam)), 0.0f);
break;
case WM_CHAR:
_sapp_win32_char_event((uint32_t)wParam, !!(lParam&0x40000000));
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
_sapp_win32_key_event(SAPP_EVENTTYPE_KEY_DOWN, (int)(HIWORD(lParam)&0x1FF), !!(lParam&0x40000000));
break;
case WM_KEYUP:
case WM_SYSKEYUP:
_sapp_win32_key_event(SAPP_EVENTTYPE_KEY_UP, (int)(HIWORD(lParam)&0x1FF), false);
break;
case WM_ENTERSIZEMOVE:
SetTimer(_sapp.win32.hwnd, 1, USER_TIMER_MINIMUM, NULL);
break;
case WM_EXITSIZEMOVE:
KillTimer(_sapp.win32.hwnd, 1);
break;
case WM_TIMER:
_sapp_win32_timing_measure();
_sapp_frame();
#if defined(SOKOL_D3D11)
// present with DXGI_PRESENT_DO_NOT_WAIT
_sapp_d3d11_present(true);
#endif
#if defined(SOKOL_GLCORE)
_sapp_wgl_swap_buffers();
#endif
/* NOTE: resizing the swap-chain during resize leads to a substantial
memory spike (hundreds of megabytes for a few seconds).
if (_sapp_win32_update_dimensions()) {
#if defined(SOKOL_D3D11)
_sapp_d3d11_resize_default_render_target();
#endif
_sapp_win32_app_event(SAPP_EVENTTYPE_RESIZED);
}
*/
break;
case WM_NCLBUTTONDOWN:
/* workaround for half-second pause when starting to move window
see: https://gamedev.net/forums/topic/672094-keeping-things-moving-during-win32-moveresize-events/5254386/
*/
if (SendMessage(_sapp.win32.hwnd, WM_NCHITTEST, wParam, lParam) == HTCAPTION) {
POINT point;
GetCursorPos(&point);
ScreenToClient(_sapp.win32.hwnd, &point);
PostMessage(_sapp.win32.hwnd, WM_MOUSEMOVE, 0, ((uint32_t)point.x)|(((uint32_t)point.y) << 16));
}
break;
case WM_DROPFILES:
_sapp_win32_files_dropped((HDROP)wParam);
break;
case WM_DISPLAYCHANGE:
// refresh rate might have changed
_sapp_timing_reset(&_sapp.timing);
break;
default:
break;
}
}
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
}
_SOKOL_PRIVATE void _sapp_win32_create_window(void) {
WNDCLASSW wndclassw;
_sapp_clear(&wndclassw, sizeof(wndclassw));
wndclassw.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndclassw.lpfnWndProc = (WNDPROC) _sapp_win32_wndproc;
wndclassw.hInstance = GetModuleHandleW(NULL);
wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclassw.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wndclassw.lpszClassName = L"SOKOLAPP";
RegisterClassW(&wndclassw);
/* NOTE: regardless whether fullscreen is requested or not, a regular
windowed-mode window will always be created first (however in hidden
mode, so that no windowed-mode window pops up before the fullscreen window)
*/
const DWORD win_ex_style = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
RECT rect = { 0, 0, 0, 0 };
DWORD win_style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX;
rect.right = (int) ((float)_sapp.window_width * _sapp.win32.dpi.window_scale);
rect.bottom = (int) ((float)_sapp.window_height * _sapp.win32.dpi.window_scale);
const bool use_default_width = 0 == _sapp.window_width;
const bool use_default_height = 0 == _sapp.window_height;
AdjustWindowRectEx(&rect, win_style, FALSE, win_ex_style);
const int win_width = rect.right - rect.left;
const int win_height = rect.bottom - rect.top;
_sapp.win32.in_create_window = true;
_sapp.win32.hwnd = CreateWindowExW(
win_ex_style, // dwExStyle
L"SOKOLAPP", // lpClassName
_sapp.window_title_wide, // lpWindowName
win_style, // dwStyle
CW_USEDEFAULT, // X
SW_HIDE, // Y (NOTE: CW_USEDEFAULT is not used for position here, but internally calls ShowWindow!
use_default_width ? CW_USEDEFAULT : win_width, // nWidth
use_default_height ? CW_USEDEFAULT : win_height, // nHeight (NOTE: if width is CW_USEDEFAULT, height is actually ignored)
NULL, // hWndParent
NULL, // hMenu
GetModuleHandle(NULL), // hInstance
NULL); // lParam
_sapp.win32.in_create_window = false;
_sapp.win32.dc = GetDC(_sapp.win32.hwnd);
_sapp.win32.hmonitor = MonitorFromWindow(_sapp.win32.hwnd, MONITOR_DEFAULTTONULL);
SOKOL_ASSERT(_sapp.win32.dc);
/* this will get the actual windowed-mode window size, if fullscreen
is requested, the set_fullscreen function will then capture the
current window rectangle, which then might be used later to
restore the window position when switching back to windowed
*/
_sapp_win32_update_dimensions();
if (_sapp.fullscreen) {
_sapp_win32_set_fullscreen(_sapp.fullscreen, SWP_HIDEWINDOW);
_sapp_win32_update_dimensions();
}
ShowWindow(_sapp.win32.hwnd, SW_SHOW);
DragAcceptFiles(_sapp.win32.hwnd, 1);
}
_SOKOL_PRIVATE void _sapp_win32_destroy_window(void) {
DestroyWindow(_sapp.win32.hwnd); _sapp.win32.hwnd = 0;
UnregisterClassW(L"SOKOLAPP", GetModuleHandleW(NULL));
}
_SOKOL_PRIVATE void _sapp_win32_destroy_icons(void) {
if (_sapp.win32.big_icon) {
DestroyIcon(_sapp.win32.big_icon);
_sapp.win32.big_icon = 0;
}
if (_sapp.win32.small_icon) {
DestroyIcon(_sapp.win32.small_icon);
_sapp.win32.small_icon = 0;
}
}
_SOKOL_PRIVATE void _sapp_win32_init_console(void) {
if (_sapp.desc.win32_console_create || _sapp.desc.win32_console_attach) {
BOOL con_valid = FALSE;
if (_sapp.desc.win32_console_create) {
con_valid = AllocConsole();
}
else if (_sapp.desc.win32_console_attach) {
con_valid = AttachConsole(ATTACH_PARENT_PROCESS);
}
if (con_valid) {
FILE* res_fp = 0;
errno_t err;
err = freopen_s(&res_fp, "CON", "w", stdout);
(void)err;
err = freopen_s(&res_fp, "CON", "w", stderr);
(void)err;
}
}
if (_sapp.desc.win32_console_utf8) {
_sapp.win32.orig_codepage = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
}
}
_SOKOL_PRIVATE void _sapp_win32_restore_console(void) {
if (_sapp.desc.win32_console_utf8) {
SetConsoleOutputCP(_sapp.win32.orig_codepage);
}
}
_SOKOL_PRIVATE void _sapp_win32_init_dpi(void) {
DECLARE_HANDLE(DPI_AWARENESS_CONTEXT_T);
typedef BOOL(WINAPI * SETPROCESSDPIAWARE_T)(void);
typedef bool (WINAPI * SETPROCESSDPIAWARENESSCONTEXT_T)(DPI_AWARENESS_CONTEXT_T); // since Windows 10, version 1703
typedef HRESULT(WINAPI * SETPROCESSDPIAWARENESS_T)(PROCESS_DPI_AWARENESS);
typedef HRESULT(WINAPI * GETDPIFORMONITOR_T)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*);
SETPROCESSDPIAWARE_T fn_setprocessdpiaware = 0;
SETPROCESSDPIAWARENESS_T fn_setprocessdpiawareness = 0;
GETDPIFORMONITOR_T fn_getdpiformonitor = 0;
SETPROCESSDPIAWARENESSCONTEXT_T fn_setprocessdpiawarenesscontext =0;
HINSTANCE user32 = LoadLibraryA("user32.dll");
if (user32) {
fn_setprocessdpiaware = (SETPROCESSDPIAWARE_T)(void*) GetProcAddress(user32, "SetProcessDPIAware");
fn_setprocessdpiawarenesscontext = (SETPROCESSDPIAWARENESSCONTEXT_T)(void*) GetProcAddress(user32, "SetProcessDpiAwarenessContext");
}
HINSTANCE shcore = LoadLibraryA("shcore.dll");
if (shcore) {
fn_setprocessdpiawareness = (SETPROCESSDPIAWARENESS_T)(void*) GetProcAddress(shcore, "SetProcessDpiAwareness");
fn_getdpiformonitor = (GETDPIFORMONITOR_T)(void*) GetProcAddress(shcore, "GetDpiForMonitor");
}
/*
NOTE on SetProcessDpiAware() vs SetProcessDpiAwareness() vs SetProcessDpiAwarenessContext():
These are different attempts to get DPI handling on Windows right, from oldest
to newest. SetProcessDpiAwarenessContext() is required for the new
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 method.
*/
if (fn_setprocessdpiawareness) {
if (_sapp.desc.high_dpi) {
/* app requests HighDPI rendering, first try the Win10 Creator Update per-monitor-dpi awareness,
if that fails, fall back to system-dpi-awareness
*/
_sapp.win32.dpi.aware = true;
DPI_AWARENESS_CONTEXT_T per_monitor_aware_v2 = (DPI_AWARENESS_CONTEXT_T)-4;
if (!(fn_setprocessdpiawarenesscontext && fn_setprocessdpiawarenesscontext(per_monitor_aware_v2))) {
// fallback to system-dpi-aware
fn_setprocessdpiawareness(PROCESS_SYSTEM_DPI_AWARE);
}
}
else {
/* if the app didn't request HighDPI rendering, let Windows do the upscaling */
_sapp.win32.dpi.aware = false;
fn_setprocessdpiawareness(PROCESS_DPI_UNAWARE);
}
}
else if (fn_setprocessdpiaware) {
// fallback for Windows 7
_sapp.win32.dpi.aware = true;
fn_setprocessdpiaware();
}
/* get dpi scale factor for main monitor */
if (fn_getdpiformonitor && _sapp.win32.dpi.aware) {
POINT pt = { 1, 1 };
HMONITOR hm = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
UINT dpix, dpiy;
HRESULT hr = fn_getdpiformonitor(hm, MDT_EFFECTIVE_DPI, &dpix, &dpiy);
_SOKOL_UNUSED(hr);
SOKOL_ASSERT(SUCCEEDED(hr));
/* clamp window scale to an integer factor */
_sapp.win32.dpi.window_scale = (float)dpix / 96.0f;
}
else {
_sapp.win32.dpi.window_scale = 1.0f;
}
if (_sapp.desc.high_dpi) {
_sapp.win32.dpi.content_scale = _sapp.win32.dpi.window_scale;
_sapp.win32.dpi.mouse_scale = 1.0f;
}
else {
_sapp.win32.dpi.content_scale = 1.0f;
_sapp.win32.dpi.mouse_scale = 1.0f / _sapp.win32.dpi.window_scale;
}
_sapp.dpi_scale = _sapp.win32.dpi.content_scale;
if (user32) {
FreeLibrary(user32);
}
if (shcore) {
FreeLibrary(shcore);
}
}
_SOKOL_PRIVATE bool _sapp_win32_set_clipboard_string(const char* str) {
SOKOL_ASSERT(str);
SOKOL_ASSERT(_sapp.win32.hwnd);
SOKOL_ASSERT(_sapp.clipboard.enabled && (_sapp.clipboard.buf_size > 0));
if (!OpenClipboard(_sapp.win32.hwnd)) {
return false;
}
HANDLE object = 0;
wchar_t* wchar_buf = 0;
const SIZE_T wchar_buf_size = (SIZE_T)_sapp.clipboard.buf_size * sizeof(wchar_t);
object = GlobalAlloc(GMEM_MOVEABLE, wchar_buf_size);
if (NULL == object) {
goto error;
}
wchar_buf = (wchar_t*) GlobalLock(object);
if (NULL == wchar_buf) {
goto error;
}
if (!_sapp_win32_utf8_to_wide(str, wchar_buf, (int)wchar_buf_size)) {
goto error;
}
GlobalUnlock(object);
wchar_buf = 0;
EmptyClipboard();
// NOTE: when successful, SetClipboardData() takes ownership of memory object!
if (NULL == SetClipboardData(CF_UNICODETEXT, object)) {
goto error;
}
CloseClipboard();
return true;
error:
if (wchar_buf) {
GlobalUnlock(object);
}
if (object) {
GlobalFree(object);
}
CloseClipboard();
return false;
}
_SOKOL_PRIVATE const char* _sapp_win32_get_clipboard_string(void) {
SOKOL_ASSERT(_sapp.clipboard.enabled && _sapp.clipboard.buffer);
SOKOL_ASSERT(_sapp.win32.hwnd);
if (!OpenClipboard(_sapp.win32.hwnd)) {
/* silently ignore any errors and just return the current
content of the local clipboard buffer
*/
return _sapp.clipboard.buffer;
}
HANDLE object = GetClipboardData(CF_UNICODETEXT);
if (!object) {
CloseClipboard();
return _sapp.clipboard.buffer;
}
const wchar_t* wchar_buf = (const wchar_t*) GlobalLock(object);
if (!wchar_buf) {
CloseClipboard();
return _sapp.clipboard.buffer;
}
if (!_sapp_win32_wide_to_utf8(wchar_buf, _sapp.clipboard.buffer, _sapp.clipboard.buf_size)) {
_SAPP_ERROR(CLIPBOARD_STRING_TOO_BIG);
}
GlobalUnlock(object);
CloseClipboard();
return _sapp.clipboard.buffer;
}
_SOKOL_PRIVATE void _sapp_win32_update_window_title(void) {
_sapp_win32_utf8_to_wide(_sapp.window_title, _sapp.window_title_wide, sizeof(_sapp.window_title_wide));
SetWindowTextW(_sapp.win32.hwnd, _sapp.window_title_wide);
}
_SOKOL_PRIVATE HICON _sapp_win32_create_icon_from_image(const sapp_image_desc* desc) {
BITMAPV5HEADER bi;
_sapp_clear(&bi, sizeof(bi));
bi.bV5Size = sizeof(bi);
bi.bV5Width = desc->width;
bi.bV5Height = -desc->height; // NOTE the '-' here to indicate that origin is top-left
bi.bV5Planes = 1;
bi.bV5BitCount = 32;
bi.bV5Compression = BI_BITFIELDS;
bi.bV5RedMask = 0x00FF0000;
bi.bV5GreenMask = 0x0000FF00;
bi.bV5BlueMask = 0x000000FF;
bi.bV5AlphaMask = 0xFF000000;
uint8_t* target = 0;
const uint8_t* source = (const uint8_t*)desc->pixels.ptr;
HDC dc = GetDC(NULL);
HBITMAP color = CreateDIBSection(dc, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&target, NULL, (DWORD)0);
ReleaseDC(NULL, dc);
if (0 == color) {
return NULL;
}
SOKOL_ASSERT(target);
HBITMAP mask = CreateBitmap(desc->width, desc->height, 1, 1, NULL);
if (0 == mask) {
DeleteObject(color);
return NULL;
}
for (int i = 0; i < (desc->width*desc->height); i++) {
target[0] = source[2];
target[1] = source[1];
target[2] = source[0];
target[3] = source[3];
target += 4;
source += 4;
}
ICONINFO icon_info;
_sapp_clear(&icon_info, sizeof(icon_info));
icon_info.fIcon = true;
icon_info.xHotspot = 0;
icon_info.yHotspot = 0;
icon_info.hbmMask = mask;
icon_info.hbmColor = color;
HICON icon_handle = CreateIconIndirect(&icon_info);
DeleteObject(color);
DeleteObject(mask);
return icon_handle;
}
_SOKOL_PRIVATE void _sapp_win32_set_icon(const sapp_icon_desc* icon_desc, int num_images) {
SOKOL_ASSERT((num_images > 0) && (num_images <= SAPP_MAX_ICONIMAGES));
int big_img_index = _sapp_image_bestmatch(icon_desc->images, num_images, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
int sml_img_index = _sapp_image_bestmatch(icon_desc->images, num_images, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
HICON big_icon = _sapp_win32_create_icon_from_image(&icon_desc->images[big_img_index]);
HICON sml_icon = _sapp_win32_create_icon_from_image(&icon_desc->images[sml_img_index]);
// if icon creation or lookup has failed for some reason, leave the currently set icon untouched
if (0 != big_icon) {
SendMessage(_sapp.win32.hwnd, WM_SETICON, ICON_BIG, (LPARAM) big_icon);
if (0 != _sapp.win32.big_icon) {
DestroyIcon(_sapp.win32.big_icon);
}
_sapp.win32.big_icon = big_icon;
}
if (0 != sml_icon) {
SendMessage(_sapp.win32.hwnd, WM_SETICON, ICON_SMALL, (LPARAM) sml_icon);
if (0 != _sapp.win32.small_icon) {
DestroyIcon(_sapp.win32.small_icon);
}
_sapp.win32.small_icon = sml_icon;
}
}
/* don't laugh, but this seems to be the easiest and most robust
way to check if we're running on Win10
From: https://github.com/videolan/vlc/blob/232fb13b0d6110c4d1b683cde24cf9a7f2c5c2ea/modules/video_output/win32/d3d11_swapchain.c#L263
*/
_SOKOL_PRIVATE bool _sapp_win32_is_win10_or_greater(void) {
HMODULE h = GetModuleHandleW(L"kernel32.dll");
if (NULL != h) {
return (NULL != GetProcAddress(h, "GetSystemCpuSetInformation"));
}
else {
return false;
}
}
_SOKOL_PRIVATE void _sapp_win32_run(const sapp_desc* desc) {
_sapp_init_state(desc);
_sapp_win32_init_console();
_sapp.win32.is_win10_or_greater = _sapp_win32_is_win10_or_greater();
_sapp_win32_init_keytable();
_sapp_win32_utf8_to_wide(_sapp.window_title, _sapp.window_title_wide, sizeof(_sapp.window_title_wide));
_sapp_win32_init_dpi();
_sapp_win32_init_cursors();
_sapp_win32_create_window();
sapp_set_icon(&desc->icon);
#if defined(SOKOL_D3D11)
_sapp_d3d11_create_device_and_swapchain();
_sapp_d3d11_create_default_render_target();
#endif
#if defined(SOKOL_GLCORE)
_sapp_wgl_init();
_sapp_wgl_load_extensions();
_sapp_wgl_create_context();
#endif
_sapp.valid = true;
bool done = false;
while (!(done || _sapp.quit_ordered)) {
_sapp_win32_timing_measure();
MSG msg;
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
if (WM_QUIT == msg.message) {
done = true;
continue;
}
else {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
_sapp_frame();
#if defined(SOKOL_D3D11)
_sapp_d3d11_present(false);
if (IsIconic(_sapp.win32.hwnd)) {
Sleep((DWORD)(16 * _sapp.swap_interval));
}
#endif
#if defined(SOKOL_GLCORE)
_sapp_wgl_swap_buffers();
#endif
/* check for window resized, this cannot happen in WM_SIZE as it explodes memory usage */
if (_sapp_win32_update_dimensions()) {
#if defined(SOKOL_D3D11)
_sapp_d3d11_resize_default_render_target();
#endif
_sapp_win32_app_event(SAPP_EVENTTYPE_RESIZED);
}
/* check if the window monitor has changed, need to reset timing because
the new monitor might have a different refresh rate
*/
if (_sapp_win32_update_monitor()) {
_sapp_timing_reset(&_sapp.timing);
}
if (_sapp.quit_requested) {
PostMessage(_sapp.win32.hwnd, WM_CLOSE, 0, 0);
}
}
_sapp_call_cleanup();
#if defined(SOKOL_D3D11)
_sapp_d3d11_destroy_default_render_target();
_sapp_d3d11_destroy_device_and_swapchain();
#else
_sapp_wgl_destroy_context();
_sapp_wgl_shutdown();
#endif
_sapp_win32_destroy_window();
_sapp_win32_destroy_icons();
_sapp_win32_restore_console();
_sapp_discard_state();
}
_SOKOL_PRIVATE char** _sapp_win32_command_line_to_utf8_argv(LPWSTR w_command_line, int* o_argc) {
int argc = 0;
char** argv = 0;
char* args;
LPWSTR* w_argv = CommandLineToArgvW(w_command_line, &argc);
if (w_argv == NULL) {
// FIXME: chicken egg problem, can't report errors before sokol_main() is called!
} else {
size_t size = wcslen(w_command_line) * 4;
argv = (char**) _sapp_malloc_clear(((size_t)argc + 1) * sizeof(char*) + size);
SOKOL_ASSERT(argv);
args = (char*) &argv[argc + 1];
int n;
for (int i = 0; i < argc; ++i) {
n = WideCharToMultiByte(CP_UTF8, 0, w_argv[i], -1, args, (int)size, NULL, NULL);
if (n == 0) {
// FIXME: chicken egg problem, can't report errors before sokol_main() is called!
break;
}
argv[i] = args;
size -= (size_t)n;
args += n;
}
LocalFree(w_argv);
}
*o_argc = argc;
return argv;
}
#if !defined(SOKOL_NO_ENTRY)
#if defined(SOKOL_WIN32_FORCE_MAIN)
int main(int argc, char* argv[]) {
sapp_desc desc = sokol_main(argc, argv);
_sapp_win32_run(&desc);
return 0;
}
#else
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
_SOKOL_UNUSED(hInstance);
_SOKOL_UNUSED(hPrevInstance);
_SOKOL_UNUSED(lpCmdLine);
_SOKOL_UNUSED(nCmdShow);
int argc_utf8 = 0;
char** argv_utf8 = _sapp_win32_command_line_to_utf8_argv(GetCommandLineW(), &argc_utf8);
sapp_desc desc = sokol_main(argc_utf8, argv_utf8);
_sapp_win32_run(&desc);
_sapp_free(argv_utf8);
return 0;
}
#endif /* SOKOL_WIN32_FORCE_MAIN */
#endif /* SOKOL_NO_ENTRY */
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif /* _SAPP_WIN32 */
// █████ ███ ██ ██████ ██████ ██████ ██ ██████
// ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██ ██ ██ ██ ██████ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ████ ██████ ██ ██ ██████ ██ ██████
//
// >>android
#if defined(_SAPP_ANDROID)
/* android loop thread */
_SOKOL_PRIVATE bool _sapp_android_init_egl(void) {
SOKOL_ASSERT(_sapp.android.display == EGL_NO_DISPLAY);
SOKOL_ASSERT(_sapp.android.context == EGL_NO_CONTEXT);
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (display == EGL_NO_DISPLAY) {
return false;
}
if (eglInitialize(display, NULL, NULL) == EGL_FALSE) {
return false;
}
EGLint alpha_size = _sapp.desc.alpha ? 8 : 0;
const EGLint cfg_attributes[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, alpha_size,
EGL_DEPTH_SIZE, 16,
EGL_STENCIL_SIZE, 0,
EGL_NONE,
};
EGLConfig available_cfgs[32];
EGLint cfg_count;
eglChooseConfig(display, cfg_attributes, available_cfgs, 32, &cfg_count);
SOKOL_ASSERT(cfg_count > 0);
SOKOL_ASSERT(cfg_count <= 32);
/* find config with 8-bit rgb buffer if available, ndk sample does not trust egl spec */
EGLConfig config;
bool exact_cfg_found = false;
for (int i = 0; i < cfg_count; ++i) {
EGLConfig c = available_cfgs[i];
EGLint r, g, b, a, d;
if (eglGetConfigAttrib(display, c, EGL_RED_SIZE, &r) == EGL_TRUE &&
eglGetConfigAttrib(display, c, EGL_GREEN_SIZE, &g) == EGL_TRUE &&
eglGetConfigAttrib(display, c, EGL_BLUE_SIZE, &b) == EGL_TRUE &&
eglGetConfigAttrib(display, c, EGL_ALPHA_SIZE, &a) == EGL_TRUE &&
eglGetConfigAttrib(display, c, EGL_DEPTH_SIZE, &d) == EGL_TRUE &&
r == 8 && g == 8 && b == 8 && (alpha_size == 0 || a == alpha_size) && d == 16) {
exact_cfg_found = true;
config = c;
break;
}
}
if (!exact_cfg_found) {
config = available_cfgs[0];
}
EGLint ctx_attributes[] = {
EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_NONE,
};
EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, ctx_attributes);
if (context == EGL_NO_CONTEXT) {
return false;
}
_sapp.android.config = config;
_sapp.android.display = display;
_sapp.android.context = context;
return true;
}
_SOKOL_PRIVATE void _sapp_android_cleanup_egl(void) {
if (_sapp.android.display != EGL_NO_DISPLAY) {
eglMakeCurrent(_sapp.android.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (_sapp.android.surface != EGL_NO_SURFACE) {
eglDestroySurface(_sapp.android.display, _sapp.android.surface);
_sapp.android.surface = EGL_NO_SURFACE;
}
if (_sapp.android.context != EGL_NO_CONTEXT) {
eglDestroyContext(_sapp.android.display, _sapp.android.context);
_sapp.android.context = EGL_NO_CONTEXT;
}
eglTerminate(_sapp.android.display);
_sapp.android.display = EGL_NO_DISPLAY;
}
}
_SOKOL_PRIVATE bool _sapp_android_init_egl_surface(ANativeWindow* window) {
SOKOL_ASSERT(_sapp.android.display != EGL_NO_DISPLAY);
SOKOL_ASSERT(_sapp.android.context != EGL_NO_CONTEXT);
SOKOL_ASSERT(_sapp.android.surface == EGL_NO_SURFACE);
SOKOL_ASSERT(window);
/* TODO: set window flags */
/* ANativeActivity_setWindowFlags(activity, AWINDOW_FLAG_KEEP_SCREEN_ON, 0); */
/* create egl surface and make it current */
EGLSurface surface = eglCreateWindowSurface(_sapp.android.display, _sapp.android.config, window, NULL);
if (surface == EGL_NO_SURFACE) {
return false;
}
if (eglMakeCurrent(_sapp.android.display, surface, surface, _sapp.android.context) == EGL_FALSE) {
return false;
}
_sapp.android.surface = surface;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer);
return true;
}
_SOKOL_PRIVATE void _sapp_android_cleanup_egl_surface(void) {
if (_sapp.android.display == EGL_NO_DISPLAY) {
return;
}
eglMakeCurrent(_sapp.android.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (_sapp.android.surface != EGL_NO_SURFACE) {
eglDestroySurface(_sapp.android.display, _sapp.android.surface);
_sapp.android.surface = EGL_NO_SURFACE;
}
}
_SOKOL_PRIVATE void _sapp_android_app_event(sapp_event_type type) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE void _sapp_android_update_dimensions(ANativeWindow* window, bool force_update) {
SOKOL_ASSERT(_sapp.android.display != EGL_NO_DISPLAY);
SOKOL_ASSERT(_sapp.android.context != EGL_NO_CONTEXT);
SOKOL_ASSERT(_sapp.android.surface != EGL_NO_SURFACE);
SOKOL_ASSERT(window);
const int32_t win_w = ANativeWindow_getWidth(window);
const int32_t win_h = ANativeWindow_getHeight(window);
SOKOL_ASSERT(win_w >= 0 && win_h >= 0);
const bool win_changed = (win_w != _sapp.window_width) || (win_h != _sapp.window_height);
_sapp.window_width = win_w;
_sapp.window_height = win_h;
if (win_changed || force_update) {
if (!_sapp.desc.high_dpi) {
const int32_t buf_w = win_w / 2;
const int32_t buf_h = win_h / 2;
EGLint format;
EGLBoolean egl_result = eglGetConfigAttrib(_sapp.android.display, _sapp.android.config, EGL_NATIVE_VISUAL_ID, &format);
SOKOL_ASSERT(egl_result == EGL_TRUE); _SOKOL_UNUSED(egl_result);
/* NOTE: calling ANativeWindow_setBuffersGeometry() with the same dimensions
as the ANativeWindow size results in weird display artefacts, that's
why it's only called when the buffer geometry is different from
the window size
*/
int32_t result = ANativeWindow_setBuffersGeometry(window, buf_w, buf_h, format);
SOKOL_ASSERT(result == 0); _SOKOL_UNUSED(result);
}
}
/* query surface size */
EGLint fb_w, fb_h;
EGLBoolean egl_result_w = eglQuerySurface(_sapp.android.display, _sapp.android.surface, EGL_WIDTH, &fb_w);
EGLBoolean egl_result_h = eglQuerySurface(_sapp.android.display, _sapp.android.surface, EGL_HEIGHT, &fb_h);
SOKOL_ASSERT(egl_result_w == EGL_TRUE); _SOKOL_UNUSED(egl_result_w);
SOKOL_ASSERT(egl_result_h == EGL_TRUE); _SOKOL_UNUSED(egl_result_h);
const bool fb_changed = (fb_w != _sapp.framebuffer_width) || (fb_h != _sapp.framebuffer_height);
_sapp.framebuffer_width = fb_w;
_sapp.framebuffer_height = fb_h;
_sapp.dpi_scale = (float)_sapp.framebuffer_width / (float)_sapp.window_width;
if (win_changed || fb_changed || force_update) {
if (!_sapp.first_frame) {
_sapp_android_app_event(SAPP_EVENTTYPE_RESIZED);
}
}
}
_SOKOL_PRIVATE void _sapp_android_cleanup(void) {
if (_sapp.android.surface != EGL_NO_SURFACE) {
/* egl context is bound, cleanup gracefully */
if (_sapp.init_called && !_sapp.cleanup_called) {
_sapp_call_cleanup();
}
}
/* always try to cleanup by destroying egl context */
_sapp_android_cleanup_egl();
}
_SOKOL_PRIVATE void _sapp_android_shutdown(void) {
/* try to cleanup while we still have a surface and can call cleanup_cb() */
_sapp_android_cleanup();
/* request exit */
ANativeActivity_finish(_sapp.android.activity);
}
_SOKOL_PRIVATE void _sapp_android_frame(void) {
SOKOL_ASSERT(_sapp.android.display != EGL_NO_DISPLAY);
SOKOL_ASSERT(_sapp.android.context != EGL_NO_CONTEXT);
SOKOL_ASSERT(_sapp.android.surface != EGL_NO_SURFACE);
_sapp_timing_measure(&_sapp.timing);
_sapp_android_update_dimensions(_sapp.android.current.window, false);
_sapp_frame();
eglSwapBuffers(_sapp.android.display, _sapp.android.surface);
}
_SOKOL_PRIVATE bool _sapp_android_touch_event(const AInputEvent* e) {
if (AInputEvent_getType(e) != AINPUT_EVENT_TYPE_MOTION) {
return false;
}
if (!_sapp_events_enabled()) {
return false;
}
int32_t action_idx = AMotionEvent_getAction(e);
int32_t action = action_idx & AMOTION_EVENT_ACTION_MASK;
sapp_event_type type = SAPP_EVENTTYPE_INVALID;
switch (action) {
case AMOTION_EVENT_ACTION_DOWN:
case AMOTION_EVENT_ACTION_POINTER_DOWN:
type = SAPP_EVENTTYPE_TOUCHES_BEGAN;
break;
case AMOTION_EVENT_ACTION_MOVE:
type = SAPP_EVENTTYPE_TOUCHES_MOVED;
break;
case AMOTION_EVENT_ACTION_UP:
case AMOTION_EVENT_ACTION_POINTER_UP:
type = SAPP_EVENTTYPE_TOUCHES_ENDED;
break;
case AMOTION_EVENT_ACTION_CANCEL:
type = SAPP_EVENTTYPE_TOUCHES_CANCELLED;
break;
default:
break;
}
if (type == SAPP_EVENTTYPE_INVALID) {
return false;
}
int32_t idx = action_idx >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
_sapp_init_event(type);
_sapp.event.num_touches = (int)AMotionEvent_getPointerCount(e);
if (_sapp.event.num_touches > SAPP_MAX_TOUCHPOINTS) {
_sapp.event.num_touches = SAPP_MAX_TOUCHPOINTS;
}
for (int32_t i = 0; i < _sapp.event.num_touches; i++) {
sapp_touchpoint* dst = &_sapp.event.touches[i];
dst->identifier = (uintptr_t)AMotionEvent_getPointerId(e, (size_t)i);
dst->pos_x = (AMotionEvent_getX(e, (size_t)i) / _sapp.window_width) * _sapp.framebuffer_width;
dst->pos_y = (AMotionEvent_getY(e, (size_t)i) / _sapp.window_height) * _sapp.framebuffer_height;
dst->android_tooltype = (sapp_android_tooltype) AMotionEvent_getToolType(e, (size_t)i);
if (action == AMOTION_EVENT_ACTION_POINTER_DOWN ||
action == AMOTION_EVENT_ACTION_POINTER_UP) {
dst->changed = (i == idx);
} else {
dst->changed = true;
}
}
_sapp_call_event(&_sapp.event);
return true;
}
_SOKOL_PRIVATE bool _sapp_android_key_event(const AInputEvent* e) {
if (AInputEvent_getType(e) != AINPUT_EVENT_TYPE_KEY) {
return false;
}
if (AKeyEvent_getKeyCode(e) == AKEYCODE_BACK) {
/* FIXME: this should be hooked into a "really quit?" mechanism
so the app can ask the user for confirmation, this is currently
generally missing in sokol_app.h
*/
_sapp_android_shutdown();
return true;
}
return false;
}
_SOKOL_PRIVATE int _sapp_android_input_cb(int fd, int events, void* data) {
_SOKOL_UNUSED(fd);
_SOKOL_UNUSED(data);
if ((events & ALOOPER_EVENT_INPUT) == 0) {
_SAPP_ERROR(ANDROID_UNSUPPORTED_INPUT_EVENT_INPUT_CB);
return 1;
}
SOKOL_ASSERT(_sapp.android.current.input);
AInputEvent* event = NULL;
while (AInputQueue_getEvent(_sapp.android.current.input, &event) >= 0) {
if (AInputQueue_preDispatchEvent(_sapp.android.current.input, event) != 0) {
continue;
}
int32_t handled = 0;
if (_sapp_android_touch_event(event) || _sapp_android_key_event(event)) {
handled = 1;
}
AInputQueue_finishEvent(_sapp.android.current.input, event, handled);
}
return 1;
}
_SOKOL_PRIVATE int _sapp_android_main_cb(int fd, int events, void* data) {
_SOKOL_UNUSED(data);
if ((events & ALOOPER_EVENT_INPUT) == 0) {
_SAPP_ERROR(ANDROID_UNSUPPORTED_INPUT_EVENT_MAIN_CB);
return 1;
}
_sapp_android_msg_t msg;
if (read(fd, &msg, sizeof(msg)) != sizeof(msg)) {
_SAPP_ERROR(ANDROID_READ_MSG_FAILED);
return 1;
}
pthread_mutex_lock(&_sapp.android.pt.mutex);
switch (msg) {
case _SOKOL_ANDROID_MSG_CREATE:
{
_SAPP_INFO(ANDROID_MSG_CREATE);
SOKOL_ASSERT(!_sapp.valid);
bool result = _sapp_android_init_egl();
SOKOL_ASSERT(result); _SOKOL_UNUSED(result);
_sapp.valid = true;
_sapp.android.has_created = true;
}
break;
case _SOKOL_ANDROID_MSG_RESUME:
_SAPP_INFO(ANDROID_MSG_RESUME);
_sapp.android.has_resumed = true;
_sapp_android_app_event(SAPP_EVENTTYPE_RESUMED);
break;
case _SOKOL_ANDROID_MSG_PAUSE:
_SAPP_INFO(ANDROID_MSG_PAUSE);
_sapp.android.has_resumed = false;
_sapp_android_app_event(SAPP_EVENTTYPE_SUSPENDED);
break;
case _SOKOL_ANDROID_MSG_FOCUS:
_SAPP_INFO(ANDROID_MSG_FOCUS);
_sapp.android.has_focus = true;
break;
case _SOKOL_ANDROID_MSG_NO_FOCUS:
_SAPP_INFO(ANDROID_MSG_NO_FOCUS);
_sapp.android.has_focus = false;
break;
case _SOKOL_ANDROID_MSG_SET_NATIVE_WINDOW:
_SAPP_INFO(ANDROID_MSG_SET_NATIVE_WINDOW);
if (_sapp.android.current.window != _sapp.android.pending.window) {
if (_sapp.android.current.window != NULL) {
_sapp_android_cleanup_egl_surface();
}
if (_sapp.android.pending.window != NULL) {
if (_sapp_android_init_egl_surface(_sapp.android.pending.window)) {
_sapp_android_update_dimensions(_sapp.android.pending.window, true);
} else {
_sapp_android_shutdown();
}
}
}
_sapp.android.current.window = _sapp.android.pending.window;
break;
case _SOKOL_ANDROID_MSG_SET_INPUT_QUEUE:
_SAPP_INFO(ANDROID_MSG_SET_INPUT_QUEUE);
if (_sapp.android.current.input != _sapp.android.pending.input) {
if (_sapp.android.current.input != NULL) {
AInputQueue_detachLooper(_sapp.android.current.input);
}
if (_sapp.android.pending.input != NULL) {
AInputQueue_attachLooper(
_sapp.android.pending.input,
_sapp.android.looper,
ALOOPER_POLL_CALLBACK,
_sapp_android_input_cb,
NULL); /* data */
}
}
_sapp.android.current.input = _sapp.android.pending.input;
break;
case _SOKOL_ANDROID_MSG_DESTROY:
_SAPP_INFO(ANDROID_MSG_DESTROY);
_sapp_android_cleanup();
_sapp.valid = false;
_sapp.android.is_thread_stopping = true;
break;
default:
_SAPP_WARN(ANDROID_UNKNOWN_MSG);
break;
}
pthread_cond_broadcast(&_sapp.android.pt.cond); /* signal "received" */
pthread_mutex_unlock(&_sapp.android.pt.mutex);
return 1;
}
_SOKOL_PRIVATE bool _sapp_android_should_update(void) {
bool is_in_front = _sapp.android.has_resumed && _sapp.android.has_focus;
bool has_surface = _sapp.android.surface != EGL_NO_SURFACE;
return is_in_front && has_surface;
}
_SOKOL_PRIVATE void _sapp_android_show_keyboard(bool shown) {
SOKOL_ASSERT(_sapp.valid);
/* This seems to be broken in the NDK, but there is (a very cumbersome) workaround... */
if (shown) {
ANativeActivity_showSoftInput(_sapp.android.activity, ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED);
} else {
ANativeActivity_hideSoftInput(_sapp.android.activity, ANATIVEACTIVITY_HIDE_SOFT_INPUT_NOT_ALWAYS);
}
}
_SOKOL_PRIVATE void* _sapp_android_loop(void* arg) {
_SOKOL_UNUSED(arg);
_SAPP_INFO(ANDROID_LOOP_THREAD_STARTED);
_sapp.android.looper = ALooper_prepare(0 /* or ALOOPER_PREPARE_ALLOW_NON_CALLBACKS*/);
ALooper_addFd(_sapp.android.looper,
_sapp.android.pt.read_from_main_fd,
ALOOPER_POLL_CALLBACK,
ALOOPER_EVENT_INPUT,
_sapp_android_main_cb,
NULL); /* data */
/* signal start to main thread */
pthread_mutex_lock(&_sapp.android.pt.mutex);
_sapp.android.is_thread_started = true;
pthread_cond_broadcast(&_sapp.android.pt.cond);
pthread_mutex_unlock(&_sapp.android.pt.mutex);
/* main loop */
while (!_sapp.android.is_thread_stopping) {
/* sokol frame */
if (_sapp_android_should_update()) {
_sapp_android_frame();
}
/* process all events (or stop early if app is requested to quit) */
bool process_events = true;
while (process_events && !_sapp.android.is_thread_stopping) {
bool block_until_event = !_sapp.android.is_thread_stopping && !_sapp_android_should_update();
process_events = ALooper_pollOnce(block_until_event ? -1 : 0, NULL, NULL, NULL) == ALOOPER_POLL_CALLBACK;
}
}
/* cleanup thread */
if (_sapp.android.current.input != NULL) {
AInputQueue_detachLooper(_sapp.android.current.input);
}
/* the following causes heap corruption on exit, why??
ALooper_removeFd(_sapp.android.looper, _sapp.android.pt.read_from_main_fd);
ALooper_release(_sapp.android.looper);*/
/* signal "destroyed" */
pthread_mutex_lock(&_sapp.android.pt.mutex);
_sapp.android.is_thread_stopped = true;
pthread_cond_broadcast(&_sapp.android.pt.cond);
pthread_mutex_unlock(&_sapp.android.pt.mutex);
_SAPP_INFO(ANDROID_LOOP_THREAD_DONE);
return NULL;
}
/* android main/ui thread */
_SOKOL_PRIVATE void _sapp_android_msg(_sapp_android_msg_t msg) {
if (write(_sapp.android.pt.write_from_main_fd, &msg, sizeof(msg)) != sizeof(msg)) {
_SAPP_ERROR(ANDROID_WRITE_MSG_FAILED);
}
}
_SOKOL_PRIVATE void _sapp_android_on_start(ANativeActivity* activity) {
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONSTART);
}
_SOKOL_PRIVATE void _sapp_android_on_resume(ANativeActivity* activity) {
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONRESUME);
_sapp_android_msg(_SOKOL_ANDROID_MSG_RESUME);
}
_SOKOL_PRIVATE void* _sapp_android_on_save_instance_state(ANativeActivity* activity, size_t* out_size) {
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONSAVEINSTANCESTATE);
*out_size = 0;
return NULL;
}
_SOKOL_PRIVATE void _sapp_android_on_window_focus_changed(ANativeActivity* activity, int has_focus) {
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONWINDOWFOCUSCHANGED);
if (has_focus) {
_sapp_android_msg(_SOKOL_ANDROID_MSG_FOCUS);
} else {
_sapp_android_msg(_SOKOL_ANDROID_MSG_NO_FOCUS);
}
}
_SOKOL_PRIVATE void _sapp_android_on_pause(ANativeActivity* activity) {
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONPAUSE);
_sapp_android_msg(_SOKOL_ANDROID_MSG_PAUSE);
}
_SOKOL_PRIVATE void _sapp_android_on_stop(ANativeActivity* activity) {
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONSTOP);
}
_SOKOL_PRIVATE void _sapp_android_msg_set_native_window(ANativeWindow* window) {
pthread_mutex_lock(&_sapp.android.pt.mutex);
_sapp.android.pending.window = window;
_sapp_android_msg(_SOKOL_ANDROID_MSG_SET_NATIVE_WINDOW);
while (_sapp.android.current.window != window) {
pthread_cond_wait(&_sapp.android.pt.cond, &_sapp.android.pt.mutex);
}
pthread_mutex_unlock(&_sapp.android.pt.mutex);
}
_SOKOL_PRIVATE void _sapp_android_on_native_window_created(ANativeActivity* activity, ANativeWindow* window) {
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONNATIVEWINDOWCREATED);
_sapp_android_msg_set_native_window(window);
}
_SOKOL_PRIVATE void _sapp_android_on_native_window_destroyed(ANativeActivity* activity, ANativeWindow* window) {
_SOKOL_UNUSED(activity);
_SOKOL_UNUSED(window);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONNATIVEWINDOWDESTROYED);
_sapp_android_msg_set_native_window(NULL);
}
_SOKOL_PRIVATE void _sapp_android_msg_set_input_queue(AInputQueue* input) {
pthread_mutex_lock(&_sapp.android.pt.mutex);
_sapp.android.pending.input = input;
_sapp_android_msg(_SOKOL_ANDROID_MSG_SET_INPUT_QUEUE);
while (_sapp.android.current.input != input) {
pthread_cond_wait(&_sapp.android.pt.cond, &_sapp.android.pt.mutex);
}
pthread_mutex_unlock(&_sapp.android.pt.mutex);
}
_SOKOL_PRIVATE void _sapp_android_on_input_queue_created(ANativeActivity* activity, AInputQueue* queue) {
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONINPUTQUEUECREATED);
_sapp_android_msg_set_input_queue(queue);
}
_SOKOL_PRIVATE void _sapp_android_on_input_queue_destroyed(ANativeActivity* activity, AInputQueue* queue) {
_SOKOL_UNUSED(activity);
_SOKOL_UNUSED(queue);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONINPUTQUEUEDESTROYED);
_sapp_android_msg_set_input_queue(NULL);
}
_SOKOL_PRIVATE void _sapp_android_on_config_changed(ANativeActivity* activity) {
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONCONFIGURATIONCHANGED);
/* see android:configChanges in manifest */
}
_SOKOL_PRIVATE void _sapp_android_on_low_memory(ANativeActivity* activity) {
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONLOWMEMORY);
}
_SOKOL_PRIVATE void _sapp_android_on_destroy(ANativeActivity* activity) {
/*
* For some reason even an empty app using nativeactivity.h will crash (WIN DEATH)
* on my device (Moto X 2nd gen) when the app is removed from the task view
* (TaskStackView: onTaskViewDismissed).
*
* However, if ANativeActivity_finish() is explicitly called from for example
* _sapp_android_on_stop(), the crash disappears. Is this a bug in NativeActivity?
*/
_SOKOL_UNUSED(activity);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONDESTROY);
/* send destroy msg */
pthread_mutex_lock(&_sapp.android.pt.mutex);
_sapp_android_msg(_SOKOL_ANDROID_MSG_DESTROY);
while (!_sapp.android.is_thread_stopped) {
pthread_cond_wait(&_sapp.android.pt.cond, &_sapp.android.pt.mutex);
}
pthread_mutex_unlock(&_sapp.android.pt.mutex);
/* clean up main thread */
pthread_cond_destroy(&_sapp.android.pt.cond);
pthread_mutex_destroy(&_sapp.android.pt.mutex);
close(_sapp.android.pt.read_from_main_fd);
close(_sapp.android.pt.write_from_main_fd);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_DONE);
/* this is a bit naughty, but causes a clean restart of the app (static globals are reset) */
exit(0);
}
JNIEXPORT
void ANativeActivity_onCreate(ANativeActivity* activity, void* saved_state, size_t saved_state_size) {
_SOKOL_UNUSED(saved_state);
_SOKOL_UNUSED(saved_state_size);
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONCREATE);
// the NativeActity pointer needs to be available inside sokol_main()
// (see https://github.com/floooh/sokol/issues/708), however _sapp_init_state()
// will clear the global _sapp_t struct, so we need to initialize the native
// activity pointer twice, once before sokol_main() and once after _sapp_init_state()
_sapp_clear(&_sapp, sizeof(_sapp));
_sapp.android.activity = activity;
sapp_desc desc = sokol_main(0, NULL);
_sapp_init_state(&desc);
_sapp.android.activity = activity;
int pipe_fd[2];
if (pipe(pipe_fd) != 0) {
_SAPP_ERROR(ANDROID_CREATE_THREAD_PIPE_FAILED);
return;
}
_sapp.android.pt.read_from_main_fd = pipe_fd[0];
_sapp.android.pt.write_from_main_fd = pipe_fd[1];
pthread_mutex_init(&_sapp.android.pt.mutex, NULL);
pthread_cond_init(&_sapp.android.pt.cond, NULL);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&_sapp.android.pt.thread, &attr, _sapp_android_loop, 0);
pthread_attr_destroy(&attr);
/* wait until main loop has started */
pthread_mutex_lock(&_sapp.android.pt.mutex);
while (!_sapp.android.is_thread_started) {
pthread_cond_wait(&_sapp.android.pt.cond, &_sapp.android.pt.mutex);
}
pthread_mutex_unlock(&_sapp.android.pt.mutex);
/* send create msg */
pthread_mutex_lock(&_sapp.android.pt.mutex);
_sapp_android_msg(_SOKOL_ANDROID_MSG_CREATE);
while (!_sapp.android.has_created) {
pthread_cond_wait(&_sapp.android.pt.cond, &_sapp.android.pt.mutex);
}
pthread_mutex_unlock(&_sapp.android.pt.mutex);
/* register for callbacks */
activity->callbacks->onStart = _sapp_android_on_start;
activity->callbacks->onResume = _sapp_android_on_resume;
activity->callbacks->onSaveInstanceState = _sapp_android_on_save_instance_state;
activity->callbacks->onWindowFocusChanged = _sapp_android_on_window_focus_changed;
activity->callbacks->onPause = _sapp_android_on_pause;
activity->callbacks->onStop = _sapp_android_on_stop;
activity->callbacks->onDestroy = _sapp_android_on_destroy;
activity->callbacks->onNativeWindowCreated = _sapp_android_on_native_window_created;
/* activity->callbacks->onNativeWindowResized = _sapp_android_on_native_window_resized; */
/* activity->callbacks->onNativeWindowRedrawNeeded = _sapp_android_on_native_window_redraw_needed; */
activity->callbacks->onNativeWindowDestroyed = _sapp_android_on_native_window_destroyed;
activity->callbacks->onInputQueueCreated = _sapp_android_on_input_queue_created;
activity->callbacks->onInputQueueDestroyed = _sapp_android_on_input_queue_destroyed;
/* activity->callbacks->onContentRectChanged = _sapp_android_on_content_rect_changed; */
activity->callbacks->onConfigurationChanged = _sapp_android_on_config_changed;
activity->callbacks->onLowMemory = _sapp_android_on_low_memory;
_SAPP_INFO(ANDROID_NATIVE_ACTIVITY_CREATE_SUCCESS);
/* NOT A BUG: do NOT call sapp_discard_state() */
}
#endif /* _SAPP_ANDROID */
// ██ ██ ███ ██ ██ ██ ██ ██
// ██ ██ ████ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ███
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██ ████ ██████ ██ ██
//
// >>linux
#if defined(_SAPP_LINUX)
/* see GLFW's xkb_unicode.c */
static const struct _sapp_x11_codepair {
uint16_t keysym;
uint16_t ucs;
} _sapp_x11_keysymtab[] = {
{ 0x01a1, 0x0104 },
{ 0x01a2, 0x02d8 },
{ 0x01a3, 0x0141 },
{ 0x01a5, 0x013d },
{ 0x01a6, 0x015a },
{ 0x01a9, 0x0160 },
{ 0x01aa, 0x015e },
{ 0x01ab, 0x0164 },
{ 0x01ac, 0x0179 },
{ 0x01ae, 0x017d },
{ 0x01af, 0x017b },
{ 0x01b1, 0x0105 },
{ 0x01b2, 0x02db },
{ 0x01b3, 0x0142 },
{ 0x01b5, 0x013e },
{ 0x01b6, 0x015b },
{ 0x01b7, 0x02c7 },
{ 0x01b9, 0x0161 },
{ 0x01ba, 0x015f },
{ 0x01bb, 0x0165 },
{ 0x01bc, 0x017a },
{ 0x01bd, 0x02dd },
{ 0x01be, 0x017e },
{ 0x01bf, 0x017c },
{ 0x01c0, 0x0154 },
{ 0x01c3, 0x0102 },
{ 0x01c5, 0x0139 },
{ 0x01c6, 0x0106 },
{ 0x01c8, 0x010c },
{ 0x01ca, 0x0118 },
{ 0x01cc, 0x011a },
{ 0x01cf, 0x010e },
{ 0x01d0, 0x0110 },
{ 0x01d1, 0x0143 },
{ 0x01d2, 0x0147 },
{ 0x01d5, 0x0150 },
{ 0x01d8, 0x0158 },
{ 0x01d9, 0x016e },
{ 0x01db, 0x0170 },
{ 0x01de, 0x0162 },
{ 0x01e0, 0x0155 },
{ 0x01e3, 0x0103 },
{ 0x01e5, 0x013a },
{ 0x01e6, 0x0107 },
{ 0x01e8, 0x010d },
{ 0x01ea, 0x0119 },
{ 0x01ec, 0x011b },
{ 0x01ef, 0x010f },
{ 0x01f0, 0x0111 },
{ 0x01f1, 0x0144 },
{ 0x01f2, 0x0148 },
{ 0x01f5, 0x0151 },
{ 0x01f8, 0x0159 },
{ 0x01f9, 0x016f },
{ 0x01fb, 0x0171 },
{ 0x01fe, 0x0163 },
{ 0x01ff, 0x02d9 },
{ 0x02a1, 0x0126 },
{ 0x02a6, 0x0124 },
{ 0x02a9, 0x0130 },
{ 0x02ab, 0x011e },
{ 0x02ac, 0x0134 },
{ 0x02b1, 0x0127 },
{ 0x02b6, 0x0125 },
{ 0x02b9, 0x0131 },
{ 0x02bb, 0x011f },
{ 0x02bc, 0x0135 },
{ 0x02c5, 0x010a },
{ 0x02c6, 0x0108 },
{ 0x02d5, 0x0120 },
{ 0x02d8, 0x011c },
{ 0x02dd, 0x016c },
{ 0x02de, 0x015c },
{ 0x02e5, 0x010b },
{ 0x02e6, 0x0109 },
{ 0x02f5, 0x0121 },
{ 0x02f8, 0x011d },
{ 0x02fd, 0x016d },
{ 0x02fe, 0x015d },
{ 0x03a2, 0x0138 },
{ 0x03a3, 0x0156 },
{ 0x03a5, 0x0128 },
{ 0x03a6, 0x013b },
{ 0x03aa, 0x0112 },
{ 0x03ab, 0x0122 },
{ 0x03ac, 0x0166 },
{ 0x03b3, 0x0157 },
{ 0x03b5, 0x0129 },
{ 0x03b6, 0x013c },
{ 0x03ba, 0x0113 },
{ 0x03bb, 0x0123 },
{ 0x03bc, 0x0167 },
{ 0x03bd, 0x014a },
{ 0x03bf, 0x014b },
{ 0x03c0, 0x0100 },
{ 0x03c7, 0x012e },
{ 0x03cc, 0x0116 },
{ 0x03cf, 0x012a },
{ 0x03d1, 0x0145 },
{ 0x03d2, 0x014c },
{ 0x03d3, 0x0136 },
{ 0x03d9, 0x0172 },
{ 0x03dd, 0x0168 },
{ 0x03de, 0x016a },
{ 0x03e0, 0x0101 },
{ 0x03e7, 0x012f },
{ 0x03ec, 0x0117 },
{ 0x03ef, 0x012b },
{ 0x03f1, 0x0146 },
{ 0x03f2, 0x014d },
{ 0x03f3, 0x0137 },
{ 0x03f9, 0x0173 },
{ 0x03fd, 0x0169 },
{ 0x03fe, 0x016b },
{ 0x047e, 0x203e },
{ 0x04a1, 0x3002 },
{ 0x04a2, 0x300c },
{ 0x04a3, 0x300d },
{ 0x04a4, 0x3001 },
{ 0x04a5, 0x30fb },
{ 0x04a6, 0x30f2 },
{ 0x04a7, 0x30a1 },
{ 0x04a8, 0x30a3 },
{ 0x04a9, 0x30a5 },
{ 0x04aa, 0x30a7 },
{ 0x04ab, 0x30a9 },
{ 0x04ac, 0x30e3 },
{ 0x04ad, 0x30e5 },
{ 0x04ae, 0x30e7 },
{ 0x04af, 0x30c3 },
{ 0x04b0, 0x30fc },
{ 0x04b1, 0x30a2 },
{ 0x04b2, 0x30a4 },
{ 0x04b3, 0x30a6 },
{ 0x04b4, 0x30a8 },
{ 0x04b5, 0x30aa },
{ 0x04b6, 0x30ab },
{ 0x04b7, 0x30ad },
{ 0x04b8, 0x30af },
{ 0x04b9, 0x30b1 },
{ 0x04ba, 0x30b3 },
{ 0x04bb, 0x30b5 },
{ 0x04bc, 0x30b7 },
{ 0x04bd, 0x30b9 },
{ 0x04be, 0x30bb },
{ 0x04bf, 0x30bd },
{ 0x04c0, 0x30bf },
{ 0x04c1, 0x30c1 },
{ 0x04c2, 0x30c4 },
{ 0x04c3, 0x30c6 },
{ 0x04c4, 0x30c8 },
{ 0x04c5, 0x30ca },
{ 0x04c6, 0x30cb },
{ 0x04c7, 0x30cc },
{ 0x04c8, 0x30cd },
{ 0x04c9, 0x30ce },
{ 0x04ca, 0x30cf },
{ 0x04cb, 0x30d2 },
{ 0x04cc, 0x30d5 },
{ 0x04cd, 0x30d8 },
{ 0x04ce, 0x30db },
{ 0x04cf, 0x30de },
{ 0x04d0, 0x30df },
{ 0x04d1, 0x30e0 },
{ 0x04d2, 0x30e1 },
{ 0x04d3, 0x30e2 },
{ 0x04d4, 0x30e4 },
{ 0x04d5, 0x30e6 },
{ 0x04d6, 0x30e8 },
{ 0x04d7, 0x30e9 },
{ 0x04d8, 0x30ea },
{ 0x04d9, 0x30eb },
{ 0x04da, 0x30ec },
{ 0x04db, 0x30ed },
{ 0x04dc, 0x30ef },
{ 0x04dd, 0x30f3 },
{ 0x04de, 0x309b },
{ 0x04df, 0x309c },
{ 0x05ac, 0x060c },
{ 0x05bb, 0x061b },
{ 0x05bf, 0x061f },
{ 0x05c1, 0x0621 },
{ 0x05c2, 0x0622 },
{ 0x05c3, 0x0623 },
{ 0x05c4, 0x0624 },
{ 0x05c5, 0x0625 },
{ 0x05c6, 0x0626 },
{ 0x05c7, 0x0627 },
{ 0x05c8, 0x0628 },
{ 0x05c9, 0x0629 },
{ 0x05ca, 0x062a },
{ 0x05cb, 0x062b },
{ 0x05cc, 0x062c },
{ 0x05cd, 0x062d },
{ 0x05ce, 0x062e },
{ 0x05cf, 0x062f },
{ 0x05d0, 0x0630 },
{ 0x05d1, 0x0631 },
{ 0x05d2, 0x0632 },
{ 0x05d3, 0x0633 },
{ 0x05d4, 0x0634 },
{ 0x05d5, 0x0635 },
{ 0x05d6, 0x0636 },
{ 0x05d7, 0x0637 },
{ 0x05d8, 0x0638 },
{ 0x05d9, 0x0639 },
{ 0x05da, 0x063a },
{ 0x05e0, 0x0640 },
{ 0x05e1, 0x0641 },
{ 0x05e2, 0x0642 },
{ 0x05e3, 0x0643 },
{ 0x05e4, 0x0644 },
{ 0x05e5, 0x0645 },
{ 0x05e6, 0x0646 },
{ 0x05e7, 0x0647 },
{ 0x05e8, 0x0648 },
{ 0x05e9, 0x0649 },
{ 0x05ea, 0x064a },
{ 0x05eb, 0x064b },
{ 0x05ec, 0x064c },
{ 0x05ed, 0x064d },
{ 0x05ee, 0x064e },
{ 0x05ef, 0x064f },
{ 0x05f0, 0x0650 },
{ 0x05f1, 0x0651 },
{ 0x05f2, 0x0652 },
{ 0x06a1, 0x0452 },
{ 0x06a2, 0x0453 },
{ 0x06a3, 0x0451 },
{ 0x06a4, 0x0454 },
{ 0x06a5, 0x0455 },
{ 0x06a6, 0x0456 },
{ 0x06a7, 0x0457 },
{ 0x06a8, 0x0458 },
{ 0x06a9, 0x0459 },
{ 0x06aa, 0x045a },
{ 0x06ab, 0x045b },
{ 0x06ac, 0x045c },
{ 0x06ae, 0x045e },
{ 0x06af, 0x045f },
{ 0x06b0, 0x2116 },
{ 0x06b1, 0x0402 },
{ 0x06b2, 0x0403 },
{ 0x06b3, 0x0401 },
{ 0x06b4, 0x0404 },
{ 0x06b5, 0x0405 },
{ 0x06b6, 0x0406 },
{ 0x06b7, 0x0407 },
{ 0x06b8, 0x0408 },
{ 0x06b9, 0x0409 },
{ 0x06ba, 0x040a },
{ 0x06bb, 0x040b },
{ 0x06bc, 0x040c },
{ 0x06be, 0x040e },
{ 0x06bf, 0x040f },
{ 0x06c0, 0x044e },
{ 0x06c1, 0x0430 },
{ 0x06c2, 0x0431 },
{ 0x06c3, 0x0446 },
{ 0x06c4, 0x0434 },
{ 0x06c5, 0x0435 },
{ 0x06c6, 0x0444 },
{ 0x06c7, 0x0433 },
{ 0x06c8, 0x0445 },
{ 0x06c9, 0x0438 },
{ 0x06ca, 0x0439 },
{ 0x06cb, 0x043a },
{ 0x06cc, 0x043b },
{ 0x06cd, 0x043c },
{ 0x06ce, 0x043d },
{ 0x06cf, 0x043e },
{ 0x06d0, 0x043f },
{ 0x06d1, 0x044f },
{ 0x06d2, 0x0440 },
{ 0x06d3, 0x0441 },
{ 0x06d4, 0x0442 },
{ 0x06d5, 0x0443 },
{ 0x06d6, 0x0436 },
{ 0x06d7, 0x0432 },
{ 0x06d8, 0x044c },
{ 0x06d9, 0x044b },
{ 0x06da, 0x0437 },
{ 0x06db, 0x0448 },
{ 0x06dc, 0x044d },
{ 0x06dd, 0x0449 },
{ 0x06de, 0x0447 },
{ 0x06df, 0x044a },
{ 0x06e0, 0x042e },
{ 0x06e1, 0x0410 },
{ 0x06e2, 0x0411 },
{ 0x06e3, 0x0426 },
{ 0x06e4, 0x0414 },
{ 0x06e5, 0x0415 },
{ 0x06e6, 0x0424 },
{ 0x06e7, 0x0413 },
{ 0x06e8, 0x0425 },
{ 0x06e9, 0x0418 },
{ 0x06ea, 0x0419 },
{ 0x06eb, 0x041a },
{ 0x06ec, 0x041b },
{ 0x06ed, 0x041c },
{ 0x06ee, 0x041d },
{ 0x06ef, 0x041e },
{ 0x06f0, 0x041f },
{ 0x06f1, 0x042f },
{ 0x06f2, 0x0420 },
{ 0x06f3, 0x0421 },
{ 0x06f4, 0x0422 },
{ 0x06f5, 0x0423 },
{ 0x06f6, 0x0416 },
{ 0x06f7, 0x0412 },
{ 0x06f8, 0x042c },
{ 0x06f9, 0x042b },
{ 0x06fa, 0x0417 },
{ 0x06fb, 0x0428 },
{ 0x06fc, 0x042d },
{ 0x06fd, 0x0429 },
{ 0x06fe, 0x0427 },
{ 0x06ff, 0x042a },
{ 0x07a1, 0x0386 },
{ 0x07a2, 0x0388 },
{ 0x07a3, 0x0389 },
{ 0x07a4, 0x038a },
{ 0x07a5, 0x03aa },
{ 0x07a7, 0x038c },
{ 0x07a8, 0x038e },
{ 0x07a9, 0x03ab },
{ 0x07ab, 0x038f },
{ 0x07ae, 0x0385 },
{ 0x07af, 0x2015 },
{ 0x07b1, 0x03ac },
{ 0x07b2, 0x03ad },
{ 0x07b3, 0x03ae },
{ 0x07b4, 0x03af },
{ 0x07b5, 0x03ca },
{ 0x07b6, 0x0390 },
{ 0x07b7, 0x03cc },
{ 0x07b8, 0x03cd },
{ 0x07b9, 0x03cb },
{ 0x07ba, 0x03b0 },
{ 0x07bb, 0x03ce },
{ 0x07c1, 0x0391 },
{ 0x07c2, 0x0392 },
{ 0x07c3, 0x0393 },
{ 0x07c4, 0x0394 },
{ 0x07c5, 0x0395 },
{ 0x07c6, 0x0396 },
{ 0x07c7, 0x0397 },
{ 0x07c8, 0x0398 },
{ 0x07c9, 0x0399 },
{ 0x07ca, 0x039a },
{ 0x07cb, 0x039b },
{ 0x07cc, 0x039c },
{ 0x07cd, 0x039d },
{ 0x07ce, 0x039e },
{ 0x07cf, 0x039f },
{ 0x07d0, 0x03a0 },
{ 0x07d1, 0x03a1 },
{ 0x07d2, 0x03a3 },
{ 0x07d4, 0x03a4 },
{ 0x07d5, 0x03a5 },
{ 0x07d6, 0x03a6 },
{ 0x07d7, 0x03a7 },
{ 0x07d8, 0x03a8 },
{ 0x07d9, 0x03a9 },
{ 0x07e1, 0x03b1 },
{ 0x07e2, 0x03b2 },
{ 0x07e3, 0x03b3 },
{ 0x07e4, 0x03b4 },
{ 0x07e5, 0x03b5 },
{ 0x07e6, 0x03b6 },
{ 0x07e7, 0x03b7 },
{ 0x07e8, 0x03b8 },
{ 0x07e9, 0x03b9 },
{ 0x07ea, 0x03ba },
{ 0x07eb, 0x03bb },
{ 0x07ec, 0x03bc },
{ 0x07ed, 0x03bd },
{ 0x07ee, 0x03be },
{ 0x07ef, 0x03bf },
{ 0x07f0, 0x03c0 },
{ 0x07f1, 0x03c1 },
{ 0x07f2, 0x03c3 },
{ 0x07f3, 0x03c2 },
{ 0x07f4, 0x03c4 },
{ 0x07f5, 0x03c5 },
{ 0x07f6, 0x03c6 },
{ 0x07f7, 0x03c7 },
{ 0x07f8, 0x03c8 },
{ 0x07f9, 0x03c9 },
{ 0x08a1, 0x23b7 },
{ 0x08a2, 0x250c },
{ 0x08a3, 0x2500 },
{ 0x08a4, 0x2320 },
{ 0x08a5, 0x2321 },
{ 0x08a6, 0x2502 },
{ 0x08a7, 0x23a1 },
{ 0x08a8, 0x23a3 },
{ 0x08a9, 0x23a4 },
{ 0x08aa, 0x23a6 },
{ 0x08ab, 0x239b },
{ 0x08ac, 0x239d },
{ 0x08ad, 0x239e },
{ 0x08ae, 0x23a0 },
{ 0x08af, 0x23a8 },
{ 0x08b0, 0x23ac },
{ 0x08bc, 0x2264 },
{ 0x08bd, 0x2260 },
{ 0x08be, 0x2265 },
{ 0x08bf, 0x222b },
{ 0x08c0, 0x2234 },
{ 0x08c1, 0x221d },
{ 0x08c2, 0x221e },
{ 0x08c5, 0x2207 },
{ 0x08c8, 0x223c },
{ 0x08c9, 0x2243 },
{ 0x08cd, 0x21d4 },
{ 0x08ce, 0x21d2 },
{ 0x08cf, 0x2261 },
{ 0x08d6, 0x221a },
{ 0x08da, 0x2282 },
{ 0x08db, 0x2283 },
{ 0x08dc, 0x2229 },
{ 0x08dd, 0x222a },
{ 0x08de, 0x2227 },
{ 0x08df, 0x2228 },
{ 0x08ef, 0x2202 },
{ 0x08f6, 0x0192 },
{ 0x08fb, 0x2190 },
{ 0x08fc, 0x2191 },
{ 0x08fd, 0x2192 },
{ 0x08fe, 0x2193 },
{ 0x09e0, 0x25c6 },
{ 0x09e1, 0x2592 },
{ 0x09e2, 0x2409 },
{ 0x09e3, 0x240c },
{ 0x09e4, 0x240d },
{ 0x09e5, 0x240a },
{ 0x09e8, 0x2424 },
{ 0x09e9, 0x240b },
{ 0x09ea, 0x2518 },
{ 0x09eb, 0x2510 },
{ 0x09ec, 0x250c },
{ 0x09ed, 0x2514 },
{ 0x09ee, 0x253c },
{ 0x09ef, 0x23ba },
{ 0x09f0, 0x23bb },
{ 0x09f1, 0x2500 },
{ 0x09f2, 0x23bc },
{ 0x09f3, 0x23bd },
{ 0x09f4, 0x251c },
{ 0x09f5, 0x2524 },
{ 0x09f6, 0x2534 },
{ 0x09f7, 0x252c },
{ 0x09f8, 0x2502 },
{ 0x0aa1, 0x2003 },
{ 0x0aa2, 0x2002 },
{ 0x0aa3, 0x2004 },
{ 0x0aa4, 0x2005 },
{ 0x0aa5, 0x2007 },
{ 0x0aa6, 0x2008 },
{ 0x0aa7, 0x2009 },
{ 0x0aa8, 0x200a },
{ 0x0aa9, 0x2014 },
{ 0x0aaa, 0x2013 },
{ 0x0aae, 0x2026 },
{ 0x0aaf, 0x2025 },
{ 0x0ab0, 0x2153 },
{ 0x0ab1, 0x2154 },
{ 0x0ab2, 0x2155 },
{ 0x0ab3, 0x2156 },
{ 0x0ab4, 0x2157 },
{ 0x0ab5, 0x2158 },
{ 0x0ab6, 0x2159 },
{ 0x0ab7, 0x215a },
{ 0x0ab8, 0x2105 },
{ 0x0abb, 0x2012 },
{ 0x0abc, 0x2329 },
{ 0x0abe, 0x232a },
{ 0x0ac3, 0x215b },
{ 0x0ac4, 0x215c },
{ 0x0ac5, 0x215d },
{ 0x0ac6, 0x215e },
{ 0x0ac9, 0x2122 },
{ 0x0aca, 0x2613 },
{ 0x0acc, 0x25c1 },
{ 0x0acd, 0x25b7 },
{ 0x0ace, 0x25cb },
{ 0x0acf, 0x25af },
{ 0x0ad0, 0x2018 },
{ 0x0ad1, 0x2019 },
{ 0x0ad2, 0x201c },
{ 0x0ad3, 0x201d },
{ 0x0ad4, 0x211e },
{ 0x0ad6, 0x2032 },
{ 0x0ad7, 0x2033 },
{ 0x0ad9, 0x271d },
{ 0x0adb, 0x25ac },
{ 0x0adc, 0x25c0 },
{ 0x0add, 0x25b6 },
{ 0x0ade, 0x25cf },
{ 0x0adf, 0x25ae },
{ 0x0ae0, 0x25e6 },
{ 0x0ae1, 0x25ab },
{ 0x0ae2, 0x25ad },
{ 0x0ae3, 0x25b3 },
{ 0x0ae4, 0x25bd },
{ 0x0ae5, 0x2606 },
{ 0x0ae6, 0x2022 },
{ 0x0ae7, 0x25aa },
{ 0x0ae8, 0x25b2 },
{ 0x0ae9, 0x25bc },
{ 0x0aea, 0x261c },
{ 0x0aeb, 0x261e },
{ 0x0aec, 0x2663 },
{ 0x0aed, 0x2666 },
{ 0x0aee, 0x2665 },
{ 0x0af0, 0x2720 },
{ 0x0af1, 0x2020 },
{ 0x0af2, 0x2021 },
{ 0x0af3, 0x2713 },
{ 0x0af4, 0x2717 },
{ 0x0af5, 0x266f },
{ 0x0af6, 0x266d },
{ 0x0af7, 0x2642 },
{ 0x0af8, 0x2640 },
{ 0x0af9, 0x260e },
{ 0x0afa, 0x2315 },
{ 0x0afb, 0x2117 },
{ 0x0afc, 0x2038 },
{ 0x0afd, 0x201a },
{ 0x0afe, 0x201e },
{ 0x0ba3, 0x003c },
{ 0x0ba6, 0x003e },
{ 0x0ba8, 0x2228 },
{ 0x0ba9, 0x2227 },
{ 0x0bc0, 0x00af },
{ 0x0bc2, 0x22a5 },
{ 0x0bc3, 0x2229 },
{ 0x0bc4, 0x230a },
{ 0x0bc6, 0x005f },
{ 0x0bca, 0x2218 },
{ 0x0bcc, 0x2395 },
{ 0x0bce, 0x22a4 },
{ 0x0bcf, 0x25cb },
{ 0x0bd3, 0x2308 },
{ 0x0bd6, 0x222a },
{ 0x0bd8, 0x2283 },
{ 0x0bda, 0x2282 },
{ 0x0bdc, 0x22a2 },
{ 0x0bfc, 0x22a3 },
{ 0x0cdf, 0x2017 },
{ 0x0ce0, 0x05d0 },
{ 0x0ce1, 0x05d1 },
{ 0x0ce2, 0x05d2 },
{ 0x0ce3, 0x05d3 },
{ 0x0ce4, 0x05d4 },
{ 0x0ce5, 0x05d5 },
{ 0x0ce6, 0x05d6 },
{ 0x0ce7, 0x05d7 },
{ 0x0ce8, 0x05d8 },
{ 0x0ce9, 0x05d9 },
{ 0x0cea, 0x05da },
{ 0x0ceb, 0x05db },
{ 0x0cec, 0x05dc },
{ 0x0ced, 0x05dd },
{ 0x0cee, 0x05de },
{ 0x0cef, 0x05df },
{ 0x0cf0, 0x05e0 },
{ 0x0cf1, 0x05e1 },
{ 0x0cf2, 0x05e2 },
{ 0x0cf3, 0x05e3 },
{ 0x0cf4, 0x05e4 },
{ 0x0cf5, 0x05e5 },
{ 0x0cf6, 0x05e6 },
{ 0x0cf7, 0x05e7 },
{ 0x0cf8, 0x05e8 },
{ 0x0cf9, 0x05e9 },
{ 0x0cfa, 0x05ea },
{ 0x0da1, 0x0e01 },
{ 0x0da2, 0x0e02 },
{ 0x0da3, 0x0e03 },
{ 0x0da4, 0x0e04 },
{ 0x0da5, 0x0e05 },
{ 0x0da6, 0x0e06 },
{ 0x0da7, 0x0e07 },
{ 0x0da8, 0x0e08 },
{ 0x0da9, 0x0e09 },
{ 0x0daa, 0x0e0a },
{ 0x0dab, 0x0e0b },
{ 0x0dac, 0x0e0c },
{ 0x0dad, 0x0e0d },
{ 0x0dae, 0x0e0e },
{ 0x0daf, 0x0e0f },
{ 0x0db0, 0x0e10 },
{ 0x0db1, 0x0e11 },
{ 0x0db2, 0x0e12 },
{ 0x0db3, 0x0e13 },
{ 0x0db4, 0x0e14 },
{ 0x0db5, 0x0e15 },
{ 0x0db6, 0x0e16 },
{ 0x0db7, 0x0e17 },
{ 0x0db8, 0x0e18 },
{ 0x0db9, 0x0e19 },
{ 0x0dba, 0x0e1a },
{ 0x0dbb, 0x0e1b },
{ 0x0dbc, 0x0e1c },
{ 0x0dbd, 0x0e1d },
{ 0x0dbe, 0x0e1e },
{ 0x0dbf, 0x0e1f },
{ 0x0dc0, 0x0e20 },
{ 0x0dc1, 0x0e21 },
{ 0x0dc2, 0x0e22 },
{ 0x0dc3, 0x0e23 },
{ 0x0dc4, 0x0e24 },
{ 0x0dc5, 0x0e25 },
{ 0x0dc6, 0x0e26 },
{ 0x0dc7, 0x0e27 },
{ 0x0dc8, 0x0e28 },
{ 0x0dc9, 0x0e29 },
{ 0x0dca, 0x0e2a },
{ 0x0dcb, 0x0e2b },
{ 0x0dcc, 0x0e2c },
{ 0x0dcd, 0x0e2d },
{ 0x0dce, 0x0e2e },
{ 0x0dcf, 0x0e2f },
{ 0x0dd0, 0x0e30 },
{ 0x0dd1, 0x0e31 },
{ 0x0dd2, 0x0e32 },
{ 0x0dd3, 0x0e33 },
{ 0x0dd4, 0x0e34 },
{ 0x0dd5, 0x0e35 },
{ 0x0dd6, 0x0e36 },
{ 0x0dd7, 0x0e37 },
{ 0x0dd8, 0x0e38 },
{ 0x0dd9, 0x0e39 },
{ 0x0dda, 0x0e3a },
{ 0x0ddf, 0x0e3f },
{ 0x0de0, 0x0e40 },
{ 0x0de1, 0x0e41 },
{ 0x0de2, 0x0e42 },
{ 0x0de3, 0x0e43 },
{ 0x0de4, 0x0e44 },
{ 0x0de5, 0x0e45 },
{ 0x0de6, 0x0e46 },
{ 0x0de7, 0x0e47 },
{ 0x0de8, 0x0e48 },
{ 0x0de9, 0x0e49 },
{ 0x0dea, 0x0e4a },
{ 0x0deb, 0x0e4b },
{ 0x0dec, 0x0e4c },
{ 0x0ded, 0x0e4d },
{ 0x0df0, 0x0e50 },
{ 0x0df1, 0x0e51 },
{ 0x0df2, 0x0e52 },
{ 0x0df3, 0x0e53 },
{ 0x0df4, 0x0e54 },
{ 0x0df5, 0x0e55 },
{ 0x0df6, 0x0e56 },
{ 0x0df7, 0x0e57 },
{ 0x0df8, 0x0e58 },
{ 0x0df9, 0x0e59 },
{ 0x0ea1, 0x3131 },
{ 0x0ea2, 0x3132 },
{ 0x0ea3, 0x3133 },
{ 0x0ea4, 0x3134 },
{ 0x0ea5, 0x3135 },
{ 0x0ea6, 0x3136 },
{ 0x0ea7, 0x3137 },
{ 0x0ea8, 0x3138 },
{ 0x0ea9, 0x3139 },
{ 0x0eaa, 0x313a },
{ 0x0eab, 0x313b },
{ 0x0eac, 0x313c },
{ 0x0ead, 0x313d },
{ 0x0eae, 0x313e },
{ 0x0eaf, 0x313f },
{ 0x0eb0, 0x3140 },
{ 0x0eb1, 0x3141 },
{ 0x0eb2, 0x3142 },
{ 0x0eb3, 0x3143 },
{ 0x0eb4, 0x3144 },
{ 0x0eb5, 0x3145 },
{ 0x0eb6, 0x3146 },
{ 0x0eb7, 0x3147 },
{ 0x0eb8, 0x3148 },
{ 0x0eb9, 0x3149 },
{ 0x0eba, 0x314a },
{ 0x0ebb, 0x314b },
{ 0x0ebc, 0x314c },
{ 0x0ebd, 0x314d },
{ 0x0ebe, 0x314e },
{ 0x0ebf, 0x314f },
{ 0x0ec0, 0x3150 },
{ 0x0ec1, 0x3151 },
{ 0x0ec2, 0x3152 },
{ 0x0ec3, 0x3153 },
{ 0x0ec4, 0x3154 },
{ 0x0ec5, 0x3155 },
{ 0x0ec6, 0x3156 },
{ 0x0ec7, 0x3157 },
{ 0x0ec8, 0x3158 },
{ 0x0ec9, 0x3159 },
{ 0x0eca, 0x315a },
{ 0x0ecb, 0x315b },
{ 0x0ecc, 0x315c },
{ 0x0ecd, 0x315d },
{ 0x0ece, 0x315e },
{ 0x0ecf, 0x315f },
{ 0x0ed0, 0x3160 },
{ 0x0ed1, 0x3161 },
{ 0x0ed2, 0x3162 },
{ 0x0ed3, 0x3163 },
{ 0x0ed4, 0x11a8 },
{ 0x0ed5, 0x11a9 },
{ 0x0ed6, 0x11aa },
{ 0x0ed7, 0x11ab },
{ 0x0ed8, 0x11ac },
{ 0x0ed9, 0x11ad },
{ 0x0eda, 0x11ae },
{ 0x0edb, 0x11af },
{ 0x0edc, 0x11b0 },
{ 0x0edd, 0x11b1 },
{ 0x0ede, 0x11b2 },
{ 0x0edf, 0x11b3 },
{ 0x0ee0, 0x11b4 },
{ 0x0ee1, 0x11b5 },
{ 0x0ee2, 0x11b6 },
{ 0x0ee3, 0x11b7 },
{ 0x0ee4, 0x11b8 },
{ 0x0ee5, 0x11b9 },
{ 0x0ee6, 0x11ba },
{ 0x0ee7, 0x11bb },
{ 0x0ee8, 0x11bc },
{ 0x0ee9, 0x11bd },
{ 0x0eea, 0x11be },
{ 0x0eeb, 0x11bf },
{ 0x0eec, 0x11c0 },
{ 0x0eed, 0x11c1 },
{ 0x0eee, 0x11c2 },
{ 0x0eef, 0x316d },
{ 0x0ef0, 0x3171 },
{ 0x0ef1, 0x3178 },
{ 0x0ef2, 0x317f },
{ 0x0ef3, 0x3181 },
{ 0x0ef4, 0x3184 },
{ 0x0ef5, 0x3186 },
{ 0x0ef6, 0x318d },
{ 0x0ef7, 0x318e },
{ 0x0ef8, 0x11eb },
{ 0x0ef9, 0x11f0 },
{ 0x0efa, 0x11f9 },
{ 0x0eff, 0x20a9 },
{ 0x13a4, 0x20ac },
{ 0x13bc, 0x0152 },
{ 0x13bd, 0x0153 },
{ 0x13be, 0x0178 },
{ 0x20ac, 0x20ac },
{ 0xfe50, '`' },
{ 0xfe51, 0x00b4 },
{ 0xfe52, '^' },
{ 0xfe53, '~' },
{ 0xfe54, 0x00af },
{ 0xfe55, 0x02d8 },
{ 0xfe56, 0x02d9 },
{ 0xfe57, 0x00a8 },
{ 0xfe58, 0x02da },
{ 0xfe59, 0x02dd },
{ 0xfe5a, 0x02c7 },
{ 0xfe5b, 0x00b8 },
{ 0xfe5c, 0x02db },
{ 0xfe5d, 0x037a },
{ 0xfe5e, 0x309b },
{ 0xfe5f, 0x309c },
{ 0xfe63, '/' },
{ 0xfe64, 0x02bc },
{ 0xfe65, 0x02bd },
{ 0xfe66, 0x02f5 },
{ 0xfe67, 0x02f3 },
{ 0xfe68, 0x02cd },
{ 0xfe69, 0xa788 },
{ 0xfe6a, 0x02f7 },
{ 0xfe6e, ',' },
{ 0xfe6f, 0x00a4 },
{ 0xfe80, 'a' }, /* XK_dead_a */
{ 0xfe81, 'A' }, /* XK_dead_A */
{ 0xfe82, 'e' }, /* XK_dead_e */
{ 0xfe83, 'E' }, /* XK_dead_E */
{ 0xfe84, 'i' }, /* XK_dead_i */
{ 0xfe85, 'I' }, /* XK_dead_I */
{ 0xfe86, 'o' }, /* XK_dead_o */
{ 0xfe87, 'O' }, /* XK_dead_O */
{ 0xfe88, 'u' }, /* XK_dead_u */
{ 0xfe89, 'U' }, /* XK_dead_U */
{ 0xfe8a, 0x0259 },
{ 0xfe8b, 0x018f },
{ 0xfe8c, 0x00b5 },
{ 0xfe90, '_' },
{ 0xfe91, 0x02c8 },
{ 0xfe92, 0x02cc },
{ 0xff80 /*XKB_KEY_KP_Space*/, ' ' },
{ 0xff95 /*XKB_KEY_KP_7*/, 0x0037 },
{ 0xff96 /*XKB_KEY_KP_4*/, 0x0034 },
{ 0xff97 /*XKB_KEY_KP_8*/, 0x0038 },
{ 0xff98 /*XKB_KEY_KP_6*/, 0x0036 },
{ 0xff99 /*XKB_KEY_KP_2*/, 0x0032 },
{ 0xff9a /*XKB_KEY_KP_9*/, 0x0039 },
{ 0xff9b /*XKB_KEY_KP_3*/, 0x0033 },
{ 0xff9c /*XKB_KEY_KP_1*/, 0x0031 },
{ 0xff9d /*XKB_KEY_KP_5*/, 0x0035 },
{ 0xff9e /*XKB_KEY_KP_0*/, 0x0030 },
{ 0xffaa /*XKB_KEY_KP_Multiply*/, '*' },
{ 0xffab /*XKB_KEY_KP_Add*/, '+' },
{ 0xffac /*XKB_KEY_KP_Separator*/, ',' },
{ 0xffad /*XKB_KEY_KP_Subtract*/, '-' },
{ 0xffae /*XKB_KEY_KP_Decimal*/, '.' },
{ 0xffaf /*XKB_KEY_KP_Divide*/, '/' },
{ 0xffb0 /*XKB_KEY_KP_0*/, 0x0030 },
{ 0xffb1 /*XKB_KEY_KP_1*/, 0x0031 },
{ 0xffb2 /*XKB_KEY_KP_2*/, 0x0032 },
{ 0xffb3 /*XKB_KEY_KP_3*/, 0x0033 },
{ 0xffb4 /*XKB_KEY_KP_4*/, 0x0034 },
{ 0xffb5 /*XKB_KEY_KP_5*/, 0x0035 },
{ 0xffb6 /*XKB_KEY_KP_6*/, 0x0036 },
{ 0xffb7 /*XKB_KEY_KP_7*/, 0x0037 },
{ 0xffb8 /*XKB_KEY_KP_8*/, 0x0038 },
{ 0xffb9 /*XKB_KEY_KP_9*/, 0x0039 },
{ 0xffbd /*XKB_KEY_KP_Equal*/, '=' }
};
_SOKOL_PRIVATE int _sapp_x11_error_handler(Display* display, XErrorEvent* event) {
_SOKOL_UNUSED(display);
_sapp.x11.error_code = event->error_code;
return 0;
}
_SOKOL_PRIVATE void _sapp_x11_grab_error_handler(void) {
_sapp.x11.error_code = Success;
XSetErrorHandler(_sapp_x11_error_handler);
}
_SOKOL_PRIVATE void _sapp_x11_release_error_handler(void) {
XSync(_sapp.x11.display, False);
XSetErrorHandler(NULL);
}
_SOKOL_PRIVATE void _sapp_x11_init_extensions(void) {
_sapp.x11.UTF8_STRING = XInternAtom(_sapp.x11.display, "UTF8_STRING", False);
_sapp.x11.WM_PROTOCOLS = XInternAtom(_sapp.x11.display, "WM_PROTOCOLS", False);
_sapp.x11.WM_DELETE_WINDOW = XInternAtom(_sapp.x11.display, "WM_DELETE_WINDOW", False);
_sapp.x11.WM_STATE = XInternAtom(_sapp.x11.display, "WM_STATE", False);
_sapp.x11.NET_WM_NAME = XInternAtom(_sapp.x11.display, "_NET_WM_NAME", False);
_sapp.x11.NET_WM_ICON_NAME = XInternAtom(_sapp.x11.display, "_NET_WM_ICON_NAME", False);
_sapp.x11.NET_WM_ICON = XInternAtom(_sapp.x11.display, "_NET_WM_ICON", False);
_sapp.x11.NET_WM_STATE = XInternAtom(_sapp.x11.display, "_NET_WM_STATE", False);
_sapp.x11.NET_WM_STATE_FULLSCREEN = XInternAtom(_sapp.x11.display, "_NET_WM_STATE_FULLSCREEN", False);
if (_sapp.drop.enabled) {
_sapp.x11.xdnd.XdndAware = XInternAtom(_sapp.x11.display, "XdndAware", False);
_sapp.x11.xdnd.XdndEnter = XInternAtom(_sapp.x11.display, "XdndEnter", False);
_sapp.x11.xdnd.XdndPosition = XInternAtom(_sapp.x11.display, "XdndPosition", False);
_sapp.x11.xdnd.XdndStatus = XInternAtom(_sapp.x11.display, "XdndStatus", False);
_sapp.x11.xdnd.XdndActionCopy = XInternAtom(_sapp.x11.display, "XdndActionCopy", False);
_sapp.x11.xdnd.XdndDrop = XInternAtom(_sapp.x11.display, "XdndDrop", False);
_sapp.x11.xdnd.XdndFinished = XInternAtom(_sapp.x11.display, "XdndFinished", False);
_sapp.x11.xdnd.XdndSelection = XInternAtom(_sapp.x11.display, "XdndSelection", False);
_sapp.x11.xdnd.XdndTypeList = XInternAtom(_sapp.x11.display, "XdndTypeList", False);
_sapp.x11.xdnd.text_uri_list = XInternAtom(_sapp.x11.display, "text/uri-list", False);
}
/* check Xi extension for raw mouse input */
if (XQueryExtension(_sapp.x11.display, "XInputExtension", &_sapp.x11.xi.major_opcode, &_sapp.x11.xi.event_base, &_sapp.x11.xi.error_base)) {
_sapp.x11.xi.major = 2;
_sapp.x11.xi.minor = 0;
if (XIQueryVersion(_sapp.x11.display, &_sapp.x11.xi.major, &_sapp.x11.xi.minor) == Success) {
_sapp.x11.xi.available = true;
}
}
}
// translate the X11 KeySyms for a key to sokol-app key code
// NOTE: this is only used as a fallback, in case the XBK method fails
// it is layout-dependent and will fail partially on most non-US layouts.
//
_SOKOL_PRIVATE sapp_keycode _sapp_x11_translate_keysyms(const KeySym* keysyms, int width) {
if (width > 1) {
switch (keysyms[1]) {
case XK_KP_0: return SAPP_KEYCODE_KP_0;
case XK_KP_1: return SAPP_KEYCODE_KP_1;
case XK_KP_2: return SAPP_KEYCODE_KP_2;
case XK_KP_3: return SAPP_KEYCODE_KP_3;
case XK_KP_4: return SAPP_KEYCODE_KP_4;
case XK_KP_5: return SAPP_KEYCODE_KP_5;
case XK_KP_6: return SAPP_KEYCODE_KP_6;
case XK_KP_7: return SAPP_KEYCODE_KP_7;
case XK_KP_8: return SAPP_KEYCODE_KP_8;
case XK_KP_9: return SAPP_KEYCODE_KP_9;
case XK_KP_Separator:
case XK_KP_Decimal: return SAPP_KEYCODE_KP_DECIMAL;
case XK_KP_Equal: return SAPP_KEYCODE_KP_EQUAL;
case XK_KP_Enter: return SAPP_KEYCODE_KP_ENTER;
default: break;
}
}
switch (keysyms[0]) {
case XK_Escape: return SAPP_KEYCODE_ESCAPE;
case XK_Tab: return SAPP_KEYCODE_TAB;
case XK_Shift_L: return SAPP_KEYCODE_LEFT_SHIFT;
case XK_Shift_R: return SAPP_KEYCODE_RIGHT_SHIFT;
case XK_Control_L: return SAPP_KEYCODE_LEFT_CONTROL;
case XK_Control_R: return SAPP_KEYCODE_RIGHT_CONTROL;
case XK_Meta_L:
case XK_Alt_L: return SAPP_KEYCODE_LEFT_ALT;
case XK_Mode_switch: // Mapped to Alt_R on many keyboards
case XK_ISO_Level3_Shift: // AltGr on at least some machines
case XK_Meta_R:
case XK_Alt_R: return SAPP_KEYCODE_RIGHT_ALT;
case XK_Super_L: return SAPP_KEYCODE_LEFT_SUPER;
case XK_Super_R: return SAPP_KEYCODE_RIGHT_SUPER;
case XK_Menu: return SAPP_KEYCODE_MENU;
case XK_Num_Lock: return SAPP_KEYCODE_NUM_LOCK;
case XK_Caps_Lock: return SAPP_KEYCODE_CAPS_LOCK;
case XK_Print: return SAPP_KEYCODE_PRINT_SCREEN;
case XK_Scroll_Lock: return SAPP_KEYCODE_SCROLL_LOCK;
case XK_Pause: return SAPP_KEYCODE_PAUSE;
case XK_Delete: return SAPP_KEYCODE_DELETE;
case XK_BackSpace: return SAPP_KEYCODE_BACKSPACE;
case XK_Return: return SAPP_KEYCODE_ENTER;
case XK_Home: return SAPP_KEYCODE_HOME;
case XK_End: return SAPP_KEYCODE_END;
case XK_Page_Up: return SAPP_KEYCODE_PAGE_UP;
case XK_Page_Down: return SAPP_KEYCODE_PAGE_DOWN;
case XK_Insert: return SAPP_KEYCODE_INSERT;
case XK_Left: return SAPP_KEYCODE_LEFT;
case XK_Right: return SAPP_KEYCODE_RIGHT;
case XK_Down: return SAPP_KEYCODE_DOWN;
case XK_Up: return SAPP_KEYCODE_UP;
case XK_F1: return SAPP_KEYCODE_F1;
case XK_F2: return SAPP_KEYCODE_F2;
case XK_F3: return SAPP_KEYCODE_F3;
case XK_F4: return SAPP_KEYCODE_F4;
case XK_F5: return SAPP_KEYCODE_F5;
case XK_F6: return SAPP_KEYCODE_F6;
case XK_F7: return SAPP_KEYCODE_F7;
case XK_F8: return SAPP_KEYCODE_F8;
case XK_F9: return SAPP_KEYCODE_F9;
case XK_F10: return SAPP_KEYCODE_F10;
case XK_F11: return SAPP_KEYCODE_F11;
case XK_F12: return SAPP_KEYCODE_F12;
case XK_F13: return SAPP_KEYCODE_F13;
case XK_F14: return SAPP_KEYCODE_F14;
case XK_F15: return SAPP_KEYCODE_F15;
case XK_F16: return SAPP_KEYCODE_F16;
case XK_F17: return SAPP_KEYCODE_F17;
case XK_F18: return SAPP_KEYCODE_F18;
case XK_F19: return SAPP_KEYCODE_F19;
case XK_F20: return SAPP_KEYCODE_F20;
case XK_F21: return SAPP_KEYCODE_F21;
case XK_F22: return SAPP_KEYCODE_F22;
case XK_F23: return SAPP_KEYCODE_F23;
case XK_F24: return SAPP_KEYCODE_F24;
case XK_F25: return SAPP_KEYCODE_F25;
// numeric keypad
case XK_KP_Divide: return SAPP_KEYCODE_KP_DIVIDE;
case XK_KP_Multiply: return SAPP_KEYCODE_KP_MULTIPLY;
case XK_KP_Subtract: return SAPP_KEYCODE_KP_SUBTRACT;
case XK_KP_Add: return SAPP_KEYCODE_KP_ADD;
// these should have been detected in secondary keysym test above!
case XK_KP_Insert: return SAPP_KEYCODE_KP_0;
case XK_KP_End: return SAPP_KEYCODE_KP_1;
case XK_KP_Down: return SAPP_KEYCODE_KP_2;
case XK_KP_Page_Down: return SAPP_KEYCODE_KP_3;
case XK_KP_Left: return SAPP_KEYCODE_KP_4;
case XK_KP_Right: return SAPP_KEYCODE_KP_6;
case XK_KP_Home: return SAPP_KEYCODE_KP_7;
case XK_KP_Up: return SAPP_KEYCODE_KP_8;
case XK_KP_Page_Up: return SAPP_KEYCODE_KP_9;
case XK_KP_Delete: return SAPP_KEYCODE_KP_DECIMAL;
case XK_KP_Equal: return SAPP_KEYCODE_KP_EQUAL;
case XK_KP_Enter: return SAPP_KEYCODE_KP_ENTER;
// last resort: Check for printable keys (should not happen if the XKB
// extension is available). This will give a layout dependent mapping
// (which is wrong, and we may miss some keys, especially on non-US
// keyboards), but it's better than nothing...
case XK_a: return SAPP_KEYCODE_A;
case XK_b: return SAPP_KEYCODE_B;
case XK_c: return SAPP_KEYCODE_C;
case XK_d: return SAPP_KEYCODE_D;
case XK_e: return SAPP_KEYCODE_E;
case XK_f: return SAPP_KEYCODE_F;
case XK_g: return SAPP_KEYCODE_G;
case XK_h: return SAPP_KEYCODE_H;
case XK_i: return SAPP_KEYCODE_I;
case XK_j: return SAPP_KEYCODE_J;
case XK_k: return SAPP_KEYCODE_K;
case XK_l: return SAPP_KEYCODE_L;
case XK_m: return SAPP_KEYCODE_M;
case XK_n: return SAPP_KEYCODE_N;
case XK_o: return SAPP_KEYCODE_O;
case XK_p: return SAPP_KEYCODE_P;
case XK_q: return SAPP_KEYCODE_Q;
case XK_r: return SAPP_KEYCODE_R;
case XK_s: return SAPP_KEYCODE_S;
case XK_t: return SAPP_KEYCODE_T;
case XK_u: return SAPP_KEYCODE_U;
case XK_v: return SAPP_KEYCODE_V;
case XK_w: return SAPP_KEYCODE_W;
case XK_x: return SAPP_KEYCODE_X;
case XK_y: return SAPP_KEYCODE_Y;
case XK_z: return SAPP_KEYCODE_Z;
case XK_1: return SAPP_KEYCODE_1;
case XK_2: return SAPP_KEYCODE_2;
case XK_3: return SAPP_KEYCODE_3;
case XK_4: return SAPP_KEYCODE_4;
case XK_5: return SAPP_KEYCODE_5;
case XK_6: return SAPP_KEYCODE_6;
case XK_7: return SAPP_KEYCODE_7;
case XK_8: return SAPP_KEYCODE_8;
case XK_9: return SAPP_KEYCODE_9;
case XK_0: return SAPP_KEYCODE_0;
case XK_space: return SAPP_KEYCODE_SPACE;
case XK_minus: return SAPP_KEYCODE_MINUS;
case XK_equal: return SAPP_KEYCODE_EQUAL;
case XK_bracketleft: return SAPP_KEYCODE_LEFT_BRACKET;
case XK_bracketright: return SAPP_KEYCODE_RIGHT_BRACKET;
case XK_backslash: return SAPP_KEYCODE_BACKSLASH;
case XK_semicolon: return SAPP_KEYCODE_SEMICOLON;
case XK_apostrophe: return SAPP_KEYCODE_APOSTROPHE;
case XK_grave: return SAPP_KEYCODE_GRAVE_ACCENT;
case XK_comma: return SAPP_KEYCODE_COMMA;
case XK_period: return SAPP_KEYCODE_PERIOD;
case XK_slash: return SAPP_KEYCODE_SLASH;
case XK_less: return SAPP_KEYCODE_WORLD_1; // At least in some layouts...
default: break;
}
// no matching translation was found
return SAPP_KEYCODE_INVALID;
}
// setup dynamic keycode/scancode mapping tables, this is required
// for getting layout-independent keycodes on X11.
//
// see GLFW x11_init.c/createKeyTables()
_SOKOL_PRIVATE void _sapp_x11_init_keytable(void) {
for (int i = 0; i < SAPP_MAX_KEYCODES; i++) {
_sapp.keycodes[i] = SAPP_KEYCODE_INVALID;
}
// use XKB to determine physical key locations independently of the current keyboard layout
XkbDescPtr desc = XkbGetMap(_sapp.x11.display, 0, XkbUseCoreKbd);
SOKOL_ASSERT(desc);
XkbGetNames(_sapp.x11.display, XkbKeyNamesMask | XkbKeyAliasesMask, desc);
const int scancode_min = desc->min_key_code;
const int scancode_max = desc->max_key_code;
const struct { sapp_keycode key; const char* name; } keymap[] = {
{ SAPP_KEYCODE_GRAVE_ACCENT, "TLDE" },
{ SAPP_KEYCODE_1, "AE01" },
{ SAPP_KEYCODE_2, "AE02" },
{ SAPP_KEYCODE_3, "AE03" },
{ SAPP_KEYCODE_4, "AE04" },
{ SAPP_KEYCODE_5, "AE05" },
{ SAPP_KEYCODE_6, "AE06" },
{ SAPP_KEYCODE_7, "AE07" },
{ SAPP_KEYCODE_8, "AE08" },
{ SAPP_KEYCODE_9, "AE09" },
{ SAPP_KEYCODE_0, "AE10" },
{ SAPP_KEYCODE_MINUS, "AE11" },
{ SAPP_KEYCODE_EQUAL, "AE12" },
{ SAPP_KEYCODE_Q, "AD01" },
{ SAPP_KEYCODE_W, "AD02" },
{ SAPP_KEYCODE_E, "AD03" },
{ SAPP_KEYCODE_R, "AD04" },
{ SAPP_KEYCODE_T, "AD05" },
{ SAPP_KEYCODE_Y, "AD06" },
{ SAPP_KEYCODE_U, "AD07" },
{ SAPP_KEYCODE_I, "AD08" },
{ SAPP_KEYCODE_O, "AD09" },
{ SAPP_KEYCODE_P, "AD10" },
{ SAPP_KEYCODE_LEFT_BRACKET, "AD11" },
{ SAPP_KEYCODE_RIGHT_BRACKET, "AD12" },
{ SAPP_KEYCODE_A, "AC01" },
{ SAPP_KEYCODE_S, "AC02" },
{ SAPP_KEYCODE_D, "AC03" },
{ SAPP_KEYCODE_F, "AC04" },
{ SAPP_KEYCODE_G, "AC05" },
{ SAPP_KEYCODE_H, "AC06" },
{ SAPP_KEYCODE_J, "AC07" },
{ SAPP_KEYCODE_K, "AC08" },
{ SAPP_KEYCODE_L, "AC09" },
{ SAPP_KEYCODE_SEMICOLON, "AC10" },
{ SAPP_KEYCODE_APOSTROPHE, "AC11" },
{ SAPP_KEYCODE_Z, "AB01" },
{ SAPP_KEYCODE_X, "AB02" },
{ SAPP_KEYCODE_C, "AB03" },
{ SAPP_KEYCODE_V, "AB04" },
{ SAPP_KEYCODE_B, "AB05" },
{ SAPP_KEYCODE_N, "AB06" },
{ SAPP_KEYCODE_M, "AB07" },
{ SAPP_KEYCODE_COMMA, "AB08" },
{ SAPP_KEYCODE_PERIOD, "AB09" },
{ SAPP_KEYCODE_SLASH, "AB10" },
{ SAPP_KEYCODE_BACKSLASH, "BKSL" },
{ SAPP_KEYCODE_WORLD_1, "LSGT" },
{ SAPP_KEYCODE_SPACE, "SPCE" },
{ SAPP_KEYCODE_ESCAPE, "ESC" },
{ SAPP_KEYCODE_ENTER, "RTRN" },
{ SAPP_KEYCODE_TAB, "TAB" },
{ SAPP_KEYCODE_BACKSPACE, "BKSP" },
{ SAPP_KEYCODE_INSERT, "INS" },
{ SAPP_KEYCODE_DELETE, "DELE" },
{ SAPP_KEYCODE_RIGHT, "RGHT" },
{ SAPP_KEYCODE_LEFT, "LEFT" },
{ SAPP_KEYCODE_DOWN, "DOWN" },
{ SAPP_KEYCODE_UP, "UP" },
{ SAPP_KEYCODE_PAGE_UP, "PGUP" },
{ SAPP_KEYCODE_PAGE_DOWN, "PGDN" },
{ SAPP_KEYCODE_HOME, "HOME" },
{ SAPP_KEYCODE_END, "END" },
{ SAPP_KEYCODE_CAPS_LOCK, "CAPS" },
{ SAPP_KEYCODE_SCROLL_LOCK, "SCLK" },
{ SAPP_KEYCODE_NUM_LOCK, "NMLK" },
{ SAPP_KEYCODE_PRINT_SCREEN, "PRSC" },
{ SAPP_KEYCODE_PAUSE, "PAUS" },
{ SAPP_KEYCODE_F1, "FK01" },
{ SAPP_KEYCODE_F2, "FK02" },
{ SAPP_KEYCODE_F3, "FK03" },
{ SAPP_KEYCODE_F4, "FK04" },
{ SAPP_KEYCODE_F5, "FK05" },
{ SAPP_KEYCODE_F6, "FK06" },
{ SAPP_KEYCODE_F7, "FK07" },
{ SAPP_KEYCODE_F8, "FK08" },
{ SAPP_KEYCODE_F9, "FK09" },
{ SAPP_KEYCODE_F10, "FK10" },
{ SAPP_KEYCODE_F11, "FK11" },
{ SAPP_KEYCODE_F12, "FK12" },
{ SAPP_KEYCODE_F13, "FK13" },
{ SAPP_KEYCODE_F14, "FK14" },
{ SAPP_KEYCODE_F15, "FK15" },
{ SAPP_KEYCODE_F16, "FK16" },
{ SAPP_KEYCODE_F17, "FK17" },
{ SAPP_KEYCODE_F18, "FK18" },
{ SAPP_KEYCODE_F19, "FK19" },
{ SAPP_KEYCODE_F20, "FK20" },
{ SAPP_KEYCODE_F21, "FK21" },
{ SAPP_KEYCODE_F22, "FK22" },
{ SAPP_KEYCODE_F23, "FK23" },
{ SAPP_KEYCODE_F24, "FK24" },
{ SAPP_KEYCODE_F25, "FK25" },
{ SAPP_KEYCODE_KP_0, "KP0" },
{ SAPP_KEYCODE_KP_1, "KP1" },
{ SAPP_KEYCODE_KP_2, "KP2" },
{ SAPP_KEYCODE_KP_3, "KP3" },
{ SAPP_KEYCODE_KP_4, "KP4" },
{ SAPP_KEYCODE_KP_5, "KP5" },
{ SAPP_KEYCODE_KP_6, "KP6" },
{ SAPP_KEYCODE_KP_7, "KP7" },
{ SAPP_KEYCODE_KP_8, "KP8" },
{ SAPP_KEYCODE_KP_9, "KP9" },
{ SAPP_KEYCODE_KP_DECIMAL, "KPDL" },
{ SAPP_KEYCODE_KP_DIVIDE, "KPDV" },
{ SAPP_KEYCODE_KP_MULTIPLY, "KPMU" },
{ SAPP_KEYCODE_KP_SUBTRACT, "KPSU" },
{ SAPP_KEYCODE_KP_ADD, "KPAD" },
{ SAPP_KEYCODE_KP_ENTER, "KPEN" },
{ SAPP_KEYCODE_KP_EQUAL, "KPEQ" },
{ SAPP_KEYCODE_LEFT_SHIFT, "LFSH" },
{ SAPP_KEYCODE_LEFT_CONTROL, "LCTL" },
{ SAPP_KEYCODE_LEFT_ALT, "LALT" },
{ SAPP_KEYCODE_LEFT_SUPER, "LWIN" },
{ SAPP_KEYCODE_RIGHT_SHIFT, "RTSH" },
{ SAPP_KEYCODE_RIGHT_CONTROL, "RCTL" },
{ SAPP_KEYCODE_RIGHT_ALT, "RALT" },
{ SAPP_KEYCODE_RIGHT_ALT, "LVL3" },
{ SAPP_KEYCODE_RIGHT_ALT, "MDSW" },
{ SAPP_KEYCODE_RIGHT_SUPER, "RWIN" },
{ SAPP_KEYCODE_MENU, "MENU" }
};
const int num_keymap_items = (int)(sizeof(keymap) / sizeof(keymap[0]));
// find X11 keycode to sokol-app key code mapping
for (int scancode = scancode_min; scancode <= scancode_max; scancode++) {
sapp_keycode key = SAPP_KEYCODE_INVALID;
for (int i = 0; i < num_keymap_items; i++) {
if (strncmp(desc->names->keys[scancode].name, keymap[i].name, XkbKeyNameLength) == 0) {
key = keymap[i].key;
break;
}
}
// fall back to key aliases in case the key name did not match
for (int i = 0; i < desc->names->num_key_aliases; i++) {
if (key != SAPP_KEYCODE_INVALID) {
break;
}
if (strncmp(desc->names->key_aliases[i].real, desc->names->keys[scancode].name, XkbKeyNameLength) != 0) {
continue;
}
for (int j = 0; j < num_keymap_items; j++) {
if (strncmp(desc->names->key_aliases[i].alias, keymap[i].name, XkbKeyNameLength) == 0) {
key = keymap[i].key;
break;
}
}
}
_sapp.keycodes[scancode] = key;
}
XkbFreeNames(desc, XkbKeyNamesMask, True);
XkbFreeKeyboard(desc, 0, True);
int width = 0;
KeySym* keysyms = XGetKeyboardMapping(_sapp.x11.display, scancode_min, scancode_max - scancode_min + 1, &width);
for (int scancode = scancode_min; scancode <= scancode_max; scancode++) {
// translate untranslated key codes using the traditional X11 KeySym lookups
if (_sapp.keycodes[scancode] == SAPP_KEYCODE_INVALID) {
const size_t base = (size_t)((scancode - scancode_min) * width);
_sapp.keycodes[scancode] = _sapp_x11_translate_keysyms(&keysyms[base], width);
}
}
XFree(keysyms);
}
_SOKOL_PRIVATE void _sapp_x11_query_system_dpi(void) {
/* from GLFW:
NOTE: Default to the display-wide DPI as we don't currently have a policy
for which monitor a window is considered to be on
_sapp.x11.dpi = DisplayWidth(_sapp.x11.display, _sapp.x11.screen) *
25.4f / DisplayWidthMM(_sapp.x11.display, _sapp.x11.screen);
NOTE: Basing the scale on Xft.dpi where available should provide the most
consistent user experience (matches Qt, Gtk, etc), although not
always the most accurate one
*/
bool dpi_ok = false;
char* rms = XResourceManagerString(_sapp.x11.display);
if (rms) {
XrmDatabase db = XrmGetStringDatabase(rms);
if (db) {
XrmValue value;
char* type = NULL;
if (XrmGetResource(db, "Xft.dpi", "Xft.Dpi", &type, &value)) {
if (type && strcmp(type, "String") == 0) {
_sapp.x11.dpi = atof(value.addr);
dpi_ok = true;
}
}
XrmDestroyDatabase(db);
}
}
// fallback if querying DPI had failed: assume the standard DPI 96.0f
if (!dpi_ok) {
_sapp.x11.dpi = 96.0f;
_SAPP_WARN(LINUX_X11_QUERY_SYSTEM_DPI_FAILED);
}
}
#if defined(_SAPP_GLX)
_SOKOL_PRIVATE bool _sapp_glx_has_ext(const char* ext, const char* extensions) {
SOKOL_ASSERT(ext);
const char* start = extensions;
while (true) {
const char* where = strstr(start, ext);
if (!where) {
return false;
}
const char* terminator = where + strlen(ext);
if ((where == start) || (*(where - 1) == ' ')) {
if (*terminator == ' ' || *terminator == '\0') {
break;
}
}
start = terminator;
}
return true;
}
_SOKOL_PRIVATE bool _sapp_glx_extsupported(const char* ext, const char* extensions) {
if (extensions) {
return _sapp_glx_has_ext(ext, extensions);
}
else {
return false;
}
}
_SOKOL_PRIVATE void* _sapp_glx_getprocaddr(const char* procname)
{
if (_sapp.glx.GetProcAddress) {
return (void*) _sapp.glx.GetProcAddress(procname);
}
else if (_sapp.glx.GetProcAddressARB) {
return (void*) _sapp.glx.GetProcAddressARB(procname);
}
else {
return dlsym(_sapp.glx.libgl, procname);
}
}
_SOKOL_PRIVATE void _sapp_glx_init(void) {
const char* sonames[] = { "libGL.so.1", "libGL.so", 0 };
for (int i = 0; sonames[i]; i++) {
_sapp.glx.libgl = dlopen(sonames[i], RTLD_LAZY|RTLD_GLOBAL);
if (_sapp.glx.libgl) {
break;
}
}
if (!_sapp.glx.libgl) {
_SAPP_PANIC(LINUX_GLX_LOAD_LIBGL_FAILED);
}
_sapp.glx.GetFBConfigs = (PFNGLXGETFBCONFIGSPROC) dlsym(_sapp.glx.libgl, "glXGetFBConfigs");
_sapp.glx.GetFBConfigAttrib = (PFNGLXGETFBCONFIGATTRIBPROC) dlsym(_sapp.glx.libgl, "glXGetFBConfigAttrib");
_sapp.glx.GetClientString = (PFNGLXGETCLIENTSTRINGPROC) dlsym(_sapp.glx.libgl, "glXGetClientString");
_sapp.glx.QueryExtension = (PFNGLXQUERYEXTENSIONPROC) dlsym(_sapp.glx.libgl, "glXQueryExtension");
_sapp.glx.QueryVersion = (PFNGLXQUERYVERSIONPROC) dlsym(_sapp.glx.libgl, "glXQueryVersion");
_sapp.glx.DestroyContext = (PFNGLXDESTROYCONTEXTPROC) dlsym(_sapp.glx.libgl, "glXDestroyContext");
_sapp.glx.MakeCurrent = (PFNGLXMAKECURRENTPROC) dlsym(_sapp.glx.libgl, "glXMakeCurrent");
_sapp.glx.SwapBuffers = (PFNGLXSWAPBUFFERSPROC) dlsym(_sapp.glx.libgl, "glXSwapBuffers");
_sapp.glx.QueryExtensionsString = (PFNGLXQUERYEXTENSIONSSTRINGPROC) dlsym(_sapp.glx.libgl, "glXQueryExtensionsString");
_sapp.glx.CreateWindow = (PFNGLXCREATEWINDOWPROC) dlsym(_sapp.glx.libgl, "glXCreateWindow");
_sapp.glx.DestroyWindow = (PFNGLXDESTROYWINDOWPROC) dlsym(_sapp.glx.libgl, "glXDestroyWindow");
_sapp.glx.GetProcAddress = (PFNGLXGETPROCADDRESSPROC) dlsym(_sapp.glx.libgl, "glXGetProcAddress");
_sapp.glx.GetProcAddressARB = (PFNGLXGETPROCADDRESSPROC) dlsym(_sapp.glx.libgl, "glXGetProcAddressARB");
_sapp.glx.GetVisualFromFBConfig = (PFNGLXGETVISUALFROMFBCONFIGPROC) dlsym(_sapp.glx.libgl, "glXGetVisualFromFBConfig");
if (!_sapp.glx.GetFBConfigs ||
!_sapp.glx.GetFBConfigAttrib ||
!_sapp.glx.GetClientString ||
!_sapp.glx.QueryExtension ||
!_sapp.glx.QueryVersion ||
!_sapp.glx.DestroyContext ||
!_sapp.glx.MakeCurrent ||
!_sapp.glx.SwapBuffers ||
!_sapp.glx.QueryExtensionsString ||
!_sapp.glx.CreateWindow ||
!_sapp.glx.DestroyWindow ||
!_sapp.glx.GetProcAddress ||
!_sapp.glx.GetProcAddressARB ||
!_sapp.glx.GetVisualFromFBConfig)
{
_SAPP_PANIC(LINUX_GLX_LOAD_ENTRY_POINTS_FAILED);
}
if (!_sapp.glx.QueryExtension(_sapp.x11.display, &_sapp.glx.error_base, &_sapp.glx.event_base)) {
_SAPP_PANIC(LINUX_GLX_EXTENSION_NOT_FOUND);
}
if (!_sapp.glx.QueryVersion(_sapp.x11.display, &_sapp.glx.major, &_sapp.glx.minor)) {
_SAPP_PANIC(LINUX_GLX_QUERY_VERSION_FAILED);
}
if (_sapp.glx.major == 1 && _sapp.glx.minor < 3) {
_SAPP_PANIC(LINUX_GLX_VERSION_TOO_LOW);
}
const char* exts = _sapp.glx.QueryExtensionsString(_sapp.x11.display, _sapp.x11.screen);
if (_sapp_glx_extsupported("GLX_EXT_swap_control", exts)) {
_sapp.glx.SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC) _sapp_glx_getprocaddr("glXSwapIntervalEXT");
_sapp.glx.EXT_swap_control = 0 != _sapp.glx.SwapIntervalEXT;
}
if (_sapp_glx_extsupported("GLX_MESA_swap_control", exts)) {
_sapp.glx.SwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC) _sapp_glx_getprocaddr("glXSwapIntervalMESA");
_sapp.glx.MESA_swap_control = 0 != _sapp.glx.SwapIntervalMESA;
}
_sapp.glx.ARB_multisample = _sapp_glx_extsupported("GLX_ARB_multisample", exts);
if (_sapp_glx_extsupported("GLX_ARB_create_context", exts)) {
_sapp.glx.CreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC) _sapp_glx_getprocaddr("glXCreateContextAttribsARB");
_sapp.glx.ARB_create_context = 0 != _sapp.glx.CreateContextAttribsARB;
}
_sapp.glx.ARB_create_context_profile = _sapp_glx_extsupported("GLX_ARB_create_context_profile", exts);
}
_SOKOL_PRIVATE int _sapp_glx_attrib(GLXFBConfig fbconfig, int attrib) {
int value;
_sapp.glx.GetFBConfigAttrib(_sapp.x11.display, fbconfig, attrib, &value);
return value;
}
_SOKOL_PRIVATE GLXFBConfig _sapp_glx_choosefbconfig(void) {
GLXFBConfig* native_configs;
_sapp_gl_fbconfig* usable_configs;
const _sapp_gl_fbconfig* closest;
int i, native_count, usable_count;
const char* vendor;
bool trust_window_bit = true;
/* HACK: This is a (hopefully temporary) workaround for Chromium
(VirtualBox GL) not setting the window bit on any GLXFBConfigs
*/
vendor = _sapp.glx.GetClientString(_sapp.x11.display, GLX_VENDOR);
if (vendor && strcmp(vendor, "Chromium") == 0) {
trust_window_bit = false;
}
native_configs = _sapp.glx.GetFBConfigs(_sapp.x11.display, _sapp.x11.screen, &native_count);
if (!native_configs || !native_count) {
_SAPP_PANIC(LINUX_GLX_NO_GLXFBCONFIGS);
}
usable_configs = (_sapp_gl_fbconfig*) _sapp_malloc_clear((size_t)native_count * sizeof(_sapp_gl_fbconfig));
usable_count = 0;
for (i = 0; i < native_count; i++) {
const GLXFBConfig n = native_configs[i];
_sapp_gl_fbconfig* u = usable_configs + usable_count;
_sapp_gl_init_fbconfig(u);
/* Only consider RGBA GLXFBConfigs */
if (0 == (_sapp_glx_attrib(n, GLX_RENDER_TYPE) & GLX_RGBA_BIT)) {
continue;
}
/* Only consider window GLXFBConfigs */
if (0 == (_sapp_glx_attrib(n, GLX_DRAWABLE_TYPE) & GLX_WINDOW_BIT)) {
if (trust_window_bit) {
continue;
}
}
u->red_bits = _sapp_glx_attrib(n, GLX_RED_SIZE);
u->green_bits = _sapp_glx_attrib(n, GLX_GREEN_SIZE);
u->blue_bits = _sapp_glx_attrib(n, GLX_BLUE_SIZE);
u->alpha_bits = _sapp_glx_attrib(n, GLX_ALPHA_SIZE);
u->depth_bits = _sapp_glx_attrib(n, GLX_DEPTH_SIZE);
u->stencil_bits = _sapp_glx_attrib(n, GLX_STENCIL_SIZE);
if (_sapp_glx_attrib(n, GLX_DOUBLEBUFFER)) {
u->doublebuffer = true;
}
if (_sapp.glx.ARB_multisample) {
u->samples = _sapp_glx_attrib(n, GLX_SAMPLES);
}
u->handle = (uintptr_t) n;
usable_count++;
}
_sapp_gl_fbconfig desired;
_sapp_gl_init_fbconfig(&desired);
desired.red_bits = 8;
desired.green_bits = 8;
desired.blue_bits = 8;
desired.alpha_bits = 8;
desired.depth_bits = 24;
desired.stencil_bits = 8;
desired.doublebuffer = true;
desired.samples = _sapp.sample_count > 1 ? _sapp.sample_count : 0;
closest = _sapp_gl_choose_fbconfig(&desired, usable_configs, usable_count);
GLXFBConfig result = 0;
if (closest) {
result = (GLXFBConfig) closest->handle;
}
XFree(native_configs);
_sapp_free(usable_configs);
return result;
}
_SOKOL_PRIVATE void _sapp_glx_choose_visual(Visual** visual, int* depth) {
GLXFBConfig native = _sapp_glx_choosefbconfig();
if (0 == native) {
_SAPP_PANIC(LINUX_GLX_NO_SUITABLE_GLXFBCONFIG);
}
XVisualInfo* result = _sapp.glx.GetVisualFromFBConfig(_sapp.x11.display, native);
if (!result) {
_SAPP_PANIC(LINUX_GLX_GET_VISUAL_FROM_FBCONFIG_FAILED);
}
*visual = result->visual;
*depth = result->depth;
XFree(result);
}
_SOKOL_PRIVATE void _sapp_glx_make_current(void) {
_sapp.glx.MakeCurrent(_sapp.x11.display, _sapp.glx.window, _sapp.glx.ctx);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer);
}
_SOKOL_PRIVATE void _sapp_glx_create_context(void) {
GLXFBConfig native = _sapp_glx_choosefbconfig();
if (0 == native){
_SAPP_PANIC(LINUX_GLX_NO_SUITABLE_GLXFBCONFIG);
}
if (!(_sapp.glx.ARB_create_context && _sapp.glx.ARB_create_context_profile)) {
_SAPP_PANIC(LINUX_GLX_REQUIRED_EXTENSIONS_MISSING);
}
_sapp_x11_grab_error_handler();
const int attribs[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, _sapp.desc.gl_major_version,
GLX_CONTEXT_MINOR_VERSION_ARB, _sapp.desc.gl_minor_version,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
0, 0
};
_sapp.glx.ctx = _sapp.glx.CreateContextAttribsARB(_sapp.x11.display, native, NULL, True, attribs);
if (!_sapp.glx.ctx) {
_SAPP_PANIC(LINUX_GLX_CREATE_CONTEXT_FAILED);
}
_sapp_x11_release_error_handler();
_sapp.glx.window = _sapp.glx.CreateWindow(_sapp.x11.display, native, _sapp.x11.window, NULL);
if (!_sapp.glx.window) {
_SAPP_PANIC(LINUX_GLX_CREATE_WINDOW_FAILED);
}
_sapp_glx_make_current();
}
_SOKOL_PRIVATE void _sapp_glx_destroy_context(void) {
if (_sapp.glx.window) {
_sapp.glx.DestroyWindow(_sapp.x11.display, _sapp.glx.window);
_sapp.glx.window = 0;
}
if (_sapp.glx.ctx) {
_sapp.glx.DestroyContext(_sapp.x11.display, _sapp.glx.ctx);
_sapp.glx.ctx = 0;
}
}
_SOKOL_PRIVATE void _sapp_glx_swap_buffers(void) {
_sapp.glx.SwapBuffers(_sapp.x11.display, _sapp.glx.window);
}
_SOKOL_PRIVATE void _sapp_glx_swapinterval(int interval) {
if (_sapp.glx.EXT_swap_control) {
_sapp.glx.SwapIntervalEXT(_sapp.x11.display, _sapp.glx.window, interval);
}
else if (_sapp.glx.MESA_swap_control) {
_sapp.glx.SwapIntervalMESA(interval);
}
}
#endif /* _SAPP_GLX */
_SOKOL_PRIVATE void _sapp_x11_send_event(Atom type, int a, int b, int c, int d, int e) {
XEvent event;
_sapp_clear(&event, sizeof(event));
event.type = ClientMessage;
event.xclient.window = _sapp.x11.window;
event.xclient.format = 32;
event.xclient.message_type = type;
event.xclient.data.l[0] = a;
event.xclient.data.l[1] = b;
event.xclient.data.l[2] = c;
event.xclient.data.l[3] = d;
event.xclient.data.l[4] = e;
XSendEvent(_sapp.x11.display, _sapp.x11.root,
False,
SubstructureNotifyMask | SubstructureRedirectMask,
&event);
}
_SOKOL_PRIVATE void _sapp_x11_query_window_size(void) {
XWindowAttributes attribs;
XGetWindowAttributes(_sapp.x11.display, _sapp.x11.window, &attribs);
_sapp.window_width = attribs.width;
_sapp.window_height = attribs.height;
_sapp.framebuffer_width = _sapp.window_width;
_sapp.framebuffer_height = _sapp.window_height;
}
_SOKOL_PRIVATE void _sapp_x11_set_fullscreen(bool enable) {
/* NOTE: this function must be called after XMapWindow (which happens in _sapp_x11_show_window()) */
if (_sapp.x11.NET_WM_STATE && _sapp.x11.NET_WM_STATE_FULLSCREEN) {
if (enable) {
const int _NET_WM_STATE_ADD = 1;
_sapp_x11_send_event(_sapp.x11.NET_WM_STATE,
_NET_WM_STATE_ADD,
_sapp.x11.NET_WM_STATE_FULLSCREEN,
0, 1, 0);
}
else {
const int _NET_WM_STATE_REMOVE = 0;
_sapp_x11_send_event(_sapp.x11.NET_WM_STATE,
_NET_WM_STATE_REMOVE,
_sapp.x11.NET_WM_STATE_FULLSCREEN,
0, 1, 0);
}
}
XFlush(_sapp.x11.display);
}
_SOKOL_PRIVATE void _sapp_x11_create_hidden_cursor(void) {
SOKOL_ASSERT(0 == _sapp.x11.hidden_cursor);
const int w = 16;
const int h = 16;
XcursorImage* img = XcursorImageCreate(w, h);
SOKOL_ASSERT(img && (img->width == 16) && (img->height == 16) && img->pixels);
img->xhot = 0;
img->yhot = 0;
const size_t num_bytes = (size_t)(w * h) * sizeof(XcursorPixel);
_sapp_clear(img->pixels, num_bytes);
_sapp.x11.hidden_cursor = XcursorImageLoadCursor(_sapp.x11.display, img);
XcursorImageDestroy(img);
}
_SOKOL_PRIVATE void _sapp_x11_create_standard_cursor(sapp_mouse_cursor cursor, const char* name, const char* theme, int size, uint32_t fallback_native) {
SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM));
SOKOL_ASSERT(_sapp.x11.display);
if (theme) {
XcursorImage* img = XcursorLibraryLoadImage(name, theme, size);
if (img) {
_sapp.x11.cursors[cursor] = XcursorImageLoadCursor(_sapp.x11.display, img);
XcursorImageDestroy(img);
}
}
if (0 == _sapp.x11.cursors[cursor]) {
_sapp.x11.cursors[cursor] = XCreateFontCursor(_sapp.x11.display, fallback_native);
}
}
_SOKOL_PRIVATE void _sapp_x11_create_cursors(void) {
SOKOL_ASSERT(_sapp.x11.display);
const char* cursor_theme = XcursorGetTheme(_sapp.x11.display);
const int size = XcursorGetDefaultSize(_sapp.x11.display);
_sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_ARROW, "default", cursor_theme, size, XC_left_ptr);
_sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_IBEAM, "text", cursor_theme, size, XC_xterm);
_sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_CROSSHAIR, "crosshair", cursor_theme, size, XC_crosshair);
_sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_POINTING_HAND, "pointer", cursor_theme, size, XC_hand2);
_sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_RESIZE_EW, "ew-resize", cursor_theme, size, XC_sb_h_double_arrow);
_sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_RESIZE_NS, "ns-resize", cursor_theme, size, XC_sb_v_double_arrow);
_sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_RESIZE_NWSE, "nwse-resize", cursor_theme, size, 0);
_sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_RESIZE_NESW, "nesw-resize", cursor_theme, size, 0);
_sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_RESIZE_ALL, "all-scroll", cursor_theme, size, XC_fleur);
_sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_NOT_ALLOWED, "no-allowed", cursor_theme, size, 0);
_sapp_x11_create_hidden_cursor();
}
_SOKOL_PRIVATE void _sapp_x11_destroy_cursors(void) {
SOKOL_ASSERT(_sapp.x11.display);
if (_sapp.x11.hidden_cursor) {
XFreeCursor(_sapp.x11.display, _sapp.x11.hidden_cursor);
_sapp.x11.hidden_cursor = 0;
}
for (int i = 0; i < _SAPP_MOUSECURSOR_NUM; i++) {
if (_sapp.x11.cursors[i]) {
XFreeCursor(_sapp.x11.display, _sapp.x11.cursors[i]);
_sapp.x11.cursors[i] = 0;
}
}
}
_SOKOL_PRIVATE void _sapp_x11_toggle_fullscreen(void) {
_sapp.fullscreen = !_sapp.fullscreen;
_sapp_x11_set_fullscreen(_sapp.fullscreen);
_sapp_x11_query_window_size();
}
_SOKOL_PRIVATE void _sapp_x11_update_cursor(sapp_mouse_cursor cursor, bool shown) {
SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM));
if (shown) {
if (_sapp.x11.cursors[cursor]) {
XDefineCursor(_sapp.x11.display, _sapp.x11.window, _sapp.x11.cursors[cursor]);
}
else {
XUndefineCursor(_sapp.x11.display, _sapp.x11.window);
}
}
else {
XDefineCursor(_sapp.x11.display, _sapp.x11.window, _sapp.x11.hidden_cursor);
}
XFlush(_sapp.x11.display);
}
_SOKOL_PRIVATE void _sapp_x11_lock_mouse(bool lock) {
if (lock == _sapp.mouse.locked) {
return;
}
_sapp.mouse.dx = 0.0f;
_sapp.mouse.dy = 0.0f;
_sapp.mouse.locked = lock;
if (_sapp.mouse.locked) {
if (_sapp.x11.xi.available) {
XIEventMask em;
unsigned char mask[XIMaskLen(XI_RawMotion)] = { 0 }; // XIMaskLen is a macro
em.deviceid = XIAllMasterDevices;
em.mask_len = sizeof(mask);
em.mask = mask;
XISetMask(mask, XI_RawMotion);
XISelectEvents(_sapp.x11.display, _sapp.x11.root, &em, 1);
}
XGrabPointer(_sapp.x11.display, // display
_sapp.x11.window, // grab_window
True, // owner_events
ButtonPressMask | ButtonReleaseMask | PointerMotionMask, // event_mask
GrabModeAsync, // pointer_mode
GrabModeAsync, // keyboard_mode
_sapp.x11.window, // confine_to
_sapp.x11.hidden_cursor, // cursor
CurrentTime); // time
}
else {
if (_sapp.x11.xi.available) {
XIEventMask em;
unsigned char mask[] = { 0 };
em.deviceid = XIAllMasterDevices;
em.mask_len = sizeof(mask);
em.mask = mask;
XISelectEvents(_sapp.x11.display, _sapp.x11.root, &em, 1);
}
XWarpPointer(_sapp.x11.display, None, _sapp.x11.window, 0, 0, 0, 0, (int) _sapp.mouse.x, _sapp.mouse.y);
XUngrabPointer(_sapp.x11.display, CurrentTime);
}
XFlush(_sapp.x11.display);
}
_SOKOL_PRIVATE void _sapp_x11_update_window_title(void) {
Xutf8SetWMProperties(_sapp.x11.display,
_sapp.x11.window,
_sapp.window_title, _sapp.window_title,
NULL, 0, NULL, NULL, NULL);
XChangeProperty(_sapp.x11.display, _sapp.x11.window,
_sapp.x11.NET_WM_NAME, _sapp.x11.UTF8_STRING, 8,
PropModeReplace,
(unsigned char*)_sapp.window_title,
strlen(_sapp.window_title));
XChangeProperty(_sapp.x11.display, _sapp.x11.window,
_sapp.x11.NET_WM_ICON_NAME, _sapp.x11.UTF8_STRING, 8,
PropModeReplace,
(unsigned char*)_sapp.window_title,
strlen(_sapp.window_title));
XFlush(_sapp.x11.display);
}
_SOKOL_PRIVATE void _sapp_x11_set_icon(const sapp_icon_desc* icon_desc, int num_images) {
SOKOL_ASSERT((num_images > 0) && (num_images <= SAPP_MAX_ICONIMAGES));
int long_count = 0;
for (int i = 0; i < num_images; i++) {
const sapp_image_desc* img_desc = &icon_desc->images[i];
long_count += 2 + (img_desc->width * img_desc->height);
}
long* icon_data = (long*) _sapp_malloc_clear((size_t)long_count * sizeof(long));
SOKOL_ASSERT(icon_data);
long* dst = icon_data;
for (int img_index = 0; img_index < num_images; img_index++) {
const sapp_image_desc* img_desc = &icon_desc->images[img_index];
const uint8_t* src = (const uint8_t*) img_desc->pixels.ptr;
*dst++ = img_desc->width;
*dst++ = img_desc->height;
const int num_pixels = img_desc->width * img_desc->height;
for (int pixel_index = 0; pixel_index < num_pixels; pixel_index++) {
*dst++ = ((long)(src[pixel_index * 4 + 0]) << 16) |
((long)(src[pixel_index * 4 + 1]) << 8) |
((long)(src[pixel_index * 4 + 2]) << 0) |
((long)(src[pixel_index * 4 + 3]) << 24);
}
}
XChangeProperty(_sapp.x11.display, _sapp.x11.window,
_sapp.x11.NET_WM_ICON,
XA_CARDINAL, 32,
PropModeReplace,
(unsigned char*)icon_data,
long_count);
_sapp_free(icon_data);
XFlush(_sapp.x11.display);
}
_SOKOL_PRIVATE void _sapp_x11_create_window(Visual* visual, int depth) {
_sapp.x11.colormap = XCreateColormap(_sapp.x11.display, _sapp.x11.root, visual, AllocNone);
XSetWindowAttributes wa;
_sapp_clear(&wa, sizeof(wa));
const uint32_t wamask = CWBorderPixel | CWColormap | CWEventMask;
wa.colormap = _sapp.x11.colormap;
wa.border_pixel = 0;
wa.event_mask = StructureNotifyMask | KeyPressMask | KeyReleaseMask |
PointerMotionMask | ButtonPressMask | ButtonReleaseMask |
ExposureMask | FocusChangeMask | VisibilityChangeMask |
EnterWindowMask | LeaveWindowMask | PropertyChangeMask;
int display_width = DisplayWidth(_sapp.x11.display, _sapp.x11.screen);
int display_height = DisplayHeight(_sapp.x11.display, _sapp.x11.screen);
int window_width = _sapp.window_width;
int window_height = _sapp.window_height;
if (0 == window_width) {
window_width = (display_width * 4) / 5;
}
if (0 == window_height) {
window_height = (display_height * 4) / 5;
}
int window_xpos = (display_width - window_width) / 2;
int window_ypos = (display_height - window_height) / 2;
if (window_xpos < 0) {
window_xpos = 0;
}
if (window_ypos < 0) {
window_ypos = 0;
}
_sapp_x11_grab_error_handler();
_sapp.x11.window = XCreateWindow(_sapp.x11.display,
_sapp.x11.root,
window_xpos,
window_ypos,
(uint32_t)window_width,
(uint32_t)window_height,
0, /* border width */
depth, /* color depth */
InputOutput,
visual,
wamask,
&wa);
_sapp_x11_release_error_handler();
if (!_sapp.x11.window) {
_SAPP_PANIC(LINUX_X11_CREATE_WINDOW_FAILED);
}
Atom protocols[] = {
_sapp.x11.WM_DELETE_WINDOW
};
XSetWMProtocols(_sapp.x11.display, _sapp.x11.window, protocols, 1);
XSizeHints* hints = XAllocSizeHints();
hints->flags = (PWinGravity | PPosition | PSize);
hints->win_gravity = StaticGravity;
hints->x = window_xpos;
hints->y = window_ypos;
hints->width = window_width;
hints->height = window_height;
XSetWMNormalHints(_sapp.x11.display, _sapp.x11.window, hints);
XFree(hints);
/* announce support for drag'n'drop */
if (_sapp.drop.enabled) {
const Atom version = _SAPP_X11_XDND_VERSION;
XChangeProperty(_sapp.x11.display, _sapp.x11.window, _sapp.x11.xdnd.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*) &version, 1);
}
_sapp_x11_update_window_title();
_sapp_x11_query_window_size();
}
_SOKOL_PRIVATE void _sapp_x11_destroy_window(void) {
if (_sapp.x11.window) {
XUnmapWindow(_sapp.x11.display, _sapp.x11.window);
XDestroyWindow(_sapp.x11.display, _sapp.x11.window);
_sapp.x11.window = 0;
}
if (_sapp.x11.colormap) {
XFreeColormap(_sapp.x11.display, _sapp.x11.colormap);
_sapp.x11.colormap = 0;
}
XFlush(_sapp.x11.display);
}
_SOKOL_PRIVATE bool _sapp_x11_window_visible(void) {
XWindowAttributes wa;
XGetWindowAttributes(_sapp.x11.display, _sapp.x11.window, &wa);
return wa.map_state == IsViewable;
}
_SOKOL_PRIVATE void _sapp_x11_show_window(void) {
if (!_sapp_x11_window_visible()) {
XMapWindow(_sapp.x11.display, _sapp.x11.window);
XRaiseWindow(_sapp.x11.display, _sapp.x11.window);
XFlush(_sapp.x11.display);
}
}
_SOKOL_PRIVATE void _sapp_x11_hide_window(void) {
XUnmapWindow(_sapp.x11.display, _sapp.x11.window);
XFlush(_sapp.x11.display);
}
_SOKOL_PRIVATE unsigned long _sapp_x11_get_window_property(Window window, Atom property, Atom type, unsigned char** value) {
Atom actualType;
int actualFormat;
unsigned long itemCount, bytesAfter;
XGetWindowProperty(_sapp.x11.display,
window,
property,
0,
LONG_MAX,
False,
type,
&actualType,
&actualFormat,
&itemCount,
&bytesAfter,
value);
return itemCount;
}
_SOKOL_PRIVATE int _sapp_x11_get_window_state(void) {
int result = WithdrawnState;
struct {
CARD32 state;
Window icon;
} *state = NULL;
if (_sapp_x11_get_window_property(_sapp.x11.window, _sapp.x11.WM_STATE, _sapp.x11.WM_STATE, (unsigned char**)&state) >= 2) {
result = (int)state->state;
}
if (state) {
XFree(state);
}
return result;
}
_SOKOL_PRIVATE uint32_t _sapp_x11_key_modifier_bit(sapp_keycode key) {
switch (key) {
case SAPP_KEYCODE_LEFT_SHIFT:
case SAPP_KEYCODE_RIGHT_SHIFT:
return SAPP_MODIFIER_SHIFT;
case SAPP_KEYCODE_LEFT_CONTROL:
case SAPP_KEYCODE_RIGHT_CONTROL:
return SAPP_MODIFIER_CTRL;
case SAPP_KEYCODE_LEFT_ALT:
case SAPP_KEYCODE_RIGHT_ALT:
return SAPP_MODIFIER_ALT;
case SAPP_KEYCODE_LEFT_SUPER:
case SAPP_KEYCODE_RIGHT_SUPER:
return SAPP_MODIFIER_SUPER;
default:
return 0;
}
}
_SOKOL_PRIVATE uint32_t _sapp_x11_button_modifier_bit(sapp_mousebutton btn) {
switch (btn) {
case SAPP_MOUSEBUTTON_LEFT: return SAPP_MODIFIER_LMB;
case SAPP_MOUSEBUTTON_RIGHT: return SAPP_MODIFIER_RMB;
case SAPP_MOUSEBUTTON_MIDDLE: return SAPP_MODIFIER_MMB;
default: return 0;
}
}
_SOKOL_PRIVATE uint32_t _sapp_x11_mods(uint32_t x11_mods) {
uint32_t mods = 0;
if (x11_mods & ShiftMask) {
mods |= SAPP_MODIFIER_SHIFT;
}
if (x11_mods & ControlMask) {
mods |= SAPP_MODIFIER_CTRL;
}
if (x11_mods & Mod1Mask) {
mods |= SAPP_MODIFIER_ALT;
}
if (x11_mods & Mod4Mask) {
mods |= SAPP_MODIFIER_SUPER;
}
if (x11_mods & Button1Mask) {
mods |= SAPP_MODIFIER_LMB;
}
if (x11_mods & Button2Mask) {
mods |= SAPP_MODIFIER_MMB;
}
if (x11_mods & Button3Mask) {
mods |= SAPP_MODIFIER_RMB;
}
return mods;
}
_SOKOL_PRIVATE void _sapp_x11_app_event(sapp_event_type type) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE sapp_mousebutton _sapp_x11_translate_button(const XEvent* event) {
switch (event->xbutton.button) {
case Button1: return SAPP_MOUSEBUTTON_LEFT;
case Button2: return SAPP_MOUSEBUTTON_MIDDLE;
case Button3: return SAPP_MOUSEBUTTON_RIGHT;
default: return SAPP_MOUSEBUTTON_INVALID;
}
}
_SOKOL_PRIVATE void _sapp_x11_mouse_update(int x, int y, bool clear_dxdy) {
if (!_sapp.mouse.locked) {
const float new_x = (float) x;
const float new_y = (float) y;
if (clear_dxdy) {
_sapp.mouse.dx = 0.0f;
_sapp.mouse.dy = 0.0f;
} else if (_sapp.mouse.pos_valid) {
_sapp.mouse.dx = new_x - _sapp.mouse.x;
_sapp.mouse.dy = new_y - _sapp.mouse.y;
}
_sapp.mouse.x = new_x;
_sapp.mouse.y = new_y;
_sapp.mouse.pos_valid = true;
}
}
_SOKOL_PRIVATE void _sapp_x11_mouse_event(sapp_event_type type, sapp_mousebutton btn, uint32_t mods) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
_sapp.event.mouse_button = btn;
_sapp.event.modifiers = mods;
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE void _sapp_x11_scroll_event(float x, float y, uint32_t mods) {
if (_sapp_events_enabled()) {
_sapp_init_event(SAPP_EVENTTYPE_MOUSE_SCROLL);
_sapp.event.modifiers = mods;
_sapp.event.scroll_x = x;
_sapp.event.scroll_y = y;
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE void _sapp_x11_key_event(sapp_event_type type, sapp_keycode key, bool repeat, uint32_t mods) {
if (_sapp_events_enabled()) {
_sapp_init_event(type);
_sapp.event.key_code = key;
_sapp.event.key_repeat = repeat;
_sapp.event.modifiers = mods;
_sapp_call_event(&_sapp.event);
/* check if a CLIPBOARD_PASTED event must be sent too */
if (_sapp.clipboard.enabled &&
(type == SAPP_EVENTTYPE_KEY_DOWN) &&
(_sapp.event.modifiers == SAPP_MODIFIER_CTRL) &&
(_sapp.event.key_code == SAPP_KEYCODE_V))
{
_sapp_init_event(SAPP_EVENTTYPE_CLIPBOARD_PASTED);
_sapp_call_event(&_sapp.event);
}
}
}
_SOKOL_PRIVATE void _sapp_x11_char_event(uint32_t chr, bool repeat, uint32_t mods) {
if (_sapp_events_enabled()) {
_sapp_init_event(SAPP_EVENTTYPE_CHAR);
_sapp.event.char_code = chr;
_sapp.event.key_repeat = repeat;
_sapp.event.modifiers = mods;
_sapp_call_event(&_sapp.event);
}
}
_SOKOL_PRIVATE sapp_keycode _sapp_x11_translate_key(int scancode) {
if ((scancode >= 0) && (scancode < _SAPP_X11_MAX_X11_KEYCODES)) {
return _sapp.keycodes[scancode];
} else {
return SAPP_KEYCODE_INVALID;
}
}
_SOKOL_PRIVATE int32_t _sapp_x11_keysym_to_unicode(KeySym keysym) {
int min = 0;
int max = sizeof(_sapp_x11_keysymtab) / sizeof(struct _sapp_x11_codepair) - 1;
int mid;
/* First check for Latin-1 characters (1:1 mapping) */
if ((keysym >= 0x0020 && keysym <= 0x007e) ||
(keysym >= 0x00a0 && keysym <= 0x00ff))
{
return keysym;
}
/* Also check for directly encoded 24-bit UCS characters */
if ((keysym & 0xff000000) == 0x01000000) {
return keysym & 0x00ffffff;
}
/* Binary search in table */
while (max >= min) {
mid = (min + max) / 2;
if (_sapp_x11_keysymtab[mid].keysym < keysym) {
min = mid + 1;
}
else if (_sapp_x11_keysymtab[mid].keysym > keysym) {
max = mid - 1;
}
else {
return _sapp_x11_keysymtab[mid].ucs;
}
}
/* No matching Unicode value found */
return -1;
}
_SOKOL_PRIVATE bool _sapp_x11_keypress_repeat(int keycode) {
bool repeat = false;
if ((keycode >= 0) && (keycode < _SAPP_X11_MAX_X11_KEYCODES)) {
repeat = _sapp.x11.key_repeat[keycode];
_sapp.x11.key_repeat[keycode] = true;
}
return repeat;
}
_SOKOL_PRIVATE void _sapp_x11_keyrelease_repeat(int keycode) {
if ((keycode >= 0) && (keycode < _SAPP_X11_MAX_X11_KEYCODES)) {
_sapp.x11.key_repeat[keycode] = false;
}
}
_SOKOL_PRIVATE bool _sapp_x11_parse_dropped_files_list(const char* src) {
SOKOL_ASSERT(src);
SOKOL_ASSERT(_sapp.drop.buffer);
_sapp_clear_drop_buffer();
_sapp.drop.num_files = 0;
/*
src is (potentially percent-encoded) string made of one or multiple paths
separated by \r\n, each path starting with 'file://'
*/
bool err = false;
int src_count = 0;
char src_chr = 0;
char* dst_ptr = _sapp.drop.buffer;
const char* dst_end_ptr = dst_ptr + (_sapp.drop.max_path_length - 1); // room for terminating 0
while (0 != (src_chr = *src++)) {
src_count++;
char dst_chr = 0;
/* check leading 'file://' */
if (src_count <= 7) {
if (((src_count == 1) && (src_chr != 'f')) ||
((src_count == 2) && (src_chr != 'i')) ||
((src_count == 3) && (src_chr != 'l')) ||
((src_count == 4) && (src_chr != 'e')) ||
((src_count == 5) && (src_chr != ':')) ||
((src_count == 6) && (src_chr != '/')) ||
((src_count == 7) && (src_chr != '/')))
{
_SAPP_ERROR(LINUX_X11_DROPPED_FILE_URI_WRONG_SCHEME);
err = true;
break;
}
}
else if (src_chr == '\r') {
// skip
}
else if (src_chr == '\n') {
src_count = 0;
_sapp.drop.num_files++;
// too many files is not an error
if (_sapp.drop.num_files >= _sapp.drop.max_files) {
break;
}
dst_ptr = _sapp.drop.buffer + _sapp.drop.num_files * _sapp.drop.max_path_length;
dst_end_ptr = dst_ptr + (_sapp.drop.max_path_length - 1);
}
else if ((src_chr == '%') && src[0] && src[1]) {
// a percent-encoded byte (most likely UTF-8 multibyte sequence)
const char digits[3] = { src[0], src[1], 0 };
src += 2;
dst_chr = (char) strtol(digits, 0, 16);
}
else {
dst_chr = src_chr;
}
if (dst_chr) {
// dst_end_ptr already has adjustment for terminating zero
if (dst_ptr < dst_end_ptr) {
*dst_ptr++ = dst_chr;
}
else {
_SAPP_ERROR(DROPPED_FILE_PATH_TOO_LONG);
err = true;
break;
}
}
}
if (err) {
_sapp_clear_drop_buffer();
_sapp.drop.num_files = 0;
return false;
}
else {
return true;
}
}
_SOKOL_PRIVATE void _sapp_x11_on_genericevent(XEvent* event) {
if (_sapp.mouse.locked && _sapp.x11.xi.available) {
if (event->xcookie.extension == _sapp.x11.xi.major_opcode) {
if (XGetEventData(_sapp.x11.display, &event->xcookie)) {
if (event->xcookie.evtype == XI_RawMotion) {
XIRawEvent* re = (XIRawEvent*) event->xcookie.data;
if (re->valuators.mask_len) {
const double* values = re->raw_values;
if (XIMaskIsSet(re->valuators.mask, 0)) {
_sapp.mouse.dx = (float) *values;
values++;
}
if (XIMaskIsSet(re->valuators.mask, 1)) {
_sapp.mouse.dy = (float) *values;
}
_sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xmotion.state));
}
}
XFreeEventData(_sapp.x11.display, &event->xcookie);
}
}
}
}
_SOKOL_PRIVATE void _sapp_x11_on_focusin(XEvent* event) {
// NOTE: ignoring NotifyGrab and NotifyUngrab is same behaviour as GLFW
if ((event->xfocus.mode != NotifyGrab) && (event->xfocus.mode != NotifyUngrab)) {
_sapp_x11_app_event(SAPP_EVENTTYPE_FOCUSED);
}
}
_SOKOL_PRIVATE void _sapp_x11_on_focusout(XEvent* event) {
// if focus is lost for any reason, and we're in mouse locked mode, disable mouse lock
if (_sapp.mouse.locked) {
_sapp_x11_lock_mouse(false);
}
// NOTE: ignoring NotifyGrab and NotifyUngrab is same behaviour as GLFW
if ((event->xfocus.mode != NotifyGrab) && (event->xfocus.mode != NotifyUngrab)) {
_sapp_x11_app_event(SAPP_EVENTTYPE_UNFOCUSED);
}
}
_SOKOL_PRIVATE void _sapp_x11_on_keypress(XEvent* event) {
int keycode = (int)event->xkey.keycode;
const sapp_keycode key = _sapp_x11_translate_key(keycode);
const bool repeat = _sapp_x11_keypress_repeat(keycode);
uint32_t mods = _sapp_x11_mods(event->xkey.state);
// X11 doesn't set modifier bit on key down, so emulate that
mods |= _sapp_x11_key_modifier_bit(key);
if (key != SAPP_KEYCODE_INVALID) {
_sapp_x11_key_event(SAPP_EVENTTYPE_KEY_DOWN, key, repeat, mods);
}
KeySym keysym;
XLookupString(&event->xkey, NULL, 0, &keysym, NULL);
int32_t chr = _sapp_x11_keysym_to_unicode(keysym);
if (chr > 0) {
_sapp_x11_char_event((uint32_t)chr, repeat, mods);
}
}
_SOKOL_PRIVATE void _sapp_x11_on_keyrelease(XEvent* event) {
int keycode = (int)event->xkey.keycode;
const sapp_keycode key = _sapp_x11_translate_key(keycode);
_sapp_x11_keyrelease_repeat(keycode);
if (key != SAPP_KEYCODE_INVALID) {
uint32_t mods = _sapp_x11_mods(event->xkey.state);
// X11 doesn't clear modifier bit on key up, so emulate that
mods &= ~_sapp_x11_key_modifier_bit(key);
_sapp_x11_key_event(SAPP_EVENTTYPE_KEY_UP, key, false, mods);
}
}
_SOKOL_PRIVATE void _sapp_x11_on_buttonpress(XEvent* event) {
_sapp_x11_mouse_update(event->xbutton.x, event->xbutton.y, false);
const sapp_mousebutton btn = _sapp_x11_translate_button(event);
uint32_t mods = _sapp_x11_mods(event->xbutton.state);
// X11 doesn't set modifier bit on button down, so emulate that
mods |= _sapp_x11_button_modifier_bit(btn);
if (btn != SAPP_MOUSEBUTTON_INVALID) {
_sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, btn, mods);
_sapp.x11.mouse_buttons |= (1 << btn);
}
else {
// might be a scroll event
switch (event->xbutton.button) {
case 4: _sapp_x11_scroll_event(0.0f, 1.0f, mods); break;
case 5: _sapp_x11_scroll_event(0.0f, -1.0f, mods); break;
case 6: _sapp_x11_scroll_event(1.0f, 0.0f, mods); break;
case 7: _sapp_x11_scroll_event(-1.0f, 0.0f, mods); break;
}
}
}
_SOKOL_PRIVATE void _sapp_x11_on_buttonrelease(XEvent* event) {
_sapp_x11_mouse_update(event->xbutton.x, event->xbutton.y, false);
const sapp_mousebutton btn = _sapp_x11_translate_button(event);
if (btn != SAPP_MOUSEBUTTON_INVALID) {
uint32_t mods = _sapp_x11_mods(event->xbutton.state);
// X11 doesn't clear modifier bit on button up, so emulate that
mods &= ~_sapp_x11_button_modifier_bit(btn);
_sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, btn, mods);
_sapp.x11.mouse_buttons &= ~(1 << btn);
}
}
_SOKOL_PRIVATE void _sapp_x11_on_enternotify(XEvent* event) {
// don't send enter/leave events while mouse button held down
if (0 == _sapp.x11.mouse_buttons) {
_sapp_x11_mouse_update(event->xcrossing.x, event->xcrossing.y, true);
_sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_ENTER, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xcrossing.state));
}
}
_SOKOL_PRIVATE void _sapp_x11_on_leavenotify(XEvent* event) {
if (0 == _sapp.x11.mouse_buttons) {
_sapp_x11_mouse_update(event->xcrossing.x, event->xcrossing.y, true);
_sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_LEAVE, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xcrossing.state));
}
}
_SOKOL_PRIVATE void _sapp_x11_on_motionnotify(XEvent* event) {
if (!_sapp.mouse.locked) {
_sapp_x11_mouse_update(event->xmotion.x, event->xmotion.y, false);
_sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xmotion.state));
}
}
_SOKOL_PRIVATE void _sapp_x11_on_configurenotify(XEvent* event) {
if ((event->xconfigure.width != _sapp.window_width) || (event->xconfigure.height != _sapp.window_height)) {
_sapp.window_width = event->xconfigure.width;
_sapp.window_height = event->xconfigure.height;
_sapp.framebuffer_width = _sapp.window_width;
_sapp.framebuffer_height = _sapp.window_height;
_sapp_x11_app_event(SAPP_EVENTTYPE_RESIZED);
}
}
_SOKOL_PRIVATE void _sapp_x11_on_propertynotify(XEvent* event) {
if (event->xproperty.state == PropertyNewValue) {
if (event->xproperty.atom == _sapp.x11.WM_STATE) {
const int state = _sapp_x11_get_window_state();
if (state != _sapp.x11.window_state) {
_sapp.x11.window_state = state;
if (state == IconicState) {
_sapp_x11_app_event(SAPP_EVENTTYPE_ICONIFIED);
}
else if (state == NormalState) {
_sapp_x11_app_event(SAPP_EVENTTYPE_RESTORED);
}
}
}
}
}
_SOKOL_PRIVATE void _sapp_x11_on_selectionnotify(XEvent* event) {
if (event->xselection.property == _sapp.x11.xdnd.XdndSelection) {
char* data = 0;
uint32_t result = _sapp_x11_get_window_property(event->xselection.requestor,
event->xselection.property,
event->xselection.target,
(unsigned char**) &data);
if (_sapp.drop.enabled && result) {
if (_sapp_x11_parse_dropped_files_list(data)) {
_sapp.mouse.dx = 0.0f;
_sapp.mouse.dy = 0.0f;
if (_sapp_events_enabled()) {
// FIXME: Figure out how to get modifier key state here.
// The XSelection event has no 'state' item, and
// XQueryKeymap() always returns a zeroed array.
_sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED);
_sapp_call_event(&_sapp.event);
}
}
}
if (_sapp.x11.xdnd.version >= 2) {
XEvent reply;
_sapp_clear(&reply, sizeof(reply));
reply.type = ClientMessage;
reply.xclient.window = _sapp.x11.xdnd.source;
reply.xclient.message_type = _sapp.x11.xdnd.XdndFinished;
reply.xclient.format = 32;
reply.xclient.data.l[0] = (long)_sapp.x11.window;
reply.xclient.data.l[1] = result;
reply.xclient.data.l[2] = (long)_sapp.x11.xdnd.XdndActionCopy;
XSendEvent(_sapp.x11.display, _sapp.x11.xdnd.source, False, NoEventMask, &reply);
XFlush(_sapp.x11.display);
}
}
}
_SOKOL_PRIVATE void _sapp_x11_on_clientmessage(XEvent* event) {
if (XFilterEvent(event, None)) {
return;
}
if (event->xclient.message_type == _sapp.x11.WM_PROTOCOLS) {
const Atom protocol = (Atom)event->xclient.data.l[0];
if (protocol == _sapp.x11.WM_DELETE_WINDOW) {
_sapp.quit_requested = true;
}
} else if (event->xclient.message_type == _sapp.x11.xdnd.XdndEnter) {
const bool is_list = 0 != (event->xclient.data.l[1] & 1);
_sapp.x11.xdnd.source = (Window)event->xclient.data.l[0];
_sapp.x11.xdnd.version = event->xclient.data.l[1] >> 24;
_sapp.x11.xdnd.format = None;
if (_sapp.x11.xdnd.version > _SAPP_X11_XDND_VERSION) {
return;
}
uint32_t count = 0;
Atom* formats = 0;
if (is_list) {
count = _sapp_x11_get_window_property(_sapp.x11.xdnd.source, _sapp.x11.xdnd.XdndTypeList, XA_ATOM, (unsigned char**)&formats);
} else {
count = 3;
formats = (Atom*) event->xclient.data.l + 2;
}
for (uint32_t i = 0; i < count; i++) {
if (formats[i] == _sapp.x11.xdnd.text_uri_list) {
_sapp.x11.xdnd.format = _sapp.x11.xdnd.text_uri_list;
break;
}
}
if (is_list && formats) {
XFree(formats);
}
} else if (event->xclient.message_type == _sapp.x11.xdnd.XdndDrop) {
if (_sapp.x11.xdnd.version > _SAPP_X11_XDND_VERSION) {
return;
}
Time time = CurrentTime;
if (_sapp.x11.xdnd.format) {
if (_sapp.x11.xdnd.version >= 1) {
time = (Time)event->xclient.data.l[2];
}
XConvertSelection(_sapp.x11.display,
_sapp.x11.xdnd.XdndSelection,
_sapp.x11.xdnd.format,
_sapp.x11.xdnd.XdndSelection,
_sapp.x11.window,
time);
} else if (_sapp.x11.xdnd.version >= 2) {
XEvent reply;
_sapp_clear(&reply, sizeof(reply));
reply.type = ClientMessage;
reply.xclient.window = _sapp.x11.xdnd.source;
reply.xclient.message_type = _sapp.x11.xdnd.XdndFinished;
reply.xclient.format = 32;
reply.xclient.data.l[0] = (long)_sapp.x11.window;
reply.xclient.data.l[1] = 0; // drag was rejected
reply.xclient.data.l[2] = None;
XSendEvent(_sapp.x11.display, _sapp.x11.xdnd.source, False, NoEventMask, &reply);
XFlush(_sapp.x11.display);
}
} else if (event->xclient.message_type == _sapp.x11.xdnd.XdndPosition) {
// drag operation has moved over the window
// FIXME: we could track the mouse position here, but
// this isn't implemented on other platforms either so far
if (_sapp.x11.xdnd.version > _SAPP_X11_XDND_VERSION) {
return;
}
XEvent reply;
_sapp_clear(&reply, sizeof(reply));
reply.type = ClientMessage;
reply.xclient.window = _sapp.x11.xdnd.source;
reply.xclient.message_type = _sapp.x11.xdnd.XdndStatus;
reply.xclient.format = 32;
reply.xclient.data.l[0] = (long)_sapp.x11.window;
if (_sapp.x11.xdnd.format) {
/* reply that we are ready to copy the dragged data */
reply.xclient.data.l[1] = 1; // accept with no rectangle
if (_sapp.x11.xdnd.version >= 2) {
reply.xclient.data.l[4] = (long)_sapp.x11.xdnd.XdndActionCopy;
}
}
XSendEvent(_sapp.x11.display, _sapp.x11.xdnd.source, False, NoEventMask, &reply);
XFlush(_sapp.x11.display);
}
}
_SOKOL_PRIVATE void _sapp_x11_process_event(XEvent* event) {
switch (event->type) {
case GenericEvent:
_sapp_x11_on_genericevent(event);
break;
case FocusIn:
_sapp_x11_on_focusin(event);
break;
case FocusOut:
_sapp_x11_on_focusout(event);
break;
case KeyPress:
_sapp_x11_on_keypress(event);
break;
case KeyRelease:
_sapp_x11_on_keyrelease(event);
break;
case ButtonPress:
_sapp_x11_on_buttonpress(event);
break;
case ButtonRelease:
_sapp_x11_on_buttonrelease(event);
break;
case EnterNotify:
_sapp_x11_on_enternotify(event);
break;
case LeaveNotify:
_sapp_x11_on_leavenotify(event);
break;
case MotionNotify:
_sapp_x11_on_motionnotify(event);
break;
case ConfigureNotify:
_sapp_x11_on_configurenotify(event);
break;
case PropertyNotify:
_sapp_x11_on_propertynotify(event);
break;
case SelectionNotify:
_sapp_x11_on_selectionnotify(event);
break;
case DestroyNotify:
// not a bug
break;
case ClientMessage:
_sapp_x11_on_clientmessage(event);
break;
}
}
#if !defined(_SAPP_GLX)
_SOKOL_PRIVATE void _sapp_egl_init(void) {
#if defined(SOKOL_GLCORE)
if (!eglBindAPI(EGL_OPENGL_API)) {
_SAPP_PANIC(LINUX_EGL_BIND_OPENGL_API_FAILED);
}
#else
if (!eglBindAPI(EGL_OPENGL_ES_API)) {
_SAPP_PANIC(LINUX_EGL_BIND_OPENGL_ES_API_FAILED);
}
#endif
_sapp.egl.display = eglGetDisplay((EGLNativeDisplayType)_sapp.x11.display);
if (EGL_NO_DISPLAY == _sapp.egl.display) {
_SAPP_PANIC(LINUX_EGL_GET_DISPLAY_FAILED);
}
EGLint major, minor;
if (!eglInitialize(_sapp.egl.display, &major, &minor)) {
_SAPP_PANIC(LINUX_EGL_INITIALIZE_FAILED);
}
EGLint sample_count = _sapp.desc.sample_count > 1 ? _sapp.desc.sample_count : 0;
EGLint alpha_size = _sapp.desc.alpha ? 8 : 0;
const EGLint config_attrs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
#if defined(SOKOL_GLCORE)
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
#elif defined(SOKOL_GLES3)
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
#endif
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, alpha_size,
EGL_DEPTH_SIZE, 24,
EGL_STENCIL_SIZE, 8,
EGL_SAMPLE_BUFFERS, _sapp.desc.sample_count > 1 ? 1 : 0,
EGL_SAMPLES, sample_count,
EGL_NONE,
};
EGLConfig egl_configs[32];
EGLint config_count;
if (!eglChooseConfig(_sapp.egl.display, config_attrs, egl_configs, 32, &config_count) || config_count == 0) {
_SAPP_PANIC(LINUX_EGL_NO_CONFIGS);
}
EGLConfig config = egl_configs[0];
for (int i = 0; i < config_count; ++i) {
EGLConfig c = egl_configs[i];
EGLint r, g, b, a, d, s, n;
if (eglGetConfigAttrib(_sapp.egl.display, c, EGL_RED_SIZE, &r) &&
eglGetConfigAttrib(_sapp.egl.display, c, EGL_GREEN_SIZE, &g) &&
eglGetConfigAttrib(_sapp.egl.display, c, EGL_BLUE_SIZE, &b) &&
eglGetConfigAttrib(_sapp.egl.display, c, EGL_ALPHA_SIZE, &a) &&
eglGetConfigAttrib(_sapp.egl.display, c, EGL_DEPTH_SIZE, &d) &&
eglGetConfigAttrib(_sapp.egl.display, c, EGL_STENCIL_SIZE, &s) &&
eglGetConfigAttrib(_sapp.egl.display, c, EGL_SAMPLES, &n) &&
(r == 8) && (g == 8) && (b == 8) && (a == alpha_size) && (d == 24) && (s == 8) && (n == sample_count)) {
config = c;
break;
}
}
EGLint visual_id;
if (!eglGetConfigAttrib(_sapp.egl.display, config, EGL_NATIVE_VISUAL_ID, &visual_id)) {
_SAPP_PANIC(LINUX_EGL_NO_NATIVE_VISUAL);
}
XVisualInfo visual_info_template;
_sapp_clear(&visual_info_template, sizeof(visual_info_template));
visual_info_template.visualid = (VisualID)visual_id;
int num_visuals;
XVisualInfo* visual_info = XGetVisualInfo(_sapp.x11.display, VisualIDMask, &visual_info_template, &num_visuals);
if (!visual_info) {
_SAPP_PANIC(LINUX_EGL_GET_VISUAL_INFO_FAILED);
}
_sapp_x11_create_window(visual_info->visual, visual_info->depth);
XFree(visual_info);
_sapp.egl.surface = eglCreateWindowSurface(_sapp.egl.display, config, (EGLNativeWindowType)_sapp.x11.window, NULL);
if (EGL_NO_SURFACE == _sapp.egl.surface) {
_SAPP_PANIC(LINUX_EGL_CREATE_WINDOW_SURFACE_FAILED);
}
EGLint ctx_attrs[] = {
#if defined(SOKOL_GLCORE)
EGL_CONTEXT_MAJOR_VERSION, _sapp.desc.gl_major_version,
EGL_CONTEXT_MINOR_VERSION, _sapp.desc.gl_minor_version,
EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
#elif defined(SOKOL_GLES3)
EGL_CONTEXT_CLIENT_VERSION, 3,
#endif
EGL_NONE,
};
_sapp.egl.context = eglCreateContext(_sapp.egl.display, config, EGL_NO_CONTEXT, ctx_attrs);
if (EGL_NO_CONTEXT == _sapp.egl.context) {
_SAPP_PANIC(LINUX_EGL_CREATE_CONTEXT_FAILED);
}
if (!eglMakeCurrent(_sapp.egl.display, _sapp.egl.surface, _sapp.egl.surface, _sapp.egl.context)) {
_SAPP_PANIC(LINUX_EGL_MAKE_CURRENT_FAILED);
}
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer);
eglSwapInterval(_sapp.egl.display, _sapp.swap_interval);
}
_SOKOL_PRIVATE void _sapp_egl_destroy(void) {
if (_sapp.egl.display != EGL_NO_DISPLAY) {
eglMakeCurrent(_sapp.egl.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (_sapp.egl.context != EGL_NO_CONTEXT) {
eglDestroyContext(_sapp.egl.display, _sapp.egl.context);
_sapp.egl.context = EGL_NO_CONTEXT;
}
if (_sapp.egl.surface != EGL_NO_SURFACE) {
eglDestroySurface(_sapp.egl.display, _sapp.egl.surface);
_sapp.egl.surface = EGL_NO_SURFACE;
}
eglTerminate(_sapp.egl.display);
_sapp.egl.display = EGL_NO_DISPLAY;
}
}
#endif /* _SAPP_GLX */
_SOKOL_PRIVATE void _sapp_linux_run(const sapp_desc* desc) {
/* The following lines are here to trigger a linker error instead of an
obscure runtime error if the user has forgotten to add -pthread to
the compiler or linker options. They have no other purpose.
*/
pthread_attr_t pthread_attr;
pthread_attr_init(&pthread_attr);
pthread_attr_destroy(&pthread_attr);
_sapp_init_state(desc);
_sapp.x11.window_state = NormalState;
XInitThreads();
XrmInitialize();
_sapp.x11.display = XOpenDisplay(NULL);
if (!_sapp.x11.display) {
_SAPP_PANIC(LINUX_X11_OPEN_DISPLAY_FAILED);
}
_sapp.x11.screen = DefaultScreen(_sapp.x11.display);
_sapp.x11.root = DefaultRootWindow(_sapp.x11.display);
_sapp_x11_query_system_dpi();
_sapp.dpi_scale = _sapp.x11.dpi / 96.0f;
_sapp_x11_init_extensions();
_sapp_x11_create_cursors();
XkbSetDetectableAutoRepeat(_sapp.x11.display, true, NULL);
_sapp_x11_init_keytable();
#if defined(_SAPP_GLX)
_sapp_glx_init();
Visual* visual = 0;
int depth = 0;
_sapp_glx_choose_visual(&visual, &depth);
_sapp_x11_create_window(visual, depth);
_sapp_glx_create_context();
_sapp_glx_swapinterval(_sapp.swap_interval);
#else
_sapp_egl_init();
#endif
sapp_set_icon(&desc->icon);
_sapp.valid = true;
_sapp_x11_show_window();
if (_sapp.fullscreen) {
_sapp_x11_set_fullscreen(true);
}
XFlush(_sapp.x11.display);
while (!_sapp.quit_ordered) {
_sapp_timing_measure(&_sapp.timing);
int count = XPending(_sapp.x11.display);
while (count--) {
XEvent event;
XNextEvent(_sapp.x11.display, &event);
_sapp_x11_process_event(&event);
}
_sapp_frame();
#if defined(_SAPP_GLX)
_sapp_glx_swap_buffers();
#else
eglSwapBuffers(_sapp.egl.display, _sapp.egl.surface);
#endif
XFlush(_sapp.x11.display);
/* handle quit-requested, either from window or from sapp_request_quit() */
if (_sapp.quit_requested && !_sapp.quit_ordered) {
/* give user code a chance to intervene */
_sapp_x11_app_event(SAPP_EVENTTYPE_QUIT_REQUESTED);
/* if user code hasn't intervened, quit the app */
if (_sapp.quit_requested) {
_sapp.quit_ordered = true;
}
}
}
_sapp_call_cleanup();
#if defined(_SAPP_GLX)
_sapp_glx_destroy_context();
#else
_sapp_egl_destroy();
#endif
_sapp_x11_destroy_window();
_sapp_x11_destroy_cursors();
XCloseDisplay(_sapp.x11.display);
_sapp_discard_state();
}
#if !defined(SOKOL_NO_ENTRY)
int main(int argc, char* argv[]) {
sapp_desc desc = sokol_main(argc, argv);
_sapp_linux_run(&desc);
return 0;
}
#endif /* SOKOL_NO_ENTRY */
#endif /* _SAPP_LINUX */
// ██████ ██ ██ ██████ ██ ██ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ██ ██████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██████ ██████ ███████ ██ ██████
//
// >>public
#if defined(SOKOL_NO_ENTRY)
SOKOL_API_IMPL void sapp_run(const sapp_desc* desc) {
SOKOL_ASSERT(desc);
#if defined(_SAPP_MACOS)
_sapp_macos_run(desc);
#elif defined(_SAPP_IOS)
_sapp_ios_run(desc);
#elif defined(_SAPP_EMSCRIPTEN)
_sapp_emsc_run(desc);
#elif defined(_SAPP_WIN32)
_sapp_win32_run(desc);
#elif defined(_SAPP_LINUX)
_sapp_linux_run(desc);
#else
#error "sapp_run() not supported on this platform"
#endif
}
/* this is just a stub so the linker doesn't complain */
sapp_desc sokol_main(int argc, char* argv[]) {
_SOKOL_UNUSED(argc);
_SOKOL_UNUSED(argv);
sapp_desc desc;
_sapp_clear(&desc, sizeof(desc));
return desc;
}
#else
/* likewise, in normal mode, sapp_run() is just an empty stub */
SOKOL_API_IMPL void sapp_run(const sapp_desc* desc) {
_SOKOL_UNUSED(desc);
}
#endif
SOKOL_API_IMPL bool sapp_isvalid(void) {
return _sapp.valid;
}
SOKOL_API_IMPL void* sapp_userdata(void) {
return _sapp.desc.user_data;
}
SOKOL_API_IMPL sapp_desc sapp_query_desc(void) {
return _sapp.desc;
}
SOKOL_API_IMPL uint64_t sapp_frame_count(void) {
return _sapp.frame_count;
}
SOKOL_API_IMPL double sapp_frame_duration(void) {
return _sapp_timing_get_avg(&_sapp.timing);
}
SOKOL_API_IMPL int sapp_width(void) {
return (_sapp.framebuffer_width > 0) ? _sapp.framebuffer_width : 1;
}
SOKOL_API_IMPL float sapp_widthf(void) {
return (float)sapp_width();
}
SOKOL_API_IMPL int sapp_height(void) {
return (_sapp.framebuffer_height > 0) ? _sapp.framebuffer_height : 1;
}
SOKOL_API_IMPL float sapp_heightf(void) {
return (float)sapp_height();
}
SOKOL_API_IMPL int sapp_color_format(void) {
#if defined(_SAPP_EMSCRIPTEN) && defined(SOKOL_WGPU)
switch (_sapp.wgpu.render_format) {
case WGPUTextureFormat_RGBA8Unorm:
return _SAPP_PIXELFORMAT_RGBA8;
case WGPUTextureFormat_BGRA8Unorm:
return _SAPP_PIXELFORMAT_BGRA8;
default:
SOKOL_UNREACHABLE;
return 0;
}
#elif defined(SOKOL_METAL) || defined(SOKOL_D3D11)
return _SAPP_PIXELFORMAT_BGRA8;
#else
return _SAPP_PIXELFORMAT_RGBA8;
#endif
}
SOKOL_API_IMPL int sapp_depth_format(void) {
return _SAPP_PIXELFORMAT_DEPTH_STENCIL;
}
SOKOL_API_IMPL int sapp_sample_count(void) {
return _sapp.sample_count;
}
SOKOL_API_IMPL bool sapp_high_dpi(void) {
return _sapp.desc.high_dpi && (_sapp.dpi_scale >= 1.5f);
}
SOKOL_API_IMPL float sapp_dpi_scale(void) {
return _sapp.dpi_scale;
}
SOKOL_API_IMPL const void* sapp_egl_get_display(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(_SAPP_ANDROID)
return _sapp.android.display;
#elif defined(_SAPP_LINUX) && !defined(_SAPP_GLX)
return _sapp.egl.display;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_egl_get_context(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(_SAPP_ANDROID)
return _sapp.android.context;
#elif defined(_SAPP_LINUX) && !defined(_SAPP_GLX)
return _sapp.egl.context;
#else
return 0;
#endif
}
SOKOL_API_IMPL void sapp_show_keyboard(bool show) {
#if defined(_SAPP_IOS)
_sapp_ios_show_keyboard(show);
#elif defined(_SAPP_ANDROID)
_sapp_android_show_keyboard(show);
#else
_SOKOL_UNUSED(show);
#endif
}
SOKOL_API_IMPL bool sapp_keyboard_shown(void) {
return _sapp.onscreen_keyboard_shown;
}
SOKOL_API_IMPL bool sapp_is_fullscreen(void) {
return _sapp.fullscreen;
}
SOKOL_API_IMPL void sapp_toggle_fullscreen(void) {
#if defined(_SAPP_MACOS)
_sapp_macos_toggle_fullscreen();
#elif defined(_SAPP_WIN32)
_sapp_win32_toggle_fullscreen();
#elif defined(_SAPP_LINUX)
_sapp_x11_toggle_fullscreen();
#endif
}
/* NOTE that sapp_show_mouse() does not "stack" like the Win32 or macOS API functions! */
SOKOL_API_IMPL void sapp_show_mouse(bool show) {
if (_sapp.mouse.shown != show) {
#if defined(_SAPP_MACOS)
_sapp_macos_update_cursor(_sapp.mouse.current_cursor, show);
#elif defined(_SAPP_WIN32)
_sapp_win32_update_cursor(_sapp.mouse.current_cursor, show, false);
#elif defined(_SAPP_LINUX)
_sapp_x11_update_cursor(_sapp.mouse.current_cursor, show);
#elif defined(_SAPP_EMSCRIPTEN)
_sapp_emsc_update_cursor(_sapp.mouse.current_cursor, show);
#endif
_sapp.mouse.shown = show;
}
}
SOKOL_API_IMPL bool sapp_mouse_shown(void) {
return _sapp.mouse.shown;
}
SOKOL_API_IMPL void sapp_lock_mouse(bool lock) {
#if defined(_SAPP_MACOS)
_sapp_macos_lock_mouse(lock);
#elif defined(_SAPP_EMSCRIPTEN)
_sapp_emsc_lock_mouse(lock);
#elif defined(_SAPP_WIN32)
_sapp_win32_lock_mouse(lock);
#elif defined(_SAPP_LINUX)
_sapp_x11_lock_mouse(lock);
#else
_sapp.mouse.locked = lock;
#endif
}
SOKOL_API_IMPL bool sapp_mouse_locked(void) {
return _sapp.mouse.locked;
}
SOKOL_API_IMPL void sapp_set_mouse_cursor(sapp_mouse_cursor cursor) {
SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM));
if (_sapp.mouse.current_cursor != cursor) {
#if defined(_SAPP_MACOS)
_sapp_macos_update_cursor(cursor, _sapp.mouse.shown);
#elif defined(_SAPP_WIN32)
_sapp_win32_update_cursor(cursor, _sapp.mouse.shown, false);
#elif defined(_SAPP_LINUX)
_sapp_x11_update_cursor(cursor, _sapp.mouse.shown);
#elif defined(_SAPP_EMSCRIPTEN)
_sapp_emsc_update_cursor(cursor, _sapp.mouse.shown);
#endif
_sapp.mouse.current_cursor = cursor;
}
}
SOKOL_API_IMPL sapp_mouse_cursor sapp_get_mouse_cursor(void) {
return _sapp.mouse.current_cursor;
}
SOKOL_API_IMPL void sapp_request_quit(void) {
_sapp.quit_requested = true;
}
SOKOL_API_IMPL void sapp_cancel_quit(void) {
_sapp.quit_requested = false;
}
SOKOL_API_IMPL void sapp_quit(void) {
_sapp.quit_ordered = true;
}
SOKOL_API_IMPL void sapp_consume_event(void) {
_sapp.event_consumed = true;
}
/* NOTE: on HTML5, sapp_set_clipboard_string() must be called from within event handler! */
SOKOL_API_IMPL void sapp_set_clipboard_string(const char* str) {
if (!_sapp.clipboard.enabled) {
return;
}
SOKOL_ASSERT(str);
#if defined(_SAPP_MACOS)
_sapp_macos_set_clipboard_string(str);
#elif defined(_SAPP_EMSCRIPTEN)
_sapp_emsc_set_clipboard_string(str);
#elif defined(_SAPP_WIN32)
_sapp_win32_set_clipboard_string(str);
#else
/* not implemented */
#endif
_sapp_strcpy(str, _sapp.clipboard.buffer, _sapp.clipboard.buf_size);
}
SOKOL_API_IMPL const char* sapp_get_clipboard_string(void) {
if (!_sapp.clipboard.enabled) {
return "";
}
#if defined(_SAPP_MACOS)
return _sapp_macos_get_clipboard_string();
#elif defined(_SAPP_EMSCRIPTEN)
return _sapp.clipboard.buffer;
#elif defined(_SAPP_WIN32)
return _sapp_win32_get_clipboard_string();
#else
/* not implemented */
return _sapp.clipboard.buffer;
#endif
}
SOKOL_API_IMPL void sapp_set_window_title(const char* title) {
SOKOL_ASSERT(title);
_sapp_strcpy(title, _sapp.window_title, sizeof(_sapp.window_title));
#if defined(_SAPP_MACOS)
_sapp_macos_update_window_title();
#elif defined(_SAPP_WIN32)
_sapp_win32_update_window_title();
#elif defined(_SAPP_LINUX)
_sapp_x11_update_window_title();
#endif
}
SOKOL_API_IMPL void sapp_set_icon(const sapp_icon_desc* desc) {
SOKOL_ASSERT(desc);
if (desc->sokol_default) {
if (0 == _sapp.default_icon_pixels) {
_sapp_setup_default_icon();
}
SOKOL_ASSERT(0 != _sapp.default_icon_pixels);
desc = &_sapp.default_icon_desc;
}
const int num_images = _sapp_icon_num_images(desc);
if (num_images == 0) {
return;
}
SOKOL_ASSERT((num_images > 0) && (num_images <= SAPP_MAX_ICONIMAGES));
if (!_sapp_validate_icon_desc(desc, num_images)) {
return;
}
#if defined(_SAPP_MACOS)
_sapp_macos_set_icon(desc, num_images);
#elif defined(_SAPP_WIN32)
_sapp_win32_set_icon(desc, num_images);
#elif defined(_SAPP_LINUX)
_sapp_x11_set_icon(desc, num_images);
#elif defined(_SAPP_EMSCRIPTEN)
_sapp_emsc_set_icon(desc, num_images);
#endif
}
SOKOL_API_IMPL int sapp_get_num_dropped_files(void) {
SOKOL_ASSERT(_sapp.drop.enabled);
return _sapp.drop.num_files;
}
SOKOL_API_IMPL const char* sapp_get_dropped_file_path(int index) {
SOKOL_ASSERT(_sapp.drop.enabled);
SOKOL_ASSERT((index >= 0) && (index < _sapp.drop.num_files));
SOKOL_ASSERT(_sapp.drop.buffer);
if (!_sapp.drop.enabled) {
return "";
}
if ((index < 0) || (index >= _sapp.drop.max_files)) {
return "";
}
return (const char*) _sapp_dropped_file_path_ptr(index);
}
SOKOL_API_IMPL uint32_t sapp_html5_get_dropped_file_size(int index) {
SOKOL_ASSERT(_sapp.drop.enabled);
SOKOL_ASSERT((index >= 0) && (index < _sapp.drop.num_files));
#if defined(_SAPP_EMSCRIPTEN)
if (!_sapp.drop.enabled) {
return 0;
}
return sapp_js_dropped_file_size(index);
#else
(void)index;
return 0;
#endif
}
SOKOL_API_IMPL void sapp_html5_fetch_dropped_file(const sapp_html5_fetch_request* request) {
SOKOL_ASSERT(_sapp.drop.enabled);
SOKOL_ASSERT(request);
SOKOL_ASSERT(request->callback);
SOKOL_ASSERT(request->buffer.ptr);
SOKOL_ASSERT(request->buffer.size > 0);
#if defined(_SAPP_EMSCRIPTEN)
const int index = request->dropped_file_index;
sapp_html5_fetch_error error_code = SAPP_HTML5_FETCH_ERROR_NO_ERROR;
if ((index < 0) || (index >= _sapp.drop.num_files)) {
error_code = SAPP_HTML5_FETCH_ERROR_OTHER;
}
if (sapp_html5_get_dropped_file_size(index) > request->buffer.size) {
error_code = SAPP_HTML5_FETCH_ERROR_BUFFER_TOO_SMALL;
}
if (SAPP_HTML5_FETCH_ERROR_NO_ERROR != error_code) {
_sapp_emsc_invoke_fetch_cb(index,
false, // success
(int)error_code,
request->callback,
0, // fetched_size
(void*)request->buffer.ptr,
request->buffer.size,
request->user_data);
}
else {
sapp_js_fetch_dropped_file(index,
request->callback,
(void*)request->buffer.ptr,
request->buffer.size,
request->user_data);
}
#else
(void)request;
#endif
}
SOKOL_API_IMPL const void* sapp_metal_get_device(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_METAL)
#if defined(_SAPP_MACOS)
const void* obj = (__bridge const void*) _sapp.macos.mtl_device;
#else
const void* obj = (__bridge const void*) _sapp.ios.mtl_device;
#endif
SOKOL_ASSERT(obj);
return obj;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_metal_get_current_drawable(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_METAL)
#if defined(_SAPP_MACOS)
const void* obj = (__bridge const void*) [_sapp.macos.view currentDrawable];
#else
const void* obj = (__bridge const void*) [_sapp.ios.view currentDrawable];
#endif
SOKOL_ASSERT(obj);
return obj;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_metal_get_depth_stencil_texture(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_METAL)
#if defined(_SAPP_MACOS)
const void* obj = (__bridge const void*) [_sapp.macos.view depthStencilTexture];
#else
const void* obj = (__bridge const void*) [_sapp.ios.view depthStencilTexture];
#endif
return obj;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_metal_get_msaa_color_texture(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_METAL)
#if defined(_SAPP_MACOS)
const void* obj = (__bridge const void*) [_sapp.macos.view multisampleColorTexture];
#else
const void* obj = (__bridge const void*) [_sapp.ios.view multisampleColorTexture];
#endif
return obj;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_macos_get_window(void) {
#if defined(_SAPP_MACOS)
const void* obj = (__bridge const void*) _sapp.macos.window;
SOKOL_ASSERT(obj);
return obj;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_ios_get_window(void) {
#if defined(_SAPP_IOS)
const void* obj = (__bridge const void*) _sapp.ios.window;
SOKOL_ASSERT(obj);
return obj;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_d3d11_get_device(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_D3D11)
return _sapp.d3d11.device;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_d3d11_get_device_context(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_D3D11)
return _sapp.d3d11.device_context;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_d3d11_get_swap_chain(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_D3D11)
return _sapp.d3d11.swap_chain;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_d3d11_get_render_view(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_D3D11)
if (_sapp.sample_count > 1) {
SOKOL_ASSERT(_sapp.d3d11.msaa_rtv);
return _sapp.d3d11.msaa_rtv;
} else {
SOKOL_ASSERT(_sapp.d3d11.rtv);
return _sapp.d3d11.rtv;
}
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_d3d11_get_resolve_view(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_D3D11)
if (_sapp.sample_count > 1) {
SOKOL_ASSERT(_sapp.d3d11.rtv);
return _sapp.d3d11.rtv;
} else {
return 0;
}
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_d3d11_get_depth_stencil_view(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_D3D11)
return _sapp.d3d11.dsv;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_win32_get_hwnd(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(_SAPP_WIN32)
return _sapp.win32.hwnd;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_wgpu_get_device(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(_SAPP_EMSCRIPTEN) && defined(SOKOL_WGPU)
return (const void*) _sapp.wgpu.device;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_wgpu_get_render_view(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(_SAPP_EMSCRIPTEN) && defined(SOKOL_WGPU)
if (_sapp.sample_count > 1) {
SOKOL_ASSERT(_sapp.wgpu.msaa_view);
return (const void*) _sapp.wgpu.msaa_view;
} else {
SOKOL_ASSERT(_sapp.wgpu.swapchain_view);
return (const void*) _sapp.wgpu.swapchain_view;
}
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_wgpu_get_resolve_view(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(_SAPP_EMSCRIPTEN) && defined(SOKOL_WGPU)
if (_sapp.sample_count > 1) {
SOKOL_ASSERT(_sapp.wgpu.swapchain_view);
return (const void*) _sapp.wgpu.swapchain_view;
} else {
return 0;
}
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_wgpu_get_depth_stencil_view(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(_SAPP_EMSCRIPTEN) && defined(SOKOL_WGPU)
return (const void*) _sapp.wgpu.depth_stencil_view;
#else
return 0;
#endif
}
SOKOL_API_IMPL uint32_t sapp_gl_get_framebuffer(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(_SAPP_ANY_GL)
return _sapp.gl.framebuffer;
#else
return 0;
#endif
}
SOKOL_API_IMPL int sapp_gl_get_major_version(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_GLCORE)
return _sapp.desc.gl_major_version;
#else
return 0;
#endif
}
SOKOL_API_IMPL int sapp_gl_get_minor_version(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_GLCORE)
return _sapp.desc.gl_minor_version;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sapp_android_get_native_activity(void) {
// NOTE: _sapp.valid is not asserted here because sapp_android_get_native_activity()
// needs to be callable from within sokol_main() (see: https://github.com/floooh/sokol/issues/708)
#if defined(_SAPP_ANDROID)
return (void*)_sapp.android.activity;
#else
return 0;
#endif
}
SOKOL_API_IMPL void sapp_html5_ask_leave_site(bool ask) {
_sapp.html5_ask_leave_site = ask;
}
#endif /* SOKOL_APP_IMPL */
|
0 |
repos
|
repos/sokol/sokol_glue.h
|
#if defined(SOKOL_IMPL) && !defined(SOKOL_GLUE_IMPL)
#define SOKOL_GLUE_IMPL
#endif
#ifndef SOKOL_GLUE_INCLUDED
/*
sokol_glue.h -- glue helper functions for sokol headers
Project URL: https://github.com/floooh/sokol
Do this:
#define SOKOL_IMPL or
#define SOKOL_GLUE_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
...optionally provide the following macros to override defaults:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_GLUE_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_GLUE_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
If sokol_glue.h is compiled as a DLL, define the following before
including the declaration or implementation:
SOKOL_DLL
On Windows, SOKOL_DLL will define SOKOL_GLUE_API_DECL as __declspec(dllexport)
or __declspec(dllimport) as needed.
OVERVIEW
========
sokol_glue.h provides glue helper functions between sokol_gfx.h and sokol_app.h,
so that sokol_gfx.h doesn't need to depend on sokol_app.h but can be
used with different window system glue libraries.
PROVIDED FUNCTIONS
==================
sg_environment sglue_environment(void)
Returns an sg_environment struct initialized by calling sokol_app.h
functions. Use this in the sg_setup() call like this:
sg_setup(&(sg_desc){
.environment = sglue_environment(),
...
});
sg_swapchain sglue_swapchain(void)
Returns an sg_swapchain struct initialized by calling sokol_app.h
functions. Use this in sg_begin_pass() for a 'swapchain pass' like
this:
sg_begin_pass(&(sg_pass){ .swapchain = sglue_swapchain(), ... });
LICENSE
=======
zlib/libpng license
Copyright (c) 2018 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define SOKOL_GLUE_INCLUDED
#if defined(SOKOL_API_DECL) && !defined(SOKOL_GLUE_API_DECL)
#define SOKOL_GLUE_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_GLUE_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_GLUE_IMPL)
#define SOKOL_GLUE_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_GLUE_API_DECL __declspec(dllimport)
#else
#define SOKOL_GLUE_API_DECL extern
#endif
#endif
#ifndef SOKOL_GFX_INCLUDED
#error "Please include sokol_gfx.h before sokol_glue.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
SOKOL_GLUE_API_DECL sg_environment sglue_environment(void);
SOKOL_GLUE_API_DECL sg_swapchain sglue_swapchain(void);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SOKOL_GLUE_INCLUDED */
/*-- IMPLEMENTATION ----------------------------------------------------------*/
#ifdef SOKOL_GLUE_IMPL
#define SOKOL_GLUE_IMPL_INCLUDED (1)
#include <string.h> /* memset */
#ifndef SOKOL_APP_INCLUDED
#error "Please include sokol_app.h before the sokol_glue.h implementation"
#endif
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
SOKOL_API_IMPL sg_environment sglue_environment(void) {
sg_environment env;
memset(&env, 0, sizeof(env));
env.defaults.color_format = (sg_pixel_format) sapp_color_format();
env.defaults.depth_format = (sg_pixel_format) sapp_depth_format();
env.defaults.sample_count = sapp_sample_count();
env.metal.device = sapp_metal_get_device();
env.d3d11.device = sapp_d3d11_get_device();
env.d3d11.device_context = sapp_d3d11_get_device_context();
env.wgpu.device = sapp_wgpu_get_device();
return env;
}
SOKOL_API_IMPL sg_swapchain sglue_swapchain(void) {
sg_swapchain swapchain;
memset(&swapchain, 0, sizeof(swapchain));
swapchain.width = sapp_width();
swapchain.height = sapp_height();
swapchain.sample_count = sapp_sample_count();
swapchain.color_format = (sg_pixel_format)sapp_color_format();
swapchain.depth_format = (sg_pixel_format)sapp_depth_format();
swapchain.metal.current_drawable = sapp_metal_get_current_drawable();
swapchain.metal.depth_stencil_texture = sapp_metal_get_depth_stencil_texture();
swapchain.metal.msaa_color_texture = sapp_metal_get_msaa_color_texture();
swapchain.d3d11.render_view = sapp_d3d11_get_render_view();
swapchain.d3d11.resolve_view = sapp_d3d11_get_resolve_view();
swapchain.d3d11.depth_stencil_view = sapp_d3d11_get_depth_stencil_view();
swapchain.wgpu.render_view = sapp_wgpu_get_render_view();
swapchain.wgpu.resolve_view = sapp_wgpu_get_resolve_view();
swapchain.wgpu.depth_stencil_view = sapp_wgpu_get_depth_stencil_view();
swapchain.gl.framebuffer = sapp_gl_get_framebuffer();
return swapchain;
}
#endif /* SOKOL_GLUE_IMPL */
|
0 |
repos
|
repos/sokol/sokol_gfx.h
|
#if defined(SOKOL_IMPL) && !defined(SOKOL_GFX_IMPL)
#define SOKOL_GFX_IMPL
#endif
#ifndef SOKOL_GFX_INCLUDED
/*
sokol_gfx.h -- simple 3D API wrapper
Project URL: https://github.com/floooh/sokol
Example code: https://github.com/floooh/sokol-samples
Do this:
#define SOKOL_IMPL or
#define SOKOL_GFX_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
In the same place define one of the following to select the rendering
backend:
#define SOKOL_GLCORE
#define SOKOL_GLES3
#define SOKOL_D3D11
#define SOKOL_METAL
#define SOKOL_WGPU
#define SOKOL_DUMMY_BACKEND
I.e. for the desktop GL it should look like this:
#include ...
#include ...
#define SOKOL_IMPL
#define SOKOL_GLCORE
#include "sokol_gfx.h"
The dummy backend replaces the platform-specific backend code with empty
stub functions. This is useful for writing tests that need to run on the
command line.
Optionally provide the following defines with your own implementations:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false))
SOKOL_GFX_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_GFX_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
SOKOL_TRACE_HOOKS - enable trace hook callbacks (search below for TRACE HOOKS)
SOKOL_EXTERNAL_GL_LOADER - indicates that you're using your own GL loader, in this case
sokol_gfx.h will not include any platform GL headers and disable
the integrated Win32 GL loader
If sokol_gfx.h is compiled as a DLL, define the following before
including the declaration or implementation:
SOKOL_DLL
On Windows, SOKOL_DLL will define SOKOL_GFX_API_DECL as __declspec(dllexport)
or __declspec(dllimport) as needed.
If you want to compile without deprecated structs and functions,
define:
SOKOL_NO_DEPRECATED
Optionally define the following to force debug checks and validations
even in release mode:
SOKOL_DEBUG - by default this is defined if _DEBUG is defined
sokol_gfx DOES NOT:
===================
- create a window, swapchain or the 3D-API context/device, you must do this
before sokol_gfx is initialized, and pass any required information
(like 3D device pointers) to the sokol_gfx initialization call
- present the rendered frame, how this is done exactly usually depends
on how the window and 3D-API context/device was created
- provide a unified shader language, instead 3D-API-specific shader
source-code or shader-bytecode must be provided (for the "official"
offline shader cross-compiler, see here:
https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md)
STEP BY STEP
============
--- to initialize sokol_gfx, after creating a window and a 3D-API
context/device, call:
sg_setup(const sg_desc*)
Depending on the selected 3D backend, sokol-gfx requires some
information, like a device pointer, default swapchain pixel formats
and so on. If you are using sokol_app.h for the window system
glue, you can use a helper function provided in the sokol_glue.h
header:
#include "sokol_gfx.h"
#include "sokol_app.h"
#include "sokol_glue.h"
//...
sg_setup(&(sg_desc){
.environment = sglue_environment(),
});
To get any logging output for errors and from the validation layer, you
need to provide a logging callback. Easiest way is through sokol_log.h:
#include "sokol_log.h"
//...
sg_setup(&(sg_desc){
//...
.logger.func = slog_func,
});
--- create resource objects (at least buffers, shaders and pipelines,
and optionally images, samplers and render-pass-attachments):
sg_buffer sg_make_buffer(const sg_buffer_desc*)
sg_image sg_make_image(const sg_image_desc*)
sg_sampler sg_make_sampler(const sg_sampler_desc*)
sg_shader sg_make_shader(const sg_shader_desc*)
sg_pipeline sg_make_pipeline(const sg_pipeline_desc*)
sg_attachments sg_make_attachments(const sg_attachments_desc*)
--- start a render pass:
sg_begin_pass(const sg_pass* pass);
Typically, passes render into an externally provided swapchain which
presents the rendering result on the display. Such a 'swapchain pass'
is started like this:
sg_begin_pass(&(sg_pass){ .action = { ... }, .swapchain = sglue_swapchain() })
...where .action is an sg_pass_action struct containing actions to be performed
at the start and end of a render pass (such as clearing the render surfaces to
a specific color), and .swapchain is an sg_swapchain
struct all the required information to render into the swapchain's surfaces.
To start an 'offscreen pass' into sokol-gfx image objects, an sg_attachment
object handle is required instead of an sg_swapchain struct. An offscreen
pass is started like this (assuming attachments is an sg_attachments handle):
sg_begin_pass(&(sg_pass){ .action = { ... }, .attachments = attachments });
--- set the render pipeline state for the next draw call with:
sg_apply_pipeline(sg_pipeline pip)
--- fill an sg_bindings struct with the resource bindings for the next
draw call (0..N vertex buffers, 0 or 1 index buffer, 0..N image-objects,
samplers and storage-buffers), and call:
sg_apply_bindings(const sg_bindings* bindings)
to update the resource bindings
--- optionally update shader uniform data with:
sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range* data)
Read the section 'UNIFORM DATA LAYOUT' to learn about the expected memory layout
of the uniform data passed into sg_apply_uniforms().
--- kick off a draw call with:
sg_draw(int base_element, int num_elements, int num_instances)
The sg_draw() function unifies all the different ways to render primitives
in a single call (indexed vs non-indexed rendering, and instanced vs non-instanced
rendering). In case of indexed rendering, base_element and num_element specify
indices in the currently bound index buffer. In case of non-indexed rendering
base_element and num_elements specify vertices in the currently bound
vertex-buffer(s). To perform instanced rendering, the rendering pipeline
must be setup for instancing (see sg_pipeline_desc below), a separate vertex buffer
containing per-instance data must be bound, and the num_instances parameter
must be > 1.
--- finish the current rendering pass with:
sg_end_pass()
--- when done with the current frame, call
sg_commit()
--- at the end of your program, shutdown sokol_gfx with:
sg_shutdown()
--- if you need to destroy resources before sg_shutdown(), call:
sg_destroy_buffer(sg_buffer buf)
sg_destroy_image(sg_image img)
sg_destroy_sampler(sg_sampler smp)
sg_destroy_shader(sg_shader shd)
sg_destroy_pipeline(sg_pipeline pip)
sg_destroy_attachments(sg_attachments atts)
--- to set a new viewport rectangle, call
sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left)
...or if you want to specify the viewport rectangle with float values:
sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left)
--- to set a new scissor rect, call:
sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left)
...or with float values:
sg_apply_scissor_rectf(float x, float y, float width, float height, bool origin_top_left)
Both sg_apply_viewport() and sg_apply_scissor_rect() must be called
inside a rendering pass
Note that sg_begin_default_pass() and sg_begin_pass() will reset both the
viewport and scissor rectangles to cover the entire framebuffer.
--- to update (overwrite) the content of buffer and image resources, call:
sg_update_buffer(sg_buffer buf, const sg_range* data)
sg_update_image(sg_image img, const sg_image_data* data)
Buffers and images to be updated must have been created with
SG_USAGE_DYNAMIC or SG_USAGE_STREAM
Only one update per frame is allowed for buffer and image resources when
using the sg_update_*() functions. The rationale is to have a simple
countermeasure to avoid the CPU scribbling over data the GPU is currently
using, or the CPU having to wait for the GPU
Buffer and image updates can be partial, as long as a rendering
operation only references the valid (updated) data in the
buffer or image.
--- to append a chunk of data to a buffer resource, call:
int sg_append_buffer(sg_buffer buf, const sg_range* data)
The difference to sg_update_buffer() is that sg_append_buffer()
can be called multiple times per frame to append new data to the
buffer piece by piece, optionally interleaved with draw calls referencing
the previously written data.
sg_append_buffer() returns a byte offset to the start of the
written data, this offset can be assigned to
sg_bindings.vertex_buffer_offsets[n] or
sg_bindings.index_buffer_offset
Code example:
for (...) {
const void* data = ...;
const int num_bytes = ...;
int offset = sg_append_buffer(buf, &(sg_range) { .ptr=data, .size=num_bytes });
bindings.vertex_buffer_offsets[0] = offset;
sg_apply_pipeline(pip);
sg_apply_bindings(&bindings);
sg_apply_uniforms(...);
sg_draw(...);
}
A buffer to be used with sg_append_buffer() must have been created
with SG_USAGE_DYNAMIC or SG_USAGE_STREAM.
If the application appends more data to the buffer then fits into
the buffer, the buffer will go into the "overflow" state for the
rest of the frame.
Any draw calls attempting to render an overflown buffer will be
silently dropped (in debug mode this will also result in a
validation error).
You can also check manually if a buffer is in overflow-state by calling
bool sg_query_buffer_overflow(sg_buffer buf)
You can manually check to see if an overflow would occur before adding
any data to a buffer by calling
bool sg_query_buffer_will_overflow(sg_buffer buf, size_t size)
NOTE: Due to restrictions in underlying 3D-APIs, appended chunks of
data will be 4-byte aligned in the destination buffer. This means
that there will be gaps in index buffers containing 16-bit indices
when the number of indices in a call to sg_append_buffer() is
odd. This isn't a problem when each call to sg_append_buffer()
is associated with one draw call, but will be problematic when
a single indexed draw call spans several appended chunks of indices.
--- to check at runtime for optional features, limits and pixelformat support,
call:
sg_features sg_query_features()
sg_limits sg_query_limits()
sg_pixelformat_info sg_query_pixelformat(sg_pixel_format fmt)
--- if you need to call into the underlying 3D-API directly, you must call:
sg_reset_state_cache()
...before calling sokol_gfx functions again
--- you can inspect the original sg_desc structure handed to sg_setup()
by calling sg_query_desc(). This will return an sg_desc struct with
the default values patched in instead of any zero-initialized values
--- you can get a desc struct matching the creation attributes of a
specific resource object via:
sg_buffer_desc sg_query_buffer_desc(sg_buffer buf)
sg_image_desc sg_query_image_desc(sg_image img)
sg_sampler_desc sg_query_sampler_desc(sg_sampler smp)
sg_shader_desc sq_query_shader_desc(sg_shader shd)
sg_pipeline_desc sg_query_pipeline_desc(sg_pipeline pip)
sg_attachments_desc sg_query_attachments_desc(sg_attachments atts)
...but NOTE that the returned desc structs may be incomplete, only
creation attributes that are kept around internally after resource
creation will be filled in, and in some cases (like shaders) that's
very little. Any missing attributes will be set to zero. The returned
desc structs might still be useful as partial blueprint for creating
similar resources if filled up with the missing attributes.
Calling the query-desc functions on an invalid resource will return
completely zeroed structs (it makes sense to check the resource state
with sg_query_*_state() first)
--- you can query the default resource creation parameters through the functions
sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc)
sg_image_desc sg_query_image_defaults(const sg_image_desc* desc)
sg_sampler_desc sg_query_sampler_defaults(const sg_sampler_desc* desc)
sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc)
sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc)
sg_attachments_desc sg_query_attachments_defaults(const sg_attachments_desc* desc)
These functions take a pointer to a desc structure which may contain
zero-initialized items for default values. These zero-init values
will be replaced with their concrete values in the returned desc
struct.
--- you can inspect various internal resource runtime values via:
sg_buffer_info sg_query_buffer_info(sg_buffer buf)
sg_image_info sg_query_image_info(sg_image img)
sg_sampler_info sg_query_sampler_info(sg_sampler smp)
sg_shader_info sg_query_shader_info(sg_shader shd)
sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip)
sg_attachments_info sg_query_attachments_info(sg_attachments atts)
...please note that the returned info-structs are tied quite closely
to sokol_gfx.h internals, and may change more often than other
public API functions and structs.
--- you can query frame stats and control stats collection via:
sg_query_frame_stats()
sg_enable_frame_stats()
sg_disable_frame_stats()
sg_frame_stats_enabled()
--- you can ask at runtime what backend sokol_gfx.h has been compiled for:
sg_backend sg_query_backend(void)
--- call the following helper functions to compute the number of
bytes in a texture row or surface for a specific pixel format.
These functions might be helpful when preparing image data for consumption
by sg_make_image() or sg_update_image():
int sg_query_row_pitch(sg_pixel_format fmt, int width, int int row_align_bytes);
int sg_query_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align_bytes);
Width and height are generally in number pixels, but note that 'row' has different meaning
for uncompressed vs compressed pixel formats: for uncompressed formats, a row is identical
with a single line if pixels, while in compressed formats, one row is a line of *compression blocks*.
This is why calling sg_query_surface_pitch() for a compressed pixel format and height
N, N+1, N+2, ... may return the same result.
The row_align_bytes parammeter is for added flexibility. For image data that goes into
the sg_make_image() or sg_update_image() this should generally be 1, because these
functions take tightly packed image data as input no matter what alignment restrictions
exist in the backend 3D APIs.
ON INITIALIZATION:
==================
When calling sg_setup(), a pointer to an sg_desc struct must be provided
which contains initialization options. These options provide two types
of information to sokol-gfx:
(1) upper bounds and limits needed to allocate various internal
data structures:
- the max number of resources of each type that can
be alive at the same time, this is used for allocating
internal pools
- the max overall size of uniform data that can be
updated per frame, including a worst-case alignment
per uniform update (this worst-case alignment is 256 bytes)
- the max size of all dynamic resource updates (sg_update_buffer,
sg_append_buffer and sg_update_image) per frame
Not all of those limit values are used by all backends, but it is
good practice to provide them none-the-less.
(2) 3D backend "environment information" in a nested sg_environment struct:
- pointers to backend-specific context- or device-objects (for instance
the D3D11, WebGPU or Metal device objects)
- defaults for external swapchain pixel formats and sample counts,
these will be used as default values in image and pipeline objects,
and the sg_swapchain struct passed into sg_begin_pass()
Usually you provide a complete sg_environment struct through
a helper function, as an example look at the sglue_environment()
function in the sokol_glue.h header.
See the documentation block of the sg_desc struct below for more information.
ON RENDER PASSES
================
Relevant samples:
- https://floooh.github.io/sokol-html5/offscreen-sapp.html
- https://floooh.github.io/sokol-html5/offscreen-msaa-sapp.html
- https://floooh.github.io/sokol-html5/mrt-sapp.html
- https://floooh.github.io/sokol-html5/mrt-pixelformats-sapp.html
A render pass groups rendering commands into a set of render target images
(called 'pass attachments'). Render target images can be used in subsequent
passes as textures (it is invalid to use the same image both as render target
and as texture in the same pass).
The following sokol-gfx functions must only be called inside a render pass:
sg_apply_viewport(f)
sg_apply_scissor_rect(f)
sg_apply_pipeline
sg_apply_bindings
sg_apply_uniforms
sg_draw
A frame must have at least one 'swapchain render pass' which renders into an
externally provided swapchain provided as an sg_swapchain struct to the
sg_begin_pass() function. The sg_swapchain struct must contain the
following information:
- the color pixel-format of the swapchain's render surface
- an optional depth/stencil pixel format if the swapchain
has a depth/stencil buffer
- an optional sample-count for MSAA rendering
- NOTE: the above three values can be zero-initialized, in that
case the defaults from the sg_environment struct will be used that
had been passed to the sg_setup() function.
- a number of backend specific objects:
- GL/GLES3: just a GL framebuffer handle
- D3D11:
- an ID3D11RenderTargetView for the rendering surface
- if MSAA is used, an ID3D11RenderTargetView as
MSAA resolve-target
- an optional ID3D11DepthStencilView for the
depth/stencil buffer
- WebGPU
- a WGPUTextureView object for the rendering surface
- if MSAA is used, a WGPUTextureView object as MSAA resolve target
- an optional WGPUTextureView for the
- Metal (NOTE that the roles of provided surfaces is slightly
different in Metal than in D3D11 or WebGPU, notably, the
CAMetalDrawable is either rendered to directly, or serves
as MSAA resolve target):
- a CAMetalDrawable object which is either rendered
into directly, or in case of MSAA rendering, serves
as MSAA-resolve-target
- if MSAA is used, an multisampled MTLTexture where
rendering goes into
- an optional MTLTexture for the depth/stencil buffer
It's recommended that you create a helper function which returns an
initialized sg_swapchain struct by value. This can then be directly plugged
into the sg_begin_pass function like this:
sg_begin_pass(&(sg_pass){ .swapchain = sglue_swapchain() });
As an example for such a helper function check out the function sglue_swapchain()
in the sokol_glue.h header.
For offscreen render passes, the render target images used in a render pass
are baked into an immutable sg_attachments object.
For a simple offscreen scenario with one color-, one depth-stencil-render
target and without multisampling, creating an attachment object looks like this:
First create two render target images, one with a color pixel format,
and one with the depth- or depth-stencil pixel format. Both images
must have the same dimensions:
const sg_image color_img = sg_make_image(&(sg_image_desc){
.render_target = true,
.width = 256,
.height = 256,
.pixel_format = SG_PIXELFORMAT_RGBA8,
.sample_count = 1,
});
const sg_image depth_img = sg_make_image(&(sg_image_desc){
.render_target = true,
.width = 256,
.height = 256,
.pixel_format = SG_PIXELFORMAT_DEPTH,
.sample_count = 1,
});
NOTE: when creating render target images, have in mind that some default values
are aligned with the default environment attributes in the sg_environment struct
that was passed into the sg_setup() call:
- the default value for sg_image_desc.pixel_format is taken from
sg_environment.defaults.color_format
- the default value for sg_image_desc.sample_count is taken from
sg_environment.defaults.sample_count
- the default value for sg_image_desc.num_mipmaps is always 1
Next create an attachments object:
const sg_attachments atts = sg_make_attachments(&(sg_attachments_desc){
.colors[0].image = color_img,
.depth_stencil.image = depth_img,
});
This attachments object is then passed into the sg_begin_pass() function
in place of the swapchain struct:
sg_begin_pass(&(sg_pass){ .attachments = atts });
Swapchain and offscreen passes form dependency trees each with a swapchain
pass at the root, offscreen passes as nodes, and render target images as
dependencies between passes.
sg_pass_action structs are used to define actions that should happen at the
start and end of rendering passes (such as clearing pass attachments to a
specific color or depth-value, or performing an MSAA resolve operation at
the end of a pass).
A typical sg_pass_action object which clears the color attachment to black
might look like this:
const sg_pass_action = {
.colors[0] = {
.load_action = SG_LOADACTION_CLEAR,
.clear_value = { 0.0f, 0.0f, 0.0f, 1.0f }
}
};
This omits the defaults for the color attachment store action, and
the depth-stencil-attachments actions. The same pass action with the
defaults explicitly filled in would look like this:
const sg_pass_action pass_action = {
.colors[0] = {
.load_action = SG_LOADACTION_CLEAR,
.store_action = SG_STOREACTION_STORE,
.clear_value = { 0.0f, 0.0f, 0.0f, 1.0f }
},
.depth = = {
.load_action = SG_LOADACTION_CLEAR,
.store_action = SG_STOREACTION_DONTCARE,
.clear_value = 1.0f,
},
.stencil = {
.load_action = SG_LOADACTION_CLEAR,
.store_action = SG_STOREACTION_DONTCARE,
.clear_value = 0
}
};
With the sg_pass object and sg_pass_action struct in place everything
is ready now for the actual render pass:
Using such this prepared sg_pass_action in a swapchain pass looks like
this:
sg_begin_pass(&(sg_pass){
.action = pass_action,
.swapchain = sglue_swapchain()
});
...
sg_end_pass();
...of alternatively in one offscreen pass:
sg_begin_pass(&(sg_pass){
.action = pass_action,
.attachments = attachments,
});
...
sg_end_pass();
Offscreen rendering can also go into a mipmap, or a slice/face of
a cube-, array- or 3d-image (which some restrictions, for instance
it's not possible to create a 3D image with a depth/stencil pixel format,
these exceptions are generally caught by the sokol-gfx validation layer).
The mipmap/slice selection happens at attachments creation time, for instance
to render into mipmap 2 of slice 3 of an array texture:
const sg_attachments atts = sg_make_attachments(&(sg_attachments_desc){
.colors[0] = {
.image = color_img,
.mip_level = 2,
.slice = 3,
},
.depth_stencil.image = depth_img,
});
If MSAA offscreen rendering is desired, the multi-sample rendering result
must be 'resolved' into a separate 'resolve image', before that image can
be used as texture.
NOTE: currently multisample-images cannot be bound as textures.
Creating a simple attachments object for multisampled rendering requires
3 attachment images: the color attachment image which has a sample
count > 1, a resolve attachment image of the same size and pixel format
but a sample count == 1, and a depth/stencil attachment image with
the same size and sample count as the color attachment image:
const sg_image color_img = sg_make_image(&(sg_image_desc){
.render_target = true,
.width = 256,
.height = 256,
.pixel_format = SG_PIXELFORMAT_RGBA8,
.sample_count = 4,
});
const sg_image resolve_img = sg_make_image(&(sg_image_desc){
.render_target = true,
.width = 256,
.height = 256,
.pixel_format = SG_PIXELFORMAT_RGBA8,
.sample_count = 1,
});
const sg_image depth_img = sg_make_image(&(sg_image_desc){
.render_target = true,
.width = 256,
.height = 256,
.pixel_format = SG_PIXELFORMAT_DEPTH,
.sample_count = 4,
});
...create the attachments object:
const sg_attachments atts = sg_make_attachments(&(sg_attachments_desc){
.colors[0].image = color_img,
.resolves[0].image = resolve_img,
.depth_stencil.image = depth_img,
});
If an attachments object defines a resolve image in a specific resolve attachment slot,
an 'msaa resolve operation' will happen in sg_end_pass().
In this scenario, the content of the MSAA color attachment doesn't need to be
preserved (since it's only needed inside sg_end_pass for the msaa-resolve), so
the .store_action should be set to "don't care":
const sg_pass_action = {
.colors[0] = {
.load_action = SG_LOADACTION_CLEAR,
.store_action = SG_STOREACTION_DONTCARE,
.clear_value = { 0.0f, 0.0f, 0.0f, 1.0f }
}
};
The actual render pass looks as usual:
sg_begin_pass(&(sg_pass){ .action = pass_action, .attachments = atts });
...
sg_end_pass();
...after sg_end_pass() the only difference to the non-msaa scenario is that the
rendering result which is going to be used as texture in a followup pass is
in 'resolve_img', not in 'color_img' (in fact, trying to bind color_img as a
texture would result in a validation error).
ON SHADER CREATION
==================
sokol-gfx doesn't come with an integrated shader cross-compiler, instead
backend-specific shader sources or binary blobs need to be provided when
creating a shader object, along with information about the shader resource
binding interface needed in the sokol-gfx validation layer and to properly
bind shader resources on the CPU-side to be consumable by the GPU-side.
The easiest way to provide all this shader creation data is to use the
sokol-shdc shader compiler tool to compile shaders from a common
GLSL syntax into backend-specific sources or binary blobs, along with
shader interface information and uniform blocks mapped to C structs.
To create a shader using a C header which has been code-generated by sokol-shdc:
// include the C header code-generated by sokol-shdc:
#include "myshader.glsl.h"
...
// create shader using a code-generated helper function from the C header:
sg_shader shd = sg_make_shader(myshader_shader_desc(sg_query_backend()));
The samples in the 'sapp' subdirectory of the sokol-samples project
also use the sokol-shdc approach:
https://github.com/floooh/sokol-samples/tree/master/sapp
If you're planning to use sokol-shdc, you can stop reading here, instead
continue with the sokol-shdc documentation:
https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md
To create shaders with backend-specific shader code or binary blobs,
the sg_make_shader() function requires the following information:
- Shader code or shader binary blobs for the vertex- and fragment- shader-stage:
- for the desktop GL backend, source code can be provided in '#version 410' or
'#version 430', version 430 is required for storage buffer support, but note
that this is not available on macOS
- for the GLES3 backend, source code must be provided in '#version 300 es' syntax
- for the D3D11 backend, shaders can be provided as source or binary blobs, the
source code should be in HLSL4.0 (for best compatibility) or alternatively
in HLSL5.0 syntax (other versions may work but are not tested), NOTE: when
shader source code is provided for the D3D11 backend, sokol-gfx will dynamically
load 'd3dcompiler_47.dll'
- for the Metal backends, shaders can be provided as source or binary blobs, the
MSL version should be in 'metal-1.1' (other versions may work but are not tested)
- for the WebGPU backend, shader must be provided as WGSL source code
- optionally the following shader-code related attributes can be provided:
- an entry function name (only on D3D11 or Metal, but not OpenGL)
- on D3D11 only, a compilation target (default is "vs_4_0" and "ps_4_0")
- Depending on backend, information about the input vertex attributes used by the
vertex shader:
- Metal: no information needed since vertex attributes are always bound
by their attribute location defined in the shader via '[[attribute(N)]]'
- WebGPU: no information needed since vertex attributes are always
bound by their attribute location defined in the shader via `@location(N)`
- GLSL: vertex attribute names can be optionally provided, in that case their
location will be looked up by name, otherwise, the vertex attribute location
can be defined with 'layout(location = N)', PLEASE NOTE that the name-lookup method
may be removed at some point
- D3D11: a 'semantic name' and 'semantic index' must be provided for each vertex
attribute, e.g. if the vertex attribute is defined as 'TEXCOORD1' in the shader,
the semantic name would be 'TEXCOORD', and the semantic index would be '1'
- Information about each uniform block used in the shader:
- The size of the uniform block in number of bytes.
- A memory layout hint (currently 'native' or 'std140') where 'native' defines a
backend-specific memory layout which shouldn't be used for cross-platform code.
Only std140 guarantees a backend-agnostic memory layout.
- For GLSL only: a description of the internal uniform block layout, which maps
member types and their offsets on the CPU side to uniform variable names
in the GLSL shader
- please also NOTE the documentation sections about UNIFORM DATA LAYOUT
and CROSS-BACKEND COMMON UNIFORM DATA LAYOUT below!
- A description of each storage buffer used in the shader:
- a boolean 'readonly' flag, note that currently only
readonly storage buffers are supported
- note that storage buffers are not supported on all backends
and platforms
- A description of each texture/image used in the shader:
- the expected image type:
- SG_IMAGETYPE_2D
- SG_IMAGETYPE_CUBE
- SG_IMAGETYPE_3D
- SG_IMAGETYPE_ARRAY
- the expected 'image sample type':
- SG_IMAGESAMPLETYPE_FLOAT
- SG_IMAGESAMPLETYPE_DEPTH
- SG_IMAGESAMPLETYPE_SINT
- SG_IMAGESAMPLETYPE_UINT
- SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT
- a flag whether the texture is expected to be multisampled
(currently it's not supported to fetch data from multisampled
textures in shaders, but this is planned for a later time)
- A description of each texture sampler used in the shader:
- SG_SAMPLERTYPE_FILTERING,
- SG_SAMPLERTYPE_NONFILTERING,
- SG_SAMPLERTYPE_COMPARISON,
- An array of 'image-sampler-pairs' used by the shader to sample textures,
for D3D11, Metal and WebGPU this is used for validation purposes to check
whether the texture and sampler are compatible with each other (especially
WebGPU is very picky about combining the correct
texture-sample-type with the correct sampler-type). For GLSL an
additional 'combined-image-sampler name' must be provided because 'OpenGL
style GLSL' cannot handle separate texture and sampler objects, but still
groups them into a traditional GLSL 'sampler object'.
Compatibility rules for image-sample-type vs sampler-type are as follows:
- SG_IMAGESAMPLETYPE_FLOAT => (SG_SAMPLERTYPE_FILTERING or SG_SAMPLERTYPE_NONFILTERING)
- SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT => SG_SAMPLERTYPE_NONFILTERING
- SG_IMAGESAMPLETYPE_SINT => SG_SAMPLERTYPE_NONFILTERING
- SG_IMAGESAMPLETYPE_UINT => SG_SAMPLERTYPE_NONFILTERING
- SG_IMAGESAMPLETYPE_DEPTH => SG_SAMPLERTYPE_COMPARISON
For example code of how to create backend-specific shader objects,
please refer to the following samples:
- for D3D11: https://github.com/floooh/sokol-samples/tree/master/d3d11
- for Metal: https://github.com/floooh/sokol-samples/tree/master/metal
- for OpenGL: https://github.com/floooh/sokol-samples/tree/master/glfw
- for GLES3: https://github.com/floooh/sokol-samples/tree/master/html5
- for WebGPI: https://github.com/floooh/sokol-samples/tree/master/wgpu
ON SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT AND SG_SAMPLERTYPE_NONFILTERING
========================================================================
The WebGPU backend introduces the concept of 'unfilterable-float' textures,
which can only be combined with 'nonfiltering' samplers (this is a restriction
specific to WebGPU, but since the same sokol-gfx code should work across
all backend, the sokol-gfx validation layer also enforces this restriction
- the alternative would be undefined behaviour in some backend APIs on
some devices).
The background is that some mobile devices (most notably iOS devices) can
not perform linear filtering when sampling textures with certain pixel
formats, most notable the 32F formats:
- SG_PIXELFORMAT_R32F
- SG_PIXELFORMAT_RG32F
- SG_PIXELFORMAT_RGBA32F
The information of whether a shader is going to be used with such an
unfilterable-float texture must already be provided in the sg_shader_desc
struct when creating the shader (see the above section "ON SHADER CREATION").
If you are using the sokol-shdc shader compiler, the information whether a
texture/sampler binding expects an 'unfilterable-float/nonfiltering'
texture/sampler combination cannot be inferred from the shader source
alone, you'll need to provide this hint via annotation-tags. For instance
here is an example from the ozz-skin-sapp.c sample shader which samples an
RGBA32F texture with skinning matrices in the vertex shader:
```glsl
@image_sample_type joint_tex unfilterable_float
uniform texture2D joint_tex;
@sampler_type smp nonfiltering
uniform sampler smp;
```
This will result in SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT and
SG_SAMPLERTYPE_NONFILTERING being written to the code-generated
sg_shader_desc struct.
UNIFORM DATA LAYOUT:
====================
NOTE: if you use the sokol-shdc shader compiler tool, you don't need to worry
about the following details.
The data that's passed into the sg_apply_uniforms() function must adhere to
specific layout rules so that the GPU shader finds the uniform block
items at the right offset.
For the D3D11 and Metal backends, sokol-gfx only cares about the size of uniform
blocks, but not about the internal layout. The data will just be copied into
a uniform/constant buffer in a single operation and it's up you to arrange the
CPU-side layout so that it matches the GPU side layout. This also means that with
the D3D11 and Metal backends you are not limited to a 'cross-platform' subset
of uniform variable types.
If you ever only use one of the D3D11, Metal *or* WebGPU backend, you can stop reading here.
For the GL backends, the internal layout of uniform blocks matters though,
and you are limited to a small number of uniform variable types. This is
because sokol-gfx must be able to locate the uniform block members in order
to upload them to the GPU with glUniformXXX() calls.
To describe the uniform block layout to sokol-gfx, the following information
must be passed to the sg_make_shader() call in the sg_shader_desc struct:
- a hint about the used packing rule (either SG_UNIFORMLAYOUT_NATIVE or
SG_UNIFORMLAYOUT_STD140)
- a list of the uniform block members types in the correct order they
appear on the CPU side
For example if the GLSL shader has the following uniform declarations:
uniform mat4 mvp;
uniform vec2 offset0;
uniform vec2 offset1;
uniform vec2 offset2;
...and on the CPU side, there's a similar C struct:
typedef struct {
float mvp[16];
float offset0[2];
float offset1[2];
float offset2[2];
} params_t;
...the uniform block description in the sg_shader_desc must look like this:
sg_shader_desc desc = {
.vs.uniform_blocks[0] = {
.size = sizeof(params_t),
.layout = SG_UNIFORMLAYOUT_NATIVE, // this is the default and can be omitted
.uniforms = {
// order must be the same as in 'params_t':
[0] = { .name = "mvp", .type = SG_UNIFORMTYPE_MAT4 },
[1] = { .name = "offset0", .type = SG_UNIFORMTYPE_VEC2 },
[2] = { .name = "offset1", .type = SG_UNIFORMTYPE_VEC2 },
[3] = { .name = "offset2", .type = SG_UNIFORMTYPE_VEC2 },
}
}
};
With this information sokol-gfx can now compute the correct offsets of the data items
within the uniform block struct.
The SG_UNIFORMLAYOUT_NATIVE packing rule works fine if only the GL backends are used,
but for proper D3D11/Metal/GL a subset of the std140 layout must be used which is
described in the next section:
CROSS-BACKEND COMMON UNIFORM DATA LAYOUT
========================================
For cross-platform / cross-3D-backend code it is important that the same uniform block
layout on the CPU side can be used for all sokol-gfx backends. To achieve this,
a common subset of the std140 layout must be used:
- The uniform block layout hint in sg_shader_desc must be explicitly set to
SG_UNIFORMLAYOUT_STD140.
- Only the following GLSL uniform types can be used (with their associated sokol-gfx enums):
- float => SG_UNIFORMTYPE_FLOAT
- vec2 => SG_UNIFORMTYPE_FLOAT2
- vec3 => SG_UNIFORMTYPE_FLOAT3
- vec4 => SG_UNIFORMTYPE_FLOAT4
- int => SG_UNIFORMTYPE_INT
- ivec2 => SG_UNIFORMTYPE_INT2
- ivec3 => SG_UNIFORMTYPE_INT3
- ivec4 => SG_UNIFORMTYPE_INT4
- mat4 => SG_UNIFORMTYPE_MAT4
- Alignment for those types must be as follows (in bytes):
- float => 4
- vec2 => 8
- vec3 => 16
- vec4 => 16
- int => 4
- ivec2 => 8
- ivec3 => 16
- ivec4 => 16
- mat4 => 16
- Arrays are only allowed for the following types: vec4, int4, mat4.
Note that the HLSL cbuffer layout rules are slightly different from the
std140 layout rules, this means that the cbuffer declarations in HLSL code
must be tweaked so that the layout is compatible with std140.
The by far easiest way to tackle the common uniform block layout problem is
to use the sokol-shdc shader cross-compiler tool!
ON STORAGE BUFFERS
==================
Storage buffers can be used to pass large amounts of random access structured
data from the CPU side to the shaders. They are similar to data textures, but are
more convenient to use both on the CPU and shader side since they can be accessed
in shaders as as a 1-dimensional array of struct items.
Storage buffers are *NOT* supported on the following platform/backend combos:
- macOS+GL (because storage buffers require GL 4.3, while macOS only goes up to GL 4.1)
- all GLES3 platforms (WebGL2, iOS, Android - with the option that support on
Android may be added at a later point)
Currently only 'readonly' storage buffers are supported (meaning it's not possible
to write to storage buffers from shaders).
To use storage buffers, the following steps are required:
- write a shader which uses storage buffers (also see the example links below)
- create one or more storage buffers via sg_make_buffer() with the
buffer type SG_BUFFERTYPE_STORAGEBUFFER
- when creating a shader via sg_make_shader(), populate the sg_shader_desc
struct with binding info (when using sokol-shdc, this step will be taken care
of automatically)
- which storage buffer bind slots on the vertex- and fragment-stage
are occupied
- whether the storage buffer on that bind slot is readonly (this is currently required
to be true)
- when calling sg_apply_bindings(), apply the matching bind slots with the previously
created storage buffers
- ...and that's it.
For more details, see the following backend-agnostic sokol samples:
- simple vertex pulling from a storage buffer:
- C code: https://github.com/floooh/sokol-samples/blob/master/sapp/vertexpull-sapp.c
- shader: https://github.com/floooh/sokol-samples/blob/master/sapp/vertexpull-sapp.glsl
- instanced rendering via storage buffers (vertex- and instance-pulling):
- C code: https://github.com/floooh/sokol-samples/blob/master/sapp/instancing-pull-sapp.c
- shader: https://github.com/floooh/sokol-samples/blob/master/sapp/instancing-pull-sapp.glsl
- storage buffers both on the vertex- and fragment-stage:
- C code: https://github.com/floooh/sokol-samples/blob/master/sapp/sbuftex-sapp.c
- shader: https://github.com/floooh/sokol-samples/blob/master/sapp/sbuftex-sapp.glsl
- the Ozz animation sample rewritten to pull all rendering data from storage buffers:
- C code: https://github.com/floooh/sokol-samples/blob/master/sapp/ozz-storagebuffer-sapp.cc
- shader: https://github.com/floooh/sokol-samples/blob/master/sapp/ozz-storagebuffer-sapp.glsl
...also see the following backend-specific vertex pulling samples (those also don't use sokol-shdc):
- D3D11: https://github.com/floooh/sokol-samples/blob/master/d3d11/vertexpulling-d3d11.c
- desktop GL: https://github.com/floooh/sokol-samples/blob/master/glfw/vertexpulling-glfw.c
- Metal: https://github.com/floooh/sokol-samples/blob/master/metal/vertexpulling-metal.c
- WebGPU: https://github.com/floooh/sokol-samples/blob/master/wgpu/vertexpulling-wgpu.c
Storage buffer shader authoring caveats when using sokol-shdc:
- declare a storage buffer interface block with `readonly buffer [name] { ... }`
- do NOT annotate storage buffers with `layout(...)`, sokol-shdc will take care of that
- declare a struct which describes a single array item in the storage buffer interface block
- only put a single flexible array member into the storage buffer interface block
E.g. a complete example in 'sokol-shdc GLSL':
```glsl
// declare a struct:
struct sb_vertex {
vec3 pos;
vec4 color;
}
// declare a buffer interface block with a single flexible struct array:
readonly buffer vertices {
sb_vertex vtx[];
}
// in the shader function, access the storage buffer like this:
void main() {
vec3 pos = vtx[gl_VertexIndex].pos;
...
}
```
Backend-specific storage-buffer caveats (not relevant when using sokol-shdc):
D3D11:
- storage buffers are created as 'raw' Byte Address Buffers
(https://learn.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-intro#raw-views-of-buffers)
- in HLSL, use a ByteAddressBuffer to access the buffer content
(https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-byteaddressbuffer)
- in D3D11, storage buffers and textures share the same bind slots, sokol-gfx reserves
shader resource slots 0..15 for textures and 16..23 for storage buffers.
- e.g. in HLSL, storage buffer bindings start at register(t16) no matter the shader stage
Metal:
- in Metal there is no internal difference between vertex-, uniform- and
storage-buffers, all are bound to the same 'buffer bind slots' with the
following reserved ranges:
- vertex shader stage:
- uniform buffers (internal): slots 0..3
- vertex buffers: slots 4..11
- storage buffers: slots 12..19
- fragment shader stage:
- uniform buffers (internal): slots 0..3
- storage buffers: slots 4..11
- this means in MSL, storage buffer bindings start at [[buffer(12)]] in the vertex
shaders, and at [[buffer(4)]] in fragment shaders
GL:
- the GL backend doesn't use name-lookup to find storage buffer bindings, this
means you must annotate buffers with `layout(std430, binding=N)` in GLSL
- ...where N is 0..7 in the vertex shader, and 8..15 in the fragment shader
WebGPU:
- in WGSL, use the following bind locations for the various shader resource types:
- vertex shader stage:
- textures `@group(1) @binding(0..15)`
- samplers `@group(1) @binding(16..31)`
- storage buffers `@group(1) @binding(32..47)`
- fragment shader stage:
- textures `@group(1) @binding(48..63)`
- samplers `@group(1) @binding(64..79)`
- storage buffers `@group(1) @binding(80..95)`
TRACE HOOKS:
============
sokol_gfx.h optionally allows to install "trace hook" callbacks for
each public API functions. When a public API function is called, and
a trace hook callback has been installed for this function, the
callback will be invoked with the parameters and result of the function.
This is useful for things like debugging- and profiling-tools, or
keeping track of resource creation and destruction.
To use the trace hook feature:
--- Define SOKOL_TRACE_HOOKS before including the implementation.
--- Setup an sg_trace_hooks structure with your callback function
pointers (keep all function pointers you're not interested
in zero-initialized), optionally set the user_data member
in the sg_trace_hooks struct.
--- Install the trace hooks by calling sg_install_trace_hooks(),
the return value of this function is another sg_trace_hooks
struct which contains the previously set of trace hooks.
You should keep this struct around, and call those previous
functions pointers from your own trace callbacks for proper
chaining.
As an example of how trace hooks are used, have a look at the
imgui/sokol_gfx_imgui.h header which implements a realtime
debugging UI for sokol_gfx.h on top of Dear ImGui.
A NOTE ON PORTABLE PACKED VERTEX FORMATS:
=========================================
There are two things to consider when using packed
vertex formats like UBYTE4, SHORT2, etc which need to work
across all backends:
- D3D11 can only convert *normalized* vertex formats to
floating point during vertex fetch, normalized formats
have a trailing 'N', and are "normalized" to a range
-1.0..+1.0 (for the signed formats) or 0.0..1.0 (for the
unsigned formats):
- SG_VERTEXFORMAT_BYTE4N
- SG_VERTEXFORMAT_UBYTE4N
- SG_VERTEXFORMAT_SHORT2N
- SG_VERTEXFORMAT_USHORT2N
- SG_VERTEXFORMAT_SHORT4N
- SG_VERTEXFORMAT_USHORT4N
D3D11 will not convert *non-normalized* vertex formats to floating point
vertex shader inputs, those can only be uses with the *ivecn* vertex shader
input types when D3D11 is used as backend (GL and Metal can use both formats)
- SG_VERTEXFORMAT_BYTE4,
- SG_VERTEXFORMAT_UBYTE4
- SG_VERTEXFORMAT_SHORT2
- SG_VERTEXFORMAT_SHORT4
For a vertex input layout which works on all platforms, only use the following
vertex formats, and if needed "expand" the normalized vertex shader
inputs in the vertex shader by multiplying with 127.0, 255.0, 32767.0 or
65535.0:
- SG_VERTEXFORMAT_FLOAT,
- SG_VERTEXFORMAT_FLOAT2,
- SG_VERTEXFORMAT_FLOAT3,
- SG_VERTEXFORMAT_FLOAT4,
- SG_VERTEXFORMAT_BYTE4N,
- SG_VERTEXFORMAT_UBYTE4N,
- SG_VERTEXFORMAT_SHORT2N,
- SG_VERTEXFORMAT_USHORT2N
- SG_VERTEXFORMAT_SHORT4N,
- SG_VERTEXFORMAT_USHORT4N
- SG_VERTEXFORMAT_UINT10_N2
- SG_VERTEXFORMAT_HALF2
- SG_VERTEXFORMAT_HALF4
MEMORY ALLOCATION OVERRIDE
==========================
You can override the memory allocation functions at initialization time
like this:
void* my_alloc(size_t size, void* user_data) {
return malloc(size);
}
void my_free(void* ptr, void* user_data) {
free(ptr);
}
...
sg_setup(&(sg_desc){
// ...
.allocator = {
.alloc_fn = my_alloc,
.free_fn = my_free,
.user_data = ...,
}
});
...
If no overrides are provided, malloc and free will be used.
This only affects memory allocation calls done by sokol_gfx.h
itself though, not any allocations in OS libraries.
ERROR REPORTING AND LOGGING
===========================
To get any logging information at all you need to provide a logging callback in the setup call
the easiest way is to use sokol_log.h:
#include "sokol_log.h"
sg_setup(&(sg_desc){ .logger.func = slog_func });
To override logging with your own callback, first write a logging function like this:
void my_log(const char* tag, // e.g. 'sg'
uint32_t log_level, // 0=panic, 1=error, 2=warn, 3=info
uint32_t log_item_id, // SG_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_gfx.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data)
{
...
}
...and then setup sokol-gfx like this:
sg_setup(&(sg_desc){
.logger = {
.func = my_log,
.user_data = my_user_data,
}
});
The provided logging function must be reentrant (e.g. be callable from
different threads).
If you don't want to provide your own custom logger it is highly recommended to use
the standard logger in sokol_log.h instead, otherwise you won't see any warnings or
errors.
COMMIT LISTENERS
================
It's possible to hook callback functions into sokol-gfx which are called from
inside sg_commit() in unspecified order. This is mainly useful for libraries
that build on top of sokol_gfx.h to be notified about the end/start of a frame.
To add a commit listener, call:
static void my_commit_listener(void* user_data) {
...
}
bool success = sg_add_commit_listener((sg_commit_listener){
.func = my_commit_listener,
.user_data = ...,
});
The function returns false if the internal array of commit listeners is full,
or the same commit listener had already been added.
If the function returns true, my_commit_listener() will be called each frame
from inside sg_commit().
By default, 1024 distinct commit listeners can be added, but this number
can be tweaked in the sg_setup() call:
sg_setup(&(sg_desc){
.max_commit_listeners = 2048,
});
An sg_commit_listener item is equal to another if both the function
pointer and user_data field are equal.
To remove a commit listener:
bool success = sg_remove_commit_listener((sg_commit_listener){
.func = my_commit_listener,
.user_data = ...,
});
...where the .func and .user_data field are equal to a previous
sg_add_commit_listener() call. The function returns true if the commit
listener item was found and removed, and false otherwise.
RESOURCE CREATION AND DESTRUCTION IN DETAIL
===========================================
The 'vanilla' way to create resource objects is with the 'make functions':
sg_buffer sg_make_buffer(const sg_buffer_desc* desc)
sg_image sg_make_image(const sg_image_desc* desc)
sg_sampler sg_make_sampler(const sg_sampler_desc* desc)
sg_shader sg_make_shader(const sg_shader_desc* desc)
sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc)
sg_attachments sg_make_attachments(const sg_attachments_desc* desc)
This will result in one of three cases:
1. The returned handle is invalid. This happens when there are no more
free slots in the resource pool for this resource type. An invalid
handle is associated with the INVALID resource state, for instance:
sg_buffer buf = sg_make_buffer(...)
if (sg_query_buffer_state(buf) == SG_RESOURCESTATE_INVALID) {
// buffer pool is exhausted
}
2. The returned handle is valid, but creating the underlying resource
has failed for some reason. This results in a resource object in the
FAILED state. The reason *why* resource creation has failed differ
by resource type. Look for log messages with more details. A failed
resource state can be checked with:
sg_buffer buf = sg_make_buffer(...)
if (sg_query_buffer_state(buf) == SG_RESOURCESTATE_FAILED) {
// creating the resource has failed
}
3. And finally, if everything goes right, the returned resource is
in resource state VALID and ready to use. This can be checked
with:
sg_buffer buf = sg_make_buffer(...)
if (sg_query_buffer_state(buf) == SG_RESOURCESTATE_VALID) {
// creating the resource has failed
}
When calling the 'make functions', the created resource goes through a number
of states:
- INITIAL: the resource slot associated with the new resource is currently
free (technically, there is no resource yet, just an empty pool slot)
- ALLOC: a handle for the new resource has been allocated, this just means
a pool slot has been reserved.
- VALID or FAILED: in VALID state any 3D API backend resource objects have
been successfully created, otherwise if anything went wrong, the resource
will be in FAILED state.
Sometimes it makes sense to first grab a handle, but initialize the
underlying resource at a later time. For instance when loading data
asynchronously from a slow data source, you may know what buffers and
textures are needed at an early stage of the loading process, but actually
loading the buffer or texture content can only be completed at a later time.
For such situations, sokol-gfx resource objects can be created in two steps.
You can allocate a handle upfront with one of the 'alloc functions':
sg_buffer sg_alloc_buffer(void)
sg_image sg_alloc_image(void)
sg_sampler sg_alloc_sampler(void)
sg_shader sg_alloc_shader(void)
sg_pipeline sg_alloc_pipeline(void)
sg_attachments sg_alloc_attachments(void)
This will return a handle with the underlying resource object in the
ALLOC state:
sg_image img = sg_alloc_image();
if (sg_query_image_state(img) == SG_RESOURCESTATE_ALLOC) {
// allocating an image handle has succeeded, otherwise
// the image pool is full
}
Such an 'incomplete' handle can be used in most sokol-gfx rendering functions
without doing any harm, sokol-gfx will simply skip any rendering operation
that involve resources which are not in VALID state.
At a later time (for instance once the texture has completed loading
asynchronously), the resource creation can be completed by calling one of
the 'init functions', those functions take an existing resource handle and
'desc struct':
void sg_init_buffer(sg_buffer buf, const sg_buffer_desc* desc)
void sg_init_image(sg_image img, const sg_image_desc* desc)
void sg_init_sampler(sg_sampler smp, const sg_sampler_desc* desc)
void sg_init_shader(sg_shader shd, const sg_shader_desc* desc)
void sg_init_pipeline(sg_pipeline pip, const sg_pipeline_desc* desc)
void sg_init_attachments(sg_attachments atts, const sg_attachments_desc* desc)
The init functions expect a resource in ALLOC state, and after the function
returns, the resource will be either in VALID or FAILED state. Calling
an 'alloc function' followed by the matching 'init function' is fully
equivalent with calling the 'make function' alone.
Destruction can also happen as a two-step process. The 'uninit functions'
will put a resource object from the VALID or FAILED state back into the
ALLOC state:
void sg_uninit_buffer(sg_buffer buf)
void sg_uninit_image(sg_image img)
void sg_uninit_sampler(sg_sampler smp)
void sg_uninit_shader(sg_shader shd)
void sg_uninit_pipeline(sg_pipeline pip)
void sg_uninit_attachments(sg_attachments pass)
Calling the 'uninit functions' with a resource that is not in the VALID or
FAILED state is a no-op.
To finally free the pool slot for recycling call the 'dealloc functions':
void sg_dealloc_buffer(sg_buffer buf)
void sg_dealloc_image(sg_image img)
void sg_dealloc_sampler(sg_sampler smp)
void sg_dealloc_shader(sg_shader shd)
void sg_dealloc_pipeline(sg_pipeline pip)
void sg_dealloc_attachments(sg_attachments atts)
Calling the 'dealloc functions' on a resource that's not in ALLOC state is
a no-op, but will generate a warning log message.
Calling an 'uninit function' and 'dealloc function' in sequence is equivalent
with calling the associated 'destroy function':
void sg_destroy_buffer(sg_buffer buf)
void sg_destroy_image(sg_image img)
void sg_destroy_sampler(sg_sampler smp)
void sg_destroy_shader(sg_shader shd)
void sg_destroy_pipeline(sg_pipeline pip)
void sg_destroy_attachments(sg_attachments atts)
The 'destroy functions' can be called on resources in any state and generally
do the right thing (for instance if the resource is in ALLOC state, the destroy
function will be equivalent to the 'dealloc function' and skip the 'uninit part').
And finally to close the circle, the 'fail functions' can be called to manually
put a resource in ALLOC state into the FAILED state:
sg_fail_buffer(sg_buffer buf)
sg_fail_image(sg_image img)
sg_fail_sampler(sg_sampler smp)
sg_fail_shader(sg_shader shd)
sg_fail_pipeline(sg_pipeline pip)
sg_fail_attachments(sg_attachments atts)
This is recommended if anything went wrong outside of sokol-gfx during asynchronous
resource setup (for instance a file loading operation failed). In this case,
the 'fail function' should be called instead of the 'init function'.
Calling a 'fail function' on a resource that's not in ALLOC state is a no-op,
but will generate a warning log message.
NOTE: that two-step resource creation usually only makes sense for buffers
and images, but not for samplers, shaders, pipelines or attachments. Most notably, trying
to create a pipeline object with a shader that's not in VALID state will
trigger a validation layer error, or if the validation layer is disabled,
result in a pipeline object in FAILED state. Same when trying to create
an attachments object with invalid image objects.
WEBGPU CAVEATS
==============
For a general overview and design notes of the WebGPU backend see:
https://floooh.github.io/2023/10/16/sokol-webgpu.html
In general, don't expect an automatic speedup when switching from the WebGL2
backend to the WebGPU backend. Some WebGPU functions currently actually
have a higher CPU overhead than similar WebGL2 functions, leading to the
paradoxical situation that some WebGPU code may be slower than similar WebGL2
code.
- when writing WGSL shader code by hand, a specific bind-slot convention
must be used:
All uniform block structs must use `@group(0)`, with up to
4 uniform blocks per shader stage.
- Vertex shader uniform block bindings must start at `@group(0) @binding(0)`
- Fragment shader uniform blocks bindings must start at `@group(0) @binding(4)`
All textures and samplers must use `@group(1)` and start at specific
offsets depending on resource type and shader stage.
- Vertex shader textures must start at `@group(1) @binding(0)`
- Vertex shader samplers must start at `@group(1) @binding(16)`
- Vertex shader storage buffers must start at `@group(1) @binding(32)`
- Fragment shader textures must start at `@group(1) @binding(48)`
- Fragment shader samplers must start at `@group(1) @binding(64)`
- Fragment shader storage buffers must start at `@group(1) @binding(80)`
Note that the actual number of allowed per-stage texture- and sampler-bindings
in sokol-gfx is currently lower than the above ranges (currently only up to
12 textures, 8 samplers and 8 storage buffers are allowed per shader stage).
If you use sokol-shdc to generate WGSL shader code, you don't need to worry
about the above binding convention since sokol-shdc assigns bind slots
automatically.
- The sokol-gfx WebGPU backend uses the sg_desc.uniform_buffer_size item
to allocate a single per-frame uniform buffer which must be big enough
to hold all data written by sg_apply_uniforms() during a single frame,
including a worst-case 256-byte alignment (e.g. each sg_apply_uniform
call will cost 256 bytes of uniform buffer size). The default size
is 4 MB, which is enough for 16384 sg_apply_uniform() calls per
frame (assuming the uniform data 'payload' is less than 256 bytes
per call). These rules are the same as for the Metal backend, so if
you are already using the Metal backend you'll be fine.
- sg_apply_bindings(): the sokol-gfx WebGPU backend implements a bindgroup
cache to prevent excessive creation and destruction of BindGroup objects
when calling sg_apply_bindings(). The number of slots in the bindgroups
cache is defined in sg_desc.wgpu_bindgroups_cache_size when calling
sg_setup. The cache size must be a power-of-2 number, with the default being
1024. The bindgroups cache behaviour can be observed by calling the new
function sg_query_frame_stats(), where the following struct items are
of interest:
.wgpu.num_bindgroup_cache_hits
.wgpu.num_bindgroup_cache_misses
.wgpu.num_bindgroup_cache_collisions
.wgpu_num_bindgroup_cache_invalidates
.wgpu.num_bindgroup_cache_vs_hash_key_mismatch
The value to pay attention to is `.wgpu.num_bindgroup_cache_collisions`,
if this number if consistently higher than a few percent of the
.wgpu.num_set_bindgroup value, it might be a good idea to bump the
bindgroups cache size to the next power-of-2.
- sg_apply_viewport(): WebGPU currently has a unique restriction that viewport
rectangles must be contained entirely within the framebuffer. As a shitty
workaround sokol_gfx.h will clip incoming viewport rectangles against
the framebuffer, but this will distort the clipspace-to-screenspace mapping.
There's no proper way to handle this inside sokol_gfx.h, this must be fixed
in a future WebGPU update.
- The sokol shader compiler generally adds `diagnostic(off, derivative_uniformity);`
into the WGSL output. Currently only the Chrome WebGPU implementation seems
to recognize this.
- The vertex format SG_VERTEXFORMAT_UINT10_N2 is currently not supported because
WebGPU lacks a matching vertex format (this is currently being worked on though,
as soon as the vertex format shows up in webgpu.h, sokol_gfx.h will add support.
- Likewise, the following sokol-gfx vertex formats are not supported in WebGPU:
R16, R16SN, RG16, RG16SN, RGBA16, RGBA16SN and all PVRTC compressed format.
Unlike unsupported vertex formats, unsupported pixel formats can be queried
in cross-backend code via sg_query_pixel_format() though.
- The Emscripten WebGPU shim currently doesn't support the Closure minification
post-link-step (e.g. currently the emcc argument '--closure 1' or '--closure 2'
will generate broken Javascript code.
- sokol-gfx requires the WebGPU device feature `depth32float-stencil8` to be enabled
(this should be widely supported)
- sokol-gfx expects that the WebGPU device feature `float32-filterable` to *not* be
enabled (since this would exclude all iOS devices)
LICENSE
=======
zlib/libpng license
Copyright (c) 2018 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define SOKOL_GFX_INCLUDED (1)
#include <stddef.h> // size_t
#include <stdint.h>
#include <stdbool.h>
#if defined(SOKOL_API_DECL) && !defined(SOKOL_GFX_API_DECL)
#define SOKOL_GFX_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_GFX_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_GFX_IMPL)
#define SOKOL_GFX_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_GFX_API_DECL __declspec(dllimport)
#else
#define SOKOL_GFX_API_DECL extern
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
Resource id typedefs:
sg_buffer: vertex- and index-buffers
sg_image: images used as textures and render targets
sg_sampler sampler object describing how a texture is sampled in a shader
sg_shader: vertex- and fragment-shaders and shader interface information
sg_pipeline: associated shader and vertex-layouts, and render states
sg_attachments: a baked collection of render pass attachment images
Instead of pointers, resource creation functions return a 32-bit
number which uniquely identifies the resource object.
The 32-bit resource id is split into a 16-bit pool index in the lower bits,
and a 16-bit 'generation counter' in the upper bits. The index allows fast
pool lookups, and combined with the generation-counter it allows to detect
'dangling accesses' (trying to use an object which no longer exists, and
its pool slot has been reused for a new object)
The resource ids are wrapped into a strongly-typed struct so that
trying to pass an incompatible resource id is a compile error.
*/
typedef struct sg_buffer { uint32_t id; } sg_buffer;
typedef struct sg_image { uint32_t id; } sg_image;
typedef struct sg_sampler { uint32_t id; } sg_sampler;
typedef struct sg_shader { uint32_t id; } sg_shader;
typedef struct sg_pipeline { uint32_t id; } sg_pipeline;
typedef struct sg_attachments { uint32_t id; } sg_attachments;
/*
sg_range is a pointer-size-pair struct used to pass memory blobs into
sokol-gfx. When initialized from a value type (array or struct), you can
use the SG_RANGE() macro to build an sg_range struct. For functions which
take either a sg_range pointer, or a (C++) sg_range reference, use the
SG_RANGE_REF macro as a solution which compiles both in C and C++.
*/
typedef struct sg_range {
const void* ptr;
size_t size;
} sg_range;
// disabling this for every includer isn't great, but the warnings are also quite pointless
#if defined(_MSC_VER)
#pragma warning(disable:4221) // /W4 only: nonstandard extension used: 'x': cannot be initialized using address of automatic variable 'y'
#pragma warning(disable:4204) // VS2015: nonstandard extension used: non-constant aggregate initializer
#endif
#if defined(__cplusplus)
#define SG_RANGE(x) sg_range{ &x, sizeof(x) }
#define SG_RANGE_REF(x) sg_range{ &x, sizeof(x) }
#else
#define SG_RANGE(x) (sg_range){ &x, sizeof(x) }
#define SG_RANGE_REF(x) &(sg_range){ &x, sizeof(x) }
#endif
// various compile-time constants
enum {
SG_INVALID_ID = 0,
SG_NUM_SHADER_STAGES = 2,
SG_NUM_INFLIGHT_FRAMES = 2,
SG_MAX_COLOR_ATTACHMENTS = 4,
SG_MAX_VERTEX_BUFFERS = 8,
SG_MAX_SHADERSTAGE_IMAGES = 12,
SG_MAX_SHADERSTAGE_SAMPLERS = 8,
SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS = 12,
SG_MAX_SHADERSTAGE_STORAGEBUFFERS = 8,
SG_MAX_SHADERSTAGE_UBS = 4,
SG_MAX_UB_MEMBERS = 16,
SG_MAX_VERTEX_ATTRIBUTES = 16,
SG_MAX_MIPMAPS = 16,
SG_MAX_TEXTUREARRAY_LAYERS = 128
};
/*
sg_color
An RGBA color value.
*/
typedef struct sg_color { float r, g, b, a; } sg_color;
/*
sg_backend
The active 3D-API backend, use the function sg_query_backend()
to get the currently active backend.
*/
typedef enum sg_backend {
SG_BACKEND_GLCORE,
SG_BACKEND_GLES3,
SG_BACKEND_D3D11,
SG_BACKEND_METAL_IOS,
SG_BACKEND_METAL_MACOS,
SG_BACKEND_METAL_SIMULATOR,
SG_BACKEND_WGPU,
SG_BACKEND_DUMMY,
} sg_backend;
/*
sg_pixel_format
sokol_gfx.h basically uses the same pixel formats as WebGPU, since these
are supported on most newer GPUs.
A pixelformat name consist of three parts:
- components (R, RG, RGB or RGBA)
- bit width per component (8, 16 or 32)
- component data type:
- unsigned normalized (no postfix)
- signed normalized (SN postfix)
- unsigned integer (UI postfix)
- signed integer (SI postfix)
- float (F postfix)
Not all pixel formats can be used for everything, call sg_query_pixelformat()
to inspect the capabilities of a given pixelformat. The function returns
an sg_pixelformat_info struct with the following members:
- sample: the pixelformat can be sampled as texture at least with
nearest filtering
- filter: the pixelformat can be samples as texture with linear
filtering
- render: the pixelformat can be used for render targets
- blend: blending is supported when using the pixelformat for
render targets
- msaa: multisample-antialiasing is supported when using the
pixelformat for render targets
- depth: the pixelformat can be used for depth-stencil attachments
- compressed: this is a block-compressed format
- bytes_per_pixel: the numbers of bytes in a pixel (0 for compressed formats)
The default pixel format for texture images is SG_PIXELFORMAT_RGBA8.
The default pixel format for render target images is platform-dependent
and taken from the sg_environment struct passed into sg_setup(). Typically
the default formats are:
- for the Metal, D3D11 and WebGPU backends: SG_PIXELFORMAT_BGRA8
- for GL backends: SG_PIXELFORMAT_RGBA8
*/
typedef enum sg_pixel_format {
_SG_PIXELFORMAT_DEFAULT, // value 0 reserved for default-init
SG_PIXELFORMAT_NONE,
SG_PIXELFORMAT_R8,
SG_PIXELFORMAT_R8SN,
SG_PIXELFORMAT_R8UI,
SG_PIXELFORMAT_R8SI,
SG_PIXELFORMAT_R16,
SG_PIXELFORMAT_R16SN,
SG_PIXELFORMAT_R16UI,
SG_PIXELFORMAT_R16SI,
SG_PIXELFORMAT_R16F,
SG_PIXELFORMAT_RG8,
SG_PIXELFORMAT_RG8SN,
SG_PIXELFORMAT_RG8UI,
SG_PIXELFORMAT_RG8SI,
SG_PIXELFORMAT_R32UI,
SG_PIXELFORMAT_R32SI,
SG_PIXELFORMAT_R32F,
SG_PIXELFORMAT_RG16,
SG_PIXELFORMAT_RG16SN,
SG_PIXELFORMAT_RG16UI,
SG_PIXELFORMAT_RG16SI,
SG_PIXELFORMAT_RG16F,
SG_PIXELFORMAT_RGBA8,
SG_PIXELFORMAT_SRGB8A8,
SG_PIXELFORMAT_RGBA8SN,
SG_PIXELFORMAT_RGBA8UI,
SG_PIXELFORMAT_RGBA8SI,
SG_PIXELFORMAT_BGRA8,
SG_PIXELFORMAT_RGB10A2,
SG_PIXELFORMAT_RG11B10F,
SG_PIXELFORMAT_RGB9E5,
SG_PIXELFORMAT_RG32UI,
SG_PIXELFORMAT_RG32SI,
SG_PIXELFORMAT_RG32F,
SG_PIXELFORMAT_RGBA16,
SG_PIXELFORMAT_RGBA16SN,
SG_PIXELFORMAT_RGBA16UI,
SG_PIXELFORMAT_RGBA16SI,
SG_PIXELFORMAT_RGBA16F,
SG_PIXELFORMAT_RGBA32UI,
SG_PIXELFORMAT_RGBA32SI,
SG_PIXELFORMAT_RGBA32F,
// NOTE: when adding/removing pixel formats before DEPTH, also update sokol_app.h/_SAPP_PIXELFORMAT_*
SG_PIXELFORMAT_DEPTH,
SG_PIXELFORMAT_DEPTH_STENCIL,
// NOTE: don't put any new compressed format in front of here
SG_PIXELFORMAT_BC1_RGBA,
SG_PIXELFORMAT_BC2_RGBA,
SG_PIXELFORMAT_BC3_RGBA,
SG_PIXELFORMAT_BC3_SRGBA,
SG_PIXELFORMAT_BC4_R,
SG_PIXELFORMAT_BC4_RSN,
SG_PIXELFORMAT_BC5_RG,
SG_PIXELFORMAT_BC5_RGSN,
SG_PIXELFORMAT_BC6H_RGBF,
SG_PIXELFORMAT_BC6H_RGBUF,
SG_PIXELFORMAT_BC7_RGBA,
SG_PIXELFORMAT_BC7_SRGBA,
SG_PIXELFORMAT_PVRTC_RGB_2BPP, // FIXME: deprecated
SG_PIXELFORMAT_PVRTC_RGB_4BPP, // FIXME: deprecated
SG_PIXELFORMAT_PVRTC_RGBA_2BPP, // FIXME: deprecated
SG_PIXELFORMAT_PVRTC_RGBA_4BPP, // FIXME: deprecated
SG_PIXELFORMAT_ETC2_RGB8,
SG_PIXELFORMAT_ETC2_SRGB8,
SG_PIXELFORMAT_ETC2_RGB8A1,
SG_PIXELFORMAT_ETC2_RGBA8,
SG_PIXELFORMAT_ETC2_SRGB8A8,
SG_PIXELFORMAT_EAC_R11,
SG_PIXELFORMAT_EAC_R11SN,
SG_PIXELFORMAT_EAC_RG11,
SG_PIXELFORMAT_EAC_RG11SN,
SG_PIXELFORMAT_ASTC_4x4_RGBA,
SG_PIXELFORMAT_ASTC_4x4_SRGBA,
_SG_PIXELFORMAT_NUM,
_SG_PIXELFORMAT_FORCE_U32 = 0x7FFFFFFF
} sg_pixel_format;
/*
Runtime information about a pixel format, returned
by sg_query_pixelformat().
*/
typedef struct sg_pixelformat_info {
bool sample; // pixel format can be sampled in shaders at least with nearest filtering
bool filter; // pixel format can be sampled with linear filtering
bool render; // pixel format can be used as render target
bool blend; // alpha-blending is supported
bool msaa; // pixel format can be used as MSAA render target
bool depth; // pixel format is a depth format
bool compressed; // true if this is a hardware-compressed format
int bytes_per_pixel; // NOTE: this is 0 for compressed formats, use sg_query_row_pitch() / sg_query_surface_pitch() as alternative
} sg_pixelformat_info;
/*
Runtime information about available optional features,
returned by sg_query_features()
*/
typedef struct sg_features {
bool origin_top_left; // framebuffer and texture origin is in top left corner
bool image_clamp_to_border; // border color and clamp-to-border UV-wrap mode is supported
bool mrt_independent_blend_state; // multiple-render-target rendering can use per-render-target blend state
bool mrt_independent_write_mask; // multiple-render-target rendering can use per-render-target color write masks
bool storage_buffer; // storage buffers are supported
} sg_features;
/*
Runtime information about resource limits, returned by sg_query_limit()
*/
typedef struct sg_limits {
int max_image_size_2d; // max width/height of SG_IMAGETYPE_2D images
int max_image_size_cube; // max width/height of SG_IMAGETYPE_CUBE images
int max_image_size_3d; // max width/height/depth of SG_IMAGETYPE_3D images
int max_image_size_array; // max width/height of SG_IMAGETYPE_ARRAY images
int max_image_array_layers; // max number of layers in SG_IMAGETYPE_ARRAY images
int max_vertex_attrs; // max number of vertex attributes, clamped to SG_MAX_VERTEX_ATTRIBUTES
int gl_max_vertex_uniform_components; // <= GL_MAX_VERTEX_UNIFORM_COMPONENTS (only on GL backends)
int gl_max_combined_texture_image_units; // <= GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (only on GL backends)
} sg_limits;
/*
sg_resource_state
The current state of a resource in its resource pool.
Resources start in the INITIAL state, which means the
pool slot is unoccupied and can be allocated. When a resource is
created, first an id is allocated, and the resource pool slot
is set to state ALLOC. After allocation, the resource is
initialized, which may result in the VALID or FAILED state. The
reason why allocation and initialization are separate is because
some resource types (e.g. buffers and images) might be asynchronously
initialized by the user application. If a resource which is not
in the VALID state is attempted to be used for rendering, rendering
operations will silently be dropped.
The special INVALID state is returned in sg_query_xxx_state() if no
resource object exists for the provided resource id.
*/
typedef enum sg_resource_state {
SG_RESOURCESTATE_INITIAL,
SG_RESOURCESTATE_ALLOC,
SG_RESOURCESTATE_VALID,
SG_RESOURCESTATE_FAILED,
SG_RESOURCESTATE_INVALID,
_SG_RESOURCESTATE_FORCE_U32 = 0x7FFFFFFF
} sg_resource_state;
/*
sg_usage
A resource usage hint describing the update strategy of
buffers and images. This is used in the sg_buffer_desc.usage
and sg_image_desc.usage members when creating buffers
and images:
SG_USAGE_IMMUTABLE: the resource will never be updated with
new data, instead the content of the
resource must be provided on creation
SG_USAGE_DYNAMIC: the resource will be updated infrequently
with new data (this could range from "once
after creation", to "quite often but not
every frame")
SG_USAGE_STREAM: the resource will be updated each frame
with new content
The rendering backends use this hint to prevent that the
CPU needs to wait for the GPU when attempting to update
a resource that might be currently accessed by the GPU.
Resource content is updated with the functions sg_update_buffer() or
sg_append_buffer() for buffer objects, and sg_update_image() for image
objects. For the sg_update_*() functions, only one update is allowed per
frame and resource object, while sg_append_buffer() can be called
multiple times per frame on the same buffer. The application must update
all data required for rendering (this means that the update data can be
smaller than the resource size, if only a part of the overall resource
size is used for rendering, you only need to make sure that the data that
*is* used is valid).
The default usage is SG_USAGE_IMMUTABLE.
*/
typedef enum sg_usage {
_SG_USAGE_DEFAULT, // value 0 reserved for default-init
SG_USAGE_IMMUTABLE,
SG_USAGE_DYNAMIC,
SG_USAGE_STREAM,
_SG_USAGE_NUM,
_SG_USAGE_FORCE_U32 = 0x7FFFFFFF
} sg_usage;
/*
sg_buffer_type
Indicates whether a buffer will be bound as vertex-,
index- or storage-buffer.
Used in the sg_buffer_desc.type member when creating a buffer.
The default value is SG_BUFFERTYPE_VERTEXBUFFER.
*/
typedef enum sg_buffer_type {
_SG_BUFFERTYPE_DEFAULT, // value 0 reserved for default-init
SG_BUFFERTYPE_VERTEXBUFFER,
SG_BUFFERTYPE_INDEXBUFFER,
SG_BUFFERTYPE_STORAGEBUFFER,
_SG_BUFFERTYPE_NUM,
_SG_BUFFERTYPE_FORCE_U32 = 0x7FFFFFFF
} sg_buffer_type;
/*
sg_index_type
Indicates whether indexed rendering (fetching vertex-indices from an
index buffer) is used, and if yes, the index data type (16- or 32-bits).
This is used in the sg_pipeline_desc.index_type member when creating a
pipeline object.
The default index type is SG_INDEXTYPE_NONE.
*/
typedef enum sg_index_type {
_SG_INDEXTYPE_DEFAULT, // value 0 reserved for default-init
SG_INDEXTYPE_NONE,
SG_INDEXTYPE_UINT16,
SG_INDEXTYPE_UINT32,
_SG_INDEXTYPE_NUM,
_SG_INDEXTYPE_FORCE_U32 = 0x7FFFFFFF
} sg_index_type;
/*
sg_image_type
Indicates the basic type of an image object (2D-texture, cubemap,
3D-texture or 2D-array-texture). Used in the sg_image_desc.type member when
creating an image, and in sg_shader_image_desc to describe a sampled texture
in the shader (both must match and will be checked in the validation layer
when calling sg_apply_bindings).
The default image type when creating an image is SG_IMAGETYPE_2D.
*/
typedef enum sg_image_type {
_SG_IMAGETYPE_DEFAULT, // value 0 reserved for default-init
SG_IMAGETYPE_2D,
SG_IMAGETYPE_CUBE,
SG_IMAGETYPE_3D,
SG_IMAGETYPE_ARRAY,
_SG_IMAGETYPE_NUM,
_SG_IMAGETYPE_FORCE_U32 = 0x7FFFFFFF
} sg_image_type;
/*
sg_image_sample_type
The basic data type of a texture sample as expected by a shader.
Must be provided in sg_shader_image_desc and used by the validation
layer in sg_apply_bindings() to check if the provided image object
is compatible with what the shader expects. Apart from the sokol-gfx
validation layer, WebGPU is the only backend API which actually requires
matching texture and sampler type to be provided upfront for validation
(other 3D APIs treat texture/sampler type mismatches as undefined behaviour).
NOTE that the following texture pixel formats require the use
of SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT, combined with a sampler
of type SG_SAMPLERTYPE_NONFILTERING:
- SG_PIXELFORMAT_R32F
- SG_PIXELFORMAT_RG32F
- SG_PIXELFORMAT_RGBA32F
(when using sokol-shdc, also check out the meta tags `@image_sample_type`
and `@sampler_type`)
*/
typedef enum sg_image_sample_type {
_SG_IMAGESAMPLETYPE_DEFAULT, // value 0 reserved for default-init
SG_IMAGESAMPLETYPE_FLOAT,
SG_IMAGESAMPLETYPE_DEPTH,
SG_IMAGESAMPLETYPE_SINT,
SG_IMAGESAMPLETYPE_UINT,
SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT,
_SG_IMAGESAMPLETYPE_NUM,
_SG_IMAGESAMPLETYPE_FORCE_U32 = 0x7FFFFFFF
} sg_image_sample_type;
/*
sg_sampler_type
The basic type of a texture sampler (sampling vs comparison) as
defined in a shader. Must be provided in sg_shader_sampler_desc.
sg_image_sample_type and sg_sampler_type for a texture/sampler
pair must be compatible with each other, specifically only
the following pairs are allowed:
- SG_IMAGESAMPLETYPE_FLOAT => (SG_SAMPLERTYPE_FILTERING or SG_SAMPLERTYPE_NONFILTERING)
- SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT => SG_SAMPLERTYPE_NONFILTERING
- SG_IMAGESAMPLETYPE_SINT => SG_SAMPLERTYPE_NONFILTERING
- SG_IMAGESAMPLETYPE_UINT => SG_SAMPLERTYPE_NONFILTERING
- SG_IMAGESAMPLETYPE_DEPTH => SG_SAMPLERTYPE_COMPARISON
*/
typedef enum sg_sampler_type {
_SG_SAMPLERTYPE_DEFAULT,
SG_SAMPLERTYPE_FILTERING,
SG_SAMPLERTYPE_NONFILTERING,
SG_SAMPLERTYPE_COMPARISON,
_SG_SAMPLERTYPE_NUM,
_SG_SAMPLERTYPE_FORCE_U32,
} sg_sampler_type;
/*
sg_cube_face
The cubemap faces. Use these as indices in the sg_image_desc.content
array.
*/
typedef enum sg_cube_face {
SG_CUBEFACE_POS_X,
SG_CUBEFACE_NEG_X,
SG_CUBEFACE_POS_Y,
SG_CUBEFACE_NEG_Y,
SG_CUBEFACE_POS_Z,
SG_CUBEFACE_NEG_Z,
SG_CUBEFACE_NUM,
_SG_CUBEFACE_FORCE_U32 = 0x7FFFFFFF
} sg_cube_face;
/*
sg_shader_stage
There are 2 shader stages: vertex- and fragment-shader-stage.
Each shader stage
- SG_MAX_SHADERSTAGE_UBS slots for applying uniform data
- SG_MAX_SHADERSTAGE_IMAGES slots for images used as textures
- SG_MAX_SHADERSTAGE_SAMPLERS slots for texture samplers
- SG_MAX_SHADERSTAGE_STORAGEBUFFERS slots for storage buffer bindings
*/
typedef enum sg_shader_stage {
SG_SHADERSTAGE_VS,
SG_SHADERSTAGE_FS,
_SG_SHADERSTAGE_FORCE_U32 = 0x7FFFFFFF
} sg_shader_stage;
/*
sg_primitive_type
This is the common subset of 3D primitive types supported across all 3D
APIs. This is used in the sg_pipeline_desc.primitive_type member when
creating a pipeline object.
The default primitive type is SG_PRIMITIVETYPE_TRIANGLES.
*/
typedef enum sg_primitive_type {
_SG_PRIMITIVETYPE_DEFAULT, // value 0 reserved for default-init
SG_PRIMITIVETYPE_POINTS,
SG_PRIMITIVETYPE_LINES,
SG_PRIMITIVETYPE_LINE_STRIP,
SG_PRIMITIVETYPE_TRIANGLES,
SG_PRIMITIVETYPE_TRIANGLE_STRIP,
_SG_PRIMITIVETYPE_NUM,
_SG_PRIMITIVETYPE_FORCE_U32 = 0x7FFFFFFF
} sg_primitive_type;
/*
sg_filter
The filtering mode when sampling a texture image. This is
used in the sg_sampler_desc.min_filter, sg_sampler_desc.mag_filter
and sg_sampler_desc.mipmap_filter members when creating a sampler object.
For the default is SG_FILTER_NEAREST.
*/
typedef enum sg_filter {
_SG_FILTER_DEFAULT, // value 0 reserved for default-init
SG_FILTER_NEAREST,
SG_FILTER_LINEAR,
_SG_FILTER_NUM,
_SG_FILTER_FORCE_U32 = 0x7FFFFFFF
} sg_filter;
/*
sg_wrap
The texture coordinates wrapping mode when sampling a texture
image. This is used in the sg_image_desc.wrap_u, .wrap_v
and .wrap_w members when creating an image.
The default wrap mode is SG_WRAP_REPEAT.
NOTE: SG_WRAP_CLAMP_TO_BORDER is not supported on all backends
and platforms. To check for support, call sg_query_features()
and check the "clamp_to_border" boolean in the returned
sg_features struct.
Platforms which don't support SG_WRAP_CLAMP_TO_BORDER will silently fall back
to SG_WRAP_CLAMP_TO_EDGE without a validation error.
*/
typedef enum sg_wrap {
_SG_WRAP_DEFAULT, // value 0 reserved for default-init
SG_WRAP_REPEAT,
SG_WRAP_CLAMP_TO_EDGE,
SG_WRAP_CLAMP_TO_BORDER,
SG_WRAP_MIRRORED_REPEAT,
_SG_WRAP_NUM,
_SG_WRAP_FORCE_U32 = 0x7FFFFFFF
} sg_wrap;
/*
sg_border_color
The border color to use when sampling a texture, and the UV wrap
mode is SG_WRAP_CLAMP_TO_BORDER.
The default border color is SG_BORDERCOLOR_OPAQUE_BLACK
*/
typedef enum sg_border_color {
_SG_BORDERCOLOR_DEFAULT, // value 0 reserved for default-init
SG_BORDERCOLOR_TRANSPARENT_BLACK,
SG_BORDERCOLOR_OPAQUE_BLACK,
SG_BORDERCOLOR_OPAQUE_WHITE,
_SG_BORDERCOLOR_NUM,
_SG_BORDERCOLOR_FORCE_U32 = 0x7FFFFFFF
} sg_border_color;
/*
sg_vertex_format
The data type of a vertex component. This is used to describe
the layout of vertex data when creating a pipeline object.
*/
typedef enum sg_vertex_format {
SG_VERTEXFORMAT_INVALID,
SG_VERTEXFORMAT_FLOAT,
SG_VERTEXFORMAT_FLOAT2,
SG_VERTEXFORMAT_FLOAT3,
SG_VERTEXFORMAT_FLOAT4,
SG_VERTEXFORMAT_BYTE4,
SG_VERTEXFORMAT_BYTE4N,
SG_VERTEXFORMAT_UBYTE4,
SG_VERTEXFORMAT_UBYTE4N,
SG_VERTEXFORMAT_SHORT2,
SG_VERTEXFORMAT_SHORT2N,
SG_VERTEXFORMAT_USHORT2N,
SG_VERTEXFORMAT_SHORT4,
SG_VERTEXFORMAT_SHORT4N,
SG_VERTEXFORMAT_USHORT4N,
SG_VERTEXFORMAT_UINT10_N2,
SG_VERTEXFORMAT_HALF2,
SG_VERTEXFORMAT_HALF4,
_SG_VERTEXFORMAT_NUM,
_SG_VERTEXFORMAT_FORCE_U32 = 0x7FFFFFFF
} sg_vertex_format;
/*
sg_vertex_step
Defines whether the input pointer of a vertex input stream is advanced
'per vertex' or 'per instance'. The default step-func is
SG_VERTEXSTEP_PER_VERTEX. SG_VERTEXSTEP_PER_INSTANCE is used with
instanced-rendering.
The vertex-step is part of the vertex-layout definition
when creating pipeline objects.
*/
typedef enum sg_vertex_step {
_SG_VERTEXSTEP_DEFAULT, // value 0 reserved for default-init
SG_VERTEXSTEP_PER_VERTEX,
SG_VERTEXSTEP_PER_INSTANCE,
_SG_VERTEXSTEP_NUM,
_SG_VERTEXSTEP_FORCE_U32 = 0x7FFFFFFF
} sg_vertex_step;
/*
sg_uniform_type
The data type of a uniform block member. This is used to
describe the internal layout of uniform blocks when creating
a shader object.
*/
typedef enum sg_uniform_type {
SG_UNIFORMTYPE_INVALID,
SG_UNIFORMTYPE_FLOAT,
SG_UNIFORMTYPE_FLOAT2,
SG_UNIFORMTYPE_FLOAT3,
SG_UNIFORMTYPE_FLOAT4,
SG_UNIFORMTYPE_INT,
SG_UNIFORMTYPE_INT2,
SG_UNIFORMTYPE_INT3,
SG_UNIFORMTYPE_INT4,
SG_UNIFORMTYPE_MAT4,
_SG_UNIFORMTYPE_NUM,
_SG_UNIFORMTYPE_FORCE_U32 = 0x7FFFFFFF
} sg_uniform_type;
/*
sg_uniform_layout
A hint for the interior memory layout of uniform blocks. This is
only really relevant for the GL backend where the internal layout
of uniform blocks must be known to sokol-gfx. For all other backends the
internal memory layout of uniform blocks doesn't matter, sokol-gfx
will just pass uniform data as a single memory blob to the
3D backend.
SG_UNIFORMLAYOUT_NATIVE (default)
Native layout means that a 'backend-native' memory layout
is used. For the GL backend this means that uniforms
are packed tightly in memory (e.g. there are no padding
bytes).
SG_UNIFORMLAYOUT_STD140
The memory layout is a subset of std140. Arrays are only
allowed for the FLOAT4, INT4 and MAT4. Alignment is as
is as follows:
FLOAT, INT: 4 byte alignment
FLOAT2, INT2: 8 byte alignment
FLOAT3, INT3: 16 byte alignment(!)
FLOAT4, INT4: 16 byte alignment
MAT4: 16 byte alignment
FLOAT4[], INT4[]: 16 byte alignment
The overall size of the uniform block must be a multiple
of 16.
For more information search for 'UNIFORM DATA LAYOUT' in the documentation block
at the start of the header.
*/
typedef enum sg_uniform_layout {
_SG_UNIFORMLAYOUT_DEFAULT, // value 0 reserved for default-init
SG_UNIFORMLAYOUT_NATIVE, // default: layout depends on currently active backend
SG_UNIFORMLAYOUT_STD140, // std140: memory layout according to std140
_SG_UNIFORMLAYOUT_NUM,
_SG_UNIFORMLAYOUT_FORCE_U32 = 0x7FFFFFFF
} sg_uniform_layout;
/*
sg_cull_mode
The face-culling mode, this is used in the
sg_pipeline_desc.cull_mode member when creating a
pipeline object.
The default cull mode is SG_CULLMODE_NONE
*/
typedef enum sg_cull_mode {
_SG_CULLMODE_DEFAULT, // value 0 reserved for default-init
SG_CULLMODE_NONE,
SG_CULLMODE_FRONT,
SG_CULLMODE_BACK,
_SG_CULLMODE_NUM,
_SG_CULLMODE_FORCE_U32 = 0x7FFFFFFF
} sg_cull_mode;
/*
sg_face_winding
The vertex-winding rule that determines a front-facing primitive. This
is used in the member sg_pipeline_desc.face_winding
when creating a pipeline object.
The default winding is SG_FACEWINDING_CW (clockwise)
*/
typedef enum sg_face_winding {
_SG_FACEWINDING_DEFAULT, // value 0 reserved for default-init
SG_FACEWINDING_CCW,
SG_FACEWINDING_CW,
_SG_FACEWINDING_NUM,
_SG_FACEWINDING_FORCE_U32 = 0x7FFFFFFF
} sg_face_winding;
/*
sg_compare_func
The compare-function for configuring depth- and stencil-ref tests
in pipeline objects, and for texture samplers which perform a comparison
instead of regular sampling operation.
sg_pipeline_desc
.depth
.compare
.stencil
.front.compare
.back.compar
sg_sampler_desc
.compare
The default compare func for depth- and stencil-tests is
SG_COMPAREFUNC_ALWAYS.
The default compare func for sampler is SG_COMPAREFUNC_NEVER.
*/
typedef enum sg_compare_func {
_SG_COMPAREFUNC_DEFAULT, // value 0 reserved for default-init
SG_COMPAREFUNC_NEVER,
SG_COMPAREFUNC_LESS,
SG_COMPAREFUNC_EQUAL,
SG_COMPAREFUNC_LESS_EQUAL,
SG_COMPAREFUNC_GREATER,
SG_COMPAREFUNC_NOT_EQUAL,
SG_COMPAREFUNC_GREATER_EQUAL,
SG_COMPAREFUNC_ALWAYS,
_SG_COMPAREFUNC_NUM,
_SG_COMPAREFUNC_FORCE_U32 = 0x7FFFFFFF
} sg_compare_func;
/*
sg_stencil_op
The operation performed on a currently stored stencil-value when a
comparison test passes or fails. This is used when creating a pipeline
object in the members:
sg_pipeline_desc
.stencil
.front
.fail_op
.depth_fail_op
.pass_op
.back
.fail_op
.depth_fail_op
.pass_op
The default value is SG_STENCILOP_KEEP.
*/
typedef enum sg_stencil_op {
_SG_STENCILOP_DEFAULT, // value 0 reserved for default-init
SG_STENCILOP_KEEP,
SG_STENCILOP_ZERO,
SG_STENCILOP_REPLACE,
SG_STENCILOP_INCR_CLAMP,
SG_STENCILOP_DECR_CLAMP,
SG_STENCILOP_INVERT,
SG_STENCILOP_INCR_WRAP,
SG_STENCILOP_DECR_WRAP,
_SG_STENCILOP_NUM,
_SG_STENCILOP_FORCE_U32 = 0x7FFFFFFF
} sg_stencil_op;
/*
sg_blend_factor
The source and destination factors in blending operations.
This is used in the following members when creating a pipeline object:
sg_pipeline_desc
.colors[i]
.blend
.src_factor_rgb
.dst_factor_rgb
.src_factor_alpha
.dst_factor_alpha
The default value is SG_BLENDFACTOR_ONE for source
factors, and SG_BLENDFACTOR_ZERO for destination factors.
*/
typedef enum sg_blend_factor {
_SG_BLENDFACTOR_DEFAULT, // value 0 reserved for default-init
SG_BLENDFACTOR_ZERO,
SG_BLENDFACTOR_ONE,
SG_BLENDFACTOR_SRC_COLOR,
SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR,
SG_BLENDFACTOR_SRC_ALPHA,
SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SG_BLENDFACTOR_DST_COLOR,
SG_BLENDFACTOR_ONE_MINUS_DST_COLOR,
SG_BLENDFACTOR_DST_ALPHA,
SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA,
SG_BLENDFACTOR_SRC_ALPHA_SATURATED,
SG_BLENDFACTOR_BLEND_COLOR,
SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR,
SG_BLENDFACTOR_BLEND_ALPHA,
SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA,
_SG_BLENDFACTOR_NUM,
_SG_BLENDFACTOR_FORCE_U32 = 0x7FFFFFFF
} sg_blend_factor;
/*
sg_blend_op
Describes how the source and destination values are combined in the
fragment blending operation. It is used in the following members when
creating a pipeline object:
sg_pipeline_desc
.colors[i]
.blend
.op_rgb
.op_alpha
The default value is SG_BLENDOP_ADD.
*/
typedef enum sg_blend_op {
_SG_BLENDOP_DEFAULT, // value 0 reserved for default-init
SG_BLENDOP_ADD,
SG_BLENDOP_SUBTRACT,
SG_BLENDOP_REVERSE_SUBTRACT,
_SG_BLENDOP_NUM,
_SG_BLENDOP_FORCE_U32 = 0x7FFFFFFF
} sg_blend_op;
/*
sg_color_mask
Selects the active color channels when writing a fragment color to the
framebuffer. This is used in the members
sg_pipeline_desc.colors[i].write_mask when creating a pipeline object.
The default colormask is SG_COLORMASK_RGBA (write all colors channels)
NOTE: since the color mask value 0 is reserved for the default value
(SG_COLORMASK_RGBA), use SG_COLORMASK_NONE if all color channels
should be disabled.
*/
typedef enum sg_color_mask {
_SG_COLORMASK_DEFAULT = 0, // value 0 reserved for default-init
SG_COLORMASK_NONE = 0x10, // special value for 'all channels disabled
SG_COLORMASK_R = 0x1,
SG_COLORMASK_G = 0x2,
SG_COLORMASK_RG = 0x3,
SG_COLORMASK_B = 0x4,
SG_COLORMASK_RB = 0x5,
SG_COLORMASK_GB = 0x6,
SG_COLORMASK_RGB = 0x7,
SG_COLORMASK_A = 0x8,
SG_COLORMASK_RA = 0x9,
SG_COLORMASK_GA = 0xA,
SG_COLORMASK_RGA = 0xB,
SG_COLORMASK_BA = 0xC,
SG_COLORMASK_RBA = 0xD,
SG_COLORMASK_GBA = 0xE,
SG_COLORMASK_RGBA = 0xF,
_SG_COLORMASK_FORCE_U32 = 0x7FFFFFFF
} sg_color_mask;
/*
sg_load_action
Defines the load action that should be performed at the start of a render pass:
SG_LOADACTION_CLEAR: clear the render target
SG_LOADACTION_LOAD: load the previous content of the render target
SG_LOADACTION_DONTCARE: leave the render target in an undefined state
This is used in the sg_pass_action structure.
The default load action for all pass attachments is SG_LOADACTION_CLEAR,
with the values rgba = { 0.5f, 0.5f, 0.5f, 1.0f }, depth=1.0f and stencil=0.
If you want to override the default behaviour, it is important to not
only set the clear color, but the 'action' field as well (as long as this
is _SG_LOADACTION_DEFAULT, the value fields will be ignored).
*/
typedef enum sg_load_action {
_SG_LOADACTION_DEFAULT,
SG_LOADACTION_CLEAR,
SG_LOADACTION_LOAD,
SG_LOADACTION_DONTCARE,
_SG_LOADACTION_FORCE_U32 = 0x7FFFFFFF
} sg_load_action;
/*
sg_store_action
Defines the store action that be performed at the end of a render pass:
SG_STOREACTION_STORE: store the rendered content to the color attachment image
SG_STOREACTION_DONTCARE: allows the GPU to discard the rendered content
*/
typedef enum sg_store_action {
_SG_STOREACTION_DEFAULT,
SG_STOREACTION_STORE,
SG_STOREACTION_DONTCARE,
_SG_STOREACTION_FORCE_U32 = 0x7FFFFFFF
} sg_store_action;
/*
sg_pass_action
The sg_pass_action struct defines the actions to be performed
at the start and end of a render pass.
- at the start of the pass: whether the render targets should be cleared,
loaded with their previous content, or start in an undefined state
- for clear operations: the clear value (color, depth, or stencil values)
- at the end of the pass: whether the rendering result should be
stored back into the render target or discarded
*/
typedef struct sg_color_attachment_action {
sg_load_action load_action; // default: SG_LOADACTION_CLEAR
sg_store_action store_action; // default: SG_STOREACTION_STORE
sg_color clear_value; // default: { 0.5f, 0.5f, 0.5f, 1.0f }
} sg_color_attachment_action;
typedef struct sg_depth_attachment_action {
sg_load_action load_action; // default: SG_LOADACTION_CLEAR
sg_store_action store_action; // default: SG_STOREACTION_DONTCARE
float clear_value; // default: 1.0
} sg_depth_attachment_action;
typedef struct sg_stencil_attachment_action {
sg_load_action load_action; // default: SG_LOADACTION_CLEAR
sg_store_action store_action; // default: SG_STOREACTION_DONTCARE
uint8_t clear_value; // default: 0
} sg_stencil_attachment_action;
typedef struct sg_pass_action {
sg_color_attachment_action colors[SG_MAX_COLOR_ATTACHMENTS];
sg_depth_attachment_action depth;
sg_stencil_attachment_action stencil;
} sg_pass_action;
/*
sg_swapchain
Used in sg_begin_pass() to provide details about an external swapchain
(pixel formats, sample count and backend-API specific render surface objects).
The following information must be provided:
- the width and height of the swapchain surfaces in number of pixels,
- the pixel format of the render- and optional msaa-resolve-surface
- the pixel format of the optional depth- or depth-stencil-surface
- the MSAA sample count for the render and depth-stencil surface
If the pixel formats and MSAA sample counts are left zero-initialized,
their defaults are taken from the sg_environment struct provided in the
sg_setup() call.
The width and height *must* be > 0.
Additionally the following backend API specific objects must be passed in
as 'type erased' void pointers:
GL: on all GL backends, a GL framebuffer object must be provided. This
can be zero for the default framebuffer.
D3D11:
- an ID3D11RenderTargetView for the rendering surface, without
MSAA rendering this surface will also be displayed
- an optional ID3D11DepthStencilView for the depth- or depth/stencil
buffer surface
- when MSAA rendering is used, another ID3D11RenderTargetView
which serves as MSAA resolve target and will be displayed
WebGPU (same as D3D11, except different types)
- a WGPUTextureView for the rendering surface, without
MSAA rendering this surface will also be displayed
- an optional WGPUTextureView for the depth- or depth/stencil
buffer surface
- when MSAA rendering is used, another WGPUTextureView
which serves as MSAA resolve target and will be displayed
Metal (NOTE that the rolves of provided surfaces is slightly different
than on D3D11 or WebGPU in case of MSAA vs non-MSAA rendering):
- A current CAMetalDrawable (NOT an MTLDrawable!) which will be presented.
This will either be rendered to directly (if no MSAA is used), or serve
as MSAA-resolve target.
- an optional MTLTexture for the depth- or depth-stencil buffer
- an optional multisampled MTLTexture which serves as intermediate
rendering surface which will then be resolved into the
CAMetalDrawable.
NOTE that for Metal you must use an ObjC __bridge cast to
properly tunnel the ObjC object handle through a C void*, e.g.:
swapchain.metal.current_drawable = (__bridge const void*) [mtkView currentDrawable];
On all other backends you shouldn't need to mess with the reference count.
It's a good practice to write a helper function which returns an initialized
sg_swapchain structs, which can then be plugged directly into
sg_pass.swapchain. Look at the function sglue_swapchain() in the sokol_glue.h
as an example.
*/
typedef struct sg_metal_swapchain {
const void* current_drawable; // CAMetalDrawable (NOT MTLDrawable!!!)
const void* depth_stencil_texture; // MTLTexture
const void* msaa_color_texture; // MTLTexture
} sg_metal_swapchain;
typedef struct sg_d3d11_swapchain {
const void* render_view; // ID3D11RenderTargetView
const void* resolve_view; // ID3D11RenderTargetView
const void* depth_stencil_view; // ID3D11DepthStencilView
} sg_d3d11_swapchain;
typedef struct sg_wgpu_swapchain {
const void* render_view; // WGPUTextureView
const void* resolve_view; // WGPUTextureView
const void* depth_stencil_view; // WGPUTextureView
} sg_wgpu_swapchain;
typedef struct sg_gl_swapchain {
uint32_t framebuffer; // GL framebuffer object
} sg_gl_swapchain;
typedef struct sg_swapchain {
int width;
int height;
int sample_count;
sg_pixel_format color_format;
sg_pixel_format depth_format;
sg_metal_swapchain metal;
sg_d3d11_swapchain d3d11;
sg_wgpu_swapchain wgpu;
sg_gl_swapchain gl;
} sg_swapchain;
/*
sg_pass
The sg_pass structure is passed as argument into the sg_begin_pass()
function.
For an offscreen rendering pass, an sg_pass_action struct and sg_attachments
object must be provided, and for swapchain passes, and sg_pass_action and
an sg_swapchain struct. It is an error to provide both an sg_attachments
handle and an initialized sg_swapchain struct in the same sg_begin_pass().
An sg_begin_pass() call for an offscreen pass would look like this (where
`attachments` is an sg_attachments handle):
sg_begin_pass(&(sg_pass){
.action = { ... },
.attachments = attachments,
});
...and a swapchain render pass would look like this (using the sokol_glue.h
helper function sglue_swapchain() which gets the swapchain properties from
sokol_app.h):
sg_begin_pass(&(sg_pass){
.action = { ... },
.swapchain = sglue_swapchain(),
});
You can also omit the .action object to get default pass action behaviour
(clear to color=grey, depth=1 and stencil=0).
*/
typedef struct sg_pass {
uint32_t _start_canary;
sg_pass_action action;
sg_attachments attachments;
sg_swapchain swapchain;
const char* label;
uint32_t _end_canary;
} sg_pass;
/*
sg_bindings
The sg_bindings structure defines the resource binding slots
of the sokol_gfx render pipeline, used as argument to the
sg_apply_bindings() function.
A resource binding struct contains:
- 1..N vertex buffers
- 0..N vertex buffer offsets
- 0..1 index buffers
- 0..1 index buffer offsets
- 0..N vertex shader stage images
- 0..N vertex shader stage samplers
- 0..N vertex shader storage buffers
- 0..N fragment shader stage images
- 0..N fragment shader stage samplers
- 0..N fragment shader storage buffers
For the max number of bindings, see the constant definitions:
- SG_MAX_VERTEX_BUFFERS
- SG_MAX_SHADERSTAGE_IMAGES
- SG_MAX_SHADERSTAGE_SAMPLERS
- SG_MAX_SHADERSTAGE_STORAGEBUFFERS
The optional buffer offsets can be used to put different unrelated
chunks of vertex- and/or index-data into the same buffer objects.
*/
typedef struct sg_stage_bindings {
sg_image images[SG_MAX_SHADERSTAGE_IMAGES];
sg_sampler samplers[SG_MAX_SHADERSTAGE_SAMPLERS];
sg_buffer storage_buffers[SG_MAX_SHADERSTAGE_STORAGEBUFFERS];
} sg_stage_bindings;
typedef struct sg_bindings {
uint32_t _start_canary;
sg_buffer vertex_buffers[SG_MAX_VERTEX_BUFFERS];
int vertex_buffer_offsets[SG_MAX_VERTEX_BUFFERS];
sg_buffer index_buffer;
int index_buffer_offset;
sg_stage_bindings vs;
sg_stage_bindings fs;
uint32_t _end_canary;
} sg_bindings;
/*
sg_buffer_desc
Creation parameters for sg_buffer objects, used in the
sg_make_buffer() call.
The default configuration is:
.size: 0 (*must* be >0 for buffers without data)
.type: SG_BUFFERTYPE_VERTEXBUFFER
.usage: SG_USAGE_IMMUTABLE
.data.ptr 0 (*must* be valid for immutable buffers)
.data.size 0 (*must* be > 0 for immutable buffers)
.label 0 (optional string label)
For immutable buffers which are initialized with initial data,
keep the .size item zero-initialized, and set the size together with the
pointer to the initial data in the .data item.
For mutable buffers without initial data, keep the .data item
zero-initialized, and set the buffer size in the .size item instead.
You can also set both size values, but currently both size values must
be identical (this may change in the future when the dynamic resource
management may become more flexible).
ADVANCED TOPIC: Injecting native 3D-API buffers:
The following struct members allow to inject your own GL, Metal
or D3D11 buffers into sokol_gfx:
.gl_buffers[SG_NUM_INFLIGHT_FRAMES]
.mtl_buffers[SG_NUM_INFLIGHT_FRAMES]
.d3d11_buffer
You must still provide all other struct items except the .data item, and
these must match the creation parameters of the native buffers you
provide. For SG_USAGE_IMMUTABLE, only provide a single native 3D-API
buffer, otherwise you need to provide SG_NUM_INFLIGHT_FRAMES buffers
(only for GL and Metal, not D3D11). Providing multiple buffers for GL and
Metal is necessary because sokol_gfx will rotate through them when
calling sg_update_buffer() to prevent lock-stalls.
Note that it is expected that immutable injected buffer have already been
initialized with content, and the .content member must be 0!
Also you need to call sg_reset_state_cache() after calling native 3D-API
functions, and before calling any sokol_gfx function.
*/
typedef struct sg_buffer_desc {
uint32_t _start_canary;
size_t size;
sg_buffer_type type;
sg_usage usage;
sg_range data;
const char* label;
// optionally inject backend-specific resources
uint32_t gl_buffers[SG_NUM_INFLIGHT_FRAMES];
const void* mtl_buffers[SG_NUM_INFLIGHT_FRAMES];
const void* d3d11_buffer;
const void* wgpu_buffer;
uint32_t _end_canary;
} sg_buffer_desc;
/*
sg_image_data
Defines the content of an image through a 2D array of sg_range structs.
The first array dimension is the cubemap face, and the second array
dimension the mipmap level.
*/
typedef struct sg_image_data {
sg_range subimage[SG_CUBEFACE_NUM][SG_MAX_MIPMAPS];
} sg_image_data;
/*
sg_image_desc
Creation parameters for sg_image objects, used in the sg_make_image() call.
The default configuration is:
.type: SG_IMAGETYPE_2D
.render_target: false
.width 0 (must be set to >0)
.height 0 (must be set to >0)
.num_slices 1 (3D textures: depth; array textures: number of layers)
.num_mipmaps: 1
.usage: SG_USAGE_IMMUTABLE
.pixel_format: SG_PIXELFORMAT_RGBA8 for textures, or sg_desc.environment.defaults.color_format for render targets
.sample_count: 1 for textures, or sg_desc.environment.defaults.sample_count for render targets
.data an sg_image_data struct to define the initial content
.label 0 (optional string label for trace hooks)
Q: Why is the default sample_count for render targets identical with the
"default sample count" from sg_desc.environment.defaults.sample_count?
A: So that it matches the default sample count in pipeline objects. Even
though it is a bit strange/confusing that offscreen render targets by default
get the same sample count as 'default swapchains', but it's better that
an offscreen render target created with default parameters matches
a pipeline object created with default parameters.
NOTE:
Images with usage SG_USAGE_IMMUTABLE must be fully initialized by
providing a valid .data member which points to initialization data.
ADVANCED TOPIC: Injecting native 3D-API textures:
The following struct members allow to inject your own GL, Metal or D3D11
textures into sokol_gfx:
.gl_textures[SG_NUM_INFLIGHT_FRAMES]
.mtl_textures[SG_NUM_INFLIGHT_FRAMES]
.d3d11_texture
.d3d11_shader_resource_view
.wgpu_texture
.wgpu_texture_view
For GL, you can also specify the texture target or leave it empty to use
the default texture target for the image type (GL_TEXTURE_2D for
SG_IMAGETYPE_2D etc)
For D3D11 and WebGPU, either only provide a texture, or both a texture and
shader-resource-view / texture-view object. If you want to use access the
injected texture in a shader you *must* provide a shader-resource-view.
The same rules apply as for injecting native buffers (see sg_buffer_desc
documentation for more details).
*/
typedef struct sg_image_desc {
uint32_t _start_canary;
sg_image_type type;
bool render_target;
int width;
int height;
int num_slices;
int num_mipmaps;
sg_usage usage;
sg_pixel_format pixel_format;
int sample_count;
sg_image_data data;
const char* label;
// optionally inject backend-specific resources
uint32_t gl_textures[SG_NUM_INFLIGHT_FRAMES];
uint32_t gl_texture_target;
const void* mtl_textures[SG_NUM_INFLIGHT_FRAMES];
const void* d3d11_texture;
const void* d3d11_shader_resource_view;
const void* wgpu_texture;
const void* wgpu_texture_view;
uint32_t _end_canary;
} sg_image_desc;
/*
sg_sampler_desc
Creation parameters for sg_sampler objects, used in the sg_make_sampler() call
.min_filter: SG_FILTER_NEAREST
.mag_filter: SG_FILTER_NEAREST
.mipmap_filter SG_FILTER_NEAREST
.wrap_u: SG_WRAP_REPEAT
.wrap_v: SG_WRAP_REPEAT
.wrap_w: SG_WRAP_REPEAT (only SG_IMAGETYPE_3D)
.min_lod 0.0f
.max_lod FLT_MAX
.border_color SG_BORDERCOLOR_OPAQUE_BLACK
.compare SG_COMPAREFUNC_NEVER
.max_anisotropy 1 (must be 1..16)
*/
typedef struct sg_sampler_desc {
uint32_t _start_canary;
sg_filter min_filter;
sg_filter mag_filter;
sg_filter mipmap_filter;
sg_wrap wrap_u;
sg_wrap wrap_v;
sg_wrap wrap_w;
float min_lod;
float max_lod;
sg_border_color border_color;
sg_compare_func compare;
uint32_t max_anisotropy;
const char* label;
// optionally inject backend-specific resources
uint32_t gl_sampler;
const void* mtl_sampler;
const void* d3d11_sampler;
const void* wgpu_sampler;
uint32_t _end_canary;
} sg_sampler_desc;
/*
sg_shader_desc
The structure sg_shader_desc defines all creation parameters for shader
programs, used as input to the sg_make_shader() function:
- reflection information for vertex attributes (vertex shader inputs):
- vertex attribute name (only optionally used by GLES3 and GL)
- a semantic name and index (required for D3D11)
- for each shader-stage (vertex and fragment):
- the shader source or bytecode
- an optional entry function name
- an optional compile target (only for D3D11 when source is provided,
defaults are "vs_4_0" and "ps_4_0")
- reflection info for each uniform block used by the shader stage:
- the size of the uniform block in bytes
- a memory layout hint (native vs std140, only required for GL backends)
- reflection info for each uniform block member (only required for GL backends):
- member name
- member type (SG_UNIFORMTYPE_xxx)
- if the member is an array, the number of array items
- reflection info for textures used in the shader stage:
- the image type (SG_IMAGETYPE_xxx)
- the image-sample type (SG_IMAGESAMPLETYPE_xxx, default is SG_IMAGESAMPLETYPE_FLOAT)
- whether the shader expects a multisampled texture
- reflection info for samplers used in the shader stage:
- the sampler type (SG_SAMPLERTYPE_xxx)
- reflection info for each image-sampler-pair used by the shader:
- the texture slot of the involved texture
- the sampler slot of the involved sampler
- for GLSL only: the name of the combined image-sampler object
- reflection info for each storage-buffer used by the shader:
- whether the storage buffer is readonly (currently this
must be true)
For all GL backends, shader source-code must be provided. For D3D11 and Metal,
either shader source-code or byte-code can be provided.
For D3D11, if source code is provided, the d3dcompiler_47.dll will be loaded
on demand. If this fails, shader creation will fail. When compiling HLSL
source code, you can provide an optional target string via
sg_shader_stage_desc.d3d11_target, the default target is "vs_4_0" for the
vertex shader stage and "ps_4_0" for the pixel shader stage.
*/
typedef struct sg_shader_attr_desc {
const char* name; // GLSL vertex attribute name (optional)
const char* sem_name; // HLSL semantic name
int sem_index; // HLSL semantic index
} sg_shader_attr_desc;
typedef struct sg_shader_uniform_desc {
const char* name;
sg_uniform_type type;
int array_count;
} sg_shader_uniform_desc;
typedef struct sg_shader_uniform_block_desc {
size_t size;
sg_uniform_layout layout;
sg_shader_uniform_desc uniforms[SG_MAX_UB_MEMBERS];
} sg_shader_uniform_block_desc;
typedef struct sg_shader_storage_buffer_desc {
bool used;
bool readonly;
} sg_shader_storage_buffer_desc;
typedef struct sg_shader_image_desc {
bool used;
bool multisampled;
sg_image_type image_type;
sg_image_sample_type sample_type;
} sg_shader_image_desc;
typedef struct sg_shader_sampler_desc {
bool used;
sg_sampler_type sampler_type;
} sg_shader_sampler_desc;
typedef struct sg_shader_image_sampler_pair_desc {
bool used;
int image_slot;
int sampler_slot;
const char* glsl_name;
} sg_shader_image_sampler_pair_desc;
typedef struct sg_shader_stage_desc {
const char* source;
sg_range bytecode;
const char* entry;
const char* d3d11_target;
sg_shader_uniform_block_desc uniform_blocks[SG_MAX_SHADERSTAGE_UBS];
sg_shader_storage_buffer_desc storage_buffers[SG_MAX_SHADERSTAGE_STORAGEBUFFERS];
sg_shader_image_desc images[SG_MAX_SHADERSTAGE_IMAGES];
sg_shader_sampler_desc samplers[SG_MAX_SHADERSTAGE_SAMPLERS];
sg_shader_image_sampler_pair_desc image_sampler_pairs[SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS];
} sg_shader_stage_desc;
typedef struct sg_shader_desc {
uint32_t _start_canary;
sg_shader_attr_desc attrs[SG_MAX_VERTEX_ATTRIBUTES];
sg_shader_stage_desc vs;
sg_shader_stage_desc fs;
const char* label;
uint32_t _end_canary;
} sg_shader_desc;
/*
sg_pipeline_desc
The sg_pipeline_desc struct defines all creation parameters for an
sg_pipeline object, used as argument to the sg_make_pipeline() function:
- the vertex layout for all input vertex buffers
- a shader object
- the 3D primitive type (points, lines, triangles, ...)
- the index type (none, 16- or 32-bit)
- all the fixed-function-pipeline state (depth-, stencil-, blend-state, etc...)
If the vertex data has no gaps between vertex components, you can omit
the .layout.buffers[].stride and layout.attrs[].offset items (leave them
default-initialized to 0), sokol-gfx will then compute the offsets and
strides from the vertex component formats (.layout.attrs[].format).
Please note that ALL vertex attribute offsets must be 0 in order for the
automatic offset computation to kick in.
The default configuration is as follows:
.shader: 0 (must be initialized with a valid sg_shader id!)
.layout:
.buffers[]: vertex buffer layouts
.stride: 0 (if no stride is given it will be computed)
.step_func SG_VERTEXSTEP_PER_VERTEX
.step_rate 1
.attrs[]: vertex attribute declarations
.buffer_index 0 the vertex buffer bind slot
.offset 0 (offsets can be omitted if the vertex layout has no gaps)
.format SG_VERTEXFORMAT_INVALID (must be initialized!)
.depth:
.pixel_format: sg_desc.context.depth_format
.compare: SG_COMPAREFUNC_ALWAYS
.write_enabled: false
.bias: 0.0f
.bias_slope_scale: 0.0f
.bias_clamp: 0.0f
.stencil:
.enabled: false
.front/back:
.compare: SG_COMPAREFUNC_ALWAYS
.fail_op: SG_STENCILOP_KEEP
.depth_fail_op: SG_STENCILOP_KEEP
.pass_op: SG_STENCILOP_KEEP
.read_mask: 0
.write_mask: 0
.ref: 0
.color_count 1
.colors[0..color_count]
.pixel_format sg_desc.context.color_format
.write_mask: SG_COLORMASK_RGBA
.blend:
.enabled: false
.src_factor_rgb: SG_BLENDFACTOR_ONE
.dst_factor_rgb: SG_BLENDFACTOR_ZERO
.op_rgb: SG_BLENDOP_ADD
.src_factor_alpha: SG_BLENDFACTOR_ONE
.dst_factor_alpha: SG_BLENDFACTOR_ZERO
.op_alpha: SG_BLENDOP_ADD
.primitive_type: SG_PRIMITIVETYPE_TRIANGLES
.index_type: SG_INDEXTYPE_NONE
.cull_mode: SG_CULLMODE_NONE
.face_winding: SG_FACEWINDING_CW
.sample_count: sg_desc.context.sample_count
.blend_color: (sg_color) { 0.0f, 0.0f, 0.0f, 0.0f }
.alpha_to_coverage_enabled: false
.label 0 (optional string label for trace hooks)
*/
typedef struct sg_vertex_buffer_layout_state {
int stride;
sg_vertex_step step_func;
int step_rate;
} sg_vertex_buffer_layout_state;
typedef struct sg_vertex_attr_state {
int buffer_index;
int offset;
sg_vertex_format format;
} sg_vertex_attr_state;
typedef struct sg_vertex_layout_state {
sg_vertex_buffer_layout_state buffers[SG_MAX_VERTEX_BUFFERS];
sg_vertex_attr_state attrs[SG_MAX_VERTEX_ATTRIBUTES];
} sg_vertex_layout_state;
typedef struct sg_stencil_face_state {
sg_compare_func compare;
sg_stencil_op fail_op;
sg_stencil_op depth_fail_op;
sg_stencil_op pass_op;
} sg_stencil_face_state;
typedef struct sg_stencil_state {
bool enabled;
sg_stencil_face_state front;
sg_stencil_face_state back;
uint8_t read_mask;
uint8_t write_mask;
uint8_t ref;
} sg_stencil_state;
typedef struct sg_depth_state {
sg_pixel_format pixel_format;
sg_compare_func compare;
bool write_enabled;
float bias;
float bias_slope_scale;
float bias_clamp;
} sg_depth_state;
typedef struct sg_blend_state {
bool enabled;
sg_blend_factor src_factor_rgb;
sg_blend_factor dst_factor_rgb;
sg_blend_op op_rgb;
sg_blend_factor src_factor_alpha;
sg_blend_factor dst_factor_alpha;
sg_blend_op op_alpha;
} sg_blend_state;
typedef struct sg_color_target_state {
sg_pixel_format pixel_format;
sg_color_mask write_mask;
sg_blend_state blend;
} sg_color_target_state;
typedef struct sg_pipeline_desc {
uint32_t _start_canary;
sg_shader shader;
sg_vertex_layout_state layout;
sg_depth_state depth;
sg_stencil_state stencil;
int color_count;
sg_color_target_state colors[SG_MAX_COLOR_ATTACHMENTS];
sg_primitive_type primitive_type;
sg_index_type index_type;
sg_cull_mode cull_mode;
sg_face_winding face_winding;
int sample_count;
sg_color blend_color;
bool alpha_to_coverage_enabled;
const char* label;
uint32_t _end_canary;
} sg_pipeline_desc;
/*
sg_attachments_desc
Creation parameters for an sg_attachments object, used as argument to the
sg_make_attachments() function.
An attachments object bundles 0..4 color attachments, 0..4 msaa-resolve
attachments, and none or one depth-stencil attachmente for use
in a render pass. At least one color attachment or one depth-stencil
attachment must be provided (no color attachment and a depth-stencil
attachment is useful for a depth-only render pass).
Each attachment definition consists of an image object, and two additional indices
describing which subimage the pass will render into: one mipmap index, and if the image
is a cubemap, array-texture or 3D-texture, the face-index, array-layer or
depth-slice.
All attachments must have the same width and height.
All color attachments and the depth-stencil attachment must have the
same sample count.
If a resolve attachment is set, an MSAA-resolve operation from the
associated color attachment image into the resolve attachment image will take
place in the sg_end_pass() function. In this case, the color attachment
must have a (sample_count>1), and the resolve attachment a
(sample_count==1). The resolve attachment also must have the same pixel
format as the color attachment.
NOTE that MSAA depth-stencil attachments cannot be msaa-resolved!
*/
typedef struct sg_attachment_desc {
sg_image image;
int mip_level;
int slice; // cube texture: face; array texture: layer; 3D texture: slice
} sg_attachment_desc;
typedef struct sg_attachments_desc {
uint32_t _start_canary;
sg_attachment_desc colors[SG_MAX_COLOR_ATTACHMENTS];
sg_attachment_desc resolves[SG_MAX_COLOR_ATTACHMENTS];
sg_attachment_desc depth_stencil;
const char* label;
uint32_t _end_canary;
} sg_attachments_desc;
/*
sg_trace_hooks
Installable callback functions to keep track of the sokol-gfx calls,
this is useful for debugging, or keeping track of resource creation
and destruction.
Trace hooks are installed with sg_install_trace_hooks(), this returns
another sg_trace_hooks struct with the previous set of
trace hook function pointers. These should be invoked by the
new trace hooks to form a proper call chain.
*/
typedef struct sg_trace_hooks {
void* user_data;
void (*reset_state_cache)(void* user_data);
void (*make_buffer)(const sg_buffer_desc* desc, sg_buffer result, void* user_data);
void (*make_image)(const sg_image_desc* desc, sg_image result, void* user_data);
void (*make_sampler)(const sg_sampler_desc* desc, sg_sampler result, void* user_data);
void (*make_shader)(const sg_shader_desc* desc, sg_shader result, void* user_data);
void (*make_pipeline)(const sg_pipeline_desc* desc, sg_pipeline result, void* user_data);
void (*make_attachments)(const sg_attachments_desc* desc, sg_attachments result, void* user_data);
void (*destroy_buffer)(sg_buffer buf, void* user_data);
void (*destroy_image)(sg_image img, void* user_data);
void (*destroy_sampler)(sg_sampler smp, void* user_data);
void (*destroy_shader)(sg_shader shd, void* user_data);
void (*destroy_pipeline)(sg_pipeline pip, void* user_data);
void (*destroy_attachments)(sg_attachments atts, void* user_data);
void (*update_buffer)(sg_buffer buf, const sg_range* data, void* user_data);
void (*update_image)(sg_image img, const sg_image_data* data, void* user_data);
void (*append_buffer)(sg_buffer buf, const sg_range* data, int result, void* user_data);
void (*begin_pass)(const sg_pass* pass, void* user_data);
void (*apply_viewport)(int x, int y, int width, int height, bool origin_top_left, void* user_data);
void (*apply_scissor_rect)(int x, int y, int width, int height, bool origin_top_left, void* user_data);
void (*apply_pipeline)(sg_pipeline pip, void* user_data);
void (*apply_bindings)(const sg_bindings* bindings, void* user_data);
void (*apply_uniforms)(sg_shader_stage stage, int ub_index, const sg_range* data, void* user_data);
void (*draw)(int base_element, int num_elements, int num_instances, void* user_data);
void (*end_pass)(void* user_data);
void (*commit)(void* user_data);
void (*alloc_buffer)(sg_buffer result, void* user_data);
void (*alloc_image)(sg_image result, void* user_data);
void (*alloc_sampler)(sg_sampler result, void* user_data);
void (*alloc_shader)(sg_shader result, void* user_data);
void (*alloc_pipeline)(sg_pipeline result, void* user_data);
void (*alloc_attachments)(sg_attachments result, void* user_data);
void (*dealloc_buffer)(sg_buffer buf_id, void* user_data);
void (*dealloc_image)(sg_image img_id, void* user_data);
void (*dealloc_sampler)(sg_sampler smp_id, void* user_data);
void (*dealloc_shader)(sg_shader shd_id, void* user_data);
void (*dealloc_pipeline)(sg_pipeline pip_id, void* user_data);
void (*dealloc_attachments)(sg_attachments atts_id, void* user_data);
void (*init_buffer)(sg_buffer buf_id, const sg_buffer_desc* desc, void* user_data);
void (*init_image)(sg_image img_id, const sg_image_desc* desc, void* user_data);
void (*init_sampler)(sg_sampler smp_id, const sg_sampler_desc* desc, void* user_data);
void (*init_shader)(sg_shader shd_id, const sg_shader_desc* desc, void* user_data);
void (*init_pipeline)(sg_pipeline pip_id, const sg_pipeline_desc* desc, void* user_data);
void (*init_attachments)(sg_attachments atts_id, const sg_attachments_desc* desc, void* user_data);
void (*uninit_buffer)(sg_buffer buf_id, void* user_data);
void (*uninit_image)(sg_image img_id, void* user_data);
void (*uninit_sampler)(sg_sampler smp_id, void* user_data);
void (*uninit_shader)(sg_shader shd_id, void* user_data);
void (*uninit_pipeline)(sg_pipeline pip_id, void* user_data);
void (*uninit_attachments)(sg_attachments atts_id, void* user_data);
void (*fail_buffer)(sg_buffer buf_id, void* user_data);
void (*fail_image)(sg_image img_id, void* user_data);
void (*fail_sampler)(sg_sampler smp_id, void* user_data);
void (*fail_shader)(sg_shader shd_id, void* user_data);
void (*fail_pipeline)(sg_pipeline pip_id, void* user_data);
void (*fail_attachments)(sg_attachments atts_id, void* user_data);
void (*push_debug_group)(const char* name, void* user_data);
void (*pop_debug_group)(void* user_data);
} sg_trace_hooks;
/*
sg_buffer_info
sg_image_info
sg_sampler_info
sg_shader_info
sg_pipeline_info
sg_attachments_info
These structs contain various internal resource attributes which
might be useful for debug-inspection. Please don't rely on the
actual content of those structs too much, as they are quite closely
tied to sokol_gfx.h internals and may change more frequently than
the other public API elements.
The *_info structs are used as the return values of the following functions:
sg_query_buffer_info()
sg_query_image_info()
sg_query_sampler_info()
sg_query_shader_info()
sg_query_pipeline_info()
sg_query_pass_info()
*/
typedef struct sg_slot_info {
sg_resource_state state; // the current state of this resource slot
uint32_t res_id; // type-neutral resource if (e.g. sg_buffer.id)
} sg_slot_info;
typedef struct sg_buffer_info {
sg_slot_info slot; // resource pool slot info
uint32_t update_frame_index; // frame index of last sg_update_buffer()
uint32_t append_frame_index; // frame index of last sg_append_buffer()
int append_pos; // current position in buffer for sg_append_buffer()
bool append_overflow; // is buffer in overflow state (due to sg_append_buffer)
int num_slots; // number of renaming-slots for dynamically updated buffers
int active_slot; // currently active write-slot for dynamically updated buffers
} sg_buffer_info;
typedef struct sg_image_info {
sg_slot_info slot; // resource pool slot info
uint32_t upd_frame_index; // frame index of last sg_update_image()
int num_slots; // number of renaming-slots for dynamically updated images
int active_slot; // currently active write-slot for dynamically updated images
} sg_image_info;
typedef struct sg_sampler_info {
sg_slot_info slot; // resource pool slot info
} sg_sampler_info;
typedef struct sg_shader_info {
sg_slot_info slot; // resource pool slot info
} sg_shader_info;
typedef struct sg_pipeline_info {
sg_slot_info slot; // resource pool slot info
} sg_pipeline_info;
typedef struct sg_attachments_info {
sg_slot_info slot; // resource pool slot info
} sg_attachments_info;
/*
sg_frame_stats
Allows to track generic and backend-specific stats about a
render frame. Obtained by calling sg_query_frame_stats(). The returned
struct contains information about the *previous* frame.
*/
typedef struct sg_frame_stats_gl {
uint32_t num_bind_buffer;
uint32_t num_active_texture;
uint32_t num_bind_texture;
uint32_t num_bind_sampler;
uint32_t num_use_program;
uint32_t num_render_state;
uint32_t num_vertex_attrib_pointer;
uint32_t num_vertex_attrib_divisor;
uint32_t num_enable_vertex_attrib_array;
uint32_t num_disable_vertex_attrib_array;
uint32_t num_uniform;
} sg_frame_stats_gl;
typedef struct sg_frame_stats_d3d11_pass {
uint32_t num_om_set_render_targets;
uint32_t num_clear_render_target_view;
uint32_t num_clear_depth_stencil_view;
uint32_t num_resolve_subresource;
} sg_frame_stats_d3d11_pass;
typedef struct sg_frame_stats_d3d11_pipeline {
uint32_t num_rs_set_state;
uint32_t num_om_set_depth_stencil_state;
uint32_t num_om_set_blend_state;
uint32_t num_ia_set_primitive_topology;
uint32_t num_ia_set_input_layout;
uint32_t num_vs_set_shader;
uint32_t num_vs_set_constant_buffers;
uint32_t num_ps_set_shader;
uint32_t num_ps_set_constant_buffers;
} sg_frame_stats_d3d11_pipeline;
typedef struct sg_frame_stats_d3d11_bindings {
uint32_t num_ia_set_vertex_buffers;
uint32_t num_ia_set_index_buffer;
uint32_t num_vs_set_shader_resources;
uint32_t num_ps_set_shader_resources;
uint32_t num_vs_set_samplers;
uint32_t num_ps_set_samplers;
} sg_frame_stats_d3d11_bindings;
typedef struct sg_frame_stats_d3d11_uniforms {
uint32_t num_update_subresource;
} sg_frame_stats_d3d11_uniforms;
typedef struct sg_frame_stats_d3d11_draw {
uint32_t num_draw_indexed_instanced;
uint32_t num_draw_indexed;
uint32_t num_draw_instanced;
uint32_t num_draw;
} sg_frame_stats_d3d11_draw;
typedef struct sg_frame_stats_d3d11 {
sg_frame_stats_d3d11_pass pass;
sg_frame_stats_d3d11_pipeline pipeline;
sg_frame_stats_d3d11_bindings bindings;
sg_frame_stats_d3d11_uniforms uniforms;
sg_frame_stats_d3d11_draw draw;
uint32_t num_map;
uint32_t num_unmap;
} sg_frame_stats_d3d11;
typedef struct sg_frame_stats_metal_idpool {
uint32_t num_added;
uint32_t num_released;
uint32_t num_garbage_collected;
} sg_frame_stats_metal_idpool;
typedef struct sg_frame_stats_metal_pipeline {
uint32_t num_set_blend_color;
uint32_t num_set_cull_mode;
uint32_t num_set_front_facing_winding;
uint32_t num_set_stencil_reference_value;
uint32_t num_set_depth_bias;
uint32_t num_set_render_pipeline_state;
uint32_t num_set_depth_stencil_state;
} sg_frame_stats_metal_pipeline;
typedef struct sg_frame_stats_metal_bindings {
uint32_t num_set_vertex_buffer;
uint32_t num_set_vertex_texture;
uint32_t num_set_vertex_sampler_state;
uint32_t num_set_fragment_buffer;
uint32_t num_set_fragment_texture;
uint32_t num_set_fragment_sampler_state;
} sg_frame_stats_metal_bindings;
typedef struct sg_frame_stats_metal_uniforms {
uint32_t num_set_vertex_buffer_offset;
uint32_t num_set_fragment_buffer_offset;
} sg_frame_stats_metal_uniforms;
typedef struct sg_frame_stats_metal {
sg_frame_stats_metal_idpool idpool;
sg_frame_stats_metal_pipeline pipeline;
sg_frame_stats_metal_bindings bindings;
sg_frame_stats_metal_uniforms uniforms;
} sg_frame_stats_metal;
typedef struct sg_frame_stats_wgpu_uniforms {
uint32_t num_set_bindgroup;
uint32_t size_write_buffer;
} sg_frame_stats_wgpu_uniforms;
typedef struct sg_frame_stats_wgpu_bindings {
uint32_t num_set_vertex_buffer;
uint32_t num_skip_redundant_vertex_buffer;
uint32_t num_set_index_buffer;
uint32_t num_skip_redundant_index_buffer;
uint32_t num_create_bindgroup;
uint32_t num_discard_bindgroup;
uint32_t num_set_bindgroup;
uint32_t num_skip_redundant_bindgroup;
uint32_t num_bindgroup_cache_hits;
uint32_t num_bindgroup_cache_misses;
uint32_t num_bindgroup_cache_collisions;
uint32_t num_bindgroup_cache_invalidates;
uint32_t num_bindgroup_cache_hash_vs_key_mismatch;
} sg_frame_stats_wgpu_bindings;
typedef struct sg_frame_stats_wgpu {
sg_frame_stats_wgpu_uniforms uniforms;
sg_frame_stats_wgpu_bindings bindings;
} sg_frame_stats_wgpu;
typedef struct sg_frame_stats {
uint32_t frame_index; // current frame counter, starts at 0
uint32_t num_passes;
uint32_t num_apply_viewport;
uint32_t num_apply_scissor_rect;
uint32_t num_apply_pipeline;
uint32_t num_apply_bindings;
uint32_t num_apply_uniforms;
uint32_t num_draw;
uint32_t num_update_buffer;
uint32_t num_append_buffer;
uint32_t num_update_image;
uint32_t size_apply_uniforms;
uint32_t size_update_buffer;
uint32_t size_append_buffer;
uint32_t size_update_image;
sg_frame_stats_gl gl;
sg_frame_stats_d3d11 d3d11;
sg_frame_stats_metal metal;
sg_frame_stats_wgpu wgpu;
} sg_frame_stats;
/*
sg_log_item
An enum with a unique item for each log message, warning, error
and validation layer message.
*/
#define _SG_LOG_ITEMS \
_SG_LOGITEM_XMACRO(OK, "Ok") \
_SG_LOGITEM_XMACRO(MALLOC_FAILED, "memory allocation failed") \
_SG_LOGITEM_XMACRO(GL_TEXTURE_FORMAT_NOT_SUPPORTED, "pixel format not supported for texture (gl)") \
_SG_LOGITEM_XMACRO(GL_3D_TEXTURES_NOT_SUPPORTED, "3d textures not supported (gl)") \
_SG_LOGITEM_XMACRO(GL_ARRAY_TEXTURES_NOT_SUPPORTED, "array textures not supported (gl)") \
_SG_LOGITEM_XMACRO(GL_SHADER_COMPILATION_FAILED, "shader compilation failed (gl)") \
_SG_LOGITEM_XMACRO(GL_SHADER_LINKING_FAILED, "shader linking failed (gl)") \
_SG_LOGITEM_XMACRO(GL_VERTEX_ATTRIBUTE_NOT_FOUND_IN_SHADER, "vertex attribute not found in shader (gl)") \
_SG_LOGITEM_XMACRO(GL_TEXTURE_NAME_NOT_FOUND_IN_SHADER, "texture name not found in shader (gl)") \
_SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_UNDEFINED, "framebuffer completeness check failed with GL_FRAMEBUFFER_UNDEFINED (gl)") \
_SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_INCOMPLETE_ATTACHMENT, "framebuffer completeness check failed with GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT (gl)") \
_SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MISSING_ATTACHMENT, "framebuffer completeness check failed with GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT (gl)") \
_SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_UNSUPPORTED, "framebuffer completeness check failed with GL_FRAMEBUFFER_UNSUPPORTED (gl)") \
_SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MULTISAMPLE, "framebuffer completeness check failed with GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE (gl)") \
_SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_UNKNOWN, "framebuffer completeness check failed (unknown reason) (gl)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_BUFFER_FAILED, "CreateBuffer() failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_BUFFER_SRV_FAILED, "CreateShaderResourceView() failed for storage buffer (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_DEPTH_TEXTURE_UNSUPPORTED_PIXEL_FORMAT, "pixel format not supported for depth-stencil texture (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_DEPTH_TEXTURE_FAILED, "CreateTexture2D() failed for depth-stencil texture (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_2D_TEXTURE_UNSUPPORTED_PIXEL_FORMAT, "pixel format not supported for 2d-, cube- or array-texture (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_2D_TEXTURE_FAILED, "CreateTexture2D() failed for 2d-, cube- or array-texture (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_2D_SRV_FAILED, "CreateShaderResourceView() failed for 2d-, cube- or array-texture (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_3D_TEXTURE_UNSUPPORTED_PIXEL_FORMAT, "pixel format not supported for 3D texture (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_3D_TEXTURE_FAILED, "CreateTexture3D() failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_3D_SRV_FAILED, "CreateShaderResourceView() failed for 3d texture (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_MSAA_TEXTURE_FAILED, "CreateTexture2D() failed for MSAA render target texture (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_SAMPLER_STATE_FAILED, "CreateSamplerState() failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_LOAD_D3DCOMPILER_47_DLL_FAILED, "loading d3dcompiler_47.dll failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_SHADER_COMPILATION_FAILED, "shader compilation failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_SHADER_COMPILATION_OUTPUT, "") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_CONSTANT_BUFFER_FAILED, "CreateBuffer() failed for uniform constant buffer (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_INPUT_LAYOUT_FAILED, "CreateInputLayout() failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_RASTERIZER_STATE_FAILED, "CreateRasterizerState() failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_DEPTH_STENCIL_STATE_FAILED, "CreateDepthStencilState() failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_BLEND_STATE_FAILED, "CreateBlendState() failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_RTV_FAILED, "CreateRenderTargetView() failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_CREATE_DSV_FAILED, "CreateDepthStencilView() failed (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_MAP_FOR_UPDATE_BUFFER_FAILED, "Map() failed when updating buffer (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_MAP_FOR_APPEND_BUFFER_FAILED, "Map() failed when appending to buffer (d3d11)") \
_SG_LOGITEM_XMACRO(D3D11_MAP_FOR_UPDATE_IMAGE_FAILED, "Map() failed when updating image (d3d11)") \
_SG_LOGITEM_XMACRO(METAL_CREATE_BUFFER_FAILED, "failed to create buffer object (metal)") \
_SG_LOGITEM_XMACRO(METAL_TEXTURE_FORMAT_NOT_SUPPORTED, "pixel format not supported for texture (metal)") \
_SG_LOGITEM_XMACRO(METAL_CREATE_TEXTURE_FAILED, "failed to create texture object (metal)") \
_SG_LOGITEM_XMACRO(METAL_CREATE_SAMPLER_FAILED, "failed to create sampler object (metal)") \
_SG_LOGITEM_XMACRO(METAL_SHADER_COMPILATION_FAILED, "shader compilation failed (metal)") \
_SG_LOGITEM_XMACRO(METAL_SHADER_CREATION_FAILED, "shader creation failed (metal)") \
_SG_LOGITEM_XMACRO(METAL_SHADER_COMPILATION_OUTPUT, "") \
_SG_LOGITEM_XMACRO(METAL_VERTEX_SHADER_ENTRY_NOT_FOUND, "vertex shader entry function not found (metal)") \
_SG_LOGITEM_XMACRO(METAL_FRAGMENT_SHADER_ENTRY_NOT_FOUND, "fragment shader entry not found (metal)") \
_SG_LOGITEM_XMACRO(METAL_CREATE_RPS_FAILED, "failed to create render pipeline state (metal)") \
_SG_LOGITEM_XMACRO(METAL_CREATE_RPS_OUTPUT, "") \
_SG_LOGITEM_XMACRO(METAL_CREATE_DSS_FAILED, "failed to create depth stencil state (metal)") \
_SG_LOGITEM_XMACRO(WGPU_BINDGROUPS_POOL_EXHAUSTED, "bindgroups pool exhausted (increase sg_desc.bindgroups_cache_size) (wgpu)") \
_SG_LOGITEM_XMACRO(WGPU_BINDGROUPSCACHE_SIZE_GREATER_ONE, "sg_desc.wgpu_bindgroups_cache_size must be > 1 (wgpu)") \
_SG_LOGITEM_XMACRO(WGPU_BINDGROUPSCACHE_SIZE_POW2, "sg_desc.wgpu_bindgroups_cache_size must be a power of 2 (wgpu)") \
_SG_LOGITEM_XMACRO(WGPU_CREATEBINDGROUP_FAILED, "wgpuDeviceCreateBindGroup failed") \
_SG_LOGITEM_XMACRO(WGPU_CREATE_BUFFER_FAILED, "wgpuDeviceCreateBuffer() failed") \
_SG_LOGITEM_XMACRO(WGPU_CREATE_TEXTURE_FAILED, "wgpuDeviceCreateTexture() failed") \
_SG_LOGITEM_XMACRO(WGPU_CREATE_TEXTURE_VIEW_FAILED, "wgpuTextureCreateView() failed") \
_SG_LOGITEM_XMACRO(WGPU_CREATE_SAMPLER_FAILED, "wgpuDeviceCreateSampler() failed") \
_SG_LOGITEM_XMACRO(WGPU_CREATE_SHADER_MODULE_FAILED, "wgpuDeviceCreateShaderModule() failed") \
_SG_LOGITEM_XMACRO(WGPU_SHADER_TOO_MANY_IMAGES, "shader uses too many sampled images on shader stage (wgpu)") \
_SG_LOGITEM_XMACRO(WGPU_SHADER_TOO_MANY_SAMPLERS, "shader uses too many samplers on shader stage (wgpu)") \
_SG_LOGITEM_XMACRO(WGPU_SHADER_TOO_MANY_STORAGEBUFFERS, "shader uses too many storage buffer bindings on shader stage (wgpu)") \
_SG_LOGITEM_XMACRO(WGPU_SHADER_CREATE_BINDGROUP_LAYOUT_FAILED, "wgpuDeviceCreateBindGroupLayout() for shader stage failed") \
_SG_LOGITEM_XMACRO(WGPU_CREATE_PIPELINE_LAYOUT_FAILED, "wgpuDeviceCreatePipelineLayout() failed") \
_SG_LOGITEM_XMACRO(WGPU_CREATE_RENDER_PIPELINE_FAILED, "wgpuDeviceCreateRenderPipeline() failed") \
_SG_LOGITEM_XMACRO(WGPU_ATTACHMENTS_CREATE_TEXTURE_VIEW_FAILED, "wgpuTextureCreateView() failed in create attachments") \
_SG_LOGITEM_XMACRO(IDENTICAL_COMMIT_LISTENER, "attempting to add identical commit listener") \
_SG_LOGITEM_XMACRO(COMMIT_LISTENER_ARRAY_FULL, "commit listener array full") \
_SG_LOGITEM_XMACRO(TRACE_HOOKS_NOT_ENABLED, "sg_install_trace_hooks() called, but SOKOL_TRACE_HOOKS is not defined") \
_SG_LOGITEM_XMACRO(DEALLOC_BUFFER_INVALID_STATE, "sg_dealloc_buffer(): buffer must be in ALLOC state") \
_SG_LOGITEM_XMACRO(DEALLOC_IMAGE_INVALID_STATE, "sg_dealloc_image(): image must be in alloc state") \
_SG_LOGITEM_XMACRO(DEALLOC_SAMPLER_INVALID_STATE, "sg_dealloc_sampler(): sampler must be in alloc state") \
_SG_LOGITEM_XMACRO(DEALLOC_SHADER_INVALID_STATE, "sg_dealloc_shader(): shader must be in ALLOC state") \
_SG_LOGITEM_XMACRO(DEALLOC_PIPELINE_INVALID_STATE, "sg_dealloc_pipeline(): pipeline must be in ALLOC state") \
_SG_LOGITEM_XMACRO(DEALLOC_ATTACHMENTS_INVALID_STATE, "sg_dealloc_attachments(): attachments must be in ALLOC state") \
_SG_LOGITEM_XMACRO(INIT_BUFFER_INVALID_STATE, "sg_init_buffer(): buffer must be in ALLOC state") \
_SG_LOGITEM_XMACRO(INIT_IMAGE_INVALID_STATE, "sg_init_image(): image must be in ALLOC state") \
_SG_LOGITEM_XMACRO(INIT_SAMPLER_INVALID_STATE, "sg_init_sampler(): sampler must be in ALLOC state") \
_SG_LOGITEM_XMACRO(INIT_SHADER_INVALID_STATE, "sg_init_shader(): shader must be in ALLOC state") \
_SG_LOGITEM_XMACRO(INIT_PIPELINE_INVALID_STATE, "sg_init_pipeline(): pipeline must be in ALLOC state") \
_SG_LOGITEM_XMACRO(INIT_ATTACHMENTS_INVALID_STATE, "sg_init_attachments(): pass must be in ALLOC state") \
_SG_LOGITEM_XMACRO(UNINIT_BUFFER_INVALID_STATE, "sg_uninit_buffer(): buffer must be in VALID or FAILED state") \
_SG_LOGITEM_XMACRO(UNINIT_IMAGE_INVALID_STATE, "sg_uninit_image(): image must be in VALID or FAILED state") \
_SG_LOGITEM_XMACRO(UNINIT_SAMPLER_INVALID_STATE, "sg_uninit_sampler(): sampler must be in VALID or FAILED state") \
_SG_LOGITEM_XMACRO(UNINIT_SHADER_INVALID_STATE, "sg_uninit_shader(): shader must be in VALID or FAILED state") \
_SG_LOGITEM_XMACRO(UNINIT_PIPELINE_INVALID_STATE, "sg_uninit_pipeline(): pipeline must be in VALID or FAILED state") \
_SG_LOGITEM_XMACRO(UNINIT_ATTACHMENTS_INVALID_STATE, "sg_uninit_attachments(): attachments must be in VALID or FAILED state") \
_SG_LOGITEM_XMACRO(FAIL_BUFFER_INVALID_STATE, "sg_fail_buffer(): buffer must be in ALLOC state") \
_SG_LOGITEM_XMACRO(FAIL_IMAGE_INVALID_STATE, "sg_fail_image(): image must be in ALLOC state") \
_SG_LOGITEM_XMACRO(FAIL_SAMPLER_INVALID_STATE, "sg_fail_sampler(): sampler must be in ALLOC state") \
_SG_LOGITEM_XMACRO(FAIL_SHADER_INVALID_STATE, "sg_fail_shader(): shader must be in ALLOC state") \
_SG_LOGITEM_XMACRO(FAIL_PIPELINE_INVALID_STATE, "sg_fail_pipeline(): pipeline must be in ALLOC state") \
_SG_LOGITEM_XMACRO(FAIL_ATTACHMENTS_INVALID_STATE, "sg_fail_attachments(): attachments must be in ALLOC state") \
_SG_LOGITEM_XMACRO(BUFFER_POOL_EXHAUSTED, "buffer pool exhausted") \
_SG_LOGITEM_XMACRO(IMAGE_POOL_EXHAUSTED, "image pool exhausted") \
_SG_LOGITEM_XMACRO(SAMPLER_POOL_EXHAUSTED, "sampler pool exhausted") \
_SG_LOGITEM_XMACRO(SHADER_POOL_EXHAUSTED, "shader pool exhausted") \
_SG_LOGITEM_XMACRO(PIPELINE_POOL_EXHAUSTED, "pipeline pool exhausted") \
_SG_LOGITEM_XMACRO(PASS_POOL_EXHAUSTED, "pass pool exhausted") \
_SG_LOGITEM_XMACRO(BEGINPASS_ATTACHMENT_INVALID, "sg_begin_pass: an attachment was provided that no longer exists") \
_SG_LOGITEM_XMACRO(DRAW_WITHOUT_BINDINGS, "attempting to draw without resource bindings") \
_SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_CANARY, "sg_buffer_desc not initialized") \
_SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_SIZE, "sg_buffer_desc.size and .data.size cannot both be 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_DATA, "immutable buffers must be initialized with data (sg_buffer_desc.data.ptr and sg_buffer_desc.data.size)") \
_SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_DATA_SIZE, "immutable buffer data size differs from buffer size") \
_SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_NO_DATA, "dynamic/stream usage buffers cannot be initialized with data") \
_SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_STORAGEBUFFER_SUPPORTED, "storage buffers not supported by the backend 3D API (requires OpenGL >= 4.3)") \
_SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_STORAGEBUFFER_SIZE_MULTIPLE_4, "size of storage buffers must be a multiple of 4") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDATA_NODATA, "sg_image_data: no data (.ptr and/or .size is zero)") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDATA_DATA_SIZE, "sg_image_data: data size doesn't match expected surface size") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_CANARY, "sg_image_desc not initialized") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_WIDTH, "sg_image_desc.width must be > 0") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_HEIGHT, "sg_image_desc.height must be > 0") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_RT_PIXELFORMAT, "invalid pixel format for render-target image") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_NONRT_PIXELFORMAT, "invalid pixel format for non-render-target image") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_MSAA_BUT_NO_RT, "non-render-target images cannot be multisampled") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_NO_MSAA_RT_SUPPORT, "MSAA not supported for this pixel format") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_MSAA_NUM_MIPMAPS, "MSAA images must have num_mipmaps == 1") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_MSAA_3D_IMAGE, "3D images cannot have a sample_count > 1") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_DEPTH_3D_IMAGE, "3D images cannot have a depth/stencil image format") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_RT_IMMUTABLE, "render target images must be SG_USAGE_IMMUTABLE") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_RT_NO_DATA, "render target images cannot be initialized with data") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_INJECTED_NO_DATA, "images with injected textures cannot be initialized with data") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_DYNAMIC_NO_DATA, "dynamic/stream images cannot be initialized with data") \
_SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_COMPRESSED_IMMUTABLE, "compressed images must be immutable") \
_SG_LOGITEM_XMACRO(VALIDATE_SAMPLERDESC_CANARY, "sg_sampler_desc not initialized") \
_SG_LOGITEM_XMACRO(VALIDATE_SAMPLERDESC_ANISTROPIC_REQUIRES_LINEAR_FILTERING, "sg_sampler_desc.max_anisotropy > 1 requires min/mag/mipmap_filter to be SG_FILTER_LINEAR") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_CANARY, "sg_shader_desc not initialized") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_SOURCE, "shader source code required") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_BYTECODE, "shader byte code required") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE, "shader source or byte code required") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_BYTECODE_SIZE, "shader byte code length (in bytes) required") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_UBS, "shader uniform blocks must occupy continuous slots") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_UB_MEMBERS, "uniform block members must occupy continuous slots") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_UB_MEMBERS, "GL backend requires uniform block member declarations") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_UB_MEMBER_NAME, "uniform block member name missing") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_UB_SIZE_MISMATCH, "size of uniform block members doesn't match uniform block size") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_UB_ARRAY_COUNT, "uniform array count must be >= 1") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_UB_STD140_ARRAY_TYPE, "uniform arrays only allowed for FLOAT4, INT4, MAT4 in std140 layout") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_STORAGEBUFFERS, "shader stage storage buffers must occupy continuous slots (sg_shader_desc.vs|fs.storage_buffers[])") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_STORAGEBUFFER_READONLY, "shader stage storage buffers must be readonly (sg_shader_desc.vs|fs.storage_buffers[].readonly)") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_IMAGES, "shader stage images must occupy continuous slots (sg_shader_desc.vs|fs.images[])") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_SAMPLERS, "shader stage samplers must occupy continuous slots (sg_shader_desc.vs|fs.samplers[])") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_IMAGE_SLOT_OUT_OF_RANGE, "shader stage: image-sampler-pair image slot index is out of range (sg_shader_desc.vs|fs.image_sampler_pairs[].image_slot)") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_SAMPLER_SLOT_OUT_OF_RANGE, "shader stage: image-sampler-pair image slot index is out of range (sg_shader_desc.vs|fs.image_sampler_pairs[].sampler_slot)") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_NAME_REQUIRED_FOR_GL, "shader stage: image-sampler-pairs must be named in GL (sg_shader_desc.vs|fs.image_sampler_pairs[].name)") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_NAME_BUT_NOT_USED, "shader stage: image-sampler-pair has name but .used field not true") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_IMAGE_BUT_NOT_USED, "shader stage: image-sampler-pair has .image_slot != 0 but .used field not true") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_SAMPLER_BUT_NOT_USED, "shader stage: image-sampler-pair .sampler_slot != 0 but .used field not true") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NONFILTERING_SAMPLER_REQUIRED, "shader stage: image sample type UNFILTERABLE_FLOAT, UINT, SINT can only be used with NONFILTERING sampler") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_COMPARISON_SAMPLER_REQUIRED, "shader stage: image sample type DEPTH can only be used with COMPARISON sampler") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_NOT_REFERENCED_BY_IMAGE_SAMPLER_PAIRS, "shader stage: one or more images are note referenced by (sg_shader_desc.vs|fs.image_sampler_pairs[].image_slot)") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_SAMPLER_NOT_REFERENCED_BY_IMAGE_SAMPLER_PAIRS, "shader stage: one or more samplers are not referenced by image-sampler-pairs (sg_shader_desc.vs|fs.image_sampler_pairs[].sampler_slot)") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_IMAGE_SAMPLER_PAIRS, "shader stage image-sampler-pairs must occupy continuous slots (sg_shader_desc.vs|fs.image_samplers[])") \
_SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG, "vertex attribute name/semantic string too long (max len 16)") \
_SG_LOGITEM_XMACRO(VALIDATE_PIPELINEDESC_CANARY, "sg_pipeline_desc not initialized") \
_SG_LOGITEM_XMACRO(VALIDATE_PIPELINEDESC_SHADER, "sg_pipeline_desc.shader missing or invalid") \
_SG_LOGITEM_XMACRO(VALIDATE_PIPELINEDESC_NO_CONT_ATTRS, "sg_pipeline_desc.layout.attrs is not continuous") \
_SG_LOGITEM_XMACRO(VALIDATE_PIPELINEDESC_LAYOUT_STRIDE4, "sg_pipeline_desc.layout.buffers[].stride must be multiple of 4") \
_SG_LOGITEM_XMACRO(VALIDATE_PIPELINEDESC_ATTR_SEMANTICS, "D3D11 missing vertex attribute semantics in shader") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_CANARY, "sg_attachments_desc not initialized") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_NO_ATTACHMENTS, "sg_attachments_desc no color or depth-stencil attachments") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_NO_CONT_COLOR_ATTS, "color attachments must occupy continuous slots") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_IMAGE, "pass attachment image is not valid") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_MIPLEVEL, "pass attachment mip level is bigger than image has mipmaps") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_FACE, "pass attachment image is cubemap, but face index is too big") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_LAYER, "pass attachment image is array texture, but layer index is too big") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_SLICE, "pass attachment image is 3d texture, but slice value is too big") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_IMAGE_NO_RT, "pass attachment image must be have render_target=true") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_COLOR_INV_PIXELFORMAT, "pass color-attachment images must be renderable color pixel format") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_INV_PIXELFORMAT, "pass depth-attachment image must be depth or depth-stencil pixel format") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_IMAGE_SIZES, "all pass attachments must have the same size") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_IMAGE_SAMPLE_COUNTS, "all pass attachments must have the same sample count") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_COLOR_IMAGE_MSAA, "pass resolve attachments must have a color attachment image with sample count > 1") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE, "pass resolve attachment image not valid") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_SAMPLE_COUNT, "pass resolve attachment image sample count must be 1") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_MIPLEVEL, "pass resolve attachment mip level is bigger than image has mipmaps") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_FACE, "pass resolve attachment is cubemap, but face index is too big") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_LAYER, "pass resolve attachment is array texture, but layer index is too big") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_SLICE, "pass resolve attachment is 3d texture, but slice value is too big") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_NO_RT, "pass resolve attachment image must have render_target=true") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_SIZES, "pass resolve attachment size must match color attachment image size") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_FORMAT, "pass resolve attachment pixel format must match color attachment pixel format") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE, "pass depth attachment image is not valid") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_MIPLEVEL, "pass depth attachment mip level is bigger than image has mipmaps") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_FACE, "pass depth attachment image is cubemap, but face index is too big") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_LAYER, "pass depth attachment image is array texture, but layer index is too big") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_SLICE, "pass depth attachment image is 3d texture, but slice value is too big") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_NO_RT, "pass depth attachment image must be have render_target=true") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_SIZES, "pass depth attachment image size must match color attachment image size") \
_SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_SAMPLE_COUNT, "pass depth attachment sample count must match color attachment sample count") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_CANARY, "sg_begin_pass: pass struct not initialized") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_ATTACHMENTS_EXISTS, "sg_begin_pass: attachments object no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_ATTACHMENTS_VALID, "sg_begin_pass: attachments object not in resource state VALID") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_COLOR_ATTACHMENT_IMAGE, "sg_begin_pass: one or more color attachment images are not valid") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_RESOLVE_ATTACHMENT_IMAGE, "sg_begin_pass: one or more resolve attachment images are not valid") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_DEPTHSTENCIL_ATTACHMENT_IMAGE, "sg_begin_pass: one or more depth-stencil attachment images are not valid") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_WIDTH, "sg_begin_pass: expected pass.swapchain.width > 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_WIDTH_NOTSET, "sg_begin_pass: expected pass.swapchain.width == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_HEIGHT, "sg_begin_pass: expected pass.swapchain.height > 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_HEIGHT_NOTSET, "sg_begin_pass: expected pass.swapchain.height == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_SAMPLECOUNT, "sg_begin_pass: expected pass.swapchain.sample_count > 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_SAMPLECOUNT_NOTSET, "sg_begin_pass: expected pass.swapchain.sample_count == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_COLORFORMAT, "sg_begin_pass: expected pass.swapchain.color_format to be valid") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_COLORFORMAT_NOTSET, "sg_begin_pass: expected pass.swapchain.color_format to be unset") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_DEPTHFORMAT_NOTSET, "sg_begin_pass: expected pass.swapchain.depth_format to be unset") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_CURRENTDRAWABLE, "sg_begin_pass: expected pass.swapchain.metal.current_drawable != 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_CURRENTDRAWABLE_NOTSET, "sg_begin_pass: expected pass.swapchain.metal.current_drawable == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_DEPTHSTENCILTEXTURE, "sg_begin_pass: expected pass.swapchain.metal.depth_stencil_texture != 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_DEPTHSTENCILTEXTURE_NOTSET, "sg_begin_pass: expected pass.swapchain.metal.depth_stencil_texture == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_MSAACOLORTEXTURE, "sg_begin_pass: expected pass.swapchain.metal.msaa_color_texture != 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_MSAACOLORTEXTURE_NOTSET, "sg_begin_pass: expected pass.swapchain.metal.msaa_color_texture == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RENDERVIEW, "sg_begin_pass: expected pass.swapchain.d3d11.render_view != 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RENDERVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.d3d11.render_view == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RESOLVEVIEW, "sg_begin_pass: expected pass.swapchain.d3d11.resolve_view != 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RESOLVEVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.d3d11.resolve_view == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_DEPTHSTENCILVIEW, "sg_begin_pass: expected pass.swapchain.d3d11.depth_stencil_view != 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_DEPTHSTENCILVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.d3d11.depth_stencil_view == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RENDERVIEW, "sg_begin_pass: expected pass.swapchain.wgpu.render_view != 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RENDERVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.wgpu.render_view == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RESOLVEVIEW, "sg_begin_pass: expected pass.swapchain.wgpu.resolve_view != 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RESOLVEVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.wgpu.resolve_view == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_DEPTHSTENCILVIEW, "sg_begin_pass: expected pass.swapchain.wgpu.depth_stencil_view != 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_DEPTHSTENCILVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.wgpu.depth_stencil_view == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_GL_EXPECT_FRAMEBUFFER_NOTSET, "sg_begin_pass: expected pass.swapchain.gl.framebuffer == 0") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_PIPELINE_VALID_ID, "sg_apply_pipeline: invalid pipeline id provided") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_PIPELINE_EXISTS, "sg_apply_pipeline: pipeline object no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_PIPELINE_VALID, "sg_apply_pipeline: pipeline object not in valid state") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_SHADER_EXISTS, "sg_apply_pipeline: shader object no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_SHADER_VALID, "sg_apply_pipeline: shader object not in valid state") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_CURPASS_ATTACHMENTS_EXISTS, "sg_apply_pipeline: current pass attachments no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_CURPASS_ATTACHMENTS_VALID, "sg_apply_pipeline: current pass attachments not in valid state") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_ATT_COUNT, "sg_apply_pipeline: number of pipeline color attachments doesn't match number of pass color attachments") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_COLOR_FORMAT, "sg_apply_pipeline: pipeline color attachment pixel format doesn't match pass color attachment pixel format") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_DEPTH_FORMAT, "sg_apply_pipeline: pipeline depth pixel_format doesn't match pass depth attachment pixel format") \
_SG_LOGITEM_XMACRO(VALIDATE_APIP_SAMPLE_COUNT, "sg_apply_pipeline: pipeline MSAA sample count doesn't match render pass attachment sample count") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_PIPELINE, "sg_apply_bindings: must be called after sg_apply_pipeline") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_PIPELINE_EXISTS, "sg_apply_bindings: currently applied pipeline object no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_PIPELINE_VALID, "sg_apply_bindings: currently applied pipeline object not in valid state") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VBS, "sg_apply_bindings: number of vertex buffers doesn't match number of pipeline vertex layouts") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VB_EXISTS, "sg_apply_bindings: vertex buffer no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VB_TYPE, "sg_apply_bindings: buffer in vertex buffer slot is not a SG_BUFFERTYPE_VERTEXBUFFER") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VB_OVERFLOW, "sg_apply_bindings: buffer in vertex buffer slot is overflown") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_NO_IB, "sg_apply_bindings: pipeline object defines indexed rendering, but no index buffer provided") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_IB, "sg_apply_bindings: pipeline object defines non-indexed rendering, but index buffer provided") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_IB_EXISTS, "sg_apply_bindings: index buffer no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_IB_TYPE, "sg_apply_bindings: buffer in index buffer slot is not a SG_BUFFERTYPE_INDEXBUFFER") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_IB_OVERFLOW, "sg_apply_bindings: buffer in index buffer slot is overflown") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_IMAGE_BINDING, "sg_apply_bindings: image binding on vertex stage is missing or the image handle is invalid") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_IMG_EXISTS, "sg_apply_bindings: image bound to vertex stage no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_IMAGE_TYPE_MISMATCH, "sg_apply_bindings: type of image bound to vertex stage doesn't match shader desc") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_IMAGE_MSAA, "sg_apply_bindings: cannot bind image with sample_count>1 to vertex stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_FILTERABLE_IMAGE, "sg_apply_bindings: filterable image expected on vertex stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_DEPTH_IMAGE, "sg_apply_bindings: depth image expected on vertex stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_UNEXPECTED_IMAGE_BINDING, "sg_apply_bindings: unexpected image binding on vertex stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_SAMPLER_BINDING, "sg_apply_bindings: sampler binding on vertex stage is missing or the sampler handle is invalid") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_UNEXPECTED_SAMPLER_COMPARE_NEVER, "sg_apply_bindings: shader expects SG_SAMPLERTYPE_COMPARISON on vertex stage but sampler has SG_COMPAREFUNC_NEVER") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_SAMPLER_COMPARE_NEVER, "sg_apply_bindings: shader expects SG_SAMPLERTYPE_FILTERING or SG_SAMPLERTYPE_NONFILTERING on vertex stage but sampler doesn't have SG_COMPAREFUNC_NEVER") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_NONFILTERING_SAMPLER, "sg_apply_bindings: shader expected SG_SAMPLERTYPE_NONFILTERING on vertex stage, but sampler has SG_FILTER_LINEAR filters") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_UNEXPECTED_SAMPLER_BINDING, "sg_apply_bindings: unexpected sampler binding on vertex stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_SMP_EXISTS, "sg_apply_bindings: sampler bound to vertex stage no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_STORAGEBUFFER_BINDING, "sg_apply_bindings: storage buffer binding on vertex stage is missing or the buffer handle is invalid") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_STORAGEBUFFER_EXISTS, "sg_apply_bindings: storage buffer bound to vertex stage no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_STORAGEBUFFER_BINDING_BUFFERTYPE, "sg_apply_bindings: buffer bound to vertex stage storage buffer slot is not of type storage buffer") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_UNEXPECTED_STORAGEBUFFER_BINDING, "sg_apply_bindings: unexpected storage buffer binding on vertex stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_IMAGE_BINDING, "sg_apply_bindings: image binding on fragment stage is missing or the image handle is invalid") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_IMG_EXISTS, "sg_apply_bindings: image bound to fragment stage no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_IMAGE_TYPE_MISMATCH, "sg_apply_bindings: type of image bound to fragment stage doesn't match shader desc") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_IMAGE_MSAA, "sg_apply_bindings: cannot bind image with sample_count>1 to fragment stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_FILTERABLE_IMAGE, "sg_apply_bindings: filterable image expected on fragment stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_DEPTH_IMAGE, "sg_apply_bindings: depth image expected on fragment stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_UNEXPECTED_IMAGE_BINDING, "sg_apply_bindings: unexpected image binding on fragment stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_SAMPLER_BINDING, "sg_apply_bindings: sampler binding on fragment stage is missing or the sampler handle is invalid") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_UNEXPECTED_SAMPLER_COMPARE_NEVER, "sg_apply_bindings: shader expects SG_SAMPLERTYPE_COMPARISON on fragment stage but sampler has SG_COMPAREFUNC_NEVER") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_SAMPLER_COMPARE_NEVER, "sg_apply_bindings: shader expects SG_SAMPLERTYPE_FILTERING on fragment stage but sampler doesn't have SG_COMPAREFUNC_NEVER") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_NONFILTERING_SAMPLER, "sg_apply_bindings: shader expected SG_SAMPLERTYPE_NONFILTERING on fragment stage, but sampler has SG_FILTER_LINEAR filters") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_UNEXPECTED_SAMPLER_BINDING, "sg_apply_bindings: unexpected sampler binding on fragment stage") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_SMP_EXISTS, "sg_apply_bindings: sampler bound to fragment stage no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_STORAGEBUFFER_BINDING, "sg_apply_bindings: storage buffer binding on fragment stage is missing or the buffer handle is invalid") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_STORAGEBUFFER_EXISTS, "sg_apply_bindings: storage buffer bound to fragment stage no longer alive") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_STORAGEBUFFER_BINDING_BUFFERTYPE, "sg_apply_bindings: buffer bound to frahment stage storage buffer slot is not of type storage buffer") \
_SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_UNEXPECTED_STORAGEBUFFER_BINDING, "sg_apply_bindings: unexpected storage buffer binding on fragment stage") \
_SG_LOGITEM_XMACRO(VALIDATE_AUB_NO_PIPELINE, "sg_apply_uniforms: must be called after sg_apply_pipeline()") \
_SG_LOGITEM_XMACRO(VALIDATE_AUB_NO_UB_AT_SLOT, "sg_apply_uniforms: no uniform block declaration at this shader stage UB slot") \
_SG_LOGITEM_XMACRO(VALIDATE_AUB_SIZE, "sg_apply_uniforms: data size doesn't match declared uniform block size") \
_SG_LOGITEM_XMACRO(VALIDATE_UPDATEBUF_USAGE, "sg_update_buffer: cannot update immutable buffer") \
_SG_LOGITEM_XMACRO(VALIDATE_UPDATEBUF_SIZE, "sg_update_buffer: update size is bigger than buffer size") \
_SG_LOGITEM_XMACRO(VALIDATE_UPDATEBUF_ONCE, "sg_update_buffer: only one update allowed per buffer and frame") \
_SG_LOGITEM_XMACRO(VALIDATE_UPDATEBUF_APPEND, "sg_update_buffer: cannot call sg_update_buffer and sg_append_buffer in same frame") \
_SG_LOGITEM_XMACRO(VALIDATE_APPENDBUF_USAGE, "sg_append_buffer: cannot append to immutable buffer") \
_SG_LOGITEM_XMACRO(VALIDATE_APPENDBUF_SIZE, "sg_append_buffer: overall appended size is bigger than buffer size") \
_SG_LOGITEM_XMACRO(VALIDATE_APPENDBUF_UPDATE, "sg_append_buffer: cannot call sg_append_buffer and sg_update_buffer in same frame") \
_SG_LOGITEM_XMACRO(VALIDATE_UPDIMG_USAGE, "sg_update_image: cannot update immutable image") \
_SG_LOGITEM_XMACRO(VALIDATE_UPDIMG_ONCE, "sg_update_image: only one update allowed per image and frame") \
_SG_LOGITEM_XMACRO(VALIDATION_FAILED, "validation layer checks failed") \
#define _SG_LOGITEM_XMACRO(item,msg) SG_LOGITEM_##item,
typedef enum sg_log_item {
_SG_LOG_ITEMS
} sg_log_item;
#undef _SG_LOGITEM_XMACRO
/*
sg_desc
The sg_desc struct contains configuration values for sokol_gfx,
it is used as parameter to the sg_setup() call.
The default configuration is:
.buffer_pool_size 128
.image_pool_size 128
.sampler_pool_size 64
.shader_pool_size 32
.pipeline_pool_size 64
.pass_pool_size 16
.uniform_buffer_size 4 MB (4*1024*1024)
.max_commit_listeners 1024
.disable_validation false
.mtl_force_managed_storage_mode false
.wgpu_disable_bindgroups_cache false
.wgpu_bindgroups_cache_size 1024
.allocator.alloc_fn 0 (in this case, malloc() will be called)
.allocator.free_fn 0 (in this case, free() will be called)
.allocator.user_data 0
.environment.defaults.color_format: default value depends on selected backend:
all GL backends: SG_PIXELFORMAT_RGBA8
Metal and D3D11: SG_PIXELFORMAT_BGRA8
WebGPU: *no default* (must be queried from WebGPU swapchain object)
.environment.defaults.depth_format: SG_PIXELFORMAT_DEPTH_STENCIL
.environment.defaults.sample_count: 1
Metal specific:
(NOTE: All Objective-C object references are transferred through
a bridged (const void*) to sokol_gfx, which will use a unretained
bridged cast (__bridged id<xxx>) to retrieve the Objective-C
references back. Since the bridge cast is unretained, the caller
must hold a strong reference to the Objective-C object for the
duration of the sokol_gfx call!
.mtl_force_managed_storage_mode
when enabled, Metal buffers and texture resources are created in managed storage
mode, otherwise sokol-gfx will decide whether to create buffers and
textures in managed or shared storage mode (this is mainly a debugging option)
.mtl_use_command_buffer_with_retained_references
when true, the sokol-gfx Metal backend will use Metal command buffers which
bump the reference count of resource objects as long as they are inflight,
this is slower than the default command-buffer-with-unretained-references
method, this may be a workaround when confronted with lifetime validation
errors from the Metal validation layer until a proper fix has been implemented
.environment.metal.device
a pointer to the MTLDevice object
D3D11 specific:
.environment.d3d11.device
a pointer to the ID3D11Device object, this must have been created
before sg_setup() is called
.environment.d3d11.device_context
a pointer to the ID3D11DeviceContext object
.d3d11_shader_debugging
set this to true to compile shaders which are provided as HLSL source
code with debug information and without optimization, this allows
shader debugging in tools like RenderDoc, to output source code
instead of byte code from sokol-shdc, omit the `--binary` cmdline
option
WebGPU specific:
.wgpu_disable_bindgroups_cache
When this is true, the WebGPU backend will create and immediately
release a BindGroup object in the sg_apply_bindings() call, only
use this for debugging purposes.
.wgpu_bindgroups_cache_size
The size of the bindgroups cache for re-using BindGroup objects
between sg_apply_bindings() calls. The smaller the cache size,
the more likely are cache slot collisions which will cause
a BindGroups object to be destroyed and a new one created.
Use the information returned by sg_query_stats() to check
if this is a frequent occurrence, and increase the cache size as
needed (the default is 1024).
NOTE: wgpu_bindgroups_cache_size must be a power-of-2 number!
.environment.wgpu.device
a WGPUDevice handle
When using sokol_gfx.h and sokol_app.h together, consider using the
helper function sglue_environment() in the sokol_glue.h header to
initialize the sg_desc.environment nested struct. sglue_environment() returns
a completely initialized sg_environment struct with information
provided by sokol_app.h.
*/
typedef struct sg_environment_defaults {
sg_pixel_format color_format;
sg_pixel_format depth_format;
int sample_count;
} sg_environment_defaults;
typedef struct sg_metal_environment {
const void* device;
} sg_metal_environment;
typedef struct sg_d3d11_environment {
const void* device;
const void* device_context;
} sg_d3d11_environment;
typedef struct sg_wgpu_environment {
const void* device;
} sg_wgpu_environment;
typedef struct sg_environment {
sg_environment_defaults defaults;
sg_metal_environment metal;
sg_d3d11_environment d3d11;
sg_wgpu_environment wgpu;
} sg_environment;
/*
sg_commit_listener
Used with function sg_add_commit_listener() to add a callback
which will be called in sg_commit(). This is useful for libraries
building on top of sokol-gfx to be notified about when a frame
ends (instead of having to guess, or add a manual 'new-frame'
function.
*/
typedef struct sg_commit_listener {
void (*func)(void* user_data);
void* user_data;
} sg_commit_listener;
/*
sg_allocator
Used in sg_desc to provide custom memory-alloc and -free functions
to sokol_gfx.h. If memory management should be overridden, both the
alloc_fn and free_fn function must be provided (e.g. it's not valid to
override one function but not the other).
*/
typedef struct sg_allocator {
void* (*alloc_fn)(size_t size, void* user_data);
void (*free_fn)(void* ptr, void* user_data);
void* user_data;
} sg_allocator;
/*
sg_logger
Used in sg_desc to provide a logging function. Please be aware
that without logging function, sokol-gfx will be completely
silent, e.g. it will not report errors, warnings and
validation layer messages. For maximum error verbosity,
compile in debug mode (e.g. NDEBUG *not* defined) and provide a
compatible logger function in the sg_setup() call
(for instance the standard logging function from sokol_log.h).
*/
typedef struct sg_logger {
void (*func)(
const char* tag, // always "sg"
uint32_t log_level, // 0=panic, 1=error, 2=warning, 3=info
uint32_t log_item_id, // SG_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_gfx.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data);
void* user_data;
} sg_logger;
typedef struct sg_desc {
uint32_t _start_canary;
int buffer_pool_size;
int image_pool_size;
int sampler_pool_size;
int shader_pool_size;
int pipeline_pool_size;
int attachments_pool_size;
int uniform_buffer_size;
int max_commit_listeners;
bool disable_validation; // disable validation layer even in debug mode, useful for tests
bool d3d11_shader_debugging; // if true, HLSL shaders are compiled with D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION
bool mtl_force_managed_storage_mode; // for debugging: use Metal managed storage mode for resources even with UMA
bool mtl_use_command_buffer_with_retained_references; // Metal: use a managed MTLCommandBuffer which ref-counts used resources
bool wgpu_disable_bindgroups_cache; // set to true to disable the WebGPU backend BindGroup cache
int wgpu_bindgroups_cache_size; // number of slots in the WebGPU bindgroup cache (must be 2^N)
sg_allocator allocator;
sg_logger logger; // optional log function override
sg_environment environment;
uint32_t _end_canary;
} sg_desc;
// setup and misc functions
SOKOL_GFX_API_DECL void sg_setup(const sg_desc* desc);
SOKOL_GFX_API_DECL void sg_shutdown(void);
SOKOL_GFX_API_DECL bool sg_isvalid(void);
SOKOL_GFX_API_DECL void sg_reset_state_cache(void);
SOKOL_GFX_API_DECL sg_trace_hooks sg_install_trace_hooks(const sg_trace_hooks* trace_hooks);
SOKOL_GFX_API_DECL void sg_push_debug_group(const char* name);
SOKOL_GFX_API_DECL void sg_pop_debug_group(void);
SOKOL_GFX_API_DECL bool sg_add_commit_listener(sg_commit_listener listener);
SOKOL_GFX_API_DECL bool sg_remove_commit_listener(sg_commit_listener listener);
// resource creation, destruction and updating
SOKOL_GFX_API_DECL sg_buffer sg_make_buffer(const sg_buffer_desc* desc);
SOKOL_GFX_API_DECL sg_image sg_make_image(const sg_image_desc* desc);
SOKOL_GFX_API_DECL sg_sampler sg_make_sampler(const sg_sampler_desc* desc);
SOKOL_GFX_API_DECL sg_shader sg_make_shader(const sg_shader_desc* desc);
SOKOL_GFX_API_DECL sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc);
SOKOL_GFX_API_DECL sg_attachments sg_make_attachments(const sg_attachments_desc* desc);
SOKOL_GFX_API_DECL void sg_destroy_buffer(sg_buffer buf);
SOKOL_GFX_API_DECL void sg_destroy_image(sg_image img);
SOKOL_GFX_API_DECL void sg_destroy_sampler(sg_sampler smp);
SOKOL_GFX_API_DECL void sg_destroy_shader(sg_shader shd);
SOKOL_GFX_API_DECL void sg_destroy_pipeline(sg_pipeline pip);
SOKOL_GFX_API_DECL void sg_destroy_attachments(sg_attachments atts);
SOKOL_GFX_API_DECL void sg_update_buffer(sg_buffer buf, const sg_range* data);
SOKOL_GFX_API_DECL void sg_update_image(sg_image img, const sg_image_data* data);
SOKOL_GFX_API_DECL int sg_append_buffer(sg_buffer buf, const sg_range* data);
SOKOL_GFX_API_DECL bool sg_query_buffer_overflow(sg_buffer buf);
SOKOL_GFX_API_DECL bool sg_query_buffer_will_overflow(sg_buffer buf, size_t size);
// rendering functions
SOKOL_GFX_API_DECL void sg_begin_pass(const sg_pass* pass);
SOKOL_GFX_API_DECL void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left);
SOKOL_GFX_API_DECL void sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left);
SOKOL_GFX_API_DECL void sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left);
SOKOL_GFX_API_DECL void sg_apply_scissor_rectf(float x, float y, float width, float height, bool origin_top_left);
SOKOL_GFX_API_DECL void sg_apply_pipeline(sg_pipeline pip);
SOKOL_GFX_API_DECL void sg_apply_bindings(const sg_bindings* bindings);
SOKOL_GFX_API_DECL void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range* data);
SOKOL_GFX_API_DECL void sg_draw(int base_element, int num_elements, int num_instances);
SOKOL_GFX_API_DECL void sg_end_pass(void);
SOKOL_GFX_API_DECL void sg_commit(void);
// getting information
SOKOL_GFX_API_DECL sg_desc sg_query_desc(void);
SOKOL_GFX_API_DECL sg_backend sg_query_backend(void);
SOKOL_GFX_API_DECL sg_features sg_query_features(void);
SOKOL_GFX_API_DECL sg_limits sg_query_limits(void);
SOKOL_GFX_API_DECL sg_pixelformat_info sg_query_pixelformat(sg_pixel_format fmt);
SOKOL_GFX_API_DECL int sg_query_row_pitch(sg_pixel_format fmt, int width, int row_align_bytes);
SOKOL_GFX_API_DECL int sg_query_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align_bytes);
// get current state of a resource (INITIAL, ALLOC, VALID, FAILED, INVALID)
SOKOL_GFX_API_DECL sg_resource_state sg_query_buffer_state(sg_buffer buf);
SOKOL_GFX_API_DECL sg_resource_state sg_query_image_state(sg_image img);
SOKOL_GFX_API_DECL sg_resource_state sg_query_sampler_state(sg_sampler smp);
SOKOL_GFX_API_DECL sg_resource_state sg_query_shader_state(sg_shader shd);
SOKOL_GFX_API_DECL sg_resource_state sg_query_pipeline_state(sg_pipeline pip);
SOKOL_GFX_API_DECL sg_resource_state sg_query_attachments_state(sg_attachments atts);
// get runtime information about a resource
SOKOL_GFX_API_DECL sg_buffer_info sg_query_buffer_info(sg_buffer buf);
SOKOL_GFX_API_DECL sg_image_info sg_query_image_info(sg_image img);
SOKOL_GFX_API_DECL sg_sampler_info sg_query_sampler_info(sg_sampler smp);
SOKOL_GFX_API_DECL sg_shader_info sg_query_shader_info(sg_shader shd);
SOKOL_GFX_API_DECL sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip);
SOKOL_GFX_API_DECL sg_attachments_info sg_query_attachments_info(sg_attachments atts);
// get desc structs matching a specific resource (NOTE that not all creation attributes may be provided)
SOKOL_GFX_API_DECL sg_buffer_desc sg_query_buffer_desc(sg_buffer buf);
SOKOL_GFX_API_DECL sg_image_desc sg_query_image_desc(sg_image img);
SOKOL_GFX_API_DECL sg_sampler_desc sg_query_sampler_desc(sg_sampler smp);
SOKOL_GFX_API_DECL sg_shader_desc sg_query_shader_desc(sg_shader shd);
SOKOL_GFX_API_DECL sg_pipeline_desc sg_query_pipeline_desc(sg_pipeline pip);
SOKOL_GFX_API_DECL sg_attachments_desc sg_query_attachments_desc(sg_attachments atts);
// get resource creation desc struct with their default values replaced
SOKOL_GFX_API_DECL sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc);
SOKOL_GFX_API_DECL sg_image_desc sg_query_image_defaults(const sg_image_desc* desc);
SOKOL_GFX_API_DECL sg_sampler_desc sg_query_sampler_defaults(const sg_sampler_desc* desc);
SOKOL_GFX_API_DECL sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc);
SOKOL_GFX_API_DECL sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc);
SOKOL_GFX_API_DECL sg_attachments_desc sg_query_attachments_defaults(const sg_attachments_desc* desc);
// separate resource allocation and initialization (for async setup)
SOKOL_GFX_API_DECL sg_buffer sg_alloc_buffer(void);
SOKOL_GFX_API_DECL sg_image sg_alloc_image(void);
SOKOL_GFX_API_DECL sg_sampler sg_alloc_sampler(void);
SOKOL_GFX_API_DECL sg_shader sg_alloc_shader(void);
SOKOL_GFX_API_DECL sg_pipeline sg_alloc_pipeline(void);
SOKOL_GFX_API_DECL sg_attachments sg_alloc_attachments(void);
SOKOL_GFX_API_DECL void sg_dealloc_buffer(sg_buffer buf);
SOKOL_GFX_API_DECL void sg_dealloc_image(sg_image img);
SOKOL_GFX_API_DECL void sg_dealloc_sampler(sg_sampler smp);
SOKOL_GFX_API_DECL void sg_dealloc_shader(sg_shader shd);
SOKOL_GFX_API_DECL void sg_dealloc_pipeline(sg_pipeline pip);
SOKOL_GFX_API_DECL void sg_dealloc_attachments(sg_attachments attachments);
SOKOL_GFX_API_DECL void sg_init_buffer(sg_buffer buf, const sg_buffer_desc* desc);
SOKOL_GFX_API_DECL void sg_init_image(sg_image img, const sg_image_desc* desc);
SOKOL_GFX_API_DECL void sg_init_sampler(sg_sampler smg, const sg_sampler_desc* desc);
SOKOL_GFX_API_DECL void sg_init_shader(sg_shader shd, const sg_shader_desc* desc);
SOKOL_GFX_API_DECL void sg_init_pipeline(sg_pipeline pip, const sg_pipeline_desc* desc);
SOKOL_GFX_API_DECL void sg_init_attachments(sg_attachments attachments, const sg_attachments_desc* desc);
SOKOL_GFX_API_DECL void sg_uninit_buffer(sg_buffer buf);
SOKOL_GFX_API_DECL void sg_uninit_image(sg_image img);
SOKOL_GFX_API_DECL void sg_uninit_sampler(sg_sampler smp);
SOKOL_GFX_API_DECL void sg_uninit_shader(sg_shader shd);
SOKOL_GFX_API_DECL void sg_uninit_pipeline(sg_pipeline pip);
SOKOL_GFX_API_DECL void sg_uninit_attachments(sg_attachments atts);
SOKOL_GFX_API_DECL void sg_fail_buffer(sg_buffer buf);
SOKOL_GFX_API_DECL void sg_fail_image(sg_image img);
SOKOL_GFX_API_DECL void sg_fail_sampler(sg_sampler smp);
SOKOL_GFX_API_DECL void sg_fail_shader(sg_shader shd);
SOKOL_GFX_API_DECL void sg_fail_pipeline(sg_pipeline pip);
SOKOL_GFX_API_DECL void sg_fail_attachments(sg_attachments atts);
// frame stats
SOKOL_GFX_API_DECL void sg_enable_frame_stats(void);
SOKOL_GFX_API_DECL void sg_disable_frame_stats(void);
SOKOL_GFX_API_DECL bool sg_frame_stats_enabled(void);
SOKOL_GFX_API_DECL sg_frame_stats sg_query_frame_stats(void);
/* Backend-specific structs and functions, these may come in handy for mixing
sokol-gfx rendering with 'native backend' rendering functions.
This group of functions will be expanded as needed.
*/
typedef struct sg_d3d11_buffer_info {
const void* buf; // ID3D11Buffer*
} sg_d3d11_buffer_info;
typedef struct sg_d3d11_image_info {
const void* tex2d; // ID3D11Texture2D*
const void* tex3d; // ID3D11Texture3D*
const void* res; // ID3D11Resource* (either tex2d or tex3d)
const void* srv; // ID3D11ShaderResourceView*
} sg_d3d11_image_info;
typedef struct sg_d3d11_sampler_info {
const void* smp; // ID3D11SamplerState*
} sg_d3d11_sampler_info;
typedef struct sg_d3d11_shader_info {
const void* vs_cbufs[SG_MAX_SHADERSTAGE_UBS]; // ID3D11Buffer* (vertex stage constant buffers)
const void* fs_cbufs[SG_MAX_SHADERSTAGE_UBS]; // ID3D11Buffer* (fragment stage constant buffers)
const void* vs; // ID3D11VertexShader*
const void* fs; // ID3D11PixelShader*
} sg_d3d11_shader_info;
typedef struct sg_d3d11_pipeline_info {
const void* il; // ID3D11InputLayout*
const void* rs; // ID3D11RasterizerState*
const void* dss; // ID3D11DepthStencilState*
const void* bs; // ID3D11BlendState*
} sg_d3d11_pipeline_info;
typedef struct sg_d3d11_attachments_info {
const void* color_rtv[SG_MAX_COLOR_ATTACHMENTS]; // ID3D11RenderTargetView
const void* resolve_rtv[SG_MAX_COLOR_ATTACHMENTS]; // ID3D11RenderTargetView
const void* dsv; // ID3D11DepthStencilView
} sg_d3d11_attachments_info;
typedef struct sg_mtl_buffer_info {
const void* buf[SG_NUM_INFLIGHT_FRAMES]; // id<MTLBuffer>
int active_slot;
} sg_mtl_buffer_info;
typedef struct sg_mtl_image_info {
const void* tex[SG_NUM_INFLIGHT_FRAMES]; // id<MTLTexture>
int active_slot;
} sg_mtl_image_info;
typedef struct sg_mtl_sampler_info {
const void* smp; // id<MTLSamplerState>
} sg_mtl_sampler_info;
typedef struct sg_mtl_shader_info {
const void* vs_lib; // id<MTLLibrary>
const void* fs_lib; // id<MTLLibrary>
const void* vs_func; // id<MTLFunction>
const void* fs_func; // id<MTLFunction>
} sg_mtl_shader_info;
typedef struct sg_mtl_pipeline_info {
const void* rps; // id<MTLRenderPipelineState>
const void* dss; // id<MTLDepthStencilState>
} sg_mtl_pipeline_info;
typedef struct sg_wgpu_buffer_info {
const void* buf; // WGPUBuffer
} sg_wgpu_buffer_info;
typedef struct sg_wgpu_image_info {
const void* tex; // WGPUTexture
const void* view; // WGPUTextureView
} sg_wgpu_image_info;
typedef struct sg_wgpu_sampler_info {
const void* smp; // WGPUSampler
} sg_wgpu_sampler_info;
typedef struct sg_wgpu_shader_info {
const void* vs_mod; // WGPUShaderModule
const void* fs_mod; // WGPUShaderModule
const void* bgl; // WGPUBindGroupLayout;
} sg_wgpu_shader_info;
typedef struct sg_wgpu_pipeline_info {
const void* pip; // WGPURenderPipeline
} sg_wgpu_pipeline_info;
typedef struct sg_wgpu_attachments_info {
const void* color_view[SG_MAX_COLOR_ATTACHMENTS]; // WGPUTextureView
const void* resolve_view[SG_MAX_COLOR_ATTACHMENTS]; // WGPUTextureView
const void* ds_view; // WGPUTextureView
} sg_wgpu_attachments_info;
typedef struct sg_gl_buffer_info {
uint32_t buf[SG_NUM_INFLIGHT_FRAMES];
int active_slot;
} sg_gl_buffer_info;
typedef struct sg_gl_image_info {
uint32_t tex[SG_NUM_INFLIGHT_FRAMES];
uint32_t tex_target;
uint32_t msaa_render_buffer;
int active_slot;
} sg_gl_image_info;
typedef struct sg_gl_sampler_info {
uint32_t smp;
} sg_gl_sampler_info;
typedef struct sg_gl_shader_info {
uint32_t prog;
} sg_gl_shader_info;
typedef struct sg_gl_attachments_info {
uint32_t framebuffer;
uint32_t msaa_resolve_framebuffer[SG_MAX_COLOR_ATTACHMENTS];
} sg_gl_attachments_info;
// D3D11: return ID3D11Device
SOKOL_GFX_API_DECL const void* sg_d3d11_device(void);
// D3D11: return ID3D11DeviceContext
SOKOL_GFX_API_DECL const void* sg_d3d11_device_context(void);
// D3D11: get internal buffer resource objects
SOKOL_GFX_API_DECL sg_d3d11_buffer_info sg_d3d11_query_buffer_info(sg_buffer buf);
// D3D11: get internal image resource objects
SOKOL_GFX_API_DECL sg_d3d11_image_info sg_d3d11_query_image_info(sg_image img);
// D3D11: get internal sampler resource objects
SOKOL_GFX_API_DECL sg_d3d11_sampler_info sg_d3d11_query_sampler_info(sg_sampler smp);
// D3D11: get internal shader resource objects
SOKOL_GFX_API_DECL sg_d3d11_shader_info sg_d3d11_query_shader_info(sg_shader shd);
// D3D11: get internal pipeline resource objects
SOKOL_GFX_API_DECL sg_d3d11_pipeline_info sg_d3d11_query_pipeline_info(sg_pipeline pip);
// D3D11: get internal pass resource objects
SOKOL_GFX_API_DECL sg_d3d11_attachments_info sg_d3d11_query_attachments_info(sg_attachments atts);
// Metal: return __bridge-casted MTLDevice
SOKOL_GFX_API_DECL const void* sg_mtl_device(void);
// Metal: return __bridge-casted MTLRenderCommandEncoder in current pass (or zero if outside pass)
SOKOL_GFX_API_DECL const void* sg_mtl_render_command_encoder(void);
// Metal: get internal __bridge-casted buffer resource objects
SOKOL_GFX_API_DECL sg_mtl_buffer_info sg_mtl_query_buffer_info(sg_buffer buf);
// Metal: get internal __bridge-casted image resource objects
SOKOL_GFX_API_DECL sg_mtl_image_info sg_mtl_query_image_info(sg_image img);
// Metal: get internal __bridge-casted sampler resource objects
SOKOL_GFX_API_DECL sg_mtl_sampler_info sg_mtl_query_sampler_info(sg_sampler smp);
// Metal: get internal __bridge-casted shader resource objects
SOKOL_GFX_API_DECL sg_mtl_shader_info sg_mtl_query_shader_info(sg_shader shd);
// Metal: get internal __bridge-casted pipeline resource objects
SOKOL_GFX_API_DECL sg_mtl_pipeline_info sg_mtl_query_pipeline_info(sg_pipeline pip);
// WebGPU: return WGPUDevice object
SOKOL_GFX_API_DECL const void* sg_wgpu_device(void);
// WebGPU: return WGPUQueue object
SOKOL_GFX_API_DECL const void* sg_wgpu_queue(void);
// WebGPU: return this frame's WGPUCommandEncoder
SOKOL_GFX_API_DECL const void* sg_wgpu_command_encoder(void);
// WebGPU: return WGPURenderPassEncoder of current pass
SOKOL_GFX_API_DECL const void* sg_wgpu_render_pass_encoder(void);
// WebGPU: get internal buffer resource objects
SOKOL_GFX_API_DECL sg_wgpu_buffer_info sg_wgpu_query_buffer_info(sg_buffer buf);
// WebGPU: get internal image resource objects
SOKOL_GFX_API_DECL sg_wgpu_image_info sg_wgpu_query_image_info(sg_image img);
// WebGPU: get internal sampler resource objects
SOKOL_GFX_API_DECL sg_wgpu_sampler_info sg_wgpu_query_sampler_info(sg_sampler smp);
// WebGPU: get internal shader resource objects
SOKOL_GFX_API_DECL sg_wgpu_shader_info sg_wgpu_query_shader_info(sg_shader shd);
// WebGPU: get internal pipeline resource objects
SOKOL_GFX_API_DECL sg_wgpu_pipeline_info sg_wgpu_query_pipeline_info(sg_pipeline pip);
// WebGPU: get internal pass resource objects
SOKOL_GFX_API_DECL sg_wgpu_attachments_info sg_wgpu_query_attachments_info(sg_attachments atts);
// GL: get internal buffer resource objects
SOKOL_GFX_API_DECL sg_gl_buffer_info sg_gl_query_buffer_info(sg_buffer buf);
// GL: get internal image resource objects
SOKOL_GFX_API_DECL sg_gl_image_info sg_gl_query_image_info(sg_image img);
// GL: get internal sampler resource objects
SOKOL_GFX_API_DECL sg_gl_sampler_info sg_gl_query_sampler_info(sg_sampler smp);
// GL: get internal shader resource objects
SOKOL_GFX_API_DECL sg_gl_shader_info sg_gl_query_shader_info(sg_shader shd);
// GL: get internal pass resource objects
SOKOL_GFX_API_DECL sg_gl_attachments_info sg_gl_query_attachments_info(sg_attachments atts);
#ifdef __cplusplus
} // extern "C"
// reference-based equivalents for c++
inline void sg_setup(const sg_desc& desc) { return sg_setup(&desc); }
inline sg_buffer sg_make_buffer(const sg_buffer_desc& desc) { return sg_make_buffer(&desc); }
inline sg_image sg_make_image(const sg_image_desc& desc) { return sg_make_image(&desc); }
inline sg_sampler sg_make_sampler(const sg_sampler_desc& desc) { return sg_make_sampler(&desc); }
inline sg_shader sg_make_shader(const sg_shader_desc& desc) { return sg_make_shader(&desc); }
inline sg_pipeline sg_make_pipeline(const sg_pipeline_desc& desc) { return sg_make_pipeline(&desc); }
inline sg_attachments sg_make_attachments(const sg_attachments_desc& desc) { return sg_make_attachments(&desc); }
inline void sg_update_image(sg_image img, const sg_image_data& data) { return sg_update_image(img, &data); }
inline void sg_begin_pass(const sg_pass& pass) { return sg_begin_pass(&pass); }
inline void sg_apply_bindings(const sg_bindings& bindings) { return sg_apply_bindings(&bindings); }
inline void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range& data) { return sg_apply_uniforms(stage, ub_index, &data); }
inline sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc& desc) { return sg_query_buffer_defaults(&desc); }
inline sg_image_desc sg_query_image_defaults(const sg_image_desc& desc) { return sg_query_image_defaults(&desc); }
inline sg_sampler_desc sg_query_sampler_defaults(const sg_sampler_desc& desc) { return sg_query_sampler_defaults(&desc); }
inline sg_shader_desc sg_query_shader_defaults(const sg_shader_desc& desc) { return sg_query_shader_defaults(&desc); }
inline sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc& desc) { return sg_query_pipeline_defaults(&desc); }
inline sg_attachments_desc sg_query_attachments_defaults(const sg_attachments_desc& desc) { return sg_query_attachments_defaults(&desc); }
inline void sg_init_buffer(sg_buffer buf, const sg_buffer_desc& desc) { return sg_init_buffer(buf, &desc); }
inline void sg_init_image(sg_image img, const sg_image_desc& desc) { return sg_init_image(img, &desc); }
inline void sg_init_sampler(sg_sampler smp, const sg_sampler_desc& desc) { return sg_init_sampler(smp, &desc); }
inline void sg_init_shader(sg_shader shd, const sg_shader_desc& desc) { return sg_init_shader(shd, &desc); }
inline void sg_init_pipeline(sg_pipeline pip, const sg_pipeline_desc& desc) { return sg_init_pipeline(pip, &desc); }
inline void sg_init_attachments(sg_attachments atts, const sg_attachments_desc& desc) { return sg_init_attachments(atts, &desc); }
inline void sg_update_buffer(sg_buffer buf_id, const sg_range& data) { return sg_update_buffer(buf_id, &data); }
inline int sg_append_buffer(sg_buffer buf_id, const sg_range& data) { return sg_append_buffer(buf_id, &data); }
#endif
#endif // SOKOL_GFX_INCLUDED
// ██ ███ ███ ██████ ██ ███████ ███ ███ ███████ ███ ██ ████████ █████ ████████ ██ ██████ ███ ██
// ██ ████ ████ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
// ██ ██ ████ ██ ██████ ██ █████ ██ ████ ██ █████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ███████ ███████ ██ ██ ███████ ██ ████ ██ ██ ██ ██ ██ ██████ ██ ████
//
// >>implementation
#ifdef SOKOL_GFX_IMPL
#define SOKOL_GFX_IMPL_INCLUDED (1)
#if !(defined(SOKOL_GLCORE)||defined(SOKOL_GLES3)||defined(SOKOL_D3D11)||defined(SOKOL_METAL)||defined(SOKOL_WGPU)||defined(SOKOL_DUMMY_BACKEND))
#error "Please select a backend with SOKOL_GLCORE, SOKOL_GLES3, SOKOL_D3D11, SOKOL_METAL, SOKOL_WGPU or SOKOL_DUMMY_BACKEND"
#endif
#if defined(SOKOL_MALLOC) || defined(SOKOL_CALLOC) || defined(SOKOL_FREE)
#error "SOKOL_MALLOC/CALLOC/FREE macros are no longer supported, please use sg_desc.allocator to override memory allocation functions"
#endif
#include <stdlib.h> // malloc, free
#include <string.h> // memset
#include <float.h> // FLT_MAX
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
#ifndef SOKOL_DEBUG
#ifndef NDEBUG
#define SOKOL_DEBUG
#endif
#endif
#ifndef SOKOL_ASSERT
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
#ifndef SOKOL_UNREACHABLE
#define SOKOL_UNREACHABLE SOKOL_ASSERT(false)
#endif
#ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static
#else
#define _SOKOL_PRIVATE static
#endif
#endif
#ifndef _SOKOL_UNUSED
#define _SOKOL_UNUSED(x) (void)(x)
#endif
#if defined(SOKOL_TRACE_HOOKS)
#define _SG_TRACE_ARGS(fn, ...) if (_sg.hooks.fn) { _sg.hooks.fn(__VA_ARGS__, _sg.hooks.user_data); }
#define _SG_TRACE_NOARGS(fn) if (_sg.hooks.fn) { _sg.hooks.fn(_sg.hooks.user_data); }
#else
#define _SG_TRACE_ARGS(fn, ...)
#define _SG_TRACE_NOARGS(fn)
#endif
// default clear values
#ifndef SG_DEFAULT_CLEAR_RED
#define SG_DEFAULT_CLEAR_RED (0.5f)
#endif
#ifndef SG_DEFAULT_CLEAR_GREEN
#define SG_DEFAULT_CLEAR_GREEN (0.5f)
#endif
#ifndef SG_DEFAULT_CLEAR_BLUE
#define SG_DEFAULT_CLEAR_BLUE (0.5f)
#endif
#ifndef SG_DEFAULT_CLEAR_ALPHA
#define SG_DEFAULT_CLEAR_ALPHA (1.0f)
#endif
#ifndef SG_DEFAULT_CLEAR_DEPTH
#define SG_DEFAULT_CLEAR_DEPTH (1.0f)
#endif
#ifndef SG_DEFAULT_CLEAR_STENCIL
#define SG_DEFAULT_CLEAR_STENCIL (0)
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4115) // named type definition in parentheses
#pragma warning(disable:4505) // unreferenced local function has been removed
#pragma warning(disable:4201) // nonstandard extension used: nameless struct/union (needed by d3d11.h)
#pragma warning(disable:4054) // 'type cast': from function pointer
#pragma warning(disable:4055) // 'type cast': from data pointer
#endif
#if defined(SOKOL_D3D11)
#ifndef D3D11_NO_HELPERS
#define D3D11_NO_HELPERS
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <d3d11.h>
#include <d3dcompiler.h>
#ifdef _MSC_VER
#pragma comment (lib, "kernel32")
#pragma comment (lib, "user32")
#pragma comment (lib, "dxgi")
#pragma comment (lib, "d3d11")
#endif
#elif defined(SOKOL_METAL)
// see https://clang.llvm.org/docs/LanguageExtensions.html#automatic-reference-counting
#if !defined(__cplusplus)
#if __has_feature(objc_arc) && !__has_feature(objc_arc_fields)
#error "sokol_gfx.h requires __has_feature(objc_arc_field) if ARC is enabled (use a more recent compiler version)"
#endif
#endif
#include <TargetConditionals.h>
#include <AvailabilityMacros.h>
#if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE
#define _SG_TARGET_MACOS (1)
#else
#define _SG_TARGET_IOS (1)
#if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR
#define _SG_TARGET_IOS_SIMULATOR (1)
#endif
#endif
#import <Metal/Metal.h>
#import <QuartzCore/CoreAnimation.h> // needed for CAMetalDrawable
#elif defined(SOKOL_WGPU)
#include <webgpu/webgpu.h>
#if defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h>
#endif
#elif defined(SOKOL_GLCORE) || defined(SOKOL_GLES3)
#define _SOKOL_ANY_GL (1)
// include platform specific GL headers (or on Win32: use an embedded GL loader)
#if !defined(SOKOL_EXTERNAL_GL_LOADER)
#if defined(_WIN32)
#if defined(SOKOL_GLCORE) && !defined(SOKOL_EXTERNAL_GL_LOADER)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#define _SOKOL_USE_WIN32_GL_LOADER (1)
#pragma comment (lib, "kernel32") // GetProcAddress()
#endif
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#ifndef GL_SILENCE_DEPRECATION
#define GL_SILENCE_DEPRECATION
#endif
#if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE
#include <OpenGL/gl3.h>
#else
#include <OpenGLES/ES3/gl.h>
#include <OpenGLES/ES3/glext.h>
#endif
#elif defined(__EMSCRIPTEN__) || defined(__ANDROID__)
#if defined(SOKOL_GLES3)
#include <GLES3/gl3.h>
#endif
#elif defined(__linux__) || defined(__unix__)
#if defined(SOKOL_GLCORE)
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#else
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#endif
#endif
#endif
// optional GL loader definitions (only on Win32)
#if defined(_SOKOL_USE_WIN32_GL_LOADER)
#define __gl_h_ 1
#define __gl32_h_ 1
#define __gl31_h_ 1
#define __GL_H__ 1
#define __glext_h_ 1
#define __GLEXT_H_ 1
#define __gltypes_h_ 1
#define __glcorearb_h_ 1
#define __gl_glcorearb_h_ 1
#define GL_APIENTRY APIENTRY
typedef unsigned int GLenum;
typedef unsigned int GLuint;
typedef int GLsizei;
typedef char GLchar;
typedef ptrdiff_t GLintptr;
typedef ptrdiff_t GLsizeiptr;
typedef double GLclampd;
typedef unsigned short GLushort;
typedef unsigned char GLubyte;
typedef unsigned char GLboolean;
typedef uint64_t GLuint64;
typedef double GLdouble;
typedef unsigned short GLhalf;
typedef float GLclampf;
typedef unsigned int GLbitfield;
typedef signed char GLbyte;
typedef short GLshort;
typedef void GLvoid;
typedef int64_t GLint64;
typedef float GLfloat;
typedef int GLint;
#define GL_INT_2_10_10_10_REV 0x8D9F
#define GL_R32F 0x822E
#define GL_PROGRAM_POINT_SIZE 0x8642
#define GL_DEPTH_ATTACHMENT 0x8D00
#define GL_DEPTH_STENCIL_ATTACHMENT 0x821A
#define GL_COLOR_ATTACHMENT2 0x8CE2
#define GL_COLOR_ATTACHMENT0 0x8CE0
#define GL_R16F 0x822D
#define GL_COLOR_ATTACHMENT22 0x8CF6
#define GL_DRAW_FRAMEBUFFER 0x8CA9
#define GL_FRAMEBUFFER_COMPLETE 0x8CD5
#define GL_NUM_EXTENSIONS 0x821D
#define GL_INFO_LOG_LENGTH 0x8B84
#define GL_VERTEX_SHADER 0x8B31
#define GL_INCR 0x1E02
#define GL_DYNAMIC_DRAW 0x88E8
#define GL_STATIC_DRAW 0x88E4
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519
#define GL_TEXTURE_CUBE_MAP 0x8513
#define GL_FUNC_SUBTRACT 0x800A
#define GL_FUNC_REVERSE_SUBTRACT 0x800B
#define GL_CONSTANT_COLOR 0x8001
#define GL_DECR_WRAP 0x8508
#define GL_R8 0x8229
#define GL_LINEAR_MIPMAP_LINEAR 0x2703
#define GL_ELEMENT_ARRAY_BUFFER 0x8893
#define GL_SHORT 0x1402
#define GL_DEPTH_TEST 0x0B71
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518
#define GL_LINK_STATUS 0x8B82
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517
#define GL_SAMPLE_ALPHA_TO_COVERAGE 0x809E
#define GL_RGBA16F 0x881A
#define GL_CONSTANT_ALPHA 0x8003
#define GL_READ_FRAMEBUFFER 0x8CA8
#define GL_TEXTURE0 0x84C0
#define GL_TEXTURE_MIN_LOD 0x813A
#define GL_CLAMP_TO_EDGE 0x812F
#define GL_UNSIGNED_SHORT_5_6_5 0x8363
#define GL_TEXTURE_WRAP_R 0x8072
#define GL_UNSIGNED_SHORT_5_5_5_1 0x8034
#define GL_NEAREST_MIPMAP_NEAREST 0x2700
#define GL_UNSIGNED_SHORT_4_4_4_4 0x8033
#define GL_SRC_ALPHA_SATURATE 0x0308
#define GL_STREAM_DRAW 0x88E0
#define GL_ONE 1
#define GL_NEAREST_MIPMAP_LINEAR 0x2702
#define GL_RGB10_A2 0x8059
#define GL_RGBA8 0x8058
#define GL_SRGB8_ALPHA8 0x8C43
#define GL_COLOR_ATTACHMENT1 0x8CE1
#define GL_RGBA4 0x8056
#define GL_RGB8 0x8051
#define GL_ARRAY_BUFFER 0x8892
#define GL_STENCIL 0x1802
#define GL_TEXTURE_2D 0x0DE1
#define GL_DEPTH 0x1801
#define GL_FRONT 0x0404
#define GL_STENCIL_BUFFER_BIT 0x00000400
#define GL_REPEAT 0x2901
#define GL_RGBA 0x1908
#define GL_TEXTURE_CUBE_MAP_POSITIVE_X 0x8515
#define GL_DECR 0x1E03
#define GL_FRAGMENT_SHADER 0x8B30
#define GL_FLOAT 0x1406
#define GL_TEXTURE_MAX_LOD 0x813B
#define GL_DEPTH_COMPONENT 0x1902
#define GL_ONE_MINUS_DST_ALPHA 0x0305
#define GL_COLOR 0x1800
#define GL_TEXTURE_2D_ARRAY 0x8C1A
#define GL_TRIANGLES 0x0004
#define GL_UNSIGNED_BYTE 0x1401
#define GL_TEXTURE_MAG_FILTER 0x2800
#define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
#define GL_NONE 0
#define GL_SRC_COLOR 0x0300
#define GL_BYTE 0x1400
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A
#define GL_LINE_STRIP 0x0003
#define GL_TEXTURE_3D 0x806F
#define GL_CW 0x0900
#define GL_LINEAR 0x2601
#define GL_RENDERBUFFER 0x8D41
#define GL_GEQUAL 0x0206
#define GL_COLOR_BUFFER_BIT 0x00004000
#define GL_RGBA32F 0x8814
#define GL_BLEND 0x0BE2
#define GL_ONE_MINUS_SRC_ALPHA 0x0303
#define GL_ONE_MINUS_CONSTANT_COLOR 0x8002
#define GL_TEXTURE_WRAP_T 0x2803
#define GL_TEXTURE_WRAP_S 0x2802
#define GL_TEXTURE_MIN_FILTER 0x2801
#define GL_LINEAR_MIPMAP_NEAREST 0x2701
#define GL_EXTENSIONS 0x1F03
#define GL_NO_ERROR 0
#define GL_REPLACE 0x1E01
#define GL_KEEP 0x1E00
#define GL_CCW 0x0901
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516
#define GL_RGB 0x1907
#define GL_TRIANGLE_STRIP 0x0005
#define GL_FALSE 0
#define GL_ZERO 0
#define GL_CULL_FACE 0x0B44
#define GL_INVERT 0x150A
#define GL_INT 0x1404
#define GL_UNSIGNED_INT 0x1405
#define GL_UNSIGNED_SHORT 0x1403
#define GL_NEAREST 0x2600
#define GL_SCISSOR_TEST 0x0C11
#define GL_LEQUAL 0x0203
#define GL_STENCIL_TEST 0x0B90
#define GL_DITHER 0x0BD0
#define GL_DEPTH_COMPONENT32F 0x8CAC
#define GL_EQUAL 0x0202
#define GL_FRAMEBUFFER 0x8D40
#define GL_RGB5 0x8050
#define GL_LINES 0x0001
#define GL_DEPTH_BUFFER_BIT 0x00000100
#define GL_SRC_ALPHA 0x0302
#define GL_INCR_WRAP 0x8507
#define GL_LESS 0x0201
#define GL_MULTISAMPLE 0x809D
#define GL_FRAMEBUFFER_BINDING 0x8CA6
#define GL_BACK 0x0405
#define GL_ALWAYS 0x0207
#define GL_FUNC_ADD 0x8006
#define GL_ONE_MINUS_DST_COLOR 0x0307
#define GL_NOTEQUAL 0x0205
#define GL_DST_COLOR 0x0306
#define GL_COMPILE_STATUS 0x8B81
#define GL_RED 0x1903
#define GL_COLOR_ATTACHMENT3 0x8CE3
#define GL_DST_ALPHA 0x0304
#define GL_RGB5_A1 0x8057
#define GL_GREATER 0x0204
#define GL_POLYGON_OFFSET_FILL 0x8037
#define GL_TRUE 1
#define GL_NEVER 0x0200
#define GL_POINTS 0x0000
#define GL_ONE_MINUS_SRC_COLOR 0x0301
#define GL_MIRRORED_REPEAT 0x8370
#define GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS 0x8B4D
#define GL_R11F_G11F_B10F 0x8C3A
#define GL_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B
#define GL_RGB9_E5 0x8C3D
#define GL_UNSIGNED_INT_5_9_9_9_REV 0x8C3E
#define GL_RGBA32UI 0x8D70
#define GL_RGB32UI 0x8D71
#define GL_RGBA16UI 0x8D76
#define GL_RGB16UI 0x8D77
#define GL_RGBA8UI 0x8D7C
#define GL_RGB8UI 0x8D7D
#define GL_RGBA32I 0x8D82
#define GL_RGB32I 0x8D83
#define GL_RGBA16I 0x8D88
#define GL_RGB16I 0x8D89
#define GL_RGBA8I 0x8D8E
#define GL_RGB8I 0x8D8F
#define GL_RED_INTEGER 0x8D94
#define GL_RG 0x8227
#define GL_RG_INTEGER 0x8228
#define GL_R8 0x8229
#define GL_R16 0x822A
#define GL_RG8 0x822B
#define GL_RG16 0x822C
#define GL_R16F 0x822D
#define GL_R32F 0x822E
#define GL_RG16F 0x822F
#define GL_RG32F 0x8230
#define GL_R8I 0x8231
#define GL_R8UI 0x8232
#define GL_R16I 0x8233
#define GL_R16UI 0x8234
#define GL_R32I 0x8235
#define GL_R32UI 0x8236
#define GL_RG8I 0x8237
#define GL_RG8UI 0x8238
#define GL_RG16I 0x8239
#define GL_RG16UI 0x823A
#define GL_RG32I 0x823B
#define GL_RG32UI 0x823C
#define GL_RGBA_INTEGER 0x8D99
#define GL_R8_SNORM 0x8F94
#define GL_RG8_SNORM 0x8F95
#define GL_RGB8_SNORM 0x8F96
#define GL_RGBA8_SNORM 0x8F97
#define GL_R16_SNORM 0x8F98
#define GL_RG16_SNORM 0x8F99
#define GL_RGB16_SNORM 0x8F9A
#define GL_RGBA16_SNORM 0x8F9B
#define GL_RGBA16 0x805B
#define GL_MAX_TEXTURE_SIZE 0x0D33
#define GL_MAX_CUBE_MAP_TEXTURE_SIZE 0x851C
#define GL_MAX_3D_TEXTURE_SIZE 0x8073
#define GL_MAX_ARRAY_TEXTURE_LAYERS 0x88FF
#define GL_MAX_VERTEX_ATTRIBS 0x8869
#define GL_CLAMP_TO_BORDER 0x812D
#define GL_TEXTURE_BORDER_COLOR 0x1004
#define GL_CURRENT_PROGRAM 0x8B8D
#define GL_MAX_VERTEX_UNIFORM_COMPONENTS 0x8B4A
#define GL_UNPACK_ALIGNMENT 0x0CF5
#define GL_FRAMEBUFFER_SRGB 0x8DB9
#define GL_TEXTURE_COMPARE_MODE 0x884C
#define GL_TEXTURE_COMPARE_FUNC 0x884D
#define GL_COMPARE_REF_TO_TEXTURE 0x884E
#define GL_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
#define GL_TEXTURE_MAX_LEVEL 0x813D
#define GL_FRAMEBUFFER_UNDEFINED 0x8219
#define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT 0x8CD6
#define GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT 0x8CD7
#define GL_FRAMEBUFFER_UNSUPPORTED 0x8CDD
#define GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE 0x8D56
#define GL_MAJOR_VERSION 0x821B
#define GL_MINOR_VERSION 0x821C
#endif
#ifndef GL_UNSIGNED_INT_2_10_10_10_REV
#define GL_UNSIGNED_INT_2_10_10_10_REV 0x8368
#endif
#ifndef GL_UNSIGNED_INT_24_8
#define GL_UNSIGNED_INT_24_8 0x84FA
#endif
#ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT
#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
#endif
#ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
#define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
#endif
#ifndef GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
#endif
#ifndef GL_COMPRESSED_RED_RGTC1
#define GL_COMPRESSED_RED_RGTC1 0x8DBB
#endif
#ifndef GL_COMPRESSED_SIGNED_RED_RGTC1
#define GL_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC
#endif
#ifndef GL_COMPRESSED_RED_GREEN_RGTC2
#define GL_COMPRESSED_RED_GREEN_RGTC2 0x8DBD
#endif
#ifndef GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2
#define GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2 0x8DBE
#endif
#ifndef GL_COMPRESSED_RGBA_BPTC_UNORM_ARB
#define GL_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
#endif
#ifndef GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB
#define GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
#endif
#ifndef GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB
#define GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
#endif
#ifndef GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB
#define GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
#endif
#ifndef GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG
#define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
#endif
#ifndef GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG
#define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
#endif
#ifndef GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
#define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
#endif
#ifndef GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
#define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
#endif
#ifndef GL_COMPRESSED_RGB8_ETC2
#define GL_COMPRESSED_RGB8_ETC2 0x9274
#endif
#ifndef GL_COMPRESSED_SRGB8_ETC2
#define GL_COMPRESSED_SRGB8_ETC2 0x9275
#endif
#ifndef GL_COMPRESSED_RGBA8_ETC2_EAC
#define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
#endif
#ifndef GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
#define GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
#endif
#ifndef GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
#define GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
#endif
#ifndef GL_COMPRESSED_R11_EAC
#define GL_COMPRESSED_R11_EAC 0x9270
#endif
#ifndef GL_COMPRESSED_SIGNED_R11_EAC
#define GL_COMPRESSED_SIGNED_R11_EAC 0x9271
#endif
#ifndef GL_COMPRESSED_RG11_EAC
#define GL_COMPRESSED_RG11_EAC 0x9272
#endif
#ifndef GL_COMPRESSED_SIGNED_RG11_EAC
#define GL_COMPRESSED_SIGNED_RG11_EAC 0x9273
#endif
#ifndef GL_COMPRESSED_RGBA_ASTC_4x4_KHR
#define GL_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0
#endif
#ifndef GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0
#endif
#ifndef GL_DEPTH24_STENCIL8
#define GL_DEPTH24_STENCIL8 0x88F0
#endif
#ifndef GL_HALF_FLOAT
#define GL_HALF_FLOAT 0x140B
#endif
#ifndef GL_DEPTH_STENCIL
#define GL_DEPTH_STENCIL 0x84F9
#endif
#ifndef GL_LUMINANCE
#define GL_LUMINANCE 0x1909
#endif
#ifndef _SG_GL_CHECK_ERROR
#define _SG_GL_CHECK_ERROR() { SOKOL_ASSERT(glGetError() == GL_NO_ERROR); }
#endif
#endif
#if defined(SOKOL_GLES3)
// on WebGL2, GL_FRAMEBUFFER_UNDEFINED technically doesn't exist (it is defined
// in the Emscripten headers, but may not exist in other WebGL2 shims)
// see: https://github.com/floooh/sokol/pull/933
#ifndef GL_FRAMEBUFFER_UNDEFINED
#define GL_FRAMEBUFFER_UNDEFINED 0x8219
#endif
#endif
// make some GL constants generally available to simplify compilation,
// use of those constants will be filtered by runtime flags
#ifndef GL_SHADER_STORAGE_BUFFER
#define GL_SHADER_STORAGE_BUFFER 0x90D2
#endif
// ███████ ████████ ██████ ██ ██ ██████ ████████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██████ ██ ██ ██ ██ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██ ██ ██████ ██████ ██ ███████
//
// >>structs
// resource pool slots
typedef struct {
uint32_t id;
sg_resource_state state;
} _sg_slot_t;
// resource pool housekeeping struct
typedef struct {
int size;
int queue_top;
uint32_t* gen_ctrs;
int* free_queue;
} _sg_pool_t;
_SOKOL_PRIVATE void _sg_init_pool(_sg_pool_t* pool, int num);
_SOKOL_PRIVATE void _sg_discard_pool(_sg_pool_t* pool);
_SOKOL_PRIVATE int _sg_pool_alloc_index(_sg_pool_t* pool);
_SOKOL_PRIVATE void _sg_pool_free_index(_sg_pool_t* pool, int slot_index);
_SOKOL_PRIVATE void _sg_reset_slot(_sg_slot_t* slot);
_SOKOL_PRIVATE uint32_t _sg_slot_alloc(_sg_pool_t* pool, _sg_slot_t* slot, int slot_index);
_SOKOL_PRIVATE int _sg_slot_index(uint32_t id);
// constants
enum {
_SG_STRING_SIZE = 32,
_SG_SLOT_SHIFT = 16,
_SG_SLOT_MASK = (1<<_SG_SLOT_SHIFT)-1,
_SG_MAX_POOL_SIZE = (1<<_SG_SLOT_SHIFT),
_SG_DEFAULT_BUFFER_POOL_SIZE = 128,
_SG_DEFAULT_IMAGE_POOL_SIZE = 128,
_SG_DEFAULT_SAMPLER_POOL_SIZE = 64,
_SG_DEFAULT_SHADER_POOL_SIZE = 32,
_SG_DEFAULT_PIPELINE_POOL_SIZE = 64,
_SG_DEFAULT_ATTACHMENTS_POOL_SIZE = 16,
_SG_DEFAULT_UB_SIZE = 4 * 1024 * 1024,
_SG_DEFAULT_MAX_COMMIT_LISTENERS = 1024,
_SG_DEFAULT_WGPU_BINDGROUP_CACHE_SIZE = 1024,
};
// fixed-size string
typedef struct {
char buf[_SG_STRING_SIZE];
} _sg_str_t;
// helper macros
#define _sg_def(val, def) (((val) == 0) ? (def) : (val))
#define _sg_def_flt(val, def) (((val) == 0.0f) ? (def) : (val))
#define _sg_min(a,b) (((a)<(b))?(a):(b))
#define _sg_max(a,b) (((a)>(b))?(a):(b))
#define _sg_clamp(v,v0,v1) (((v)<(v0))?(v0):(((v)>(v1))?(v1):(v)))
#define _sg_fequal(val,cmp,delta) ((((val)-(cmp))> -(delta))&&(((val)-(cmp))<(delta)))
#define _sg_ispow2(val) ((val&(val-1))==0)
#define _sg_stats_add(key,val) {if(_sg.stats_enabled){ _sg.stats.key+=val;}}
_SOKOL_PRIVATE void* _sg_malloc_clear(size_t size);
_SOKOL_PRIVATE void _sg_free(void* ptr);
_SOKOL_PRIVATE void _sg_clear(void* ptr, size_t size);
typedef struct {
int size;
int append_pos;
bool append_overflow;
uint32_t update_frame_index;
uint32_t append_frame_index;
int num_slots;
int active_slot;
sg_buffer_type type;
sg_usage usage;
} _sg_buffer_common_t;
_SOKOL_PRIVATE void _sg_buffer_common_init(_sg_buffer_common_t* cmn, const sg_buffer_desc* desc) {
cmn->size = (int)desc->size;
cmn->append_pos = 0;
cmn->append_overflow = false;
cmn->update_frame_index = 0;
cmn->append_frame_index = 0;
cmn->num_slots = (desc->usage == SG_USAGE_IMMUTABLE) ? 1 : SG_NUM_INFLIGHT_FRAMES;
cmn->active_slot = 0;
cmn->type = desc->type;
cmn->usage = desc->usage;
}
typedef struct {
uint32_t upd_frame_index;
int num_slots;
int active_slot;
sg_image_type type;
bool render_target;
int width;
int height;
int num_slices;
int num_mipmaps;
sg_usage usage;
sg_pixel_format pixel_format;
int sample_count;
} _sg_image_common_t;
_SOKOL_PRIVATE void _sg_image_common_init(_sg_image_common_t* cmn, const sg_image_desc* desc) {
cmn->upd_frame_index = 0;
cmn->num_slots = (desc->usage == SG_USAGE_IMMUTABLE) ? 1 : SG_NUM_INFLIGHT_FRAMES;
cmn->active_slot = 0;
cmn->type = desc->type;
cmn->render_target = desc->render_target;
cmn->width = desc->width;
cmn->height = desc->height;
cmn->num_slices = desc->num_slices;
cmn->num_mipmaps = desc->num_mipmaps;
cmn->usage = desc->usage;
cmn->pixel_format = desc->pixel_format;
cmn->sample_count = desc->sample_count;
}
typedef struct {
sg_filter min_filter;
sg_filter mag_filter;
sg_filter mipmap_filter;
sg_wrap wrap_u;
sg_wrap wrap_v;
sg_wrap wrap_w;
float min_lod;
float max_lod;
sg_border_color border_color;
sg_compare_func compare;
uint32_t max_anisotropy;
} _sg_sampler_common_t;
_SOKOL_PRIVATE void _sg_sampler_common_init(_sg_sampler_common_t* cmn, const sg_sampler_desc* desc) {
cmn->min_filter = desc->min_filter;
cmn->mag_filter = desc->mag_filter;
cmn->mipmap_filter = desc->mipmap_filter;
cmn->wrap_u = desc->wrap_u;
cmn->wrap_v = desc->wrap_v;
cmn->wrap_w = desc->wrap_w;
cmn->min_lod = desc->min_lod;
cmn->max_lod = desc->max_lod;
cmn->border_color = desc->border_color;
cmn->compare = desc->compare;
cmn->max_anisotropy = desc->max_anisotropy;
}
typedef struct {
size_t size;
} _sg_shader_uniform_block_t;
typedef struct {
bool used;
bool readonly;
} _sg_shader_storage_buffer_t;
typedef struct {
sg_image_type image_type;
sg_image_sample_type sample_type;
bool multisampled;
} _sg_shader_image_t;
typedef struct {
sg_sampler_type sampler_type;
} _sg_shader_sampler_t;
// combined image sampler mappings, only needed on GL
typedef struct {
int image_slot;
int sampler_slot;
} _sg_shader_image_sampler_t;
typedef struct {
int num_uniform_blocks;
int num_storage_buffers;
int num_images;
int num_samplers;
int num_image_samplers;
_sg_shader_uniform_block_t uniform_blocks[SG_MAX_SHADERSTAGE_UBS];
_sg_shader_storage_buffer_t storage_buffers[SG_MAX_SHADERSTAGE_STORAGEBUFFERS];
_sg_shader_image_t images[SG_MAX_SHADERSTAGE_IMAGES];
_sg_shader_sampler_t samplers[SG_MAX_SHADERSTAGE_SAMPLERS];
_sg_shader_image_sampler_t image_samplers[SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS];
} _sg_shader_stage_t;
typedef struct {
_sg_shader_stage_t stage[SG_NUM_SHADER_STAGES];
} _sg_shader_common_t;
_SOKOL_PRIVATE void _sg_shader_common_init(_sg_shader_common_t* cmn, const sg_shader_desc* desc) {
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS) ? &desc->vs : &desc->fs;
_sg_shader_stage_t* stage = &cmn->stage[stage_index];
SOKOL_ASSERT(stage->num_uniform_blocks == 0);
for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) {
const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index];
if (0 == ub_desc->size) {
break;
}
stage->uniform_blocks[ub_index].size = ub_desc->size;
stage->num_uniform_blocks++;
}
SOKOL_ASSERT(stage->num_images == 0);
for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) {
const sg_shader_image_desc* img_desc = &stage_desc->images[img_index];
if (!img_desc->used) {
break;
}
stage->images[img_index].multisampled = img_desc->multisampled;
stage->images[img_index].image_type = img_desc->image_type;
stage->images[img_index].sample_type = img_desc->sample_type;
stage->num_images++;
}
SOKOL_ASSERT(stage->num_samplers == 0);
for (int smp_index = 0; smp_index < SG_MAX_SHADERSTAGE_SAMPLERS; smp_index++) {
const sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[smp_index];
if (!smp_desc->used) {
break;
}
stage->samplers[smp_index].sampler_type = smp_desc->sampler_type;
stage->num_samplers++;
}
SOKOL_ASSERT(stage->num_image_samplers == 0);
for (int img_smp_index = 0; img_smp_index < SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS; img_smp_index++) {
const sg_shader_image_sampler_pair_desc* img_smp_desc = &stage_desc->image_sampler_pairs[img_smp_index];
if (!img_smp_desc->used) {
break;
}
SOKOL_ASSERT((img_smp_desc->image_slot >= 0) && (img_smp_desc->image_slot < stage->num_images));
stage->image_samplers[img_smp_index].image_slot = img_smp_desc->image_slot;
SOKOL_ASSERT((img_smp_desc->sampler_slot >= 0) && (img_smp_desc->sampler_slot < stage->num_samplers));
stage->image_samplers[img_smp_index].sampler_slot = img_smp_desc->sampler_slot;
stage->num_image_samplers++;
}
SOKOL_ASSERT(stage->num_storage_buffers == 0);
for (int sbuf_index = 0; sbuf_index < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; sbuf_index++) {
const sg_shader_storage_buffer_desc* sbuf_desc = &stage_desc->storage_buffers[sbuf_index];
if (!sbuf_desc->used) {
break;
}
stage->storage_buffers[sbuf_index].used = sbuf_desc->used;
stage->storage_buffers[sbuf_index].readonly = sbuf_desc->readonly;
stage->num_storage_buffers++;
}
}
}
typedef struct {
bool vertex_buffer_layout_active[SG_MAX_VERTEX_BUFFERS];
bool use_instanced_draw;
sg_shader shader_id;
sg_vertex_layout_state layout;
sg_depth_state depth;
sg_stencil_state stencil;
int color_count;
sg_color_target_state colors[SG_MAX_COLOR_ATTACHMENTS];
sg_primitive_type primitive_type;
sg_index_type index_type;
sg_cull_mode cull_mode;
sg_face_winding face_winding;
int sample_count;
sg_color blend_color;
bool alpha_to_coverage_enabled;
} _sg_pipeline_common_t;
_SOKOL_PRIVATE void _sg_pipeline_common_init(_sg_pipeline_common_t* cmn, const sg_pipeline_desc* desc) {
SOKOL_ASSERT((desc->color_count >= 0) && (desc->color_count <= SG_MAX_COLOR_ATTACHMENTS));
for (int i = 0; i < SG_MAX_VERTEX_BUFFERS; i++) {
cmn->vertex_buffer_layout_active[i] = false;
}
cmn->use_instanced_draw = false;
cmn->shader_id = desc->shader;
cmn->layout = desc->layout;
cmn->depth = desc->depth;
cmn->stencil = desc->stencil;
cmn->color_count = desc->color_count;
for (int i = 0; i < desc->color_count; i++) {
cmn->colors[i] = desc->colors[i];
}
cmn->primitive_type = desc->primitive_type;
cmn->index_type = desc->index_type;
cmn->cull_mode = desc->cull_mode;
cmn->face_winding = desc->face_winding;
cmn->sample_count = desc->sample_count;
cmn->blend_color = desc->blend_color;
cmn->alpha_to_coverage_enabled = desc->alpha_to_coverage_enabled;
}
typedef struct {
sg_image image_id;
int mip_level;
int slice;
} _sg_attachment_common_t;
typedef struct {
int width;
int height;
int num_colors;
_sg_attachment_common_t colors[SG_MAX_COLOR_ATTACHMENTS];
_sg_attachment_common_t resolves[SG_MAX_COLOR_ATTACHMENTS];
_sg_attachment_common_t depth_stencil;
} _sg_attachments_common_t;
_SOKOL_PRIVATE void _sg_attachment_common_init(_sg_attachment_common_t* cmn, const sg_attachment_desc* desc) {
cmn->image_id = desc->image;
cmn->mip_level = desc->mip_level;
cmn->slice = desc->slice;
}
_SOKOL_PRIVATE void _sg_attachments_common_init(_sg_attachments_common_t* cmn, const sg_attachments_desc* desc, int width, int height) {
SOKOL_ASSERT((width > 0) && (height > 0));
cmn->width = width;
cmn->height = height;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (desc->colors[i].image.id != SG_INVALID_ID) {
cmn->num_colors++;
_sg_attachment_common_init(&cmn->colors[i], &desc->colors[i]);
_sg_attachment_common_init(&cmn->resolves[i], &desc->resolves[i]);
}
}
if (desc->depth_stencil.image.id != SG_INVALID_ID) {
_sg_attachment_common_init(&cmn->depth_stencil, &desc->depth_stencil);
}
}
#if defined(SOKOL_DUMMY_BACKEND)
typedef struct {
_sg_slot_t slot;
_sg_buffer_common_t cmn;
} _sg_dummy_buffer_t;
typedef _sg_dummy_buffer_t _sg_buffer_t;
typedef struct {
_sg_slot_t slot;
_sg_image_common_t cmn;
} _sg_dummy_image_t;
typedef _sg_dummy_image_t _sg_image_t;
typedef struct {
_sg_slot_t slot;
_sg_sampler_common_t cmn;
} _sg_dummy_sampler_t;
typedef _sg_dummy_sampler_t _sg_sampler_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_common_t cmn;
} _sg_dummy_shader_t;
typedef _sg_dummy_shader_t _sg_shader_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_t* shader;
_sg_pipeline_common_t cmn;
} _sg_dummy_pipeline_t;
typedef _sg_dummy_pipeline_t _sg_pipeline_t;
typedef struct {
_sg_image_t* image;
} _sg_dummy_attachment_t;
typedef struct {
_sg_slot_t slot;
_sg_attachments_common_t cmn;
struct {
_sg_dummy_attachment_t colors[SG_MAX_COLOR_ATTACHMENTS];
_sg_dummy_attachment_t resolves[SG_MAX_COLOR_ATTACHMENTS];
_sg_dummy_attachment_t depth_stencil;
} dmy;
} _sg_dummy_attachments_t;
typedef _sg_dummy_attachments_t _sg_attachments_t;
#elif defined(_SOKOL_ANY_GL)
#define _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE (SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS * SG_NUM_SHADER_STAGES)
#define _SG_GL_STORAGEBUFFER_STAGE_INDEX_PITCH (SG_MAX_SHADERSTAGE_STORAGEBUFFERS)
typedef struct {
_sg_slot_t slot;
_sg_buffer_common_t cmn;
struct {
GLuint buf[SG_NUM_INFLIGHT_FRAMES];
bool injected; // if true, external buffers were injected with sg_buffer_desc.gl_buffers
} gl;
} _sg_gl_buffer_t;
typedef _sg_gl_buffer_t _sg_buffer_t;
typedef struct {
_sg_slot_t slot;
_sg_image_common_t cmn;
struct {
GLenum target;
GLuint msaa_render_buffer;
GLuint tex[SG_NUM_INFLIGHT_FRAMES];
bool injected; // if true, external textures were injected with sg_image_desc.gl_textures
} gl;
} _sg_gl_image_t;
typedef _sg_gl_image_t _sg_image_t;
typedef struct {
_sg_slot_t slot;
_sg_sampler_common_t cmn;
struct {
GLuint smp;
bool injected; // true if external sampler was injects in sg_sampler_desc.gl_sampler
} gl;
} _sg_gl_sampler_t;
typedef _sg_gl_sampler_t _sg_sampler_t;
typedef struct {
GLint gl_loc;
sg_uniform_type type;
uint16_t count;
uint16_t offset;
} _sg_gl_uniform_t;
typedef struct {
int num_uniforms;
_sg_gl_uniform_t uniforms[SG_MAX_UB_MEMBERS];
} _sg_gl_uniform_block_t;
typedef struct {
int gl_tex_slot;
} _sg_gl_shader_image_sampler_t;
typedef struct {
_sg_str_t name;
} _sg_gl_shader_attr_t;
typedef struct {
_sg_gl_uniform_block_t uniform_blocks[SG_MAX_SHADERSTAGE_UBS];
_sg_gl_shader_image_sampler_t image_samplers[SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS];
} _sg_gl_shader_stage_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_common_t cmn;
struct {
GLuint prog;
_sg_gl_shader_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES];
_sg_gl_shader_stage_t stage[SG_NUM_SHADER_STAGES];
} gl;
} _sg_gl_shader_t;
typedef _sg_gl_shader_t _sg_shader_t;
typedef struct {
int8_t vb_index; // -1 if attr is not enabled
int8_t divisor; // -1 if not initialized
uint8_t stride;
uint8_t size;
uint8_t normalized;
int offset;
GLenum type;
} _sg_gl_attr_t;
typedef struct {
_sg_slot_t slot;
_sg_pipeline_common_t cmn;
_sg_shader_t* shader;
struct {
_sg_gl_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES];
sg_depth_state depth;
sg_stencil_state stencil;
sg_primitive_type primitive_type;
sg_blend_state blend;
sg_color_mask color_write_mask[SG_MAX_COLOR_ATTACHMENTS];
sg_cull_mode cull_mode;
sg_face_winding face_winding;
int sample_count;
bool alpha_to_coverage_enabled;
} gl;
} _sg_gl_pipeline_t;
typedef _sg_gl_pipeline_t _sg_pipeline_t;
typedef struct {
_sg_image_t* image;
} _sg_gl_attachment_t;
typedef struct {
_sg_slot_t slot;
_sg_attachments_common_t cmn;
struct {
GLuint fb;
_sg_gl_attachment_t colors[SG_MAX_COLOR_ATTACHMENTS];
_sg_gl_attachment_t resolves[SG_MAX_COLOR_ATTACHMENTS];
_sg_gl_attachment_t depth_stencil;
GLuint msaa_resolve_framebuffer[SG_MAX_COLOR_ATTACHMENTS];
} gl;
} _sg_gl_attachments_t;
typedef _sg_gl_attachments_t _sg_attachments_t;
typedef struct {
_sg_gl_attr_t gl_attr;
GLuint gl_vbuf;
} _sg_gl_cache_attr_t;
typedef struct {
GLenum target;
GLuint texture;
GLuint sampler;
} _sg_gl_cache_texture_sampler_bind_slot;
typedef struct {
sg_depth_state depth;
sg_stencil_state stencil;
sg_blend_state blend;
sg_color_mask color_write_mask[SG_MAX_COLOR_ATTACHMENTS];
sg_cull_mode cull_mode;
sg_face_winding face_winding;
bool polygon_offset_enabled;
int sample_count;
sg_color blend_color;
bool alpha_to_coverage_enabled;
_sg_gl_cache_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES];
GLuint vertex_buffer;
GLuint index_buffer;
GLuint storage_buffer; // general bind point
GLuint stage_storage_buffers[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_STORAGEBUFFERS];
GLuint stored_vertex_buffer;
GLuint stored_index_buffer;
GLuint stored_storage_buffer;
GLuint prog;
_sg_gl_cache_texture_sampler_bind_slot texture_samplers[_SG_GL_TEXTURE_SAMPLER_CACHE_SIZE];
_sg_gl_cache_texture_sampler_bind_slot stored_texture_sampler;
int cur_ib_offset;
GLenum cur_primitive_type;
GLenum cur_index_type;
GLenum cur_active_texture;
_sg_pipeline_t* cur_pipeline;
sg_pipeline cur_pipeline_id;
} _sg_gl_state_cache_t;
typedef struct {
bool valid;
GLuint vao;
_sg_gl_state_cache_t cache;
bool ext_anisotropic;
GLint max_anisotropy;
sg_store_action color_store_actions[SG_MAX_COLOR_ATTACHMENTS];
sg_store_action depth_store_action;
sg_store_action stencil_store_action;
#if _SOKOL_USE_WIN32_GL_LOADER
HINSTANCE opengl32_dll;
#endif
} _sg_gl_backend_t;
#elif defined(SOKOL_D3D11)
typedef struct {
_sg_slot_t slot;
_sg_buffer_common_t cmn;
struct {
ID3D11Buffer* buf;
ID3D11ShaderResourceView* srv;
} d3d11;
} _sg_d3d11_buffer_t;
typedef _sg_d3d11_buffer_t _sg_buffer_t;
typedef struct {
_sg_slot_t slot;
_sg_image_common_t cmn;
struct {
DXGI_FORMAT format;
ID3D11Texture2D* tex2d;
ID3D11Texture3D* tex3d;
ID3D11Resource* res; // either tex2d or tex3d
ID3D11ShaderResourceView* srv;
} d3d11;
} _sg_d3d11_image_t;
typedef _sg_d3d11_image_t _sg_image_t;
typedef struct {
_sg_slot_t slot;
_sg_sampler_common_t cmn;
struct {
ID3D11SamplerState* smp;
} d3d11;
} _sg_d3d11_sampler_t;
typedef _sg_d3d11_sampler_t _sg_sampler_t;
typedef struct {
_sg_str_t sem_name;
int sem_index;
} _sg_d3d11_shader_attr_t;
typedef struct {
ID3D11Buffer* cbufs[SG_MAX_SHADERSTAGE_UBS];
} _sg_d3d11_shader_stage_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_common_t cmn;
struct {
_sg_d3d11_shader_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES];
_sg_d3d11_shader_stage_t stage[SG_NUM_SHADER_STAGES];
ID3D11VertexShader* vs;
ID3D11PixelShader* fs;
void* vs_blob;
size_t vs_blob_length;
} d3d11;
} _sg_d3d11_shader_t;
typedef _sg_d3d11_shader_t _sg_shader_t;
typedef struct {
_sg_slot_t slot;
_sg_pipeline_common_t cmn;
_sg_shader_t* shader;
struct {
UINT stencil_ref;
UINT vb_strides[SG_MAX_VERTEX_BUFFERS];
D3D_PRIMITIVE_TOPOLOGY topology;
DXGI_FORMAT index_format;
ID3D11InputLayout* il;
ID3D11RasterizerState* rs;
ID3D11DepthStencilState* dss;
ID3D11BlendState* bs;
} d3d11;
} _sg_d3d11_pipeline_t;
typedef _sg_d3d11_pipeline_t _sg_pipeline_t;
typedef struct {
_sg_image_t* image;
union {
ID3D11RenderTargetView* rtv;
ID3D11DepthStencilView* dsv;
} view;
} _sg_d3d11_attachment_t;
typedef struct {
_sg_slot_t slot;
_sg_attachments_common_t cmn;
struct {
_sg_d3d11_attachment_t colors[SG_MAX_COLOR_ATTACHMENTS];
_sg_d3d11_attachment_t resolves[SG_MAX_COLOR_ATTACHMENTS];
_sg_d3d11_attachment_t depth_stencil;
} d3d11;
} _sg_d3d11_attachments_t;
typedef _sg_d3d11_attachments_t _sg_attachments_t;
typedef struct {
bool valid;
ID3D11Device* dev;
ID3D11DeviceContext* ctx;
bool use_indexed_draw;
bool use_instanced_draw;
_sg_pipeline_t* cur_pipeline;
sg_pipeline cur_pipeline_id;
struct {
ID3D11RenderTargetView* render_view;
ID3D11RenderTargetView* resolve_view;
} cur_pass;
// on-demand loaded d3dcompiler_47.dll handles
HINSTANCE d3dcompiler_dll;
bool d3dcompiler_dll_load_failed;
pD3DCompile D3DCompile_func;
// global subresourcedata array for texture updates
D3D11_SUBRESOURCE_DATA subres_data[SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS];
} _sg_d3d11_backend_t;
#elif defined(SOKOL_METAL)
#if defined(_SG_TARGET_MACOS) || defined(_SG_TARGET_IOS_SIMULATOR)
#define _SG_MTL_UB_ALIGN (256)
#else
#define _SG_MTL_UB_ALIGN (16)
#endif
#define _SG_MTL_INVALID_SLOT_INDEX (0)
typedef struct {
uint32_t frame_index; // frame index at which it is safe to release this resource
int slot_index;
} _sg_mtl_release_item_t;
typedef struct {
NSMutableArray* pool;
int num_slots;
int free_queue_top;
int* free_queue;
int release_queue_front;
int release_queue_back;
_sg_mtl_release_item_t* release_queue;
} _sg_mtl_idpool_t;
typedef struct {
_sg_slot_t slot;
_sg_buffer_common_t cmn;
struct {
int buf[SG_NUM_INFLIGHT_FRAMES]; // index into _sg_mtl_pool
} mtl;
} _sg_mtl_buffer_t;
typedef _sg_mtl_buffer_t _sg_buffer_t;
typedef struct {
_sg_slot_t slot;
_sg_image_common_t cmn;
struct {
int tex[SG_NUM_INFLIGHT_FRAMES];
} mtl;
} _sg_mtl_image_t;
typedef _sg_mtl_image_t _sg_image_t;
typedef struct {
_sg_slot_t slot;
_sg_sampler_common_t cmn;
struct {
int sampler_state;
} mtl;
} _sg_mtl_sampler_t;
typedef _sg_mtl_sampler_t _sg_sampler_t;
typedef struct {
int mtl_lib;
int mtl_func;
} _sg_mtl_shader_stage_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_common_t cmn;
struct {
_sg_mtl_shader_stage_t stage[SG_NUM_SHADER_STAGES];
} mtl;
} _sg_mtl_shader_t;
typedef _sg_mtl_shader_t _sg_shader_t;
typedef struct {
_sg_slot_t slot;
_sg_pipeline_common_t cmn;
_sg_shader_t* shader;
struct {
MTLPrimitiveType prim_type;
int index_size;
MTLIndexType index_type;
MTLCullMode cull_mode;
MTLWinding winding;
uint32_t stencil_ref;
int rps;
int dss;
} mtl;
} _sg_mtl_pipeline_t;
typedef _sg_mtl_pipeline_t _sg_pipeline_t;
typedef struct {
_sg_image_t* image;
} _sg_mtl_attachment_t;
typedef struct {
_sg_slot_t slot;
_sg_attachments_common_t cmn;
struct {
_sg_mtl_attachment_t colors[SG_MAX_COLOR_ATTACHMENTS];
_sg_mtl_attachment_t resolves[SG_MAX_COLOR_ATTACHMENTS];
_sg_mtl_attachment_t depth_stencil;
} mtl;
} _sg_mtl_attachments_t;
typedef _sg_mtl_attachments_t _sg_attachments_t;
// resource binding state cache
typedef struct {
const _sg_pipeline_t* cur_pipeline;
sg_pipeline cur_pipeline_id;
const _sg_buffer_t* cur_indexbuffer;
sg_buffer cur_indexbuffer_id;
int cur_indexbuffer_offset;
int cur_vertexbuffer_offsets[SG_MAX_VERTEX_BUFFERS];
sg_buffer cur_vertexbuffer_ids[SG_MAX_VERTEX_BUFFERS];
sg_image cur_vs_image_ids[SG_MAX_SHADERSTAGE_IMAGES];
sg_image cur_fs_image_ids[SG_MAX_SHADERSTAGE_IMAGES];
sg_sampler cur_vs_sampler_ids[SG_MAX_SHADERSTAGE_SAMPLERS];
sg_sampler cur_fs_sampler_ids[SG_MAX_SHADERSTAGE_SAMPLERS];
sg_buffer cur_vs_storagebuffer_ids[SG_MAX_SHADERSTAGE_STORAGEBUFFERS];
sg_buffer cur_fs_storagebuffer_ids[SG_MAX_SHADERSTAGE_STORAGEBUFFERS];
} _sg_mtl_state_cache_t;
typedef struct {
bool valid;
bool use_shared_storage_mode;
uint32_t cur_frame_rotate_index;
int ub_size;
int cur_ub_offset;
uint8_t* cur_ub_base_ptr;
_sg_mtl_state_cache_t state_cache;
_sg_mtl_idpool_t idpool;
dispatch_semaphore_t sem;
id<MTLDevice> device;
id<MTLCommandQueue> cmd_queue;
id<MTLCommandBuffer> cmd_buffer;
id<MTLRenderCommandEncoder> cmd_encoder;
id<CAMetalDrawable> cur_drawable;
id<MTLBuffer> uniform_buffers[SG_NUM_INFLIGHT_FRAMES];
} _sg_mtl_backend_t;
#elif defined(SOKOL_WGPU)
#define _SG_WGPU_ROWPITCH_ALIGN (256)
#define _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE (1<<16) // also see WGPULimits.maxUniformBufferBindingSize
#define _SG_WGPU_NUM_BINDGROUPS (2) // 0: uniforms, 1: images and sampler on both shader stages
#define _SG_WGPU_UNIFORM_BINDGROUP_INDEX (0)
#define _SG_WGPU_IMAGE_SAMPLER_BINDGROUP_INDEX (1)
#define _SG_WGPU_MAX_BINDGROUP_ENTRIES (SG_NUM_SHADER_STAGES * (SG_MAX_SHADERSTAGE_IMAGES + SG_MAX_SHADERSTAGE_SAMPLERS + SG_MAX_SHADERSTAGE_STORAGEBUFFERS))
typedef struct {
_sg_slot_t slot;
_sg_buffer_common_t cmn;
struct {
WGPUBuffer buf;
} wgpu;
} _sg_wgpu_buffer_t;
typedef _sg_wgpu_buffer_t _sg_buffer_t;
typedef struct {
_sg_slot_t slot;
_sg_image_common_t cmn;
struct {
WGPUTexture tex;
WGPUTextureView view;
} wgpu;
} _sg_wgpu_image_t;
typedef _sg_wgpu_image_t _sg_image_t;
typedef struct {
_sg_slot_t slot;
_sg_sampler_common_t cmn;
struct {
WGPUSampler smp;
} wgpu;
} _sg_wgpu_sampler_t;
typedef _sg_wgpu_sampler_t _sg_sampler_t;
typedef struct {
WGPUShaderModule module;
_sg_str_t entry;
} _sg_wgpu_shader_stage_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_common_t cmn;
struct {
_sg_wgpu_shader_stage_t stage[SG_NUM_SHADER_STAGES];
WGPUBindGroupLayout bind_group_layout;
} wgpu;
} _sg_wgpu_shader_t;
typedef _sg_wgpu_shader_t _sg_shader_t;
typedef struct {
_sg_slot_t slot;
_sg_pipeline_common_t cmn;
_sg_shader_t* shader;
struct {
WGPURenderPipeline pip;
WGPUColor blend_color;
} wgpu;
} _sg_wgpu_pipeline_t;
typedef _sg_wgpu_pipeline_t _sg_pipeline_t;
typedef struct {
_sg_image_t* image;
WGPUTextureView view;
} _sg_wgpu_attachment_t;
typedef struct {
_sg_slot_t slot;
_sg_attachments_common_t cmn;
struct {
_sg_wgpu_attachment_t colors[SG_MAX_COLOR_ATTACHMENTS];
_sg_wgpu_attachment_t resolves[SG_MAX_COLOR_ATTACHMENTS];
_sg_wgpu_attachment_t depth_stencil;
} wgpu;
} _sg_wgpu_attachments_t;
typedef _sg_wgpu_attachments_t _sg_attachments_t;
// a pool of per-frame uniform buffers
typedef struct {
uint32_t num_bytes;
uint32_t offset; // current offset into buf
uint8_t* staging; // intermediate buffer for uniform data updates
WGPUBuffer buf; // the GPU-side uniform buffer
struct {
WGPUBindGroupLayout group_layout;
WGPUBindGroup group;
uint32_t offsets[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_UBS];
} bind;
} _sg_wgpu_uniform_buffer_t;
typedef struct {
uint32_t id;
} _sg_wgpu_bindgroup_handle_t;
typedef enum {
_SG_WGPU_BINDGROUPSCACHEITEMTYPE_NONE = 0,
_SG_WGPU_BINDGROUPSCACHEITEMTYPE_IMAGE = 0x1111111111111111,
_SG_WGPU_BINDGROUPSCACHEITEMTYPE_SAMPLER = 0x2222222222222222,
_SG_WGPU_BINDGROUPSCACHEITEMTYPE_STORAGEBUFFER = 0x3333333333333333,
_SG_WGPU_BINDGROUPSCACHEITEMTYPE_PIPELINE = 0x4444444444444444,
} _sg_wgpu_bindgroups_cache_item_type_t;
#define _SG_WGPU_BINDGROUPSCACHEKEY_NUM_ITEMS (1 + _SG_WGPU_MAX_BINDGROUP_ENTRIES)
typedef struct {
uint64_t hash;
// the format of cache key items is (_sg_wgpu_bindgroups_cache_item_type_t << 32) | handle.id,
// where the item type is a per-resource-type bit pattern
uint64_t items[_SG_WGPU_BINDGROUPSCACHEKEY_NUM_ITEMS];
} _sg_wgpu_bindgroups_cache_key_t;
typedef struct {
uint32_t num; // must be 2^n
uint32_t index_mask; // mask to turn hash into valid index
_sg_wgpu_bindgroup_handle_t* items;
} _sg_wgpu_bindgroups_cache_t;
typedef struct {
_sg_slot_t slot;
WGPUBindGroup bindgroup;
_sg_wgpu_bindgroups_cache_key_t key;
} _sg_wgpu_bindgroup_t;
typedef struct {
_sg_pool_t pool;
_sg_wgpu_bindgroup_t* bindgroups;
} _sg_wgpu_bindgroups_pool_t;
typedef struct {
struct {
sg_buffer buffer;
int offset;
} vbs[SG_MAX_VERTEX_BUFFERS];
struct {
sg_buffer buffer;
int offset;
} ib;
_sg_wgpu_bindgroup_handle_t bg;
} _sg_wgpu_bindings_cache_t;
// the WGPU backend state
typedef struct {
bool valid;
bool use_indexed_draw;
WGPUDevice dev;
WGPUSupportedLimits limits;
WGPUQueue queue;
WGPUCommandEncoder cmd_enc;
WGPURenderPassEncoder pass_enc;
WGPUBindGroup empty_bind_group;
const _sg_pipeline_t* cur_pipeline;
sg_pipeline cur_pipeline_id;
_sg_wgpu_uniform_buffer_t uniform;
_sg_wgpu_bindings_cache_t bindings_cache;
_sg_wgpu_bindgroups_cache_t bindgroups_cache;
_sg_wgpu_bindgroups_pool_t bindgroups_pool;
} _sg_wgpu_backend_t;
#endif
// POOL STRUCTS
// this *MUST* remain 0
#define _SG_INVALID_SLOT_INDEX (0)
typedef struct {
_sg_pool_t buffer_pool;
_sg_pool_t image_pool;
_sg_pool_t sampler_pool;
_sg_pool_t shader_pool;
_sg_pool_t pipeline_pool;
_sg_pool_t attachments_pool;
_sg_buffer_t* buffers;
_sg_image_t* images;
_sg_sampler_t* samplers;
_sg_shader_t* shaders;
_sg_pipeline_t* pipelines;
_sg_attachments_t* attachments;
} _sg_pools_t;
typedef struct {
int num; // number of allocated commit listener items
int upper; // the current upper index (no valid items past this point)
sg_commit_listener* items;
} _sg_commit_listeners_t;
// resolved resource bindings struct
typedef struct {
_sg_pipeline_t* pip;
int num_vbs;
int num_vs_imgs;
int num_vs_smps;
int num_vs_sbufs;
int num_fs_imgs;
int num_fs_smps;
int num_fs_sbufs;
int vb_offsets[SG_MAX_VERTEX_BUFFERS];
int ib_offset;
_sg_buffer_t* vbs[SG_MAX_VERTEX_BUFFERS];
_sg_buffer_t* ib;
_sg_image_t* vs_imgs[SG_MAX_SHADERSTAGE_IMAGES];
_sg_sampler_t* vs_smps[SG_MAX_SHADERSTAGE_SAMPLERS];
_sg_buffer_t* vs_sbufs[SG_MAX_SHADERSTAGE_STORAGEBUFFERS];
_sg_image_t* fs_imgs[SG_MAX_SHADERSTAGE_IMAGES];
_sg_sampler_t* fs_smps[SG_MAX_SHADERSTAGE_SAMPLERS];
_sg_buffer_t* fs_sbufs[SG_MAX_SHADERSTAGE_STORAGEBUFFERS];
} _sg_bindings_t;
typedef struct {
bool sample;
bool filter;
bool render;
bool blend;
bool msaa;
bool depth;
} _sg_pixelformat_info_t;
typedef struct {
bool valid;
sg_desc desc; // original desc with default values patched in
uint32_t frame_index;
struct {
bool valid;
bool in_pass;
sg_attachments atts_id; // SG_INVALID_ID in a swapchain pass
_sg_attachments_t* atts; // 0 in a swapchain pass
int width;
int height;
struct {
sg_pixel_format color_fmt;
sg_pixel_format depth_fmt;
int sample_count;
} swapchain;
} cur_pass;
sg_pipeline cur_pipeline;
bool next_draw_valid;
#if defined(SOKOL_DEBUG)
sg_log_item validate_error;
#endif
_sg_pools_t pools;
sg_backend backend;
sg_features features;
sg_limits limits;
_sg_pixelformat_info_t formats[_SG_PIXELFORMAT_NUM];
bool stats_enabled;
sg_frame_stats stats;
sg_frame_stats prev_stats;
#if defined(_SOKOL_ANY_GL)
_sg_gl_backend_t gl;
#elif defined(SOKOL_METAL)
_sg_mtl_backend_t mtl;
#elif defined(SOKOL_D3D11)
_sg_d3d11_backend_t d3d11;
#elif defined(SOKOL_WGPU)
_sg_wgpu_backend_t wgpu;
#endif
#if defined(SOKOL_TRACE_HOOKS)
sg_trace_hooks hooks;
#endif
_sg_commit_listeners_t commit_listeners;
} _sg_state_t;
static _sg_state_t _sg;
// ██ ██████ ██████ ██████ ██ ███ ██ ██████
// ██ ██ ██ ██ ██ ██ ████ ██ ██
// ██ ██ ██ ██ ███ ██ ███ ██ ██ ██ ██ ██ ███
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██████ ██████ ██████ ██ ██ ████ ██████
//
// >>logging
#if defined(SOKOL_DEBUG)
#define _SG_LOGITEM_XMACRO(item,msg) #item ": " msg,
static const char* _sg_log_messages[] = {
_SG_LOG_ITEMS
};
#undef _SG_LOGITEM_XMACRO
#endif // SOKOL_DEBUG
#define _SG_PANIC(code) _sg_log(SG_LOGITEM_ ##code, 0, 0, __LINE__)
#define _SG_ERROR(code) _sg_log(SG_LOGITEM_ ##code, 1, 0, __LINE__)
#define _SG_WARN(code) _sg_log(SG_LOGITEM_ ##code, 2, 0, __LINE__)
#define _SG_INFO(code) _sg_log(SG_LOGITEM_ ##code, 3, 0, __LINE__)
#define _SG_LOGMSG(code,msg) _sg_log(SG_LOGITEM_ ##code, 3, msg, __LINE__)
#define _SG_VALIDATE(cond,code) if (!(cond)){ _sg.validate_error = SG_LOGITEM_ ##code; _sg_log(SG_LOGITEM_ ##code, 1, 0, __LINE__); }
static void _sg_log(sg_log_item log_item, uint32_t log_level, const char* msg, uint32_t line_nr) {
if (_sg.desc.logger.func) {
const char* filename = 0;
#if defined(SOKOL_DEBUG)
filename = __FILE__;
if (0 == msg) {
msg = _sg_log_messages[log_item];
}
#endif
_sg.desc.logger.func("sg", log_level, log_item, msg, line_nr, filename, _sg.desc.logger.user_data);
} else {
// for log level PANIC it would be 'undefined behaviour' to continue
if (log_level == 0) {
abort();
}
}
}
// ███ ███ ███████ ███ ███ ██████ ██████ ██ ██
// ████ ████ ██ ████ ████ ██ ██ ██ ██ ██ ██
// ██ ████ ██ █████ ██ ████ ██ ██ ██ ██████ ████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ██ ██ ██████ ██ ██ ██
//
// >>memory
// a helper macro to clear a struct with potentially ARC'ed ObjC references
#if defined(SOKOL_METAL)
#if defined(__cplusplus)
#define _SG_CLEAR_ARC_STRUCT(type, item) { item = type(); }
#else
#define _SG_CLEAR_ARC_STRUCT(type, item) { item = (type) { 0 }; }
#endif
#else
#define _SG_CLEAR_ARC_STRUCT(type, item) { _sg_clear(&item, sizeof(item)); }
#endif
_SOKOL_PRIVATE void _sg_clear(void* ptr, size_t size) {
SOKOL_ASSERT(ptr && (size > 0));
memset(ptr, 0, size);
}
_SOKOL_PRIVATE void* _sg_malloc(size_t size) {
SOKOL_ASSERT(size > 0);
void* ptr;
if (_sg.desc.allocator.alloc_fn) {
ptr = _sg.desc.allocator.alloc_fn(size, _sg.desc.allocator.user_data);
} else {
ptr = malloc(size);
}
if (0 == ptr) {
_SG_PANIC(MALLOC_FAILED);
}
return ptr;
}
_SOKOL_PRIVATE void* _sg_malloc_clear(size_t size) {
void* ptr = _sg_malloc(size);
_sg_clear(ptr, size);
return ptr;
}
_SOKOL_PRIVATE void _sg_free(void* ptr) {
if (_sg.desc.allocator.free_fn) {
_sg.desc.allocator.free_fn(ptr, _sg.desc.allocator.user_data);
} else {
free(ptr);
}
}
_SOKOL_PRIVATE bool _sg_strempty(const _sg_str_t* str) {
return 0 == str->buf[0];
}
_SOKOL_PRIVATE const char* _sg_strptr(const _sg_str_t* str) {
return &str->buf[0];
}
_SOKOL_PRIVATE void _sg_strcpy(_sg_str_t* dst, const char* src) {
SOKOL_ASSERT(dst);
if (src) {
#if defined(_MSC_VER)
strncpy_s(dst->buf, _SG_STRING_SIZE, src, (_SG_STRING_SIZE-1));
#else
strncpy(dst->buf, src, _SG_STRING_SIZE);
#endif
dst->buf[_SG_STRING_SIZE-1] = 0;
} else {
_sg_clear(dst->buf, _SG_STRING_SIZE);
}
}
// ██ ██ ███████ ██ ██████ ███████ ██████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ █████ ██ ██████ █████ ██████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ███████ ██ ███████ ██ ██ ███████
//
// >>helpers
_SOKOL_PRIVATE uint32_t _sg_align_u32(uint32_t val, uint32_t align) {
SOKOL_ASSERT((align > 0) && ((align & (align - 1)) == 0));
return (val + (align - 1)) & ~(align - 1);
}
typedef struct { int x, y, w, h; } _sg_recti_t;
_SOKOL_PRIVATE _sg_recti_t _sg_clipi(int x, int y, int w, int h, int clip_width, int clip_height) {
x = _sg_min(_sg_max(0, x), clip_width-1);
y = _sg_min(_sg_max(0, y), clip_height-1);
if ((x + w) > clip_width) {
w = clip_width - x;
}
if ((y + h) > clip_height) {
h = clip_height - y;
}
w = _sg_max(w, 1);
h = _sg_max(h, 1);
const _sg_recti_t res = { x, y, w, h };
return res;
}
_SOKOL_PRIVATE int _sg_vertexformat_bytesize(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_FLOAT: return 4;
case SG_VERTEXFORMAT_FLOAT2: return 8;
case SG_VERTEXFORMAT_FLOAT3: return 12;
case SG_VERTEXFORMAT_FLOAT4: return 16;
case SG_VERTEXFORMAT_BYTE4: return 4;
case SG_VERTEXFORMAT_BYTE4N: return 4;
case SG_VERTEXFORMAT_UBYTE4: return 4;
case SG_VERTEXFORMAT_UBYTE4N: return 4;
case SG_VERTEXFORMAT_SHORT2: return 4;
case SG_VERTEXFORMAT_SHORT2N: return 4;
case SG_VERTEXFORMAT_USHORT2N: return 4;
case SG_VERTEXFORMAT_SHORT4: return 8;
case SG_VERTEXFORMAT_SHORT4N: return 8;
case SG_VERTEXFORMAT_USHORT4N: return 8;
case SG_VERTEXFORMAT_UINT10_N2: return 4;
case SG_VERTEXFORMAT_HALF2: return 4;
case SG_VERTEXFORMAT_HALF4: return 8;
case SG_VERTEXFORMAT_INVALID: return 0;
default:
SOKOL_UNREACHABLE;
return -1;
}
}
_SOKOL_PRIVATE uint32_t _sg_uniform_alignment(sg_uniform_type type, int array_count, sg_uniform_layout ub_layout) {
if (ub_layout == SG_UNIFORMLAYOUT_NATIVE) {
return 1;
} else {
SOKOL_ASSERT(array_count > 0);
if (array_count == 1) {
switch (type) {
case SG_UNIFORMTYPE_FLOAT:
case SG_UNIFORMTYPE_INT:
return 4;
case SG_UNIFORMTYPE_FLOAT2:
case SG_UNIFORMTYPE_INT2:
return 8;
case SG_UNIFORMTYPE_FLOAT3:
case SG_UNIFORMTYPE_FLOAT4:
case SG_UNIFORMTYPE_INT3:
case SG_UNIFORMTYPE_INT4:
return 16;
case SG_UNIFORMTYPE_MAT4:
return 16;
default:
SOKOL_UNREACHABLE;
return 1;
}
} else {
return 16;
}
}
}
_SOKOL_PRIVATE uint32_t _sg_uniform_size(sg_uniform_type type, int array_count, sg_uniform_layout ub_layout) {
SOKOL_ASSERT(array_count > 0);
if (array_count == 1) {
switch (type) {
case SG_UNIFORMTYPE_FLOAT:
case SG_UNIFORMTYPE_INT:
return 4;
case SG_UNIFORMTYPE_FLOAT2:
case SG_UNIFORMTYPE_INT2:
return 8;
case SG_UNIFORMTYPE_FLOAT3:
case SG_UNIFORMTYPE_INT3:
return 12;
case SG_UNIFORMTYPE_FLOAT4:
case SG_UNIFORMTYPE_INT4:
return 16;
case SG_UNIFORMTYPE_MAT4:
return 64;
default:
SOKOL_UNREACHABLE;
return 0;
}
} else {
if (ub_layout == SG_UNIFORMLAYOUT_NATIVE) {
switch (type) {
case SG_UNIFORMTYPE_FLOAT:
case SG_UNIFORMTYPE_INT:
return 4 * (uint32_t)array_count;
case SG_UNIFORMTYPE_FLOAT2:
case SG_UNIFORMTYPE_INT2:
return 8 * (uint32_t)array_count;
case SG_UNIFORMTYPE_FLOAT3:
case SG_UNIFORMTYPE_INT3:
return 12 * (uint32_t)array_count;
case SG_UNIFORMTYPE_FLOAT4:
case SG_UNIFORMTYPE_INT4:
return 16 * (uint32_t)array_count;
case SG_UNIFORMTYPE_MAT4:
return 64 * (uint32_t)array_count;
default:
SOKOL_UNREACHABLE;
return 0;
}
} else {
switch (type) {
case SG_UNIFORMTYPE_FLOAT:
case SG_UNIFORMTYPE_FLOAT2:
case SG_UNIFORMTYPE_FLOAT3:
case SG_UNIFORMTYPE_FLOAT4:
case SG_UNIFORMTYPE_INT:
case SG_UNIFORMTYPE_INT2:
case SG_UNIFORMTYPE_INT3:
case SG_UNIFORMTYPE_INT4:
return 16 * (uint32_t)array_count;
case SG_UNIFORMTYPE_MAT4:
return 64 * (uint32_t)array_count;
default:
SOKOL_UNREACHABLE;
return 0;
}
}
}
}
_SOKOL_PRIVATE bool _sg_is_compressed_pixel_format(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_BC1_RGBA:
case SG_PIXELFORMAT_BC2_RGBA:
case SG_PIXELFORMAT_BC3_RGBA:
case SG_PIXELFORMAT_BC3_SRGBA:
case SG_PIXELFORMAT_BC4_R:
case SG_PIXELFORMAT_BC4_RSN:
case SG_PIXELFORMAT_BC5_RG:
case SG_PIXELFORMAT_BC5_RGSN:
case SG_PIXELFORMAT_BC6H_RGBF:
case SG_PIXELFORMAT_BC6H_RGBUF:
case SG_PIXELFORMAT_BC7_RGBA:
case SG_PIXELFORMAT_BC7_SRGBA:
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
case SG_PIXELFORMAT_ETC2_RGB8:
case SG_PIXELFORMAT_ETC2_SRGB8:
case SG_PIXELFORMAT_ETC2_RGB8A1:
case SG_PIXELFORMAT_ETC2_RGBA8:
case SG_PIXELFORMAT_ETC2_SRGB8A8:
case SG_PIXELFORMAT_EAC_R11:
case SG_PIXELFORMAT_EAC_R11SN:
case SG_PIXELFORMAT_EAC_RG11:
case SG_PIXELFORMAT_EAC_RG11SN:
case SG_PIXELFORMAT_ASTC_4x4_RGBA:
case SG_PIXELFORMAT_ASTC_4x4_SRGBA:
return true;
default:
return false;
}
}
_SOKOL_PRIVATE bool _sg_is_valid_rendertarget_color_format(sg_pixel_format fmt) {
const int fmt_index = (int) fmt;
SOKOL_ASSERT((fmt_index >= 0) && (fmt_index < _SG_PIXELFORMAT_NUM));
return _sg.formats[fmt_index].render && !_sg.formats[fmt_index].depth;
}
_SOKOL_PRIVATE bool _sg_is_valid_rendertarget_depth_format(sg_pixel_format fmt) {
const int fmt_index = (int) fmt;
SOKOL_ASSERT((fmt_index >= 0) && (fmt_index < _SG_PIXELFORMAT_NUM));
return _sg.formats[fmt_index].render && _sg.formats[fmt_index].depth;
}
_SOKOL_PRIVATE bool _sg_is_depth_or_depth_stencil_format(sg_pixel_format fmt) {
return (SG_PIXELFORMAT_DEPTH == fmt) || (SG_PIXELFORMAT_DEPTH_STENCIL == fmt);
}
_SOKOL_PRIVATE bool _sg_is_depth_stencil_format(sg_pixel_format fmt) {
return (SG_PIXELFORMAT_DEPTH_STENCIL == fmt);
}
_SOKOL_PRIVATE int _sg_pixelformat_bytesize(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8:
case SG_PIXELFORMAT_R8SN:
case SG_PIXELFORMAT_R8UI:
case SG_PIXELFORMAT_R8SI:
return 1;
case SG_PIXELFORMAT_R16:
case SG_PIXELFORMAT_R16SN:
case SG_PIXELFORMAT_R16UI:
case SG_PIXELFORMAT_R16SI:
case SG_PIXELFORMAT_R16F:
case SG_PIXELFORMAT_RG8:
case SG_PIXELFORMAT_RG8SN:
case SG_PIXELFORMAT_RG8UI:
case SG_PIXELFORMAT_RG8SI:
return 2;
case SG_PIXELFORMAT_R32UI:
case SG_PIXELFORMAT_R32SI:
case SG_PIXELFORMAT_R32F:
case SG_PIXELFORMAT_RG16:
case SG_PIXELFORMAT_RG16SN:
case SG_PIXELFORMAT_RG16UI:
case SG_PIXELFORMAT_RG16SI:
case SG_PIXELFORMAT_RG16F:
case SG_PIXELFORMAT_RGBA8:
case SG_PIXELFORMAT_SRGB8A8:
case SG_PIXELFORMAT_RGBA8SN:
case SG_PIXELFORMAT_RGBA8UI:
case SG_PIXELFORMAT_RGBA8SI:
case SG_PIXELFORMAT_BGRA8:
case SG_PIXELFORMAT_RGB10A2:
case SG_PIXELFORMAT_RG11B10F:
case SG_PIXELFORMAT_RGB9E5:
return 4;
case SG_PIXELFORMAT_RG32UI:
case SG_PIXELFORMAT_RG32SI:
case SG_PIXELFORMAT_RG32F:
case SG_PIXELFORMAT_RGBA16:
case SG_PIXELFORMAT_RGBA16SN:
case SG_PIXELFORMAT_RGBA16UI:
case SG_PIXELFORMAT_RGBA16SI:
case SG_PIXELFORMAT_RGBA16F:
return 8;
case SG_PIXELFORMAT_RGBA32UI:
case SG_PIXELFORMAT_RGBA32SI:
case SG_PIXELFORMAT_RGBA32F:
return 16;
case SG_PIXELFORMAT_DEPTH:
case SG_PIXELFORMAT_DEPTH_STENCIL:
return 4;
default:
SOKOL_UNREACHABLE;
return 0;
}
}
_SOKOL_PRIVATE int _sg_roundup(int val, int round_to) {
return (val+(round_to-1)) & ~(round_to-1);
}
_SOKOL_PRIVATE uint32_t _sg_roundup_u32(uint32_t val, uint32_t round_to) {
return (val+(round_to-1)) & ~(round_to-1);
}
_SOKOL_PRIVATE uint64_t _sg_roundup_u64(uint64_t val, uint64_t round_to) {
return (val+(round_to-1)) & ~(round_to-1);
}
_SOKOL_PRIVATE bool _sg_multiple_u64(uint64_t val, uint64_t of) {
return (val & (of-1)) == 0;
}
/* return row pitch for an image
see ComputePitch in https://github.com/microsoft/DirectXTex/blob/master/DirectXTex/DirectXTexUtil.cpp
For the special PVRTC pitch computation, see:
GL extension requirement (https://www.khronos.org/registry/OpenGL/extensions/IMG/IMG_texture_compression_pvrtc.txt)
Quote:
6) How is the imageSize argument calculated for the CompressedTexImage2D
and CompressedTexSubImage2D functions.
Resolution: For PVRTC 4BPP formats the imageSize is calculated as:
( max(width, 8) * max(height, 8) * 4 + 7) / 8
For PVRTC 2BPP formats the imageSize is calculated as:
( max(width, 16) * max(height, 8) * 2 + 7) / 8
*/
_SOKOL_PRIVATE int _sg_row_pitch(sg_pixel_format fmt, int width, int row_align) {
int pitch;
switch (fmt) {
case SG_PIXELFORMAT_BC1_RGBA:
case SG_PIXELFORMAT_BC4_R:
case SG_PIXELFORMAT_BC4_RSN:
case SG_PIXELFORMAT_ETC2_RGB8:
case SG_PIXELFORMAT_ETC2_SRGB8:
case SG_PIXELFORMAT_ETC2_RGB8A1:
case SG_PIXELFORMAT_EAC_R11:
case SG_PIXELFORMAT_EAC_R11SN:
pitch = ((width + 3) / 4) * 8;
pitch = pitch < 8 ? 8 : pitch;
break;
case SG_PIXELFORMAT_BC2_RGBA:
case SG_PIXELFORMAT_BC3_RGBA:
case SG_PIXELFORMAT_BC3_SRGBA:
case SG_PIXELFORMAT_BC5_RG:
case SG_PIXELFORMAT_BC5_RGSN:
case SG_PIXELFORMAT_BC6H_RGBF:
case SG_PIXELFORMAT_BC6H_RGBUF:
case SG_PIXELFORMAT_BC7_RGBA:
case SG_PIXELFORMAT_BC7_SRGBA:
case SG_PIXELFORMAT_ETC2_RGBA8:
case SG_PIXELFORMAT_ETC2_SRGB8A8:
case SG_PIXELFORMAT_EAC_RG11:
case SG_PIXELFORMAT_EAC_RG11SN:
case SG_PIXELFORMAT_ASTC_4x4_RGBA:
case SG_PIXELFORMAT_ASTC_4x4_SRGBA:
pitch = ((width + 3) / 4) * 16;
pitch = pitch < 16 ? 16 : pitch;
break;
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
pitch = (_sg_max(width, 8) * 4 + 7) / 8;
break;
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
pitch = (_sg_max(width, 16) * 2 + 7) / 8;
break;
default:
pitch = width * _sg_pixelformat_bytesize(fmt);
break;
}
pitch = _sg_roundup(pitch, row_align);
return pitch;
}
// compute the number of rows in a surface depending on pixel format
_SOKOL_PRIVATE int _sg_num_rows(sg_pixel_format fmt, int height) {
int num_rows;
switch (fmt) {
case SG_PIXELFORMAT_BC1_RGBA:
case SG_PIXELFORMAT_BC4_R:
case SG_PIXELFORMAT_BC4_RSN:
case SG_PIXELFORMAT_ETC2_RGB8:
case SG_PIXELFORMAT_ETC2_SRGB8:
case SG_PIXELFORMAT_ETC2_RGB8A1:
case SG_PIXELFORMAT_ETC2_RGBA8:
case SG_PIXELFORMAT_ETC2_SRGB8A8:
case SG_PIXELFORMAT_EAC_R11:
case SG_PIXELFORMAT_EAC_R11SN:
case SG_PIXELFORMAT_EAC_RG11:
case SG_PIXELFORMAT_EAC_RG11SN:
case SG_PIXELFORMAT_BC2_RGBA:
case SG_PIXELFORMAT_BC3_RGBA:
case SG_PIXELFORMAT_BC3_SRGBA:
case SG_PIXELFORMAT_BC5_RG:
case SG_PIXELFORMAT_BC5_RGSN:
case SG_PIXELFORMAT_BC6H_RGBF:
case SG_PIXELFORMAT_BC6H_RGBUF:
case SG_PIXELFORMAT_BC7_RGBA:
case SG_PIXELFORMAT_BC7_SRGBA:
case SG_PIXELFORMAT_ASTC_4x4_RGBA:
case SG_PIXELFORMAT_ASTC_4x4_SRGBA:
num_rows = ((height + 3) / 4);
break;
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
/* NOTE: this is most likely not correct because it ignores any
PVCRTC block size, but multiplied with _sg_row_pitch()
it gives the correct surface pitch.
See: https://www.khronos.org/registry/OpenGL/extensions/IMG/IMG_texture_compression_pvrtc.txt
*/
num_rows = ((_sg_max(height, 8) + 7) / 8) * 8;
break;
default:
num_rows = height;
break;
}
if (num_rows < 1) {
num_rows = 1;
}
return num_rows;
}
// return size of a mipmap level
_SOKOL_PRIVATE int _sg_miplevel_dim(int base_dim, int mip_level) {
return _sg_max(base_dim >> mip_level, 1);
}
/* return pitch of a 2D subimage / texture slice
see ComputePitch in https://github.com/microsoft/DirectXTex/blob/master/DirectXTex/DirectXTexUtil.cpp
*/
_SOKOL_PRIVATE int _sg_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align) {
int num_rows = _sg_num_rows(fmt, height);
return num_rows * _sg_row_pitch(fmt, width, row_align);
}
// capability table pixel format helper functions
_SOKOL_PRIVATE void _sg_pixelformat_all(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
pfi->filter = true;
pfi->blend = true;
pfi->render = true;
pfi->msaa = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_s(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sf(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
pfi->filter = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sr(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
pfi->render = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sfr(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
pfi->filter = true;
pfi->render = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_srmd(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
pfi->render = true;
pfi->msaa = true;
pfi->depth = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_srm(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
pfi->render = true;
pfi->msaa = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sfrm(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
pfi->filter = true;
pfi->render = true;
pfi->msaa = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sbrm(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
pfi->blend = true;
pfi->render = true;
pfi->msaa = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sbr(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
pfi->blend = true;
pfi->render = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sfbr(_sg_pixelformat_info_t* pfi) {
pfi->sample = true;
pfi->filter = true;
pfi->blend = true;
pfi->render = true;
}
_SOKOL_PRIVATE sg_pass_action _sg_pass_action_defaults(const sg_pass_action* action) {
SOKOL_ASSERT(action);
sg_pass_action res = *action;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (res.colors[i].load_action == _SG_LOADACTION_DEFAULT) {
res.colors[i].load_action = SG_LOADACTION_CLEAR;
res.colors[i].clear_value.r = SG_DEFAULT_CLEAR_RED;
res.colors[i].clear_value.g = SG_DEFAULT_CLEAR_GREEN;
res.colors[i].clear_value.b = SG_DEFAULT_CLEAR_BLUE;
res.colors[i].clear_value.a = SG_DEFAULT_CLEAR_ALPHA;
}
if (res.colors[i].store_action == _SG_STOREACTION_DEFAULT) {
res.colors[i].store_action = SG_STOREACTION_STORE;
}
}
if (res.depth.load_action == _SG_LOADACTION_DEFAULT) {
res.depth.load_action = SG_LOADACTION_CLEAR;
res.depth.clear_value = SG_DEFAULT_CLEAR_DEPTH;
}
if (res.depth.store_action == _SG_STOREACTION_DEFAULT) {
res.depth.store_action = SG_STOREACTION_DONTCARE;
}
if (res.stencil.load_action == _SG_LOADACTION_DEFAULT) {
res.stencil.load_action = SG_LOADACTION_CLEAR;
res.stencil.clear_value = SG_DEFAULT_CLEAR_STENCIL;
}
if (res.stencil.store_action == _SG_STOREACTION_DEFAULT) {
res.stencil.store_action = SG_STOREACTION_DONTCARE;
}
return res;
}
// ██████ ██ ██ ███ ███ ███ ███ ██ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████
// ██ ██ ██ ██ ████ ████ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██
// ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ████ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██████ ██ ██ ██ ██ ██ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████
//
// >>dummy backend
#if defined(SOKOL_DUMMY_BACKEND)
_SOKOL_PRIVATE void _sg_dummy_setup_backend(const sg_desc* desc) {
SOKOL_ASSERT(desc);
_SOKOL_UNUSED(desc);
_sg.backend = SG_BACKEND_DUMMY;
for (int i = SG_PIXELFORMAT_R8; i < SG_PIXELFORMAT_BC1_RGBA; i++) {
_sg.formats[i].sample = true;
_sg.formats[i].filter = true;
_sg.formats[i].render = true;
_sg.formats[i].blend = true;
_sg.formats[i].msaa = true;
}
_sg.formats[SG_PIXELFORMAT_DEPTH].depth = true;
_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL].depth = true;
}
_SOKOL_PRIVATE void _sg_dummy_discard_backend(void) {
// empty
}
_SOKOL_PRIVATE void _sg_dummy_reset_state_cache(void) {
// empty
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && desc);
_SOKOL_UNUSED(buf);
_SOKOL_UNUSED(desc);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_discard_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
_SOKOL_UNUSED(buf);
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && desc);
_SOKOL_UNUSED(img);
_SOKOL_UNUSED(desc);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_discard_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
_SOKOL_UNUSED(img);
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) {
SOKOL_ASSERT(smp && desc);
_SOKOL_UNUSED(smp);
_SOKOL_UNUSED(desc);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_discard_sampler(_sg_sampler_t* smp) {
SOKOL_ASSERT(smp);
_SOKOL_UNUSED(smp);
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && desc);
_SOKOL_UNUSED(shd);
_SOKOL_UNUSED(desc);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_discard_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
_SOKOL_UNUSED(shd);
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && desc);
pip->shader = shd;
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
const sg_vertex_attr_state* a_state = &desc->layout.attrs[attr_index];
if (a_state->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS);
pip->cmn.vertex_buffer_layout_active[a_state->buffer_index] = true;
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_discard_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
_SOKOL_UNUSED(pip);
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_img, const sg_attachments_desc* desc) {
SOKOL_ASSERT(atts && desc);
SOKOL_ASSERT(color_images && resolve_images);
for (int i = 0; i < atts->cmn.num_colors; i++) {
const sg_attachment_desc* color_desc = &desc->colors[i];
_SOKOL_UNUSED(color_desc);
SOKOL_ASSERT(color_desc->image.id != SG_INVALID_ID);
SOKOL_ASSERT(0 == atts->dmy.colors[i].image);
SOKOL_ASSERT(color_images[i] && (color_images[i]->slot.id == color_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(color_images[i]->cmn.pixel_format));
atts->dmy.colors[i].image = color_images[i];
const sg_attachment_desc* resolve_desc = &desc->resolves[i];
if (resolve_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(0 == atts->dmy.resolves[i].image);
SOKOL_ASSERT(resolve_images[i] && (resolve_images[i]->slot.id == resolve_desc->image.id));
SOKOL_ASSERT(color_images[i] && (color_images[i]->cmn.pixel_format == resolve_images[i]->cmn.pixel_format));
atts->dmy.resolves[i].image = resolve_images[i];
}
}
SOKOL_ASSERT(0 == atts->dmy.depth_stencil.image);
const sg_attachment_desc* ds_desc = &desc->depth_stencil;
if (ds_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(ds_img && (ds_img->slot.id == ds_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(ds_img->cmn.pixel_format));
atts->dmy.depth_stencil.image = ds_img;
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_discard_attachments(_sg_attachments_t* atts) {
SOKOL_ASSERT(atts);
_SOKOL_UNUSED(atts);
}
_SOKOL_PRIVATE _sg_image_t* _sg_dummy_attachments_color_image(const _sg_attachments_t* atts, int index) {
SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
return atts->dmy.colors[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_dummy_attachments_resolve_image(const _sg_attachments_t* atts, int index) {
SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
return atts->dmy.resolves[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_dummy_attachments_ds_image(const _sg_attachments_t* atts) {
SOKOL_ASSERT(atts);
return atts->dmy.depth_stencil.image;
}
_SOKOL_PRIVATE void _sg_dummy_begin_pass(const sg_pass* pass) {
SOKOL_ASSERT(pass);
_SOKOL_UNUSED(pass);
}
_SOKOL_PRIVATE void _sg_dummy_end_pass(void) {
// empty
}
_SOKOL_PRIVATE void _sg_dummy_commit(void) {
// empty
}
_SOKOL_PRIVATE void _sg_dummy_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
_SOKOL_UNUSED(x);
_SOKOL_UNUSED(y);
_SOKOL_UNUSED(w);
_SOKOL_UNUSED(h);
_SOKOL_UNUSED(origin_top_left);
}
_SOKOL_PRIVATE void _sg_dummy_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
_SOKOL_UNUSED(x);
_SOKOL_UNUSED(y);
_SOKOL_UNUSED(w);
_SOKOL_UNUSED(h);
_SOKOL_UNUSED(origin_top_left);
}
_SOKOL_PRIVATE void _sg_dummy_apply_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
_SOKOL_UNUSED(pip);
}
_SOKOL_PRIVATE bool _sg_dummy_apply_bindings(_sg_bindings_t* bnd) {
SOKOL_ASSERT(bnd);
SOKOL_ASSERT(bnd->pip);
_SOKOL_UNUSED(bnd);
return true;
}
_SOKOL_PRIVATE void _sg_dummy_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
_SOKOL_UNUSED(stage_index);
_SOKOL_UNUSED(ub_index);
_SOKOL_UNUSED(data);
}
_SOKOL_PRIVATE void _sg_dummy_draw(int base_element, int num_elements, int num_instances) {
_SOKOL_UNUSED(base_element);
_SOKOL_UNUSED(num_elements);
_SOKOL_UNUSED(num_instances);
}
_SOKOL_PRIVATE void _sg_dummy_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
_SOKOL_UNUSED(data);
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
}
_SOKOL_PRIVATE bool _sg_dummy_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
_SOKOL_UNUSED(data);
if (new_frame) {
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
}
return true;
}
_SOKOL_PRIVATE void _sg_dummy_update_image(_sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(img && data);
_SOKOL_UNUSED(data);
if (++img->cmn.active_slot >= img->cmn.num_slots) {
img->cmn.active_slot = 0;
}
}
// ██████ ██████ ███████ ███ ██ ██████ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████
// ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██
// ██ ██ ██████ █████ ██ ██ ██ ██ ███ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ███████ ██ ████ ██████ ███████ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████
//
// >>opengl backend
#elif defined(_SOKOL_ANY_GL)
// optional GL loader for win32
#if defined(_SOKOL_USE_WIN32_GL_LOADER)
#ifndef SG_GL_FUNCS_EXT
#define SG_GL_FUNCS_EXT
#endif
// X Macro list of GL function names and signatures
#define _SG_GL_FUNCS \
SG_GL_FUNCS_EXT \
_SG_XMACRO(glBindVertexArray, void, (GLuint array)) \
_SG_XMACRO(glFramebufferTextureLayer, void, (GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer)) \
_SG_XMACRO(glGenFramebuffers, void, (GLsizei n, GLuint * framebuffers)) \
_SG_XMACRO(glBindFramebuffer, void, (GLenum target, GLuint framebuffer)) \
_SG_XMACRO(glBindRenderbuffer, void, (GLenum target, GLuint renderbuffer)) \
_SG_XMACRO(glGetStringi, const GLubyte *, (GLenum name, GLuint index)) \
_SG_XMACRO(glClearBufferfi, void, (GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil)) \
_SG_XMACRO(glClearBufferfv, void, (GLenum buffer, GLint drawbuffer, const GLfloat * value)) \
_SG_XMACRO(glClearBufferuiv, void, (GLenum buffer, GLint drawbuffer, const GLuint * value)) \
_SG_XMACRO(glClearBufferiv, void, (GLenum buffer, GLint drawbuffer, const GLint * value)) \
_SG_XMACRO(glDeleteRenderbuffers, void, (GLsizei n, const GLuint * renderbuffers)) \
_SG_XMACRO(glUniform1fv, void, (GLint location, GLsizei count, const GLfloat * value)) \
_SG_XMACRO(glUniform2fv, void, (GLint location, GLsizei count, const GLfloat * value)) \
_SG_XMACRO(glUniform3fv, void, (GLint location, GLsizei count, const GLfloat * value)) \
_SG_XMACRO(glUniform4fv, void, (GLint location, GLsizei count, const GLfloat * value)) \
_SG_XMACRO(glUniform1iv, void, (GLint location, GLsizei count, const GLint * value)) \
_SG_XMACRO(glUniform2iv, void, (GLint location, GLsizei count, const GLint * value)) \
_SG_XMACRO(glUniform3iv, void, (GLint location, GLsizei count, const GLint * value)) \
_SG_XMACRO(glUniform4iv, void, (GLint location, GLsizei count, const GLint * value)) \
_SG_XMACRO(glUniformMatrix4fv, void, (GLint location, GLsizei count, GLboolean transpose, const GLfloat * value)) \
_SG_XMACRO(glUseProgram, void, (GLuint program)) \
_SG_XMACRO(glShaderSource, void, (GLuint shader, GLsizei count, const GLchar *const* string, const GLint * length)) \
_SG_XMACRO(glLinkProgram, void, (GLuint program)) \
_SG_XMACRO(glGetUniformLocation, GLint, (GLuint program, const GLchar * name)) \
_SG_XMACRO(glGetShaderiv, void, (GLuint shader, GLenum pname, GLint * params)) \
_SG_XMACRO(glGetProgramInfoLog, void, (GLuint program, GLsizei bufSize, GLsizei * length, GLchar * infoLog)) \
_SG_XMACRO(glGetAttribLocation, GLint, (GLuint program, const GLchar * name)) \
_SG_XMACRO(glDisableVertexAttribArray, void, (GLuint index)) \
_SG_XMACRO(glDeleteShader, void, (GLuint shader)) \
_SG_XMACRO(glDeleteProgram, void, (GLuint program)) \
_SG_XMACRO(glCompileShader, void, (GLuint shader)) \
_SG_XMACRO(glStencilFuncSeparate, void, (GLenum face, GLenum func, GLint ref, GLuint mask)) \
_SG_XMACRO(glStencilOpSeparate, void, (GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass)) \
_SG_XMACRO(glRenderbufferStorageMultisample, void, (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height)) \
_SG_XMACRO(glDrawBuffers, void, (GLsizei n, const GLenum * bufs)) \
_SG_XMACRO(glVertexAttribDivisor, void, (GLuint index, GLuint divisor)) \
_SG_XMACRO(glBufferSubData, void, (GLenum target, GLintptr offset, GLsizeiptr size, const void * data)) \
_SG_XMACRO(glGenBuffers, void, (GLsizei n, GLuint * buffers)) \
_SG_XMACRO(glCheckFramebufferStatus, GLenum, (GLenum target)) \
_SG_XMACRO(glFramebufferRenderbuffer, void, (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)) \
_SG_XMACRO(glCompressedTexImage2D, void, (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const void * data)) \
_SG_XMACRO(glCompressedTexImage3D, void, (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void * data)) \
_SG_XMACRO(glActiveTexture, void, (GLenum texture)) \
_SG_XMACRO(glTexSubImage3D, void, (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void * pixels)) \
_SG_XMACRO(glRenderbufferStorage, void, (GLenum target, GLenum internalformat, GLsizei width, GLsizei height)) \
_SG_XMACRO(glGenTextures, void, (GLsizei n, GLuint * textures)) \
_SG_XMACRO(glPolygonOffset, void, (GLfloat factor, GLfloat units)) \
_SG_XMACRO(glDrawElements, void, (GLenum mode, GLsizei count, GLenum type, const void * indices)) \
_SG_XMACRO(glDeleteFramebuffers, void, (GLsizei n, const GLuint * framebuffers)) \
_SG_XMACRO(glBlendEquationSeparate, void, (GLenum modeRGB, GLenum modeAlpha)) \
_SG_XMACRO(glDeleteTextures, void, (GLsizei n, const GLuint * textures)) \
_SG_XMACRO(glGetProgramiv, void, (GLuint program, GLenum pname, GLint * params)) \
_SG_XMACRO(glBindTexture, void, (GLenum target, GLuint texture)) \
_SG_XMACRO(glTexImage3D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void * pixels)) \
_SG_XMACRO(glCreateShader, GLuint, (GLenum type)) \
_SG_XMACRO(glTexSubImage2D, void, (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void * pixels)) \
_SG_XMACRO(glFramebufferTexture2D, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)) \
_SG_XMACRO(glCreateProgram, GLuint, (void)) \
_SG_XMACRO(glViewport, void, (GLint x, GLint y, GLsizei width, GLsizei height)) \
_SG_XMACRO(glDeleteBuffers, void, (GLsizei n, const GLuint * buffers)) \
_SG_XMACRO(glDrawArrays, void, (GLenum mode, GLint first, GLsizei count)) \
_SG_XMACRO(glDrawElementsInstanced, void, (GLenum mode, GLsizei count, GLenum type, const void * indices, GLsizei instancecount)) \
_SG_XMACRO(glVertexAttribPointer, void, (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void * pointer)) \
_SG_XMACRO(glUniform1i, void, (GLint location, GLint v0)) \
_SG_XMACRO(glDisable, void, (GLenum cap)) \
_SG_XMACRO(glColorMask, void, (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)) \
_SG_XMACRO(glColorMaski, void, (GLuint buf, GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)) \
_SG_XMACRO(glBindBuffer, void, (GLenum target, GLuint buffer)) \
_SG_XMACRO(glDeleteVertexArrays, void, (GLsizei n, const GLuint * arrays)) \
_SG_XMACRO(glDepthMask, void, (GLboolean flag)) \
_SG_XMACRO(glDrawArraysInstanced, void, (GLenum mode, GLint first, GLsizei count, GLsizei instancecount)) \
_SG_XMACRO(glScissor, void, (GLint x, GLint y, GLsizei width, GLsizei height)) \
_SG_XMACRO(glGenRenderbuffers, void, (GLsizei n, GLuint * renderbuffers)) \
_SG_XMACRO(glBufferData, void, (GLenum target, GLsizeiptr size, const void * data, GLenum usage)) \
_SG_XMACRO(glBlendFuncSeparate, void, (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha)) \
_SG_XMACRO(glTexParameteri, void, (GLenum target, GLenum pname, GLint param)) \
_SG_XMACRO(glGetIntegerv, void, (GLenum pname, GLint * data)) \
_SG_XMACRO(glEnable, void, (GLenum cap)) \
_SG_XMACRO(glBlitFramebuffer, void, (GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter)) \
_SG_XMACRO(glStencilMask, void, (GLuint mask)) \
_SG_XMACRO(glAttachShader, void, (GLuint program, GLuint shader)) \
_SG_XMACRO(glGetError, GLenum, (void)) \
_SG_XMACRO(glBlendColor, void, (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)) \
_SG_XMACRO(glTexParameterf, void, (GLenum target, GLenum pname, GLfloat param)) \
_SG_XMACRO(glTexParameterfv, void, (GLenum target, GLenum pname, const GLfloat* params)) \
_SG_XMACRO(glGetShaderInfoLog, void, (GLuint shader, GLsizei bufSize, GLsizei * length, GLchar * infoLog)) \
_SG_XMACRO(glDepthFunc, void, (GLenum func)) \
_SG_XMACRO(glStencilOp , void, (GLenum fail, GLenum zfail, GLenum zpass)) \
_SG_XMACRO(glStencilFunc, void, (GLenum func, GLint ref, GLuint mask)) \
_SG_XMACRO(glEnableVertexAttribArray, void, (GLuint index)) \
_SG_XMACRO(glBlendFunc, void, (GLenum sfactor, GLenum dfactor)) \
_SG_XMACRO(glReadBuffer, void, (GLenum src)) \
_SG_XMACRO(glTexImage2D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void * pixels)) \
_SG_XMACRO(glGenVertexArrays, void, (GLsizei n, GLuint * arrays)) \
_SG_XMACRO(glFrontFace, void, (GLenum mode)) \
_SG_XMACRO(glCullFace, void, (GLenum mode)) \
_SG_XMACRO(glPixelStorei, void, (GLenum pname, GLint param)) \
_SG_XMACRO(glBindSampler, void, (GLuint unit, GLuint sampler)) \
_SG_XMACRO(glGenSamplers, void, (GLsizei n, GLuint* samplers)) \
_SG_XMACRO(glSamplerParameteri, void, (GLuint sampler, GLenum pname, GLint param)) \
_SG_XMACRO(glSamplerParameterf, void, (GLuint sampler, GLenum pname, GLfloat param)) \
_SG_XMACRO(glSamplerParameterfv, void, (GLuint sampler, GLenum pname, const GLfloat* params)) \
_SG_XMACRO(glDeleteSamplers, void, (GLsizei n, const GLuint* samplers)) \
_SG_XMACRO(glBindBufferBase, void, (GLenum target, GLuint index, GLuint buffer))
// generate GL function pointer typedefs
#define _SG_XMACRO(name, ret, args) typedef ret (GL_APIENTRY* PFN_ ## name) args;
_SG_GL_FUNCS
#undef _SG_XMACRO
// generate GL function pointers
#define _SG_XMACRO(name, ret, args) static PFN_ ## name name;
_SG_GL_FUNCS
#undef _SG_XMACRO
// helper function to lookup GL functions in GL DLL
typedef PROC (WINAPI * _sg_wglGetProcAddress)(LPCSTR);
_SOKOL_PRIVATE void* _sg_gl_getprocaddr(const char* name, _sg_wglGetProcAddress wgl_getprocaddress) {
void* proc_addr = (void*) wgl_getprocaddress(name);
if (0 == proc_addr) {
proc_addr = (void*) GetProcAddress(_sg.gl.opengl32_dll, name);
}
SOKOL_ASSERT(proc_addr);
return proc_addr;
}
// populate GL function pointers
_SOKOL_PRIVATE void _sg_gl_load_opengl(void) {
SOKOL_ASSERT(0 == _sg.gl.opengl32_dll);
_sg.gl.opengl32_dll = LoadLibraryA("opengl32.dll");
SOKOL_ASSERT(_sg.gl.opengl32_dll);
_sg_wglGetProcAddress wgl_getprocaddress = (_sg_wglGetProcAddress) GetProcAddress(_sg.gl.opengl32_dll, "wglGetProcAddress");
SOKOL_ASSERT(wgl_getprocaddress);
#define _SG_XMACRO(name, ret, args) name = (PFN_ ## name) _sg_gl_getprocaddr(#name, wgl_getprocaddress);
_SG_GL_FUNCS
#undef _SG_XMACRO
}
_SOKOL_PRIVATE void _sg_gl_unload_opengl(void) {
SOKOL_ASSERT(_sg.gl.opengl32_dll);
FreeLibrary(_sg.gl.opengl32_dll);
_sg.gl.opengl32_dll = 0;
}
#endif // _SOKOL_USE_WIN32_GL_LOADER
//-- type translation ----------------------------------------------------------
_SOKOL_PRIVATE GLenum _sg_gl_buffer_target(sg_buffer_type t) {
switch (t) {
case SG_BUFFERTYPE_VERTEXBUFFER: return GL_ARRAY_BUFFER;
case SG_BUFFERTYPE_INDEXBUFFER: return GL_ELEMENT_ARRAY_BUFFER;
case SG_BUFFERTYPE_STORAGEBUFFER: return GL_SHADER_STORAGE_BUFFER;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_texture_target(sg_image_type t) {
switch (t) {
case SG_IMAGETYPE_2D: return GL_TEXTURE_2D;
case SG_IMAGETYPE_CUBE: return GL_TEXTURE_CUBE_MAP;
case SG_IMAGETYPE_3D: return GL_TEXTURE_3D;
case SG_IMAGETYPE_ARRAY: return GL_TEXTURE_2D_ARRAY;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_usage(sg_usage u) {
switch (u) {
case SG_USAGE_IMMUTABLE: return GL_STATIC_DRAW;
case SG_USAGE_DYNAMIC: return GL_DYNAMIC_DRAW;
case SG_USAGE_STREAM: return GL_STREAM_DRAW;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_shader_stage(sg_shader_stage stage) {
switch (stage) {
case SG_SHADERSTAGE_VS: return GL_VERTEX_SHADER;
case SG_SHADERSTAGE_FS: return GL_FRAGMENT_SHADER;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLint _sg_gl_vertexformat_size(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_FLOAT: return 1;
case SG_VERTEXFORMAT_FLOAT2: return 2;
case SG_VERTEXFORMAT_FLOAT3: return 3;
case SG_VERTEXFORMAT_FLOAT4: return 4;
case SG_VERTEXFORMAT_BYTE4: return 4;
case SG_VERTEXFORMAT_BYTE4N: return 4;
case SG_VERTEXFORMAT_UBYTE4: return 4;
case SG_VERTEXFORMAT_UBYTE4N: return 4;
case SG_VERTEXFORMAT_SHORT2: return 2;
case SG_VERTEXFORMAT_SHORT2N: return 2;
case SG_VERTEXFORMAT_USHORT2N: return 2;
case SG_VERTEXFORMAT_SHORT4: return 4;
case SG_VERTEXFORMAT_SHORT4N: return 4;
case SG_VERTEXFORMAT_USHORT4N: return 4;
case SG_VERTEXFORMAT_UINT10_N2: return 4;
case SG_VERTEXFORMAT_HALF2: return 2;
case SG_VERTEXFORMAT_HALF4: return 4;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_vertexformat_type(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_FLOAT:
case SG_VERTEXFORMAT_FLOAT2:
case SG_VERTEXFORMAT_FLOAT3:
case SG_VERTEXFORMAT_FLOAT4:
return GL_FLOAT;
case SG_VERTEXFORMAT_BYTE4:
case SG_VERTEXFORMAT_BYTE4N:
return GL_BYTE;
case SG_VERTEXFORMAT_UBYTE4:
case SG_VERTEXFORMAT_UBYTE4N:
return GL_UNSIGNED_BYTE;
case SG_VERTEXFORMAT_SHORT2:
case SG_VERTEXFORMAT_SHORT2N:
case SG_VERTEXFORMAT_SHORT4:
case SG_VERTEXFORMAT_SHORT4N:
return GL_SHORT;
case SG_VERTEXFORMAT_USHORT2N:
case SG_VERTEXFORMAT_USHORT4N:
return GL_UNSIGNED_SHORT;
case SG_VERTEXFORMAT_UINT10_N2:
return GL_UNSIGNED_INT_2_10_10_10_REV;
case SG_VERTEXFORMAT_HALF2:
case SG_VERTEXFORMAT_HALF4:
return GL_HALF_FLOAT;
default:
SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLboolean _sg_gl_vertexformat_normalized(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_BYTE4N:
case SG_VERTEXFORMAT_UBYTE4N:
case SG_VERTEXFORMAT_SHORT2N:
case SG_VERTEXFORMAT_USHORT2N:
case SG_VERTEXFORMAT_SHORT4N:
case SG_VERTEXFORMAT_USHORT4N:
case SG_VERTEXFORMAT_UINT10_N2:
return GL_TRUE;
default:
return GL_FALSE;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_primitive_type(sg_primitive_type t) {
switch (t) {
case SG_PRIMITIVETYPE_POINTS: return GL_POINTS;
case SG_PRIMITIVETYPE_LINES: return GL_LINES;
case SG_PRIMITIVETYPE_LINE_STRIP: return GL_LINE_STRIP;
case SG_PRIMITIVETYPE_TRIANGLES: return GL_TRIANGLES;
case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return GL_TRIANGLE_STRIP;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_index_type(sg_index_type t) {
switch (t) {
case SG_INDEXTYPE_NONE: return 0;
case SG_INDEXTYPE_UINT16: return GL_UNSIGNED_SHORT;
case SG_INDEXTYPE_UINT32: return GL_UNSIGNED_INT;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_compare_func(sg_compare_func cmp) {
switch (cmp) {
case SG_COMPAREFUNC_NEVER: return GL_NEVER;
case SG_COMPAREFUNC_LESS: return GL_LESS;
case SG_COMPAREFUNC_EQUAL: return GL_EQUAL;
case SG_COMPAREFUNC_LESS_EQUAL: return GL_LEQUAL;
case SG_COMPAREFUNC_GREATER: return GL_GREATER;
case SG_COMPAREFUNC_NOT_EQUAL: return GL_NOTEQUAL;
case SG_COMPAREFUNC_GREATER_EQUAL: return GL_GEQUAL;
case SG_COMPAREFUNC_ALWAYS: return GL_ALWAYS;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_stencil_op(sg_stencil_op op) {
switch (op) {
case SG_STENCILOP_KEEP: return GL_KEEP;
case SG_STENCILOP_ZERO: return GL_ZERO;
case SG_STENCILOP_REPLACE: return GL_REPLACE;
case SG_STENCILOP_INCR_CLAMP: return GL_INCR;
case SG_STENCILOP_DECR_CLAMP: return GL_DECR;
case SG_STENCILOP_INVERT: return GL_INVERT;
case SG_STENCILOP_INCR_WRAP: return GL_INCR_WRAP;
case SG_STENCILOP_DECR_WRAP: return GL_DECR_WRAP;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_blend_factor(sg_blend_factor f) {
switch (f) {
case SG_BLENDFACTOR_ZERO: return GL_ZERO;
case SG_BLENDFACTOR_ONE: return GL_ONE;
case SG_BLENDFACTOR_SRC_COLOR: return GL_SRC_COLOR;
case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return GL_ONE_MINUS_SRC_COLOR;
case SG_BLENDFACTOR_SRC_ALPHA: return GL_SRC_ALPHA;
case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return GL_ONE_MINUS_SRC_ALPHA;
case SG_BLENDFACTOR_DST_COLOR: return GL_DST_COLOR;
case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return GL_ONE_MINUS_DST_COLOR;
case SG_BLENDFACTOR_DST_ALPHA: return GL_DST_ALPHA;
case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return GL_ONE_MINUS_DST_ALPHA;
case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return GL_SRC_ALPHA_SATURATE;
case SG_BLENDFACTOR_BLEND_COLOR: return GL_CONSTANT_COLOR;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return GL_ONE_MINUS_CONSTANT_COLOR;
case SG_BLENDFACTOR_BLEND_ALPHA: return GL_CONSTANT_ALPHA;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return GL_ONE_MINUS_CONSTANT_ALPHA;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_blend_op(sg_blend_op op) {
switch (op) {
case SG_BLENDOP_ADD: return GL_FUNC_ADD;
case SG_BLENDOP_SUBTRACT: return GL_FUNC_SUBTRACT;
case SG_BLENDOP_REVERSE_SUBTRACT: return GL_FUNC_REVERSE_SUBTRACT;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_min_filter(sg_filter min_f, sg_filter mipmap_f) {
if (min_f == SG_FILTER_NEAREST) {
switch (mipmap_f) {
case SG_FILTER_NEAREST: return GL_NEAREST_MIPMAP_NEAREST;
case SG_FILTER_LINEAR: return GL_NEAREST_MIPMAP_LINEAR;
default: SOKOL_UNREACHABLE; return (GLenum)0;
}
} else if (min_f == SG_FILTER_LINEAR) {
switch (mipmap_f) {
case SG_FILTER_NEAREST: return GL_LINEAR_MIPMAP_NEAREST;
case SG_FILTER_LINEAR: return GL_LINEAR_MIPMAP_LINEAR;
default: SOKOL_UNREACHABLE; return (GLenum)0;
}
} else {
SOKOL_UNREACHABLE; return (GLenum)0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_mag_filter(sg_filter mag_f) {
if (mag_f == SG_FILTER_NEAREST) {
return GL_NEAREST;
} else {
return GL_LINEAR;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_wrap(sg_wrap w) {
switch (w) {
case SG_WRAP_CLAMP_TO_EDGE: return GL_CLAMP_TO_EDGE;
#if defined(SOKOL_GLCORE)
case SG_WRAP_CLAMP_TO_BORDER: return GL_CLAMP_TO_BORDER;
#else
case SG_WRAP_CLAMP_TO_BORDER: return GL_CLAMP_TO_EDGE;
#endif
case SG_WRAP_REPEAT: return GL_REPEAT;
case SG_WRAP_MIRRORED_REPEAT: return GL_MIRRORED_REPEAT;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_teximage_type(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8:
case SG_PIXELFORMAT_R8UI:
case SG_PIXELFORMAT_RG8:
case SG_PIXELFORMAT_RG8UI:
case SG_PIXELFORMAT_RGBA8:
case SG_PIXELFORMAT_SRGB8A8:
case SG_PIXELFORMAT_RGBA8UI:
case SG_PIXELFORMAT_BGRA8:
return GL_UNSIGNED_BYTE;
case SG_PIXELFORMAT_R8SN:
case SG_PIXELFORMAT_R8SI:
case SG_PIXELFORMAT_RG8SN:
case SG_PIXELFORMAT_RG8SI:
case SG_PIXELFORMAT_RGBA8SN:
case SG_PIXELFORMAT_RGBA8SI:
return GL_BYTE;
case SG_PIXELFORMAT_R16:
case SG_PIXELFORMAT_R16UI:
case SG_PIXELFORMAT_RG16:
case SG_PIXELFORMAT_RG16UI:
case SG_PIXELFORMAT_RGBA16:
case SG_PIXELFORMAT_RGBA16UI:
return GL_UNSIGNED_SHORT;
case SG_PIXELFORMAT_R16SN:
case SG_PIXELFORMAT_R16SI:
case SG_PIXELFORMAT_RG16SN:
case SG_PIXELFORMAT_RG16SI:
case SG_PIXELFORMAT_RGBA16SN:
case SG_PIXELFORMAT_RGBA16SI:
return GL_SHORT;
case SG_PIXELFORMAT_R16F:
case SG_PIXELFORMAT_RG16F:
case SG_PIXELFORMAT_RGBA16F:
return GL_HALF_FLOAT;
case SG_PIXELFORMAT_R32UI:
case SG_PIXELFORMAT_RG32UI:
case SG_PIXELFORMAT_RGBA32UI:
return GL_UNSIGNED_INT;
case SG_PIXELFORMAT_R32SI:
case SG_PIXELFORMAT_RG32SI:
case SG_PIXELFORMAT_RGBA32SI:
return GL_INT;
case SG_PIXELFORMAT_R32F:
case SG_PIXELFORMAT_RG32F:
case SG_PIXELFORMAT_RGBA32F:
return GL_FLOAT;
case SG_PIXELFORMAT_RGB10A2:
return GL_UNSIGNED_INT_2_10_10_10_REV;
case SG_PIXELFORMAT_RG11B10F:
return GL_UNSIGNED_INT_10F_11F_11F_REV;
case SG_PIXELFORMAT_RGB9E5:
return GL_UNSIGNED_INT_5_9_9_9_REV;
case SG_PIXELFORMAT_DEPTH:
return GL_FLOAT;
case SG_PIXELFORMAT_DEPTH_STENCIL:
return GL_UNSIGNED_INT_24_8;
default:
SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_teximage_format(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8:
case SG_PIXELFORMAT_R8SN:
case SG_PIXELFORMAT_R16:
case SG_PIXELFORMAT_R16SN:
case SG_PIXELFORMAT_R16F:
case SG_PIXELFORMAT_R32F:
return GL_RED;
case SG_PIXELFORMAT_R8UI:
case SG_PIXELFORMAT_R8SI:
case SG_PIXELFORMAT_R16UI:
case SG_PIXELFORMAT_R16SI:
case SG_PIXELFORMAT_R32UI:
case SG_PIXELFORMAT_R32SI:
return GL_RED_INTEGER;
case SG_PIXELFORMAT_RG8:
case SG_PIXELFORMAT_RG8SN:
case SG_PIXELFORMAT_RG16:
case SG_PIXELFORMAT_RG16SN:
case SG_PIXELFORMAT_RG16F:
case SG_PIXELFORMAT_RG32F:
return GL_RG;
case SG_PIXELFORMAT_RG8UI:
case SG_PIXELFORMAT_RG8SI:
case SG_PIXELFORMAT_RG16UI:
case SG_PIXELFORMAT_RG16SI:
case SG_PIXELFORMAT_RG32UI:
case SG_PIXELFORMAT_RG32SI:
return GL_RG_INTEGER;
case SG_PIXELFORMAT_RGBA8:
case SG_PIXELFORMAT_SRGB8A8:
case SG_PIXELFORMAT_RGBA8SN:
case SG_PIXELFORMAT_RGBA16:
case SG_PIXELFORMAT_RGBA16SN:
case SG_PIXELFORMAT_RGBA16F:
case SG_PIXELFORMAT_RGBA32F:
case SG_PIXELFORMAT_RGB10A2:
return GL_RGBA;
case SG_PIXELFORMAT_RGBA8UI:
case SG_PIXELFORMAT_RGBA8SI:
case SG_PIXELFORMAT_RGBA16UI:
case SG_PIXELFORMAT_RGBA16SI:
case SG_PIXELFORMAT_RGBA32UI:
case SG_PIXELFORMAT_RGBA32SI:
return GL_RGBA_INTEGER;
case SG_PIXELFORMAT_RG11B10F:
case SG_PIXELFORMAT_RGB9E5:
return GL_RGB;
case SG_PIXELFORMAT_DEPTH:
return GL_DEPTH_COMPONENT;
case SG_PIXELFORMAT_DEPTH_STENCIL:
return GL_DEPTH_STENCIL;
case SG_PIXELFORMAT_BC1_RGBA:
return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
case SG_PIXELFORMAT_BC2_RGBA:
return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
case SG_PIXELFORMAT_BC3_RGBA:
return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
case SG_PIXELFORMAT_BC3_SRGBA:
return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
case SG_PIXELFORMAT_BC4_R:
return GL_COMPRESSED_RED_RGTC1;
case SG_PIXELFORMAT_BC4_RSN:
return GL_COMPRESSED_SIGNED_RED_RGTC1;
case SG_PIXELFORMAT_BC5_RG:
return GL_COMPRESSED_RED_GREEN_RGTC2;
case SG_PIXELFORMAT_BC5_RGSN:
return GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2;
case SG_PIXELFORMAT_BC6H_RGBF:
return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB;
case SG_PIXELFORMAT_BC6H_RGBUF:
return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB;
case SG_PIXELFORMAT_BC7_RGBA:
return GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
case SG_PIXELFORMAT_BC7_SRGBA:
return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB;
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
return GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
case SG_PIXELFORMAT_ETC2_RGB8:
return GL_COMPRESSED_RGB8_ETC2;
case SG_PIXELFORMAT_ETC2_SRGB8:
return GL_COMPRESSED_SRGB8_ETC2;
case SG_PIXELFORMAT_ETC2_RGB8A1:
return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
case SG_PIXELFORMAT_ETC2_RGBA8:
return GL_COMPRESSED_RGBA8_ETC2_EAC;
case SG_PIXELFORMAT_ETC2_SRGB8A8:
return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
case SG_PIXELFORMAT_EAC_R11:
return GL_COMPRESSED_R11_EAC;
case SG_PIXELFORMAT_EAC_R11SN:
return GL_COMPRESSED_SIGNED_R11_EAC;
case SG_PIXELFORMAT_EAC_RG11:
return GL_COMPRESSED_RG11_EAC;
case SG_PIXELFORMAT_EAC_RG11SN:
return GL_COMPRESSED_SIGNED_RG11_EAC;
case SG_PIXELFORMAT_ASTC_4x4_RGBA:
return GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
case SG_PIXELFORMAT_ASTC_4x4_SRGBA:
return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
default:
SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_teximage_internal_format(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8: return GL_R8;
case SG_PIXELFORMAT_R8SN: return GL_R8_SNORM;
case SG_PIXELFORMAT_R8UI: return GL_R8UI;
case SG_PIXELFORMAT_R8SI: return GL_R8I;
#if !defined(SOKOL_GLES3)
case SG_PIXELFORMAT_R16: return GL_R16;
case SG_PIXELFORMAT_R16SN: return GL_R16_SNORM;
#endif
case SG_PIXELFORMAT_R16UI: return GL_R16UI;
case SG_PIXELFORMAT_R16SI: return GL_R16I;
case SG_PIXELFORMAT_R16F: return GL_R16F;
case SG_PIXELFORMAT_RG8: return GL_RG8;
case SG_PIXELFORMAT_RG8SN: return GL_RG8_SNORM;
case SG_PIXELFORMAT_RG8UI: return GL_RG8UI;
case SG_PIXELFORMAT_RG8SI: return GL_RG8I;
case SG_PIXELFORMAT_R32UI: return GL_R32UI;
case SG_PIXELFORMAT_R32SI: return GL_R32I;
case SG_PIXELFORMAT_R32F: return GL_R32F;
#if !defined(SOKOL_GLES3)
case SG_PIXELFORMAT_RG16: return GL_RG16;
case SG_PIXELFORMAT_RG16SN: return GL_RG16_SNORM;
#endif
case SG_PIXELFORMAT_RG16UI: return GL_RG16UI;
case SG_PIXELFORMAT_RG16SI: return GL_RG16I;
case SG_PIXELFORMAT_RG16F: return GL_RG16F;
case SG_PIXELFORMAT_RGBA8: return GL_RGBA8;
case SG_PIXELFORMAT_SRGB8A8: return GL_SRGB8_ALPHA8;
case SG_PIXELFORMAT_RGBA8SN: return GL_RGBA8_SNORM;
case SG_PIXELFORMAT_RGBA8UI: return GL_RGBA8UI;
case SG_PIXELFORMAT_RGBA8SI: return GL_RGBA8I;
case SG_PIXELFORMAT_RGB10A2: return GL_RGB10_A2;
case SG_PIXELFORMAT_RG11B10F: return GL_R11F_G11F_B10F;
case SG_PIXELFORMAT_RGB9E5: return GL_RGB9_E5;
case SG_PIXELFORMAT_RG32UI: return GL_RG32UI;
case SG_PIXELFORMAT_RG32SI: return GL_RG32I;
case SG_PIXELFORMAT_RG32F: return GL_RG32F;
#if !defined(SOKOL_GLES3)
case SG_PIXELFORMAT_RGBA16: return GL_RGBA16;
case SG_PIXELFORMAT_RGBA16SN: return GL_RGBA16_SNORM;
#endif
case SG_PIXELFORMAT_RGBA16UI: return GL_RGBA16UI;
case SG_PIXELFORMAT_RGBA16SI: return GL_RGBA16I;
case SG_PIXELFORMAT_RGBA16F: return GL_RGBA16F;
case SG_PIXELFORMAT_RGBA32UI: return GL_RGBA32UI;
case SG_PIXELFORMAT_RGBA32SI: return GL_RGBA32I;
case SG_PIXELFORMAT_RGBA32F: return GL_RGBA32F;
case SG_PIXELFORMAT_DEPTH: return GL_DEPTH_COMPONENT32F;
case SG_PIXELFORMAT_DEPTH_STENCIL: return GL_DEPTH24_STENCIL8;
case SG_PIXELFORMAT_BC1_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
case SG_PIXELFORMAT_BC2_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
case SG_PIXELFORMAT_BC3_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
case SG_PIXELFORMAT_BC3_SRGBA: return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
case SG_PIXELFORMAT_BC4_R: return GL_COMPRESSED_RED_RGTC1;
case SG_PIXELFORMAT_BC4_RSN: return GL_COMPRESSED_SIGNED_RED_RGTC1;
case SG_PIXELFORMAT_BC5_RG: return GL_COMPRESSED_RED_GREEN_RGTC2;
case SG_PIXELFORMAT_BC5_RGSN: return GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2;
case SG_PIXELFORMAT_BC6H_RGBF: return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB;
case SG_PIXELFORMAT_BC6H_RGBUF: return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB;
case SG_PIXELFORMAT_BC7_RGBA: return GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
case SG_PIXELFORMAT_BC7_SRGBA: return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB;
case SG_PIXELFORMAT_PVRTC_RGB_2BPP: return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGB_4BPP: return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: return GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
case SG_PIXELFORMAT_ETC2_RGB8: return GL_COMPRESSED_RGB8_ETC2;
case SG_PIXELFORMAT_ETC2_SRGB8: return GL_COMPRESSED_SRGB8_ETC2;
case SG_PIXELFORMAT_ETC2_RGB8A1: return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
case SG_PIXELFORMAT_ETC2_RGBA8: return GL_COMPRESSED_RGBA8_ETC2_EAC;
case SG_PIXELFORMAT_ETC2_SRGB8A8: return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
case SG_PIXELFORMAT_EAC_R11: return GL_COMPRESSED_R11_EAC;
case SG_PIXELFORMAT_EAC_R11SN: return GL_COMPRESSED_SIGNED_R11_EAC;
case SG_PIXELFORMAT_EAC_RG11: return GL_COMPRESSED_RG11_EAC;
case SG_PIXELFORMAT_EAC_RG11SN: return GL_COMPRESSED_SIGNED_RG11_EAC;
case SG_PIXELFORMAT_ASTC_4x4_RGBA: return GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
case SG_PIXELFORMAT_ASTC_4x4_SRGBA: return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_cubeface_target(int face_index) {
switch (face_index) {
case 0: return GL_TEXTURE_CUBE_MAP_POSITIVE_X;
case 1: return GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
case 2: return GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
case 3: return GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
case 4: return GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
case 5: return GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
default: SOKOL_UNREACHABLE; return 0;
}
}
// see: https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml
_SOKOL_PRIVATE void _sg_gl_init_pixelformats(bool has_bgra) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8SI]);
#if !defined(SOKOL_GLES3)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32SI]);
#if !defined(SOKOL_GLES3)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_SRGB8A8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8SI]);
if (has_bgra) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_BGRA8]);
}
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB10A2]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG11B10F]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGB9E5]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32SI]);
#if !defined(SOKOL_GLES3)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16SI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]);
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH]);
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL]);
}
// FIXME: OES_half_float_blend
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_half_float(bool has_colorbuffer_half_float) {
if (has_colorbuffer_half_float) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
} else {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R16F]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG16F]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
}
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_float(bool has_colorbuffer_float, bool has_texture_float_linear, bool has_float_blend) {
if (has_texture_float_linear) {
if (has_colorbuffer_float) {
if (has_float_blend) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
} else {
_sg_pixelformat_sfrm(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_sfrm(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_sfrm(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
} else {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
} else {
if (has_colorbuffer_float) {
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
} else {
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
}
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_s3tc(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC1_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC2_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_SRGBA]);
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_rgtc(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_R]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_RSN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RG]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RGSN]);
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_bptc(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBUF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_SRGBA]);
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_pvrtc(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_2BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_4BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_2BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_4BPP]);
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_etc2(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8A1]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGBA8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8A8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_R11]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_R11SN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_RG11]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_RG11SN]);
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_astc(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_SRGBA]);
}
_SOKOL_PRIVATE void _sg_gl_init_limits(void) {
_SG_GL_CHECK_ERROR();
GLint gl_int;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.limits.max_image_size_2d = gl_int;
_sg.limits.max_image_size_array = gl_int;
glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.limits.max_image_size_cube = gl_int;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &gl_int);
_SG_GL_CHECK_ERROR();
if (gl_int > SG_MAX_VERTEX_ATTRIBUTES) {
gl_int = SG_MAX_VERTEX_ATTRIBUTES;
}
_sg.limits.max_vertex_attrs = gl_int;
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.limits.gl_max_vertex_uniform_components = gl_int;
glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.limits.max_image_size_3d = gl_int;
glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.limits.max_image_array_layers = gl_int;
if (_sg.gl.ext_anisotropic) {
glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.gl.max_anisotropy = gl_int;
} else {
_sg.gl.max_anisotropy = 1;
}
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.limits.gl_max_combined_texture_image_units = gl_int;
}
#if defined(SOKOL_GLCORE)
_SOKOL_PRIVATE void _sg_gl_init_caps_glcore(void) {
_sg.backend = SG_BACKEND_GLCORE;
GLint major_version = 0;
GLint minor_version = 0;
glGetIntegerv(GL_MAJOR_VERSION, &major_version);
glGetIntegerv(GL_MINOR_VERSION, &minor_version);
const int version = major_version * 100 + minor_version * 10;
_sg.features.origin_top_left = false;
_sg.features.image_clamp_to_border = true;
_sg.features.mrt_independent_blend_state = false;
_sg.features.mrt_independent_write_mask = true;
_sg.features.storage_buffer = version >= 430;
// scan extensions
bool has_s3tc = false; // BC1..BC3
bool has_rgtc = false; // BC4 and BC5
bool has_bptc = false; // BC6H and BC7
bool has_pvrtc = false;
bool has_etc2 = false;
bool has_astc = false;
GLint num_ext = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &num_ext);
for (int i = 0; i < num_ext; i++) {
const char* ext = (const char*) glGetStringi(GL_EXTENSIONS, (GLuint)i);
if (ext) {
if (strstr(ext, "_texture_compression_s3tc")) {
has_s3tc = true;
} else if (strstr(ext, "_texture_compression_rgtc")) {
has_rgtc = true;
} else if (strstr(ext, "_texture_compression_bptc")) {
has_bptc = true;
} else if (strstr(ext, "_texture_compression_pvrtc")) {
has_pvrtc = true;
} else if (strstr(ext, "_ES3_compatibility")) {
has_etc2 = true;
} else if (strstr(ext, "_texture_filter_anisotropic")) {
_sg.gl.ext_anisotropic = true;
} else if (strstr(ext, "_texture_compression_astc_ldr")) {
has_astc = true;
}
}
}
// limits
_sg_gl_init_limits();
// pixel formats
const bool has_bgra = false; // not a bug
const bool has_colorbuffer_float = true;
const bool has_colorbuffer_half_float = true;
const bool has_texture_float_linear = true; // FIXME???
const bool has_float_blend = true;
_sg_gl_init_pixelformats(has_bgra);
_sg_gl_init_pixelformats_float(has_colorbuffer_float, has_texture_float_linear, has_float_blend);
_sg_gl_init_pixelformats_half_float(has_colorbuffer_half_float);
if (has_s3tc) {
_sg_gl_init_pixelformats_s3tc();
}
if (has_rgtc) {
_sg_gl_init_pixelformats_rgtc();
}
if (has_bptc) {
_sg_gl_init_pixelformats_bptc();
}
if (has_pvrtc) {
_sg_gl_init_pixelformats_pvrtc();
}
if (has_etc2) {
_sg_gl_init_pixelformats_etc2();
}
if (has_astc) {
_sg_gl_init_pixelformats_astc();
}
}
#endif
#if defined(SOKOL_GLES3)
_SOKOL_PRIVATE void _sg_gl_init_caps_gles3(void) {
_sg.backend = SG_BACKEND_GLES3;
_sg.features.origin_top_left = false;
_sg.features.image_clamp_to_border = false;
_sg.features.mrt_independent_blend_state = false;
_sg.features.mrt_independent_write_mask = false;
_sg.features.storage_buffer = false;
bool has_s3tc = false; // BC1..BC3
bool has_rgtc = false; // BC4 and BC5
bool has_bptc = false; // BC6H and BC7
bool has_pvrtc = false;
#if defined(__EMSCRIPTEN__)
bool has_etc2 = false;
#else
bool has_etc2 = true;
#endif
bool has_astc = false;
bool has_colorbuffer_float = false;
bool has_colorbuffer_half_float = false;
bool has_texture_float_linear = false;
bool has_float_blend = false;
GLint num_ext = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &num_ext);
for (int i = 0; i < num_ext; i++) {
const char* ext = (const char*) glGetStringi(GL_EXTENSIONS, (GLuint)i);
if (ext) {
if (strstr(ext, "_texture_compression_s3tc")) {
has_s3tc = true;
} else if (strstr(ext, "_compressed_texture_s3tc")) {
has_s3tc = true;
} else if (strstr(ext, "_texture_compression_rgtc")) {
has_rgtc = true;
} else if (strstr(ext, "_texture_compression_bptc")) {
has_bptc = true;
} else if (strstr(ext, "_texture_compression_pvrtc")) {
has_pvrtc = true;
} else if (strstr(ext, "_compressed_texture_pvrtc")) {
has_pvrtc = true;
} else if (strstr(ext, "_compressed_texture_etc")) {
has_etc2 = true;
} else if (strstr(ext, "_compressed_texture_astc")) {
has_astc = true;
} else if (strstr(ext, "_color_buffer_float")) {
has_colorbuffer_float = true;
} else if (strstr(ext, "_color_buffer_half_float")) {
has_colorbuffer_half_float = true;
} else if (strstr(ext, "_texture_float_linear")) {
has_texture_float_linear = true;
} else if (strstr(ext, "_float_blend")) {
has_float_blend = true;
} else if (strstr(ext, "_texture_filter_anisotropic")) {
_sg.gl.ext_anisotropic = true;
}
}
}
/* on WebGL2, color_buffer_float also includes 16-bit formats
see: https://developer.mozilla.org/en-US/docs/Web/API/EXT_color_buffer_float
*/
#if defined(__EMSCRIPTEN__)
if (!has_colorbuffer_half_float && has_colorbuffer_float) {
has_colorbuffer_half_float = has_colorbuffer_float;
}
#endif
// limits
_sg_gl_init_limits();
// pixel formats
const bool has_bgra = false; // not a bug
_sg_gl_init_pixelformats(has_bgra);
_sg_gl_init_pixelformats_float(has_colorbuffer_float, has_texture_float_linear, has_float_blend);
_sg_gl_init_pixelformats_half_float(has_colorbuffer_half_float);
if (has_s3tc) {
_sg_gl_init_pixelformats_s3tc();
}
if (has_rgtc) {
_sg_gl_init_pixelformats_rgtc();
}
if (has_bptc) {
_sg_gl_init_pixelformats_bptc();
}
if (has_pvrtc) {
_sg_gl_init_pixelformats_pvrtc();
}
if (has_etc2) {
_sg_gl_init_pixelformats_etc2();
}
if (has_astc) {
_sg_gl_init_pixelformats_astc();
}
}
#endif
//-- state cache implementation ------------------------------------------------
_SOKOL_PRIVATE GLuint _sg_gl_storagebuffer_bind_index(int stage, int slot) {
SOKOL_ASSERT((stage >= 0) && (stage < SG_NUM_SHADER_STAGES));
SOKOL_ASSERT((slot >= 0) && (slot < SG_MAX_SHADERSTAGE_STORAGEBUFFERS));
return (GLuint) (stage * _SG_GL_STORAGEBUFFER_STAGE_INDEX_PITCH + slot);
}
_SOKOL_PRIVATE void _sg_gl_cache_clear_buffer_bindings(bool force) {
if (force || (_sg.gl.cache.vertex_buffer != 0)) {
glBindBuffer(GL_ARRAY_BUFFER, 0);
_sg.gl.cache.vertex_buffer = 0;
_sg_stats_add(gl.num_bind_buffer, 1);
}
if (force || (_sg.gl.cache.index_buffer != 0)) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
_sg.gl.cache.index_buffer = 0;
_sg_stats_add(gl.num_bind_buffer, 1);
}
if (force || (_sg.gl.cache.storage_buffer != 0)) {
if (_sg.features.storage_buffer) {
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
_sg.gl.cache.storage_buffer = 0;
_sg_stats_add(gl.num_bind_buffer, 1);
}
for (int stage = 0; stage < SG_NUM_SHADER_STAGES; stage++) {
for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++) {
if (force || (_sg.gl.cache.stage_storage_buffers[stage][i] != 0)) {
const GLuint bind_index = _sg_gl_storagebuffer_bind_index(stage, i);
if (_sg.features.storage_buffer) {
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, bind_index, 0);
}
_sg.gl.cache.stage_storage_buffers[stage][i] = 0;
_sg_stats_add(gl.num_bind_buffer, 1);
}
}
}
}
_SOKOL_PRIVATE void _sg_gl_cache_bind_buffer(GLenum target, GLuint buffer) {
SOKOL_ASSERT((GL_ARRAY_BUFFER == target) || (GL_ELEMENT_ARRAY_BUFFER == target) || (GL_SHADER_STORAGE_BUFFER == target));
if (target == GL_ARRAY_BUFFER) {
if (_sg.gl.cache.vertex_buffer != buffer) {
_sg.gl.cache.vertex_buffer = buffer;
glBindBuffer(target, buffer);
_sg_stats_add(gl.num_bind_buffer, 1);
}
} else if (target == GL_ELEMENT_ARRAY_BUFFER) {
if (_sg.gl.cache.index_buffer != buffer) {
_sg.gl.cache.index_buffer = buffer;
glBindBuffer(target, buffer);
_sg_stats_add(gl.num_bind_buffer, 1);
}
} else if (target == GL_SHADER_STORAGE_BUFFER) {
if (_sg.gl.cache.storage_buffer != buffer) {
_sg.gl.cache.storage_buffer = buffer;
if (_sg.features.storage_buffer) {
glBindBuffer(target, buffer);
}
_sg_stats_add(gl.num_bind_buffer, 1);
}
} else {
SOKOL_UNREACHABLE;
}
}
_SOKOL_PRIVATE void _sg_gl_cache_bind_storage_buffer(int stage, int slot, GLuint buffer) {
SOKOL_ASSERT((stage >= 0) && (stage < SG_NUM_SHADER_STAGES));
SOKOL_ASSERT((slot >= 0) && (slot < SG_MAX_SHADERSTAGE_STORAGEBUFFERS));
if (_sg.gl.cache.stage_storage_buffers[stage][slot] != buffer) {
_sg.gl.cache.stage_storage_buffers[stage][slot] = buffer;
_sg.gl.cache.storage_buffer = buffer; // not a bug
GLuint bind_index = _sg_gl_storagebuffer_bind_index(stage, slot);
if (_sg.features.storage_buffer) {
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, bind_index, buffer);
}
_sg_stats_add(gl.num_bind_buffer, 1);
}
}
_SOKOL_PRIVATE void _sg_gl_cache_store_buffer_binding(GLenum target) {
if (target == GL_ARRAY_BUFFER) {
_sg.gl.cache.stored_vertex_buffer = _sg.gl.cache.vertex_buffer;
} else if (target == GL_ELEMENT_ARRAY_BUFFER) {
_sg.gl.cache.stored_index_buffer = _sg.gl.cache.index_buffer;
} else if (target == GL_SHADER_STORAGE_BUFFER) {
_sg.gl.cache.stored_storage_buffer = _sg.gl.cache.storage_buffer;
} else {
SOKOL_UNREACHABLE;
}
}
_SOKOL_PRIVATE void _sg_gl_cache_restore_buffer_binding(GLenum target) {
if (target == GL_ARRAY_BUFFER) {
if (_sg.gl.cache.stored_vertex_buffer != 0) {
// we only care about restoring valid ids
_sg_gl_cache_bind_buffer(target, _sg.gl.cache.stored_vertex_buffer);
_sg.gl.cache.stored_vertex_buffer = 0;
}
} else if (target == GL_ELEMENT_ARRAY_BUFFER) {
if (_sg.gl.cache.stored_index_buffer != 0) {
// we only care about restoring valid ids
_sg_gl_cache_bind_buffer(target, _sg.gl.cache.stored_index_buffer);
_sg.gl.cache.stored_index_buffer = 0;
}
} else if (target == GL_SHADER_STORAGE_BUFFER) {
if (_sg.gl.cache.stored_storage_buffer != 0) {
// we only care about restoring valid ids
_sg_gl_cache_bind_buffer(target, _sg.gl.cache.stored_storage_buffer);
_sg.gl.cache.stored_storage_buffer = 0;
}
} else {
SOKOL_UNREACHABLE;
}
}
// called when _sg_gl_discard_buffer()
_SOKOL_PRIVATE void _sg_gl_cache_invalidate_buffer(GLuint buf) {
if (buf == _sg.gl.cache.vertex_buffer) {
_sg.gl.cache.vertex_buffer = 0;
glBindBuffer(GL_ARRAY_BUFFER, 0);
_sg_stats_add(gl.num_bind_buffer, 1);
}
if (buf == _sg.gl.cache.index_buffer) {
_sg.gl.cache.index_buffer = 0;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
_sg_stats_add(gl.num_bind_buffer, 1);
}
if (buf == _sg.gl.cache.storage_buffer) {
_sg.gl.cache.storage_buffer = 0;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
_sg_stats_add(gl.num_bind_buffer, 1);
}
for (int stage = 0; stage < SG_NUM_SHADER_STAGES; stage++) {
for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++) {
if (buf == _sg.gl.cache.stage_storage_buffers[stage][i]) {
_sg.gl.cache.stage_storage_buffers[stage][i] = 0;
_sg.gl.cache.storage_buffer = 0; // not a bug!
const GLuint bind_index = _sg_gl_storagebuffer_bind_index(stage, i);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, bind_index, 0);
_sg_stats_add(gl.num_bind_buffer, 1);
}
}
}
if (buf == _sg.gl.cache.stored_vertex_buffer) {
_sg.gl.cache.stored_vertex_buffer = 0;
}
if (buf == _sg.gl.cache.stored_index_buffer) {
_sg.gl.cache.stored_index_buffer = 0;
}
if (buf == _sg.gl.cache.stored_storage_buffer) {
_sg.gl.cache.stored_storage_buffer = 0;
}
for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) {
if (buf == _sg.gl.cache.attrs[i].gl_vbuf) {
_sg.gl.cache.attrs[i].gl_vbuf = 0;
}
}
}
_SOKOL_PRIVATE void _sg_gl_cache_active_texture(GLenum texture) {
_SG_GL_CHECK_ERROR();
if (_sg.gl.cache.cur_active_texture != texture) {
_sg.gl.cache.cur_active_texture = texture;
glActiveTexture(texture);
_sg_stats_add(gl.num_active_texture, 1);
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE void _sg_gl_cache_clear_texture_sampler_bindings(bool force) {
_SG_GL_CHECK_ERROR();
for (int i = 0; (i < _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE) && (i < _sg.limits.gl_max_combined_texture_image_units); i++) {
if (force || (_sg.gl.cache.texture_samplers[i].texture != 0)) {
GLenum gl_texture_unit = (GLenum) (GL_TEXTURE0 + i);
glActiveTexture(gl_texture_unit);
_sg_stats_add(gl.num_active_texture, 1);
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
glBindTexture(GL_TEXTURE_3D, 0);
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
_sg_stats_add(gl.num_bind_texture, 4);
glBindSampler((GLuint)i, 0);
_sg_stats_add(gl.num_bind_sampler, 1);
_sg.gl.cache.texture_samplers[i].target = 0;
_sg.gl.cache.texture_samplers[i].texture = 0;
_sg.gl.cache.texture_samplers[i].sampler = 0;
_sg.gl.cache.cur_active_texture = gl_texture_unit;
}
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE void _sg_gl_cache_bind_texture_sampler(int slot_index, GLenum target, GLuint texture, GLuint sampler) {
/* it's valid to call this function with target=0 and/or texture=0
target=0 will unbind the previous binding, texture=0 will clear
the new binding
*/
SOKOL_ASSERT((slot_index >= 0) && (slot_index < _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE));
if (slot_index >= _sg.limits.gl_max_combined_texture_image_units) {
return;
}
_SG_GL_CHECK_ERROR();
_sg_gl_cache_texture_sampler_bind_slot* slot = &_sg.gl.cache.texture_samplers[slot_index];
if ((slot->target != target) || (slot->texture != texture) || (slot->sampler != sampler)) {
_sg_gl_cache_active_texture((GLenum)(GL_TEXTURE0 + slot_index));
// if the target has changed, clear the previous binding on that target
if ((target != slot->target) && (slot->target != 0)) {
glBindTexture(slot->target, 0);
_SG_GL_CHECK_ERROR();
_sg_stats_add(gl.num_bind_texture, 1);
}
// apply new binding (can be 0 to unbind)
if (target != 0) {
glBindTexture(target, texture);
_SG_GL_CHECK_ERROR();
_sg_stats_add(gl.num_bind_texture, 1);
}
// apply new sampler (can be 0 to unbind)
glBindSampler((GLuint)slot_index, sampler);
_SG_GL_CHECK_ERROR();
_sg_stats_add(gl.num_bind_sampler, 1);
slot->target = target;
slot->texture = texture;
slot->sampler = sampler;
}
}
_SOKOL_PRIVATE void _sg_gl_cache_store_texture_sampler_binding(int slot_index) {
SOKOL_ASSERT((slot_index >= 0) && (slot_index < _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE));
_sg.gl.cache.stored_texture_sampler = _sg.gl.cache.texture_samplers[slot_index];
}
_SOKOL_PRIVATE void _sg_gl_cache_restore_texture_sampler_binding(int slot_index) {
SOKOL_ASSERT((slot_index >= 0) && (slot_index < _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE));
_sg_gl_cache_texture_sampler_bind_slot* slot = &_sg.gl.cache.stored_texture_sampler;
if (slot->texture != 0) {
// we only care about restoring valid ids
SOKOL_ASSERT(slot->target != 0);
_sg_gl_cache_bind_texture_sampler(slot_index, slot->target, slot->texture, slot->sampler);
slot->target = 0;
slot->texture = 0;
slot->sampler = 0;
}
}
// called from _sg_gl_discard_texture() and _sg_gl_discard_sampler()
_SOKOL_PRIVATE void _sg_gl_cache_invalidate_texture_sampler(GLuint tex, GLuint smp) {
_SG_GL_CHECK_ERROR();
for (int i = 0; i < _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE; i++) {
_sg_gl_cache_texture_sampler_bind_slot* slot = &_sg.gl.cache.texture_samplers[i];
if ((0 != slot->target) && ((tex == slot->texture) || (smp == slot->sampler))) {
_sg_gl_cache_active_texture((GLenum)(GL_TEXTURE0 + i));
glBindTexture(slot->target, 0);
_SG_GL_CHECK_ERROR();
_sg_stats_add(gl.num_bind_texture, 1);
glBindSampler((GLuint)i, 0);
_SG_GL_CHECK_ERROR();
_sg_stats_add(gl.num_bind_sampler, 1);
slot->target = 0;
slot->texture = 0;
slot->sampler = 0;
}
}
if ((tex == _sg.gl.cache.stored_texture_sampler.texture) || (smp == _sg.gl.cache.stored_texture_sampler.sampler)) {
_sg.gl.cache.stored_texture_sampler.target = 0;
_sg.gl.cache.stored_texture_sampler.texture = 0;
_sg.gl.cache.stored_texture_sampler.sampler = 0;
}
}
// called from _sg_gl_discard_shader()
_SOKOL_PRIVATE void _sg_gl_cache_invalidate_program(GLuint prog) {
if (prog == _sg.gl.cache.prog) {
_sg.gl.cache.prog = 0;
glUseProgram(0);
_sg_stats_add(gl.num_use_program, 1);
}
}
// called from _sg_gl_discard_pipeline()
_SOKOL_PRIVATE void _sg_gl_cache_invalidate_pipeline(_sg_pipeline_t* pip) {
if (pip == _sg.gl.cache.cur_pipeline) {
_sg.gl.cache.cur_pipeline = 0;
_sg.gl.cache.cur_pipeline_id.id = SG_INVALID_ID;
}
}
_SOKOL_PRIVATE void _sg_gl_reset_state_cache(void) {
_SG_GL_CHECK_ERROR();
glBindVertexArray(_sg.gl.vao);
_SG_GL_CHECK_ERROR();
_sg_clear(&_sg.gl.cache, sizeof(_sg.gl.cache));
_sg_gl_cache_clear_buffer_bindings(true);
_SG_GL_CHECK_ERROR();
_sg_gl_cache_clear_texture_sampler_bindings(true);
_SG_GL_CHECK_ERROR();
for (int i = 0; i < _sg.limits.max_vertex_attrs; i++) {
_sg_gl_attr_t* attr = &_sg.gl.cache.attrs[i].gl_attr;
attr->vb_index = -1;
attr->divisor = -1;
glDisableVertexAttribArray((GLuint)i);
_SG_GL_CHECK_ERROR();
_sg_stats_add(gl.num_disable_vertex_attrib_array, 1);
}
_sg.gl.cache.cur_primitive_type = GL_TRIANGLES;
// shader program
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&_sg.gl.cache.prog);
_SG_GL_CHECK_ERROR();
// depth and stencil state
_sg.gl.cache.depth.compare = SG_COMPAREFUNC_ALWAYS;
_sg.gl.cache.stencil.front.compare = SG_COMPAREFUNC_ALWAYS;
_sg.gl.cache.stencil.front.fail_op = SG_STENCILOP_KEEP;
_sg.gl.cache.stencil.front.depth_fail_op = SG_STENCILOP_KEEP;
_sg.gl.cache.stencil.front.pass_op = SG_STENCILOP_KEEP;
_sg.gl.cache.stencil.back.compare = SG_COMPAREFUNC_ALWAYS;
_sg.gl.cache.stencil.back.fail_op = SG_STENCILOP_KEEP;
_sg.gl.cache.stencil.back.depth_fail_op = SG_STENCILOP_KEEP;
_sg.gl.cache.stencil.back.pass_op = SG_STENCILOP_KEEP;
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
glDepthMask(GL_FALSE);
glDisable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 0, 0);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glStencilMask(0);
_sg_stats_add(gl.num_render_state, 7);
// blend state
_sg.gl.cache.blend.src_factor_rgb = SG_BLENDFACTOR_ONE;
_sg.gl.cache.blend.dst_factor_rgb = SG_BLENDFACTOR_ZERO;
_sg.gl.cache.blend.op_rgb = SG_BLENDOP_ADD;
_sg.gl.cache.blend.src_factor_alpha = SG_BLENDFACTOR_ONE;
_sg.gl.cache.blend.dst_factor_alpha = SG_BLENDFACTOR_ZERO;
_sg.gl.cache.blend.op_alpha = SG_BLENDOP_ADD;
glDisable(GL_BLEND);
glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO);
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
glBlendColor(0.0f, 0.0f, 0.0f, 0.0f);
_sg_stats_add(gl.num_render_state, 4);
// standalone state
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
_sg.gl.cache.color_write_mask[i] = SG_COLORMASK_RGBA;
}
_sg.gl.cache.cull_mode = SG_CULLMODE_NONE;
_sg.gl.cache.face_winding = SG_FACEWINDING_CW;
_sg.gl.cache.sample_count = 1;
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glPolygonOffset(0.0f, 0.0f);
glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_CULL_FACE);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_SCISSOR_TEST);
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
glEnable(GL_DITHER);
glDisable(GL_POLYGON_OFFSET_FILL);
_sg_stats_add(gl.num_render_state, 10);
#if defined(SOKOL_GLCORE)
glEnable(GL_MULTISAMPLE);
glEnable(GL_PROGRAM_POINT_SIZE);
_sg_stats_add(gl.num_render_state, 2);
#endif
}
_SOKOL_PRIVATE void _sg_gl_setup_backend(const sg_desc* desc) {
_SOKOL_UNUSED(desc);
// assumes that _sg.gl is already zero-initialized
_sg.gl.valid = true;
#if defined(_SOKOL_USE_WIN32_GL_LOADER)
_sg_gl_load_opengl();
#endif
// clear initial GL error state
#if defined(SOKOL_DEBUG)
while (glGetError() != GL_NO_ERROR);
#endif
#if defined(SOKOL_GLCORE)
_sg_gl_init_caps_glcore();
#elif defined(SOKOL_GLES3)
_sg_gl_init_caps_gles3();
#endif
glGenVertexArrays(1, &_sg.gl.vao);
glBindVertexArray(_sg.gl.vao);
_SG_GL_CHECK_ERROR();
// incoming texture data is generally expected to be packed tightly
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
#if defined(SOKOL_GLCORE)
// enable seamless cubemap sampling (only desktop GL)
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
#endif
_sg_gl_reset_state_cache();
}
_SOKOL_PRIVATE void _sg_gl_discard_backend(void) {
SOKOL_ASSERT(_sg.gl.valid);
if (_sg.gl.vao) {
glDeleteVertexArrays(1, &_sg.gl.vao);
}
#if defined(_SOKOL_USE_WIN32_GL_LOADER)
_sg_gl_unload_opengl();
#endif
_sg.gl.valid = false;
}
//-- GL backend resource creation and destruction ------------------------------
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && desc);
_SG_GL_CHECK_ERROR();
buf->gl.injected = (0 != desc->gl_buffers[0]);
const GLenum gl_target = _sg_gl_buffer_target(buf->cmn.type);
const GLenum gl_usage = _sg_gl_usage(buf->cmn.usage);
for (int slot = 0; slot < buf->cmn.num_slots; slot++) {
GLuint gl_buf = 0;
if (buf->gl.injected) {
SOKOL_ASSERT(desc->gl_buffers[slot]);
gl_buf = desc->gl_buffers[slot];
} else {
glGenBuffers(1, &gl_buf);
SOKOL_ASSERT(gl_buf);
_sg_gl_cache_store_buffer_binding(gl_target);
_sg_gl_cache_bind_buffer(gl_target, gl_buf);
glBufferData(gl_target, buf->cmn.size, 0, gl_usage);
if (buf->cmn.usage == SG_USAGE_IMMUTABLE) {
SOKOL_ASSERT(desc->data.ptr);
glBufferSubData(gl_target, 0, buf->cmn.size, desc->data.ptr);
}
_sg_gl_cache_restore_buffer_binding(gl_target);
}
buf->gl.buf[slot] = gl_buf;
}
_SG_GL_CHECK_ERROR();
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_discard_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
_SG_GL_CHECK_ERROR();
for (int slot = 0; slot < buf->cmn.num_slots; slot++) {
if (buf->gl.buf[slot]) {
_sg_gl_cache_invalidate_buffer(buf->gl.buf[slot]);
if (!buf->gl.injected) {
glDeleteBuffers(1, &buf->gl.buf[slot]);
}
}
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE bool _sg_gl_supported_texture_format(sg_pixel_format fmt) {
const int fmt_index = (int) fmt;
SOKOL_ASSERT((fmt_index > SG_PIXELFORMAT_NONE) && (fmt_index < _SG_PIXELFORMAT_NUM));
return _sg.formats[fmt_index].sample;
}
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && desc);
_SG_GL_CHECK_ERROR();
img->gl.injected = (0 != desc->gl_textures[0]);
// check if texture format is support
if (!_sg_gl_supported_texture_format(img->cmn.pixel_format)) {
_SG_ERROR(GL_TEXTURE_FORMAT_NOT_SUPPORTED);
return SG_RESOURCESTATE_FAILED;
}
const GLenum gl_internal_format = _sg_gl_teximage_internal_format(img->cmn.pixel_format);
// if this is a MSAA render target, a render buffer object will be created instead of a regulat texture
// (since GLES3 has no multisampled texture objects)
if (img->cmn.render_target && (img->cmn.sample_count > 1)) {
glGenRenderbuffers(1, &img->gl.msaa_render_buffer);
glBindRenderbuffer(GL_RENDERBUFFER, img->gl.msaa_render_buffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, img->cmn.sample_count, gl_internal_format, img->cmn.width, img->cmn.height);
} else if (img->gl.injected) {
img->gl.target = _sg_gl_texture_target(img->cmn.type);
// inject externally GL textures
for (int slot = 0; slot < img->cmn.num_slots; slot++) {
SOKOL_ASSERT(desc->gl_textures[slot]);
img->gl.tex[slot] = desc->gl_textures[slot];
}
if (desc->gl_texture_target) {
img->gl.target = (GLenum)desc->gl_texture_target;
}
} else {
// create our own GL texture(s)
img->gl.target = _sg_gl_texture_target(img->cmn.type);
const GLenum gl_format = _sg_gl_teximage_format(img->cmn.pixel_format);
const bool is_compressed = _sg_is_compressed_pixel_format(img->cmn.pixel_format);
for (int slot = 0; slot < img->cmn.num_slots; slot++) {
glGenTextures(1, &img->gl.tex[slot]);
SOKOL_ASSERT(img->gl.tex[slot]);
_sg_gl_cache_store_texture_sampler_binding(0);
_sg_gl_cache_bind_texture_sampler(0, img->gl.target, img->gl.tex[slot], 0);
glTexParameteri(img->gl.target, GL_TEXTURE_MAX_LEVEL, img->cmn.num_mipmaps - 1);
// NOTE: workaround for https://issues.chromium.org/issues/355605685
// FIXME: on GLES3 and GL 4.3 (e.g. not macOS) the texture initialization
// should be rewritten to use glTexStorage + glTexSubImage
bool tex_storage_allocated = false;
#if defined(__EMSCRIPTEN__)
if (desc->data.subimage[0][0].ptr == 0) {
tex_storage_allocated = true;
if ((SG_IMAGETYPE_2D == img->cmn.type) || (SG_IMAGETYPE_CUBE == img->cmn.type)) {
glTexStorage2D(img->gl.target, img->cmn.num_mipmaps, gl_internal_format, img->cmn.width, img->cmn.height);
} else if ((SG_IMAGETYPE_3D == img->cmn.type) || (SG_IMAGETYPE_ARRAY == img->cmn.type)) {
glTexStorage3D(img->gl.target, img->cmn.num_mipmaps, gl_internal_format, img->cmn.width, img->cmn.height, img->cmn.num_slices);
}
}
#endif
if (!tex_storage_allocated) {
const int num_faces = img->cmn.type == SG_IMAGETYPE_CUBE ? 6 : 1;
int data_index = 0;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++, data_index++) {
GLenum gl_img_target = img->gl.target;
if (SG_IMAGETYPE_CUBE == img->cmn.type) {
gl_img_target = _sg_gl_cubeface_target(face_index);
}
const GLvoid* data_ptr = desc->data.subimage[face_index][mip_index].ptr;
const int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index);
const int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index);
if ((SG_IMAGETYPE_2D == img->cmn.type) || (SG_IMAGETYPE_CUBE == img->cmn.type)) {
if (is_compressed) {
const GLsizei data_size = (GLsizei) desc->data.subimage[face_index][mip_index].size;
glCompressedTexImage2D(gl_img_target, mip_index, gl_internal_format,
mip_width, mip_height, 0, data_size, data_ptr);
} else {
const GLenum gl_type = _sg_gl_teximage_type(img->cmn.pixel_format);
glTexImage2D(gl_img_target, mip_index, (GLint)gl_internal_format,
mip_width, mip_height, 0, gl_format, gl_type, data_ptr);
}
} else if ((SG_IMAGETYPE_3D == img->cmn.type) || (SG_IMAGETYPE_ARRAY == img->cmn.type)) {
int mip_depth = img->cmn.num_slices;
if (SG_IMAGETYPE_3D == img->cmn.type) {
mip_depth = _sg_miplevel_dim(mip_depth, mip_index);
}
if (is_compressed) {
const GLsizei data_size = (GLsizei) desc->data.subimage[face_index][mip_index].size;
glCompressedTexImage3D(gl_img_target, mip_index, gl_internal_format,
mip_width, mip_height, mip_depth, 0, data_size, data_ptr);
} else {
const GLenum gl_type = _sg_gl_teximage_type(img->cmn.pixel_format);
glTexImage3D(gl_img_target, mip_index, (GLint)gl_internal_format,
mip_width, mip_height, mip_depth, 0, gl_format, gl_type, data_ptr);
}
}
}
}
}
_sg_gl_cache_restore_texture_sampler_binding(0);
}
}
_SG_GL_CHECK_ERROR();
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_discard_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
_SG_GL_CHECK_ERROR();
for (int slot = 0; slot < img->cmn.num_slots; slot++) {
if (img->gl.tex[slot]) {
_sg_gl_cache_invalidate_texture_sampler(img->gl.tex[slot], 0);
if (!img->gl.injected) {
glDeleteTextures(1, &img->gl.tex[slot]);
}
}
}
if (img->gl.msaa_render_buffer) {
glDeleteRenderbuffers(1, &img->gl.msaa_render_buffer);
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) {
SOKOL_ASSERT(smp && desc);
_SG_GL_CHECK_ERROR();
smp->gl.injected = (0 != desc->gl_sampler);
if (smp->gl.injected) {
smp->gl.smp = (GLuint) desc->gl_sampler;
} else {
glGenSamplers(1, &smp->gl.smp);
SOKOL_ASSERT(smp->gl.smp);
const GLenum gl_min_filter = _sg_gl_min_filter(smp->cmn.min_filter, smp->cmn.mipmap_filter);
const GLenum gl_mag_filter = _sg_gl_mag_filter(smp->cmn.mag_filter);
glSamplerParameteri(smp->gl.smp, GL_TEXTURE_MIN_FILTER, (GLint)gl_min_filter);
glSamplerParameteri(smp->gl.smp, GL_TEXTURE_MAG_FILTER, (GLint)gl_mag_filter);
// GL spec has strange defaults for mipmap min/max lod: -1000 to +1000
const float min_lod = _sg_clamp(desc->min_lod, 0.0f, 1000.0f);
const float max_lod = _sg_clamp(desc->max_lod, 0.0f, 1000.0f);
glSamplerParameterf(smp->gl.smp, GL_TEXTURE_MIN_LOD, min_lod);
glSamplerParameterf(smp->gl.smp, GL_TEXTURE_MAX_LOD, max_lod);
glSamplerParameteri(smp->gl.smp, GL_TEXTURE_WRAP_S, (GLint)_sg_gl_wrap(smp->cmn.wrap_u));
glSamplerParameteri(smp->gl.smp, GL_TEXTURE_WRAP_T, (GLint)_sg_gl_wrap(smp->cmn.wrap_v));
glSamplerParameteri(smp->gl.smp, GL_TEXTURE_WRAP_R, (GLint)_sg_gl_wrap(smp->cmn.wrap_w));
#if defined(SOKOL_GLCORE)
float border[4];
switch (smp->cmn.border_color) {
case SG_BORDERCOLOR_TRANSPARENT_BLACK:
border[0] = 0.0f; border[1] = 0.0f; border[2] = 0.0f; border[3] = 0.0f;
break;
case SG_BORDERCOLOR_OPAQUE_WHITE:
border[0] = 1.0f; border[1] = 1.0f; border[2] = 1.0f; border[3] = 1.0f;
break;
default:
border[0] = 0.0f; border[1] = 0.0f; border[2] = 0.0f; border[3] = 1.0f;
break;
}
glSamplerParameterfv(smp->gl.smp, GL_TEXTURE_BORDER_COLOR, border);
#endif
if (smp->cmn.compare != SG_COMPAREFUNC_NEVER) {
glSamplerParameteri(smp->gl.smp, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glSamplerParameteri(smp->gl.smp, GL_TEXTURE_COMPARE_FUNC, (GLint)_sg_gl_compare_func(smp->cmn.compare));
} else {
glSamplerParameteri(smp->gl.smp, GL_TEXTURE_COMPARE_MODE, GL_NONE);
}
if (_sg.gl.ext_anisotropic && (smp->cmn.max_anisotropy > 1)) {
GLint max_aniso = (GLint) smp->cmn.max_anisotropy;
if (max_aniso > _sg.gl.max_anisotropy) {
max_aniso = _sg.gl.max_anisotropy;
}
glSamplerParameteri(smp->gl.smp, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_aniso);
}
}
_SG_GL_CHECK_ERROR();
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_discard_sampler(_sg_sampler_t* smp) {
SOKOL_ASSERT(smp);
_SG_GL_CHECK_ERROR();
_sg_gl_cache_invalidate_texture_sampler(0, smp->gl.smp);
if (!smp->gl.injected) {
glDeleteSamplers(1, &smp->gl.smp);
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE GLuint _sg_gl_compile_shader(sg_shader_stage stage, const char* src) {
SOKOL_ASSERT(src);
_SG_GL_CHECK_ERROR();
GLuint gl_shd = glCreateShader(_sg_gl_shader_stage(stage));
glShaderSource(gl_shd, 1, &src, 0);
glCompileShader(gl_shd);
GLint compile_status = 0;
glGetShaderiv(gl_shd, GL_COMPILE_STATUS, &compile_status);
if (!compile_status) {
// compilation failed, log error and delete shader
GLint log_len = 0;
glGetShaderiv(gl_shd, GL_INFO_LOG_LENGTH, &log_len);
if (log_len > 0) {
GLchar* log_buf = (GLchar*) _sg_malloc((size_t)log_len);
glGetShaderInfoLog(gl_shd, log_len, &log_len, log_buf);
_SG_ERROR(GL_SHADER_COMPILATION_FAILED);
_SG_LOGMSG(GL_SHADER_COMPILATION_FAILED, log_buf);
_sg_free(log_buf);
}
glDeleteShader(gl_shd);
gl_shd = 0;
}
_SG_GL_CHECK_ERROR();
return gl_shd;
}
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && desc);
SOKOL_ASSERT(!shd->gl.prog);
_SG_GL_CHECK_ERROR();
// copy the optional vertex attribute names over
for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) {
_sg_strcpy(&shd->gl.attrs[i].name, desc->attrs[i].name);
}
GLuint gl_vs = _sg_gl_compile_shader(SG_SHADERSTAGE_VS, desc->vs.source);
GLuint gl_fs = _sg_gl_compile_shader(SG_SHADERSTAGE_FS, desc->fs.source);
if (!(gl_vs && gl_fs)) {
return SG_RESOURCESTATE_FAILED;
}
GLuint gl_prog = glCreateProgram();
glAttachShader(gl_prog, gl_vs);
glAttachShader(gl_prog, gl_fs);
glLinkProgram(gl_prog);
glDeleteShader(gl_vs);
glDeleteShader(gl_fs);
_SG_GL_CHECK_ERROR();
GLint link_status;
glGetProgramiv(gl_prog, GL_LINK_STATUS, &link_status);
if (!link_status) {
GLint log_len = 0;
glGetProgramiv(gl_prog, GL_INFO_LOG_LENGTH, &log_len);
if (log_len > 0) {
GLchar* log_buf = (GLchar*) _sg_malloc((size_t)log_len);
glGetProgramInfoLog(gl_prog, log_len, &log_len, log_buf);
_SG_ERROR(GL_SHADER_LINKING_FAILED);
_SG_LOGMSG(GL_SHADER_LINKING_FAILED, log_buf);
_sg_free(log_buf);
}
glDeleteProgram(gl_prog);
return SG_RESOURCESTATE_FAILED;
}
shd->gl.prog = gl_prog;
// resolve uniforms
_SG_GL_CHECK_ERROR();
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &desc->vs : &desc->fs;
const _sg_shader_stage_t* stage = &shd->cmn.stage[stage_index];
_sg_gl_shader_stage_t* gl_stage = &shd->gl.stage[stage_index];
for (int ub_index = 0; ub_index < stage->num_uniform_blocks; ub_index++) {
const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index];
SOKOL_ASSERT(ub_desc->size > 0);
_sg_gl_uniform_block_t* ub = &gl_stage->uniform_blocks[ub_index];
SOKOL_ASSERT(ub->num_uniforms == 0);
uint32_t cur_uniform_offset = 0;
for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) {
const sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index];
if (u_desc->type == SG_UNIFORMTYPE_INVALID) {
break;
}
const uint32_t u_align = _sg_uniform_alignment(u_desc->type, u_desc->array_count, ub_desc->layout);
const uint32_t u_size = _sg_uniform_size(u_desc->type, u_desc->array_count, ub_desc->layout);
cur_uniform_offset = _sg_align_u32(cur_uniform_offset, u_align);
_sg_gl_uniform_t* u = &ub->uniforms[u_index];
u->type = u_desc->type;
u->count = (uint16_t) u_desc->array_count;
u->offset = (uint16_t) cur_uniform_offset;
cur_uniform_offset += u_size;
if (u_desc->name) {
u->gl_loc = glGetUniformLocation(gl_prog, u_desc->name);
} else {
u->gl_loc = u_index;
}
ub->num_uniforms++;
}
if (ub_desc->layout == SG_UNIFORMLAYOUT_STD140) {
cur_uniform_offset = _sg_align_u32(cur_uniform_offset, 16);
}
SOKOL_ASSERT(ub_desc->size == (size_t)cur_uniform_offset);
_SOKOL_UNUSED(cur_uniform_offset);
}
}
// resolve combined image samplers
_SG_GL_CHECK_ERROR();
GLuint cur_prog = 0;
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&cur_prog);
glUseProgram(gl_prog);
int gl_tex_slot = 0;
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &desc->vs : &desc->fs;
const _sg_shader_stage_t* stage = &shd->cmn.stage[stage_index];
_sg_gl_shader_stage_t* gl_stage = &shd->gl.stage[stage_index];
for (int img_smp_index = 0; img_smp_index < stage->num_image_samplers; img_smp_index++) {
const sg_shader_image_sampler_pair_desc* img_smp_desc = &stage_desc->image_sampler_pairs[img_smp_index];
_sg_gl_shader_image_sampler_t* gl_img_smp = &gl_stage->image_samplers[img_smp_index];
SOKOL_ASSERT(img_smp_desc->glsl_name);
GLint gl_loc = glGetUniformLocation(gl_prog, img_smp_desc->glsl_name);
if (gl_loc != -1) {
gl_img_smp->gl_tex_slot = gl_tex_slot++;
glUniform1i(gl_loc, gl_img_smp->gl_tex_slot);
} else {
gl_img_smp->gl_tex_slot = -1;
_SG_ERROR(GL_TEXTURE_NAME_NOT_FOUND_IN_SHADER);
_SG_LOGMSG(GL_TEXTURE_NAME_NOT_FOUND_IN_SHADER, img_smp_desc->glsl_name);
}
}
}
// it's legal to call glUseProgram with 0
glUseProgram(cur_prog);
_SG_GL_CHECK_ERROR();
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_discard_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
_SG_GL_CHECK_ERROR();
if (shd->gl.prog) {
_sg_gl_cache_invalidate_program(shd->gl.prog);
glDeleteProgram(shd->gl.prog);
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && shd && desc);
SOKOL_ASSERT((pip->shader == 0) && (pip->cmn.shader_id.id != SG_INVALID_ID));
SOKOL_ASSERT(desc->shader.id == shd->slot.id);
SOKOL_ASSERT(shd->gl.prog);
pip->shader = shd;
pip->gl.primitive_type = desc->primitive_type;
pip->gl.depth = desc->depth;
pip->gl.stencil = desc->stencil;
// FIXME: blend color and write mask per draw-buffer-attachment (requires GL4)
pip->gl.blend = desc->colors[0].blend;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
pip->gl.color_write_mask[i] = desc->colors[i].write_mask;
}
pip->gl.cull_mode = desc->cull_mode;
pip->gl.face_winding = desc->face_winding;
pip->gl.sample_count = desc->sample_count;
pip->gl.alpha_to_coverage_enabled = desc->alpha_to_coverage_enabled;
// resolve vertex attributes
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
pip->gl.attrs[attr_index].vb_index = -1;
}
for (int attr_index = 0; attr_index < _sg.limits.max_vertex_attrs; attr_index++) {
const sg_vertex_attr_state* a_state = &desc->layout.attrs[attr_index];
if (a_state->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS);
const sg_vertex_buffer_layout_state* l_state = &desc->layout.buffers[a_state->buffer_index];
const sg_vertex_step step_func = l_state->step_func;
const int step_rate = l_state->step_rate;
GLint attr_loc = attr_index;
if (!_sg_strempty(&shd->gl.attrs[attr_index].name)) {
attr_loc = glGetAttribLocation(pip->shader->gl.prog, _sg_strptr(&shd->gl.attrs[attr_index].name));
}
SOKOL_ASSERT(attr_loc < (GLint)_sg.limits.max_vertex_attrs);
if (attr_loc != -1) {
_sg_gl_attr_t* gl_attr = &pip->gl.attrs[attr_loc];
SOKOL_ASSERT(gl_attr->vb_index == -1);
gl_attr->vb_index = (int8_t) a_state->buffer_index;
if (step_func == SG_VERTEXSTEP_PER_VERTEX) {
gl_attr->divisor = 0;
} else {
gl_attr->divisor = (int8_t) step_rate;
pip->cmn.use_instanced_draw = true;
}
SOKOL_ASSERT(l_state->stride > 0);
gl_attr->stride = (uint8_t) l_state->stride;
gl_attr->offset = a_state->offset;
gl_attr->size = (uint8_t) _sg_gl_vertexformat_size(a_state->format);
gl_attr->type = _sg_gl_vertexformat_type(a_state->format);
gl_attr->normalized = _sg_gl_vertexformat_normalized(a_state->format);
pip->cmn.vertex_buffer_layout_active[a_state->buffer_index] = true;
} else {
_SG_ERROR(GL_VERTEX_ATTRIBUTE_NOT_FOUND_IN_SHADER);
_SG_LOGMSG(GL_VERTEX_ATTRIBUTE_NOT_FOUND_IN_SHADER, _sg_strptr(&shd->gl.attrs[attr_index].name));
}
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_discard_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
_sg_gl_cache_invalidate_pipeline(pip);
}
_SOKOL_PRIVATE void _sg_gl_fb_attach_texture(const _sg_gl_attachment_t* gl_att, const _sg_attachment_common_t* cmn_att, GLenum gl_att_type) {
const _sg_image_t* img = gl_att->image;
SOKOL_ASSERT(img);
const GLuint gl_tex = img->gl.tex[0];
SOKOL_ASSERT(gl_tex);
const GLuint gl_target = img->gl.target;
SOKOL_ASSERT(gl_target);
const int mip_level = cmn_att->mip_level;
const int slice = cmn_att->slice;
switch (img->cmn.type) {
case SG_IMAGETYPE_2D:
glFramebufferTexture2D(GL_FRAMEBUFFER, gl_att_type, gl_target, gl_tex, mip_level);
break;
case SG_IMAGETYPE_CUBE:
glFramebufferTexture2D(GL_FRAMEBUFFER, gl_att_type, _sg_gl_cubeface_target(slice), gl_tex, mip_level);
break;
default:
glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_att_type, gl_tex, mip_level, slice);
break;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_depth_stencil_attachment_type(const _sg_gl_attachment_t* ds_att) {
const _sg_image_t* img = ds_att->image;
SOKOL_ASSERT(img);
if (_sg_is_depth_stencil_format(img->cmn.pixel_format)) {
return GL_DEPTH_STENCIL_ATTACHMENT;
} else {
return GL_DEPTH_ATTACHMENT;
}
}
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_image, const sg_attachments_desc* desc) {
SOKOL_ASSERT(atts && desc);
SOKOL_ASSERT(color_images && resolve_images);
_SG_GL_CHECK_ERROR();
// copy image pointers
for (int i = 0; i < atts->cmn.num_colors; i++) {
const sg_attachment_desc* color_desc = &desc->colors[i];
_SOKOL_UNUSED(color_desc);
SOKOL_ASSERT(color_desc->image.id != SG_INVALID_ID);
SOKOL_ASSERT(0 == atts->gl.colors[i].image);
SOKOL_ASSERT(color_images[i] && (color_images[i]->slot.id == color_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(color_images[i]->cmn.pixel_format));
atts->gl.colors[i].image = color_images[i];
const sg_attachment_desc* resolve_desc = &desc->resolves[i];
if (resolve_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(0 == atts->gl.resolves[i].image);
SOKOL_ASSERT(resolve_images[i] && (resolve_images[i]->slot.id == resolve_desc->image.id));
SOKOL_ASSERT(color_images[i] && (color_images[i]->cmn.pixel_format == resolve_images[i]->cmn.pixel_format));
atts->gl.resolves[i].image = resolve_images[i];
}
}
SOKOL_ASSERT(0 == atts->gl.depth_stencil.image);
const sg_attachment_desc* ds_desc = &desc->depth_stencil;
if (ds_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(ds_image && (ds_image->slot.id == ds_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(ds_image->cmn.pixel_format));
atts->gl.depth_stencil.image = ds_image;
}
// store current framebuffer binding (restored at end of function)
GLuint gl_orig_fb;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&gl_orig_fb);
// create a framebuffer object
glGenFramebuffers(1, &atts->gl.fb);
glBindFramebuffer(GL_FRAMEBUFFER, atts->gl.fb);
// attach color attachments to framebuffer
for (int i = 0; i < atts->cmn.num_colors; i++) {
const _sg_image_t* color_img = atts->gl.colors[i].image;
SOKOL_ASSERT(color_img);
const GLuint gl_msaa_render_buffer = color_img->gl.msaa_render_buffer;
if (gl_msaa_render_buffer) {
glFramebufferRenderbuffer(GL_FRAMEBUFFER, (GLenum)(GL_COLOR_ATTACHMENT0+i), GL_RENDERBUFFER, gl_msaa_render_buffer);
} else {
const GLenum gl_att_type = (GLenum)(GL_COLOR_ATTACHMENT0 + i);
_sg_gl_fb_attach_texture(&atts->gl.colors[i], &atts->cmn.colors[i], gl_att_type);
}
}
// attach depth-stencil attachment
if (atts->gl.depth_stencil.image) {
const GLenum gl_att = _sg_gl_depth_stencil_attachment_type(&atts->gl.depth_stencil);
const _sg_image_t* ds_img = atts->gl.depth_stencil.image;
const GLuint gl_msaa_render_buffer = ds_img->gl.msaa_render_buffer;
if (gl_msaa_render_buffer) {
glFramebufferRenderbuffer(GL_FRAMEBUFFER, gl_att, GL_RENDERBUFFER, gl_msaa_render_buffer);
} else {
const GLenum gl_att_type = _sg_gl_depth_stencil_attachment_type(&atts->gl.depth_stencil);
_sg_gl_fb_attach_texture(&atts->gl.depth_stencil, &atts->cmn.depth_stencil, gl_att_type);
}
}
// check if framebuffer is complete
{
const GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fb_status != GL_FRAMEBUFFER_COMPLETE) {
switch (fb_status) {
case GL_FRAMEBUFFER_UNDEFINED:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_UNDEFINED);
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_ATTACHMENT);
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MISSING_ATTACHMENT);
break;
case GL_FRAMEBUFFER_UNSUPPORTED:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_UNSUPPORTED);
break;
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MULTISAMPLE);
break;
default:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_UNKNOWN);
break;
}
return SG_RESOURCESTATE_FAILED;
}
}
// setup color attachments for the framebuffer
static const GLenum gl_draw_bufs[SG_MAX_COLOR_ATTACHMENTS] = {
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2,
GL_COLOR_ATTACHMENT3
};
glDrawBuffers(atts->cmn.num_colors, gl_draw_bufs);
// create MSAA resolve framebuffers if necessary
for (int i = 0; i < atts->cmn.num_colors; i++) {
_sg_gl_attachment_t* gl_resolve_att = &atts->gl.resolves[i];
if (gl_resolve_att->image) {
_sg_attachment_common_t* cmn_resolve_att = &atts->cmn.resolves[i];
SOKOL_ASSERT(0 == atts->gl.msaa_resolve_framebuffer[i]);
glGenFramebuffers(1, &atts->gl.msaa_resolve_framebuffer[i]);
glBindFramebuffer(GL_FRAMEBUFFER, atts->gl.msaa_resolve_framebuffer[i]);
_sg_gl_fb_attach_texture(gl_resolve_att, cmn_resolve_att, GL_COLOR_ATTACHMENT0);
// check if framebuffer is complete
const GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fb_status != GL_FRAMEBUFFER_COMPLETE) {
switch (fb_status) {
case GL_FRAMEBUFFER_UNDEFINED:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_UNDEFINED);
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_ATTACHMENT);
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MISSING_ATTACHMENT);
break;
case GL_FRAMEBUFFER_UNSUPPORTED:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_UNSUPPORTED);
break;
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MULTISAMPLE);
break;
default:
_SG_ERROR(GL_FRAMEBUFFER_STATUS_UNKNOWN);
break;
}
return SG_RESOURCESTATE_FAILED;
}
// setup color attachments for the framebuffer
glDrawBuffers(1, &gl_draw_bufs[0]);
}
}
// restore original framebuffer binding
glBindFramebuffer(GL_FRAMEBUFFER, gl_orig_fb);
_SG_GL_CHECK_ERROR();
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_discard_attachments(_sg_attachments_t* atts) {
SOKOL_ASSERT(atts);
_SG_GL_CHECK_ERROR();
if (0 != atts->gl.fb) {
glDeleteFramebuffers(1, &atts->gl.fb);
}
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (atts->gl.msaa_resolve_framebuffer[i]) {
glDeleteFramebuffers(1, &atts->gl.msaa_resolve_framebuffer[i]);
}
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE _sg_image_t* _sg_gl_attachments_color_image(const _sg_attachments_t* atts, int index) {
SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
return atts->gl.colors[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_gl_attachments_resolve_image(const _sg_attachments_t* atts, int index) {
SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
return atts->gl.resolves[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_gl_attachments_ds_image(const _sg_attachments_t* atts) {
SOKOL_ASSERT(atts);
return atts->gl.depth_stencil.image;
}
_SOKOL_PRIVATE void _sg_gl_begin_pass(const sg_pass* pass) {
// FIXME: what if a texture used as render target is still bound, should we
// unbind all currently bound textures in begin pass?
SOKOL_ASSERT(pass);
_SG_GL_CHECK_ERROR();
const _sg_attachments_t* atts = _sg.cur_pass.atts;
const sg_swapchain* swapchain = &pass->swapchain;
const sg_pass_action* action = &pass->action;
// bind the render pass framebuffer
//
// FIXME: Disabling SRGB conversion for the default framebuffer is
// a crude hack to make behaviour for sRGB render target textures
// identical with the Metal and D3D11 swapchains created by sokol-app.
//
// This will need a cleaner solution (e.g. allowing to configure
// sokol_app.h with an sRGB or RGB framebuffer.
if (atts) {
// offscreen pass
SOKOL_ASSERT(atts->gl.fb);
#if defined(SOKOL_GLCORE)
glEnable(GL_FRAMEBUFFER_SRGB);
#endif
glBindFramebuffer(GL_FRAMEBUFFER, atts->gl.fb);
} else {
// default pass
#if defined(SOKOL_GLCORE)
glDisable(GL_FRAMEBUFFER_SRGB);
#endif
// NOTE: on some platforms, the default framebuffer of a context
// is null, so we can't actually assert here that the
// framebuffer has been provided
glBindFramebuffer(GL_FRAMEBUFFER, swapchain->gl.framebuffer);
}
glViewport(0, 0, _sg.cur_pass.width, _sg.cur_pass.height);
glScissor(0, 0, _sg.cur_pass.width, _sg.cur_pass.height);
// number of color attachments
const int num_color_atts = atts ? atts->cmn.num_colors : 1;
// clear color and depth-stencil attachments if needed
bool clear_any_color = false;
for (int i = 0; i < num_color_atts; i++) {
if (SG_LOADACTION_CLEAR == action->colors[i].load_action) {
clear_any_color = true;
break;
}
}
const bool clear_depth = (action->depth.load_action == SG_LOADACTION_CLEAR);
const bool clear_stencil = (action->stencil.load_action == SG_LOADACTION_CLEAR);
bool need_pip_cache_flush = false;
if (clear_any_color) {
bool need_color_mask_flush = false;
// NOTE: not a bug to iterate over all possible color attachments
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (SG_COLORMASK_RGBA != _sg.gl.cache.color_write_mask[i]) {
need_pip_cache_flush = true;
need_color_mask_flush = true;
_sg.gl.cache.color_write_mask[i] = SG_COLORMASK_RGBA;
}
}
if (need_color_mask_flush) {
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
}
}
if (clear_depth) {
if (!_sg.gl.cache.depth.write_enabled) {
need_pip_cache_flush = true;
_sg.gl.cache.depth.write_enabled = true;
glDepthMask(GL_TRUE);
}
if (_sg.gl.cache.depth.compare != SG_COMPAREFUNC_ALWAYS) {
need_pip_cache_flush = true;
_sg.gl.cache.depth.compare = SG_COMPAREFUNC_ALWAYS;
glDepthFunc(GL_ALWAYS);
}
}
if (clear_stencil) {
if (_sg.gl.cache.stencil.write_mask != 0xFF) {
need_pip_cache_flush = true;
_sg.gl.cache.stencil.write_mask = 0xFF;
glStencilMask(0xFF);
}
}
if (need_pip_cache_flush) {
// we messed with the state cache directly, need to clear cached
// pipeline to force re-evaluation in next sg_apply_pipeline()
_sg.gl.cache.cur_pipeline = 0;
_sg.gl.cache.cur_pipeline_id.id = SG_INVALID_ID;
}
for (int i = 0; i < num_color_atts; i++) {
if (action->colors[i].load_action == SG_LOADACTION_CLEAR) {
glClearBufferfv(GL_COLOR, i, &action->colors[i].clear_value.r);
}
}
if ((atts == 0) || (atts->gl.depth_stencil.image)) {
if (clear_depth && clear_stencil) {
glClearBufferfi(GL_DEPTH_STENCIL, 0, action->depth.clear_value, action->stencil.clear_value);
} else if (clear_depth) {
glClearBufferfv(GL_DEPTH, 0, &action->depth.clear_value);
} else if (clear_stencil) {
GLint val = (GLint) action->stencil.clear_value;
glClearBufferiv(GL_STENCIL, 0, &val);
}
}
// keep store actions for end-pass
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
_sg.gl.color_store_actions[i] = action->colors[i].store_action;
}
_sg.gl.depth_store_action = action->depth.store_action;
_sg.gl.stencil_store_action = action->stencil.store_action;
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE void _sg_gl_end_pass(void) {
_SG_GL_CHECK_ERROR();
if (_sg.cur_pass.atts) {
const _sg_attachments_t* atts = _sg.cur_pass.atts;
SOKOL_ASSERT(atts->slot.id == _sg.cur_pass.atts_id.id);
bool fb_read_bound = false;
bool fb_draw_bound = false;
const int num_color_atts = atts->cmn.num_colors;
for (int i = 0; i < num_color_atts; i++) {
// perform MSAA resolve if needed
if (atts->gl.msaa_resolve_framebuffer[i] != 0) {
if (!fb_read_bound) {
SOKOL_ASSERT(atts->gl.fb);
glBindFramebuffer(GL_READ_FRAMEBUFFER, atts->gl.fb);
fb_read_bound = true;
}
const int w = atts->gl.colors[i].image->cmn.width;
const int h = atts->gl.colors[i].image->cmn.height;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, atts->gl.msaa_resolve_framebuffer[i]);
glReadBuffer((GLenum)(GL_COLOR_ATTACHMENT0 + i));
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
fb_draw_bound = true;
}
}
// invalidate framebuffers
_SOKOL_UNUSED(fb_draw_bound);
#if defined(SOKOL_GLES3)
// need to restore framebuffer binding before invalidate if the MSAA resolve had changed the binding
if (fb_draw_bound) {
glBindFramebuffer(GL_FRAMEBUFFER, atts->gl.fb);
}
GLenum invalidate_atts[SG_MAX_COLOR_ATTACHMENTS + 2] = { 0 };
int att_index = 0;
for (int i = 0; i < num_color_atts; i++) {
if (_sg.gl.color_store_actions[i] == SG_STOREACTION_DONTCARE) {
invalidate_atts[att_index++] = (GLenum)(GL_COLOR_ATTACHMENT0 + i);
}
}
if ((_sg.gl.depth_store_action == SG_STOREACTION_DONTCARE) && (_sg.cur_pass.atts->cmn.depth_stencil.image_id.id != SG_INVALID_ID)) {
invalidate_atts[att_index++] = GL_DEPTH_ATTACHMENT;
}
if ((_sg.gl.stencil_store_action == SG_STOREACTION_DONTCARE) && (_sg.cur_pass.atts->cmn.depth_stencil.image_id.id != SG_INVALID_ID)) {
invalidate_atts[att_index++] = GL_STENCIL_ATTACHMENT;
}
if (att_index > 0) {
glInvalidateFramebuffer(GL_DRAW_FRAMEBUFFER, att_index, invalidate_atts);
}
#endif
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE void _sg_gl_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
y = origin_top_left ? (_sg.cur_pass.height - (y+h)) : y;
glViewport(x, y, w, h);
}
_SOKOL_PRIVATE void _sg_gl_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
y = origin_top_left ? (_sg.cur_pass.height - (y+h)) : y;
glScissor(x, y, w, h);
}
_SOKOL_PRIVATE void _sg_gl_apply_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id));
_SG_GL_CHECK_ERROR();
if ((_sg.gl.cache.cur_pipeline != pip) || (_sg.gl.cache.cur_pipeline_id.id != pip->slot.id)) {
_sg.gl.cache.cur_pipeline = pip;
_sg.gl.cache.cur_pipeline_id.id = pip->slot.id;
_sg.gl.cache.cur_primitive_type = _sg_gl_primitive_type(pip->gl.primitive_type);
_sg.gl.cache.cur_index_type = _sg_gl_index_type(pip->cmn.index_type);
// update depth state
{
const sg_depth_state* state_ds = &pip->gl.depth;
sg_depth_state* cache_ds = &_sg.gl.cache.depth;
if (state_ds->compare != cache_ds->compare) {
cache_ds->compare = state_ds->compare;
glDepthFunc(_sg_gl_compare_func(state_ds->compare));
_sg_stats_add(gl.num_render_state, 1);
}
if (state_ds->write_enabled != cache_ds->write_enabled) {
cache_ds->write_enabled = state_ds->write_enabled;
glDepthMask(state_ds->write_enabled);
_sg_stats_add(gl.num_render_state, 1);
}
if (!_sg_fequal(state_ds->bias, cache_ds->bias, 0.000001f) ||
!_sg_fequal(state_ds->bias_slope_scale, cache_ds->bias_slope_scale, 0.000001f))
{
/* according to ANGLE's D3D11 backend:
D3D11 SlopeScaledDepthBias ==> GL polygonOffsetFactor
D3D11 DepthBias ==> GL polygonOffsetUnits
DepthBiasClamp has no meaning on GL
*/
cache_ds->bias = state_ds->bias;
cache_ds->bias_slope_scale = state_ds->bias_slope_scale;
glPolygonOffset(state_ds->bias_slope_scale, state_ds->bias);
_sg_stats_add(gl.num_render_state, 1);
bool po_enabled = true;
if (_sg_fequal(state_ds->bias, 0.0f, 0.000001f) &&
_sg_fequal(state_ds->bias_slope_scale, 0.0f, 0.000001f))
{
po_enabled = false;
}
if (po_enabled != _sg.gl.cache.polygon_offset_enabled) {
_sg.gl.cache.polygon_offset_enabled = po_enabled;
if (po_enabled) {
glEnable(GL_POLYGON_OFFSET_FILL);
} else {
glDisable(GL_POLYGON_OFFSET_FILL);
}
_sg_stats_add(gl.num_render_state, 1);
}
}
}
// update stencil state
{
const sg_stencil_state* state_ss = &pip->gl.stencil;
sg_stencil_state* cache_ss = &_sg.gl.cache.stencil;
if (state_ss->enabled != cache_ss->enabled) {
cache_ss->enabled = state_ss->enabled;
if (state_ss->enabled) {
glEnable(GL_STENCIL_TEST);
} else {
glDisable(GL_STENCIL_TEST);
}
_sg_stats_add(gl.num_render_state, 1);
}
if (state_ss->write_mask != cache_ss->write_mask) {
cache_ss->write_mask = state_ss->write_mask;
glStencilMask(state_ss->write_mask);
_sg_stats_add(gl.num_render_state, 1);
}
for (int i = 0; i < 2; i++) {
const sg_stencil_face_state* state_sfs = (i==0)? &state_ss->front : &state_ss->back;
sg_stencil_face_state* cache_sfs = (i==0)? &cache_ss->front : &cache_ss->back;
GLenum gl_face = (i==0)? GL_FRONT : GL_BACK;
if ((state_sfs->compare != cache_sfs->compare) ||
(state_ss->read_mask != cache_ss->read_mask) ||
(state_ss->ref != cache_ss->ref))
{
cache_sfs->compare = state_sfs->compare;
glStencilFuncSeparate(gl_face,
_sg_gl_compare_func(state_sfs->compare),
state_ss->ref,
state_ss->read_mask);
_sg_stats_add(gl.num_render_state, 1);
}
if ((state_sfs->fail_op != cache_sfs->fail_op) ||
(state_sfs->depth_fail_op != cache_sfs->depth_fail_op) ||
(state_sfs->pass_op != cache_sfs->pass_op))
{
cache_sfs->fail_op = state_sfs->fail_op;
cache_sfs->depth_fail_op = state_sfs->depth_fail_op;
cache_sfs->pass_op = state_sfs->pass_op;
glStencilOpSeparate(gl_face,
_sg_gl_stencil_op(state_sfs->fail_op),
_sg_gl_stencil_op(state_sfs->depth_fail_op),
_sg_gl_stencil_op(state_sfs->pass_op));
_sg_stats_add(gl.num_render_state, 1);
}
}
cache_ss->read_mask = state_ss->read_mask;
cache_ss->ref = state_ss->ref;
}
if (pip->cmn.color_count > 0) {
// update blend state
// FIXME: separate blend state per color attachment not support, needs GL4
const sg_blend_state* state_bs = &pip->gl.blend;
sg_blend_state* cache_bs = &_sg.gl.cache.blend;
if (state_bs->enabled != cache_bs->enabled) {
cache_bs->enabled = state_bs->enabled;
if (state_bs->enabled) {
glEnable(GL_BLEND);
} else {
glDisable(GL_BLEND);
}
_sg_stats_add(gl.num_render_state, 1);
}
if ((state_bs->src_factor_rgb != cache_bs->src_factor_rgb) ||
(state_bs->dst_factor_rgb != cache_bs->dst_factor_rgb) ||
(state_bs->src_factor_alpha != cache_bs->src_factor_alpha) ||
(state_bs->dst_factor_alpha != cache_bs->dst_factor_alpha))
{
cache_bs->src_factor_rgb = state_bs->src_factor_rgb;
cache_bs->dst_factor_rgb = state_bs->dst_factor_rgb;
cache_bs->src_factor_alpha = state_bs->src_factor_alpha;
cache_bs->dst_factor_alpha = state_bs->dst_factor_alpha;
glBlendFuncSeparate(_sg_gl_blend_factor(state_bs->src_factor_rgb),
_sg_gl_blend_factor(state_bs->dst_factor_rgb),
_sg_gl_blend_factor(state_bs->src_factor_alpha),
_sg_gl_blend_factor(state_bs->dst_factor_alpha));
_sg_stats_add(gl.num_render_state, 1);
}
if ((state_bs->op_rgb != cache_bs->op_rgb) || (state_bs->op_alpha != cache_bs->op_alpha)) {
cache_bs->op_rgb = state_bs->op_rgb;
cache_bs->op_alpha = state_bs->op_alpha;
glBlendEquationSeparate(_sg_gl_blend_op(state_bs->op_rgb), _sg_gl_blend_op(state_bs->op_alpha));
_sg_stats_add(gl.num_render_state, 1);
}
// standalone color target state
for (GLuint i = 0; i < (GLuint)pip->cmn.color_count; i++) {
if (pip->gl.color_write_mask[i] != _sg.gl.cache.color_write_mask[i]) {
const sg_color_mask cm = pip->gl.color_write_mask[i];
_sg.gl.cache.color_write_mask[i] = cm;
#ifdef SOKOL_GLCORE
glColorMaski(i,
(cm & SG_COLORMASK_R) != 0,
(cm & SG_COLORMASK_G) != 0,
(cm & SG_COLORMASK_B) != 0,
(cm & SG_COLORMASK_A) != 0);
#else
if (0 == i) {
glColorMask((cm & SG_COLORMASK_R) != 0,
(cm & SG_COLORMASK_G) != 0,
(cm & SG_COLORMASK_B) != 0,
(cm & SG_COLORMASK_A) != 0);
}
#endif
_sg_stats_add(gl.num_render_state, 1);
}
}
if (!_sg_fequal(pip->cmn.blend_color.r, _sg.gl.cache.blend_color.r, 0.0001f) ||
!_sg_fequal(pip->cmn.blend_color.g, _sg.gl.cache.blend_color.g, 0.0001f) ||
!_sg_fequal(pip->cmn.blend_color.b, _sg.gl.cache.blend_color.b, 0.0001f) ||
!_sg_fequal(pip->cmn.blend_color.a, _sg.gl.cache.blend_color.a, 0.0001f))
{
sg_color c = pip->cmn.blend_color;
_sg.gl.cache.blend_color = c;
glBlendColor(c.r, c.g, c.b, c.a);
_sg_stats_add(gl.num_render_state, 1);
}
} // pip->cmn.color_count > 0
if (pip->gl.cull_mode != _sg.gl.cache.cull_mode) {
_sg.gl.cache.cull_mode = pip->gl.cull_mode;
if (SG_CULLMODE_NONE == pip->gl.cull_mode) {
glDisable(GL_CULL_FACE);
_sg_stats_add(gl.num_render_state, 1);
} else {
glEnable(GL_CULL_FACE);
GLenum gl_mode = (SG_CULLMODE_FRONT == pip->gl.cull_mode) ? GL_FRONT : GL_BACK;
glCullFace(gl_mode);
_sg_stats_add(gl.num_render_state, 2);
}
}
if (pip->gl.face_winding != _sg.gl.cache.face_winding) {
_sg.gl.cache.face_winding = pip->gl.face_winding;
GLenum gl_winding = (SG_FACEWINDING_CW == pip->gl.face_winding) ? GL_CW : GL_CCW;
glFrontFace(gl_winding);
_sg_stats_add(gl.num_render_state, 1);
}
if (pip->gl.alpha_to_coverage_enabled != _sg.gl.cache.alpha_to_coverage_enabled) {
_sg.gl.cache.alpha_to_coverage_enabled = pip->gl.alpha_to_coverage_enabled;
if (pip->gl.alpha_to_coverage_enabled) {
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
} else {
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
}
_sg_stats_add(gl.num_render_state, 1);
}
#ifdef SOKOL_GLCORE
if (pip->gl.sample_count != _sg.gl.cache.sample_count) {
_sg.gl.cache.sample_count = pip->gl.sample_count;
if (pip->gl.sample_count > 1) {
glEnable(GL_MULTISAMPLE);
} else {
glDisable(GL_MULTISAMPLE);
}
_sg_stats_add(gl.num_render_state, 1);
}
#endif
// bind shader program
if (pip->shader->gl.prog != _sg.gl.cache.prog) {
_sg.gl.cache.prog = pip->shader->gl.prog;
glUseProgram(pip->shader->gl.prog);
_sg_stats_add(gl.num_use_program, 1);
}
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE bool _sg_gl_apply_bindings(_sg_bindings_t* bnd) {
SOKOL_ASSERT(bnd);
SOKOL_ASSERT(bnd->pip);
_SG_GL_CHECK_ERROR();
// bind combined image-samplers
_SG_GL_CHECK_ERROR();
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const _sg_shader_stage_t* stage = &bnd->pip->shader->cmn.stage[stage_index];
const _sg_gl_shader_stage_t* gl_stage = &bnd->pip->shader->gl.stage[stage_index];
_sg_image_t** imgs = (stage_index == SG_SHADERSTAGE_VS) ? bnd->vs_imgs : bnd->fs_imgs;
_sg_sampler_t** smps = (stage_index == SG_SHADERSTAGE_VS) ? bnd->vs_smps : bnd->fs_smps;
const int num_imgs = (stage_index == SG_SHADERSTAGE_VS) ? bnd->num_vs_imgs : bnd->num_fs_imgs;
const int num_smps = (stage_index == SG_SHADERSTAGE_VS) ? bnd->num_vs_smps : bnd->num_fs_smps;
SOKOL_ASSERT(num_imgs == stage->num_images); _SOKOL_UNUSED(num_imgs);
SOKOL_ASSERT(num_smps == stage->num_samplers); _SOKOL_UNUSED(num_smps);
for (int img_smp_index = 0; img_smp_index < stage->num_image_samplers; img_smp_index++) {
const int gl_tex_slot = gl_stage->image_samplers[img_smp_index].gl_tex_slot;
if (gl_tex_slot != -1) {
const int img_index = stage->image_samplers[img_smp_index].image_slot;
const int smp_index = stage->image_samplers[img_smp_index].sampler_slot;
SOKOL_ASSERT(img_index < num_imgs);
SOKOL_ASSERT(smp_index < num_smps);
_sg_image_t* img = imgs[img_index];
_sg_sampler_t* smp = smps[smp_index];
const GLenum gl_tgt = img->gl.target;
const GLuint gl_tex = img->gl.tex[img->cmn.active_slot];
const GLuint gl_smp = smp->gl.smp;
_sg_gl_cache_bind_texture_sampler(gl_tex_slot, gl_tgt, gl_tex, gl_smp);
}
}
}
_SG_GL_CHECK_ERROR();
// bind storage buffers
for (int slot = 0; slot < bnd->num_vs_sbufs; slot++) {
_sg_buffer_t* sb = bnd->vs_sbufs[slot];
GLuint gl_sb = sb->gl.buf[sb->cmn.active_slot];
_sg_gl_cache_bind_storage_buffer(SG_SHADERSTAGE_VS, slot, gl_sb);
}
for (int slot = 0; slot < bnd->num_fs_sbufs; slot++) {
_sg_buffer_t* sb = bnd->fs_sbufs[slot];
GLuint gl_sb = sb->gl.buf[sb->cmn.active_slot];
_sg_gl_cache_bind_storage_buffer(SG_SHADERSTAGE_FS, slot, gl_sb);
}
// index buffer (can be 0)
const GLuint gl_ib = bnd->ib ? bnd->ib->gl.buf[bnd->ib->cmn.active_slot] : 0;
_sg_gl_cache_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, gl_ib);
_sg.gl.cache.cur_ib_offset = bnd->ib_offset;
// vertex attributes
for (GLuint attr_index = 0; attr_index < (GLuint)_sg.limits.max_vertex_attrs; attr_index++) {
_sg_gl_attr_t* attr = &bnd->pip->gl.attrs[attr_index];
_sg_gl_cache_attr_t* cache_attr = &_sg.gl.cache.attrs[attr_index];
bool cache_attr_dirty = false;
int vb_offset = 0;
GLuint gl_vb = 0;
if (attr->vb_index >= 0) {
// attribute is enabled
SOKOL_ASSERT(attr->vb_index < bnd->num_vbs);
_sg_buffer_t* vb = bnd->vbs[attr->vb_index];
SOKOL_ASSERT(vb);
gl_vb = vb->gl.buf[vb->cmn.active_slot];
vb_offset = bnd->vb_offsets[attr->vb_index] + attr->offset;
if ((gl_vb != cache_attr->gl_vbuf) ||
(attr->size != cache_attr->gl_attr.size) ||
(attr->type != cache_attr->gl_attr.type) ||
(attr->normalized != cache_attr->gl_attr.normalized) ||
(attr->stride != cache_attr->gl_attr.stride) ||
(vb_offset != cache_attr->gl_attr.offset) ||
(cache_attr->gl_attr.divisor != attr->divisor))
{
_sg_gl_cache_bind_buffer(GL_ARRAY_BUFFER, gl_vb);
glVertexAttribPointer(attr_index, attr->size, attr->type, attr->normalized, attr->stride, (const GLvoid*)(GLintptr)vb_offset);
_sg_stats_add(gl.num_vertex_attrib_pointer, 1);
glVertexAttribDivisor(attr_index, (GLuint)attr->divisor);
_sg_stats_add(gl.num_vertex_attrib_divisor, 1);
cache_attr_dirty = true;
}
if (cache_attr->gl_attr.vb_index == -1) {
glEnableVertexAttribArray(attr_index);
_sg_stats_add(gl.num_enable_vertex_attrib_array, 1);
cache_attr_dirty = true;
}
} else {
// attribute is disabled
if (cache_attr->gl_attr.vb_index != -1) {
glDisableVertexAttribArray(attr_index);
_sg_stats_add(gl.num_disable_vertex_attrib_array, 1);
cache_attr_dirty = true;
}
}
if (cache_attr_dirty) {
cache_attr->gl_attr = *attr;
cache_attr->gl_attr.offset = vb_offset;
cache_attr->gl_vbuf = gl_vb;
}
}
_SG_GL_CHECK_ERROR();
return true;
}
_SOKOL_PRIVATE void _sg_gl_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline);
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->slot.id == _sg.gl.cache.cur_pipeline_id.id);
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->slot.id == _sg.gl.cache.cur_pipeline->cmn.shader_id.id);
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks > ub_index);
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size == data->size);
const _sg_gl_shader_stage_t* gl_stage = &_sg.gl.cache.cur_pipeline->shader->gl.stage[stage_index];
const _sg_gl_uniform_block_t* gl_ub = &gl_stage->uniform_blocks[ub_index];
for (int u_index = 0; u_index < gl_ub->num_uniforms; u_index++) {
const _sg_gl_uniform_t* u = &gl_ub->uniforms[u_index];
SOKOL_ASSERT(u->type != SG_UNIFORMTYPE_INVALID);
if (u->gl_loc == -1) {
continue;
}
_sg_stats_add(gl.num_uniform, 1);
GLfloat* fptr = (GLfloat*) (((uint8_t*)data->ptr) + u->offset);
GLint* iptr = (GLint*) (((uint8_t*)data->ptr) + u->offset);
switch (u->type) {
case SG_UNIFORMTYPE_INVALID:
break;
case SG_UNIFORMTYPE_FLOAT:
glUniform1fv(u->gl_loc, u->count, fptr);
break;
case SG_UNIFORMTYPE_FLOAT2:
glUniform2fv(u->gl_loc, u->count, fptr);
break;
case SG_UNIFORMTYPE_FLOAT3:
glUniform3fv(u->gl_loc, u->count, fptr);
break;
case SG_UNIFORMTYPE_FLOAT4:
glUniform4fv(u->gl_loc, u->count, fptr);
break;
case SG_UNIFORMTYPE_INT:
glUniform1iv(u->gl_loc, u->count, iptr);
break;
case SG_UNIFORMTYPE_INT2:
glUniform2iv(u->gl_loc, u->count, iptr);
break;
case SG_UNIFORMTYPE_INT3:
glUniform3iv(u->gl_loc, u->count, iptr);
break;
case SG_UNIFORMTYPE_INT4:
glUniform4iv(u->gl_loc, u->count, iptr);
break;
case SG_UNIFORMTYPE_MAT4:
glUniformMatrix4fv(u->gl_loc, u->count, GL_FALSE, fptr);
break;
default:
SOKOL_UNREACHABLE;
break;
}
}
}
_SOKOL_PRIVATE void _sg_gl_draw(int base_element, int num_elements, int num_instances) {
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline);
const GLenum i_type = _sg.gl.cache.cur_index_type;
const GLenum p_type = _sg.gl.cache.cur_primitive_type;
const bool use_instanced_draw = (num_instances > 1) || (_sg.gl.cache.cur_pipeline->cmn.use_instanced_draw);
if (0 != i_type) {
// indexed rendering
const int i_size = (i_type == GL_UNSIGNED_SHORT) ? 2 : 4;
const int ib_offset = _sg.gl.cache.cur_ib_offset;
const GLvoid* indices = (const GLvoid*)(GLintptr)(base_element*i_size+ib_offset);
if (use_instanced_draw) {
glDrawElementsInstanced(p_type, num_elements, i_type, indices, num_instances);
} else {
glDrawElements(p_type, num_elements, i_type, indices);
}
} else {
// non-indexed rendering
if (use_instanced_draw) {
glDrawArraysInstanced(p_type, base_element, num_elements, num_instances);
} else {
glDrawArrays(p_type, base_element, num_elements);
}
}
}
_SOKOL_PRIVATE void _sg_gl_commit(void) {
// "soft" clear bindings (only those that are actually bound)
_sg_gl_cache_clear_buffer_bindings(false);
_sg_gl_cache_clear_texture_sampler_bindings(false);
}
_SOKOL_PRIVATE void _sg_gl_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
// only one update per buffer per frame allowed
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
GLenum gl_tgt = _sg_gl_buffer_target(buf->cmn.type);
SOKOL_ASSERT(buf->cmn.active_slot < SG_NUM_INFLIGHT_FRAMES);
GLuint gl_buf = buf->gl.buf[buf->cmn.active_slot];
SOKOL_ASSERT(gl_buf);
_SG_GL_CHECK_ERROR();
_sg_gl_cache_store_buffer_binding(gl_tgt);
_sg_gl_cache_bind_buffer(gl_tgt, gl_buf);
glBufferSubData(gl_tgt, 0, (GLsizeiptr)data->size, data->ptr);
_sg_gl_cache_restore_buffer_binding(gl_tgt);
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE void _sg_gl_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
if (new_frame) {
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
}
GLenum gl_tgt = _sg_gl_buffer_target(buf->cmn.type);
SOKOL_ASSERT(buf->cmn.active_slot < SG_NUM_INFLIGHT_FRAMES);
GLuint gl_buf = buf->gl.buf[buf->cmn.active_slot];
SOKOL_ASSERT(gl_buf);
_SG_GL_CHECK_ERROR();
_sg_gl_cache_store_buffer_binding(gl_tgt);
_sg_gl_cache_bind_buffer(gl_tgt, gl_buf);
glBufferSubData(gl_tgt, buf->cmn.append_pos, (GLsizeiptr)data->size, data->ptr);
_sg_gl_cache_restore_buffer_binding(gl_tgt);
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE void _sg_gl_update_image(_sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(img && data);
// only one update per image per frame allowed
if (++img->cmn.active_slot >= img->cmn.num_slots) {
img->cmn.active_slot = 0;
}
SOKOL_ASSERT(img->cmn.active_slot < SG_NUM_INFLIGHT_FRAMES);
SOKOL_ASSERT(0 != img->gl.tex[img->cmn.active_slot]);
_sg_gl_cache_store_texture_sampler_binding(0);
_sg_gl_cache_bind_texture_sampler(0, img->gl.target, img->gl.tex[img->cmn.active_slot], 0);
const GLenum gl_img_format = _sg_gl_teximage_format(img->cmn.pixel_format);
const GLenum gl_img_type = _sg_gl_teximage_type(img->cmn.pixel_format);
const int num_faces = img->cmn.type == SG_IMAGETYPE_CUBE ? 6 : 1;
const int num_mips = img->cmn.num_mipmaps;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int mip_index = 0; mip_index < num_mips; mip_index++) {
GLenum gl_img_target = img->gl.target;
if (SG_IMAGETYPE_CUBE == img->cmn.type) {
gl_img_target = _sg_gl_cubeface_target(face_index);
}
const GLvoid* data_ptr = data->subimage[face_index][mip_index].ptr;
int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index);
int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index);
if ((SG_IMAGETYPE_2D == img->cmn.type) || (SG_IMAGETYPE_CUBE == img->cmn.type)) {
glTexSubImage2D(gl_img_target, mip_index,
0, 0,
mip_width, mip_height,
gl_img_format, gl_img_type,
data_ptr);
} else if ((SG_IMAGETYPE_3D == img->cmn.type) || (SG_IMAGETYPE_ARRAY == img->cmn.type)) {
int mip_depth = img->cmn.num_slices;
if (SG_IMAGETYPE_3D == img->cmn.type) {
mip_depth = _sg_miplevel_dim(img->cmn.num_slices, mip_index);
}
glTexSubImage3D(gl_img_target, mip_index,
0, 0, 0,
mip_width, mip_height, mip_depth,
gl_img_format, gl_img_type,
data_ptr);
}
}
}
_sg_gl_cache_restore_texture_sampler_binding(0);
}
// ██████ ██████ ██████ ██ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████
// ██ ██ ██ ██ ██ ███ ███ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██
// ██ ██ █████ ██ ██ ██ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██████ ██████ ██ ██ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████
//
// >>d3d11 backend
#elif defined(SOKOL_D3D11)
#define _SG_D3D11_MAX_SHADERSTAGE_SRVS (32)
#define _SG_D3D11_SHADERSTAGE_IMAGE_SRV_OFFSET (0)
#define _SG_D3D11_SHADERSTAGE_BUFFER_SRV_OFFSET (16)
#if defined(__cplusplus)
#define _sg_d3d11_AddRef(self) (self)->AddRef()
#else
#define _sg_d3d11_AddRef(self) (self)->lpVtbl->AddRef(self)
#endif
#if defined(__cplusplus)
#define _sg_d3d11_Release(self) (self)->Release()
#else
#define _sg_d3d11_Release(self) (self)->lpVtbl->Release(self)
#endif
// NOTE: This needs to be a macro since we can't use the polymorphism in C. It's called on many kinds of resources.
// NOTE: Based on microsoft docs, it's fine to call this with pData=NULL if DataSize is also zero.
#if defined(__cplusplus)
#define _sg_d3d11_SetPrivateData(self, guid, DataSize, pData) (self)->SetPrivateData(guid, DataSize, pData)
#else
#define _sg_d3d11_SetPrivateData(self, guid, DataSize, pData) (self)->lpVtbl->SetPrivateData(self, guid, DataSize, pData)
#endif
#if defined(__cplusplus)
#define _sg_win32_refguid(guid) guid
#else
#define _sg_win32_refguid(guid) &guid
#endif
static const GUID _sg_d3d11_WKPDID_D3DDebugObjectName = { 0x429b8c22,0x9188,0x4b0c, {0x87,0x42,0xac,0xb0,0xbf,0x85,0xc2,0x00} };
#if defined(SOKOL_DEBUG)
#define _sg_d3d11_setlabel(self, label) _sg_d3d11_SetPrivateData(self, _sg_win32_refguid(_sg_d3d11_WKPDID_D3DDebugObjectName), label ? (UINT)strlen(label) : 0, label)
#else
#define _sg_d3d11_setlabel(self, label)
#endif
//-- D3D11 C/C++ wrappers ------------------------------------------------------
static inline HRESULT _sg_d3d11_CheckFormatSupport(ID3D11Device* self, DXGI_FORMAT Format, UINT* pFormatSupport) {
#if defined(__cplusplus)
return self->CheckFormatSupport(Format, pFormatSupport);
#else
return self->lpVtbl->CheckFormatSupport(self, Format, pFormatSupport);
#endif
}
static inline void _sg_d3d11_OMSetRenderTargets(ID3D11DeviceContext* self, UINT NumViews, ID3D11RenderTargetView* const* ppRenderTargetViews, ID3D11DepthStencilView *pDepthStencilView) {
#if defined(__cplusplus)
self->OMSetRenderTargets(NumViews, ppRenderTargetViews, pDepthStencilView);
#else
self->lpVtbl->OMSetRenderTargets(self, NumViews, ppRenderTargetViews, pDepthStencilView);
#endif
}
static inline void _sg_d3d11_RSSetState(ID3D11DeviceContext* self, ID3D11RasterizerState* pRasterizerState) {
#if defined(__cplusplus)
self->RSSetState(pRasterizerState);
#else
self->lpVtbl->RSSetState(self, pRasterizerState);
#endif
}
static inline void _sg_d3d11_OMSetDepthStencilState(ID3D11DeviceContext* self, ID3D11DepthStencilState* pDepthStencilState, UINT StencilRef) {
#if defined(__cplusplus)
self->OMSetDepthStencilState(pDepthStencilState, StencilRef);
#else
self->lpVtbl->OMSetDepthStencilState(self, pDepthStencilState, StencilRef);
#endif
}
static inline void _sg_d3d11_OMSetBlendState(ID3D11DeviceContext* self, ID3D11BlendState* pBlendState, const FLOAT BlendFactor[4], UINT SampleMask) {
#if defined(__cplusplus)
self->OMSetBlendState(pBlendState, BlendFactor, SampleMask);
#else
self->lpVtbl->OMSetBlendState(self, pBlendState, BlendFactor, SampleMask);
#endif
}
static inline void _sg_d3d11_IASetVertexBuffers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppVertexBuffers, const UINT* pStrides, const UINT* pOffsets) {
#if defined(__cplusplus)
self->IASetVertexBuffers(StartSlot, NumBuffers, ppVertexBuffers, pStrides, pOffsets);
#else
self->lpVtbl->IASetVertexBuffers(self, StartSlot, NumBuffers, ppVertexBuffers, pStrides, pOffsets);
#endif
}
static inline void _sg_d3d11_IASetIndexBuffer(ID3D11DeviceContext* self, ID3D11Buffer* pIndexBuffer, DXGI_FORMAT Format, UINT Offset) {
#if defined(__cplusplus)
self->IASetIndexBuffer(pIndexBuffer, Format, Offset);
#else
self->lpVtbl->IASetIndexBuffer(self, pIndexBuffer, Format, Offset);
#endif
}
static inline void _sg_d3d11_IASetInputLayout(ID3D11DeviceContext* self, ID3D11InputLayout* pInputLayout) {
#if defined(__cplusplus)
self->IASetInputLayout(pInputLayout);
#else
self->lpVtbl->IASetInputLayout(self, pInputLayout);
#endif
}
static inline void _sg_d3d11_VSSetShader(ID3D11DeviceContext* self, ID3D11VertexShader* pVertexShader, ID3D11ClassInstance* const* ppClassInstances, UINT NumClassInstances) {
#if defined(__cplusplus)
self->VSSetShader(pVertexShader, ppClassInstances, NumClassInstances);
#else
self->lpVtbl->VSSetShader(self, pVertexShader, ppClassInstances, NumClassInstances);
#endif
}
static inline void _sg_d3d11_PSSetShader(ID3D11DeviceContext* self, ID3D11PixelShader* pPixelShader, ID3D11ClassInstance* const* ppClassInstances, UINT NumClassInstances) {
#if defined(__cplusplus)
self->PSSetShader(pPixelShader, ppClassInstances, NumClassInstances);
#else
self->lpVtbl->PSSetShader(self, pPixelShader, ppClassInstances, NumClassInstances);
#endif
}
static inline void _sg_d3d11_VSSetConstantBuffers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppConstantBuffers) {
#if defined(__cplusplus)
self->VSSetConstantBuffers(StartSlot, NumBuffers, ppConstantBuffers);
#else
self->lpVtbl->VSSetConstantBuffers(self, StartSlot, NumBuffers, ppConstantBuffers);
#endif
}
static inline void _sg_d3d11_PSSetConstantBuffers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppConstantBuffers) {
#if defined(__cplusplus)
self->PSSetConstantBuffers(StartSlot, NumBuffers, ppConstantBuffers);
#else
self->lpVtbl->PSSetConstantBuffers(self, StartSlot, NumBuffers, ppConstantBuffers);
#endif
}
static inline void _sg_d3d11_VSSetShaderResources(ID3D11DeviceContext* self, UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView* const* ppShaderResourceViews) {
#if defined(__cplusplus)
self->VSSetShaderResources(StartSlot, NumViews, ppShaderResourceViews);
#else
self->lpVtbl->VSSetShaderResources(self, StartSlot, NumViews, ppShaderResourceViews);
#endif
}
static inline void _sg_d3d11_PSSetShaderResources(ID3D11DeviceContext* self, UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView* const* ppShaderResourceViews) {
#if defined(__cplusplus)
self->PSSetShaderResources(StartSlot, NumViews, ppShaderResourceViews);
#else
self->lpVtbl->PSSetShaderResources(self, StartSlot, NumViews, ppShaderResourceViews);
#endif
}
static inline void _sg_d3d11_VSSetSamplers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumSamplers, ID3D11SamplerState* const* ppSamplers) {
#if defined(__cplusplus)
self->VSSetSamplers(StartSlot, NumSamplers, ppSamplers);
#else
self->lpVtbl->VSSetSamplers(self, StartSlot, NumSamplers, ppSamplers);
#endif
}
static inline void _sg_d3d11_PSSetSamplers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumSamplers, ID3D11SamplerState* const* ppSamplers) {
#if defined(__cplusplus)
self->PSSetSamplers(StartSlot, NumSamplers, ppSamplers);
#else
self->lpVtbl->PSSetSamplers(self, StartSlot, NumSamplers, ppSamplers);
#endif
}
static inline HRESULT _sg_d3d11_CreateBuffer(ID3D11Device* self, const D3D11_BUFFER_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Buffer** ppBuffer) {
#if defined(__cplusplus)
return self->CreateBuffer(pDesc, pInitialData, ppBuffer);
#else
return self->lpVtbl->CreateBuffer(self, pDesc, pInitialData, ppBuffer);
#endif
}
static inline HRESULT _sg_d3d11_CreateTexture2D(ID3D11Device* self, const D3D11_TEXTURE2D_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Texture2D** ppTexture2D) {
#if defined(__cplusplus)
return self->CreateTexture2D(pDesc, pInitialData, ppTexture2D);
#else
return self->lpVtbl->CreateTexture2D(self, pDesc, pInitialData, ppTexture2D);
#endif
}
static inline HRESULT _sg_d3d11_CreateShaderResourceView(ID3D11Device* self, ID3D11Resource* pResource, const D3D11_SHADER_RESOURCE_VIEW_DESC* pDesc, ID3D11ShaderResourceView** ppSRView) {
#if defined(__cplusplus)
return self->CreateShaderResourceView(pResource, pDesc, ppSRView);
#else
return self->lpVtbl->CreateShaderResourceView(self, pResource, pDesc, ppSRView);
#endif
}
static inline void _sg_d3d11_GetResource(ID3D11View* self, ID3D11Resource** ppResource) {
#if defined(__cplusplus)
self->GetResource(ppResource);
#else
self->lpVtbl->GetResource(self, ppResource);
#endif
}
static inline HRESULT _sg_d3d11_CreateTexture3D(ID3D11Device* self, const D3D11_TEXTURE3D_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Texture3D** ppTexture3D) {
#if defined(__cplusplus)
return self->CreateTexture3D(pDesc, pInitialData, ppTexture3D);
#else
return self->lpVtbl->CreateTexture3D(self, pDesc, pInitialData, ppTexture3D);
#endif
}
static inline HRESULT _sg_d3d11_CreateSamplerState(ID3D11Device* self, const D3D11_SAMPLER_DESC* pSamplerDesc, ID3D11SamplerState** ppSamplerState) {
#if defined(__cplusplus)
return self->CreateSamplerState(pSamplerDesc, ppSamplerState);
#else
return self->lpVtbl->CreateSamplerState(self, pSamplerDesc, ppSamplerState);
#endif
}
static inline LPVOID _sg_d3d11_GetBufferPointer(ID3D10Blob* self) {
#if defined(__cplusplus)
return self->GetBufferPointer();
#else
return self->lpVtbl->GetBufferPointer(self);
#endif
}
static inline SIZE_T _sg_d3d11_GetBufferSize(ID3D10Blob* self) {
#if defined(__cplusplus)
return self->GetBufferSize();
#else
return self->lpVtbl->GetBufferSize(self);
#endif
}
static inline HRESULT _sg_d3d11_CreateVertexShader(ID3D11Device* self, const void* pShaderBytecode, SIZE_T BytecodeLength, ID3D11ClassLinkage* pClassLinkage, ID3D11VertexShader** ppVertexShader) {
#if defined(__cplusplus)
return self->CreateVertexShader(pShaderBytecode, BytecodeLength, pClassLinkage, ppVertexShader);
#else
return self->lpVtbl->CreateVertexShader(self, pShaderBytecode, BytecodeLength, pClassLinkage, ppVertexShader);
#endif
}
static inline HRESULT _sg_d3d11_CreatePixelShader(ID3D11Device* self, const void* pShaderBytecode, SIZE_T BytecodeLength, ID3D11ClassLinkage* pClassLinkage, ID3D11PixelShader** ppPixelShader) {
#if defined(__cplusplus)
return self->CreatePixelShader(pShaderBytecode, BytecodeLength, pClassLinkage, ppPixelShader);
#else
return self->lpVtbl->CreatePixelShader(self, pShaderBytecode, BytecodeLength, pClassLinkage, ppPixelShader);
#endif
}
static inline HRESULT _sg_d3d11_CreateInputLayout(ID3D11Device* self, const D3D11_INPUT_ELEMENT_DESC* pInputElementDescs, UINT NumElements, const void* pShaderBytecodeWithInputSignature, SIZE_T BytecodeLength, ID3D11InputLayout **ppInputLayout) {
#if defined(__cplusplus)
return self->CreateInputLayout(pInputElementDescs, NumElements, pShaderBytecodeWithInputSignature, BytecodeLength, ppInputLayout);
#else
return self->lpVtbl->CreateInputLayout(self, pInputElementDescs, NumElements, pShaderBytecodeWithInputSignature, BytecodeLength, ppInputLayout);
#endif
}
static inline HRESULT _sg_d3d11_CreateRasterizerState(ID3D11Device* self, const D3D11_RASTERIZER_DESC* pRasterizerDesc, ID3D11RasterizerState** ppRasterizerState) {
#if defined(__cplusplus)
return self->CreateRasterizerState(pRasterizerDesc, ppRasterizerState);
#else
return self->lpVtbl->CreateRasterizerState(self, pRasterizerDesc, ppRasterizerState);
#endif
}
static inline HRESULT _sg_d3d11_CreateDepthStencilState(ID3D11Device* self, const D3D11_DEPTH_STENCIL_DESC* pDepthStencilDesc, ID3D11DepthStencilState** ppDepthStencilState) {
#if defined(__cplusplus)
return self->CreateDepthStencilState(pDepthStencilDesc, ppDepthStencilState);
#else
return self->lpVtbl->CreateDepthStencilState(self, pDepthStencilDesc, ppDepthStencilState);
#endif
}
static inline HRESULT _sg_d3d11_CreateBlendState(ID3D11Device* self, const D3D11_BLEND_DESC* pBlendStateDesc, ID3D11BlendState** ppBlendState) {
#if defined(__cplusplus)
return self->CreateBlendState(pBlendStateDesc, ppBlendState);
#else
return self->lpVtbl->CreateBlendState(self, pBlendStateDesc, ppBlendState);
#endif
}
static inline HRESULT _sg_d3d11_CreateRenderTargetView(ID3D11Device* self, ID3D11Resource *pResource, const D3D11_RENDER_TARGET_VIEW_DESC* pDesc, ID3D11RenderTargetView** ppRTView) {
#if defined(__cplusplus)
return self->CreateRenderTargetView(pResource, pDesc, ppRTView);
#else
return self->lpVtbl->CreateRenderTargetView(self, pResource, pDesc, ppRTView);
#endif
}
static inline HRESULT _sg_d3d11_CreateDepthStencilView(ID3D11Device* self, ID3D11Resource* pResource, const D3D11_DEPTH_STENCIL_VIEW_DESC* pDesc, ID3D11DepthStencilView** ppDepthStencilView) {
#if defined(__cplusplus)
return self->CreateDepthStencilView(pResource, pDesc, ppDepthStencilView);
#else
return self->lpVtbl->CreateDepthStencilView(self, pResource, pDesc, ppDepthStencilView);
#endif
}
static inline void _sg_d3d11_RSSetViewports(ID3D11DeviceContext* self, UINT NumViewports, const D3D11_VIEWPORT* pViewports) {
#if defined(__cplusplus)
self->RSSetViewports(NumViewports, pViewports);
#else
self->lpVtbl->RSSetViewports(self, NumViewports, pViewports);
#endif
}
static inline void _sg_d3d11_RSSetScissorRects(ID3D11DeviceContext* self, UINT NumRects, const D3D11_RECT* pRects) {
#if defined(__cplusplus)
self->RSSetScissorRects(NumRects, pRects);
#else
self->lpVtbl->RSSetScissorRects(self, NumRects, pRects);
#endif
}
static inline void _sg_d3d11_ClearRenderTargetView(ID3D11DeviceContext* self, ID3D11RenderTargetView* pRenderTargetView, const FLOAT ColorRGBA[4]) {
#if defined(__cplusplus)
self->ClearRenderTargetView(pRenderTargetView, ColorRGBA);
#else
self->lpVtbl->ClearRenderTargetView(self, pRenderTargetView, ColorRGBA);
#endif
}
static inline void _sg_d3d11_ClearDepthStencilView(ID3D11DeviceContext* self, ID3D11DepthStencilView* pDepthStencilView, UINT ClearFlags, FLOAT Depth, UINT8 Stencil) {
#if defined(__cplusplus)
self->ClearDepthStencilView(pDepthStencilView, ClearFlags, Depth, Stencil);
#else
self->lpVtbl->ClearDepthStencilView(self, pDepthStencilView, ClearFlags, Depth, Stencil);
#endif
}
static inline void _sg_d3d11_ResolveSubresource(ID3D11DeviceContext* self, ID3D11Resource* pDstResource, UINT DstSubresource, ID3D11Resource* pSrcResource, UINT SrcSubresource, DXGI_FORMAT Format) {
#if defined(__cplusplus)
self->ResolveSubresource(pDstResource, DstSubresource, pSrcResource, SrcSubresource, Format);
#else
self->lpVtbl->ResolveSubresource(self, pDstResource, DstSubresource, pSrcResource, SrcSubresource, Format);
#endif
}
static inline void _sg_d3d11_IASetPrimitiveTopology(ID3D11DeviceContext* self, D3D11_PRIMITIVE_TOPOLOGY Topology) {
#if defined(__cplusplus)
self->IASetPrimitiveTopology(Topology);
#else
self->lpVtbl->IASetPrimitiveTopology(self, Topology);
#endif
}
static inline void _sg_d3d11_UpdateSubresource(ID3D11DeviceContext* self, ID3D11Resource* pDstResource, UINT DstSubresource, const D3D11_BOX* pDstBox, const void* pSrcData, UINT SrcRowPitch, UINT SrcDepthPitch) {
#if defined(__cplusplus)
self->UpdateSubresource(pDstResource, DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch);
#else
self->lpVtbl->UpdateSubresource(self, pDstResource, DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch);
#endif
}
static inline void _sg_d3d11_DrawIndexed(ID3D11DeviceContext* self, UINT IndexCount, UINT StartIndexLocation, INT BaseVertexLocation) {
#if defined(__cplusplus)
self->DrawIndexed(IndexCount, StartIndexLocation, BaseVertexLocation);
#else
self->lpVtbl->DrawIndexed(self, IndexCount, StartIndexLocation, BaseVertexLocation);
#endif
}
static inline void _sg_d3d11_DrawIndexedInstanced(ID3D11DeviceContext* self, UINT IndexCountPerInstance, UINT InstanceCount, UINT StartIndexLocation, INT BaseVertexLocation, UINT StartInstanceLocation) {
#if defined(__cplusplus)
self->DrawIndexedInstanced(IndexCountPerInstance, InstanceCount, StartIndexLocation, BaseVertexLocation, StartInstanceLocation);
#else
self->lpVtbl->DrawIndexedInstanced(self, IndexCountPerInstance, InstanceCount, StartIndexLocation, BaseVertexLocation, StartInstanceLocation);
#endif
}
static inline void _sg_d3d11_Draw(ID3D11DeviceContext* self, UINT VertexCount, UINT StartVertexLocation) {
#if defined(__cplusplus)
self->Draw(VertexCount, StartVertexLocation);
#else
self->lpVtbl->Draw(self, VertexCount, StartVertexLocation);
#endif
}
static inline void _sg_d3d11_DrawInstanced(ID3D11DeviceContext* self, UINT VertexCountPerInstance, UINT InstanceCount, UINT StartVertexLocation, UINT StartInstanceLocation) {
#if defined(__cplusplus)
self->DrawInstanced(VertexCountPerInstance, InstanceCount, StartVertexLocation, StartInstanceLocation);
#else
self->lpVtbl->DrawInstanced(self, VertexCountPerInstance, InstanceCount, StartVertexLocation, StartInstanceLocation);
#endif
}
static inline HRESULT _sg_d3d11_Map(ID3D11DeviceContext* self, ID3D11Resource* pResource, UINT Subresource, D3D11_MAP MapType, UINT MapFlags, D3D11_MAPPED_SUBRESOURCE* pMappedResource) {
#if defined(__cplusplus)
return self->Map(pResource, Subresource, MapType, MapFlags, pMappedResource);
#else
return self->lpVtbl->Map(self, pResource, Subresource, MapType, MapFlags, pMappedResource);
#endif
}
static inline void _sg_d3d11_Unmap(ID3D11DeviceContext* self, ID3D11Resource* pResource, UINT Subresource) {
#if defined(__cplusplus)
self->Unmap(pResource, Subresource);
#else
self->lpVtbl->Unmap(self, pResource, Subresource);
#endif
}
static inline void _sg_d3d11_ClearState(ID3D11DeviceContext* self) {
#if defined(__cplusplus)
self->ClearState();
#else
self->lpVtbl->ClearState(self);
#endif
}
//-- enum translation functions ------------------------------------------------
_SOKOL_PRIVATE D3D11_USAGE _sg_d3d11_usage(sg_usage usg) {
switch (usg) {
case SG_USAGE_IMMUTABLE:
return D3D11_USAGE_IMMUTABLE;
case SG_USAGE_DYNAMIC:
case SG_USAGE_STREAM:
return D3D11_USAGE_DYNAMIC;
default:
SOKOL_UNREACHABLE;
return (D3D11_USAGE) 0;
}
}
_SOKOL_PRIVATE UINT _sg_d3d11_buffer_bind_flags(sg_buffer_type t) {
switch (t) {
case SG_BUFFERTYPE_VERTEXBUFFER:
return D3D11_BIND_VERTEX_BUFFER;
case SG_BUFFERTYPE_INDEXBUFFER:
return D3D11_BIND_INDEX_BUFFER;
case SG_BUFFERTYPE_STORAGEBUFFER:
// FIXME: for compute shaders we'd want UNORDERED_ACCESS?
return D3D11_BIND_SHADER_RESOURCE;
default:
SOKOL_UNREACHABLE;
return 0;
}
}
_SOKOL_PRIVATE UINT _sg_d3d11_buffer_misc_flags(sg_buffer_type t) {
switch (t) {
case SG_BUFFERTYPE_VERTEXBUFFER:
case SG_BUFFERTYPE_INDEXBUFFER:
return 0;
case SG_BUFFERTYPE_STORAGEBUFFER:
return D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS;
default:
SOKOL_UNREACHABLE;
return 0;
}
}
_SOKOL_PRIVATE UINT _sg_d3d11_cpu_access_flags(sg_usage usg) {
switch (usg) {
case SG_USAGE_IMMUTABLE:
return 0;
case SG_USAGE_DYNAMIC:
case SG_USAGE_STREAM:
return D3D11_CPU_ACCESS_WRITE;
default:
SOKOL_UNREACHABLE;
return 0;
}
}
_SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_texture_pixel_format(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8: return DXGI_FORMAT_R8_UNORM;
case SG_PIXELFORMAT_R8SN: return DXGI_FORMAT_R8_SNORM;
case SG_PIXELFORMAT_R8UI: return DXGI_FORMAT_R8_UINT;
case SG_PIXELFORMAT_R8SI: return DXGI_FORMAT_R8_SINT;
case SG_PIXELFORMAT_R16: return DXGI_FORMAT_R16_UNORM;
case SG_PIXELFORMAT_R16SN: return DXGI_FORMAT_R16_SNORM;
case SG_PIXELFORMAT_R16UI: return DXGI_FORMAT_R16_UINT;
case SG_PIXELFORMAT_R16SI: return DXGI_FORMAT_R16_SINT;
case SG_PIXELFORMAT_R16F: return DXGI_FORMAT_R16_FLOAT;
case SG_PIXELFORMAT_RG8: return DXGI_FORMAT_R8G8_UNORM;
case SG_PIXELFORMAT_RG8SN: return DXGI_FORMAT_R8G8_SNORM;
case SG_PIXELFORMAT_RG8UI: return DXGI_FORMAT_R8G8_UINT;
case SG_PIXELFORMAT_RG8SI: return DXGI_FORMAT_R8G8_SINT;
case SG_PIXELFORMAT_R32UI: return DXGI_FORMAT_R32_UINT;
case SG_PIXELFORMAT_R32SI: return DXGI_FORMAT_R32_SINT;
case SG_PIXELFORMAT_R32F: return DXGI_FORMAT_R32_FLOAT;
case SG_PIXELFORMAT_RG16: return DXGI_FORMAT_R16G16_UNORM;
case SG_PIXELFORMAT_RG16SN: return DXGI_FORMAT_R16G16_SNORM;
case SG_PIXELFORMAT_RG16UI: return DXGI_FORMAT_R16G16_UINT;
case SG_PIXELFORMAT_RG16SI: return DXGI_FORMAT_R16G16_SINT;
case SG_PIXELFORMAT_RG16F: return DXGI_FORMAT_R16G16_FLOAT;
case SG_PIXELFORMAT_RGBA8: return DXGI_FORMAT_R8G8B8A8_UNORM;
case SG_PIXELFORMAT_SRGB8A8: return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
case SG_PIXELFORMAT_RGBA8SN: return DXGI_FORMAT_R8G8B8A8_SNORM;
case SG_PIXELFORMAT_RGBA8UI: return DXGI_FORMAT_R8G8B8A8_UINT;
case SG_PIXELFORMAT_RGBA8SI: return DXGI_FORMAT_R8G8B8A8_SINT;
case SG_PIXELFORMAT_BGRA8: return DXGI_FORMAT_B8G8R8A8_UNORM;
case SG_PIXELFORMAT_RGB10A2: return DXGI_FORMAT_R10G10B10A2_UNORM;
case SG_PIXELFORMAT_RG11B10F: return DXGI_FORMAT_R11G11B10_FLOAT;
case SG_PIXELFORMAT_RGB9E5: return DXGI_FORMAT_R9G9B9E5_SHAREDEXP;
case SG_PIXELFORMAT_RG32UI: return DXGI_FORMAT_R32G32_UINT;
case SG_PIXELFORMAT_RG32SI: return DXGI_FORMAT_R32G32_SINT;
case SG_PIXELFORMAT_RG32F: return DXGI_FORMAT_R32G32_FLOAT;
case SG_PIXELFORMAT_RGBA16: return DXGI_FORMAT_R16G16B16A16_UNORM;
case SG_PIXELFORMAT_RGBA16SN: return DXGI_FORMAT_R16G16B16A16_SNORM;
case SG_PIXELFORMAT_RGBA16UI: return DXGI_FORMAT_R16G16B16A16_UINT;
case SG_PIXELFORMAT_RGBA16SI: return DXGI_FORMAT_R16G16B16A16_SINT;
case SG_PIXELFORMAT_RGBA16F: return DXGI_FORMAT_R16G16B16A16_FLOAT;
case SG_PIXELFORMAT_RGBA32UI: return DXGI_FORMAT_R32G32B32A32_UINT;
case SG_PIXELFORMAT_RGBA32SI: return DXGI_FORMAT_R32G32B32A32_SINT;
case SG_PIXELFORMAT_RGBA32F: return DXGI_FORMAT_R32G32B32A32_FLOAT;
case SG_PIXELFORMAT_DEPTH: return DXGI_FORMAT_R32_TYPELESS;
case SG_PIXELFORMAT_DEPTH_STENCIL: return DXGI_FORMAT_R24G8_TYPELESS;
case SG_PIXELFORMAT_BC1_RGBA: return DXGI_FORMAT_BC1_UNORM;
case SG_PIXELFORMAT_BC2_RGBA: return DXGI_FORMAT_BC2_UNORM;
case SG_PIXELFORMAT_BC3_RGBA: return DXGI_FORMAT_BC3_UNORM;
case SG_PIXELFORMAT_BC3_SRGBA: return DXGI_FORMAT_BC3_UNORM_SRGB;
case SG_PIXELFORMAT_BC4_R: return DXGI_FORMAT_BC4_UNORM;
case SG_PIXELFORMAT_BC4_RSN: return DXGI_FORMAT_BC4_SNORM;
case SG_PIXELFORMAT_BC5_RG: return DXGI_FORMAT_BC5_UNORM;
case SG_PIXELFORMAT_BC5_RGSN: return DXGI_FORMAT_BC5_SNORM;
case SG_PIXELFORMAT_BC6H_RGBF: return DXGI_FORMAT_BC6H_SF16;
case SG_PIXELFORMAT_BC6H_RGBUF: return DXGI_FORMAT_BC6H_UF16;
case SG_PIXELFORMAT_BC7_RGBA: return DXGI_FORMAT_BC7_UNORM;
case SG_PIXELFORMAT_BC7_SRGBA: return DXGI_FORMAT_BC7_UNORM_SRGB;
default: return DXGI_FORMAT_UNKNOWN;
};
}
_SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_srv_pixel_format(sg_pixel_format fmt) {
if (fmt == SG_PIXELFORMAT_DEPTH) {
return DXGI_FORMAT_R32_FLOAT;
} else if (fmt == SG_PIXELFORMAT_DEPTH_STENCIL) {
return DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
} else {
return _sg_d3d11_texture_pixel_format(fmt);
}
}
_SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_dsv_pixel_format(sg_pixel_format fmt) {
if (fmt == SG_PIXELFORMAT_DEPTH) {
return DXGI_FORMAT_D32_FLOAT;
} else if (fmt == SG_PIXELFORMAT_DEPTH_STENCIL) {
return DXGI_FORMAT_D24_UNORM_S8_UINT;
} else {
return _sg_d3d11_texture_pixel_format(fmt);
}
}
_SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_rtv_pixel_format(sg_pixel_format fmt) {
if (fmt == SG_PIXELFORMAT_DEPTH) {
return DXGI_FORMAT_R32_FLOAT;
} else if (fmt == SG_PIXELFORMAT_DEPTH_STENCIL) {
return DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
} else {
return _sg_d3d11_texture_pixel_format(fmt);
}
}
_SOKOL_PRIVATE D3D11_PRIMITIVE_TOPOLOGY _sg_d3d11_primitive_topology(sg_primitive_type prim_type) {
switch (prim_type) {
case SG_PRIMITIVETYPE_POINTS: return D3D11_PRIMITIVE_TOPOLOGY_POINTLIST;
case SG_PRIMITIVETYPE_LINES: return D3D11_PRIMITIVE_TOPOLOGY_LINELIST;
case SG_PRIMITIVETYPE_LINE_STRIP: return D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP;
case SG_PRIMITIVETYPE_TRIANGLES: return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
default: SOKOL_UNREACHABLE; return (D3D11_PRIMITIVE_TOPOLOGY) 0;
}
}
_SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_index_format(sg_index_type index_type) {
switch (index_type) {
case SG_INDEXTYPE_NONE: return DXGI_FORMAT_UNKNOWN;
case SG_INDEXTYPE_UINT16: return DXGI_FORMAT_R16_UINT;
case SG_INDEXTYPE_UINT32: return DXGI_FORMAT_R32_UINT;
default: SOKOL_UNREACHABLE; return (DXGI_FORMAT) 0;
}
}
_SOKOL_PRIVATE D3D11_FILTER _sg_d3d11_filter(sg_filter min_f, sg_filter mag_f, sg_filter mipmap_f, bool comparison, uint32_t max_anisotropy) {
uint32_t d3d11_filter = 0;
if (max_anisotropy > 1) {
// D3D11_FILTER_ANISOTROPIC = 0x55,
d3d11_filter |= 0x55;
} else {
// D3D11_FILTER_MIN_MAG_MIP_POINT = 0,
// D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR = 0x1,
// D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT = 0x4,
// D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR = 0x5,
// D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT = 0x10,
// D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR = 0x11,
// D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT = 0x14,
// D3D11_FILTER_MIN_MAG_MIP_LINEAR = 0x15,
if (mipmap_f == SG_FILTER_LINEAR) {
d3d11_filter |= 0x01;
}
if (mag_f == SG_FILTER_LINEAR) {
d3d11_filter |= 0x04;
}
if (min_f == SG_FILTER_LINEAR) {
d3d11_filter |= 0x10;
}
}
// D3D11_FILTER_COMPARISON_MIN_MAG_MIP_POINT = 0x80,
// D3D11_FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR = 0x81,
// D3D11_FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT = 0x84,
// D3D11_FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR = 0x85,
// D3D11_FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT = 0x90,
// D3D11_FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR = 0x91,
// D3D11_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT = 0x94,
// D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR = 0x95,
// D3D11_FILTER_COMPARISON_ANISOTROPIC = 0xd5,
if (comparison) {
d3d11_filter |= 0x80;
}
return (D3D11_FILTER)d3d11_filter;
}
_SOKOL_PRIVATE D3D11_TEXTURE_ADDRESS_MODE _sg_d3d11_address_mode(sg_wrap m) {
switch (m) {
case SG_WRAP_REPEAT: return D3D11_TEXTURE_ADDRESS_WRAP;
case SG_WRAP_CLAMP_TO_EDGE: return D3D11_TEXTURE_ADDRESS_CLAMP;
case SG_WRAP_CLAMP_TO_BORDER: return D3D11_TEXTURE_ADDRESS_BORDER;
case SG_WRAP_MIRRORED_REPEAT: return D3D11_TEXTURE_ADDRESS_MIRROR;
default: SOKOL_UNREACHABLE; return (D3D11_TEXTURE_ADDRESS_MODE) 0;
}
}
_SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_vertex_format(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_FLOAT: return DXGI_FORMAT_R32_FLOAT;
case SG_VERTEXFORMAT_FLOAT2: return DXGI_FORMAT_R32G32_FLOAT;
case SG_VERTEXFORMAT_FLOAT3: return DXGI_FORMAT_R32G32B32_FLOAT;
case SG_VERTEXFORMAT_FLOAT4: return DXGI_FORMAT_R32G32B32A32_FLOAT;
case SG_VERTEXFORMAT_BYTE4: return DXGI_FORMAT_R8G8B8A8_SINT;
case SG_VERTEXFORMAT_BYTE4N: return DXGI_FORMAT_R8G8B8A8_SNORM;
case SG_VERTEXFORMAT_UBYTE4: return DXGI_FORMAT_R8G8B8A8_UINT;
case SG_VERTEXFORMAT_UBYTE4N: return DXGI_FORMAT_R8G8B8A8_UNORM;
case SG_VERTEXFORMAT_SHORT2: return DXGI_FORMAT_R16G16_SINT;
case SG_VERTEXFORMAT_SHORT2N: return DXGI_FORMAT_R16G16_SNORM;
case SG_VERTEXFORMAT_USHORT2N: return DXGI_FORMAT_R16G16_UNORM;
case SG_VERTEXFORMAT_SHORT4: return DXGI_FORMAT_R16G16B16A16_SINT;
case SG_VERTEXFORMAT_SHORT4N: return DXGI_FORMAT_R16G16B16A16_SNORM;
case SG_VERTEXFORMAT_USHORT4N: return DXGI_FORMAT_R16G16B16A16_UNORM;
case SG_VERTEXFORMAT_UINT10_N2: return DXGI_FORMAT_R10G10B10A2_UNORM;
case SG_VERTEXFORMAT_HALF2: return DXGI_FORMAT_R16G16_FLOAT;
case SG_VERTEXFORMAT_HALF4: return DXGI_FORMAT_R16G16B16A16_FLOAT;
default: SOKOL_UNREACHABLE; return (DXGI_FORMAT) 0;
}
}
_SOKOL_PRIVATE D3D11_INPUT_CLASSIFICATION _sg_d3d11_input_classification(sg_vertex_step step) {
switch (step) {
case SG_VERTEXSTEP_PER_VERTEX: return D3D11_INPUT_PER_VERTEX_DATA;
case SG_VERTEXSTEP_PER_INSTANCE: return D3D11_INPUT_PER_INSTANCE_DATA;
default: SOKOL_UNREACHABLE; return (D3D11_INPUT_CLASSIFICATION) 0;
}
}
_SOKOL_PRIVATE D3D11_CULL_MODE _sg_d3d11_cull_mode(sg_cull_mode m) {
switch (m) {
case SG_CULLMODE_NONE: return D3D11_CULL_NONE;
case SG_CULLMODE_FRONT: return D3D11_CULL_FRONT;
case SG_CULLMODE_BACK: return D3D11_CULL_BACK;
default: SOKOL_UNREACHABLE; return (D3D11_CULL_MODE) 0;
}
}
_SOKOL_PRIVATE D3D11_COMPARISON_FUNC _sg_d3d11_compare_func(sg_compare_func f) {
switch (f) {
case SG_COMPAREFUNC_NEVER: return D3D11_COMPARISON_NEVER;
case SG_COMPAREFUNC_LESS: return D3D11_COMPARISON_LESS;
case SG_COMPAREFUNC_EQUAL: return D3D11_COMPARISON_EQUAL;
case SG_COMPAREFUNC_LESS_EQUAL: return D3D11_COMPARISON_LESS_EQUAL;
case SG_COMPAREFUNC_GREATER: return D3D11_COMPARISON_GREATER;
case SG_COMPAREFUNC_NOT_EQUAL: return D3D11_COMPARISON_NOT_EQUAL;
case SG_COMPAREFUNC_GREATER_EQUAL: return D3D11_COMPARISON_GREATER_EQUAL;
case SG_COMPAREFUNC_ALWAYS: return D3D11_COMPARISON_ALWAYS;
default: SOKOL_UNREACHABLE; return (D3D11_COMPARISON_FUNC) 0;
}
}
_SOKOL_PRIVATE D3D11_STENCIL_OP _sg_d3d11_stencil_op(sg_stencil_op op) {
switch (op) {
case SG_STENCILOP_KEEP: return D3D11_STENCIL_OP_KEEP;
case SG_STENCILOP_ZERO: return D3D11_STENCIL_OP_ZERO;
case SG_STENCILOP_REPLACE: return D3D11_STENCIL_OP_REPLACE;
case SG_STENCILOP_INCR_CLAMP: return D3D11_STENCIL_OP_INCR_SAT;
case SG_STENCILOP_DECR_CLAMP: return D3D11_STENCIL_OP_DECR_SAT;
case SG_STENCILOP_INVERT: return D3D11_STENCIL_OP_INVERT;
case SG_STENCILOP_INCR_WRAP: return D3D11_STENCIL_OP_INCR;
case SG_STENCILOP_DECR_WRAP: return D3D11_STENCIL_OP_DECR;
default: SOKOL_UNREACHABLE; return (D3D11_STENCIL_OP) 0;
}
}
_SOKOL_PRIVATE D3D11_BLEND _sg_d3d11_blend_factor(sg_blend_factor f) {
switch (f) {
case SG_BLENDFACTOR_ZERO: return D3D11_BLEND_ZERO;
case SG_BLENDFACTOR_ONE: return D3D11_BLEND_ONE;
case SG_BLENDFACTOR_SRC_COLOR: return D3D11_BLEND_SRC_COLOR;
case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return D3D11_BLEND_INV_SRC_COLOR;
case SG_BLENDFACTOR_SRC_ALPHA: return D3D11_BLEND_SRC_ALPHA;
case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return D3D11_BLEND_INV_SRC_ALPHA;
case SG_BLENDFACTOR_DST_COLOR: return D3D11_BLEND_DEST_COLOR;
case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return D3D11_BLEND_INV_DEST_COLOR;
case SG_BLENDFACTOR_DST_ALPHA: return D3D11_BLEND_DEST_ALPHA;
case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return D3D11_BLEND_INV_DEST_ALPHA;
case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return D3D11_BLEND_SRC_ALPHA_SAT;
case SG_BLENDFACTOR_BLEND_COLOR: return D3D11_BLEND_BLEND_FACTOR;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return D3D11_BLEND_INV_BLEND_FACTOR;
case SG_BLENDFACTOR_BLEND_ALPHA: return D3D11_BLEND_BLEND_FACTOR;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return D3D11_BLEND_INV_BLEND_FACTOR;
default: SOKOL_UNREACHABLE; return (D3D11_BLEND) 0;
}
}
_SOKOL_PRIVATE D3D11_BLEND_OP _sg_d3d11_blend_op(sg_blend_op op) {
switch (op) {
case SG_BLENDOP_ADD: return D3D11_BLEND_OP_ADD;
case SG_BLENDOP_SUBTRACT: return D3D11_BLEND_OP_SUBTRACT;
case SG_BLENDOP_REVERSE_SUBTRACT: return D3D11_BLEND_OP_REV_SUBTRACT;
default: SOKOL_UNREACHABLE; return (D3D11_BLEND_OP) 0;
}
}
_SOKOL_PRIVATE UINT8 _sg_d3d11_color_write_mask(sg_color_mask m) {
UINT8 res = 0;
if (m & SG_COLORMASK_R) {
res |= D3D11_COLOR_WRITE_ENABLE_RED;
}
if (m & SG_COLORMASK_G) {
res |= D3D11_COLOR_WRITE_ENABLE_GREEN;
}
if (m & SG_COLORMASK_B) {
res |= D3D11_COLOR_WRITE_ENABLE_BLUE;
}
if (m & SG_COLORMASK_A) {
res |= D3D11_COLOR_WRITE_ENABLE_ALPHA;
}
return res;
}
_SOKOL_PRIVATE UINT _sg_d3d11_dxgi_fmt_caps(DXGI_FORMAT dxgi_fmt) {
UINT dxgi_fmt_caps = 0;
if (dxgi_fmt != DXGI_FORMAT_UNKNOWN) {
HRESULT hr = _sg_d3d11_CheckFormatSupport(_sg.d3d11.dev, dxgi_fmt, &dxgi_fmt_caps);
SOKOL_ASSERT(SUCCEEDED(hr) || (E_FAIL == hr));
if (!SUCCEEDED(hr)) {
dxgi_fmt_caps = 0;
}
}
return dxgi_fmt_caps;
}
// see: https://docs.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-limits#resource-limits-for-feature-level-11-hardware
_SOKOL_PRIVATE void _sg_d3d11_init_caps(void) {
_sg.backend = SG_BACKEND_D3D11;
_sg.features.origin_top_left = true;
_sg.features.image_clamp_to_border = true;
_sg.features.mrt_independent_blend_state = true;
_sg.features.mrt_independent_write_mask = true;
_sg.features.storage_buffer = true;
_sg.limits.max_image_size_2d = 16 * 1024;
_sg.limits.max_image_size_cube = 16 * 1024;
_sg.limits.max_image_size_3d = 2 * 1024;
_sg.limits.max_image_size_array = 16 * 1024;
_sg.limits.max_image_array_layers = 2 * 1024;
_sg.limits.max_vertex_attrs = SG_MAX_VERTEX_ATTRIBUTES;
// see: https://docs.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_format_support
for (int fmt = (SG_PIXELFORMAT_NONE+1); fmt < _SG_PIXELFORMAT_NUM; fmt++) {
const UINT srv_dxgi_fmt_caps = _sg_d3d11_dxgi_fmt_caps(_sg_d3d11_srv_pixel_format((sg_pixel_format)fmt));
const UINT rtv_dxgi_fmt_caps = _sg_d3d11_dxgi_fmt_caps(_sg_d3d11_rtv_pixel_format((sg_pixel_format)fmt));
const UINT dsv_dxgi_fmt_caps = _sg_d3d11_dxgi_fmt_caps(_sg_d3d11_dsv_pixel_format((sg_pixel_format)fmt));
_sg_pixelformat_info_t* info = &_sg.formats[fmt];
const bool render = 0 != (rtv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_RENDER_TARGET);
const bool depth = 0 != (dsv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
info->sample = 0 != (srv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_TEXTURE2D);
info->filter = 0 != (srv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE);
info->render = render || depth;
info->blend = 0 != (rtv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_BLENDABLE);
info->msaa = 0 != (rtv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET);
info->depth = depth;
}
}
_SOKOL_PRIVATE void _sg_d3d11_setup_backend(const sg_desc* desc) {
// assume _sg.d3d11 already is zero-initialized
SOKOL_ASSERT(desc);
SOKOL_ASSERT(desc->environment.d3d11.device);
SOKOL_ASSERT(desc->environment.d3d11.device_context);
_sg.d3d11.valid = true;
_sg.d3d11.dev = (ID3D11Device*) desc->environment.d3d11.device;
_sg.d3d11.ctx = (ID3D11DeviceContext*) desc->environment.d3d11.device_context;
_sg_d3d11_init_caps();
}
_SOKOL_PRIVATE void _sg_d3d11_discard_backend(void) {
SOKOL_ASSERT(_sg.d3d11.valid);
_sg.d3d11.valid = false;
}
_SOKOL_PRIVATE void _sg_d3d11_clear_state(void) {
// clear all the device context state, so that resource refs don't keep stuck in the d3d device context
_sg_d3d11_ClearState(_sg.d3d11.ctx);
}
_SOKOL_PRIVATE void _sg_d3d11_reset_state_cache(void) {
// just clear the d3d11 device context state
_sg_d3d11_clear_state();
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && desc);
SOKOL_ASSERT(!buf->d3d11.buf);
const bool injected = (0 != desc->d3d11_buffer);
if (injected) {
buf->d3d11.buf = (ID3D11Buffer*) desc->d3d11_buffer;
_sg_d3d11_AddRef(buf->d3d11.buf);
// FIXME: for storage buffers also need to inject resource view
} else {
D3D11_BUFFER_DESC d3d11_buf_desc;
_sg_clear(&d3d11_buf_desc, sizeof(d3d11_buf_desc));
d3d11_buf_desc.ByteWidth = (UINT)buf->cmn.size;
d3d11_buf_desc.Usage = _sg_d3d11_usage(buf->cmn.usage);
d3d11_buf_desc.BindFlags = _sg_d3d11_buffer_bind_flags(buf->cmn.type);
d3d11_buf_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(buf->cmn.usage);
d3d11_buf_desc.MiscFlags = _sg_d3d11_buffer_misc_flags(buf->cmn.type);
D3D11_SUBRESOURCE_DATA* init_data_ptr = 0;
D3D11_SUBRESOURCE_DATA init_data;
_sg_clear(&init_data, sizeof(init_data));
if (buf->cmn.usage == SG_USAGE_IMMUTABLE) {
SOKOL_ASSERT(desc->data.ptr);
init_data.pSysMem = desc->data.ptr;
init_data_ptr = &init_data;
}
HRESULT hr = _sg_d3d11_CreateBuffer(_sg.d3d11.dev, &d3d11_buf_desc, init_data_ptr, &buf->d3d11.buf);
if (!(SUCCEEDED(hr) && buf->d3d11.buf)) {
_SG_ERROR(D3D11_CREATE_BUFFER_FAILED);
return SG_RESOURCESTATE_FAILED;
}
// for storage buffers need to create a view object
if (buf->cmn.type == SG_BUFFERTYPE_STORAGEBUFFER) {
// FIXME: currently only shader-resource-view, in future also UAV
// storage buffer size must be multiple of 4
SOKOL_ASSERT(_sg_multiple_u64(buf->cmn.size, 4));
D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc;
_sg_clear(&d3d11_srv_desc, sizeof(d3d11_srv_desc));
d3d11_srv_desc.Format = DXGI_FORMAT_R32_TYPELESS;
d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX;
d3d11_srv_desc.BufferEx.FirstElement = 0;
d3d11_srv_desc.BufferEx.NumElements = buf->cmn.size / 4;
d3d11_srv_desc.BufferEx.Flags = D3D11_BUFFEREX_SRV_FLAG_RAW;
hr = _sg_d3d11_CreateShaderResourceView(_sg.d3d11.dev, (ID3D11Resource*)buf->d3d11.buf, &d3d11_srv_desc, &buf->d3d11.srv);
if (!(SUCCEEDED(hr) && buf->d3d11.srv)) {
_SG_ERROR(D3D11_CREATE_BUFFER_SRV_FAILED);
return SG_RESOURCESTATE_FAILED;
}
}
_sg_d3d11_setlabel(buf->d3d11.buf, desc->label);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_d3d11_discard_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
if (buf->d3d11.buf) {
_sg_d3d11_Release(buf->d3d11.buf);
}
if (buf->d3d11.srv) {
_sg_d3d11_Release(buf->d3d11.srv);
}
}
_SOKOL_PRIVATE void _sg_d3d11_fill_subres_data(const _sg_image_t* img, const sg_image_data* data) {
const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1;
const int num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices:1;
int subres_index = 0;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int slice_index = 0; slice_index < num_slices; slice_index++) {
for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++, subres_index++) {
SOKOL_ASSERT(subres_index < (SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS));
D3D11_SUBRESOURCE_DATA* subres_data = &_sg.d3d11.subres_data[subres_index];
const int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index);
const int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index);
const sg_range* subimg_data = &(data->subimage[face_index][mip_index]);
const size_t slice_size = subimg_data->size / (size_t)num_slices;
const size_t slice_offset = slice_size * (size_t)slice_index;
const uint8_t* ptr = (const uint8_t*) subimg_data->ptr;
subres_data->pSysMem = ptr + slice_offset;
subres_data->SysMemPitch = (UINT)_sg_row_pitch(img->cmn.pixel_format, mip_width, 1);
if (img->cmn.type == SG_IMAGETYPE_3D) {
// FIXME? const int mip_depth = _sg_miplevel_dim(img->depth, mip_index);
subres_data->SysMemSlicePitch = (UINT)_sg_surface_pitch(img->cmn.pixel_format, mip_width, mip_height, 1);
} else {
subres_data->SysMemSlicePitch = 0;
}
}
}
}
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && desc);
SOKOL_ASSERT((0 == img->d3d11.tex2d) && (0 == img->d3d11.tex3d) && (0 == img->d3d11.res) && (0 == img->d3d11.srv));
HRESULT hr;
const bool injected = (0 != desc->d3d11_texture);
const bool msaa = (img->cmn.sample_count > 1);
img->d3d11.format = _sg_d3d11_texture_pixel_format(img->cmn.pixel_format);
if (img->d3d11.format == DXGI_FORMAT_UNKNOWN) {
_SG_ERROR(D3D11_CREATE_2D_TEXTURE_UNSUPPORTED_PIXEL_FORMAT);
return SG_RESOURCESTATE_FAILED;
}
// prepare initial content pointers
D3D11_SUBRESOURCE_DATA* init_data = 0;
if (!injected && (img->cmn.usage == SG_USAGE_IMMUTABLE) && !img->cmn.render_target) {
_sg_d3d11_fill_subres_data(img, &desc->data);
init_data = _sg.d3d11.subres_data;
}
if (img->cmn.type != SG_IMAGETYPE_3D) {
// 2D-, cube- or array-texture
// first check for injected texture and/or resource view
if (injected) {
img->d3d11.tex2d = (ID3D11Texture2D*) desc->d3d11_texture;
_sg_d3d11_AddRef(img->d3d11.tex2d);
img->d3d11.srv = (ID3D11ShaderResourceView*) desc->d3d11_shader_resource_view;
if (img->d3d11.srv) {
_sg_d3d11_AddRef(img->d3d11.srv);
}
} else {
// if not injected, create 2D texture
D3D11_TEXTURE2D_DESC d3d11_tex_desc;
_sg_clear(&d3d11_tex_desc, sizeof(d3d11_tex_desc));
d3d11_tex_desc.Width = (UINT)img->cmn.width;
d3d11_tex_desc.Height = (UINT)img->cmn.height;
d3d11_tex_desc.MipLevels = (UINT)img->cmn.num_mipmaps;
switch (img->cmn.type) {
case SG_IMAGETYPE_ARRAY: d3d11_tex_desc.ArraySize = (UINT)img->cmn.num_slices; break;
case SG_IMAGETYPE_CUBE: d3d11_tex_desc.ArraySize = 6; break;
default: d3d11_tex_desc.ArraySize = 1; break;
}
d3d11_tex_desc.Format = img->d3d11.format;
if (img->cmn.render_target) {
d3d11_tex_desc.Usage = D3D11_USAGE_DEFAULT;
if (_sg_is_depth_or_depth_stencil_format(img->cmn.pixel_format)) {
d3d11_tex_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
} else {
d3d11_tex_desc.BindFlags = D3D11_BIND_RENDER_TARGET;
}
if (!msaa) {
d3d11_tex_desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
}
d3d11_tex_desc.CPUAccessFlags = 0;
} else {
d3d11_tex_desc.Usage = _sg_d3d11_usage(img->cmn.usage);
d3d11_tex_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
d3d11_tex_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(img->cmn.usage);
}
d3d11_tex_desc.SampleDesc.Count = (UINT)img->cmn.sample_count;
d3d11_tex_desc.SampleDesc.Quality = (UINT) (msaa ? D3D11_STANDARD_MULTISAMPLE_PATTERN : 0);
d3d11_tex_desc.MiscFlags = (img->cmn.type == SG_IMAGETYPE_CUBE) ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0;
hr = _sg_d3d11_CreateTexture2D(_sg.d3d11.dev, &d3d11_tex_desc, init_data, &img->d3d11.tex2d);
if (!(SUCCEEDED(hr) && img->d3d11.tex2d)) {
_SG_ERROR(D3D11_CREATE_2D_TEXTURE_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(img->d3d11.tex2d, desc->label);
// create shader-resource-view for 2D texture
// FIXME: currently we don't support setting MSAA texture as shader resource
if (!msaa) {
D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc;
_sg_clear(&d3d11_srv_desc, sizeof(d3d11_srv_desc));
d3d11_srv_desc.Format = _sg_d3d11_srv_pixel_format(img->cmn.pixel_format);
switch (img->cmn.type) {
case SG_IMAGETYPE_2D:
d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
d3d11_srv_desc.Texture2D.MipLevels = (UINT)img->cmn.num_mipmaps;
break;
case SG_IMAGETYPE_CUBE:
d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
d3d11_srv_desc.TextureCube.MipLevels = (UINT)img->cmn.num_mipmaps;
break;
case SG_IMAGETYPE_ARRAY:
d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
d3d11_srv_desc.Texture2DArray.MipLevels = (UINT)img->cmn.num_mipmaps;
d3d11_srv_desc.Texture2DArray.ArraySize = (UINT)img->cmn.num_slices;
break;
default:
SOKOL_UNREACHABLE; break;
}
hr = _sg_d3d11_CreateShaderResourceView(_sg.d3d11.dev, (ID3D11Resource*)img->d3d11.tex2d, &d3d11_srv_desc, &img->d3d11.srv);
if (!(SUCCEEDED(hr) && img->d3d11.srv)) {
_SG_ERROR(D3D11_CREATE_2D_SRV_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(img->d3d11.srv, desc->label);
}
}
SOKOL_ASSERT(img->d3d11.tex2d);
img->d3d11.res = (ID3D11Resource*)img->d3d11.tex2d;
_sg_d3d11_AddRef(img->d3d11.res);
} else {
// 3D texture - same procedure, first check if injected, than create non-injected
if (injected) {
img->d3d11.tex3d = (ID3D11Texture3D*) desc->d3d11_texture;
_sg_d3d11_AddRef(img->d3d11.tex3d);
img->d3d11.srv = (ID3D11ShaderResourceView*) desc->d3d11_shader_resource_view;
if (img->d3d11.srv) {
_sg_d3d11_AddRef(img->d3d11.srv);
}
} else {
// not injected, create 3d texture
D3D11_TEXTURE3D_DESC d3d11_tex_desc;
_sg_clear(&d3d11_tex_desc, sizeof(d3d11_tex_desc));
d3d11_tex_desc.Width = (UINT)img->cmn.width;
d3d11_tex_desc.Height = (UINT)img->cmn.height;
d3d11_tex_desc.Depth = (UINT)img->cmn.num_slices;
d3d11_tex_desc.MipLevels = (UINT)img->cmn.num_mipmaps;
d3d11_tex_desc.Format = img->d3d11.format;
if (img->cmn.render_target) {
d3d11_tex_desc.Usage = D3D11_USAGE_DEFAULT;
d3d11_tex_desc.BindFlags = D3D11_BIND_RENDER_TARGET;
d3d11_tex_desc.CPUAccessFlags = 0;
} else {
d3d11_tex_desc.Usage = _sg_d3d11_usage(img->cmn.usage);
d3d11_tex_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
d3d11_tex_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(img->cmn.usage);
}
if (img->d3d11.format == DXGI_FORMAT_UNKNOWN) {
_SG_ERROR(D3D11_CREATE_3D_TEXTURE_UNSUPPORTED_PIXEL_FORMAT);
return SG_RESOURCESTATE_FAILED;
}
hr = _sg_d3d11_CreateTexture3D(_sg.d3d11.dev, &d3d11_tex_desc, init_data, &img->d3d11.tex3d);
if (!(SUCCEEDED(hr) && img->d3d11.tex3d)) {
_SG_ERROR(D3D11_CREATE_3D_TEXTURE_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(img->d3d11.tex3d, desc->label);
// create shader-resource-view for 3D texture
if (!msaa) {
D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc;
_sg_clear(&d3d11_srv_desc, sizeof(d3d11_srv_desc));
d3d11_srv_desc.Format = _sg_d3d11_srv_pixel_format(img->cmn.pixel_format);
d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
d3d11_srv_desc.Texture3D.MipLevels = (UINT)img->cmn.num_mipmaps;
hr = _sg_d3d11_CreateShaderResourceView(_sg.d3d11.dev, (ID3D11Resource*)img->d3d11.tex3d, &d3d11_srv_desc, &img->d3d11.srv);
if (!(SUCCEEDED(hr) && img->d3d11.srv)) {
_SG_ERROR(D3D11_CREATE_3D_SRV_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(img->d3d11.srv, desc->label);
}
}
SOKOL_ASSERT(img->d3d11.tex3d);
img->d3d11.res = (ID3D11Resource*)img->d3d11.tex3d;
_sg_d3d11_AddRef(img->d3d11.res);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_d3d11_discard_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
if (img->d3d11.tex2d) {
_sg_d3d11_Release(img->d3d11.tex2d);
}
if (img->d3d11.tex3d) {
_sg_d3d11_Release(img->d3d11.tex3d);
}
if (img->d3d11.res) {
_sg_d3d11_Release(img->d3d11.res);
}
if (img->d3d11.srv) {
_sg_d3d11_Release(img->d3d11.srv);
}
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) {
SOKOL_ASSERT(smp && desc);
SOKOL_ASSERT(0 == smp->d3d11.smp);
const bool injected = (0 != desc->d3d11_sampler);
if (injected) {
smp->d3d11.smp = (ID3D11SamplerState*)desc->d3d11_sampler;
_sg_d3d11_AddRef(smp->d3d11.smp);
} else {
D3D11_SAMPLER_DESC d3d11_smp_desc;
_sg_clear(&d3d11_smp_desc, sizeof(d3d11_smp_desc));
d3d11_smp_desc.Filter = _sg_d3d11_filter(desc->min_filter, desc->mag_filter, desc->mipmap_filter, desc->compare != SG_COMPAREFUNC_NEVER, desc->max_anisotropy);
d3d11_smp_desc.AddressU = _sg_d3d11_address_mode(desc->wrap_u);
d3d11_smp_desc.AddressV = _sg_d3d11_address_mode(desc->wrap_v);
d3d11_smp_desc.AddressW = _sg_d3d11_address_mode(desc->wrap_w);
d3d11_smp_desc.MipLODBias = 0.0f; // FIXME?
switch (desc->border_color) {
case SG_BORDERCOLOR_TRANSPARENT_BLACK:
// all 0.0f
break;
case SG_BORDERCOLOR_OPAQUE_WHITE:
for (int i = 0; i < 4; i++) {
d3d11_smp_desc.BorderColor[i] = 1.0f;
}
break;
default:
// opaque black
d3d11_smp_desc.BorderColor[3] = 1.0f;
break;
}
d3d11_smp_desc.MaxAnisotropy = desc->max_anisotropy;
d3d11_smp_desc.ComparisonFunc = _sg_d3d11_compare_func(desc->compare);
d3d11_smp_desc.MinLOD = desc->min_lod;
d3d11_smp_desc.MaxLOD = desc->max_lod;
HRESULT hr = _sg_d3d11_CreateSamplerState(_sg.d3d11.dev, &d3d11_smp_desc, &smp->d3d11.smp);
if (!(SUCCEEDED(hr) && smp->d3d11.smp)) {
_SG_ERROR(D3D11_CREATE_SAMPLER_STATE_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(smp->d3d11.smp, desc->label);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_d3d11_discard_sampler(_sg_sampler_t* smp) {
SOKOL_ASSERT(smp);
if (smp->d3d11.smp) {
_sg_d3d11_Release(smp->d3d11.smp);
}
}
_SOKOL_PRIVATE bool _sg_d3d11_load_d3dcompiler_dll(void) {
if ((0 == _sg.d3d11.d3dcompiler_dll) && !_sg.d3d11.d3dcompiler_dll_load_failed) {
_sg.d3d11.d3dcompiler_dll = LoadLibraryA("d3dcompiler_47.dll");
if (0 == _sg.d3d11.d3dcompiler_dll) {
// don't attempt to load missing DLL in the future
_SG_ERROR(D3D11_LOAD_D3DCOMPILER_47_DLL_FAILED);
_sg.d3d11.d3dcompiler_dll_load_failed = true;
return false;
}
// look up function pointers
_sg.d3d11.D3DCompile_func = (pD3DCompile)(void*) GetProcAddress(_sg.d3d11.d3dcompiler_dll, "D3DCompile");
SOKOL_ASSERT(_sg.d3d11.D3DCompile_func);
}
return 0 != _sg.d3d11.d3dcompiler_dll;
}
_SOKOL_PRIVATE ID3DBlob* _sg_d3d11_compile_shader(const sg_shader_stage_desc* stage_desc) {
if (!_sg_d3d11_load_d3dcompiler_dll()) {
return NULL;
}
SOKOL_ASSERT(stage_desc->d3d11_target);
UINT flags1 = D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR;
if (_sg.desc.d3d11_shader_debugging) {
flags1 |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
} else {
flags1 |= D3DCOMPILE_OPTIMIZATION_LEVEL3;
}
ID3DBlob* output = NULL;
ID3DBlob* errors_or_warnings = NULL;
HRESULT hr = _sg.d3d11.D3DCompile_func(
stage_desc->source, // pSrcData
strlen(stage_desc->source), // SrcDataSize
NULL, // pSourceName
NULL, // pDefines
NULL, // pInclude
stage_desc->entry ? stage_desc->entry : "main", // pEntryPoint
stage_desc->d3d11_target, // pTarget
flags1, // Flags1
0, // Flags2
&output, // ppCode
&errors_or_warnings); // ppErrorMsgs
if (FAILED(hr)) {
_SG_ERROR(D3D11_SHADER_COMPILATION_FAILED);
}
if (errors_or_warnings) {
_SG_WARN(D3D11_SHADER_COMPILATION_OUTPUT);
_SG_LOGMSG(D3D11_SHADER_COMPILATION_OUTPUT, (LPCSTR)_sg_d3d11_GetBufferPointer(errors_or_warnings));
_sg_d3d11_Release(errors_or_warnings); errors_or_warnings = NULL;
}
if (FAILED(hr)) {
// just in case, usually output is NULL here
if (output) {
_sg_d3d11_Release(output);
output = NULL;
}
}
return output;
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && desc);
SOKOL_ASSERT(!shd->d3d11.vs && !shd->d3d11.fs && !shd->d3d11.vs_blob);
HRESULT hr;
// copy vertex attribute semantic names and indices
for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) {
_sg_strcpy(&shd->d3d11.attrs[i].sem_name, desc->attrs[i].sem_name);
shd->d3d11.attrs[i].sem_index = desc->attrs[i].sem_index;
}
// shader stage uniform blocks and image slots
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
_sg_shader_stage_t* cmn_stage = &shd->cmn.stage[stage_index];
_sg_d3d11_shader_stage_t* d3d11_stage = &shd->d3d11.stage[stage_index];
for (int ub_index = 0; ub_index < cmn_stage->num_uniform_blocks; ub_index++) {
const _sg_shader_uniform_block_t* ub = &cmn_stage->uniform_blocks[ub_index];
// create a D3D constant buffer for each uniform block
SOKOL_ASSERT(0 == d3d11_stage->cbufs[ub_index]);
D3D11_BUFFER_DESC cb_desc;
_sg_clear(&cb_desc, sizeof(cb_desc));
cb_desc.ByteWidth = (UINT)_sg_roundup((int)ub->size, 16);
cb_desc.Usage = D3D11_USAGE_DEFAULT;
cb_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
hr = _sg_d3d11_CreateBuffer(_sg.d3d11.dev, &cb_desc, NULL, &d3d11_stage->cbufs[ub_index]);
if (!(SUCCEEDED(hr) && d3d11_stage->cbufs[ub_index])) {
_SG_ERROR(D3D11_CREATE_CONSTANT_BUFFER_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(d3d11_stage->cbufs[ub_index], desc->label);
}
}
const void* vs_ptr = 0, *fs_ptr = 0;
SIZE_T vs_length = 0, fs_length = 0;
ID3DBlob* vs_blob = 0, *fs_blob = 0;
if (desc->vs.bytecode.ptr && desc->fs.bytecode.ptr) {
// create from shader byte code
vs_ptr = desc->vs.bytecode.ptr;
fs_ptr = desc->fs.bytecode.ptr;
vs_length = desc->vs.bytecode.size;
fs_length = desc->fs.bytecode.size;
} else {
// compile from shader source code
vs_blob = _sg_d3d11_compile_shader(&desc->vs);
fs_blob = _sg_d3d11_compile_shader(&desc->fs);
if (vs_blob && fs_blob) {
vs_ptr = _sg_d3d11_GetBufferPointer(vs_blob);
vs_length = _sg_d3d11_GetBufferSize(vs_blob);
fs_ptr = _sg_d3d11_GetBufferPointer(fs_blob);
fs_length = _sg_d3d11_GetBufferSize(fs_blob);
}
}
sg_resource_state result = SG_RESOURCESTATE_FAILED;
if (vs_ptr && fs_ptr && (vs_length > 0) && (fs_length > 0)) {
// create the D3D vertex- and pixel-shader objects
hr = _sg_d3d11_CreateVertexShader(_sg.d3d11.dev, vs_ptr, vs_length, NULL, &shd->d3d11.vs);
bool vs_succeeded = SUCCEEDED(hr) && shd->d3d11.vs;
hr = _sg_d3d11_CreatePixelShader(_sg.d3d11.dev, fs_ptr, fs_length, NULL, &shd->d3d11.fs);
bool fs_succeeded = SUCCEEDED(hr) && shd->d3d11.fs;
// need to store the vertex shader byte code, this is needed later in sg_create_pipeline
if (vs_succeeded && fs_succeeded) {
shd->d3d11.vs_blob_length = vs_length;
shd->d3d11.vs_blob = _sg_malloc((size_t)vs_length);
SOKOL_ASSERT(shd->d3d11.vs_blob);
memcpy(shd->d3d11.vs_blob, vs_ptr, vs_length);
result = SG_RESOURCESTATE_VALID;
_sg_d3d11_setlabel(shd->d3d11.vs, desc->label);
_sg_d3d11_setlabel(shd->d3d11.fs, desc->label);
}
}
if (vs_blob) {
_sg_d3d11_Release(vs_blob); vs_blob = 0;
}
if (fs_blob) {
_sg_d3d11_Release(fs_blob); fs_blob = 0;
}
return result;
}
_SOKOL_PRIVATE void _sg_d3d11_discard_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
if (shd->d3d11.vs) {
_sg_d3d11_Release(shd->d3d11.vs);
}
if (shd->d3d11.fs) {
_sg_d3d11_Release(shd->d3d11.fs);
}
if (shd->d3d11.vs_blob) {
_sg_free(shd->d3d11.vs_blob);
}
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
_sg_shader_stage_t* cmn_stage = &shd->cmn.stage[stage_index];
_sg_d3d11_shader_stage_t* d3d11_stage = &shd->d3d11.stage[stage_index];
for (int ub_index = 0; ub_index < cmn_stage->num_uniform_blocks; ub_index++) {
if (d3d11_stage->cbufs[ub_index]) {
_sg_d3d11_Release(d3d11_stage->cbufs[ub_index]);
}
}
}
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && shd && desc);
SOKOL_ASSERT(desc->shader.id == shd->slot.id);
SOKOL_ASSERT(shd->slot.state == SG_RESOURCESTATE_VALID);
SOKOL_ASSERT(shd->d3d11.vs_blob && shd->d3d11.vs_blob_length > 0);
SOKOL_ASSERT(!pip->d3d11.il && !pip->d3d11.rs && !pip->d3d11.dss && !pip->d3d11.bs);
pip->shader = shd;
pip->d3d11.index_format = _sg_d3d11_index_format(pip->cmn.index_type);
pip->d3d11.topology = _sg_d3d11_primitive_topology(desc->primitive_type);
pip->d3d11.stencil_ref = desc->stencil.ref;
// create input layout object
HRESULT hr;
D3D11_INPUT_ELEMENT_DESC d3d11_comps[SG_MAX_VERTEX_ATTRIBUTES];
_sg_clear(d3d11_comps, sizeof(d3d11_comps));
int attr_index = 0;
for (; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
const sg_vertex_attr_state* a_state = &desc->layout.attrs[attr_index];
if (a_state->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS);
const sg_vertex_buffer_layout_state* l_state = &desc->layout.buffers[a_state->buffer_index];
const sg_vertex_step step_func = l_state->step_func;
const int step_rate = l_state->step_rate;
D3D11_INPUT_ELEMENT_DESC* d3d11_comp = &d3d11_comps[attr_index];
d3d11_comp->SemanticName = _sg_strptr(&shd->d3d11.attrs[attr_index].sem_name);
d3d11_comp->SemanticIndex = (UINT)shd->d3d11.attrs[attr_index].sem_index;
d3d11_comp->Format = _sg_d3d11_vertex_format(a_state->format);
d3d11_comp->InputSlot = (UINT)a_state->buffer_index;
d3d11_comp->AlignedByteOffset = (UINT)a_state->offset;
d3d11_comp->InputSlotClass = _sg_d3d11_input_classification(step_func);
if (SG_VERTEXSTEP_PER_INSTANCE == step_func) {
d3d11_comp->InstanceDataStepRate = (UINT)step_rate;
pip->cmn.use_instanced_draw = true;
}
pip->cmn.vertex_buffer_layout_active[a_state->buffer_index] = true;
}
for (int layout_index = 0; layout_index < SG_MAX_VERTEX_BUFFERS; layout_index++) {
if (pip->cmn.vertex_buffer_layout_active[layout_index]) {
const sg_vertex_buffer_layout_state* l_state = &desc->layout.buffers[layout_index];
SOKOL_ASSERT(l_state->stride > 0);
pip->d3d11.vb_strides[layout_index] = (UINT)l_state->stride;
} else {
pip->d3d11.vb_strides[layout_index] = 0;
}
}
if (attr_index > 0) {
hr = _sg_d3d11_CreateInputLayout(_sg.d3d11.dev,
d3d11_comps, // pInputElementDesc
(UINT)attr_index, // NumElements
shd->d3d11.vs_blob, // pShaderByteCodeWithInputSignature
shd->d3d11.vs_blob_length, // BytecodeLength
&pip->d3d11.il);
if (!(SUCCEEDED(hr) && pip->d3d11.il)) {
_SG_ERROR(D3D11_CREATE_INPUT_LAYOUT_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(pip->d3d11.il, desc->label);
}
// create rasterizer state
D3D11_RASTERIZER_DESC rs_desc;
_sg_clear(&rs_desc, sizeof(rs_desc));
rs_desc.FillMode = D3D11_FILL_SOLID;
rs_desc.CullMode = _sg_d3d11_cull_mode(desc->cull_mode);
rs_desc.FrontCounterClockwise = desc->face_winding == SG_FACEWINDING_CCW;
rs_desc.DepthBias = (INT) pip->cmn.depth.bias;
rs_desc.DepthBiasClamp = pip->cmn.depth.bias_clamp;
rs_desc.SlopeScaledDepthBias = pip->cmn.depth.bias_slope_scale;
rs_desc.DepthClipEnable = TRUE;
rs_desc.ScissorEnable = TRUE;
rs_desc.MultisampleEnable = desc->sample_count > 1;
rs_desc.AntialiasedLineEnable = FALSE;
hr = _sg_d3d11_CreateRasterizerState(_sg.d3d11.dev, &rs_desc, &pip->d3d11.rs);
if (!(SUCCEEDED(hr) && pip->d3d11.rs)) {
_SG_ERROR(D3D11_CREATE_RASTERIZER_STATE_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(pip->d3d11.rs, desc->label);
// create depth-stencil state
D3D11_DEPTH_STENCIL_DESC dss_desc;
_sg_clear(&dss_desc, sizeof(dss_desc));
dss_desc.DepthEnable = TRUE;
dss_desc.DepthWriteMask = desc->depth.write_enabled ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
dss_desc.DepthFunc = _sg_d3d11_compare_func(desc->depth.compare);
dss_desc.StencilEnable = desc->stencil.enabled;
dss_desc.StencilReadMask = desc->stencil.read_mask;
dss_desc.StencilWriteMask = desc->stencil.write_mask;
const sg_stencil_face_state* sf = &desc->stencil.front;
dss_desc.FrontFace.StencilFailOp = _sg_d3d11_stencil_op(sf->fail_op);
dss_desc.FrontFace.StencilDepthFailOp = _sg_d3d11_stencil_op(sf->depth_fail_op);
dss_desc.FrontFace.StencilPassOp = _sg_d3d11_stencil_op(sf->pass_op);
dss_desc.FrontFace.StencilFunc = _sg_d3d11_compare_func(sf->compare);
const sg_stencil_face_state* sb = &desc->stencil.back;
dss_desc.BackFace.StencilFailOp = _sg_d3d11_stencil_op(sb->fail_op);
dss_desc.BackFace.StencilDepthFailOp = _sg_d3d11_stencil_op(sb->depth_fail_op);
dss_desc.BackFace.StencilPassOp = _sg_d3d11_stencil_op(sb->pass_op);
dss_desc.BackFace.StencilFunc = _sg_d3d11_compare_func(sb->compare);
hr = _sg_d3d11_CreateDepthStencilState(_sg.d3d11.dev, &dss_desc, &pip->d3d11.dss);
if (!(SUCCEEDED(hr) && pip->d3d11.dss)) {
_SG_ERROR(D3D11_CREATE_DEPTH_STENCIL_STATE_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(pip->d3d11.dss, desc->label);
// create blend state
D3D11_BLEND_DESC bs_desc;
_sg_clear(&bs_desc, sizeof(bs_desc));
bs_desc.AlphaToCoverageEnable = desc->alpha_to_coverage_enabled;
bs_desc.IndependentBlendEnable = TRUE;
{
int i = 0;
for (i = 0; i < desc->color_count; i++) {
const sg_blend_state* src = &desc->colors[i].blend;
D3D11_RENDER_TARGET_BLEND_DESC* dst = &bs_desc.RenderTarget[i];
dst->BlendEnable = src->enabled;
dst->SrcBlend = _sg_d3d11_blend_factor(src->src_factor_rgb);
dst->DestBlend = _sg_d3d11_blend_factor(src->dst_factor_rgb);
dst->BlendOp = _sg_d3d11_blend_op(src->op_rgb);
dst->SrcBlendAlpha = _sg_d3d11_blend_factor(src->src_factor_alpha);
dst->DestBlendAlpha = _sg_d3d11_blend_factor(src->dst_factor_alpha);
dst->BlendOpAlpha = _sg_d3d11_blend_op(src->op_alpha);
dst->RenderTargetWriteMask = _sg_d3d11_color_write_mask(desc->colors[i].write_mask);
}
for (; i < 8; i++) {
D3D11_RENDER_TARGET_BLEND_DESC* dst = &bs_desc.RenderTarget[i];
dst->BlendEnable = FALSE;
dst->SrcBlend = dst->SrcBlendAlpha = D3D11_BLEND_ONE;
dst->DestBlend = dst->DestBlendAlpha = D3D11_BLEND_ZERO;
dst->BlendOp = dst->BlendOpAlpha = D3D11_BLEND_OP_ADD;
dst->RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
}
}
hr = _sg_d3d11_CreateBlendState(_sg.d3d11.dev, &bs_desc, &pip->d3d11.bs);
if (!(SUCCEEDED(hr) && pip->d3d11.bs)) {
_SG_ERROR(D3D11_CREATE_BLEND_STATE_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(pip->d3d11.bs, desc->label);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_d3d11_discard_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
if (pip == _sg.d3d11.cur_pipeline) {
_sg.d3d11.cur_pipeline = 0;
_sg.d3d11.cur_pipeline_id.id = SG_INVALID_ID;
}
if (pip->d3d11.il) {
_sg_d3d11_Release(pip->d3d11.il);
}
if (pip->d3d11.rs) {
_sg_d3d11_Release(pip->d3d11.rs);
}
if (pip->d3d11.dss) {
_sg_d3d11_Release(pip->d3d11.dss);
}
if (pip->d3d11.bs) {
_sg_d3d11_Release(pip->d3d11.bs);
}
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_img, const sg_attachments_desc* desc) {
SOKOL_ASSERT(atts && desc);
SOKOL_ASSERT(color_images && resolve_images);
SOKOL_ASSERT(_sg.d3d11.dev);
// copy image pointers
for (int i = 0; i < atts->cmn.num_colors; i++) {
const sg_attachment_desc* color_desc = &desc->colors[i];
_SOKOL_UNUSED(color_desc);
SOKOL_ASSERT(color_desc->image.id != SG_INVALID_ID);
SOKOL_ASSERT(0 == atts->d3d11.colors[i].image);
SOKOL_ASSERT(color_images[i] && (color_images[i]->slot.id == color_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(color_images[i]->cmn.pixel_format));
atts->d3d11.colors[i].image = color_images[i];
const sg_attachment_desc* resolve_desc = &desc->resolves[i];
if (resolve_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(0 == atts->d3d11.resolves[i].image);
SOKOL_ASSERT(resolve_images[i] && (resolve_images[i]->slot.id == resolve_desc->image.id));
SOKOL_ASSERT(color_images[i] && (color_images[i]->cmn.pixel_format == resolve_images[i]->cmn.pixel_format));
atts->d3d11.resolves[i].image = resolve_images[i];
}
}
SOKOL_ASSERT(0 == atts->d3d11.depth_stencil.image);
const sg_attachment_desc* ds_desc = &desc->depth_stencil;
if (ds_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(ds_img && (ds_img->slot.id == ds_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(ds_img->cmn.pixel_format));
atts->d3d11.depth_stencil.image = ds_img;
}
// create render-target views
for (int i = 0; i < atts->cmn.num_colors; i++) {
const _sg_attachment_common_t* cmn_color_att = &atts->cmn.colors[i];
const _sg_image_t* color_img = color_images[i];
SOKOL_ASSERT(0 == atts->d3d11.colors[i].view.rtv);
const bool msaa = color_img->cmn.sample_count > 1;
D3D11_RENDER_TARGET_VIEW_DESC d3d11_rtv_desc;
_sg_clear(&d3d11_rtv_desc, sizeof(d3d11_rtv_desc));
d3d11_rtv_desc.Format = _sg_d3d11_rtv_pixel_format(color_img->cmn.pixel_format);
if (color_img->cmn.type == SG_IMAGETYPE_2D) {
if (msaa) {
d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
} else {
d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
d3d11_rtv_desc.Texture2D.MipSlice = (UINT)cmn_color_att->mip_level;
}
} else if ((color_img->cmn.type == SG_IMAGETYPE_CUBE) || (color_img->cmn.type == SG_IMAGETYPE_ARRAY)) {
if (msaa) {
d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY;
d3d11_rtv_desc.Texture2DMSArray.FirstArraySlice = (UINT)cmn_color_att->slice;
d3d11_rtv_desc.Texture2DMSArray.ArraySize = 1;
} else {
d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
d3d11_rtv_desc.Texture2DArray.MipSlice = (UINT)cmn_color_att->mip_level;
d3d11_rtv_desc.Texture2DArray.FirstArraySlice = (UINT)cmn_color_att->slice;
d3d11_rtv_desc.Texture2DArray.ArraySize = 1;
}
} else {
SOKOL_ASSERT(color_img->cmn.type == SG_IMAGETYPE_3D);
SOKOL_ASSERT(!msaa);
d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D;
d3d11_rtv_desc.Texture3D.MipSlice = (UINT)cmn_color_att->mip_level;
d3d11_rtv_desc.Texture3D.FirstWSlice = (UINT)cmn_color_att->slice;
d3d11_rtv_desc.Texture3D.WSize = 1;
}
SOKOL_ASSERT(color_img->d3d11.res);
HRESULT hr = _sg_d3d11_CreateRenderTargetView(_sg.d3d11.dev, color_img->d3d11.res, &d3d11_rtv_desc, &atts->d3d11.colors[i].view.rtv);
if (!(SUCCEEDED(hr) && atts->d3d11.colors[i].view.rtv)) {
_SG_ERROR(D3D11_CREATE_RTV_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(atts->d3d11.colors[i].view.rtv, desc->label);
}
SOKOL_ASSERT(0 == atts->d3d11.depth_stencil.view.dsv);
if (ds_desc->image.id != SG_INVALID_ID) {
const _sg_attachment_common_t* cmn_ds_att = &atts->cmn.depth_stencil;
const bool msaa = ds_img->cmn.sample_count > 1;
D3D11_DEPTH_STENCIL_VIEW_DESC d3d11_dsv_desc;
_sg_clear(&d3d11_dsv_desc, sizeof(d3d11_dsv_desc));
d3d11_dsv_desc.Format = _sg_d3d11_dsv_pixel_format(ds_img->cmn.pixel_format);
SOKOL_ASSERT(ds_img && ds_img->cmn.type != SG_IMAGETYPE_3D);
if (ds_img->cmn.type == SG_IMAGETYPE_2D) {
if (msaa) {
d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
} else {
d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
d3d11_dsv_desc.Texture2D.MipSlice = (UINT)cmn_ds_att->mip_level;
}
} else if ((ds_img->cmn.type == SG_IMAGETYPE_CUBE) || (ds_img->cmn.type == SG_IMAGETYPE_ARRAY)) {
if (msaa) {
d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY;
d3d11_dsv_desc.Texture2DMSArray.FirstArraySlice = (UINT)cmn_ds_att->slice;
d3d11_dsv_desc.Texture2DMSArray.ArraySize = 1;
} else {
d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
d3d11_dsv_desc.Texture2DArray.MipSlice = (UINT)cmn_ds_att->mip_level;
d3d11_dsv_desc.Texture2DArray.FirstArraySlice = (UINT)cmn_ds_att->slice;
d3d11_dsv_desc.Texture2DArray.ArraySize = 1;
}
}
SOKOL_ASSERT(ds_img->d3d11.res);
HRESULT hr = _sg_d3d11_CreateDepthStencilView(_sg.d3d11.dev, ds_img->d3d11.res, &d3d11_dsv_desc, &atts->d3d11.depth_stencil.view.dsv);
if (!(SUCCEEDED(hr) && atts->d3d11.depth_stencil.view.dsv)) {
_SG_ERROR(D3D11_CREATE_DSV_FAILED);
return SG_RESOURCESTATE_FAILED;
}
_sg_d3d11_setlabel(atts->d3d11.depth_stencil.view.dsv, desc->label);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_d3d11_discard_attachments(_sg_attachments_t* atts) {
SOKOL_ASSERT(atts);
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (atts->d3d11.colors[i].view.rtv) {
_sg_d3d11_Release(atts->d3d11.colors[i].view.rtv);
}
if (atts->d3d11.resolves[i].view.rtv) {
_sg_d3d11_Release(atts->d3d11.resolves[i].view.rtv);
}
}
if (atts->d3d11.depth_stencil.view.dsv) {
_sg_d3d11_Release(atts->d3d11.depth_stencil.view.dsv);
}
}
_SOKOL_PRIVATE _sg_image_t* _sg_d3d11_attachments_color_image(const _sg_attachments_t* atts, int index) {
SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
return atts->d3d11.colors[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_d3d11_attachments_resolve_image(const _sg_attachments_t* atts, int index) {
SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
return atts->d3d11.resolves[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_d3d11_attachments_ds_image(const _sg_attachments_t* atts) {
SOKOL_ASSERT(atts);
return atts->d3d11.depth_stencil.image;
}
_SOKOL_PRIVATE void _sg_d3d11_begin_pass(const sg_pass* pass) {
SOKOL_ASSERT(pass);
const _sg_attachments_t* atts = _sg.cur_pass.atts;
const sg_swapchain* swapchain = &pass->swapchain;
const sg_pass_action* action = &pass->action;
int num_rtvs = 0;
ID3D11RenderTargetView* rtvs[SG_MAX_COLOR_ATTACHMENTS] = { 0 };
ID3D11DepthStencilView* dsv = 0;
_sg.d3d11.cur_pass.render_view = 0;
_sg.d3d11.cur_pass.resolve_view = 0;
if (atts) {
num_rtvs = atts->cmn.num_colors;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
rtvs[i] = atts->d3d11.colors[i].view.rtv;
}
dsv = atts->d3d11.depth_stencil.view.dsv;
} else {
// NOTE: depth-stencil-view is optional
SOKOL_ASSERT(swapchain->d3d11.render_view);
num_rtvs = 1;
rtvs[0] = (ID3D11RenderTargetView*) swapchain->d3d11.render_view;
dsv = (ID3D11DepthStencilView*) swapchain->d3d11.depth_stencil_view;
_sg.d3d11.cur_pass.render_view = (ID3D11RenderTargetView*) swapchain->d3d11.render_view;
_sg.d3d11.cur_pass.resolve_view = (ID3D11RenderTargetView*) swapchain->d3d11.resolve_view;
}
// apply the render-target- and depth-stencil-views
_sg_d3d11_OMSetRenderTargets(_sg.d3d11.ctx, SG_MAX_COLOR_ATTACHMENTS, rtvs, dsv);
_sg_stats_add(d3d11.pass.num_om_set_render_targets, 1);
// set viewport and scissor rect to cover whole screen
D3D11_VIEWPORT vp;
_sg_clear(&vp, sizeof(vp));
vp.Width = (FLOAT) _sg.cur_pass.width;
vp.Height = (FLOAT) _sg.cur_pass.height;
vp.MaxDepth = 1.0f;
_sg_d3d11_RSSetViewports(_sg.d3d11.ctx, 1, &vp);
D3D11_RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = _sg.cur_pass.width;
rect.bottom = _sg.cur_pass.height;
_sg_d3d11_RSSetScissorRects(_sg.d3d11.ctx, 1, &rect);
// perform clear action
for (int i = 0; i < num_rtvs; i++) {
if (action->colors[i].load_action == SG_LOADACTION_CLEAR) {
_sg_d3d11_ClearRenderTargetView(_sg.d3d11.ctx, rtvs[i], (float*)&action->colors[i].clear_value);
_sg_stats_add(d3d11.pass.num_clear_render_target_view, 1);
}
}
UINT ds_flags = 0;
if (action->depth.load_action == SG_LOADACTION_CLEAR) {
ds_flags |= D3D11_CLEAR_DEPTH;
}
if (action->stencil.load_action == SG_LOADACTION_CLEAR) {
ds_flags |= D3D11_CLEAR_STENCIL;
}
if ((0 != ds_flags) && dsv) {
_sg_d3d11_ClearDepthStencilView(_sg.d3d11.ctx, dsv, ds_flags, action->depth.clear_value, action->stencil.clear_value);
_sg_stats_add(d3d11.pass.num_clear_depth_stencil_view, 1);
}
}
// D3D11CalcSubresource only exists for C++
_SOKOL_PRIVATE UINT _sg_d3d11_calcsubresource(UINT mip_slice, UINT array_slice, UINT mip_levels) {
return mip_slice + array_slice * mip_levels;
}
_SOKOL_PRIVATE void _sg_d3d11_end_pass(void) {
SOKOL_ASSERT(_sg.d3d11.ctx);
// need to resolve MSAA render attachments into texture?
if (_sg.cur_pass.atts_id.id != SG_INVALID_ID) {
// ...for offscreen pass...
SOKOL_ASSERT(_sg.cur_pass.atts && _sg.cur_pass.atts->slot.id == _sg.cur_pass.atts_id.id);
for (int i = 0; i < _sg.cur_pass.atts->cmn.num_colors; i++) {
const _sg_image_t* resolve_img = _sg.cur_pass.atts->d3d11.resolves[i].image;
if (resolve_img) {
const _sg_image_t* color_img = _sg.cur_pass.atts->d3d11.colors[i].image;
const _sg_attachment_common_t* cmn_color_att = &_sg.cur_pass.atts->cmn.colors[i];
const _sg_attachment_common_t* cmn_resolve_att = &_sg.cur_pass.atts->cmn.resolves[i];
SOKOL_ASSERT(resolve_img->slot.id == cmn_resolve_att->image_id.id);
SOKOL_ASSERT(color_img && (color_img->slot.id == cmn_color_att->image_id.id));
SOKOL_ASSERT(color_img->cmn.sample_count > 1);
SOKOL_ASSERT(resolve_img->cmn.sample_count == 1);
const UINT src_subres = _sg_d3d11_calcsubresource(
(UINT)cmn_color_att->mip_level,
(UINT)cmn_color_att->slice,
(UINT)color_img->cmn.num_mipmaps);
const UINT dst_subres = _sg_d3d11_calcsubresource(
(UINT)cmn_resolve_att->mip_level,
(UINT)cmn_resolve_att->slice,
(UINT)resolve_img->cmn.num_mipmaps);
_sg_d3d11_ResolveSubresource(_sg.d3d11.ctx,
resolve_img->d3d11.res,
dst_subres,
color_img->d3d11.res,
src_subres,
color_img->d3d11.format);
_sg_stats_add(d3d11.pass.num_resolve_subresource, 1);
}
}
} else {
// ...for swapchain pass...
if (_sg.d3d11.cur_pass.resolve_view) {
SOKOL_ASSERT(_sg.d3d11.cur_pass.render_view);
SOKOL_ASSERT(_sg.cur_pass.swapchain.sample_count > 1);
SOKOL_ASSERT(_sg.cur_pass.swapchain.color_fmt > SG_PIXELFORMAT_NONE);
ID3D11Resource* d3d11_render_res = 0;
ID3D11Resource* d3d11_resolve_res = 0;
_sg_d3d11_GetResource((ID3D11View*)_sg.d3d11.cur_pass.render_view, &d3d11_render_res);
_sg_d3d11_GetResource((ID3D11View*)_sg.d3d11.cur_pass.resolve_view, &d3d11_resolve_res);
SOKOL_ASSERT(d3d11_render_res);
SOKOL_ASSERT(d3d11_resolve_res);
const sg_pixel_format color_fmt = _sg.cur_pass.swapchain.color_fmt;
_sg_d3d11_ResolveSubresource(_sg.d3d11.ctx, d3d11_resolve_res, 0, d3d11_render_res, 0, _sg_d3d11_rtv_pixel_format(color_fmt));
_sg_d3d11_Release(d3d11_render_res);
_sg_d3d11_Release(d3d11_resolve_res);
_sg_stats_add(d3d11.pass.num_resolve_subresource, 1);
}
}
_sg.d3d11.cur_pass.render_view = 0;
_sg.d3d11.cur_pass.resolve_view = 0;
_sg.d3d11.cur_pipeline = 0;
_sg.d3d11.cur_pipeline_id.id = SG_INVALID_ID;
_sg_d3d11_clear_state();
}
_SOKOL_PRIVATE void _sg_d3d11_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.d3d11.ctx);
D3D11_VIEWPORT vp;
vp.TopLeftX = (FLOAT) x;
vp.TopLeftY = (FLOAT) (origin_top_left ? y : (_sg.cur_pass.height - (y + h)));
vp.Width = (FLOAT) w;
vp.Height = (FLOAT) h;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
_sg_d3d11_RSSetViewports(_sg.d3d11.ctx, 1, &vp);
}
_SOKOL_PRIVATE void _sg_d3d11_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.d3d11.ctx);
D3D11_RECT rect;
rect.left = x;
rect.top = (origin_top_left ? y : (_sg.cur_pass.height - (y + h)));
rect.right = x + w;
rect.bottom = origin_top_left ? (y + h) : (_sg.cur_pass.height - y);
_sg_d3d11_RSSetScissorRects(_sg.d3d11.ctx, 1, &rect);
}
_SOKOL_PRIVATE void _sg_d3d11_apply_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id));
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(pip->d3d11.rs && pip->d3d11.bs && pip->d3d11.dss);
_sg.d3d11.cur_pipeline = pip;
_sg.d3d11.cur_pipeline_id.id = pip->slot.id;
_sg.d3d11.use_indexed_draw = (pip->d3d11.index_format != DXGI_FORMAT_UNKNOWN);
_sg.d3d11.use_instanced_draw = pip->cmn.use_instanced_draw;
_sg_d3d11_RSSetState(_sg.d3d11.ctx, pip->d3d11.rs);
_sg_d3d11_OMSetDepthStencilState(_sg.d3d11.ctx, pip->d3d11.dss, pip->d3d11.stencil_ref);
_sg_d3d11_OMSetBlendState(_sg.d3d11.ctx, pip->d3d11.bs, (float*)&pip->cmn.blend_color, 0xFFFFFFFF);
_sg_d3d11_IASetPrimitiveTopology(_sg.d3d11.ctx, pip->d3d11.topology);
_sg_d3d11_IASetInputLayout(_sg.d3d11.ctx, pip->d3d11.il);
_sg_d3d11_VSSetShader(_sg.d3d11.ctx, pip->shader->d3d11.vs, NULL, 0);
_sg_d3d11_VSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, pip->shader->d3d11.stage[SG_SHADERSTAGE_VS].cbufs);
_sg_d3d11_PSSetShader(_sg.d3d11.ctx, pip->shader->d3d11.fs, NULL, 0);
_sg_d3d11_PSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, pip->shader->d3d11.stage[SG_SHADERSTAGE_FS].cbufs);
_sg_stats_add(d3d11.pipeline.num_rs_set_state, 1);
_sg_stats_add(d3d11.pipeline.num_om_set_depth_stencil_state, 1);
_sg_stats_add(d3d11.pipeline.num_om_set_blend_state, 1);
_sg_stats_add(d3d11.pipeline.num_ia_set_primitive_topology, 1);
_sg_stats_add(d3d11.pipeline.num_ia_set_input_layout, 1);
_sg_stats_add(d3d11.pipeline.num_vs_set_shader, 1);
_sg_stats_add(d3d11.pipeline.num_vs_set_constant_buffers, 1);
_sg_stats_add(d3d11.pipeline.num_ps_set_shader, 1);
_sg_stats_add(d3d11.pipeline.num_ps_set_constant_buffers, 1);
}
_SOKOL_PRIVATE bool _sg_d3d11_apply_bindings(_sg_bindings_t* bnd) {
SOKOL_ASSERT(bnd);
SOKOL_ASSERT(bnd->pip);
SOKOL_ASSERT(_sg.d3d11.ctx);
// gather all the D3D11 resources into arrays
ID3D11Buffer* d3d11_ib = bnd->ib ? bnd->ib->d3d11.buf : 0;
ID3D11Buffer* d3d11_vbs[SG_MAX_VERTEX_BUFFERS] = {0};
UINT d3d11_vb_offsets[SG_MAX_VERTEX_BUFFERS] = {0};
ID3D11ShaderResourceView* d3d11_vs_srvs[_SG_D3D11_MAX_SHADERSTAGE_SRVS] = {0};
ID3D11ShaderResourceView* d3d11_fs_srvs[_SG_D3D11_MAX_SHADERSTAGE_SRVS] = {0};
ID3D11SamplerState* d3d11_vs_smps[SG_MAX_SHADERSTAGE_SAMPLERS] = {0};
ID3D11SamplerState* d3d11_fs_smps[SG_MAX_SHADERSTAGE_SAMPLERS] = {0};
for (int i = 0; i < bnd->num_vbs; i++) {
SOKOL_ASSERT(bnd->vbs[i]->d3d11.buf);
d3d11_vbs[i] = bnd->vbs[i]->d3d11.buf;
d3d11_vb_offsets[i] = (UINT)bnd->vb_offsets[i];
}
for (int i = 0; i < bnd->num_vs_imgs; i++) {
SOKOL_ASSERT(bnd->vs_imgs[i]->d3d11.srv);
d3d11_vs_srvs[_SG_D3D11_SHADERSTAGE_IMAGE_SRV_OFFSET + i] = bnd->vs_imgs[i]->d3d11.srv;
}
for (int i = 0; i < bnd->num_vs_sbufs; i++) {
SOKOL_ASSERT(bnd->vs_sbufs[i]->d3d11.srv);
d3d11_vs_srvs[_SG_D3D11_SHADERSTAGE_BUFFER_SRV_OFFSET + i] = bnd->vs_sbufs[i]->d3d11.srv;
}
for (int i = 0; i < bnd->num_fs_imgs; i++) {
SOKOL_ASSERT(bnd->fs_imgs[i]->d3d11.srv);
d3d11_fs_srvs[_SG_D3D11_SHADERSTAGE_IMAGE_SRV_OFFSET + i] = bnd->fs_imgs[i]->d3d11.srv;
}
for (int i = 0; i < bnd->num_fs_sbufs; i++) {
SOKOL_ASSERT(bnd->fs_sbufs[i]->d3d11.srv);
d3d11_fs_srvs[_SG_D3D11_SHADERSTAGE_BUFFER_SRV_OFFSET + i] = bnd->fs_sbufs[i]->d3d11.srv;
}
for (int i = 0; i < bnd->num_vs_smps; i++) {
SOKOL_ASSERT(bnd->vs_smps[i]->d3d11.smp);
d3d11_vs_smps[i] = bnd->vs_smps[i]->d3d11.smp;
}
for (int i = 0; i < bnd->num_fs_smps; i++) {
SOKOL_ASSERT(bnd->fs_smps[i]->d3d11.smp);
d3d11_fs_smps[i] = bnd->fs_smps[i]->d3d11.smp;
}
_sg_d3d11_IASetVertexBuffers(_sg.d3d11.ctx, 0, SG_MAX_VERTEX_BUFFERS, d3d11_vbs, bnd->pip->d3d11.vb_strides, d3d11_vb_offsets);
_sg_d3d11_IASetIndexBuffer(_sg.d3d11.ctx, d3d11_ib, bnd->pip->d3d11.index_format, (UINT)bnd->ib_offset);
_sg_d3d11_VSSetShaderResources(_sg.d3d11.ctx, 0, _SG_D3D11_MAX_SHADERSTAGE_SRVS, d3d11_vs_srvs);
_sg_d3d11_PSSetShaderResources(_sg.d3d11.ctx, 0, _SG_D3D11_MAX_SHADERSTAGE_SRVS, d3d11_fs_srvs);
_sg_d3d11_VSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_SAMPLERS, d3d11_vs_smps);
_sg_d3d11_PSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_SAMPLERS, d3d11_fs_smps);
_sg_stats_add(d3d11.bindings.num_ia_set_vertex_buffers, 1);
_sg_stats_add(d3d11.bindings.num_ia_set_index_buffer, 1);
_sg_stats_add(d3d11.bindings.num_vs_set_shader_resources, 1);
_sg_stats_add(d3d11.bindings.num_ps_set_shader_resources, 1);
_sg_stats_add(d3d11.bindings.num_vs_set_samplers, 1);
_sg_stats_add(d3d11.bindings.num_ps_set_samplers, 1);
return true;
}
_SOKOL_PRIVATE void _sg_d3d11_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(_sg.d3d11.cur_pipeline && _sg.d3d11.cur_pipeline->slot.id == _sg.d3d11.cur_pipeline_id.id);
SOKOL_ASSERT(_sg.d3d11.cur_pipeline->shader && _sg.d3d11.cur_pipeline->shader->slot.id == _sg.d3d11.cur_pipeline->cmn.shader_id.id);
SOKOL_ASSERT(ub_index < _sg.d3d11.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks);
SOKOL_ASSERT(data->size == _sg.d3d11.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size);
ID3D11Buffer* cb = _sg.d3d11.cur_pipeline->shader->d3d11.stage[stage_index].cbufs[ub_index];
SOKOL_ASSERT(cb);
_sg_d3d11_UpdateSubresource(_sg.d3d11.ctx, (ID3D11Resource*)cb, 0, NULL, data->ptr, 0, 0);
_sg_stats_add(d3d11.uniforms.num_update_subresource, 1);
}
_SOKOL_PRIVATE void _sg_d3d11_draw(int base_element, int num_elements, int num_instances) {
const bool use_instanced_draw = (num_instances > 1) || (_sg.d3d11.use_instanced_draw);
if (_sg.d3d11.use_indexed_draw) {
if (use_instanced_draw) {
_sg_d3d11_DrawIndexedInstanced(_sg.d3d11.ctx, (UINT)num_elements, (UINT)num_instances, (UINT)base_element, 0, 0);
_sg_stats_add(d3d11.draw.num_draw_indexed_instanced, 1);
} else {
_sg_d3d11_DrawIndexed(_sg.d3d11.ctx, (UINT)num_elements, (UINT)base_element, 0);
_sg_stats_add(d3d11.draw.num_draw_indexed, 1);
}
} else {
if (use_instanced_draw) {
_sg_d3d11_DrawInstanced(_sg.d3d11.ctx, (UINT)num_elements, (UINT)num_instances, (UINT)base_element, 0);
_sg_stats_add(d3d11.draw.num_draw_instanced, 1);
} else {
_sg_d3d11_Draw(_sg.d3d11.ctx, (UINT)num_elements, (UINT)base_element);
_sg_stats_add(d3d11.draw.num_draw, 1);
}
}
}
_SOKOL_PRIVATE void _sg_d3d11_commit(void) {
// empty
}
_SOKOL_PRIVATE void _sg_d3d11_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(buf->d3d11.buf);
D3D11_MAPPED_SUBRESOURCE d3d11_msr;
HRESULT hr = _sg_d3d11_Map(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &d3d11_msr);
_sg_stats_add(d3d11.num_map, 1);
if (SUCCEEDED(hr)) {
memcpy(d3d11_msr.pData, data->ptr, data->size);
_sg_d3d11_Unmap(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0);
_sg_stats_add(d3d11.num_unmap, 1);
} else {
_SG_ERROR(D3D11_MAP_FOR_UPDATE_BUFFER_FAILED);
}
}
_SOKOL_PRIVATE void _sg_d3d11_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(buf->d3d11.buf);
D3D11_MAP map_type = new_frame ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE;
D3D11_MAPPED_SUBRESOURCE d3d11_msr;
HRESULT hr = _sg_d3d11_Map(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0, map_type, 0, &d3d11_msr);
_sg_stats_add(d3d11.num_map, 1);
if (SUCCEEDED(hr)) {
uint8_t* dst_ptr = (uint8_t*)d3d11_msr.pData + buf->cmn.append_pos;
memcpy(dst_ptr, data->ptr, data->size);
_sg_d3d11_Unmap(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0);
_sg_stats_add(d3d11.num_unmap, 1);
} else {
_SG_ERROR(D3D11_MAP_FOR_APPEND_BUFFER_FAILED);
}
}
// see: https://learn.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-subresources
// also see: https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-d3d11calcsubresource
_SOKOL_PRIVATE void _sg_d3d11_update_image(_sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(img && data);
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(img->d3d11.res);
const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1;
const int num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices:1;
const int num_depth_slices = (img->cmn.type == SG_IMAGETYPE_3D) ? img->cmn.num_slices:1;
UINT subres_index = 0;
HRESULT hr;
D3D11_MAPPED_SUBRESOURCE d3d11_msr;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int slice_index = 0; slice_index < num_slices; slice_index++) {
for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++, subres_index++) {
SOKOL_ASSERT(subres_index < (SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS));
const int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index);
const int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index);
const int src_row_pitch = _sg_row_pitch(img->cmn.pixel_format, mip_width, 1);
const int src_depth_pitch = _sg_surface_pitch(img->cmn.pixel_format, mip_width, mip_height, 1);
const sg_range* subimg_data = &(data->subimage[face_index][mip_index]);
const size_t slice_size = subimg_data->size / (size_t)num_slices;
SOKOL_ASSERT(slice_size == (size_t)(src_depth_pitch * num_depth_slices));
const size_t slice_offset = slice_size * (size_t)slice_index;
const uint8_t* slice_ptr = ((const uint8_t*)subimg_data->ptr) + slice_offset;
hr = _sg_d3d11_Map(_sg.d3d11.ctx, img->d3d11.res, subres_index, D3D11_MAP_WRITE_DISCARD, 0, &d3d11_msr);
_sg_stats_add(d3d11.num_map, 1);
if (SUCCEEDED(hr)) {
const uint8_t* src_ptr = slice_ptr;
uint8_t* dst_ptr = (uint8_t*)d3d11_msr.pData;
for (int depth_index = 0; depth_index < num_depth_slices; depth_index++) {
if (src_row_pitch == (int)d3d11_msr.RowPitch) {
const size_t copy_size = slice_size / (size_t)num_depth_slices;
SOKOL_ASSERT((copy_size * (size_t)num_depth_slices) == slice_size);
memcpy(dst_ptr, src_ptr, copy_size);
} else {
SOKOL_ASSERT(src_row_pitch < (int)d3d11_msr.RowPitch);
const uint8_t* src_row_ptr = src_ptr;
uint8_t* dst_row_ptr = dst_ptr;
for (int row_index = 0; row_index < mip_height; row_index++) {
memcpy(dst_row_ptr, src_row_ptr, (size_t)src_row_pitch);
src_row_ptr += src_row_pitch;
dst_row_ptr += d3d11_msr.RowPitch;
}
}
src_ptr += src_depth_pitch;
dst_ptr += d3d11_msr.DepthPitch;
}
_sg_d3d11_Unmap(_sg.d3d11.ctx, img->d3d11.res, subres_index);
_sg_stats_add(d3d11.num_unmap, 1);
} else {
_SG_ERROR(D3D11_MAP_FOR_UPDATE_IMAGE_FAILED);
}
}
}
}
}
// ███ ███ ███████ ████████ █████ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████
// ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██
// ██ ████ ██ █████ ██ ███████ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ██ ██ ██ ███████ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████
//
// >>metal backend
#elif defined(SOKOL_METAL)
#if __has_feature(objc_arc)
#define _SG_OBJC_RETAIN(obj) { }
#define _SG_OBJC_RELEASE(obj) { obj = nil; }
#else
#define _SG_OBJC_RETAIN(obj) { [obj retain]; }
#define _SG_OBJC_RELEASE(obj) { [obj release]; obj = nil; }
#endif
//-- enum translation functions ------------------------------------------------
_SOKOL_PRIVATE MTLLoadAction _sg_mtl_load_action(sg_load_action a) {
switch (a) {
case SG_LOADACTION_CLEAR: return MTLLoadActionClear;
case SG_LOADACTION_LOAD: return MTLLoadActionLoad;
case SG_LOADACTION_DONTCARE: return MTLLoadActionDontCare;
default: SOKOL_UNREACHABLE; return (MTLLoadAction)0;
}
}
_SOKOL_PRIVATE MTLStoreAction _sg_mtl_store_action(sg_store_action a, bool resolve) {
switch (a) {
case SG_STOREACTION_STORE:
if (resolve) {
return MTLStoreActionStoreAndMultisampleResolve;
} else {
return MTLStoreActionStore;
}
break;
case SG_STOREACTION_DONTCARE:
if (resolve) {
return MTLStoreActionMultisampleResolve;
} else {
return MTLStoreActionDontCare;
}
break;
default: SOKOL_UNREACHABLE; return (MTLStoreAction)0;
}
}
_SOKOL_PRIVATE MTLResourceOptions _sg_mtl_resource_options_storage_mode_managed_or_shared(void) {
#if defined(_SG_TARGET_MACOS)
if (_sg.mtl.use_shared_storage_mode) {
return MTLResourceStorageModeShared;
} else {
return MTLResourceStorageModeManaged;
}
#else
// MTLResourceStorageModeManaged is not even defined on iOS SDK
return MTLResourceStorageModeShared;
#endif
}
_SOKOL_PRIVATE MTLResourceOptions _sg_mtl_buffer_resource_options(sg_usage usg) {
switch (usg) {
case SG_USAGE_IMMUTABLE:
return _sg_mtl_resource_options_storage_mode_managed_or_shared();
case SG_USAGE_DYNAMIC:
case SG_USAGE_STREAM:
return MTLResourceCPUCacheModeWriteCombined | _sg_mtl_resource_options_storage_mode_managed_or_shared();
default:
SOKOL_UNREACHABLE;
return 0;
}
}
_SOKOL_PRIVATE MTLVertexStepFunction _sg_mtl_step_function(sg_vertex_step step) {
switch (step) {
case SG_VERTEXSTEP_PER_VERTEX: return MTLVertexStepFunctionPerVertex;
case SG_VERTEXSTEP_PER_INSTANCE: return MTLVertexStepFunctionPerInstance;
default: SOKOL_UNREACHABLE; return (MTLVertexStepFunction)0;
}
}
_SOKOL_PRIVATE MTLVertexFormat _sg_mtl_vertex_format(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_FLOAT: return MTLVertexFormatFloat;
case SG_VERTEXFORMAT_FLOAT2: return MTLVertexFormatFloat2;
case SG_VERTEXFORMAT_FLOAT3: return MTLVertexFormatFloat3;
case SG_VERTEXFORMAT_FLOAT4: return MTLVertexFormatFloat4;
case SG_VERTEXFORMAT_BYTE4: return MTLVertexFormatChar4;
case SG_VERTEXFORMAT_BYTE4N: return MTLVertexFormatChar4Normalized;
case SG_VERTEXFORMAT_UBYTE4: return MTLVertexFormatUChar4;
case SG_VERTEXFORMAT_UBYTE4N: return MTLVertexFormatUChar4Normalized;
case SG_VERTEXFORMAT_SHORT2: return MTLVertexFormatShort2;
case SG_VERTEXFORMAT_SHORT2N: return MTLVertexFormatShort2Normalized;
case SG_VERTEXFORMAT_USHORT2N: return MTLVertexFormatUShort2Normalized;
case SG_VERTEXFORMAT_SHORT4: return MTLVertexFormatShort4;
case SG_VERTEXFORMAT_SHORT4N: return MTLVertexFormatShort4Normalized;
case SG_VERTEXFORMAT_USHORT4N: return MTLVertexFormatUShort4Normalized;
case SG_VERTEXFORMAT_UINT10_N2: return MTLVertexFormatUInt1010102Normalized;
case SG_VERTEXFORMAT_HALF2: return MTLVertexFormatHalf2;
case SG_VERTEXFORMAT_HALF4: return MTLVertexFormatHalf4;
default: SOKOL_UNREACHABLE; return (MTLVertexFormat)0;
}
}
_SOKOL_PRIVATE MTLPrimitiveType _sg_mtl_primitive_type(sg_primitive_type t) {
switch (t) {
case SG_PRIMITIVETYPE_POINTS: return MTLPrimitiveTypePoint;
case SG_PRIMITIVETYPE_LINES: return MTLPrimitiveTypeLine;
case SG_PRIMITIVETYPE_LINE_STRIP: return MTLPrimitiveTypeLineStrip;
case SG_PRIMITIVETYPE_TRIANGLES: return MTLPrimitiveTypeTriangle;
case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return MTLPrimitiveTypeTriangleStrip;
default: SOKOL_UNREACHABLE; return (MTLPrimitiveType)0;
}
}
_SOKOL_PRIVATE MTLPixelFormat _sg_mtl_pixel_format(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8: return MTLPixelFormatR8Unorm;
case SG_PIXELFORMAT_R8SN: return MTLPixelFormatR8Snorm;
case SG_PIXELFORMAT_R8UI: return MTLPixelFormatR8Uint;
case SG_PIXELFORMAT_R8SI: return MTLPixelFormatR8Sint;
case SG_PIXELFORMAT_R16: return MTLPixelFormatR16Unorm;
case SG_PIXELFORMAT_R16SN: return MTLPixelFormatR16Snorm;
case SG_PIXELFORMAT_R16UI: return MTLPixelFormatR16Uint;
case SG_PIXELFORMAT_R16SI: return MTLPixelFormatR16Sint;
case SG_PIXELFORMAT_R16F: return MTLPixelFormatR16Float;
case SG_PIXELFORMAT_RG8: return MTLPixelFormatRG8Unorm;
case SG_PIXELFORMAT_RG8SN: return MTLPixelFormatRG8Snorm;
case SG_PIXELFORMAT_RG8UI: return MTLPixelFormatRG8Uint;
case SG_PIXELFORMAT_RG8SI: return MTLPixelFormatRG8Sint;
case SG_PIXELFORMAT_R32UI: return MTLPixelFormatR32Uint;
case SG_PIXELFORMAT_R32SI: return MTLPixelFormatR32Sint;
case SG_PIXELFORMAT_R32F: return MTLPixelFormatR32Float;
case SG_PIXELFORMAT_RG16: return MTLPixelFormatRG16Unorm;
case SG_PIXELFORMAT_RG16SN: return MTLPixelFormatRG16Snorm;
case SG_PIXELFORMAT_RG16UI: return MTLPixelFormatRG16Uint;
case SG_PIXELFORMAT_RG16SI: return MTLPixelFormatRG16Sint;
case SG_PIXELFORMAT_RG16F: return MTLPixelFormatRG16Float;
case SG_PIXELFORMAT_RGBA8: return MTLPixelFormatRGBA8Unorm;
case SG_PIXELFORMAT_SRGB8A8: return MTLPixelFormatRGBA8Unorm_sRGB;
case SG_PIXELFORMAT_RGBA8SN: return MTLPixelFormatRGBA8Snorm;
case SG_PIXELFORMAT_RGBA8UI: return MTLPixelFormatRGBA8Uint;
case SG_PIXELFORMAT_RGBA8SI: return MTLPixelFormatRGBA8Sint;
case SG_PIXELFORMAT_BGRA8: return MTLPixelFormatBGRA8Unorm;
case SG_PIXELFORMAT_RGB10A2: return MTLPixelFormatRGB10A2Unorm;
case SG_PIXELFORMAT_RG11B10F: return MTLPixelFormatRG11B10Float;
case SG_PIXELFORMAT_RGB9E5: return MTLPixelFormatRGB9E5Float;
case SG_PIXELFORMAT_RG32UI: return MTLPixelFormatRG32Uint;
case SG_PIXELFORMAT_RG32SI: return MTLPixelFormatRG32Sint;
case SG_PIXELFORMAT_RG32F: return MTLPixelFormatRG32Float;
case SG_PIXELFORMAT_RGBA16: return MTLPixelFormatRGBA16Unorm;
case SG_PIXELFORMAT_RGBA16SN: return MTLPixelFormatRGBA16Snorm;
case SG_PIXELFORMAT_RGBA16UI: return MTLPixelFormatRGBA16Uint;
case SG_PIXELFORMAT_RGBA16SI: return MTLPixelFormatRGBA16Sint;
case SG_PIXELFORMAT_RGBA16F: return MTLPixelFormatRGBA16Float;
case SG_PIXELFORMAT_RGBA32UI: return MTLPixelFormatRGBA32Uint;
case SG_PIXELFORMAT_RGBA32SI: return MTLPixelFormatRGBA32Sint;
case SG_PIXELFORMAT_RGBA32F: return MTLPixelFormatRGBA32Float;
case SG_PIXELFORMAT_DEPTH: return MTLPixelFormatDepth32Float;
case SG_PIXELFORMAT_DEPTH_STENCIL: return MTLPixelFormatDepth32Float_Stencil8;
#if defined(_SG_TARGET_MACOS)
case SG_PIXELFORMAT_BC1_RGBA: return MTLPixelFormatBC1_RGBA;
case SG_PIXELFORMAT_BC2_RGBA: return MTLPixelFormatBC2_RGBA;
case SG_PIXELFORMAT_BC3_RGBA: return MTLPixelFormatBC3_RGBA;
case SG_PIXELFORMAT_BC3_SRGBA: return MTLPixelFormatBC3_RGBA_sRGB;
case SG_PIXELFORMAT_BC4_R: return MTLPixelFormatBC4_RUnorm;
case SG_PIXELFORMAT_BC4_RSN: return MTLPixelFormatBC4_RSnorm;
case SG_PIXELFORMAT_BC5_RG: return MTLPixelFormatBC5_RGUnorm;
case SG_PIXELFORMAT_BC5_RGSN: return MTLPixelFormatBC5_RGSnorm;
case SG_PIXELFORMAT_BC6H_RGBF: return MTLPixelFormatBC6H_RGBFloat;
case SG_PIXELFORMAT_BC6H_RGBUF: return MTLPixelFormatBC6H_RGBUfloat;
case SG_PIXELFORMAT_BC7_RGBA: return MTLPixelFormatBC7_RGBAUnorm;
case SG_PIXELFORMAT_BC7_SRGBA: return MTLPixelFormatBC7_RGBAUnorm_sRGB;
#else
case SG_PIXELFORMAT_PVRTC_RGB_2BPP: return MTLPixelFormatPVRTC_RGB_2BPP;
case SG_PIXELFORMAT_PVRTC_RGB_4BPP: return MTLPixelFormatPVRTC_RGB_4BPP;
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: return MTLPixelFormatPVRTC_RGBA_2BPP;
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: return MTLPixelFormatPVRTC_RGBA_4BPP;
case SG_PIXELFORMAT_ETC2_RGB8: return MTLPixelFormatETC2_RGB8;
case SG_PIXELFORMAT_ETC2_SRGB8: return MTLPixelFormatETC2_RGB8_sRGB;
case SG_PIXELFORMAT_ETC2_RGB8A1: return MTLPixelFormatETC2_RGB8A1;
case SG_PIXELFORMAT_ETC2_RGBA8: return MTLPixelFormatEAC_RGBA8;
case SG_PIXELFORMAT_ETC2_SRGB8A8: return MTLPixelFormatEAC_RGBA8_sRGB;
case SG_PIXELFORMAT_EAC_R11: return MTLPixelFormatEAC_R11Unorm;
case SG_PIXELFORMAT_EAC_R11SN: return MTLPixelFormatEAC_R11Snorm;
case SG_PIXELFORMAT_EAC_RG11: return MTLPixelFormatEAC_RG11Unorm;
case SG_PIXELFORMAT_EAC_RG11SN: return MTLPixelFormatEAC_RG11Snorm;
case SG_PIXELFORMAT_ASTC_4x4_RGBA: return MTLPixelFormatASTC_4x4_LDR;
case SG_PIXELFORMAT_ASTC_4x4_SRGBA: return MTLPixelFormatASTC_4x4_sRGB;
#endif
default: return MTLPixelFormatInvalid;
}
}
_SOKOL_PRIVATE MTLColorWriteMask _sg_mtl_color_write_mask(sg_color_mask m) {
MTLColorWriteMask mtl_mask = MTLColorWriteMaskNone;
if (m & SG_COLORMASK_R) {
mtl_mask |= MTLColorWriteMaskRed;
}
if (m & SG_COLORMASK_G) {
mtl_mask |= MTLColorWriteMaskGreen;
}
if (m & SG_COLORMASK_B) {
mtl_mask |= MTLColorWriteMaskBlue;
}
if (m & SG_COLORMASK_A) {
mtl_mask |= MTLColorWriteMaskAlpha;
}
return mtl_mask;
}
_SOKOL_PRIVATE MTLBlendOperation _sg_mtl_blend_op(sg_blend_op op) {
switch (op) {
case SG_BLENDOP_ADD: return MTLBlendOperationAdd;
case SG_BLENDOP_SUBTRACT: return MTLBlendOperationSubtract;
case SG_BLENDOP_REVERSE_SUBTRACT: return MTLBlendOperationReverseSubtract;
default: SOKOL_UNREACHABLE; return (MTLBlendOperation)0;
}
}
_SOKOL_PRIVATE MTLBlendFactor _sg_mtl_blend_factor(sg_blend_factor f) {
switch (f) {
case SG_BLENDFACTOR_ZERO: return MTLBlendFactorZero;
case SG_BLENDFACTOR_ONE: return MTLBlendFactorOne;
case SG_BLENDFACTOR_SRC_COLOR: return MTLBlendFactorSourceColor;
case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return MTLBlendFactorOneMinusSourceColor;
case SG_BLENDFACTOR_SRC_ALPHA: return MTLBlendFactorSourceAlpha;
case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return MTLBlendFactorOneMinusSourceAlpha;
case SG_BLENDFACTOR_DST_COLOR: return MTLBlendFactorDestinationColor;
case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return MTLBlendFactorOneMinusDestinationColor;
case SG_BLENDFACTOR_DST_ALPHA: return MTLBlendFactorDestinationAlpha;
case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return MTLBlendFactorOneMinusDestinationAlpha;
case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return MTLBlendFactorSourceAlphaSaturated;
case SG_BLENDFACTOR_BLEND_COLOR: return MTLBlendFactorBlendColor;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return MTLBlendFactorOneMinusBlendColor;
case SG_BLENDFACTOR_BLEND_ALPHA: return MTLBlendFactorBlendAlpha;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return MTLBlendFactorOneMinusBlendAlpha;
default: SOKOL_UNREACHABLE; return (MTLBlendFactor)0;
}
}
_SOKOL_PRIVATE MTLCompareFunction _sg_mtl_compare_func(sg_compare_func f) {
switch (f) {
case SG_COMPAREFUNC_NEVER: return MTLCompareFunctionNever;
case SG_COMPAREFUNC_LESS: return MTLCompareFunctionLess;
case SG_COMPAREFUNC_EQUAL: return MTLCompareFunctionEqual;
case SG_COMPAREFUNC_LESS_EQUAL: return MTLCompareFunctionLessEqual;
case SG_COMPAREFUNC_GREATER: return MTLCompareFunctionGreater;
case SG_COMPAREFUNC_NOT_EQUAL: return MTLCompareFunctionNotEqual;
case SG_COMPAREFUNC_GREATER_EQUAL: return MTLCompareFunctionGreaterEqual;
case SG_COMPAREFUNC_ALWAYS: return MTLCompareFunctionAlways;
default: SOKOL_UNREACHABLE; return (MTLCompareFunction)0;
}
}
_SOKOL_PRIVATE MTLStencilOperation _sg_mtl_stencil_op(sg_stencil_op op) {
switch (op) {
case SG_STENCILOP_KEEP: return MTLStencilOperationKeep;
case SG_STENCILOP_ZERO: return MTLStencilOperationZero;
case SG_STENCILOP_REPLACE: return MTLStencilOperationReplace;
case SG_STENCILOP_INCR_CLAMP: return MTLStencilOperationIncrementClamp;
case SG_STENCILOP_DECR_CLAMP: return MTLStencilOperationDecrementClamp;
case SG_STENCILOP_INVERT: return MTLStencilOperationInvert;
case SG_STENCILOP_INCR_WRAP: return MTLStencilOperationIncrementWrap;
case SG_STENCILOP_DECR_WRAP: return MTLStencilOperationDecrementWrap;
default: SOKOL_UNREACHABLE; return (MTLStencilOperation)0;
}
}
_SOKOL_PRIVATE MTLCullMode _sg_mtl_cull_mode(sg_cull_mode m) {
switch (m) {
case SG_CULLMODE_NONE: return MTLCullModeNone;
case SG_CULLMODE_FRONT: return MTLCullModeFront;
case SG_CULLMODE_BACK: return MTLCullModeBack;
default: SOKOL_UNREACHABLE; return (MTLCullMode)0;
}
}
_SOKOL_PRIVATE MTLWinding _sg_mtl_winding(sg_face_winding w) {
switch (w) {
case SG_FACEWINDING_CW: return MTLWindingClockwise;
case SG_FACEWINDING_CCW: return MTLWindingCounterClockwise;
default: SOKOL_UNREACHABLE; return (MTLWinding)0;
}
}
_SOKOL_PRIVATE MTLIndexType _sg_mtl_index_type(sg_index_type t) {
switch (t) {
case SG_INDEXTYPE_UINT16: return MTLIndexTypeUInt16;
case SG_INDEXTYPE_UINT32: return MTLIndexTypeUInt32;
default: SOKOL_UNREACHABLE; return (MTLIndexType)0;
}
}
_SOKOL_PRIVATE int _sg_mtl_index_size(sg_index_type t) {
switch (t) {
case SG_INDEXTYPE_NONE: return 0;
case SG_INDEXTYPE_UINT16: return 2;
case SG_INDEXTYPE_UINT32: return 4;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE MTLTextureType _sg_mtl_texture_type(sg_image_type t) {
switch (t) {
case SG_IMAGETYPE_2D: return MTLTextureType2D;
case SG_IMAGETYPE_CUBE: return MTLTextureTypeCube;
case SG_IMAGETYPE_3D: return MTLTextureType3D;
case SG_IMAGETYPE_ARRAY: return MTLTextureType2DArray;
default: SOKOL_UNREACHABLE; return (MTLTextureType)0;
}
}
_SOKOL_PRIVATE bool _sg_mtl_is_pvrtc(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
return true;
default:
return false;
}
}
_SOKOL_PRIVATE MTLSamplerAddressMode _sg_mtl_address_mode(sg_wrap w) {
if (_sg.features.image_clamp_to_border) {
if (@available(macOS 12.0, iOS 14.0, *)) {
// border color feature available
switch (w) {
case SG_WRAP_REPEAT: return MTLSamplerAddressModeRepeat;
case SG_WRAP_CLAMP_TO_EDGE: return MTLSamplerAddressModeClampToEdge;
case SG_WRAP_CLAMP_TO_BORDER: return MTLSamplerAddressModeClampToBorderColor;
case SG_WRAP_MIRRORED_REPEAT: return MTLSamplerAddressModeMirrorRepeat;
default: SOKOL_UNREACHABLE; return (MTLSamplerAddressMode)0;
}
}
}
// fallthrough: clamp to border no supported
switch (w) {
case SG_WRAP_REPEAT: return MTLSamplerAddressModeRepeat;
case SG_WRAP_CLAMP_TO_EDGE: return MTLSamplerAddressModeClampToEdge;
case SG_WRAP_CLAMP_TO_BORDER: return MTLSamplerAddressModeClampToEdge;
case SG_WRAP_MIRRORED_REPEAT: return MTLSamplerAddressModeMirrorRepeat;
default: SOKOL_UNREACHABLE; return (MTLSamplerAddressMode)0;
}
}
_SOKOL_PRIVATE API_AVAILABLE(ios(14.0), macos(12.0)) MTLSamplerBorderColor _sg_mtl_border_color(sg_border_color c) {
switch (c) {
case SG_BORDERCOLOR_TRANSPARENT_BLACK: return MTLSamplerBorderColorTransparentBlack;
case SG_BORDERCOLOR_OPAQUE_BLACK: return MTLSamplerBorderColorOpaqueBlack;
case SG_BORDERCOLOR_OPAQUE_WHITE: return MTLSamplerBorderColorOpaqueWhite;
default: SOKOL_UNREACHABLE; return (MTLSamplerBorderColor)0;
}
}
_SOKOL_PRIVATE MTLSamplerMinMagFilter _sg_mtl_minmag_filter(sg_filter f) {
switch (f) {
case SG_FILTER_NEAREST:
return MTLSamplerMinMagFilterNearest;
case SG_FILTER_LINEAR:
return MTLSamplerMinMagFilterLinear;
default:
SOKOL_UNREACHABLE; return (MTLSamplerMinMagFilter)0;
}
}
_SOKOL_PRIVATE MTLSamplerMipFilter _sg_mtl_mipmap_filter(sg_filter f) {
switch (f) {
case SG_FILTER_NEAREST:
return MTLSamplerMipFilterNearest;
case SG_FILTER_LINEAR:
return MTLSamplerMipFilterLinear;
default:
SOKOL_UNREACHABLE; return (MTLSamplerMipFilter)0;
}
}
//-- a pool for all Metal resource objects, with deferred release queue ---------
_SOKOL_PRIVATE void _sg_mtl_init_pool(const sg_desc* desc) {
_sg.mtl.idpool.num_slots = 2 *
(
2 * desc->buffer_pool_size +
4 * desc->image_pool_size +
1 * desc->sampler_pool_size +
4 * desc->shader_pool_size +
2 * desc->pipeline_pool_size +
desc->attachments_pool_size +
128
);
_sg.mtl.idpool.pool = [NSMutableArray arrayWithCapacity:(NSUInteger)_sg.mtl.idpool.num_slots];
_SG_OBJC_RETAIN(_sg.mtl.idpool.pool);
NSNull* null = [NSNull null];
for (int i = 0; i < _sg.mtl.idpool.num_slots; i++) {
[_sg.mtl.idpool.pool addObject:null];
}
SOKOL_ASSERT([_sg.mtl.idpool.pool count] == (NSUInteger)_sg.mtl.idpool.num_slots);
// a queue of currently free slot indices
_sg.mtl.idpool.free_queue_top = 0;
_sg.mtl.idpool.free_queue = (int*)_sg_malloc_clear((size_t)_sg.mtl.idpool.num_slots * sizeof(int));
// pool slot 0 is reserved!
for (int i = _sg.mtl.idpool.num_slots-1; i >= 1; i--) {
_sg.mtl.idpool.free_queue[_sg.mtl.idpool.free_queue_top++] = i;
}
// a circular queue which holds release items (frame index when a resource is to be released, and the resource's pool index
_sg.mtl.idpool.release_queue_front = 0;
_sg.mtl.idpool.release_queue_back = 0;
_sg.mtl.idpool.release_queue = (_sg_mtl_release_item_t*)_sg_malloc_clear((size_t)_sg.mtl.idpool.num_slots * sizeof(_sg_mtl_release_item_t));
for (int i = 0; i < _sg.mtl.idpool.num_slots; i++) {
_sg.mtl.idpool.release_queue[i].frame_index = 0;
_sg.mtl.idpool.release_queue[i].slot_index = _SG_MTL_INVALID_SLOT_INDEX;
}
}
_SOKOL_PRIVATE void _sg_mtl_destroy_pool(void) {
_sg_free(_sg.mtl.idpool.release_queue); _sg.mtl.idpool.release_queue = 0;
_sg_free(_sg.mtl.idpool.free_queue); _sg.mtl.idpool.free_queue = 0;
_SG_OBJC_RELEASE(_sg.mtl.idpool.pool);
}
// get a new free resource pool slot
_SOKOL_PRIVATE int _sg_mtl_alloc_pool_slot(void) {
SOKOL_ASSERT(_sg.mtl.idpool.free_queue_top > 0);
const int slot_index = _sg.mtl.idpool.free_queue[--_sg.mtl.idpool.free_queue_top];
SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots));
return slot_index;
}
// put a free resource pool slot back into the free-queue
_SOKOL_PRIVATE void _sg_mtl_free_pool_slot(int slot_index) {
SOKOL_ASSERT(_sg.mtl.idpool.free_queue_top < _sg.mtl.idpool.num_slots);
SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots));
_sg.mtl.idpool.free_queue[_sg.mtl.idpool.free_queue_top++] = slot_index;
}
// add an MTLResource to the pool, return pool index or 0 if input was 'nil'
_SOKOL_PRIVATE int _sg_mtl_add_resource(id res) {
if (nil == res) {
return _SG_MTL_INVALID_SLOT_INDEX;
}
_sg_stats_add(metal.idpool.num_added, 1);
const int slot_index = _sg_mtl_alloc_pool_slot();
// NOTE: the NSMutableArray will take ownership of its items
SOKOL_ASSERT([NSNull null] == _sg.mtl.idpool.pool[(NSUInteger)slot_index]);
_sg.mtl.idpool.pool[(NSUInteger)slot_index] = res;
return slot_index;
}
/* mark an MTLResource for release, this will put the resource into the
deferred-release queue, and the resource will then be released N frames later,
the special pool index 0 will be ignored (this means that a nil
value was provided to _sg_mtl_add_resource()
*/
_SOKOL_PRIVATE void _sg_mtl_release_resource(uint32_t frame_index, int slot_index) {
if (slot_index == _SG_MTL_INVALID_SLOT_INDEX) {
return;
}
_sg_stats_add(metal.idpool.num_released, 1);
SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots));
SOKOL_ASSERT([NSNull null] != _sg.mtl.idpool.pool[(NSUInteger)slot_index]);
int release_index = _sg.mtl.idpool.release_queue_front++;
if (_sg.mtl.idpool.release_queue_front >= _sg.mtl.idpool.num_slots) {
// wrap-around
_sg.mtl.idpool.release_queue_front = 0;
}
// release queue full?
SOKOL_ASSERT(_sg.mtl.idpool.release_queue_front != _sg.mtl.idpool.release_queue_back);
SOKOL_ASSERT(0 == _sg.mtl.idpool.release_queue[release_index].frame_index);
const uint32_t safe_to_release_frame_index = frame_index + SG_NUM_INFLIGHT_FRAMES + 1;
_sg.mtl.idpool.release_queue[release_index].frame_index = safe_to_release_frame_index;
_sg.mtl.idpool.release_queue[release_index].slot_index = slot_index;
}
// run garbage-collection pass on all resources in the release-queue
_SOKOL_PRIVATE void _sg_mtl_garbage_collect(uint32_t frame_index) {
while (_sg.mtl.idpool.release_queue_back != _sg.mtl.idpool.release_queue_front) {
if (frame_index < _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].frame_index) {
// don't need to check further, release-items past this are too young
break;
}
_sg_stats_add(metal.idpool.num_garbage_collected, 1);
// safe to release this resource
const int slot_index = _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].slot_index;
SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots));
// note: the NSMutableArray takes ownership of its items, assigning an NSNull object will
// release the object, no matter if using ARC or not
SOKOL_ASSERT(_sg.mtl.idpool.pool[(NSUInteger)slot_index] != [NSNull null]);
_sg.mtl.idpool.pool[(NSUInteger)slot_index] = [NSNull null];
// put the now free pool index back on the free queue
_sg_mtl_free_pool_slot(slot_index);
// reset the release queue slot and advance the back index
_sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].frame_index = 0;
_sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].slot_index = _SG_MTL_INVALID_SLOT_INDEX;
_sg.mtl.idpool.release_queue_back++;
if (_sg.mtl.idpool.release_queue_back >= _sg.mtl.idpool.num_slots) {
// wrap-around
_sg.mtl.idpool.release_queue_back = 0;
}
}
}
_SOKOL_PRIVATE id _sg_mtl_id(int slot_index) {
return _sg.mtl.idpool.pool[(NSUInteger)slot_index];
}
_SOKOL_PRIVATE void _sg_mtl_clear_state_cache(void) {
_sg_clear(&_sg.mtl.state_cache, sizeof(_sg.mtl.state_cache));
}
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
_SOKOL_PRIVATE void _sg_mtl_init_caps(void) {
#if defined(_SG_TARGET_MACOS)
_sg.backend = SG_BACKEND_METAL_MACOS;
#elif defined(_SG_TARGET_IOS)
#if defined(_SG_TARGET_IOS_SIMULATOR)
_sg.backend = SG_BACKEND_METAL_SIMULATOR;
#else
_sg.backend = SG_BACKEND_METAL_IOS;
#endif
#endif
_sg.features.origin_top_left = true;
_sg.features.mrt_independent_blend_state = true;
_sg.features.mrt_independent_write_mask = true;
_sg.features.storage_buffer = true;
_sg.features.image_clamp_to_border = false;
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 120000) || (__IPHONE_OS_VERSION_MAX_ALLOWED >= 140000)
if (@available(macOS 12.0, iOS 14.0, *)) {
_sg.features.image_clamp_to_border = [_sg.mtl.device supportsFamily:MTLGPUFamilyApple7]
|| [_sg.mtl.device supportsFamily:MTLGPUFamilyMac2];
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 130000) || (__IPHONE_OS_VERSION_MAX_ALLOWED >= 160000)
if (!_sg.features.image_clamp_to_border) {
if (@available(macOS 13.0, iOS 16.0, *)) {
_sg.features.image_clamp_to_border = [_sg.mtl.device supportsFamily:MTLGPUFamilyMetal3];
}
}
#endif
}
#endif
#if defined(_SG_TARGET_MACOS)
_sg.limits.max_image_size_2d = 16 * 1024;
_sg.limits.max_image_size_cube = 16 * 1024;
_sg.limits.max_image_size_3d = 2 * 1024;
_sg.limits.max_image_size_array = 16 * 1024;
_sg.limits.max_image_array_layers = 2 * 1024;
#else
// FIXME: newer iOS devices support 16k textures
_sg.limits.max_image_size_2d = 8 * 1024;
_sg.limits.max_image_size_cube = 8 * 1024;
_sg.limits.max_image_size_3d = 2 * 1024;
_sg.limits.max_image_size_array = 8 * 1024;
_sg.limits.max_image_array_layers = 2 * 1024;
#endif
_sg.limits.max_vertex_attrs = SG_MAX_VERTEX_ATTRIBUTES;
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8SI]);
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16SN]);
#else
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_R16]);
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_R16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32SI]);
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R32F]);
#else
_sg_pixelformat_sbr(&_sg.formats[SG_PIXELFORMAT_R32F]);
#endif
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16SN]);
#else
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RG16]);
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RG16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_SRGB8A8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_BGRA8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB10A2]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG11B10F]);
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGB9E5]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32SI]);
#else
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB9E5]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32SI]);
#endif
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG32F]);
#else
_sg_pixelformat_sbr(&_sg.formats[SG_PIXELFORMAT_RG32F]);
#endif
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16SN]);
#else
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RGBA16]);
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RGBA16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
#else
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
#endif
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH]);
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL]);
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC1_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC2_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_SRGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_R]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_RSN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RG]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RGSN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBUF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_SRGBA]);
#else
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_2BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_4BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_2BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_4BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8A1]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGBA8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8A8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_R11]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_R11SN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_RG11]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_RG11SN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_SRGBA]);
#endif
}
//-- main Metal backend state and functions ------------------------------------
_SOKOL_PRIVATE void _sg_mtl_setup_backend(const sg_desc* desc) {
// assume already zero-initialized
SOKOL_ASSERT(desc);
SOKOL_ASSERT(desc->environment.metal.device);
SOKOL_ASSERT(desc->uniform_buffer_size > 0);
_sg_mtl_init_pool(desc);
_sg_mtl_clear_state_cache();
_sg.mtl.valid = true;
_sg.mtl.ub_size = desc->uniform_buffer_size;
_sg.mtl.sem = dispatch_semaphore_create(SG_NUM_INFLIGHT_FRAMES);
_sg.mtl.device = (__bridge id<MTLDevice>) desc->environment.metal.device;
_sg.mtl.cmd_queue = [_sg.mtl.device newCommandQueue];
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
_sg.mtl.uniform_buffers[i] = [_sg.mtl.device
newBufferWithLength:(NSUInteger)_sg.mtl.ub_size
options:MTLResourceCPUCacheModeWriteCombined|MTLResourceStorageModeShared
];
#if defined(SOKOL_DEBUG)
_sg.mtl.uniform_buffers[i].label = [NSString stringWithFormat:@"sg-uniform-buffer.%d", i];
#endif
}
if (desc->mtl_force_managed_storage_mode) {
_sg.mtl.use_shared_storage_mode = false;
} else if (@available(macOS 10.15, iOS 13.0, *)) {
// on Intel Macs, always use managed resources even though the
// device says it supports unified memory (because of texture restrictions)
const bool is_apple_gpu = [_sg.mtl.device supportsFamily:MTLGPUFamilyApple1];
if (!is_apple_gpu) {
_sg.mtl.use_shared_storage_mode = false;
} else {
_sg.mtl.use_shared_storage_mode = true;
}
} else {
#if defined(_SG_TARGET_MACOS)
_sg.mtl.use_shared_storage_mode = false;
#else
_sg.mtl.use_shared_storage_mode = true;
#endif
}
_sg_mtl_init_caps();
}
_SOKOL_PRIVATE void _sg_mtl_discard_backend(void) {
SOKOL_ASSERT(_sg.mtl.valid);
// wait for the last frame to finish
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
dispatch_semaphore_wait(_sg.mtl.sem, DISPATCH_TIME_FOREVER);
}
// semaphore must be "relinquished" before destruction
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
dispatch_semaphore_signal(_sg.mtl.sem);
}
_sg_mtl_garbage_collect(_sg.frame_index + SG_NUM_INFLIGHT_FRAMES + 2);
_sg_mtl_destroy_pool();
_sg.mtl.valid = false;
_SG_OBJC_RELEASE(_sg.mtl.sem);
_SG_OBJC_RELEASE(_sg.mtl.device);
_SG_OBJC_RELEASE(_sg.mtl.cmd_queue);
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
_SG_OBJC_RELEASE(_sg.mtl.uniform_buffers[i]);
}
// NOTE: MTLCommandBuffer and MTLRenderCommandEncoder are auto-released
_sg.mtl.cmd_buffer = nil;
_sg.mtl.cmd_encoder = nil;
}
_SOKOL_PRIVATE void _sg_mtl_bind_uniform_buffers(void) {
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
for (int slot = 0; slot < SG_MAX_SHADERSTAGE_UBS; slot++) {
[_sg.mtl.cmd_encoder
setVertexBuffer:_sg.mtl.uniform_buffers[_sg.mtl.cur_frame_rotate_index]
offset:0
atIndex:(NSUInteger)slot];
[_sg.mtl.cmd_encoder
setFragmentBuffer:_sg.mtl.uniform_buffers[_sg.mtl.cur_frame_rotate_index]
offset:0
atIndex:(NSUInteger)slot];
}
}
_SOKOL_PRIVATE void _sg_mtl_reset_state_cache(void) {
_sg_mtl_clear_state_cache();
// need to restore the uniform buffer binding (normally happens in _sg_mtl_begin_pass()
if (nil != _sg.mtl.cmd_encoder) {
_sg_mtl_bind_uniform_buffers();
}
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && desc);
const bool injected = (0 != desc->mtl_buffers[0]);
MTLResourceOptions mtl_options = _sg_mtl_buffer_resource_options(buf->cmn.usage);
for (int slot = 0; slot < buf->cmn.num_slots; slot++) {
id<MTLBuffer> mtl_buf;
if (injected) {
SOKOL_ASSERT(desc->mtl_buffers[slot]);
mtl_buf = (__bridge id<MTLBuffer>) desc->mtl_buffers[slot];
} else {
if (buf->cmn.usage == SG_USAGE_IMMUTABLE) {
SOKOL_ASSERT(desc->data.ptr);
mtl_buf = [_sg.mtl.device newBufferWithBytes:desc->data.ptr length:(NSUInteger)buf->cmn.size options:mtl_options];
} else {
mtl_buf = [_sg.mtl.device newBufferWithLength:(NSUInteger)buf->cmn.size options:mtl_options];
}
if (nil == mtl_buf) {
_SG_ERROR(METAL_CREATE_BUFFER_FAILED);
return SG_RESOURCESTATE_FAILED;
}
}
#if defined(SOKOL_DEBUG)
if (desc->label) {
mtl_buf.label = [NSString stringWithFormat:@"%s.%d", desc->label, slot];
}
#endif
buf->mtl.buf[slot] = _sg_mtl_add_resource(mtl_buf);
_SG_OBJC_RELEASE(mtl_buf);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_discard_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
for (int slot = 0; slot < buf->cmn.num_slots; slot++) {
// it's valid to call release resource with '0'
_sg_mtl_release_resource(_sg.frame_index, buf->mtl.buf[slot]);
}
}
_SOKOL_PRIVATE void _sg_mtl_copy_image_data(const _sg_image_t* img, __unsafe_unretained id<MTLTexture> mtl_tex, const sg_image_data* data) {
const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1;
const int num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices : 1;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++) {
SOKOL_ASSERT(data->subimage[face_index][mip_index].ptr);
SOKOL_ASSERT(data->subimage[face_index][mip_index].size > 0);
const uint8_t* data_ptr = (const uint8_t*)data->subimage[face_index][mip_index].ptr;
const int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index);
const int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index);
// special case PVRTC formats: bytePerRow and bytesPerImage must be 0
int bytes_per_row = 0;
int bytes_per_slice = 0;
if (!_sg_mtl_is_pvrtc(img->cmn.pixel_format)) {
bytes_per_row = _sg_row_pitch(img->cmn.pixel_format, mip_width, 1);
bytes_per_slice = _sg_surface_pitch(img->cmn.pixel_format, mip_width, mip_height, 1);
}
/* bytesPerImage special case: https://developer.apple.com/documentation/metal/mtltexture/1515679-replaceregion
"Supply a nonzero value only when you copy data to a MTLTextureType3D type texture"
*/
MTLRegion region;
int bytes_per_image;
if (img->cmn.type == SG_IMAGETYPE_3D) {
const int mip_depth = _sg_miplevel_dim(img->cmn.num_slices, mip_index);
region = MTLRegionMake3D(0, 0, 0, (NSUInteger)mip_width, (NSUInteger)mip_height, (NSUInteger)mip_depth);
bytes_per_image = bytes_per_slice;
// FIXME: apparently the minimal bytes_per_image size for 3D texture is 4 KByte... somehow need to handle this
} else {
region = MTLRegionMake2D(0, 0, (NSUInteger)mip_width, (NSUInteger)mip_height);
bytes_per_image = 0;
}
for (int slice_index = 0; slice_index < num_slices; slice_index++) {
const int mtl_slice_index = (img->cmn.type == SG_IMAGETYPE_CUBE) ? face_index : slice_index;
const int slice_offset = slice_index * bytes_per_slice;
SOKOL_ASSERT((slice_offset + bytes_per_slice) <= (int)data->subimage[face_index][mip_index].size);
[mtl_tex replaceRegion:region
mipmapLevel:(NSUInteger)mip_index
slice:(NSUInteger)mtl_slice_index
withBytes:data_ptr + slice_offset
bytesPerRow:(NSUInteger)bytes_per_row
bytesPerImage:(NSUInteger)bytes_per_image];
}
}
}
}
// initialize MTLTextureDescriptor with common attributes
_SOKOL_PRIVATE bool _sg_mtl_init_texdesc_common(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) {
mtl_desc.textureType = _sg_mtl_texture_type(img->cmn.type);
mtl_desc.pixelFormat = _sg_mtl_pixel_format(img->cmn.pixel_format);
if (MTLPixelFormatInvalid == mtl_desc.pixelFormat) {
_SG_ERROR(METAL_TEXTURE_FORMAT_NOT_SUPPORTED);
return false;
}
mtl_desc.width = (NSUInteger)img->cmn.width;
mtl_desc.height = (NSUInteger)img->cmn.height;
if (SG_IMAGETYPE_3D == img->cmn.type) {
mtl_desc.depth = (NSUInteger)img->cmn.num_slices;
} else {
mtl_desc.depth = 1;
}
mtl_desc.mipmapLevelCount = (NSUInteger)img->cmn.num_mipmaps;
if (SG_IMAGETYPE_ARRAY == img->cmn.type) {
mtl_desc.arrayLength = (NSUInteger)img->cmn.num_slices;
} else {
mtl_desc.arrayLength = 1;
}
mtl_desc.usage = MTLTextureUsageShaderRead;
MTLResourceOptions res_options = 0;
if (img->cmn.usage != SG_USAGE_IMMUTABLE) {
res_options |= MTLResourceCPUCacheModeWriteCombined;
}
res_options |= _sg_mtl_resource_options_storage_mode_managed_or_shared();
mtl_desc.resourceOptions = res_options;
return true;
}
// initialize MTLTextureDescriptor with rendertarget attributes
_SOKOL_PRIVATE void _sg_mtl_init_texdesc_rt(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) {
SOKOL_ASSERT(img->cmn.render_target);
_SOKOL_UNUSED(img);
mtl_desc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
mtl_desc.resourceOptions = MTLResourceStorageModePrivate;
}
// initialize MTLTextureDescriptor with MSAA attributes
_SOKOL_PRIVATE void _sg_mtl_init_texdesc_rt_msaa(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) {
SOKOL_ASSERT(img->cmn.sample_count > 1);
mtl_desc.usage = MTLTextureUsageRenderTarget;
mtl_desc.resourceOptions = MTLResourceStorageModePrivate;
mtl_desc.textureType = MTLTextureType2DMultisample;
mtl_desc.sampleCount = (NSUInteger)img->cmn.sample_count;
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && desc);
const bool injected = (0 != desc->mtl_textures[0]);
// first initialize all Metal resource pool slots to 'empty'
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
img->mtl.tex[i] = _sg_mtl_add_resource(nil);
}
// initialize a Metal texture descriptor
MTLTextureDescriptor* mtl_desc = [[MTLTextureDescriptor alloc] init];
if (!_sg_mtl_init_texdesc_common(mtl_desc, img)) {
_SG_OBJC_RELEASE(mtl_desc);
return SG_RESOURCESTATE_FAILED;
}
if (img->cmn.render_target) {
if (img->cmn.sample_count > 1) {
_sg_mtl_init_texdesc_rt_msaa(mtl_desc, img);
} else {
_sg_mtl_init_texdesc_rt(mtl_desc, img);
}
}
for (int slot = 0; slot < img->cmn.num_slots; slot++) {
id<MTLTexture> mtl_tex;
if (injected) {
SOKOL_ASSERT(desc->mtl_textures[slot]);
mtl_tex = (__bridge id<MTLTexture>) desc->mtl_textures[slot];
} else {
mtl_tex = [_sg.mtl.device newTextureWithDescriptor:mtl_desc];
if (nil == mtl_tex) {
_SG_OBJC_RELEASE(mtl_desc);
_SG_ERROR(METAL_CREATE_TEXTURE_FAILED);
return SG_RESOURCESTATE_FAILED;
}
if ((img->cmn.usage == SG_USAGE_IMMUTABLE) && !img->cmn.render_target) {
_sg_mtl_copy_image_data(img, mtl_tex, &desc->data);
}
}
#if defined(SOKOL_DEBUG)
if (desc->label) {
mtl_tex.label = [NSString stringWithFormat:@"%s.%d", desc->label, slot];
}
#endif
img->mtl.tex[slot] = _sg_mtl_add_resource(mtl_tex);
_SG_OBJC_RELEASE(mtl_tex);
}
_SG_OBJC_RELEASE(mtl_desc);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_discard_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
// it's valid to call release resource with a 'null resource'
for (int slot = 0; slot < img->cmn.num_slots; slot++) {
_sg_mtl_release_resource(_sg.frame_index, img->mtl.tex[slot]);
}
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) {
SOKOL_ASSERT(smp && desc);
id<MTLSamplerState> mtl_smp;
const bool injected = (0 != desc->mtl_sampler);
if (injected) {
SOKOL_ASSERT(desc->mtl_sampler);
mtl_smp = (__bridge id<MTLSamplerState>) desc->mtl_sampler;
} else {
MTLSamplerDescriptor* mtl_desc = [[MTLSamplerDescriptor alloc] init];
mtl_desc.sAddressMode = _sg_mtl_address_mode(desc->wrap_u);
mtl_desc.tAddressMode = _sg_mtl_address_mode(desc->wrap_v);
mtl_desc.rAddressMode = _sg_mtl_address_mode(desc->wrap_w);
if (_sg.features.image_clamp_to_border) {
if (@available(macOS 12.0, iOS 14.0, *)) {
mtl_desc.borderColor = _sg_mtl_border_color(desc->border_color);
}
}
mtl_desc.minFilter = _sg_mtl_minmag_filter(desc->min_filter);
mtl_desc.magFilter = _sg_mtl_minmag_filter(desc->mag_filter);
mtl_desc.mipFilter = _sg_mtl_mipmap_filter(desc->mipmap_filter);
mtl_desc.lodMinClamp = desc->min_lod;
mtl_desc.lodMaxClamp = desc->max_lod;
// FIXME: lodAverage?
mtl_desc.maxAnisotropy = desc->max_anisotropy;
mtl_desc.normalizedCoordinates = YES;
mtl_desc.compareFunction = _sg_mtl_compare_func(desc->compare);
#if defined(SOKOL_DEBUG)
if (desc->label) {
mtl_desc.label = [NSString stringWithUTF8String:desc->label];
}
#endif
mtl_smp = [_sg.mtl.device newSamplerStateWithDescriptor:mtl_desc];
_SG_OBJC_RELEASE(mtl_desc);
if (nil == mtl_smp) {
_SG_ERROR(METAL_CREATE_SAMPLER_FAILED);
return SG_RESOURCESTATE_FAILED;
}
}
smp->mtl.sampler_state = _sg_mtl_add_resource(mtl_smp);
_SG_OBJC_RELEASE(mtl_smp);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_discard_sampler(_sg_sampler_t* smp) {
SOKOL_ASSERT(smp);
// it's valid to call release resource with a 'null resource'
_sg_mtl_release_resource(_sg.frame_index, smp->mtl.sampler_state);
}
_SOKOL_PRIVATE id<MTLLibrary> _sg_mtl_compile_library(const char* src) {
NSError* err = NULL;
id<MTLLibrary> lib = [_sg.mtl.device
newLibraryWithSource:[NSString stringWithUTF8String:src]
options:nil
error:&err
];
if (err) {
_SG_ERROR(METAL_SHADER_COMPILATION_FAILED);
_SG_LOGMSG(METAL_SHADER_COMPILATION_OUTPUT, [err.localizedDescription UTF8String]);
}
return lib;
}
_SOKOL_PRIVATE id<MTLLibrary> _sg_mtl_library_from_bytecode(const void* ptr, size_t num_bytes) {
NSError* err = NULL;
dispatch_data_t lib_data = dispatch_data_create(ptr, num_bytes, NULL, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
id<MTLLibrary> lib = [_sg.mtl.device newLibraryWithData:lib_data error:&err];
if (err) {
_SG_ERROR(METAL_SHADER_CREATION_FAILED);
_SG_LOGMSG(METAL_SHADER_COMPILATION_OUTPUT, [err.localizedDescription UTF8String]);
}
_SG_OBJC_RELEASE(lib_data);
return lib;
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && desc);
// create metal library objects and lookup entry functions
id<MTLLibrary> vs_lib = nil;
id<MTLLibrary> fs_lib = nil;
id<MTLFunction> vs_func = nil;
id<MTLFunction> fs_func = nil;
const char* vs_entry = desc->vs.entry;
const char* fs_entry = desc->fs.entry;
if (desc->vs.bytecode.ptr && desc->fs.bytecode.ptr) {
// separate byte code provided
vs_lib = _sg_mtl_library_from_bytecode(desc->vs.bytecode.ptr, desc->vs.bytecode.size);
fs_lib = _sg_mtl_library_from_bytecode(desc->fs.bytecode.ptr, desc->fs.bytecode.size);
if ((nil == vs_lib) || (nil == fs_lib)) {
goto failed;
}
vs_func = [vs_lib newFunctionWithName:[NSString stringWithUTF8String:vs_entry]];
fs_func = [fs_lib newFunctionWithName:[NSString stringWithUTF8String:fs_entry]];
} else if (desc->vs.source && desc->fs.source) {
// separate sources provided
vs_lib = _sg_mtl_compile_library(desc->vs.source);
fs_lib = _sg_mtl_compile_library(desc->fs.source);
if ((nil == vs_lib) || (nil == fs_lib)) {
goto failed;
}
vs_func = [vs_lib newFunctionWithName:[NSString stringWithUTF8String:vs_entry]];
fs_func = [fs_lib newFunctionWithName:[NSString stringWithUTF8String:fs_entry]];
} else {
goto failed;
}
if (nil == vs_func) {
_SG_ERROR(METAL_VERTEX_SHADER_ENTRY_NOT_FOUND);
goto failed;
}
if (nil == fs_func) {
_SG_ERROR(METAL_FRAGMENT_SHADER_ENTRY_NOT_FOUND);
goto failed;
}
#if defined(SOKOL_DEBUG)
if (desc->label) {
vs_lib.label = [NSString stringWithFormat:@"%s.vs", desc->label];
fs_lib.label = [NSString stringWithFormat:@"%s.fs", desc->label];
}
#endif
// it is legal to call _sg_mtl_add_resource with a nil value, this will return a special 0xFFFFFFFF index
shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_lib = _sg_mtl_add_resource(vs_lib);
_SG_OBJC_RELEASE(vs_lib);
shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_lib = _sg_mtl_add_resource(fs_lib);
_SG_OBJC_RELEASE(fs_lib);
shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func = _sg_mtl_add_resource(vs_func);
_SG_OBJC_RELEASE(vs_func);
shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func = _sg_mtl_add_resource(fs_func);
_SG_OBJC_RELEASE(fs_func);
return SG_RESOURCESTATE_VALID;
failed:
if (vs_lib != nil) {
_SG_OBJC_RELEASE(vs_lib);
}
if (fs_lib != nil) {
_SG_OBJC_RELEASE(fs_lib);
}
if (vs_func != nil) {
_SG_OBJC_RELEASE(vs_func);
}
if (fs_func != nil) {
_SG_OBJC_RELEASE(fs_func);
}
return SG_RESOURCESTATE_FAILED;
}
_SOKOL_PRIVATE void _sg_mtl_discard_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
// it is valid to call _sg_mtl_release_resource with a 'null resource'
_sg_mtl_release_resource(_sg.frame_index, shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func);
_sg_mtl_release_resource(_sg.frame_index, shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_lib);
_sg_mtl_release_resource(_sg.frame_index, shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func);
_sg_mtl_release_resource(_sg.frame_index, shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_lib);
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && shd && desc);
SOKOL_ASSERT(desc->shader.id == shd->slot.id);
pip->shader = shd;
sg_primitive_type prim_type = desc->primitive_type;
pip->mtl.prim_type = _sg_mtl_primitive_type(prim_type);
pip->mtl.index_size = _sg_mtl_index_size(pip->cmn.index_type);
if (SG_INDEXTYPE_NONE != pip->cmn.index_type) {
pip->mtl.index_type = _sg_mtl_index_type(pip->cmn.index_type);
}
pip->mtl.cull_mode = _sg_mtl_cull_mode(desc->cull_mode);
pip->mtl.winding = _sg_mtl_winding(desc->face_winding);
pip->mtl.stencil_ref = desc->stencil.ref;
// create vertex-descriptor
MTLVertexDescriptor* vtx_desc = [MTLVertexDescriptor vertexDescriptor];
for (NSUInteger attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
const sg_vertex_attr_state* a_state = &desc->layout.attrs[attr_index];
if (a_state->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS);
vtx_desc.attributes[attr_index].format = _sg_mtl_vertex_format(a_state->format);
vtx_desc.attributes[attr_index].offset = (NSUInteger)a_state->offset;
vtx_desc.attributes[attr_index].bufferIndex = (NSUInteger)(a_state->buffer_index + SG_MAX_SHADERSTAGE_UBS);
pip->cmn.vertex_buffer_layout_active[a_state->buffer_index] = true;
}
for (NSUInteger layout_index = 0; layout_index < SG_MAX_VERTEX_BUFFERS; layout_index++) {
if (pip->cmn.vertex_buffer_layout_active[layout_index]) {
const sg_vertex_buffer_layout_state* l_state = &desc->layout.buffers[layout_index];
const NSUInteger mtl_vb_slot = layout_index + SG_MAX_SHADERSTAGE_UBS;
SOKOL_ASSERT(l_state->stride > 0);
vtx_desc.layouts[mtl_vb_slot].stride = (NSUInteger)l_state->stride;
vtx_desc.layouts[mtl_vb_slot].stepFunction = _sg_mtl_step_function(l_state->step_func);
vtx_desc.layouts[mtl_vb_slot].stepRate = (NSUInteger)l_state->step_rate;
if (SG_VERTEXSTEP_PER_INSTANCE == l_state->step_func) {
// NOTE: not actually used in _sg_mtl_draw()
pip->cmn.use_instanced_draw = true;
}
}
}
// render-pipeline descriptor
MTLRenderPipelineDescriptor* rp_desc = [[MTLRenderPipelineDescriptor alloc] init];
rp_desc.vertexDescriptor = vtx_desc;
SOKOL_ASSERT(shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func != _SG_MTL_INVALID_SLOT_INDEX);
rp_desc.vertexFunction = _sg_mtl_id(shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func);
SOKOL_ASSERT(shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func != _SG_MTL_INVALID_SLOT_INDEX);
rp_desc.fragmentFunction = _sg_mtl_id(shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func);
rp_desc.rasterSampleCount = (NSUInteger)desc->sample_count;
rp_desc.alphaToCoverageEnabled = desc->alpha_to_coverage_enabled;
rp_desc.alphaToOneEnabled = NO;
rp_desc.rasterizationEnabled = YES;
rp_desc.depthAttachmentPixelFormat = _sg_mtl_pixel_format(desc->depth.pixel_format);
if (desc->depth.pixel_format == SG_PIXELFORMAT_DEPTH_STENCIL) {
rp_desc.stencilAttachmentPixelFormat = _sg_mtl_pixel_format(desc->depth.pixel_format);
}
if (@available(macOS 10.13, iOS 11.0, *)) {
for (NSUInteger i = 0; i < (SG_MAX_SHADERSTAGE_UBS+SG_MAX_VERTEX_BUFFERS); i++) {
rp_desc.vertexBuffers[i].mutability = MTLMutabilityImmutable;
}
for (NSUInteger i = 0; i < SG_MAX_SHADERSTAGE_UBS; i++) {
rp_desc.fragmentBuffers[i].mutability = MTLMutabilityImmutable;
}
}
for (NSUInteger i = 0; i < (NSUInteger)desc->color_count; i++) {
SOKOL_ASSERT(i < SG_MAX_COLOR_ATTACHMENTS);
const sg_color_target_state* cs = &desc->colors[i];
rp_desc.colorAttachments[i].pixelFormat = _sg_mtl_pixel_format(cs->pixel_format);
rp_desc.colorAttachments[i].writeMask = _sg_mtl_color_write_mask(cs->write_mask);
rp_desc.colorAttachments[i].blendingEnabled = cs->blend.enabled;
rp_desc.colorAttachments[i].alphaBlendOperation = _sg_mtl_blend_op(cs->blend.op_alpha);
rp_desc.colorAttachments[i].rgbBlendOperation = _sg_mtl_blend_op(cs->blend.op_rgb);
rp_desc.colorAttachments[i].destinationAlphaBlendFactor = _sg_mtl_blend_factor(cs->blend.dst_factor_alpha);
rp_desc.colorAttachments[i].destinationRGBBlendFactor = _sg_mtl_blend_factor(cs->blend.dst_factor_rgb);
rp_desc.colorAttachments[i].sourceAlphaBlendFactor = _sg_mtl_blend_factor(cs->blend.src_factor_alpha);
rp_desc.colorAttachments[i].sourceRGBBlendFactor = _sg_mtl_blend_factor(cs->blend.src_factor_rgb);
}
#if defined(SOKOL_DEBUG)
if (desc->label) {
rp_desc.label = [NSString stringWithFormat:@"%s", desc->label];
}
#endif
NSError* err = NULL;
id<MTLRenderPipelineState> mtl_rps = [_sg.mtl.device newRenderPipelineStateWithDescriptor:rp_desc error:&err];
_SG_OBJC_RELEASE(rp_desc);
if (nil == mtl_rps) {
SOKOL_ASSERT(err);
_SG_ERROR(METAL_CREATE_RPS_FAILED);
_SG_LOGMSG(METAL_CREATE_RPS_OUTPUT, [err.localizedDescription UTF8String]);
return SG_RESOURCESTATE_FAILED;
}
pip->mtl.rps = _sg_mtl_add_resource(mtl_rps);
_SG_OBJC_RELEASE(mtl_rps);
// depth-stencil-state
MTLDepthStencilDescriptor* ds_desc = [[MTLDepthStencilDescriptor alloc] init];
ds_desc.depthCompareFunction = _sg_mtl_compare_func(desc->depth.compare);
ds_desc.depthWriteEnabled = desc->depth.write_enabled;
if (desc->stencil.enabled) {
const sg_stencil_face_state* sb = &desc->stencil.back;
ds_desc.backFaceStencil = [[MTLStencilDescriptor alloc] init];
ds_desc.backFaceStencil.stencilFailureOperation = _sg_mtl_stencil_op(sb->fail_op);
ds_desc.backFaceStencil.depthFailureOperation = _sg_mtl_stencil_op(sb->depth_fail_op);
ds_desc.backFaceStencil.depthStencilPassOperation = _sg_mtl_stencil_op(sb->pass_op);
ds_desc.backFaceStencil.stencilCompareFunction = _sg_mtl_compare_func(sb->compare);
ds_desc.backFaceStencil.readMask = desc->stencil.read_mask;
ds_desc.backFaceStencil.writeMask = desc->stencil.write_mask;
const sg_stencil_face_state* sf = &desc->stencil.front;
ds_desc.frontFaceStencil = [[MTLStencilDescriptor alloc] init];
ds_desc.frontFaceStencil.stencilFailureOperation = _sg_mtl_stencil_op(sf->fail_op);
ds_desc.frontFaceStencil.depthFailureOperation = _sg_mtl_stencil_op(sf->depth_fail_op);
ds_desc.frontFaceStencil.depthStencilPassOperation = _sg_mtl_stencil_op(sf->pass_op);
ds_desc.frontFaceStencil.stencilCompareFunction = _sg_mtl_compare_func(sf->compare);
ds_desc.frontFaceStencil.readMask = desc->stencil.read_mask;
ds_desc.frontFaceStencil.writeMask = desc->stencil.write_mask;
}
#if defined(SOKOL_DEBUG)
if (desc->label) {
ds_desc.label = [NSString stringWithFormat:@"%s.dss", desc->label];
}
#endif
id<MTLDepthStencilState> mtl_dss = [_sg.mtl.device newDepthStencilStateWithDescriptor:ds_desc];
_SG_OBJC_RELEASE(ds_desc);
if (nil == mtl_dss) {
_SG_ERROR(METAL_CREATE_DSS_FAILED);
return SG_RESOURCESTATE_FAILED;
}
pip->mtl.dss = _sg_mtl_add_resource(mtl_dss);
_SG_OBJC_RELEASE(mtl_dss);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_discard_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
// it's valid to call release resource with a 'null resource'
_sg_mtl_release_resource(_sg.frame_index, pip->mtl.rps);
_sg_mtl_release_resource(_sg.frame_index, pip->mtl.dss);
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_img, const sg_attachments_desc* desc) {
SOKOL_ASSERT(atts && desc);
SOKOL_ASSERT(color_images && resolve_images);
// copy image pointers
for (int i = 0; i < atts->cmn.num_colors; i++) {
const sg_attachment_desc* color_desc = &desc->colors[i];
_SOKOL_UNUSED(color_desc);
SOKOL_ASSERT(color_desc->image.id != SG_INVALID_ID);
SOKOL_ASSERT(0 == atts->mtl.colors[i].image);
SOKOL_ASSERT(color_images[i] && (color_images[i]->slot.id == color_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(color_images[i]->cmn.pixel_format));
atts->mtl.colors[i].image = color_images[i];
const sg_attachment_desc* resolve_desc = &desc->resolves[i];
if (resolve_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(0 == atts->mtl.resolves[i].image);
SOKOL_ASSERT(resolve_images[i] && (resolve_images[i]->slot.id == resolve_desc->image.id));
SOKOL_ASSERT(color_images[i] && (color_images[i]->cmn.pixel_format == resolve_images[i]->cmn.pixel_format));
atts->mtl.resolves[i].image = resolve_images[i];
}
}
SOKOL_ASSERT(0 == atts->mtl.depth_stencil.image);
const sg_attachment_desc* ds_desc = &desc->depth_stencil;
if (ds_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(ds_img && (ds_img->slot.id == ds_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(ds_img->cmn.pixel_format));
atts->mtl.depth_stencil.image = ds_img;
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_discard_attachments(_sg_attachments_t* atts) {
SOKOL_ASSERT(atts);
_SOKOL_UNUSED(atts);
}
_SOKOL_PRIVATE _sg_image_t* _sg_mtl_attachments_color_image(const _sg_attachments_t* atts, int index) {
// NOTE: may return null
SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
return atts->mtl.colors[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_mtl_attachments_resolve_image(const _sg_attachments_t* atts, int index) {
// NOTE: may return null
SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
return atts->mtl.resolves[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_mtl_attachments_ds_image(const _sg_attachments_t* atts) {
// NOTE: may return null
SOKOL_ASSERT(atts);
return atts->mtl.depth_stencil.image;
}
_SOKOL_PRIVATE void _sg_mtl_begin_pass(const sg_pass* pass) {
SOKOL_ASSERT(pass);
SOKOL_ASSERT(_sg.mtl.cmd_queue);
SOKOL_ASSERT(nil == _sg.mtl.cmd_encoder);
SOKOL_ASSERT(nil == _sg.mtl.cur_drawable);
_sg_mtl_clear_state_cache();
const _sg_attachments_t* atts = _sg.cur_pass.atts;
const sg_swapchain* swapchain = &pass->swapchain;
const sg_pass_action* action = &pass->action;
/*
if this is the first pass in the frame, create command buffers
NOTE: we're creating two command buffers here, one with unretained references
for storing the regular commands, and one with retained references for
storing the presentDrawable call (this needs to hold on the drawable until
presentation has happened - and the easiest way to do this is to let the
command buffer manage the lifetime of the drawable).
Also see: https://github.com/floooh/sokol/issues/762
*/
if (nil == _sg.mtl.cmd_buffer) {
// block until the oldest frame in flight has finished
dispatch_semaphore_wait(_sg.mtl.sem, DISPATCH_TIME_FOREVER);
if (_sg.desc.mtl_use_command_buffer_with_retained_references) {
_sg.mtl.cmd_buffer = [_sg.mtl.cmd_queue commandBuffer];
} else {
_sg.mtl.cmd_buffer = [_sg.mtl.cmd_queue commandBufferWithUnretainedReferences];
}
[_sg.mtl.cmd_buffer enqueue];
[_sg.mtl.cmd_buffer addCompletedHandler:^(id<MTLCommandBuffer> cmd_buf) {
// NOTE: this code is called on a different thread!
_SOKOL_UNUSED(cmd_buf);
dispatch_semaphore_signal(_sg.mtl.sem);
}];
}
// if this is first pass in frame, get uniform buffer base pointer
if (0 == _sg.mtl.cur_ub_base_ptr) {
_sg.mtl.cur_ub_base_ptr = (uint8_t*)[_sg.mtl.uniform_buffers[_sg.mtl.cur_frame_rotate_index] contents];
}
MTLRenderPassDescriptor* pass_desc = [MTLRenderPassDescriptor renderPassDescriptor];
SOKOL_ASSERT(pass_desc);
if (atts) {
// setup pass descriptor for offscreen rendering
SOKOL_ASSERT(atts->slot.state == SG_RESOURCESTATE_VALID);
for (NSUInteger i = 0; i < (NSUInteger)atts->cmn.num_colors; i++) {
const _sg_attachment_common_t* cmn_color_att = &atts->cmn.colors[i];
const _sg_mtl_attachment_t* mtl_color_att = &atts->mtl.colors[i];
const _sg_image_t* color_att_img = mtl_color_att->image;
const _sg_attachment_common_t* cmn_resolve_att = &atts->cmn.resolves[i];
const _sg_mtl_attachment_t* mtl_resolve_att = &atts->mtl.resolves[i];
const _sg_image_t* resolve_att_img = mtl_resolve_att->image;
SOKOL_ASSERT(color_att_img->slot.state == SG_RESOURCESTATE_VALID);
SOKOL_ASSERT(color_att_img->slot.id == cmn_color_att->image_id.id);
SOKOL_ASSERT(color_att_img->mtl.tex[color_att_img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
pass_desc.colorAttachments[i].loadAction = _sg_mtl_load_action(action->colors[i].load_action);
pass_desc.colorAttachments[i].storeAction = _sg_mtl_store_action(action->colors[i].store_action, resolve_att_img != 0);
sg_color c = action->colors[i].clear_value;
pass_desc.colorAttachments[i].clearColor = MTLClearColorMake(c.r, c.g, c.b, c.a);
pass_desc.colorAttachments[i].texture = _sg_mtl_id(color_att_img->mtl.tex[color_att_img->cmn.active_slot]);
pass_desc.colorAttachments[i].level = (NSUInteger)cmn_color_att->mip_level;
switch (color_att_img->cmn.type) {
case SG_IMAGETYPE_CUBE:
case SG_IMAGETYPE_ARRAY:
pass_desc.colorAttachments[i].slice = (NSUInteger)cmn_color_att->slice;
break;
case SG_IMAGETYPE_3D:
pass_desc.colorAttachments[i].depthPlane = (NSUInteger)cmn_color_att->slice;
break;
default: break;
}
if (resolve_att_img) {
SOKOL_ASSERT(resolve_att_img->slot.state == SG_RESOURCESTATE_VALID);
SOKOL_ASSERT(resolve_att_img->slot.id == cmn_resolve_att->image_id.id);
SOKOL_ASSERT(resolve_att_img->mtl.tex[resolve_att_img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
pass_desc.colorAttachments[i].resolveTexture = _sg_mtl_id(resolve_att_img->mtl.tex[resolve_att_img->cmn.active_slot]);
pass_desc.colorAttachments[i].resolveLevel = (NSUInteger)cmn_resolve_att->mip_level;
switch (resolve_att_img->cmn.type) {
case SG_IMAGETYPE_CUBE:
case SG_IMAGETYPE_ARRAY:
pass_desc.colorAttachments[i].resolveSlice = (NSUInteger)cmn_resolve_att->slice;
break;
case SG_IMAGETYPE_3D:
pass_desc.colorAttachments[i].resolveDepthPlane = (NSUInteger)cmn_resolve_att->slice;
break;
default: break;
}
}
}
const _sg_image_t* ds_att_img = atts->mtl.depth_stencil.image;
if (0 != ds_att_img) {
SOKOL_ASSERT(ds_att_img->slot.state == SG_RESOURCESTATE_VALID);
SOKOL_ASSERT(ds_att_img->slot.id == atts->cmn.depth_stencil.image_id.id);
SOKOL_ASSERT(ds_att_img->mtl.tex[ds_att_img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
pass_desc.depthAttachment.texture = _sg_mtl_id(ds_att_img->mtl.tex[ds_att_img->cmn.active_slot]);
pass_desc.depthAttachment.loadAction = _sg_mtl_load_action(action->depth.load_action);
pass_desc.depthAttachment.storeAction = _sg_mtl_store_action(action->depth.store_action, false);
pass_desc.depthAttachment.clearDepth = action->depth.clear_value;
const _sg_attachment_common_t* cmn_ds_att = &atts->cmn.depth_stencil;
switch (ds_att_img->cmn.type) {
case SG_IMAGETYPE_CUBE:
case SG_IMAGETYPE_ARRAY:
pass_desc.depthAttachment.slice = (NSUInteger)cmn_ds_att->slice;
break;
case SG_IMAGETYPE_3D:
pass_desc.depthAttachment.resolveDepthPlane = (NSUInteger)cmn_ds_att->slice;
break;
default: break;
}
if (_sg_is_depth_stencil_format(ds_att_img->cmn.pixel_format)) {
pass_desc.stencilAttachment.texture = _sg_mtl_id(ds_att_img->mtl.tex[ds_att_img->cmn.active_slot]);
pass_desc.stencilAttachment.loadAction = _sg_mtl_load_action(action->stencil.load_action);
pass_desc.stencilAttachment.storeAction = _sg_mtl_store_action(action->depth.store_action, false);
pass_desc.stencilAttachment.clearStencil = action->stencil.clear_value;
switch (ds_att_img->cmn.type) {
case SG_IMAGETYPE_CUBE:
case SG_IMAGETYPE_ARRAY:
pass_desc.stencilAttachment.slice = (NSUInteger)cmn_ds_att->slice;
break;
case SG_IMAGETYPE_3D:
pass_desc.stencilAttachment.resolveDepthPlane = (NSUInteger)cmn_ds_att->slice;
break;
default: break;
}
}
}
} else {
// setup pass descriptor for swapchain rendering
//
// NOTE: at least in macOS Sonoma this no longer seems to be the case, the
// current drawable is also valid in a minimized window
// ===
// an MTKView current_drawable will not be valid if window is minimized, don't do any rendering in this case
if (0 == swapchain->metal.current_drawable) {
_sg.cur_pass.valid = false;
return;
}
// pin the swapchain resources into memory so that they outlive their command buffer
// (this is necessary because the command buffer doesn't retain references)
int pass_desc_ref = _sg_mtl_add_resource(pass_desc);
_sg_mtl_release_resource(_sg.frame_index, pass_desc_ref);
_sg.mtl.cur_drawable = (__bridge id<CAMetalDrawable>) swapchain->metal.current_drawable;
if (swapchain->sample_count > 1) {
// multi-sampling: render into msaa texture, resolve into drawable texture
id<MTLTexture> msaa_tex = (__bridge id<MTLTexture>) swapchain->metal.msaa_color_texture;
SOKOL_ASSERT(msaa_tex != nil);
pass_desc.colorAttachments[0].texture = msaa_tex;
pass_desc.colorAttachments[0].resolveTexture = _sg.mtl.cur_drawable.texture;
pass_desc.colorAttachments[0].storeAction = MTLStoreActionMultisampleResolve;
} else {
// non-msaa: render into current_drawable
pass_desc.colorAttachments[0].texture = _sg.mtl.cur_drawable.texture;
pass_desc.colorAttachments[0].storeAction = MTLStoreActionStore;
}
pass_desc.colorAttachments[0].loadAction = _sg_mtl_load_action(action->colors[0].load_action);
const sg_color c = action->colors[0].clear_value;
pass_desc.colorAttachments[0].clearColor = MTLClearColorMake(c.r, c.g, c.b, c.a);
// optional depth-stencil texture
if (swapchain->metal.depth_stencil_texture) {
id<MTLTexture> ds_tex = (__bridge id<MTLTexture>) swapchain->metal.depth_stencil_texture;
SOKOL_ASSERT(ds_tex != nil);
pass_desc.depthAttachment.texture = ds_tex;
pass_desc.depthAttachment.storeAction = MTLStoreActionDontCare;
pass_desc.depthAttachment.loadAction = _sg_mtl_load_action(action->depth.load_action);
pass_desc.depthAttachment.clearDepth = action->depth.clear_value;
if (_sg_is_depth_stencil_format(swapchain->depth_format)) {
pass_desc.stencilAttachment.texture = ds_tex;
pass_desc.stencilAttachment.storeAction = MTLStoreActionDontCare;
pass_desc.stencilAttachment.loadAction = _sg_mtl_load_action(action->stencil.load_action);
pass_desc.stencilAttachment.clearStencil = action->stencil.clear_value;
}
}
}
// NOTE: at least in macOS Sonoma, the following is no longer the case, a valid
// render command encoder is also returned in a minimized window
// ===
// create a render command encoder, this might return nil if window is minimized
_sg.mtl.cmd_encoder = [_sg.mtl.cmd_buffer renderCommandEncoderWithDescriptor:pass_desc];
if (nil == _sg.mtl.cmd_encoder) {
_sg.cur_pass.valid = false;
return;
}
#if defined(SOKOL_DEBUG)
if (pass->label) {
_sg.mtl.cmd_encoder.label = [NSString stringWithUTF8String:pass->label];
}
#endif
// bind the global uniform buffer, this only happens once per pass
_sg_mtl_bind_uniform_buffers();
}
_SOKOL_PRIVATE void _sg_mtl_end_pass(void) {
if (nil != _sg.mtl.cmd_encoder) {
[_sg.mtl.cmd_encoder endEncoding];
// NOTE: MTLRenderCommandEncoder is autoreleased
_sg.mtl.cmd_encoder = nil;
}
// if this is a swapchain pass, present the drawable
if (nil != _sg.mtl.cur_drawable) {
[_sg.mtl.cmd_buffer presentDrawable:_sg.mtl.cur_drawable];
_sg.mtl.cur_drawable = nil;
}
}
_SOKOL_PRIVATE void _sg_mtl_commit(void) {
SOKOL_ASSERT(nil == _sg.mtl.cmd_encoder);
SOKOL_ASSERT(nil != _sg.mtl.cmd_buffer);
// commit the frame's command buffer
[_sg.mtl.cmd_buffer commit];
// garbage-collect resources pending for release
_sg_mtl_garbage_collect(_sg.frame_index);
// rotate uniform buffer slot
if (++_sg.mtl.cur_frame_rotate_index >= SG_NUM_INFLIGHT_FRAMES) {
_sg.mtl.cur_frame_rotate_index = 0;
}
_sg.mtl.cur_ub_offset = 0;
_sg.mtl.cur_ub_base_ptr = 0;
// NOTE: MTLCommandBuffer is autoreleased
_sg.mtl.cmd_buffer = nil;
}
_SOKOL_PRIVATE void _sg_mtl_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
SOKOL_ASSERT(_sg.cur_pass.height > 0);
MTLViewport vp;
vp.originX = (double) x;
vp.originY = (double) (origin_top_left ? y : (_sg.cur_pass.height - (y + h)));
vp.width = (double) w;
vp.height = (double) h;
vp.znear = 0.0;
vp.zfar = 1.0;
[_sg.mtl.cmd_encoder setViewport:vp];
}
_SOKOL_PRIVATE void _sg_mtl_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
SOKOL_ASSERT(_sg.cur_pass.width > 0);
SOKOL_ASSERT(_sg.cur_pass.height > 0);
// clip against framebuffer rect
const _sg_recti_t clip = _sg_clipi(x, y, w, h, _sg.cur_pass.width, _sg.cur_pass.height);
MTLScissorRect r;
r.x = (NSUInteger)clip.x;
r.y = (NSUInteger) (origin_top_left ? clip.y : (_sg.cur_pass.height - (clip.y + clip.h)));
r.width = (NSUInteger)clip.w;
r.height = (NSUInteger)clip.h;
[_sg.mtl.cmd_encoder setScissorRect:r];
}
_SOKOL_PRIVATE void _sg_mtl_apply_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id));
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
if (_sg.mtl.state_cache.cur_pipeline_id.id != pip->slot.id) {
_sg.mtl.state_cache.cur_pipeline = pip;
_sg.mtl.state_cache.cur_pipeline_id.id = pip->slot.id;
sg_color c = pip->cmn.blend_color;
[_sg.mtl.cmd_encoder setBlendColorRed:c.r green:c.g blue:c.b alpha:c.a];
_sg_stats_add(metal.pipeline.num_set_blend_color, 1);
[_sg.mtl.cmd_encoder setCullMode:pip->mtl.cull_mode];
_sg_stats_add(metal.pipeline.num_set_cull_mode, 1);
[_sg.mtl.cmd_encoder setFrontFacingWinding:pip->mtl.winding];
_sg_stats_add(metal.pipeline.num_set_front_facing_winding, 1);
[_sg.mtl.cmd_encoder setStencilReferenceValue:pip->mtl.stencil_ref];
_sg_stats_add(metal.pipeline.num_set_stencil_reference_value, 1);
[_sg.mtl.cmd_encoder setDepthBias:pip->cmn.depth.bias slopeScale:pip->cmn.depth.bias_slope_scale clamp:pip->cmn.depth.bias_clamp];
_sg_stats_add(metal.pipeline.num_set_depth_bias, 1);
SOKOL_ASSERT(pip->mtl.rps != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setRenderPipelineState:_sg_mtl_id(pip->mtl.rps)];
_sg_stats_add(metal.pipeline.num_set_render_pipeline_state, 1);
SOKOL_ASSERT(pip->mtl.dss != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setDepthStencilState:_sg_mtl_id(pip->mtl.dss)];
_sg_stats_add(metal.pipeline.num_set_depth_stencil_state, 1);
}
}
_SOKOL_PRIVATE bool _sg_mtl_apply_bindings(_sg_bindings_t* bnd) {
SOKOL_ASSERT(bnd);
SOKOL_ASSERT(bnd->pip);
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
// store index buffer binding, this will be needed later in sg_draw()
_sg.mtl.state_cache.cur_indexbuffer = bnd->ib;
_sg.mtl.state_cache.cur_indexbuffer_offset = bnd->ib_offset;
if (bnd->ib) {
SOKOL_ASSERT(bnd->pip->cmn.index_type != SG_INDEXTYPE_NONE);
_sg.mtl.state_cache.cur_indexbuffer_id.id = bnd->ib->slot.id;
} else {
SOKOL_ASSERT(bnd->pip->cmn.index_type == SG_INDEXTYPE_NONE);
_sg.mtl.state_cache.cur_indexbuffer_id.id = SG_INVALID_ID;
}
// apply vertex buffers
for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_vbs; slot++) {
const _sg_buffer_t* vb = bnd->vbs[slot];
const int vb_offset = bnd->vb_offsets[slot];
if ((_sg.mtl.state_cache.cur_vertexbuffer_ids[slot].id != vb->slot.id) ||
(_sg.mtl.state_cache.cur_vertexbuffer_offsets[slot] != vb_offset))
{
_sg.mtl.state_cache.cur_vertexbuffer_offsets[slot] = vb_offset;
const NSUInteger mtl_slot = SG_MAX_SHADERSTAGE_UBS + slot;
if (_sg.mtl.state_cache.cur_vertexbuffer_ids[slot].id != vb->slot.id) {
_sg.mtl.state_cache.cur_vertexbuffer_ids[slot].id = vb->slot.id;
SOKOL_ASSERT(vb->mtl.buf[vb->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setVertexBuffer:_sg_mtl_id(vb->mtl.buf[vb->cmn.active_slot])
offset:(NSUInteger)vb_offset
atIndex:mtl_slot];
} else {
[_sg.mtl.cmd_encoder setVertexBufferOffset:(NSUInteger)vb_offset atIndex:mtl_slot];
}
_sg_stats_add(metal.bindings.num_set_vertex_buffer, 1);
}
}
// apply vertex stage images
for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_vs_imgs; slot++) {
const _sg_image_t* img = bnd->vs_imgs[slot];
if (_sg.mtl.state_cache.cur_vs_image_ids[slot].id != img->slot.id) {
_sg.mtl.state_cache.cur_vs_image_ids[slot].id = img->slot.id;
SOKOL_ASSERT(img->mtl.tex[img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setVertexTexture:_sg_mtl_id(img->mtl.tex[img->cmn.active_slot]) atIndex:slot];
_sg_stats_add(metal.bindings.num_set_vertex_texture, 1);
}
}
// apply vertex stage samplers
for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_vs_smps; slot++) {
const _sg_sampler_t* smp = bnd->vs_smps[slot];
if (_sg.mtl.state_cache.cur_vs_sampler_ids[slot].id != smp->slot.id) {
_sg.mtl.state_cache.cur_vs_sampler_ids[slot].id = smp->slot.id;
SOKOL_ASSERT(smp->mtl.sampler_state != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setVertexSamplerState:_sg_mtl_id(smp->mtl.sampler_state) atIndex:slot];
_sg_stats_add(metal.bindings.num_set_vertex_sampler_state, 1);
}
}
// apply vertex stage storage buffers
for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_vs_sbufs; slot++) {
const _sg_buffer_t* sbuf = bnd->vs_sbufs[slot];
if (_sg.mtl.state_cache.cur_vs_storagebuffer_ids[slot].id != sbuf->slot.id) {
_sg.mtl.state_cache.cur_vs_storagebuffer_ids[slot].id = sbuf->slot.id;
SOKOL_ASSERT(sbuf->mtl.buf[sbuf->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
const NSUInteger mtl_slot = SG_MAX_SHADERSTAGE_UBS + SG_MAX_VERTEX_BUFFERS + slot;
[_sg.mtl.cmd_encoder setVertexBuffer:_sg_mtl_id(sbuf->mtl.buf[sbuf->cmn.active_slot]) offset:0 atIndex:mtl_slot];
_sg_stats_add(metal.bindings.num_set_vertex_buffer, 1);
}
}
// apply fragment stage images
for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_fs_imgs; slot++) {
const _sg_image_t* img = bnd->fs_imgs[slot];
if (_sg.mtl.state_cache.cur_fs_image_ids[slot].id != img->slot.id) {
_sg.mtl.state_cache.cur_fs_image_ids[slot].id = img->slot.id;
SOKOL_ASSERT(img->mtl.tex[img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setFragmentTexture:_sg_mtl_id(img->mtl.tex[img->cmn.active_slot]) atIndex:slot];
_sg_stats_add(metal.bindings.num_set_fragment_texture, 1);
}
}
// apply fragment stage samplers
for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_fs_smps; slot++) {
const _sg_sampler_t* smp = bnd->fs_smps[slot];
if (_sg.mtl.state_cache.cur_fs_sampler_ids[slot].id != smp->slot.id) {
_sg.mtl.state_cache.cur_fs_sampler_ids[slot].id = smp->slot.id;
SOKOL_ASSERT(smp->mtl.sampler_state != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setFragmentSamplerState:_sg_mtl_id(smp->mtl.sampler_state) atIndex:slot];
_sg_stats_add(metal.bindings.num_set_fragment_sampler_state, 1);
}
}
// apply fragment stage storage buffers
for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_fs_sbufs; slot++) {
const _sg_buffer_t* sbuf = bnd->fs_sbufs[slot];
if (_sg.mtl.state_cache.cur_fs_storagebuffer_ids[slot].id != sbuf->slot.id) {
_sg.mtl.state_cache.cur_fs_storagebuffer_ids[slot].id = sbuf->slot.id;
SOKOL_ASSERT(sbuf->mtl.buf[sbuf->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
const NSUInteger mtl_slot = SG_MAX_SHADERSTAGE_UBS + slot;
[_sg.mtl.cmd_encoder setFragmentBuffer:_sg_mtl_id(sbuf->mtl.buf[sbuf->cmn.active_slot]) offset:0 atIndex:mtl_slot];
_sg_stats_add(metal.bindings.num_set_fragment_buffer, 1);
}
}
return true;
}
_SOKOL_PRIVATE void _sg_mtl_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
SOKOL_ASSERT(((size_t)_sg.mtl.cur_ub_offset + data->size) <= (size_t)_sg.mtl.ub_size);
SOKOL_ASSERT((_sg.mtl.cur_ub_offset & (_SG_MTL_UB_ALIGN-1)) == 0);
SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline && _sg.mtl.state_cache.cur_pipeline->shader);
SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline->slot.id == _sg.mtl.state_cache.cur_pipeline_id.id);
SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline->shader->slot.id == _sg.mtl.state_cache.cur_pipeline->cmn.shader_id.id);
SOKOL_ASSERT(ub_index < _sg.mtl.state_cache.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks);
SOKOL_ASSERT(data->size == _sg.mtl.state_cache.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size);
// copy to global uniform buffer, record offset into cmd encoder, and advance offset
uint8_t* dst = &_sg.mtl.cur_ub_base_ptr[_sg.mtl.cur_ub_offset];
memcpy(dst, data->ptr, data->size);
if (stage_index == SG_SHADERSTAGE_VS) {
[_sg.mtl.cmd_encoder setVertexBufferOffset:(NSUInteger)_sg.mtl.cur_ub_offset atIndex:(NSUInteger)ub_index];
_sg_stats_add(metal.uniforms.num_set_vertex_buffer_offset, 1);
} else {
[_sg.mtl.cmd_encoder setFragmentBufferOffset:(NSUInteger)_sg.mtl.cur_ub_offset atIndex:(NSUInteger)ub_index];
_sg_stats_add(metal.uniforms.num_set_fragment_buffer_offset, 1);
}
_sg.mtl.cur_ub_offset = _sg_roundup(_sg.mtl.cur_ub_offset + (int)data->size, _SG_MTL_UB_ALIGN);
}
_SOKOL_PRIVATE void _sg_mtl_draw(int base_element, int num_elements, int num_instances) {
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline && (_sg.mtl.state_cache.cur_pipeline->slot.id == _sg.mtl.state_cache.cur_pipeline_id.id));
if (SG_INDEXTYPE_NONE != _sg.mtl.state_cache.cur_pipeline->cmn.index_type) {
// indexed rendering
SOKOL_ASSERT(_sg.mtl.state_cache.cur_indexbuffer && (_sg.mtl.state_cache.cur_indexbuffer->slot.id == _sg.mtl.state_cache.cur_indexbuffer_id.id));
const _sg_buffer_t* ib = _sg.mtl.state_cache.cur_indexbuffer;
SOKOL_ASSERT(ib->mtl.buf[ib->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
const NSUInteger index_buffer_offset = (NSUInteger) (_sg.mtl.state_cache.cur_indexbuffer_offset + base_element * _sg.mtl.state_cache.cur_pipeline->mtl.index_size);
[_sg.mtl.cmd_encoder drawIndexedPrimitives:_sg.mtl.state_cache.cur_pipeline->mtl.prim_type
indexCount:(NSUInteger)num_elements
indexType:_sg.mtl.state_cache.cur_pipeline->mtl.index_type
indexBuffer:_sg_mtl_id(ib->mtl.buf[ib->cmn.active_slot])
indexBufferOffset:index_buffer_offset
instanceCount:(NSUInteger)num_instances];
} else {
// non-indexed rendering
[_sg.mtl.cmd_encoder drawPrimitives:_sg.mtl.state_cache.cur_pipeline->mtl.prim_type
vertexStart:(NSUInteger)base_element
vertexCount:(NSUInteger)num_elements
instanceCount:(NSUInteger)num_instances];
}
}
_SOKOL_PRIVATE void _sg_mtl_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
__unsafe_unretained id<MTLBuffer> mtl_buf = _sg_mtl_id(buf->mtl.buf[buf->cmn.active_slot]);
void* dst_ptr = [mtl_buf contents];
memcpy(dst_ptr, data->ptr, data->size);
#if defined(_SG_TARGET_MACOS)
if (_sg_mtl_resource_options_storage_mode_managed_or_shared() == MTLResourceStorageModeManaged) {
[mtl_buf didModifyRange:NSMakeRange(0, data->size)];
}
#endif
}
_SOKOL_PRIVATE void _sg_mtl_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
if (new_frame) {
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
}
__unsafe_unretained id<MTLBuffer> mtl_buf = _sg_mtl_id(buf->mtl.buf[buf->cmn.active_slot]);
uint8_t* dst_ptr = (uint8_t*) [mtl_buf contents];
dst_ptr += buf->cmn.append_pos;
memcpy(dst_ptr, data->ptr, data->size);
#if defined(_SG_TARGET_MACOS)
if (_sg_mtl_resource_options_storage_mode_managed_or_shared() == MTLResourceStorageModeManaged) {
[mtl_buf didModifyRange:NSMakeRange((NSUInteger)buf->cmn.append_pos, (NSUInteger)data->size)];
}
#endif
}
_SOKOL_PRIVATE void _sg_mtl_update_image(_sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(img && data);
if (++img->cmn.active_slot >= img->cmn.num_slots) {
img->cmn.active_slot = 0;
}
__unsafe_unretained id<MTLTexture> mtl_tex = _sg_mtl_id(img->mtl.tex[img->cmn.active_slot]);
_sg_mtl_copy_image_data(img, mtl_tex, data);
}
_SOKOL_PRIVATE void _sg_mtl_push_debug_group(const char* name) {
SOKOL_ASSERT(name);
if (_sg.mtl.cmd_encoder) {
[_sg.mtl.cmd_encoder pushDebugGroup:[NSString stringWithUTF8String:name]];
}
}
_SOKOL_PRIVATE void _sg_mtl_pop_debug_group(void) {
if (_sg.mtl.cmd_encoder) {
[_sg.mtl.cmd_encoder popDebugGroup];
}
}
// ██ ██ ███████ ██████ ██████ ██████ ██ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██
// ██ █ ██ █████ ██████ ██ ███ ██████ ██ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██
// ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███ ███ ███████ ██████ ██████ ██ ██████ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████
//
// >>webgpu
// >>wgpu
#elif defined(SOKOL_WGPU)
_SOKOL_PRIVATE WGPUBufferUsageFlags _sg_wgpu_buffer_usage(sg_buffer_type t, sg_usage u) {
WGPUBufferUsageFlags res = 0;
if (SG_BUFFERTYPE_VERTEXBUFFER == t) {
res = WGPUBufferUsage_Vertex;
} else if (SG_BUFFERTYPE_STORAGEBUFFER == t) {
res = WGPUBufferUsage_Storage;
} else {
res = WGPUBufferUsage_Index;
}
if (SG_USAGE_IMMUTABLE != u) {
res |= WGPUBufferUsage_CopyDst;
}
return res;
}
_SOKOL_PRIVATE WGPULoadOp _sg_wgpu_load_op(WGPUTextureView view, sg_load_action a) {
if (0 == view) {
return WGPULoadOp_Undefined;
} else switch (a) {
case SG_LOADACTION_CLEAR:
case SG_LOADACTION_DONTCARE:
return WGPULoadOp_Clear;
case SG_LOADACTION_LOAD:
return WGPULoadOp_Load;
default:
SOKOL_UNREACHABLE;
return WGPULoadOp_Force32;
}
}
_SOKOL_PRIVATE WGPUStoreOp _sg_wgpu_store_op(WGPUTextureView view, sg_store_action a) {
if (0 == view) {
return WGPUStoreOp_Undefined;
} else switch (a) {
case SG_STOREACTION_STORE:
return WGPUStoreOp_Store;
case SG_STOREACTION_DONTCARE:
return WGPUStoreOp_Discard;
default:
SOKOL_UNREACHABLE;
return WGPUStoreOp_Force32;
}
}
_SOKOL_PRIVATE WGPUTextureViewDimension _sg_wgpu_texture_view_dimension(sg_image_type t) {
switch (t) {
case SG_IMAGETYPE_2D: return WGPUTextureViewDimension_2D;
case SG_IMAGETYPE_CUBE: return WGPUTextureViewDimension_Cube;
case SG_IMAGETYPE_3D: return WGPUTextureViewDimension_3D;
case SG_IMAGETYPE_ARRAY: return WGPUTextureViewDimension_2DArray;
default: SOKOL_UNREACHABLE; return WGPUTextureViewDimension_Force32;
}
}
_SOKOL_PRIVATE WGPUTextureDimension _sg_wgpu_texture_dimension(sg_image_type t) {
if (SG_IMAGETYPE_3D == t) {
return WGPUTextureDimension_3D;
} else {
return WGPUTextureDimension_2D;
}
}
_SOKOL_PRIVATE WGPUTextureSampleType _sg_wgpu_texture_sample_type(sg_image_sample_type t) {
switch (t) {
case SG_IMAGESAMPLETYPE_FLOAT: return WGPUTextureSampleType_Float;
case SG_IMAGESAMPLETYPE_DEPTH: return WGPUTextureSampleType_Depth;
case SG_IMAGESAMPLETYPE_SINT: return WGPUTextureSampleType_Sint;
case SG_IMAGESAMPLETYPE_UINT: return WGPUTextureSampleType_Uint;
case SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT: return WGPUTextureSampleType_UnfilterableFloat;
default: SOKOL_UNREACHABLE; return WGPUTextureSampleType_Force32;
}
}
_SOKOL_PRIVATE WGPUSamplerBindingType _sg_wgpu_sampler_binding_type(sg_sampler_type t) {
switch (t) {
case SG_SAMPLERTYPE_FILTERING: return WGPUSamplerBindingType_Filtering;
case SG_SAMPLERTYPE_COMPARISON: return WGPUSamplerBindingType_Comparison;
case SG_SAMPLERTYPE_NONFILTERING: return WGPUSamplerBindingType_NonFiltering;
default: SOKOL_UNREACHABLE; return WGPUSamplerBindingType_Force32;
}
}
_SOKOL_PRIVATE WGPUAddressMode _sg_wgpu_sampler_address_mode(sg_wrap m) {
switch (m) {
case SG_WRAP_REPEAT:
return WGPUAddressMode_Repeat;
case SG_WRAP_CLAMP_TO_EDGE:
case SG_WRAP_CLAMP_TO_BORDER:
return WGPUAddressMode_ClampToEdge;
case SG_WRAP_MIRRORED_REPEAT:
return WGPUAddressMode_MirrorRepeat;
default:
SOKOL_UNREACHABLE;
return WGPUAddressMode_Force32;
}
}
_SOKOL_PRIVATE WGPUFilterMode _sg_wgpu_sampler_minmag_filter(sg_filter f) {
switch (f) {
case SG_FILTER_NEAREST:
return WGPUFilterMode_Nearest;
case SG_FILTER_LINEAR:
return WGPUFilterMode_Linear;
default:
SOKOL_UNREACHABLE;
return WGPUFilterMode_Force32;
}
}
_SOKOL_PRIVATE WGPUMipmapFilterMode _sg_wgpu_sampler_mipmap_filter(sg_filter f) {
switch (f) {
case SG_FILTER_NEAREST:
return WGPUMipmapFilterMode_Nearest;
case SG_FILTER_LINEAR:
return WGPUMipmapFilterMode_Linear;
default:
SOKOL_UNREACHABLE;
return WGPUMipmapFilterMode_Force32;
}
}
_SOKOL_PRIVATE WGPUIndexFormat _sg_wgpu_indexformat(sg_index_type t) {
// NOTE: there's no WGPUIndexFormat_None
return (t == SG_INDEXTYPE_UINT16) ? WGPUIndexFormat_Uint16 : WGPUIndexFormat_Uint32;
}
_SOKOL_PRIVATE WGPUIndexFormat _sg_wgpu_stripindexformat(sg_primitive_type prim_type, sg_index_type idx_type) {
if (idx_type == SG_INDEXTYPE_NONE) {
return WGPUIndexFormat_Undefined;
} else if ((prim_type == SG_PRIMITIVETYPE_LINE_STRIP) || (prim_type == SG_PRIMITIVETYPE_TRIANGLE_STRIP)) {
return _sg_wgpu_indexformat(idx_type);
} else {
return WGPUIndexFormat_Undefined;
}
}
_SOKOL_PRIVATE WGPUVertexStepMode _sg_wgpu_stepmode(sg_vertex_step s) {
return (s == SG_VERTEXSTEP_PER_VERTEX) ? WGPUVertexStepMode_Vertex : WGPUVertexStepMode_Instance;
}
_SOKOL_PRIVATE WGPUVertexFormat _sg_wgpu_vertexformat(sg_vertex_format f) {
switch (f) {
case SG_VERTEXFORMAT_FLOAT: return WGPUVertexFormat_Float32;
case SG_VERTEXFORMAT_FLOAT2: return WGPUVertexFormat_Float32x2;
case SG_VERTEXFORMAT_FLOAT3: return WGPUVertexFormat_Float32x3;
case SG_VERTEXFORMAT_FLOAT4: return WGPUVertexFormat_Float32x4;
case SG_VERTEXFORMAT_BYTE4: return WGPUVertexFormat_Sint8x4;
case SG_VERTEXFORMAT_BYTE4N: return WGPUVertexFormat_Snorm8x4;
case SG_VERTEXFORMAT_UBYTE4: return WGPUVertexFormat_Uint8x4;
case SG_VERTEXFORMAT_UBYTE4N: return WGPUVertexFormat_Unorm8x4;
case SG_VERTEXFORMAT_SHORT2: return WGPUVertexFormat_Sint16x2;
case SG_VERTEXFORMAT_SHORT2N: return WGPUVertexFormat_Snorm16x2;
case SG_VERTEXFORMAT_USHORT2N: return WGPUVertexFormat_Unorm16x2;
case SG_VERTEXFORMAT_SHORT4: return WGPUVertexFormat_Sint16x4;
case SG_VERTEXFORMAT_SHORT4N: return WGPUVertexFormat_Snorm16x4;
case SG_VERTEXFORMAT_USHORT4N: return WGPUVertexFormat_Unorm16x4;
case SG_VERTEXFORMAT_HALF2: return WGPUVertexFormat_Float16x2;
case SG_VERTEXFORMAT_HALF4: return WGPUVertexFormat_Float16x4;
// FIXME! UINT10_N2 (see https://github.com/gpuweb/gpuweb/issues/4275)
case SG_VERTEXFORMAT_UINT10_N2: return WGPUVertexFormat_Undefined;
default:
SOKOL_UNREACHABLE;
return WGPUVertexFormat_Force32;
}
}
_SOKOL_PRIVATE WGPUPrimitiveTopology _sg_wgpu_topology(sg_primitive_type t) {
switch (t) {
case SG_PRIMITIVETYPE_POINTS: return WGPUPrimitiveTopology_PointList;
case SG_PRIMITIVETYPE_LINES: return WGPUPrimitiveTopology_LineList;
case SG_PRIMITIVETYPE_LINE_STRIP: return WGPUPrimitiveTopology_LineStrip;
case SG_PRIMITIVETYPE_TRIANGLES: return WGPUPrimitiveTopology_TriangleList;
case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return WGPUPrimitiveTopology_TriangleStrip;
default:
SOKOL_UNREACHABLE;
return WGPUPrimitiveTopology_Force32;
}
}
_SOKOL_PRIVATE WGPUFrontFace _sg_wgpu_frontface(sg_face_winding fw) {
return (fw == SG_FACEWINDING_CCW) ? WGPUFrontFace_CCW : WGPUFrontFace_CW;
}
_SOKOL_PRIVATE WGPUCullMode _sg_wgpu_cullmode(sg_cull_mode cm) {
switch (cm) {
case SG_CULLMODE_NONE: return WGPUCullMode_None;
case SG_CULLMODE_FRONT: return WGPUCullMode_Front;
case SG_CULLMODE_BACK: return WGPUCullMode_Back;
default:
SOKOL_UNREACHABLE;
return WGPUCullMode_Force32;
}
}
_SOKOL_PRIVATE WGPUTextureFormat _sg_wgpu_textureformat(sg_pixel_format p) {
switch (p) {
case SG_PIXELFORMAT_NONE: return WGPUTextureFormat_Undefined;
case SG_PIXELFORMAT_R8: return WGPUTextureFormat_R8Unorm;
case SG_PIXELFORMAT_R8SN: return WGPUTextureFormat_R8Snorm;
case SG_PIXELFORMAT_R8UI: return WGPUTextureFormat_R8Uint;
case SG_PIXELFORMAT_R8SI: return WGPUTextureFormat_R8Sint;
case SG_PIXELFORMAT_R16UI: return WGPUTextureFormat_R16Uint;
case SG_PIXELFORMAT_R16SI: return WGPUTextureFormat_R16Sint;
case SG_PIXELFORMAT_R16F: return WGPUTextureFormat_R16Float;
case SG_PIXELFORMAT_RG8: return WGPUTextureFormat_RG8Unorm;
case SG_PIXELFORMAT_RG8SN: return WGPUTextureFormat_RG8Snorm;
case SG_PIXELFORMAT_RG8UI: return WGPUTextureFormat_RG8Uint;
case SG_PIXELFORMAT_RG8SI: return WGPUTextureFormat_RG8Sint;
case SG_PIXELFORMAT_R32UI: return WGPUTextureFormat_R32Uint;
case SG_PIXELFORMAT_R32SI: return WGPUTextureFormat_R32Sint;
case SG_PIXELFORMAT_R32F: return WGPUTextureFormat_R32Float;
case SG_PIXELFORMAT_RG16UI: return WGPUTextureFormat_RG16Uint;
case SG_PIXELFORMAT_RG16SI: return WGPUTextureFormat_RG16Sint;
case SG_PIXELFORMAT_RG16F: return WGPUTextureFormat_RG16Float;
case SG_PIXELFORMAT_RGBA8: return WGPUTextureFormat_RGBA8Unorm;
case SG_PIXELFORMAT_SRGB8A8: return WGPUTextureFormat_RGBA8UnormSrgb;
case SG_PIXELFORMAT_RGBA8SN: return WGPUTextureFormat_RGBA8Snorm;
case SG_PIXELFORMAT_RGBA8UI: return WGPUTextureFormat_RGBA8Uint;
case SG_PIXELFORMAT_RGBA8SI: return WGPUTextureFormat_RGBA8Sint;
case SG_PIXELFORMAT_BGRA8: return WGPUTextureFormat_BGRA8Unorm;
case SG_PIXELFORMAT_RGB10A2: return WGPUTextureFormat_RGB10A2Unorm;
case SG_PIXELFORMAT_RG11B10F: return WGPUTextureFormat_RG11B10Ufloat;
case SG_PIXELFORMAT_RG32UI: return WGPUTextureFormat_RG32Uint;
case SG_PIXELFORMAT_RG32SI: return WGPUTextureFormat_RG32Sint;
case SG_PIXELFORMAT_RG32F: return WGPUTextureFormat_RG32Float;
case SG_PIXELFORMAT_RGBA16UI: return WGPUTextureFormat_RGBA16Uint;
case SG_PIXELFORMAT_RGBA16SI: return WGPUTextureFormat_RGBA16Sint;
case SG_PIXELFORMAT_RGBA16F: return WGPUTextureFormat_RGBA16Float;
case SG_PIXELFORMAT_RGBA32UI: return WGPUTextureFormat_RGBA32Uint;
case SG_PIXELFORMAT_RGBA32SI: return WGPUTextureFormat_RGBA32Sint;
case SG_PIXELFORMAT_RGBA32F: return WGPUTextureFormat_RGBA32Float;
case SG_PIXELFORMAT_DEPTH: return WGPUTextureFormat_Depth32Float;
case SG_PIXELFORMAT_DEPTH_STENCIL: return WGPUTextureFormat_Depth32FloatStencil8;
case SG_PIXELFORMAT_BC1_RGBA: return WGPUTextureFormat_BC1RGBAUnorm;
case SG_PIXELFORMAT_BC2_RGBA: return WGPUTextureFormat_BC2RGBAUnorm;
case SG_PIXELFORMAT_BC3_RGBA: return WGPUTextureFormat_BC3RGBAUnorm;
case SG_PIXELFORMAT_BC3_SRGBA: return WGPUTextureFormat_BC3RGBAUnormSrgb;
case SG_PIXELFORMAT_BC4_R: return WGPUTextureFormat_BC4RUnorm;
case SG_PIXELFORMAT_BC4_RSN: return WGPUTextureFormat_BC4RSnorm;
case SG_PIXELFORMAT_BC5_RG: return WGPUTextureFormat_BC5RGUnorm;
case SG_PIXELFORMAT_BC5_RGSN: return WGPUTextureFormat_BC5RGSnorm;
case SG_PIXELFORMAT_BC6H_RGBF: return WGPUTextureFormat_BC6HRGBFloat;
case SG_PIXELFORMAT_BC6H_RGBUF: return WGPUTextureFormat_BC6HRGBUfloat;
case SG_PIXELFORMAT_BC7_RGBA: return WGPUTextureFormat_BC7RGBAUnorm;
case SG_PIXELFORMAT_BC7_SRGBA: return WGPUTextureFormat_BC7RGBAUnormSrgb;
case SG_PIXELFORMAT_ETC2_RGB8: return WGPUTextureFormat_ETC2RGB8Unorm;
case SG_PIXELFORMAT_ETC2_RGB8A1: return WGPUTextureFormat_ETC2RGB8A1Unorm;
case SG_PIXELFORMAT_ETC2_RGBA8: return WGPUTextureFormat_ETC2RGBA8Unorm;
case SG_PIXELFORMAT_ETC2_SRGB8: return WGPUTextureFormat_ETC2RGB8UnormSrgb;
case SG_PIXELFORMAT_ETC2_SRGB8A8: return WGPUTextureFormat_ETC2RGBA8UnormSrgb;
case SG_PIXELFORMAT_EAC_R11: return WGPUTextureFormat_EACR11Unorm;
case SG_PIXELFORMAT_EAC_R11SN: return WGPUTextureFormat_EACR11Snorm;
case SG_PIXELFORMAT_EAC_RG11: return WGPUTextureFormat_EACRG11Unorm;
case SG_PIXELFORMAT_EAC_RG11SN: return WGPUTextureFormat_EACRG11Snorm;
case SG_PIXELFORMAT_RGB9E5: return WGPUTextureFormat_RGB9E5Ufloat;
case SG_PIXELFORMAT_ASTC_4x4_RGBA: return WGPUTextureFormat_ASTC4x4Unorm;
case SG_PIXELFORMAT_ASTC_4x4_SRGBA: return WGPUTextureFormat_ASTC4x4UnormSrgb;
// NOT SUPPORTED
case SG_PIXELFORMAT_R16:
case SG_PIXELFORMAT_R16SN:
case SG_PIXELFORMAT_RG16:
case SG_PIXELFORMAT_RG16SN:
case SG_PIXELFORMAT_RGBA16:
case SG_PIXELFORMAT_RGBA16SN:
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
return WGPUTextureFormat_Undefined;
default:
SOKOL_UNREACHABLE;
return WGPUTextureFormat_Force32;
}
}
_SOKOL_PRIVATE WGPUCompareFunction _sg_wgpu_comparefunc(sg_compare_func f) {
switch (f) {
case SG_COMPAREFUNC_NEVER: return WGPUCompareFunction_Never;
case SG_COMPAREFUNC_LESS: return WGPUCompareFunction_Less;
case SG_COMPAREFUNC_EQUAL: return WGPUCompareFunction_Equal;
case SG_COMPAREFUNC_LESS_EQUAL: return WGPUCompareFunction_LessEqual;
case SG_COMPAREFUNC_GREATER: return WGPUCompareFunction_Greater;
case SG_COMPAREFUNC_NOT_EQUAL: return WGPUCompareFunction_NotEqual;
case SG_COMPAREFUNC_GREATER_EQUAL: return WGPUCompareFunction_GreaterEqual;
case SG_COMPAREFUNC_ALWAYS: return WGPUCompareFunction_Always;
default:
SOKOL_UNREACHABLE;
return WGPUCompareFunction_Force32;
}
}
_SOKOL_PRIVATE WGPUStencilOperation _sg_wgpu_stencilop(sg_stencil_op op) {
switch (op) {
case SG_STENCILOP_KEEP: return WGPUStencilOperation_Keep;
case SG_STENCILOP_ZERO: return WGPUStencilOperation_Zero;
case SG_STENCILOP_REPLACE: return WGPUStencilOperation_Replace;
case SG_STENCILOP_INCR_CLAMP: return WGPUStencilOperation_IncrementClamp;
case SG_STENCILOP_DECR_CLAMP: return WGPUStencilOperation_DecrementClamp;
case SG_STENCILOP_INVERT: return WGPUStencilOperation_Invert;
case SG_STENCILOP_INCR_WRAP: return WGPUStencilOperation_IncrementWrap;
case SG_STENCILOP_DECR_WRAP: return WGPUStencilOperation_DecrementWrap;
default:
SOKOL_UNREACHABLE;
return WGPUStencilOperation_Force32;
}
}
_SOKOL_PRIVATE WGPUBlendOperation _sg_wgpu_blendop(sg_blend_op op) {
switch (op) {
case SG_BLENDOP_ADD: return WGPUBlendOperation_Add;
case SG_BLENDOP_SUBTRACT: return WGPUBlendOperation_Subtract;
case SG_BLENDOP_REVERSE_SUBTRACT: return WGPUBlendOperation_ReverseSubtract;
default:
SOKOL_UNREACHABLE;
return WGPUBlendOperation_Force32;
}
}
_SOKOL_PRIVATE WGPUBlendFactor _sg_wgpu_blendfactor(sg_blend_factor f) {
switch (f) {
case SG_BLENDFACTOR_ZERO: return WGPUBlendFactor_Zero;
case SG_BLENDFACTOR_ONE: return WGPUBlendFactor_One;
case SG_BLENDFACTOR_SRC_COLOR: return WGPUBlendFactor_Src;
case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return WGPUBlendFactor_OneMinusSrc;
case SG_BLENDFACTOR_SRC_ALPHA: return WGPUBlendFactor_SrcAlpha;
case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return WGPUBlendFactor_OneMinusSrcAlpha;
case SG_BLENDFACTOR_DST_COLOR: return WGPUBlendFactor_Dst;
case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return WGPUBlendFactor_OneMinusDst;
case SG_BLENDFACTOR_DST_ALPHA: return WGPUBlendFactor_DstAlpha;
case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return WGPUBlendFactor_OneMinusDstAlpha;
case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return WGPUBlendFactor_SrcAlphaSaturated;
case SG_BLENDFACTOR_BLEND_COLOR: return WGPUBlendFactor_Constant;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return WGPUBlendFactor_OneMinusConstant;
// FIXME: separate blend alpha value not supported?
case SG_BLENDFACTOR_BLEND_ALPHA: return WGPUBlendFactor_Constant;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return WGPUBlendFactor_OneMinusConstant;
default:
SOKOL_UNREACHABLE;
return WGPUBlendFactor_Force32;
}
}
_SOKOL_PRIVATE WGPUColorWriteMaskFlags _sg_wgpu_colorwritemask(uint8_t m) {
WGPUColorWriteMaskFlags res = 0;
if (0 != (m & SG_COLORMASK_R)) {
res |= WGPUColorWriteMask_Red;
}
if (0 != (m & SG_COLORMASK_G)) {
res |= WGPUColorWriteMask_Green;
}
if (0 != (m & SG_COLORMASK_B)) {
res |= WGPUColorWriteMask_Blue;
}
if (0 != (m & SG_COLORMASK_A)) {
res |= WGPUColorWriteMask_Alpha;
}
return res;
}
// image/sampler binding on wgpu follows this convention:
//
// - all images and sampler are in @group(1)
// - vertex stage images start at @binding(0)
// - vertex stage samplers start at @binding(16)
// - vertex stage storage buffers start at @binding(32)
// - fragment stage images start at @binding(48)
// - fragment stage samplers start at @binding(64)
// - fragment stage storage buffers start at @binding(80)
//
_SOKOL_PRIVATE uint32_t _sg_wgpu_image_binding(sg_shader_stage stage, int img_slot) {
SOKOL_ASSERT((img_slot >= 0) && (img_slot < 16));
if (SG_SHADERSTAGE_VS == stage) {
return 0 + (uint32_t)img_slot;
} else {
return 48 + (uint32_t)img_slot;
}
}
_SOKOL_PRIVATE uint32_t _sg_wgpu_sampler_binding(sg_shader_stage stage, int smp_slot) {
SOKOL_ASSERT((smp_slot >= 0) && (smp_slot < 16));
if (SG_SHADERSTAGE_VS == stage) {
return 16 + (uint32_t)smp_slot;
} else {
return 64 + (uint32_t)smp_slot;
}
}
_SOKOL_PRIVATE uint32_t _sg_wgpu_storagebuffer_binding(sg_shader_stage stage, int sbuf_slot) {
SOKOL_ASSERT((sbuf_slot >= 0) && (sbuf_slot < 16));
if (SG_SHADERSTAGE_VS == stage) {
return 32 + (uint32_t)sbuf_slot;
} else {
return 80 + (uint32_t)sbuf_slot;
}
}
_SOKOL_PRIVATE WGPUShaderStage _sg_wgpu_shader_stage(sg_shader_stage stage) {
switch (stage) {
case SG_SHADERSTAGE_VS: return WGPUShaderStage_Vertex;
case SG_SHADERSTAGE_FS: return WGPUShaderStage_Fragment;
default: SOKOL_UNREACHABLE; return WGPUShaderStage_None;
}
}
_SOKOL_PRIVATE void _sg_wgpu_init_caps(void) {
_sg.backend = SG_BACKEND_WGPU;
_sg.features.origin_top_left = true;
_sg.features.image_clamp_to_border = false;
_sg.features.mrt_independent_blend_state = true;
_sg.features.mrt_independent_write_mask = true;
_sg.features.storage_buffer = true;
wgpuDeviceGetLimits(_sg.wgpu.dev, &_sg.wgpu.limits);
const WGPULimits* l = &_sg.wgpu.limits.limits;
_sg.limits.max_image_size_2d = (int) l->maxTextureDimension2D;
_sg.limits.max_image_size_cube = (int) l->maxTextureDimension2D; // not a bug, see: https://github.com/gpuweb/gpuweb/issues/1327
_sg.limits.max_image_size_3d = (int) l->maxTextureDimension3D;
_sg.limits.max_image_size_array = (int) l->maxTextureDimension2D;
_sg.limits.max_image_array_layers = (int) l->maxTextureArrayLayers;
_sg.limits.max_vertex_attrs = SG_MAX_VERTEX_ATTRIBUTES;
// NOTE: no WGPUTextureFormat_R16Unorm
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_SRGB8A8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_BGRA8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB10A2]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R8SN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG8SN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA8SN]);
// FIXME: can be made renderable via extension
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG11B10F]);
// NOTE: msaa rendering is possible in WebGPU, but no resolve
// which is a combination that's not currently supported in sokol-gfx
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R8UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R8SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG8UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG8SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA8UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA8SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R16UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R16SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG16UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG16SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA16UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA16SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]);
if (wgpuDeviceHasFeature(_sg.wgpu.dev, WGPUFeatureName_Float32Filterable)) {
_sg_pixelformat_sfr(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_sfr(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_sfr(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
} else {
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH]);
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGB9E5]);
if (wgpuDeviceHasFeature(_sg.wgpu.dev, WGPUFeatureName_TextureCompressionBC)) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC1_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC2_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_SRGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_R]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_RSN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RG]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RGSN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBUF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_SRGBA]);
}
if (wgpuDeviceHasFeature(_sg.wgpu.dev, WGPUFeatureName_TextureCompressionETC2)) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8A1]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGBA8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8A8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_R11]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_R11SN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_RG11]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_EAC_RG11SN]);
}
if (wgpuDeviceHasFeature(_sg.wgpu.dev, WGPUFeatureName_TextureCompressionASTC)) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_SRGBA]);
}
}
_SOKOL_PRIVATE void _sg_wgpu_uniform_buffer_init(const sg_desc* desc) {
SOKOL_ASSERT(0 == _sg.wgpu.uniform.staging);
SOKOL_ASSERT(0 == _sg.wgpu.uniform.buf);
SOKOL_ASSERT(0 == _sg.wgpu.uniform.bind.group_layout);
SOKOL_ASSERT(0 == _sg.wgpu.uniform.bind.group);
// Add the max-uniform-update size (64 KB) to the requested buffer size,
// this is to prevent validation errors in the WebGPU implementation
// if the entire buffer size is used per frame. 64 KB is the allowed
// max uniform update size on NVIDIA
//
// FIXME: is this still needed?
_sg.wgpu.uniform.num_bytes = (uint32_t)(desc->uniform_buffer_size + _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE);
_sg.wgpu.uniform.staging = (uint8_t*)_sg_malloc(_sg.wgpu.uniform.num_bytes);
WGPUBufferDescriptor ub_desc;
_sg_clear(&ub_desc, sizeof(ub_desc));
ub_desc.size = _sg.wgpu.uniform.num_bytes;
ub_desc.usage = WGPUBufferUsage_Uniform|WGPUBufferUsage_CopyDst;
_sg.wgpu.uniform.buf = wgpuDeviceCreateBuffer(_sg.wgpu.dev, &ub_desc);
SOKOL_ASSERT(_sg.wgpu.uniform.buf);
WGPUBindGroupLayoutEntry ub_bgle_desc[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_UBS];
_sg_clear(ub_bgle_desc, sizeof(ub_bgle_desc));
for (uint32_t stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
WGPUShaderStage vis = (stage_index == SG_SHADERSTAGE_VS) ? WGPUShaderStage_Vertex : WGPUShaderStage_Fragment;
for (uint32_t ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) {
uint32_t bind_index = stage_index * SG_MAX_SHADERSTAGE_UBS + ub_index;
ub_bgle_desc[stage_index][ub_index].binding = bind_index;
ub_bgle_desc[stage_index][ub_index].visibility = vis;
ub_bgle_desc[stage_index][ub_index].buffer.type = WGPUBufferBindingType_Uniform;
ub_bgle_desc[stage_index][ub_index].buffer.hasDynamicOffset = true;
}
}
WGPUBindGroupLayoutDescriptor ub_bgl_desc;
_sg_clear(&ub_bgl_desc, sizeof(ub_bgl_desc));
ub_bgl_desc.entryCount = SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS;
ub_bgl_desc.entries = &ub_bgle_desc[0][0];
_sg.wgpu.uniform.bind.group_layout = wgpuDeviceCreateBindGroupLayout(_sg.wgpu.dev, &ub_bgl_desc);
SOKOL_ASSERT(_sg.wgpu.uniform.bind.group_layout);
WGPUBindGroupEntry ub_bge[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_UBS];
_sg_clear(ub_bge, sizeof(ub_bge));
for (uint32_t stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
for (uint32_t ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) {
uint32_t bind_index = stage_index * SG_MAX_SHADERSTAGE_UBS + ub_index;
ub_bge[stage_index][ub_index].binding = bind_index;
ub_bge[stage_index][ub_index].buffer = _sg.wgpu.uniform.buf;
ub_bge[stage_index][ub_index].size = _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE;
}
}
WGPUBindGroupDescriptor bg_desc;
_sg_clear(&bg_desc, sizeof(bg_desc));
bg_desc.layout = _sg.wgpu.uniform.bind.group_layout;
bg_desc.entryCount = SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS;
bg_desc.entries = &ub_bge[0][0];
_sg.wgpu.uniform.bind.group = wgpuDeviceCreateBindGroup(_sg.wgpu.dev, &bg_desc);
SOKOL_ASSERT(_sg.wgpu.uniform.bind.group);
}
_SOKOL_PRIVATE void _sg_wgpu_uniform_buffer_discard(void) {
if (_sg.wgpu.uniform.buf) {
wgpuBufferRelease(_sg.wgpu.uniform.buf);
_sg.wgpu.uniform.buf = 0;
}
if (_sg.wgpu.uniform.bind.group) {
wgpuBindGroupRelease(_sg.wgpu.uniform.bind.group);
_sg.wgpu.uniform.bind.group = 0;
}
if (_sg.wgpu.uniform.bind.group_layout) {
wgpuBindGroupLayoutRelease(_sg.wgpu.uniform.bind.group_layout);
_sg.wgpu.uniform.bind.group_layout = 0;
}
if (_sg.wgpu.uniform.staging) {
_sg_free(_sg.wgpu.uniform.staging);
_sg.wgpu.uniform.staging = 0;
}
}
_SOKOL_PRIVATE void _sg_wgpu_uniform_buffer_on_begin_pass(void) {
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc,
0, // groupIndex 0 is reserved for uniform buffers
_sg.wgpu.uniform.bind.group,
SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS,
&_sg.wgpu.uniform.bind.offsets[0][0]);
}
_SOKOL_PRIVATE void _sg_wgpu_uniform_buffer_on_commit(void) {
wgpuQueueWriteBuffer(_sg.wgpu.queue, _sg.wgpu.uniform.buf, 0, _sg.wgpu.uniform.staging, _sg.wgpu.uniform.offset);
_sg_stats_add(wgpu.uniforms.size_write_buffer, _sg.wgpu.uniform.offset);
_sg.wgpu.uniform.offset = 0;
_sg_clear(&_sg.wgpu.uniform.bind.offsets[0][0], sizeof(_sg.wgpu.uniform.bind.offsets));
}
_SOKOL_PRIVATE void _sg_wgpu_bindgroups_pool_init(const sg_desc* desc) {
SOKOL_ASSERT((desc->wgpu_bindgroups_cache_size > 0) && (desc->wgpu_bindgroups_cache_size < _SG_MAX_POOL_SIZE));
_sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool;
SOKOL_ASSERT(0 == p->bindgroups);
const int pool_size = desc->wgpu_bindgroups_cache_size;
_sg_init_pool(&p->pool, pool_size);
size_t pool_byte_size = sizeof(_sg_wgpu_bindgroup_t) * (size_t)p->pool.size;
p->bindgroups = (_sg_wgpu_bindgroup_t*) _sg_malloc_clear(pool_byte_size);
}
_SOKOL_PRIVATE void _sg_wgpu_bindgroups_pool_discard(void) {
_sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool;
SOKOL_ASSERT(p->bindgroups);
_sg_free(p->bindgroups); p->bindgroups = 0;
_sg_discard_pool(&p->pool);
}
_SOKOL_PRIVATE _sg_wgpu_bindgroup_t* _sg_wgpu_bindgroup_at(uint32_t bg_id) {
SOKOL_ASSERT(SG_INVALID_ID != bg_id);
_sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool;
int slot_index = _sg_slot_index(bg_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->pool.size));
return &p->bindgroups[slot_index];
}
_SOKOL_PRIVATE _sg_wgpu_bindgroup_t* _sg_wgpu_lookup_bindgroup(uint32_t bg_id) {
if (SG_INVALID_ID != bg_id) {
_sg_wgpu_bindgroup_t* bg = _sg_wgpu_bindgroup_at(bg_id);
if (bg->slot.id == bg_id) {
return bg;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_wgpu_bindgroup_handle_t _sg_wgpu_alloc_bindgroup(void) {
_sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool;
_sg_wgpu_bindgroup_handle_t res;
int slot_index = _sg_pool_alloc_index(&p->pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&p->pool, &p->bindgroups[slot_index].slot, slot_index);
} else {
res.id = SG_INVALID_ID;
_SG_ERROR(WGPU_BINDGROUPS_POOL_EXHAUSTED);
}
return res;
}
_SOKOL_PRIVATE void _sg_wgpu_dealloc_bindgroup(_sg_wgpu_bindgroup_t* bg) {
SOKOL_ASSERT(bg && (bg->slot.state == SG_RESOURCESTATE_ALLOC) && (bg->slot.id != SG_INVALID_ID));
_sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool;
_sg_pool_free_index(&p->pool, _sg_slot_index(bg->slot.id));
_sg_reset_slot(&bg->slot);
}
_SOKOL_PRIVATE void _sg_wgpu_reset_bindgroup_to_alloc_state(_sg_wgpu_bindgroup_t* bg) {
SOKOL_ASSERT(bg);
_sg_slot_t slot = bg->slot;
_sg_clear(bg, sizeof(_sg_wgpu_bindgroup_t));
bg->slot = slot;
bg->slot.state = SG_RESOURCESTATE_ALLOC;
}
// MurmurHash64B (see: https://github.com/aappleby/smhasher/blob/61a0530f28277f2e850bfc39600ce61d02b518de/src/MurmurHash2.cpp#L142)
_SOKOL_PRIVATE uint64_t _sg_wgpu_hash(const void* key, int len, uint64_t seed) {
const uint32_t m = 0x5bd1e995;
const int r = 24;
uint32_t h1 = (uint32_t)seed ^ (uint32_t)len;
uint32_t h2 = (uint32_t)(seed >> 32);
const uint32_t * data = (const uint32_t *)key;
while (len >= 8) {
uint32_t k1 = *data++;
k1 *= m; k1 ^= k1 >> r; k1 *= m;
h1 *= m; h1 ^= k1;
len -= 4;
uint32_t k2 = *data++;
k2 *= m; k2 ^= k2 >> r; k2 *= m;
h2 *= m; h2 ^= k2;
len -= 4;
}
if (len >= 4) {
uint32_t k1 = *data++;
k1 *= m; k1 ^= k1 >> r; k1 *= m;
h1 *= m; h1 ^= k1;
len -= 4;
}
switch(len) {
case 3: h2 ^= (uint32_t)(((unsigned char*)data)[2] << 16);
case 2: h2 ^= (uint32_t)(((unsigned char*)data)[1] << 8);
case 1: h2 ^= ((unsigned char*)data)[0];
h2 *= m;
};
h1 ^= h2 >> 18; h1 *= m;
h2 ^= h1 >> 22; h2 *= m;
h1 ^= h2 >> 17; h1 *= m;
h2 ^= h1 >> 19; h2 *= m;
uint64_t h = h1;
h = (h << 32) | h2;
return h;
}
_SOKOL_PRIVATE uint64_t _sg_wgpu_bindgroups_cache_item(_sg_wgpu_bindgroups_cache_item_type_t type, uint32_t id) {
return (((uint64_t)type) << 32) | id;
}
_SOKOL_PRIVATE uint64_t _sg_wgpu_bindgroups_cache_pip_item(uint32_t id) {
return _sg_wgpu_bindgroups_cache_item(_SG_WGPU_BINDGROUPSCACHEITEMTYPE_PIPELINE, id);
}
_SOKOL_PRIVATE uint64_t _sg_wgpu_bindgroups_cache_image_item(uint32_t id) {
return _sg_wgpu_bindgroups_cache_item(_SG_WGPU_BINDGROUPSCACHEITEMTYPE_IMAGE, id);
}
_SOKOL_PRIVATE uint64_t _sg_wgpu_bindgroups_cache_sampler_item(uint32_t id) {
return _sg_wgpu_bindgroups_cache_item(_SG_WGPU_BINDGROUPSCACHEITEMTYPE_SAMPLER, id);
}
_SOKOL_PRIVATE uint64_t _sg_wgpu_bindgroups_cache_sbuf_item(uint32_t id) {
return _sg_wgpu_bindgroups_cache_item(_SG_WGPU_BINDGROUPSCACHEITEMTYPE_STORAGEBUFFER, id);
}
_SOKOL_PRIVATE void _sg_wgpu_init_bindgroups_cache_key(_sg_wgpu_bindgroups_cache_key_t* key, const _sg_bindings_t* bnd) {
SOKOL_ASSERT(bnd);
SOKOL_ASSERT(bnd->pip);
SOKOL_ASSERT(bnd->num_vs_imgs <= SG_MAX_SHADERSTAGE_IMAGES);
SOKOL_ASSERT(bnd->num_vs_smps <= SG_MAX_SHADERSTAGE_SAMPLERS);
SOKOL_ASSERT(bnd->num_vs_sbufs <= SG_MAX_SHADERSTAGE_STORAGEBUFFERS);
SOKOL_ASSERT(bnd->num_fs_imgs <= SG_MAX_SHADERSTAGE_IMAGES);
SOKOL_ASSERT(bnd->num_fs_smps <= SG_MAX_SHADERSTAGE_SAMPLERS);
SOKOL_ASSERT(bnd->num_fs_sbufs <= SG_MAX_SHADERSTAGE_STORAGEBUFFERS);
_sg_clear(key->items, sizeof(key->items));
int item_idx = 0;
key->items[item_idx++] = _sg_wgpu_bindgroups_cache_pip_item(bnd->pip->slot.id);
for (int i = 0; i < bnd->num_vs_imgs; i++) {
SOKOL_ASSERT(bnd->vs_imgs[i]);
SOKOL_ASSERT(item_idx < _SG_WGPU_BINDGROUPSCACHEKEY_NUM_ITEMS);
key->items[item_idx++] = _sg_wgpu_bindgroups_cache_image_item(bnd->vs_imgs[i]->slot.id);
}
for (int i = 0; i < bnd->num_vs_smps; i++) {
SOKOL_ASSERT(bnd->vs_smps[i]);
SOKOL_ASSERT(item_idx < _SG_WGPU_BINDGROUPSCACHEKEY_NUM_ITEMS);
key->items[item_idx++] = _sg_wgpu_bindgroups_cache_sampler_item(bnd->vs_smps[i]->slot.id);
}
for (int i = 0; i < bnd->num_vs_sbufs; i++) {
SOKOL_ASSERT(bnd->vs_sbufs[i]);
SOKOL_ASSERT(item_idx < _SG_WGPU_BINDGROUPSCACHEKEY_NUM_ITEMS);
key->items[item_idx++] = _sg_wgpu_bindgroups_cache_sbuf_item(bnd->vs_sbufs[i]->slot.id);
}
for (int i = 0; i < bnd->num_fs_imgs; i++) {
SOKOL_ASSERT(bnd->fs_imgs[i]);
SOKOL_ASSERT(item_idx < _SG_WGPU_BINDGROUPSCACHEKEY_NUM_ITEMS);
key->items[item_idx++] = _sg_wgpu_bindgroups_cache_image_item(bnd->fs_imgs[i]->slot.id);
}
for (int i = 0; i < bnd->num_fs_smps; i++) {
SOKOL_ASSERT(bnd->fs_smps[i]);
SOKOL_ASSERT(item_idx < _SG_WGPU_BINDGROUPSCACHEKEY_NUM_ITEMS);
key->items[item_idx++] = _sg_wgpu_bindgroups_cache_sampler_item(bnd->fs_smps[i]->slot.id);
}
for (int i = 0; i < bnd->num_fs_sbufs; i++) {
SOKOL_ASSERT(bnd->fs_sbufs[i]);
SOKOL_ASSERT(item_idx < _SG_WGPU_BINDGROUPSCACHEKEY_NUM_ITEMS);
key->items[item_idx++] = _sg_wgpu_bindgroups_cache_sbuf_item(bnd->fs_sbufs[i]->slot.id);
}
SOKOL_ASSERT(item_idx == (1 + bnd->num_vs_imgs + bnd->num_vs_smps + bnd->num_vs_sbufs + bnd->num_fs_imgs + bnd->num_fs_smps + bnd->num_fs_sbufs));
key->hash = _sg_wgpu_hash(&key->items, (int)sizeof(key->items), 0x1234567887654321);
}
_SOKOL_PRIVATE bool _sg_wgpu_compare_bindgroups_cache_key(_sg_wgpu_bindgroups_cache_key_t* k0, _sg_wgpu_bindgroups_cache_key_t* k1) {
SOKOL_ASSERT(k0 && k1);
if (k0->hash != k1->hash) {
return false;
}
if (memcmp(&k0->items, &k1->items, sizeof(k0->items)) != 0) {
_sg_stats_add(wgpu.bindings.num_bindgroup_cache_hash_vs_key_mismatch, 1);
return false;
}
return true;
}
_SOKOL_PRIVATE _sg_wgpu_bindgroup_t* _sg_wgpu_create_bindgroup(_sg_bindings_t* bnd) {
SOKOL_ASSERT(_sg.wgpu.dev);
SOKOL_ASSERT(bnd->pip);
SOKOL_ASSERT(bnd->pip->shader && (bnd->pip->cmn.shader_id.id == bnd->pip->shader->slot.id));
_sg_stats_add(wgpu.bindings.num_create_bindgroup, 1);
_sg_wgpu_bindgroup_handle_t bg_id = _sg_wgpu_alloc_bindgroup();
if (bg_id.id == SG_INVALID_ID) {
return 0;
}
_sg_wgpu_bindgroup_t* bg = _sg_wgpu_bindgroup_at(bg_id.id);
SOKOL_ASSERT(bg && (bg->slot.state == SG_RESOURCESTATE_ALLOC));
// create wgpu bindgroup object
WGPUBindGroupLayout bgl = bnd->pip->shader->wgpu.bind_group_layout;
SOKOL_ASSERT(bgl);
WGPUBindGroupEntry wgpu_entries[_SG_WGPU_MAX_BINDGROUP_ENTRIES];
_sg_clear(&wgpu_entries, sizeof(wgpu_entries));
int bge_index = 0;
for (int i = 0; i < bnd->num_vs_imgs; i++) {
WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++];
wgpu_entry->binding = _sg_wgpu_image_binding(SG_SHADERSTAGE_VS, i);
wgpu_entry->textureView = bnd->vs_imgs[i]->wgpu.view;
}
for (int i = 0; i < bnd->num_vs_smps; i++) {
WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++];
wgpu_entry->binding = _sg_wgpu_sampler_binding(SG_SHADERSTAGE_VS, i);
wgpu_entry->sampler = bnd->vs_smps[i]->wgpu.smp;
}
for (int i = 0; i < bnd->num_vs_sbufs; i++) {
WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++];
wgpu_entry->binding = _sg_wgpu_storagebuffer_binding(SG_SHADERSTAGE_VS, i);
wgpu_entry->buffer = bnd->vs_sbufs[i]->wgpu.buf;
wgpu_entry->size = (uint64_t) bnd->vs_sbufs[i]->cmn.size;
}
for (int i = 0; i < bnd->num_fs_imgs; i++) {
WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++];
wgpu_entry->binding = _sg_wgpu_image_binding(SG_SHADERSTAGE_FS, i);
wgpu_entry->textureView = bnd->fs_imgs[i]->wgpu.view;
}
for (int i = 0; i < bnd->num_fs_smps; i++) {
WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++];
wgpu_entry->binding = _sg_wgpu_sampler_binding(SG_SHADERSTAGE_FS, i);
wgpu_entry->sampler = bnd->fs_smps[i]->wgpu.smp;
}
for (int i = 0; i < bnd->num_fs_sbufs; i++) {
WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++];
wgpu_entry->binding = _sg_wgpu_storagebuffer_binding(SG_SHADERSTAGE_FS, i);
wgpu_entry->buffer = bnd->fs_sbufs[i]->wgpu.buf;
wgpu_entry->size = (uint64_t) bnd->fs_sbufs[i]->cmn.size;
}
WGPUBindGroupDescriptor bg_desc;
_sg_clear(&bg_desc, sizeof(bg_desc));
bg_desc.layout = bgl;
bg_desc.entryCount = (size_t)bge_index;
bg_desc.entries = &wgpu_entries[0];
bg->bindgroup = wgpuDeviceCreateBindGroup(_sg.wgpu.dev, &bg_desc);
if (bg->bindgroup == 0) {
_SG_ERROR(WGPU_CREATEBINDGROUP_FAILED);
bg->slot.state = SG_RESOURCESTATE_FAILED;
return bg;
}
_sg_wgpu_init_bindgroups_cache_key(&bg->key, bnd);
bg->slot.state = SG_RESOURCESTATE_VALID;
return bg;
}
_SOKOL_PRIVATE void _sg_wgpu_discard_bindgroup(_sg_wgpu_bindgroup_t* bg) {
SOKOL_ASSERT(bg);
_sg_stats_add(wgpu.bindings.num_discard_bindgroup, 1);
if (bg->slot.state == SG_RESOURCESTATE_VALID) {
if (bg->bindgroup) {
wgpuBindGroupRelease(bg->bindgroup);
bg->bindgroup = 0;
}
_sg_wgpu_reset_bindgroup_to_alloc_state(bg);
SOKOL_ASSERT(bg->slot.state == SG_RESOURCESTATE_ALLOC);
}
if (bg->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_wgpu_dealloc_bindgroup(bg);
SOKOL_ASSERT(bg->slot.state == SG_RESOURCESTATE_INITIAL);
}
}
_SOKOL_PRIVATE void _sg_wgpu_discard_all_bindgroups(void) {
_sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool;
for (int i = 0; i < p->pool.size; i++) {
sg_resource_state state = p->bindgroups[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_wgpu_discard_bindgroup(&p->bindgroups[i]);
}
}
}
_SOKOL_PRIVATE void _sg_wgpu_bindgroups_cache_init(const sg_desc* desc) {
SOKOL_ASSERT(desc);
SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.num == 0);
SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.index_mask == 0);
SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.items == 0);
const int num = desc->wgpu_bindgroups_cache_size;
if (num <= 1) {
_SG_PANIC(WGPU_BINDGROUPSCACHE_SIZE_GREATER_ONE);
}
if (!_sg_ispow2(num)) {
_SG_PANIC(WGPU_BINDGROUPSCACHE_SIZE_POW2);
}
_sg.wgpu.bindgroups_cache.num = (uint32_t)desc->wgpu_bindgroups_cache_size;
_sg.wgpu.bindgroups_cache.index_mask = _sg.wgpu.bindgroups_cache.num - 1;
size_t size_in_bytes = sizeof(_sg_wgpu_bindgroup_handle_t) * (size_t)num;
_sg.wgpu.bindgroups_cache.items = (_sg_wgpu_bindgroup_handle_t*)_sg_malloc_clear(size_in_bytes);
}
_SOKOL_PRIVATE void _sg_wgpu_bindgroups_cache_discard(void) {
if (_sg.wgpu.bindgroups_cache.items) {
_sg_free(_sg.wgpu.bindgroups_cache.items);
_sg.wgpu.bindgroups_cache.items = 0;
}
_sg.wgpu.bindgroups_cache.num = 0;
_sg.wgpu.bindgroups_cache.index_mask = 0;
}
_SOKOL_PRIVATE void _sg_wgpu_bindgroups_cache_set(uint64_t hash, uint32_t bg_id) {
uint32_t index = hash & _sg.wgpu.bindgroups_cache.index_mask;
SOKOL_ASSERT(index < _sg.wgpu.bindgroups_cache.num);
SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.items);
_sg.wgpu.bindgroups_cache.items[index].id = bg_id;
}
_SOKOL_PRIVATE uint32_t _sg_wgpu_bindgroups_cache_get(uint64_t hash) {
uint32_t index = hash & _sg.wgpu.bindgroups_cache.index_mask;
SOKOL_ASSERT(index < _sg.wgpu.bindgroups_cache.num);
SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.items);
return _sg.wgpu.bindgroups_cache.items[index].id;
}
// called from wgpu resource destroy functions to also invalidate any
// bindgroups cache slot and bindgroup referencing that resource
_SOKOL_PRIVATE void _sg_wgpu_bindgroups_cache_invalidate(_sg_wgpu_bindgroups_cache_item_type_t type, uint32_t id) {
const uint64_t key_item = _sg_wgpu_bindgroups_cache_item(type, id);
SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.items);
for (uint32_t cache_item_idx = 0; cache_item_idx < _sg.wgpu.bindgroups_cache.num; cache_item_idx++) {
const uint32_t bg_id = _sg.wgpu.bindgroups_cache.items[cache_item_idx].id;
if (bg_id != SG_INVALID_ID) {
_sg_wgpu_bindgroup_t* bg = _sg_wgpu_lookup_bindgroup(bg_id);
SOKOL_ASSERT(bg && (bg->slot.state == SG_RESOURCESTATE_VALID));
// check if resource is in bindgroup, if yes discard bindgroup and invalidate cache slot
bool invalidate_cache_item = false;
for (int key_item_idx = 0; key_item_idx < _SG_WGPU_BINDGROUPSCACHEKEY_NUM_ITEMS; key_item_idx++) {
if (bg->key.items[key_item_idx] == key_item) {
invalidate_cache_item = true;
break;
}
}
if (invalidate_cache_item) {
_sg_wgpu_discard_bindgroup(bg); bg = 0;
_sg_wgpu_bindgroups_cache_set(cache_item_idx, SG_INVALID_ID);
_sg_stats_add(wgpu.bindings.num_bindgroup_cache_invalidates, 1);
}
}
}
}
_SOKOL_PRIVATE void _sg_wgpu_bindings_cache_clear(void) {
memset(&_sg.wgpu.bindings_cache, 0, sizeof(_sg.wgpu.bindings_cache));
}
_SOKOL_PRIVATE bool _sg_wgpu_bindings_cache_vb_dirty(int index, const _sg_buffer_t* vb, int offset) {
SOKOL_ASSERT((index >= 0) && (index < SG_MAX_VERTEX_BUFFERS));
if (vb) {
return (_sg.wgpu.bindings_cache.vbs[index].buffer.id != vb->slot.id)
|| (_sg.wgpu.bindings_cache.vbs[index].offset != offset);
} else {
return _sg.wgpu.bindings_cache.vbs[index].buffer.id != SG_INVALID_ID;
}
}
_SOKOL_PRIVATE void _sg_wgpu_bindings_cache_vb_update(int index, const _sg_buffer_t* vb, int offset) {
SOKOL_ASSERT((index >= 0) && (index < SG_MAX_VERTEX_BUFFERS));
if (vb) {
_sg.wgpu.bindings_cache.vbs[index].buffer.id = vb->slot.id;
_sg.wgpu.bindings_cache.vbs[index].offset = offset;
} else {
_sg.wgpu.bindings_cache.vbs[index].buffer.id = SG_INVALID_ID;
_sg.wgpu.bindings_cache.vbs[index].offset = 0;
}
}
_SOKOL_PRIVATE bool _sg_wgpu_bindings_cache_ib_dirty(const _sg_buffer_t* ib, int offset) {
if (ib) {
return (_sg.wgpu.bindings_cache.ib.buffer.id != ib->slot.id)
|| (_sg.wgpu.bindings_cache.ib.offset != offset);
} else {
return _sg.wgpu.bindings_cache.ib.buffer.id != SG_INVALID_ID;
}
}
_SOKOL_PRIVATE void _sg_wgpu_bindings_cache_ib_update(const _sg_buffer_t* ib, int offset) {
if (ib) {
_sg.wgpu.bindings_cache.ib.buffer.id = ib->slot.id;
_sg.wgpu.bindings_cache.ib.offset = offset;
} else {
_sg.wgpu.bindings_cache.ib.buffer.id = SG_INVALID_ID;
_sg.wgpu.bindings_cache.ib.offset = 0;
}
}
_SOKOL_PRIVATE bool _sg_wgpu_bindings_cache_bg_dirty(const _sg_wgpu_bindgroup_t* bg) {
if (bg) {
return _sg.wgpu.bindings_cache.bg.id != bg->slot.id;
} else {
return _sg.wgpu.bindings_cache.bg.id != SG_INVALID_ID;
}
}
_SOKOL_PRIVATE void _sg_wgpu_bindings_cache_bg_update(const _sg_wgpu_bindgroup_t* bg) {
if (bg) {
_sg.wgpu.bindings_cache.bg.id = bg->slot.id;
} else {
_sg.wgpu.bindings_cache.bg.id = SG_INVALID_ID;
}
}
_SOKOL_PRIVATE void _sg_wgpu_set_bindgroup(_sg_wgpu_bindgroup_t* bg) {
if (_sg_wgpu_bindings_cache_bg_dirty(bg)) {
_sg_wgpu_bindings_cache_bg_update(bg);
_sg_stats_add(wgpu.bindings.num_set_bindgroup, 1);
if (bg) {
SOKOL_ASSERT(bg->slot.state == SG_RESOURCESTATE_VALID);
SOKOL_ASSERT(bg->bindgroup);
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, _SG_WGPU_IMAGE_SAMPLER_BINDGROUP_INDEX, bg->bindgroup, 0, 0);
} else {
// a nullptr bindgroup means setting the empty bindgroup
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, _SG_WGPU_IMAGE_SAMPLER_BINDGROUP_INDEX, _sg.wgpu.empty_bind_group, 0, 0);
}
} else {
_sg_stats_add(wgpu.bindings.num_skip_redundant_bindgroup, 1);
}
}
_SOKOL_PRIVATE bool _sg_wgpu_apply_bindgroup(_sg_bindings_t* bnd) {
if ((bnd->num_vs_imgs + bnd->num_vs_smps + bnd->num_vs_sbufs + bnd->num_fs_imgs + bnd->num_fs_smps + bnd->num_fs_sbufs) > 0) {
if (!_sg.desc.wgpu_disable_bindgroups_cache) {
_sg_wgpu_bindgroup_t* bg = 0;
_sg_wgpu_bindgroups_cache_key_t key;
_sg_wgpu_init_bindgroups_cache_key(&key, bnd);
uint32_t bg_id = _sg_wgpu_bindgroups_cache_get(key.hash);
if (bg_id != SG_INVALID_ID) {
// potential cache hit
bg = _sg_wgpu_lookup_bindgroup(bg_id);
SOKOL_ASSERT(bg && (bg->slot.state == SG_RESOURCESTATE_VALID));
if (!_sg_wgpu_compare_bindgroups_cache_key(&key, &bg->key)) {
// cache collision, need to delete cached bindgroup
_sg_stats_add(wgpu.bindings.num_bindgroup_cache_collisions, 1);
_sg_wgpu_discard_bindgroup(bg);
_sg_wgpu_bindgroups_cache_set(key.hash, SG_INVALID_ID);
bg = 0;
} else {
_sg_stats_add(wgpu.bindings.num_bindgroup_cache_hits, 1);
}
} else {
_sg_stats_add(wgpu.bindings.num_bindgroup_cache_misses, 1);
}
if (bg == 0) {
// either no cache entry yet, or cache collision, create new bindgroup and store in cache
bg = _sg_wgpu_create_bindgroup(bnd);
_sg_wgpu_bindgroups_cache_set(key.hash, bg->slot.id);
}
if (bg && bg->slot.state == SG_RESOURCESTATE_VALID) {
_sg_wgpu_set_bindgroup(bg);
} else {
return false;
}
} else {
// bindgroups cache disabled, create and destroy bindgroup on the fly (expensive!)
_sg_wgpu_bindgroup_t* bg = _sg_wgpu_create_bindgroup(bnd);
if (bg) {
if (bg->slot.state == SG_RESOURCESTATE_VALID) {
_sg_wgpu_set_bindgroup(bg);
}
_sg_wgpu_discard_bindgroup(bg);
} else {
return false;
}
}
} else {
_sg_wgpu_set_bindgroup(0);
}
return true;
}
_SOKOL_PRIVATE bool _sg_wgpu_apply_index_buffer(_sg_bindings_t* bnd) {
if (_sg_wgpu_bindings_cache_ib_dirty(bnd->ib, bnd->ib_offset)) {
_sg_wgpu_bindings_cache_ib_update(bnd->ib, bnd->ib_offset);
if (bnd->ib) {
const WGPUIndexFormat format = _sg_wgpu_indexformat(bnd->pip->cmn.index_type);
const uint64_t buf_size = (uint64_t)bnd->ib->cmn.size;
const uint64_t offset = (uint64_t)bnd->ib_offset;
SOKOL_ASSERT(buf_size > offset);
const uint64_t max_bytes = buf_size - offset;
wgpuRenderPassEncoderSetIndexBuffer(_sg.wgpu.pass_enc, bnd->ib->wgpu.buf, format, offset, max_bytes);
_sg_stats_add(wgpu.bindings.num_set_index_buffer, 1);
}
// FIXME: else-path should actually set a null index buffer (this was just recently implemented in WebGPU)
} else {
_sg_stats_add(wgpu.bindings.num_skip_redundant_index_buffer, 1);
}
return true;
}
_SOKOL_PRIVATE bool _sg_wgpu_apply_vertex_buffers(_sg_bindings_t* bnd) {
for (int slot = 0; slot < bnd->num_vbs; slot++) {
if (_sg_wgpu_bindings_cache_vb_dirty(slot, bnd->vbs[slot], bnd->vb_offsets[slot])) {
_sg_wgpu_bindings_cache_vb_update(slot, bnd->vbs[slot], bnd->vb_offsets[slot]);
const uint64_t buf_size = (uint64_t)bnd->vbs[slot]->cmn.size;
const uint64_t offset = (uint64_t)bnd->vb_offsets[slot];
SOKOL_ASSERT(buf_size > offset);
const uint64_t max_bytes = buf_size - offset;
wgpuRenderPassEncoderSetVertexBuffer(_sg.wgpu.pass_enc, (uint32_t)slot, bnd->vbs[slot]->wgpu.buf, offset, max_bytes);
_sg_stats_add(wgpu.bindings.num_set_vertex_buffer, 1);
} else {
_sg_stats_add(wgpu.bindings.num_skip_redundant_vertex_buffer, 1);
}
}
// FIXME: remaining vb slots should actually set a null vertex buffer (this was just recently implemented in WebGPU)
return true;
}
_SOKOL_PRIVATE void _sg_wgpu_setup_backend(const sg_desc* desc) {
SOKOL_ASSERT(desc);
SOKOL_ASSERT(desc->environment.wgpu.device);
SOKOL_ASSERT(desc->uniform_buffer_size > 0);
_sg.backend = SG_BACKEND_WGPU;
_sg.wgpu.valid = true;
_sg.wgpu.dev = (WGPUDevice) desc->environment.wgpu.device;
_sg.wgpu.queue = wgpuDeviceGetQueue(_sg.wgpu.dev);
SOKOL_ASSERT(_sg.wgpu.queue);
_sg_wgpu_init_caps();
_sg_wgpu_uniform_buffer_init(desc);
_sg_wgpu_bindgroups_pool_init(desc);
_sg_wgpu_bindgroups_cache_init(desc);
_sg_wgpu_bindings_cache_clear();
// create an empty bind group for shader stages without bound images
// FIXME: once WebGPU supports setting null objects, this can be removed
WGPUBindGroupLayoutDescriptor bgl_desc;
_sg_clear(&bgl_desc, sizeof(bgl_desc));
WGPUBindGroupLayout empty_bgl = wgpuDeviceCreateBindGroupLayout(_sg.wgpu.dev, &bgl_desc);
SOKOL_ASSERT(empty_bgl);
WGPUBindGroupDescriptor bg_desc;
_sg_clear(&bg_desc, sizeof(bg_desc));
bg_desc.layout = empty_bgl;
_sg.wgpu.empty_bind_group = wgpuDeviceCreateBindGroup(_sg.wgpu.dev, &bg_desc);
SOKOL_ASSERT(_sg.wgpu.empty_bind_group);
wgpuBindGroupLayoutRelease(empty_bgl);
// create initial per-frame command encoder
WGPUCommandEncoderDescriptor cmd_enc_desc;
_sg_clear(&cmd_enc_desc, sizeof(cmd_enc_desc));
_sg.wgpu.cmd_enc = wgpuDeviceCreateCommandEncoder(_sg.wgpu.dev, &cmd_enc_desc);
SOKOL_ASSERT(_sg.wgpu.cmd_enc);
}
_SOKOL_PRIVATE void _sg_wgpu_discard_backend(void) {
SOKOL_ASSERT(_sg.wgpu.valid);
SOKOL_ASSERT(_sg.wgpu.cmd_enc);
_sg.wgpu.valid = false;
_sg_wgpu_discard_all_bindgroups();
_sg_wgpu_bindgroups_cache_discard();
_sg_wgpu_bindgroups_pool_discard();
_sg_wgpu_uniform_buffer_discard();
wgpuBindGroupRelease(_sg.wgpu.empty_bind_group); _sg.wgpu.empty_bind_group = 0;
wgpuCommandEncoderRelease(_sg.wgpu.cmd_enc); _sg.wgpu.cmd_enc = 0;
wgpuQueueRelease(_sg.wgpu.queue); _sg.wgpu.queue = 0;
}
_SOKOL_PRIVATE void _sg_wgpu_reset_state_cache(void) {
_sg_wgpu_bindings_cache_clear();
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && desc);
const bool injected = (0 != desc->wgpu_buffer);
if (injected) {
buf->wgpu.buf = (WGPUBuffer) desc->wgpu_buffer;
wgpuBufferReference(buf->wgpu.buf);
} else {
// buffer mapping size must be multiple of 4, so round up buffer size (only a problem
// with index buffers containing odd number of indices)
const uint64_t wgpu_buf_size = _sg_roundup_u64((uint64_t)buf->cmn.size, 4);
const bool map_at_creation = (SG_USAGE_IMMUTABLE == buf->cmn.usage);
WGPUBufferDescriptor wgpu_buf_desc;
_sg_clear(&wgpu_buf_desc, sizeof(wgpu_buf_desc));
wgpu_buf_desc.usage = _sg_wgpu_buffer_usage(buf->cmn.type, buf->cmn.usage);
wgpu_buf_desc.size = wgpu_buf_size;
wgpu_buf_desc.mappedAtCreation = map_at_creation;
wgpu_buf_desc.label = desc->label;
buf->wgpu.buf = wgpuDeviceCreateBuffer(_sg.wgpu.dev, &wgpu_buf_desc);
if (0 == buf->wgpu.buf) {
_SG_ERROR(WGPU_CREATE_BUFFER_FAILED);
return SG_RESOURCESTATE_FAILED;
}
if (map_at_creation) {
SOKOL_ASSERT(desc->data.ptr && (desc->data.size > 0));
SOKOL_ASSERT(desc->data.size <= (size_t)buf->cmn.size);
// FIXME: inefficient on WASM
void* ptr = wgpuBufferGetMappedRange(buf->wgpu.buf, 0, wgpu_buf_size);
SOKOL_ASSERT(ptr);
memcpy(ptr, desc->data.ptr, desc->data.size);
wgpuBufferUnmap(buf->wgpu.buf);
}
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_discard_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
if (buf->cmn.type == SG_BUFFERTYPE_STORAGEBUFFER) {
_sg_wgpu_bindgroups_cache_invalidate(_SG_WGPU_BINDGROUPSCACHEITEMTYPE_STORAGEBUFFER, buf->slot.id);
}
if (buf->wgpu.buf) {
wgpuBufferRelease(buf->wgpu.buf);
}
}
_SOKOL_PRIVATE void _sg_wgpu_copy_buffer_data(const _sg_buffer_t* buf, uint64_t offset, const sg_range* data) {
SOKOL_ASSERT((offset + data->size) <= (size_t)buf->cmn.size);
// WebGPU's write-buffer requires the size to be a multiple of four, so we may need to split the copy
// operation into two writeBuffer calls
uint64_t clamped_size = data->size & ~3UL;
uint64_t extra_size = data->size & 3UL;
SOKOL_ASSERT(extra_size < 4);
wgpuQueueWriteBuffer(_sg.wgpu.queue, buf->wgpu.buf, offset, data->ptr, clamped_size);
if (extra_size > 0) {
const uint64_t extra_src_offset = clamped_size;
const uint64_t extra_dst_offset = offset + clamped_size;
uint8_t extra_data[4] = { 0 };
uint8_t* extra_src_ptr = ((uint8_t*)data->ptr) + extra_src_offset;
for (size_t i = 0; i < extra_size; i++) {
extra_data[i] = extra_src_ptr[i];
}
wgpuQueueWriteBuffer(_sg.wgpu.queue, buf->wgpu.buf, extra_dst_offset, extra_src_ptr, 4);
}
}
_SOKOL_PRIVATE void _sg_wgpu_copy_image_data(const _sg_image_t* img, WGPUTexture wgpu_tex, const sg_image_data* data) {
WGPUTextureDataLayout wgpu_layout;
_sg_clear(&wgpu_layout, sizeof(wgpu_layout));
WGPUImageCopyTexture wgpu_copy_tex;
_sg_clear(&wgpu_copy_tex, sizeof(wgpu_copy_tex));
wgpu_copy_tex.texture = wgpu_tex;
wgpu_copy_tex.aspect = WGPUTextureAspect_All;
WGPUExtent3D wgpu_extent;
_sg_clear(&wgpu_extent, sizeof(wgpu_extent));
const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6 : 1;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++) {
wgpu_copy_tex.mipLevel = (uint32_t)mip_index;
wgpu_copy_tex.origin.z = (uint32_t)face_index;
int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index);
int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index);
int mip_slices;
switch (img->cmn.type) {
case SG_IMAGETYPE_CUBE:
mip_slices = 1;
break;
case SG_IMAGETYPE_3D:
mip_slices = _sg_miplevel_dim(img->cmn.num_slices, mip_index);
break;
default:
mip_slices = img->cmn.num_slices;
break;
}
const int row_pitch = _sg_row_pitch(img->cmn.pixel_format, mip_width, 1);
const int num_rows = _sg_num_rows(img->cmn.pixel_format, mip_height);
if (_sg_is_compressed_pixel_format(img->cmn.pixel_format)) {
mip_width = _sg_roundup(mip_width, 4);
mip_height = _sg_roundup(mip_height, 4);
}
wgpu_layout.offset = 0;
wgpu_layout.bytesPerRow = (uint32_t)row_pitch;
wgpu_layout.rowsPerImage = (uint32_t)num_rows;
wgpu_extent.width = (uint32_t)mip_width;
wgpu_extent.height = (uint32_t)mip_height;
wgpu_extent.depthOrArrayLayers = (uint32_t)mip_slices;
const sg_range* mip_data = &data->subimage[face_index][mip_index];
wgpuQueueWriteTexture(_sg.wgpu.queue, &wgpu_copy_tex, mip_data->ptr, mip_data->size, &wgpu_layout, &wgpu_extent);
}
}
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && desc);
const bool injected = (0 != desc->wgpu_texture);
if (injected) {
img->wgpu.tex = (WGPUTexture)desc->wgpu_texture;
wgpuTextureReference(img->wgpu.tex);
img->wgpu.view = (WGPUTextureView)desc->wgpu_texture_view;
if (img->wgpu.view) {
wgpuTextureViewReference(img->wgpu.view);
}
} else {
WGPUTextureDescriptor wgpu_tex_desc;
_sg_clear(&wgpu_tex_desc, sizeof(wgpu_tex_desc));
wgpu_tex_desc.label = desc->label;
wgpu_tex_desc.usage = WGPUTextureUsage_TextureBinding|WGPUTextureUsage_CopyDst;
if (desc->render_target) {
wgpu_tex_desc.usage |= WGPUTextureUsage_RenderAttachment;
}
wgpu_tex_desc.dimension = _sg_wgpu_texture_dimension(img->cmn.type);
wgpu_tex_desc.size.width = (uint32_t) img->cmn.width;
wgpu_tex_desc.size.height = (uint32_t) img->cmn.height;
if (desc->type == SG_IMAGETYPE_CUBE) {
wgpu_tex_desc.size.depthOrArrayLayers = 6;
} else {
wgpu_tex_desc.size.depthOrArrayLayers = (uint32_t) img->cmn.num_slices;
}
wgpu_tex_desc.format = _sg_wgpu_textureformat(img->cmn.pixel_format);
wgpu_tex_desc.mipLevelCount = (uint32_t) img->cmn.num_mipmaps;
wgpu_tex_desc.sampleCount = (uint32_t) img->cmn.sample_count;
img->wgpu.tex = wgpuDeviceCreateTexture(_sg.wgpu.dev, &wgpu_tex_desc);
if (0 == img->wgpu.tex) {
_SG_ERROR(WGPU_CREATE_TEXTURE_FAILED);
return SG_RESOURCESTATE_FAILED;
}
if ((img->cmn.usage == SG_USAGE_IMMUTABLE) && !img->cmn.render_target) {
_sg_wgpu_copy_image_data(img, img->wgpu.tex, &desc->data);
}
WGPUTextureViewDescriptor wgpu_texview_desc;
_sg_clear(&wgpu_texview_desc, sizeof(wgpu_texview_desc));
wgpu_texview_desc.label = desc->label;
wgpu_texview_desc.dimension = _sg_wgpu_texture_view_dimension(img->cmn.type);
wgpu_texview_desc.mipLevelCount = (uint32_t)img->cmn.num_mipmaps;
if (img->cmn.type == SG_IMAGETYPE_CUBE) {
wgpu_texview_desc.arrayLayerCount = 6;
} else if (img->cmn.type == SG_IMAGETYPE_ARRAY) {
wgpu_texview_desc.arrayLayerCount = (uint32_t)img->cmn.num_slices;
} else {
wgpu_texview_desc.arrayLayerCount = 1;
}
if (_sg_is_depth_or_depth_stencil_format(img->cmn.pixel_format)) {
wgpu_texview_desc.aspect = WGPUTextureAspect_DepthOnly;
} else {
wgpu_texview_desc.aspect = WGPUTextureAspect_All;
}
img->wgpu.view = wgpuTextureCreateView(img->wgpu.tex, &wgpu_texview_desc);
if (0 == img->wgpu.view) {
_SG_ERROR(WGPU_CREATE_TEXTURE_VIEW_FAILED);
return SG_RESOURCESTATE_FAILED;
}
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_discard_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
_sg_wgpu_bindgroups_cache_invalidate(_SG_WGPU_BINDGROUPSCACHEITEMTYPE_IMAGE, img->slot.id);
if (img->wgpu.view) {
wgpuTextureViewRelease(img->wgpu.view);
img->wgpu.view = 0;
}
if (img->wgpu.tex) {
wgpuTextureRelease(img->wgpu.tex);
img->wgpu.tex = 0;
}
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) {
SOKOL_ASSERT(smp && desc);
SOKOL_ASSERT(_sg.wgpu.dev);
const bool injected = (0 != desc->wgpu_sampler);
if (injected) {
smp->wgpu.smp = (WGPUSampler) desc->wgpu_sampler;
wgpuSamplerReference(smp->wgpu.smp);
} else {
WGPUSamplerDescriptor wgpu_desc;
_sg_clear(&wgpu_desc, sizeof(wgpu_desc));
wgpu_desc.label = desc->label;
wgpu_desc.addressModeU = _sg_wgpu_sampler_address_mode(desc->wrap_u);
wgpu_desc.addressModeV = _sg_wgpu_sampler_address_mode(desc->wrap_v);
wgpu_desc.addressModeW = _sg_wgpu_sampler_address_mode(desc->wrap_w);
wgpu_desc.magFilter = _sg_wgpu_sampler_minmag_filter(desc->mag_filter);
wgpu_desc.minFilter = _sg_wgpu_sampler_minmag_filter(desc->min_filter);
wgpu_desc.mipmapFilter = _sg_wgpu_sampler_mipmap_filter(desc->mipmap_filter);
wgpu_desc.lodMinClamp = desc->min_lod;
wgpu_desc.lodMaxClamp = desc->max_lod;
wgpu_desc.compare = _sg_wgpu_comparefunc(desc->compare);
if (wgpu_desc.compare == WGPUCompareFunction_Never) {
wgpu_desc.compare = WGPUCompareFunction_Undefined;
}
wgpu_desc.maxAnisotropy = (uint16_t)desc->max_anisotropy;
smp->wgpu.smp = wgpuDeviceCreateSampler(_sg.wgpu.dev, &wgpu_desc);
if (0 == smp->wgpu.smp) {
_SG_ERROR(WGPU_CREATE_SAMPLER_FAILED);
return SG_RESOURCESTATE_FAILED;
}
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_discard_sampler(_sg_sampler_t* smp) {
SOKOL_ASSERT(smp);
_sg_wgpu_bindgroups_cache_invalidate(_SG_WGPU_BINDGROUPSCACHEITEMTYPE_SAMPLER, smp->slot.id);
if (smp->wgpu.smp) {
wgpuSamplerRelease(smp->wgpu.smp);
smp->wgpu.smp = 0;
}
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && desc);
SOKOL_ASSERT(desc->vs.source && desc->fs.source);
WGPUBindGroupLayoutEntry wgpu_bgl_entries[_SG_WGPU_MAX_BINDGROUP_ENTRIES];
_sg_clear(wgpu_bgl_entries, sizeof(wgpu_bgl_entries));
int bgl_index = 0;
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS) ? &desc->vs : &desc->fs;
_sg_shader_stage_t* cmn_stage = &shd->cmn.stage[stage_index];
_sg_wgpu_shader_stage_t* wgpu_stage = &shd->wgpu.stage[stage_index];
_sg_strcpy(&wgpu_stage->entry, stage_desc->entry);
WGPUShaderModuleWGSLDescriptor wgpu_shdmod_wgsl_desc;
_sg_clear(&wgpu_shdmod_wgsl_desc, sizeof(wgpu_shdmod_wgsl_desc));
wgpu_shdmod_wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
wgpu_shdmod_wgsl_desc.code = stage_desc->source;
WGPUShaderModuleDescriptor wgpu_shdmod_desc;
_sg_clear(&wgpu_shdmod_desc, sizeof(wgpu_shdmod_desc));
wgpu_shdmod_desc.nextInChain = &wgpu_shdmod_wgsl_desc.chain;
wgpu_shdmod_desc.label = desc->label;
wgpu_stage->module = wgpuDeviceCreateShaderModule(_sg.wgpu.dev, &wgpu_shdmod_desc);
if (0 == wgpu_stage->module) {
_SG_ERROR(WGPU_CREATE_SHADER_MODULE_FAILED);
return SG_RESOURCESTATE_FAILED;
}
const int num_images = cmn_stage->num_images;
if (num_images > (int)_sg.wgpu.limits.limits.maxSampledTexturesPerShaderStage) {
_SG_ERROR(WGPU_SHADER_TOO_MANY_IMAGES);
return SG_RESOURCESTATE_FAILED;
}
const int num_samplers = cmn_stage->num_samplers;
if (num_samplers > (int)_sg.wgpu.limits.limits.maxSamplersPerShaderStage) {
_SG_ERROR(WGPU_SHADER_TOO_MANY_SAMPLERS);
return SG_RESOURCESTATE_FAILED;
}
const int num_sbufs = cmn_stage->num_storage_buffers;
if (num_sbufs > (int)_sg.wgpu.limits.limits.maxStorageBuffersPerShaderStage) {
_SG_ERROR(WGPU_SHADER_TOO_MANY_STORAGEBUFFERS);
return SG_RESOURCESTATE_FAILED;
}
for (int img_index = 0; img_index < num_images; img_index++) {
SOKOL_ASSERT(bgl_index < _SG_WGPU_MAX_BINDGROUP_ENTRIES);
WGPUBindGroupLayoutEntry* wgpu_bgl_entry = &wgpu_bgl_entries[bgl_index++];
const sg_shader_image_desc* img_desc = &stage_desc->images[img_index];
wgpu_bgl_entry->binding = _sg_wgpu_image_binding((sg_shader_stage)stage_index, img_index);
wgpu_bgl_entry->visibility = _sg_wgpu_shader_stage((sg_shader_stage)stage_index);
wgpu_bgl_entry->texture.viewDimension = _sg_wgpu_texture_view_dimension(img_desc->image_type);
wgpu_bgl_entry->texture.sampleType = _sg_wgpu_texture_sample_type(img_desc->sample_type);
wgpu_bgl_entry->texture.multisampled = img_desc->multisampled;
}
for (int smp_index = 0; smp_index < num_samplers; smp_index++) {
SOKOL_ASSERT(bgl_index < _SG_WGPU_MAX_BINDGROUP_ENTRIES);
WGPUBindGroupLayoutEntry* wgpu_bgl_entry = &wgpu_bgl_entries[bgl_index++];
const sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[smp_index];
wgpu_bgl_entry->binding = _sg_wgpu_sampler_binding((sg_shader_stage)stage_index, smp_index);
wgpu_bgl_entry->visibility = _sg_wgpu_shader_stage((sg_shader_stage)stage_index);
wgpu_bgl_entry->sampler.type = _sg_wgpu_sampler_binding_type(smp_desc->sampler_type);
}
for (int sbuf_index = 0; sbuf_index < num_sbufs; sbuf_index++) {
SOKOL_ASSERT(bgl_index < _SG_WGPU_MAX_BINDGROUP_ENTRIES);
WGPUBindGroupLayoutEntry* wgpu_bgl_entry = &wgpu_bgl_entries[bgl_index++];
const sg_shader_storage_buffer_desc* sbuf_desc = &stage_desc->storage_buffers[sbuf_index];
wgpu_bgl_entry->binding = _sg_wgpu_storagebuffer_binding((sg_shader_stage)stage_index, sbuf_index);
wgpu_bgl_entry->visibility = _sg_wgpu_shader_stage((sg_shader_stage)stage_index);
wgpu_bgl_entry->buffer.type = sbuf_desc->readonly ? WGPUBufferBindingType_ReadOnlyStorage : WGPUBufferBindingType_Storage;
}
}
WGPUBindGroupLayoutDescriptor wgpu_bgl_desc;
_sg_clear(&wgpu_bgl_desc, sizeof(wgpu_bgl_desc));
wgpu_bgl_desc.entryCount = (size_t)bgl_index;
wgpu_bgl_desc.entries = &wgpu_bgl_entries[0];
shd->wgpu.bind_group_layout = wgpuDeviceCreateBindGroupLayout(_sg.wgpu.dev, &wgpu_bgl_desc);
if (shd->wgpu.bind_group_layout == 0) {
_SG_ERROR(WGPU_SHADER_CREATE_BINDGROUP_LAYOUT_FAILED);
return SG_RESOURCESTATE_FAILED;
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_discard_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
if (shd->wgpu.bind_group_layout) {
wgpuBindGroupLayoutRelease(shd->wgpu.bind_group_layout);
shd->wgpu.bind_group_layout = 0;
}
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
_sg_wgpu_shader_stage_t* wgpu_stage = &shd->wgpu.stage[stage_index];
if (wgpu_stage->module) {
wgpuShaderModuleRelease(wgpu_stage->module);
wgpu_stage->module = 0;
}
}
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && shd && desc);
SOKOL_ASSERT(desc->shader.id == shd->slot.id);
SOKOL_ASSERT(shd->wgpu.bind_group_layout);
pip->shader = shd;
pip->wgpu.blend_color.r = (double) desc->blend_color.r;
pip->wgpu.blend_color.g = (double) desc->blend_color.g;
pip->wgpu.blend_color.b = (double) desc->blend_color.b;
pip->wgpu.blend_color.a = (double) desc->blend_color.a;
// - @group(0) for uniform blocks
// - @group(1) for all image and sampler resources
WGPUBindGroupLayout wgpu_bgl[_SG_WGPU_NUM_BINDGROUPS];
_sg_clear(&wgpu_bgl, sizeof(wgpu_bgl));
wgpu_bgl[_SG_WGPU_UNIFORM_BINDGROUP_INDEX] = _sg.wgpu.uniform.bind.group_layout;
wgpu_bgl[_SG_WGPU_IMAGE_SAMPLER_BINDGROUP_INDEX] = shd->wgpu.bind_group_layout;
WGPUPipelineLayoutDescriptor wgpu_pl_desc;
_sg_clear(&wgpu_pl_desc, sizeof(wgpu_pl_desc));
wgpu_pl_desc.bindGroupLayoutCount = _SG_WGPU_NUM_BINDGROUPS;
wgpu_pl_desc.bindGroupLayouts = &wgpu_bgl[0];
const WGPUPipelineLayout wgpu_pip_layout = wgpuDeviceCreatePipelineLayout(_sg.wgpu.dev, &wgpu_pl_desc);
if (0 == wgpu_pip_layout) {
_SG_ERROR(WGPU_CREATE_PIPELINE_LAYOUT_FAILED);
return SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT(wgpu_pip_layout);
WGPUVertexBufferLayout wgpu_vb_layouts[SG_MAX_VERTEX_BUFFERS];
_sg_clear(wgpu_vb_layouts, sizeof(wgpu_vb_layouts));
WGPUVertexAttribute wgpu_vtx_attrs[SG_MAX_VERTEX_BUFFERS][SG_MAX_VERTEX_ATTRIBUTES];
_sg_clear(wgpu_vtx_attrs, sizeof(wgpu_vtx_attrs));
int wgpu_vb_num = 0;
for (int vb_idx = 0; vb_idx < SG_MAX_VERTEX_BUFFERS; vb_idx++, wgpu_vb_num++) {
const sg_vertex_buffer_layout_state* vbl_state = &desc->layout.buffers[vb_idx];
if (0 == vbl_state->stride) {
break;
}
wgpu_vb_layouts[vb_idx].arrayStride = (uint64_t)vbl_state->stride;
wgpu_vb_layouts[vb_idx].stepMode = _sg_wgpu_stepmode(vbl_state->step_func);
wgpu_vb_layouts[vb_idx].attributes = &wgpu_vtx_attrs[vb_idx][0];
}
for (int va_idx = 0; va_idx < SG_MAX_VERTEX_ATTRIBUTES; va_idx++) {
const sg_vertex_attr_state* va_state = &desc->layout.attrs[va_idx];
if (SG_VERTEXFORMAT_INVALID == va_state->format) {
break;
}
const int vb_idx = va_state->buffer_index;
SOKOL_ASSERT(vb_idx < SG_MAX_VERTEX_BUFFERS);
pip->cmn.vertex_buffer_layout_active[vb_idx] = true;
const size_t wgpu_attr_idx = wgpu_vb_layouts[vb_idx].attributeCount;
wgpu_vb_layouts[vb_idx].attributeCount += 1;
wgpu_vtx_attrs[vb_idx][wgpu_attr_idx].format = _sg_wgpu_vertexformat(va_state->format);
wgpu_vtx_attrs[vb_idx][wgpu_attr_idx].offset = (uint64_t)va_state->offset;
wgpu_vtx_attrs[vb_idx][wgpu_attr_idx].shaderLocation = (uint32_t)va_idx;
}
WGPURenderPipelineDescriptor wgpu_pip_desc;
_sg_clear(&wgpu_pip_desc, sizeof(wgpu_pip_desc));
WGPUDepthStencilState wgpu_ds_state;
_sg_clear(&wgpu_ds_state, sizeof(wgpu_ds_state));
WGPUFragmentState wgpu_frag_state;
_sg_clear(&wgpu_frag_state, sizeof(wgpu_frag_state));
WGPUColorTargetState wgpu_ctgt_state[SG_MAX_COLOR_ATTACHMENTS];
_sg_clear(&wgpu_ctgt_state, sizeof(wgpu_ctgt_state));
WGPUBlendState wgpu_blend_state[SG_MAX_COLOR_ATTACHMENTS];
_sg_clear(&wgpu_blend_state, sizeof(wgpu_blend_state));
wgpu_pip_desc.label = desc->label;
wgpu_pip_desc.layout = wgpu_pip_layout;
wgpu_pip_desc.vertex.module = shd->wgpu.stage[SG_SHADERSTAGE_VS].module;
wgpu_pip_desc.vertex.entryPoint = shd->wgpu.stage[SG_SHADERSTAGE_VS].entry.buf;
wgpu_pip_desc.vertex.bufferCount = (size_t)wgpu_vb_num;
wgpu_pip_desc.vertex.buffers = &wgpu_vb_layouts[0];
wgpu_pip_desc.primitive.topology = _sg_wgpu_topology(desc->primitive_type);
wgpu_pip_desc.primitive.stripIndexFormat = _sg_wgpu_stripindexformat(desc->primitive_type, desc->index_type);
wgpu_pip_desc.primitive.frontFace = _sg_wgpu_frontface(desc->face_winding);
wgpu_pip_desc.primitive.cullMode = _sg_wgpu_cullmode(desc->cull_mode);
if (SG_PIXELFORMAT_NONE != desc->depth.pixel_format) {
wgpu_ds_state.format = _sg_wgpu_textureformat(desc->depth.pixel_format);
wgpu_ds_state.depthWriteEnabled = desc->depth.write_enabled;
wgpu_ds_state.depthCompare = _sg_wgpu_comparefunc(desc->depth.compare);
wgpu_ds_state.stencilFront.compare = _sg_wgpu_comparefunc(desc->stencil.front.compare);
wgpu_ds_state.stencilFront.failOp = _sg_wgpu_stencilop(desc->stencil.front.fail_op);
wgpu_ds_state.stencilFront.depthFailOp = _sg_wgpu_stencilop(desc->stencil.front.depth_fail_op);
wgpu_ds_state.stencilFront.passOp = _sg_wgpu_stencilop(desc->stencil.front.pass_op);
wgpu_ds_state.stencilBack.compare = _sg_wgpu_comparefunc(desc->stencil.back.compare);
wgpu_ds_state.stencilBack.failOp = _sg_wgpu_stencilop(desc->stencil.back.fail_op);
wgpu_ds_state.stencilBack.depthFailOp = _sg_wgpu_stencilop(desc->stencil.back.depth_fail_op);
wgpu_ds_state.stencilBack.passOp = _sg_wgpu_stencilop(desc->stencil.back.pass_op);
wgpu_ds_state.stencilReadMask = desc->stencil.read_mask;
wgpu_ds_state.stencilWriteMask = desc->stencil.write_mask;
wgpu_ds_state.depthBias = (int32_t)desc->depth.bias;
wgpu_ds_state.depthBiasSlopeScale = desc->depth.bias_slope_scale;
wgpu_ds_state.depthBiasClamp = desc->depth.bias_clamp;
wgpu_pip_desc.depthStencil = &wgpu_ds_state;
}
wgpu_pip_desc.multisample.count = (uint32_t)desc->sample_count;
wgpu_pip_desc.multisample.mask = 0xFFFFFFFF;
wgpu_pip_desc.multisample.alphaToCoverageEnabled = desc->alpha_to_coverage_enabled;
if (desc->color_count > 0) {
wgpu_frag_state.module = shd->wgpu.stage[SG_SHADERSTAGE_FS].module;
wgpu_frag_state.entryPoint = shd->wgpu.stage[SG_SHADERSTAGE_FS].entry.buf;
wgpu_frag_state.targetCount = (size_t)desc->color_count;
wgpu_frag_state.targets = &wgpu_ctgt_state[0];
for (int i = 0; i < desc->color_count; i++) {
SOKOL_ASSERT(i < SG_MAX_COLOR_ATTACHMENTS);
wgpu_ctgt_state[i].format = _sg_wgpu_textureformat(desc->colors[i].pixel_format);
wgpu_ctgt_state[i].writeMask = _sg_wgpu_colorwritemask(desc->colors[i].write_mask);
if (desc->colors[i].blend.enabled) {
wgpu_ctgt_state[i].blend = &wgpu_blend_state[i];
wgpu_blend_state[i].color.operation = _sg_wgpu_blendop(desc->colors[i].blend.op_rgb);
wgpu_blend_state[i].color.srcFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.src_factor_rgb);
wgpu_blend_state[i].color.dstFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.dst_factor_rgb);
wgpu_blend_state[i].alpha.operation = _sg_wgpu_blendop(desc->colors[i].blend.op_alpha);
wgpu_blend_state[i].alpha.srcFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.src_factor_alpha);
wgpu_blend_state[i].alpha.dstFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.dst_factor_alpha);
}
}
wgpu_pip_desc.fragment = &wgpu_frag_state;
}
pip->wgpu.pip = wgpuDeviceCreateRenderPipeline(_sg.wgpu.dev, &wgpu_pip_desc);
wgpuPipelineLayoutRelease(wgpu_pip_layout);
if (0 == pip->wgpu.pip) {
_SG_ERROR(WGPU_CREATE_RENDER_PIPELINE_FAILED);
return SG_RESOURCESTATE_FAILED;
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_discard_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
_sg_wgpu_bindgroups_cache_invalidate(_SG_WGPU_BINDGROUPSCACHEITEMTYPE_PIPELINE, pip->slot.id);
if (pip == _sg.wgpu.cur_pipeline) {
_sg.wgpu.cur_pipeline = 0;
_sg.wgpu.cur_pipeline_id.id = SG_INVALID_ID;
}
if (pip->wgpu.pip) {
wgpuRenderPipelineRelease(pip->wgpu.pip);
pip->wgpu.pip = 0;
}
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_img, const sg_attachments_desc* desc) {
SOKOL_ASSERT(atts && desc);
SOKOL_ASSERT(color_images && resolve_images);
// copy image pointers and create renderable wgpu texture views
for (int i = 0; i < atts->cmn.num_colors; i++) {
const sg_attachment_desc* color_desc = &desc->colors[i];
_SOKOL_UNUSED(color_desc);
SOKOL_ASSERT(color_desc->image.id != SG_INVALID_ID);
SOKOL_ASSERT(0 == atts->wgpu.colors[i].image);
SOKOL_ASSERT(color_images[i] && (color_images[i]->slot.id == color_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(color_images[i]->cmn.pixel_format));
SOKOL_ASSERT(color_images[i]->wgpu.tex);
atts->wgpu.colors[i].image = color_images[i];
WGPUTextureViewDescriptor wgpu_color_view_desc;
_sg_clear(&wgpu_color_view_desc, sizeof(wgpu_color_view_desc));
wgpu_color_view_desc.baseMipLevel = (uint32_t) color_desc->mip_level;
wgpu_color_view_desc.mipLevelCount = 1;
wgpu_color_view_desc.baseArrayLayer = (uint32_t) color_desc->slice;
wgpu_color_view_desc.arrayLayerCount = 1;
atts->wgpu.colors[i].view = wgpuTextureCreateView(color_images[i]->wgpu.tex, &wgpu_color_view_desc);
if (0 == atts->wgpu.colors[i].view) {
_SG_ERROR(WGPU_ATTACHMENTS_CREATE_TEXTURE_VIEW_FAILED);
return SG_RESOURCESTATE_FAILED;
}
const sg_attachment_desc* resolve_desc = &desc->resolves[i];
if (resolve_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(0 == atts->wgpu.resolves[i].image);
SOKOL_ASSERT(resolve_images[i] && (resolve_images[i]->slot.id == resolve_desc->image.id));
SOKOL_ASSERT(color_images[i] && (color_images[i]->cmn.pixel_format == resolve_images[i]->cmn.pixel_format));
SOKOL_ASSERT(resolve_images[i]->wgpu.tex);
atts->wgpu.resolves[i].image = resolve_images[i];
WGPUTextureViewDescriptor wgpu_resolve_view_desc;
_sg_clear(&wgpu_resolve_view_desc, sizeof(wgpu_resolve_view_desc));
wgpu_resolve_view_desc.baseMipLevel = (uint32_t) resolve_desc->mip_level;
wgpu_resolve_view_desc.mipLevelCount = 1;
wgpu_resolve_view_desc.baseArrayLayer = (uint32_t) resolve_desc->slice;
wgpu_resolve_view_desc.arrayLayerCount = 1;
atts->wgpu.resolves[i].view = wgpuTextureCreateView(resolve_images[i]->wgpu.tex, &wgpu_resolve_view_desc);
if (0 == atts->wgpu.resolves[i].view) {
_SG_ERROR(WGPU_ATTACHMENTS_CREATE_TEXTURE_VIEW_FAILED);
return SG_RESOURCESTATE_FAILED;
}
}
}
SOKOL_ASSERT(0 == atts->wgpu.depth_stencil.image);
const sg_attachment_desc* ds_desc = &desc->depth_stencil;
if (ds_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(ds_img && (ds_img->slot.id == ds_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(ds_img->cmn.pixel_format));
SOKOL_ASSERT(ds_img->wgpu.tex);
atts->wgpu.depth_stencil.image = ds_img;
WGPUTextureViewDescriptor wgpu_ds_view_desc;
_sg_clear(&wgpu_ds_view_desc, sizeof(wgpu_ds_view_desc));
wgpu_ds_view_desc.baseMipLevel = (uint32_t) ds_desc->mip_level;
wgpu_ds_view_desc.mipLevelCount = 1;
wgpu_ds_view_desc.baseArrayLayer = (uint32_t) ds_desc->slice;
wgpu_ds_view_desc.arrayLayerCount = 1;
atts->wgpu.depth_stencil.view = wgpuTextureCreateView(ds_img->wgpu.tex, &wgpu_ds_view_desc);
if (0 == atts->wgpu.depth_stencil.view) {
_SG_ERROR(WGPU_ATTACHMENTS_CREATE_TEXTURE_VIEW_FAILED);
return SG_RESOURCESTATE_FAILED;
}
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_discard_attachments(_sg_attachments_t* atts) {
SOKOL_ASSERT(atts);
for (int i = 0; i < atts->cmn.num_colors; i++) {
if (atts->wgpu.colors[i].view) {
wgpuTextureViewRelease(atts->wgpu.colors[i].view);
atts->wgpu.colors[i].view = 0;
}
if (atts->wgpu.resolves[i].view) {
wgpuTextureViewRelease(atts->wgpu.resolves[i].view);
atts->wgpu.resolves[i].view = 0;
}
}
if (atts->wgpu.depth_stencil.view) {
wgpuTextureViewRelease(atts->wgpu.depth_stencil.view);
atts->wgpu.depth_stencil.view = 0;
}
}
_SOKOL_PRIVATE _sg_image_t* _sg_wgpu_attachments_color_image(const _sg_attachments_t* atts, int index) {
SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
// NOTE: may return null
return atts->wgpu.colors[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_wgpu_attachments_resolve_image(const _sg_attachments_t* atts, int index) {
SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
// NOTE: may return null
return atts->wgpu.resolves[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_wgpu_attachments_ds_image(const _sg_attachments_t* atts) {
// NOTE: may return null
SOKOL_ASSERT(atts);
return atts->wgpu.depth_stencil.image;
}
_SOKOL_PRIVATE void _sg_wgpu_init_color_att(WGPURenderPassColorAttachment* wgpu_att, const sg_color_attachment_action* action, WGPUTextureView color_view, WGPUTextureView resolve_view) {
wgpu_att->depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
wgpu_att->view = color_view;
wgpu_att->resolveTarget = resolve_view;
wgpu_att->loadOp = _sg_wgpu_load_op(color_view, action->load_action);
wgpu_att->storeOp = _sg_wgpu_store_op(color_view, action->store_action);
wgpu_att->clearValue.r = action->clear_value.r;
wgpu_att->clearValue.g = action->clear_value.g;
wgpu_att->clearValue.b = action->clear_value.b;
wgpu_att->clearValue.a = action->clear_value.a;
}
_SOKOL_PRIVATE void _sg_wgpu_init_ds_att(WGPURenderPassDepthStencilAttachment* wgpu_att, const sg_pass_action* action, sg_pixel_format fmt, WGPUTextureView view) {
wgpu_att->view = view;
wgpu_att->depthLoadOp = _sg_wgpu_load_op(view, action->depth.load_action);
wgpu_att->depthStoreOp = _sg_wgpu_store_op(view, action->depth.store_action);
wgpu_att->depthClearValue = action->depth.clear_value;
wgpu_att->depthReadOnly = false;
if (_sg_is_depth_stencil_format(fmt)) {
wgpu_att->stencilLoadOp = _sg_wgpu_load_op(view, action->stencil.load_action);
wgpu_att->stencilStoreOp = _sg_wgpu_store_op(view, action->stencil.store_action);
} else {
wgpu_att->stencilLoadOp = WGPULoadOp_Undefined;
wgpu_att->stencilStoreOp = WGPUStoreOp_Undefined;
}
wgpu_att->stencilClearValue = action->stencil.clear_value;
wgpu_att->stencilReadOnly = false;
}
_SOKOL_PRIVATE void _sg_wgpu_begin_pass(const sg_pass* pass) {
SOKOL_ASSERT(pass);
SOKOL_ASSERT(_sg.wgpu.cmd_enc);
SOKOL_ASSERT(_sg.wgpu.dev);
const _sg_attachments_t* atts = _sg.cur_pass.atts;
const sg_swapchain* swapchain = &pass->swapchain;
const sg_pass_action* action = &pass->action;
_sg.wgpu.cur_pipeline = 0;
_sg.wgpu.cur_pipeline_id.id = SG_INVALID_ID;
WGPURenderPassDescriptor wgpu_pass_desc;
WGPURenderPassColorAttachment wgpu_color_att[SG_MAX_COLOR_ATTACHMENTS];
WGPURenderPassDepthStencilAttachment wgpu_ds_att;
_sg_clear(&wgpu_pass_desc, sizeof(wgpu_pass_desc));
_sg_clear(&wgpu_color_att, sizeof(wgpu_color_att));
_sg_clear(&wgpu_ds_att, sizeof(wgpu_ds_att));
wgpu_pass_desc.label = pass->label;
if (atts) {
SOKOL_ASSERT(atts->slot.state == SG_RESOURCESTATE_VALID);
for (int i = 0; i < atts->cmn.num_colors; i++) {
_sg_wgpu_init_color_att(&wgpu_color_att[i], &action->colors[i], atts->wgpu.colors[i].view, atts->wgpu.resolves[i].view);
}
wgpu_pass_desc.colorAttachmentCount = (size_t)atts->cmn.num_colors;
wgpu_pass_desc.colorAttachments = &wgpu_color_att[0];
if (atts->wgpu.depth_stencil.image) {
_sg_wgpu_init_ds_att(&wgpu_ds_att, action, atts->wgpu.depth_stencil.image->cmn.pixel_format, atts->wgpu.depth_stencil.view);
wgpu_pass_desc.depthStencilAttachment = &wgpu_ds_att;
}
} else {
WGPUTextureView wgpu_color_view = (WGPUTextureView) swapchain->wgpu.render_view;
WGPUTextureView wgpu_resolve_view = (WGPUTextureView) swapchain->wgpu.resolve_view;
WGPUTextureView wgpu_depth_stencil_view = (WGPUTextureView) swapchain->wgpu.depth_stencil_view;
_sg_wgpu_init_color_att(&wgpu_color_att[0], &action->colors[0], wgpu_color_view, wgpu_resolve_view);
wgpu_pass_desc.colorAttachmentCount = 1;
wgpu_pass_desc.colorAttachments = &wgpu_color_att[0];
if (wgpu_depth_stencil_view) {
SOKOL_ASSERT(swapchain->depth_format > SG_PIXELFORMAT_NONE);
_sg_wgpu_init_ds_att(&wgpu_ds_att, action, swapchain->depth_format, wgpu_depth_stencil_view);
wgpu_pass_desc.depthStencilAttachment = &wgpu_ds_att;
}
}
_sg.wgpu.pass_enc = wgpuCommandEncoderBeginRenderPass(_sg.wgpu.cmd_enc, &wgpu_pass_desc);
SOKOL_ASSERT(_sg.wgpu.pass_enc);
// clear bindings cache and apply an empty image-sampler bindgroup
_sg_wgpu_bindings_cache_clear();
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, _SG_WGPU_IMAGE_SAMPLER_BINDGROUP_INDEX, _sg.wgpu.empty_bind_group, 0, 0);
_sg_stats_add(wgpu.bindings.num_set_bindgroup, 1);
// initial uniform buffer binding (required even if no uniforms are set in the frame)
_sg_wgpu_uniform_buffer_on_begin_pass();
}
_SOKOL_PRIVATE void _sg_wgpu_end_pass(void) {
if (_sg.wgpu.pass_enc) {
wgpuRenderPassEncoderEnd(_sg.wgpu.pass_enc);
wgpuRenderPassEncoderRelease(_sg.wgpu.pass_enc);
_sg.wgpu.pass_enc = 0;
}
}
_SOKOL_PRIVATE void _sg_wgpu_commit(void) {
SOKOL_ASSERT(_sg.wgpu.cmd_enc);
_sg_wgpu_uniform_buffer_on_commit();
WGPUCommandBufferDescriptor cmd_buf_desc;
_sg_clear(&cmd_buf_desc, sizeof(cmd_buf_desc));
WGPUCommandBuffer wgpu_cmd_buf = wgpuCommandEncoderFinish(_sg.wgpu.cmd_enc, &cmd_buf_desc);
SOKOL_ASSERT(wgpu_cmd_buf);
wgpuCommandEncoderRelease(_sg.wgpu.cmd_enc);
_sg.wgpu.cmd_enc = 0;
wgpuQueueSubmit(_sg.wgpu.queue, 1, &wgpu_cmd_buf);
wgpuCommandBufferRelease(wgpu_cmd_buf);
// create a new render-command-encoder for next frame
WGPUCommandEncoderDescriptor cmd_enc_desc;
_sg_clear(&cmd_enc_desc, sizeof(cmd_enc_desc));
_sg.wgpu.cmd_enc = wgpuDeviceCreateCommandEncoder(_sg.wgpu.dev, &cmd_enc_desc);
}
_SOKOL_PRIVATE void _sg_wgpu_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.wgpu.pass_enc);
// FIXME FIXME FIXME: CLIPPING THE VIEWPORT HERE IS WRONG!!!
// (but currently required because WebGPU insists that the viewport rectangle must be
// fully contained inside the framebuffer, but this doesn't make any sense, and also
// isn't required by the backend APIs)
const _sg_recti_t clip = _sg_clipi(x, y, w, h, _sg.cur_pass.width, _sg.cur_pass.height);
float xf = (float) clip.x;
float yf = (float) (origin_top_left ? clip.y : (_sg.cur_pass.height - (clip.y + clip.h)));
float wf = (float) clip.w;
float hf = (float) clip.h;
wgpuRenderPassEncoderSetViewport(_sg.wgpu.pass_enc, xf, yf, wf, hf, 0.0f, 1.0f);
}
_SOKOL_PRIVATE void _sg_wgpu_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.wgpu.pass_enc);
const _sg_recti_t clip = _sg_clipi(x, y, w, h, _sg.cur_pass.width, _sg.cur_pass.height);
uint32_t sx = (uint32_t) clip.x;
uint32_t sy = (uint32_t) (origin_top_left ? clip.y : (_sg.cur_pass.height - (clip.y + clip.h)));
uint32_t sw = (uint32_t) clip.w;
uint32_t sh = (uint32_t) clip.h;
wgpuRenderPassEncoderSetScissorRect(_sg.wgpu.pass_enc, sx, sy, sw, sh);
}
_SOKOL_PRIVATE void _sg_wgpu_apply_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
SOKOL_ASSERT(pip->wgpu.pip);
SOKOL_ASSERT(_sg.wgpu.pass_enc);
_sg.wgpu.use_indexed_draw = (pip->cmn.index_type != SG_INDEXTYPE_NONE);
_sg.wgpu.cur_pipeline = pip;
_sg.wgpu.cur_pipeline_id.id = pip->slot.id;
wgpuRenderPassEncoderSetPipeline(_sg.wgpu.pass_enc, pip->wgpu.pip);
wgpuRenderPassEncoderSetBlendConstant(_sg.wgpu.pass_enc, &pip->wgpu.blend_color);
wgpuRenderPassEncoderSetStencilReference(_sg.wgpu.pass_enc, pip->cmn.stencil.ref);
}
_SOKOL_PRIVATE bool _sg_wgpu_apply_bindings(_sg_bindings_t* bnd) {
SOKOL_ASSERT(_sg.wgpu.pass_enc);
SOKOL_ASSERT(bnd);
SOKOL_ASSERT(bnd->pip->shader && (bnd->pip->cmn.shader_id.id == bnd->pip->shader->slot.id));
bool retval = true;
retval &= _sg_wgpu_apply_index_buffer(bnd);
retval &= _sg_wgpu_apply_vertex_buffers(bnd);
retval &= _sg_wgpu_apply_bindgroup(bnd);
return retval;
}
_SOKOL_PRIVATE void _sg_wgpu_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
const uint32_t alignment = _sg.wgpu.limits.limits.minUniformBufferOffsetAlignment;
SOKOL_ASSERT(_sg.wgpu.pass_enc);
SOKOL_ASSERT(_sg.wgpu.uniform.staging);
SOKOL_ASSERT((_sg.wgpu.uniform.offset + data->size) <= _sg.wgpu.uniform.num_bytes);
SOKOL_ASSERT((_sg.wgpu.uniform.offset & (alignment - 1)) == 0);
SOKOL_ASSERT(_sg.wgpu.cur_pipeline && _sg.wgpu.cur_pipeline->shader);
SOKOL_ASSERT(_sg.wgpu.cur_pipeline->slot.id == _sg.wgpu.cur_pipeline_id.id);
SOKOL_ASSERT(_sg.wgpu.cur_pipeline->shader->slot.id == _sg.wgpu.cur_pipeline->cmn.shader_id.id);
SOKOL_ASSERT(ub_index < _sg.wgpu.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks);
SOKOL_ASSERT(data->size <= _sg.wgpu.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size);
SOKOL_ASSERT(data->size <= _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE);
_sg_stats_add(wgpu.uniforms.num_set_bindgroup, 1);
memcpy(_sg.wgpu.uniform.staging + _sg.wgpu.uniform.offset, data->ptr, data->size);
_sg.wgpu.uniform.bind.offsets[stage_index][ub_index] = _sg.wgpu.uniform.offset;
_sg.wgpu.uniform.offset = _sg_roundup_u32(_sg.wgpu.uniform.offset + (uint32_t)data->size, alignment);
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc,
_SG_WGPU_UNIFORM_BINDGROUP_INDEX,
_sg.wgpu.uniform.bind.group,
SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS,
&_sg.wgpu.uniform.bind.offsets[0][0]);
}
_SOKOL_PRIVATE void _sg_wgpu_draw(int base_element, int num_elements, int num_instances) {
SOKOL_ASSERT(_sg.wgpu.pass_enc);
SOKOL_ASSERT(_sg.wgpu.cur_pipeline && (_sg.wgpu.cur_pipeline->slot.id == _sg.wgpu.cur_pipeline_id.id));
if (SG_INDEXTYPE_NONE != _sg.wgpu.cur_pipeline->cmn.index_type) {
wgpuRenderPassEncoderDrawIndexed(_sg.wgpu.pass_enc, (uint32_t)num_elements, (uint32_t)num_instances, (uint32_t)base_element, 0, 0);
} else {
wgpuRenderPassEncoderDraw(_sg.wgpu.pass_enc, (uint32_t)num_elements, (uint32_t)num_instances, (uint32_t)base_element, 0);
}
}
_SOKOL_PRIVATE void _sg_wgpu_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
SOKOL_ASSERT(data && data->ptr && (data->size > 0));
SOKOL_ASSERT(buf);
_sg_wgpu_copy_buffer_data(buf, 0, data);
}
_SOKOL_PRIVATE void _sg_wgpu_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
SOKOL_ASSERT(data && data->ptr && (data->size > 0));
_SOKOL_UNUSED(new_frame);
_sg_wgpu_copy_buffer_data(buf, (uint64_t)buf->cmn.append_pos, data);
}
_SOKOL_PRIVATE void _sg_wgpu_update_image(_sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(img && data);
_sg_wgpu_copy_image_data(img, img->wgpu.tex, data);
}
#endif
// ██████ ███████ ███ ██ ███████ ██████ ██ ██████ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████
// ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██
// ██ ███ █████ ██ ██ ██ █████ ██████ ██ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ███████ ██ ████ ███████ ██ ██ ██ ██████ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████
//
// >>generic backend
static inline void _sg_setup_backend(const sg_desc* desc) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_setup_backend(desc);
#elif defined(SOKOL_METAL)
_sg_mtl_setup_backend(desc);
#elif defined(SOKOL_D3D11)
_sg_d3d11_setup_backend(desc);
#elif defined(SOKOL_WGPU)
_sg_wgpu_setup_backend(desc);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_setup_backend(desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_discard_backend(void) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_discard_backend();
#elif defined(SOKOL_METAL)
_sg_mtl_discard_backend();
#elif defined(SOKOL_D3D11)
_sg_d3d11_discard_backend();
#elif defined(SOKOL_WGPU)
_sg_wgpu_discard_backend();
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_discard_backend();
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_reset_state_cache(void) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_reset_state_cache();
#elif defined(SOKOL_METAL)
_sg_mtl_reset_state_cache();
#elif defined(SOKOL_D3D11)
_sg_d3d11_reset_state_cache();
#elif defined(SOKOL_WGPU)
_sg_wgpu_reset_state_cache();
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_reset_state_cache();
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_buffer(buf, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_buffer(buf, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_buffer(buf, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_buffer(buf, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_buffer(buf, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_discard_buffer(_sg_buffer_t* buf) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_discard_buffer(buf);
#elif defined(SOKOL_METAL)
_sg_mtl_discard_buffer(buf);
#elif defined(SOKOL_D3D11)
_sg_d3d11_discard_buffer(buf);
#elif defined(SOKOL_WGPU)
_sg_wgpu_discard_buffer(buf);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_discard_buffer(buf);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_image(_sg_image_t* img, const sg_image_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_image(img, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_image(img, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_image(img, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_image(img, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_image(img, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_discard_image(_sg_image_t* img) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_discard_image(img);
#elif defined(SOKOL_METAL)
_sg_mtl_discard_image(img);
#elif defined(SOKOL_D3D11)
_sg_d3d11_discard_image(img);
#elif defined(SOKOL_WGPU)
_sg_wgpu_discard_image(img);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_discard_image(img);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_sampler(smp, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_sampler(smp, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_sampler(smp, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_sampler(smp, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_sampler(smp, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_discard_sampler(_sg_sampler_t* smp) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_discard_sampler(smp);
#elif defined(SOKOL_METAL)
_sg_mtl_discard_sampler(smp);
#elif defined(SOKOL_D3D11)
_sg_d3d11_discard_sampler(smp);
#elif defined(SOKOL_WGPU)
_sg_wgpu_discard_sampler(smp);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_discard_sampler(smp);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_shader(shd, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_shader(shd, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_shader(shd, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_shader(shd, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_shader(shd, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_discard_shader(_sg_shader_t* shd) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_discard_shader(shd);
#elif defined(SOKOL_METAL)
_sg_mtl_discard_shader(shd);
#elif defined(SOKOL_D3D11)
_sg_d3d11_discard_shader(shd);
#elif defined(SOKOL_WGPU)
_sg_wgpu_discard_shader(shd);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_discard_shader(shd);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_pipeline(pip, shd, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_pipeline(pip, shd, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_pipeline(pip, shd, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_pipeline(pip, shd, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_pipeline(pip, shd, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_discard_pipeline(_sg_pipeline_t* pip) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_discard_pipeline(pip);
#elif defined(SOKOL_METAL)
_sg_mtl_discard_pipeline(pip);
#elif defined(SOKOL_D3D11)
_sg_d3d11_discard_pipeline(pip);
#elif defined(SOKOL_WGPU)
_sg_wgpu_discard_pipeline(pip);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_discard_pipeline(pip);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_image, const sg_attachments_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_attachments(atts, color_images, resolve_images, ds_image, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_attachments(atts, color_images, resolve_images, ds_image, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_attachments(atts, color_images, resolve_images, ds_image, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_attachments(atts, color_images, resolve_images, ds_image, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_attachments(atts, color_images, resolve_images, ds_image, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_discard_attachments(_sg_attachments_t* atts) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_discard_attachments(atts);
#elif defined(SOKOL_METAL)
_sg_mtl_discard_attachments(atts);
#elif defined(SOKOL_D3D11)
_sg_d3d11_discard_attachments(atts);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_discard_attachments(atts);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_discard_attachments(atts);
#else
#error("INVALID BACKEND");
#endif
}
static inline _sg_image_t* _sg_attachments_color_image(const _sg_attachments_t* atts, int index) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_attachments_color_image(atts, index);
#elif defined(SOKOL_METAL)
return _sg_mtl_attachments_color_image(atts, index);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_attachments_color_image(atts, index);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_attachments_color_image(atts, index);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_attachments_color_image(atts, index);
#else
#error("INVALID BACKEND");
#endif
}
static inline _sg_image_t* _sg_attachments_resolve_image(const _sg_attachments_t* atts, int index) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_attachments_resolve_image(atts, index);
#elif defined(SOKOL_METAL)
return _sg_mtl_attachments_resolve_image(atts, index);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_attachments_resolve_image(atts, index);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_attachments_resolve_image(atts, index);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_attachments_resolve_image(atts, index);
#else
#error("INVALID BACKEND");
#endif
}
static inline _sg_image_t* _sg_attachments_ds_image(const _sg_attachments_t* atts) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_attachments_ds_image(atts);
#elif defined(SOKOL_METAL)
return _sg_mtl_attachments_ds_image(atts);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_attachments_ds_image(atts);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_attachments_ds_image(atts);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_attachments_ds_image(atts);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_begin_pass(const sg_pass* pass) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_begin_pass(pass);
#elif defined(SOKOL_METAL)
_sg_mtl_begin_pass(pass);
#elif defined(SOKOL_D3D11)
_sg_d3d11_begin_pass(pass);
#elif defined(SOKOL_WGPU)
_sg_wgpu_begin_pass(pass);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_begin_pass(pass);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_end_pass(void) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_end_pass();
#elif defined(SOKOL_METAL)
_sg_mtl_end_pass();
#elif defined(SOKOL_D3D11)
_sg_d3d11_end_pass();
#elif defined(SOKOL_WGPU)
_sg_wgpu_end_pass();
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_end_pass();
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_apply_viewport(x, y, w, h, origin_top_left);
#elif defined(SOKOL_METAL)
_sg_mtl_apply_viewport(x, y, w, h, origin_top_left);
#elif defined(SOKOL_D3D11)
_sg_d3d11_apply_viewport(x, y, w, h, origin_top_left);
#elif defined(SOKOL_WGPU)
_sg_wgpu_apply_viewport(x, y, w, h, origin_top_left);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_apply_viewport(x, y, w, h, origin_top_left);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_apply_scissor_rect(x, y, w, h, origin_top_left);
#elif defined(SOKOL_METAL)
_sg_mtl_apply_scissor_rect(x, y, w, h, origin_top_left);
#elif defined(SOKOL_D3D11)
_sg_d3d11_apply_scissor_rect(x, y, w, h, origin_top_left);
#elif defined(SOKOL_WGPU)
_sg_wgpu_apply_scissor_rect(x, y, w, h, origin_top_left);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_apply_scissor_rect(x, y, w, h, origin_top_left);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_apply_pipeline(_sg_pipeline_t* pip) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_apply_pipeline(pip);
#elif defined(SOKOL_METAL)
_sg_mtl_apply_pipeline(pip);
#elif defined(SOKOL_D3D11)
_sg_d3d11_apply_pipeline(pip);
#elif defined(SOKOL_WGPU)
_sg_wgpu_apply_pipeline(pip);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_apply_pipeline(pip);
#else
#error("INVALID BACKEND");
#endif
}
static inline bool _sg_apply_bindings(_sg_bindings_t* bnd) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_apply_bindings(bnd);
#elif defined(SOKOL_METAL)
return _sg_mtl_apply_bindings(bnd);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_apply_bindings(bnd);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_apply_bindings(bnd);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_apply_bindings(bnd);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_apply_uniforms(stage_index, ub_index, data);
#elif defined(SOKOL_METAL)
_sg_mtl_apply_uniforms(stage_index, ub_index, data);
#elif defined(SOKOL_D3D11)
_sg_d3d11_apply_uniforms(stage_index, ub_index, data);
#elif defined(SOKOL_WGPU)
_sg_wgpu_apply_uniforms(stage_index, ub_index, data);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_apply_uniforms(stage_index, ub_index, data);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_draw(int base_element, int num_elements, int num_instances) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_draw(base_element, num_elements, num_instances);
#elif defined(SOKOL_METAL)
_sg_mtl_draw(base_element, num_elements, num_instances);
#elif defined(SOKOL_D3D11)
_sg_d3d11_draw(base_element, num_elements, num_instances);
#elif defined(SOKOL_WGPU)
_sg_wgpu_draw(base_element, num_elements, num_instances);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_draw(base_element, num_elements, num_instances);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_commit(void) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_commit();
#elif defined(SOKOL_METAL)
_sg_mtl_commit();
#elif defined(SOKOL_D3D11)
_sg_d3d11_commit();
#elif defined(SOKOL_WGPU)
_sg_wgpu_commit();
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_commit();
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_update_buffer(buf, data);
#elif defined(SOKOL_METAL)
_sg_mtl_update_buffer(buf, data);
#elif defined(SOKOL_D3D11)
_sg_d3d11_update_buffer(buf, data);
#elif defined(SOKOL_WGPU)
_sg_wgpu_update_buffer(buf, data);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_update_buffer(buf, data);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_append_buffer(buf, data, new_frame);
#elif defined(SOKOL_METAL)
_sg_mtl_append_buffer(buf, data, new_frame);
#elif defined(SOKOL_D3D11)
_sg_d3d11_append_buffer(buf, data, new_frame);
#elif defined(SOKOL_WGPU)
_sg_wgpu_append_buffer(buf, data, new_frame);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_append_buffer(buf, data, new_frame);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_update_image(_sg_image_t* img, const sg_image_data* data) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_update_image(img, data);
#elif defined(SOKOL_METAL)
_sg_mtl_update_image(img, data);
#elif defined(SOKOL_D3D11)
_sg_d3d11_update_image(img, data);
#elif defined(SOKOL_WGPU)
_sg_wgpu_update_image(img, data);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_update_image(img, data);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_push_debug_group(const char* name) {
#if defined(SOKOL_METAL)
_sg_mtl_push_debug_group(name);
#else
_SOKOL_UNUSED(name);
#endif
}
static inline void _sg_pop_debug_group(void) {
#if defined(SOKOL_METAL)
_sg_mtl_pop_debug_group();
#endif
}
// ██████ ██████ ██████ ██
// ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██
// ██ ██████ ██████ ███████
//
// >>pool
_SOKOL_PRIVATE void _sg_init_pool(_sg_pool_t* pool, int num) {
SOKOL_ASSERT(pool && (num >= 1));
// slot 0 is reserved for the 'invalid id', so bump the pool size by 1
pool->size = num + 1;
pool->queue_top = 0;
// generation counters indexable by pool slot index, slot 0 is reserved
size_t gen_ctrs_size = sizeof(uint32_t) * (size_t)pool->size;
pool->gen_ctrs = (uint32_t*)_sg_malloc_clear(gen_ctrs_size);
// it's not a bug to only reserve 'num' here
pool->free_queue = (int*) _sg_malloc_clear(sizeof(int) * (size_t)num);
// never allocate the zero-th pool item since the invalid id is 0
for (int i = pool->size-1; i >= 1; i--) {
pool->free_queue[pool->queue_top++] = i;
}
}
_SOKOL_PRIVATE void _sg_discard_pool(_sg_pool_t* pool) {
SOKOL_ASSERT(pool);
SOKOL_ASSERT(pool->free_queue);
_sg_free(pool->free_queue);
pool->free_queue = 0;
SOKOL_ASSERT(pool->gen_ctrs);
_sg_free(pool->gen_ctrs);
pool->gen_ctrs = 0;
pool->size = 0;
pool->queue_top = 0;
}
_SOKOL_PRIVATE int _sg_pool_alloc_index(_sg_pool_t* pool) {
SOKOL_ASSERT(pool);
SOKOL_ASSERT(pool->free_queue);
if (pool->queue_top > 0) {
int slot_index = pool->free_queue[--pool->queue_top];
SOKOL_ASSERT((slot_index > 0) && (slot_index < pool->size));
return slot_index;
} else {
// pool exhausted
return _SG_INVALID_SLOT_INDEX;
}
}
_SOKOL_PRIVATE void _sg_pool_free_index(_sg_pool_t* pool, int slot_index) {
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < pool->size));
SOKOL_ASSERT(pool);
SOKOL_ASSERT(pool->free_queue);
SOKOL_ASSERT(pool->queue_top < pool->size);
#ifdef SOKOL_DEBUG
// debug check against double-free
for (int i = 0; i < pool->queue_top; i++) {
SOKOL_ASSERT(pool->free_queue[i] != slot_index);
}
#endif
pool->free_queue[pool->queue_top++] = slot_index;
SOKOL_ASSERT(pool->queue_top <= (pool->size-1));
}
_SOKOL_PRIVATE void _sg_reset_slot(_sg_slot_t* slot) {
SOKOL_ASSERT(slot);
_sg_clear(slot, sizeof(_sg_slot_t));
}
_SOKOL_PRIVATE void _sg_reset_buffer_to_alloc_state(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
_sg_slot_t slot = buf->slot;
_sg_clear(buf, sizeof(*buf));
buf->slot = slot;
buf->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_reset_image_to_alloc_state(_sg_image_t* img) {
SOKOL_ASSERT(img);
_sg_slot_t slot = img->slot;
_sg_clear(img, sizeof(*img));
img->slot = slot;
img->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_reset_sampler_to_alloc_state(_sg_sampler_t* smp) {
SOKOL_ASSERT(smp);
_sg_slot_t slot = smp->slot;
_sg_clear(smp, sizeof(*smp));
smp->slot = slot;
smp->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_reset_shader_to_alloc_state(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
_sg_slot_t slot = shd->slot;
_sg_clear(shd, sizeof(*shd));
shd->slot = slot;
shd->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_reset_pipeline_to_alloc_state(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
_sg_slot_t slot = pip->slot;
_sg_clear(pip, sizeof(*pip));
pip->slot = slot;
pip->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_reset_attachments_to_alloc_state(_sg_attachments_t* atts) {
SOKOL_ASSERT(atts);
_sg_slot_t slot = atts->slot;
_sg_clear(atts, sizeof(*atts));
atts->slot = slot;
atts->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_setup_pools(_sg_pools_t* p, const sg_desc* desc) {
SOKOL_ASSERT(p);
SOKOL_ASSERT(desc);
// note: the pools here will have an additional item, since slot 0 is reserved
SOKOL_ASSERT((desc->buffer_pool_size > 0) && (desc->buffer_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->buffer_pool, desc->buffer_pool_size);
size_t buffer_pool_byte_size = sizeof(_sg_buffer_t) * (size_t)p->buffer_pool.size;
p->buffers = (_sg_buffer_t*) _sg_malloc_clear(buffer_pool_byte_size);
SOKOL_ASSERT((desc->image_pool_size > 0) && (desc->image_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->image_pool, desc->image_pool_size);
size_t image_pool_byte_size = sizeof(_sg_image_t) * (size_t)p->image_pool.size;
p->images = (_sg_image_t*) _sg_malloc_clear(image_pool_byte_size);
SOKOL_ASSERT((desc->sampler_pool_size > 0) && (desc->sampler_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->sampler_pool, desc->sampler_pool_size);
size_t sampler_pool_byte_size = sizeof(_sg_sampler_t) * (size_t)p->sampler_pool.size;
p->samplers = (_sg_sampler_t*) _sg_malloc_clear(sampler_pool_byte_size);
SOKOL_ASSERT((desc->shader_pool_size > 0) && (desc->shader_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->shader_pool, desc->shader_pool_size);
size_t shader_pool_byte_size = sizeof(_sg_shader_t) * (size_t)p->shader_pool.size;
p->shaders = (_sg_shader_t*) _sg_malloc_clear(shader_pool_byte_size);
SOKOL_ASSERT((desc->pipeline_pool_size > 0) && (desc->pipeline_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->pipeline_pool, desc->pipeline_pool_size);
size_t pipeline_pool_byte_size = sizeof(_sg_pipeline_t) * (size_t)p->pipeline_pool.size;
p->pipelines = (_sg_pipeline_t*) _sg_malloc_clear(pipeline_pool_byte_size);
SOKOL_ASSERT((desc->attachments_pool_size > 0) && (desc->attachments_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->attachments_pool, desc->attachments_pool_size);
size_t attachments_pool_byte_size = sizeof(_sg_attachments_t) * (size_t)p->attachments_pool.size;
p->attachments = (_sg_attachments_t*) _sg_malloc_clear(attachments_pool_byte_size);
}
_SOKOL_PRIVATE void _sg_discard_pools(_sg_pools_t* p) {
SOKOL_ASSERT(p);
_sg_free(p->attachments); p->attachments = 0;
_sg_free(p->pipelines); p->pipelines = 0;
_sg_free(p->shaders); p->shaders = 0;
_sg_free(p->samplers); p->samplers = 0;
_sg_free(p->images); p->images = 0;
_sg_free(p->buffers); p->buffers = 0;
_sg_discard_pool(&p->attachments_pool);
_sg_discard_pool(&p->pipeline_pool);
_sg_discard_pool(&p->shader_pool);
_sg_discard_pool(&p->sampler_pool);
_sg_discard_pool(&p->image_pool);
_sg_discard_pool(&p->buffer_pool);
}
/* allocate the slot at slot_index:
- bump the slot's generation counter
- create a resource id from the generation counter and slot index
- set the slot's id to this id
- set the slot's state to ALLOC
- return the resource id
*/
_SOKOL_PRIVATE uint32_t _sg_slot_alloc(_sg_pool_t* pool, _sg_slot_t* slot, int slot_index) {
/* FIXME: add handling for an overflowing generation counter,
for now, just overflow (another option is to disable
the slot)
*/
SOKOL_ASSERT(pool && pool->gen_ctrs);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < pool->size));
SOKOL_ASSERT(slot->id == SG_INVALID_ID);
SOKOL_ASSERT(slot->state == SG_RESOURCESTATE_INITIAL);
uint32_t ctr = ++pool->gen_ctrs[slot_index];
slot->id = (ctr<<_SG_SLOT_SHIFT)|(slot_index & _SG_SLOT_MASK);
slot->state = SG_RESOURCESTATE_ALLOC;
return slot->id;
}
// extract slot index from id
_SOKOL_PRIVATE int _sg_slot_index(uint32_t id) {
int slot_index = (int) (id & _SG_SLOT_MASK);
SOKOL_ASSERT(_SG_INVALID_SLOT_INDEX != slot_index);
return slot_index;
}
// returns pointer to resource by id without matching id check
_SOKOL_PRIVATE _sg_buffer_t* _sg_buffer_at(const _sg_pools_t* p, uint32_t buf_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != buf_id));
int slot_index = _sg_slot_index(buf_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->buffer_pool.size));
return &p->buffers[slot_index];
}
_SOKOL_PRIVATE _sg_image_t* _sg_image_at(const _sg_pools_t* p, uint32_t img_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != img_id));
int slot_index = _sg_slot_index(img_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->image_pool.size));
return &p->images[slot_index];
}
_SOKOL_PRIVATE _sg_sampler_t* _sg_sampler_at(const _sg_pools_t* p, uint32_t smp_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != smp_id));
int slot_index = _sg_slot_index(smp_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->sampler_pool.size));
return &p->samplers[slot_index];
}
_SOKOL_PRIVATE _sg_shader_t* _sg_shader_at(const _sg_pools_t* p, uint32_t shd_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != shd_id));
int slot_index = _sg_slot_index(shd_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->shader_pool.size));
return &p->shaders[slot_index];
}
_SOKOL_PRIVATE _sg_pipeline_t* _sg_pipeline_at(const _sg_pools_t* p, uint32_t pip_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != pip_id));
int slot_index = _sg_slot_index(pip_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->pipeline_pool.size));
return &p->pipelines[slot_index];
}
_SOKOL_PRIVATE _sg_attachments_t* _sg_attachments_at(const _sg_pools_t* p, uint32_t atts_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != atts_id));
int slot_index = _sg_slot_index(atts_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->attachments_pool.size));
return &p->attachments[slot_index];
}
// returns pointer to resource with matching id check, may return 0
_SOKOL_PRIVATE _sg_buffer_t* _sg_lookup_buffer(const _sg_pools_t* p, uint32_t buf_id) {
if (SG_INVALID_ID != buf_id) {
_sg_buffer_t* buf = _sg_buffer_at(p, buf_id);
if (buf->slot.id == buf_id) {
return buf;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_image_t* _sg_lookup_image(const _sg_pools_t* p, uint32_t img_id) {
if (SG_INVALID_ID != img_id) {
_sg_image_t* img = _sg_image_at(p, img_id);
if (img->slot.id == img_id) {
return img;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_sampler_t* _sg_lookup_sampler(const _sg_pools_t* p, uint32_t smp_id) {
if (SG_INVALID_ID != smp_id) {
_sg_sampler_t* smp = _sg_sampler_at(p, smp_id);
if (smp->slot.id == smp_id) {
return smp;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_shader_t* _sg_lookup_shader(const _sg_pools_t* p, uint32_t shd_id) {
SOKOL_ASSERT(p);
if (SG_INVALID_ID != shd_id) {
_sg_shader_t* shd = _sg_shader_at(p, shd_id);
if (shd->slot.id == shd_id) {
return shd;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_pipeline_t* _sg_lookup_pipeline(const _sg_pools_t* p, uint32_t pip_id) {
SOKOL_ASSERT(p);
if (SG_INVALID_ID != pip_id) {
_sg_pipeline_t* pip = _sg_pipeline_at(p, pip_id);
if (pip->slot.id == pip_id) {
return pip;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_attachments_t* _sg_lookup_attachments(const _sg_pools_t* p, uint32_t atts_id) {
SOKOL_ASSERT(p);
if (SG_INVALID_ID != atts_id) {
_sg_attachments_t* atts = _sg_attachments_at(p, atts_id);
if (atts->slot.id == atts_id) {
return atts;
}
}
return 0;
}
_SOKOL_PRIVATE void _sg_discard_all_resources(_sg_pools_t* p) {
/* this is a bit dumb since it loops over all pool slots to
find the occupied slots, on the other hand it is only ever
executed at shutdown
NOTE: ONLY EXECUTE THIS AT SHUTDOWN
...because the free queues will not be reset
and the resource slots not be cleared!
*/
for (int i = 1; i < p->buffer_pool.size; i++) {
sg_resource_state state = p->buffers[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_discard_buffer(&p->buffers[i]);
}
}
for (int i = 1; i < p->image_pool.size; i++) {
sg_resource_state state = p->images[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_discard_image(&p->images[i]);
}
}
for (int i = 1; i < p->sampler_pool.size; i++) {
sg_resource_state state = p->samplers[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_discard_sampler(&p->samplers[i]);
}
}
for (int i = 1; i < p->shader_pool.size; i++) {
sg_resource_state state = p->shaders[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_discard_shader(&p->shaders[i]);
}
}
for (int i = 1; i < p->pipeline_pool.size; i++) {
sg_resource_state state = p->pipelines[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_discard_pipeline(&p->pipelines[i]);
}
}
for (int i = 1; i < p->attachments_pool.size; i++) {
sg_resource_state state = p->attachments[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_discard_attachments(&p->attachments[i]);
}
}
}
// ██ ██ █████ ██ ██ ██████ █████ ████████ ██ ██████ ███ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
// ██ ██ ███████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ████ ██ ██ ███████ ██ ██████ ██ ██ ██ ██ ██████ ██ ████
//
// >>validation
#if defined(SOKOL_DEBUG)
_SOKOL_PRIVATE void _sg_validate_begin(void) {
_sg.validate_error = SG_LOGITEM_OK;
}
_SOKOL_PRIVATE bool _sg_validate_end(void) {
if (_sg.validate_error != SG_LOGITEM_OK) {
#if !defined(SOKOL_VALIDATE_NON_FATAL)
_SG_PANIC(VALIDATION_FAILED);
return false;
#else
return false;
#endif
} else {
return true;
}
}
#endif
_SOKOL_PRIVATE bool _sg_validate_buffer_desc(const sg_buffer_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
SOKOL_ASSERT(desc);
_sg_validate_begin();
_SG_VALIDATE(desc->_start_canary == 0, VALIDATE_BUFFERDESC_CANARY);
_SG_VALIDATE(desc->_end_canary == 0, VALIDATE_BUFFERDESC_CANARY);
_SG_VALIDATE(desc->size > 0, VALIDATE_BUFFERDESC_SIZE);
bool injected = (0 != desc->gl_buffers[0]) ||
(0 != desc->mtl_buffers[0]) ||
(0 != desc->d3d11_buffer) ||
(0 != desc->wgpu_buffer);
if (!injected && (desc->usage == SG_USAGE_IMMUTABLE)) {
_SG_VALIDATE((0 != desc->data.ptr) && (desc->data.size > 0), VALIDATE_BUFFERDESC_DATA);
_SG_VALIDATE(desc->size == desc->data.size, VALIDATE_BUFFERDESC_DATA_SIZE);
} else {
_SG_VALIDATE(0 == desc->data.ptr, VALIDATE_BUFFERDESC_NO_DATA);
}
if (desc->type == SG_BUFFERTYPE_STORAGEBUFFER) {
_SG_VALIDATE(_sg.features.storage_buffer, VALIDATE_BUFFERDESC_STORAGEBUFFER_SUPPORTED);
_SG_VALIDATE(_sg_multiple_u64(desc->size, 4), VALIDATE_BUFFERDESC_STORAGEBUFFER_SIZE_MULTIPLE_4);
}
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE void _sg_validate_image_data(const sg_image_data* data, sg_pixel_format fmt, int width, int height, int num_faces, int num_mips, int num_slices) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(data);
_SOKOL_UNUSED(fmt);
_SOKOL_UNUSED(width);
_SOKOL_UNUSED(height);
_SOKOL_UNUSED(num_faces);
_SOKOL_UNUSED(num_mips);
_SOKOL_UNUSED(num_slices);
#else
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int mip_index = 0; mip_index < num_mips; mip_index++) {
const bool has_data = data->subimage[face_index][mip_index].ptr != 0;
const bool has_size = data->subimage[face_index][mip_index].size > 0;
_SG_VALIDATE(has_data && has_size, VALIDATE_IMAGEDATA_NODATA);
const int mip_width = _sg_miplevel_dim(width, mip_index);
const int mip_height = _sg_miplevel_dim(height, mip_index);
const int bytes_per_slice = _sg_surface_pitch(fmt, mip_width, mip_height, 1);
const int expected_size = bytes_per_slice * num_slices;
_SG_VALIDATE(expected_size == (int)data->subimage[face_index][mip_index].size, VALIDATE_IMAGEDATA_DATA_SIZE);
}
}
#endif
}
_SOKOL_PRIVATE bool _sg_validate_image_desc(const sg_image_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
SOKOL_ASSERT(desc);
_sg_validate_begin();
_SG_VALIDATE(desc->_start_canary == 0, VALIDATE_IMAGEDESC_CANARY);
_SG_VALIDATE(desc->_end_canary == 0, VALIDATE_IMAGEDESC_CANARY);
_SG_VALIDATE(desc->width > 0, VALIDATE_IMAGEDESC_WIDTH);
_SG_VALIDATE(desc->height > 0, VALIDATE_IMAGEDESC_HEIGHT);
const sg_pixel_format fmt = desc->pixel_format;
const sg_usage usage = desc->usage;
const bool injected = (0 != desc->gl_textures[0]) ||
(0 != desc->mtl_textures[0]) ||
(0 != desc->d3d11_texture) ||
(0 != desc->wgpu_texture);
if (_sg_is_depth_or_depth_stencil_format(fmt)) {
_SG_VALIDATE(desc->type != SG_IMAGETYPE_3D, VALIDATE_IMAGEDESC_DEPTH_3D_IMAGE);
}
if (desc->render_target) {
SOKOL_ASSERT(((int)fmt >= 0) && ((int)fmt < _SG_PIXELFORMAT_NUM));
_SG_VALIDATE(_sg.formats[fmt].render, VALIDATE_IMAGEDESC_RT_PIXELFORMAT);
_SG_VALIDATE(usage == SG_USAGE_IMMUTABLE, VALIDATE_IMAGEDESC_RT_IMMUTABLE);
_SG_VALIDATE(desc->data.subimage[0][0].ptr==0, VALIDATE_IMAGEDESC_RT_NO_DATA);
if (desc->sample_count > 1) {
_SG_VALIDATE(_sg.formats[fmt].msaa, VALIDATE_IMAGEDESC_NO_MSAA_RT_SUPPORT);
_SG_VALIDATE(desc->num_mipmaps == 1, VALIDATE_IMAGEDESC_MSAA_NUM_MIPMAPS);
_SG_VALIDATE(desc->type != SG_IMAGETYPE_3D, VALIDATE_IMAGEDESC_MSAA_3D_IMAGE);
}
} else {
_SG_VALIDATE(desc->sample_count == 1, VALIDATE_IMAGEDESC_MSAA_BUT_NO_RT);
const bool valid_nonrt_fmt = !_sg_is_valid_rendertarget_depth_format(fmt);
_SG_VALIDATE(valid_nonrt_fmt, VALIDATE_IMAGEDESC_NONRT_PIXELFORMAT);
const bool is_compressed = _sg_is_compressed_pixel_format(desc->pixel_format);
const bool is_immutable = (usage == SG_USAGE_IMMUTABLE);
if (is_compressed) {
_SG_VALIDATE(is_immutable, VALIDATE_IMAGEDESC_COMPRESSED_IMMUTABLE);
}
if (!injected && is_immutable) {
// image desc must have valid data
_sg_validate_image_data(&desc->data,
desc->pixel_format,
desc->width,
desc->height,
(desc->type == SG_IMAGETYPE_CUBE) ? 6 : 1,
desc->num_mipmaps,
desc->num_slices);
} else {
// image desc must not have data
for (int face_index = 0; face_index < SG_CUBEFACE_NUM; face_index++) {
for (int mip_index = 0; mip_index < SG_MAX_MIPMAPS; mip_index++) {
const bool no_data = 0 == desc->data.subimage[face_index][mip_index].ptr;
const bool no_size = 0 == desc->data.subimage[face_index][mip_index].size;
if (injected) {
_SG_VALIDATE(no_data && no_size, VALIDATE_IMAGEDESC_INJECTED_NO_DATA);
}
if (!is_immutable) {
_SG_VALIDATE(no_data && no_size, VALIDATE_IMAGEDESC_DYNAMIC_NO_DATA);
}
}
}
}
}
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_sampler_desc(const sg_sampler_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
SOKOL_ASSERT(desc);
_sg_validate_begin();
_SG_VALIDATE(desc->_start_canary == 0, VALIDATE_SAMPLERDESC_CANARY);
_SG_VALIDATE(desc->_end_canary == 0, VALIDATE_SAMPLERDESC_CANARY);
// restriction from WebGPU: when anisotropy > 1, all filters must be linear
if (desc->max_anisotropy > 1) {
_SG_VALIDATE((desc->min_filter == SG_FILTER_LINEAR)
&& (desc->mag_filter == SG_FILTER_LINEAR)
&& (desc->mipmap_filter == SG_FILTER_LINEAR),
VALIDATE_SAMPLERDESC_ANISTROPIC_REQUIRES_LINEAR_FILTERING);
}
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_shader_desc(const sg_shader_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
SOKOL_ASSERT(desc);
_sg_validate_begin();
_SG_VALIDATE(desc->_start_canary == 0, VALIDATE_SHADERDESC_CANARY);
_SG_VALIDATE(desc->_end_canary == 0, VALIDATE_SHADERDESC_CANARY);
#if defined(SOKOL_GLCORE) || defined(SOKOL_GLES3) || defined(SOKOL_WGPU)
// on GL or WebGPU, must provide shader source code
_SG_VALIDATE(0 != desc->vs.source, VALIDATE_SHADERDESC_SOURCE);
_SG_VALIDATE(0 != desc->fs.source, VALIDATE_SHADERDESC_SOURCE);
#elif defined(SOKOL_METAL) || defined(SOKOL_D3D11)
// on Metal or D3D11, must provide shader source code or byte code
_SG_VALIDATE((0 != desc->vs.source)||(0 != desc->vs.bytecode.ptr), VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE);
_SG_VALIDATE((0 != desc->fs.source)||(0 != desc->fs.bytecode.ptr), VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE);
#else
// Dummy Backend, don't require source or bytecode
#endif
for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) {
if (desc->attrs[i].name) {
_SG_VALIDATE(strlen(desc->attrs[i].name) < _SG_STRING_SIZE, VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG);
}
if (desc->attrs[i].sem_name) {
_SG_VALIDATE(strlen(desc->attrs[i].sem_name) < _SG_STRING_SIZE, VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG);
}
}
// if shader byte code, the size must also be provided
if (0 != desc->vs.bytecode.ptr) {
_SG_VALIDATE(desc->vs.bytecode.size > 0, VALIDATE_SHADERDESC_NO_BYTECODE_SIZE);
}
if (0 != desc->fs.bytecode.ptr) {
_SG_VALIDATE(desc->fs.bytecode.size > 0, VALIDATE_SHADERDESC_NO_BYTECODE_SIZE);
}
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const sg_shader_stage_desc* stage_desc = (stage_index == 0)? &desc->vs : &desc->fs;
bool uniform_blocks_continuous = true;
for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) {
const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index];
if (ub_desc->size > 0) {
_SG_VALIDATE(uniform_blocks_continuous, VALIDATE_SHADERDESC_NO_CONT_UBS);
#if defined(_SOKOL_ANY_GL)
bool uniforms_continuous = true;
uint32_t uniform_offset = 0;
int num_uniforms = 0;
for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) {
const sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index];
if (u_desc->type != SG_UNIFORMTYPE_INVALID) {
_SG_VALIDATE(uniforms_continuous, VALIDATE_SHADERDESC_NO_CONT_UB_MEMBERS);
#if defined(SOKOL_GLES3)
_SG_VALIDATE(0 != u_desc->name, VALIDATE_SHADERDESC_UB_MEMBER_NAME);
#endif
const int array_count = u_desc->array_count;
_SG_VALIDATE(array_count > 0, VALIDATE_SHADERDESC_UB_ARRAY_COUNT);
const uint32_t u_align = _sg_uniform_alignment(u_desc->type, array_count, ub_desc->layout);
const uint32_t u_size = _sg_uniform_size(u_desc->type, array_count, ub_desc->layout);
uniform_offset = _sg_align_u32(uniform_offset, u_align);
uniform_offset += u_size;
num_uniforms++;
// with std140, arrays are only allowed for FLOAT4, INT4, MAT4
if (ub_desc->layout == SG_UNIFORMLAYOUT_STD140) {
if (array_count > 1) {
_SG_VALIDATE((u_desc->type == SG_UNIFORMTYPE_FLOAT4) || (u_desc->type == SG_UNIFORMTYPE_INT4) || (u_desc->type == SG_UNIFORMTYPE_MAT4), VALIDATE_SHADERDESC_UB_STD140_ARRAY_TYPE);
}
}
} else {
uniforms_continuous = false;
}
}
if (ub_desc->layout == SG_UNIFORMLAYOUT_STD140) {
uniform_offset = _sg_align_u32(uniform_offset, 16);
}
_SG_VALIDATE((size_t)uniform_offset == ub_desc->size, VALIDATE_SHADERDESC_UB_SIZE_MISMATCH);
_SG_VALIDATE(num_uniforms > 0, VALIDATE_SHADERDESC_NO_UB_MEMBERS);
#endif
} else {
uniform_blocks_continuous = false;
}
}
bool storage_buffers_continuous = true;
for (int sbuf_index = 0; sbuf_index < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; sbuf_index++) {
const sg_shader_storage_buffer_desc* sbuf_desc = &stage_desc->storage_buffers[sbuf_index];
if (sbuf_desc->used) {
_SG_VALIDATE(storage_buffers_continuous, VALIDATE_SHADERDESC_NO_CONT_STORAGEBUFFERS);
_SG_VALIDATE(sbuf_desc->readonly, VALIDATE_SHADERDESC_STORAGEBUFFER_READONLY);
} else {
storage_buffers_continuous = false;
}
}
bool images_continuous = true;
int num_images = 0;
for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) {
const sg_shader_image_desc* img_desc = &stage_desc->images[img_index];
if (img_desc->used) {
_SG_VALIDATE(images_continuous, VALIDATE_SHADERDESC_NO_CONT_IMAGES);
num_images++;
} else {
images_continuous = false;
}
}
bool samplers_continuous = true;
int num_samplers = 0;
for (int smp_index = 0; smp_index < SG_MAX_SHADERSTAGE_SAMPLERS; smp_index++) {
const sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[smp_index];
if (smp_desc->used) {
_SG_VALIDATE(samplers_continuous, VALIDATE_SHADERDESC_NO_CONT_SAMPLERS);
num_samplers++;
} else {
samplers_continuous = false;
}
}
bool image_samplers_continuous = true;
int num_image_samplers = 0;
for (int img_smp_index = 0; img_smp_index < SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS; img_smp_index++) {
const sg_shader_image_sampler_pair_desc* img_smp_desc = &stage_desc->image_sampler_pairs[img_smp_index];
if (img_smp_desc->used) {
_SG_VALIDATE(image_samplers_continuous, VALIDATE_SHADERDESC_NO_CONT_IMAGE_SAMPLER_PAIRS);
num_image_samplers++;
const bool img_slot_in_range = (img_smp_desc->image_slot >= 0) && (img_smp_desc->image_slot < SG_MAX_SHADERSTAGE_IMAGES);
const bool smp_slot_in_range = (img_smp_desc->sampler_slot >= 0) && (img_smp_desc->sampler_slot < SG_MAX_SHADERSTAGE_SAMPLERS);
_SG_VALIDATE(img_slot_in_range && (img_smp_desc->image_slot < num_images), VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_IMAGE_SLOT_OUT_OF_RANGE);
_SG_VALIDATE(smp_slot_in_range && (img_smp_desc->sampler_slot < num_samplers), VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_IMAGE_SLOT_OUT_OF_RANGE);
#if defined(_SOKOL_ANY_GL)
_SG_VALIDATE(img_smp_desc->glsl_name != 0, VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_NAME_REQUIRED_FOR_GL);
#endif
if (img_slot_in_range && smp_slot_in_range) {
const sg_shader_image_desc* img_desc = &stage_desc->images[img_smp_desc->image_slot];
const sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[img_smp_desc->sampler_slot];
const bool needs_nonfiltering = (img_desc->sample_type == SG_IMAGESAMPLETYPE_UINT)
|| (img_desc->sample_type == SG_IMAGESAMPLETYPE_SINT)
|| (img_desc->sample_type == SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT);
const bool needs_comparison = img_desc->sample_type == SG_IMAGESAMPLETYPE_DEPTH;
if (needs_nonfiltering) {
_SG_VALIDATE(needs_nonfiltering && (smp_desc->sampler_type == SG_SAMPLERTYPE_NONFILTERING), VALIDATE_SHADERDESC_NONFILTERING_SAMPLER_REQUIRED);
}
if (needs_comparison) {
_SG_VALIDATE(needs_comparison && (smp_desc->sampler_type == SG_SAMPLERTYPE_COMPARISON), VALIDATE_SHADERDESC_COMPARISON_SAMPLER_REQUIRED);
}
}
} else {
_SG_VALIDATE(img_smp_desc->glsl_name == 0, VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_NAME_BUT_NOT_USED);
_SG_VALIDATE(img_smp_desc->image_slot == 0, VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_IMAGE_BUT_NOT_USED);
_SG_VALIDATE(img_smp_desc->sampler_slot == 0, VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_SAMPLER_BUT_NOT_USED);
image_samplers_continuous = false;
}
}
// each image and sampler must be referenced by an image sampler
const uint32_t expected_img_slot_mask = (uint32_t)((1 << num_images) - 1);
const uint32_t expected_smp_slot_mask = (uint32_t)((1 << num_samplers) - 1);
uint32_t actual_img_slot_mask = 0;
uint32_t actual_smp_slot_mask = 0;
for (int img_smp_index = 0; img_smp_index < num_image_samplers; img_smp_index++) {
const sg_shader_image_sampler_pair_desc* img_smp_desc = &stage_desc->image_sampler_pairs[img_smp_index];
actual_img_slot_mask |= (1 << ((uint32_t)img_smp_desc->image_slot & 31));
actual_smp_slot_mask |= (1 << ((uint32_t)img_smp_desc->sampler_slot & 31));
}
_SG_VALIDATE(expected_img_slot_mask == actual_img_slot_mask, VALIDATE_SHADERDESC_IMAGE_NOT_REFERENCED_BY_IMAGE_SAMPLER_PAIRS);
_SG_VALIDATE(expected_smp_slot_mask == actual_smp_slot_mask, VALIDATE_SHADERDESC_SAMPLER_NOT_REFERENCED_BY_IMAGE_SAMPLER_PAIRS);
}
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_pipeline_desc(const sg_pipeline_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
SOKOL_ASSERT(desc);
_sg_validate_begin();
_SG_VALIDATE(desc->_start_canary == 0, VALIDATE_PIPELINEDESC_CANARY);
_SG_VALIDATE(desc->_end_canary == 0, VALIDATE_PIPELINEDESC_CANARY);
_SG_VALIDATE(desc->shader.id != SG_INVALID_ID, VALIDATE_PIPELINEDESC_SHADER);
for (int buf_index = 0; buf_index < SG_MAX_VERTEX_BUFFERS; buf_index++) {
const sg_vertex_buffer_layout_state* l_state = &desc->layout.buffers[buf_index];
if (l_state->stride == 0) {
continue;
}
_SG_VALIDATE(_sg_multiple_u64((uint64_t)l_state->stride, 4), VALIDATE_PIPELINEDESC_LAYOUT_STRIDE4);
}
const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, desc->shader.id);
_SG_VALIDATE(0 != shd, VALIDATE_PIPELINEDESC_SHADER);
if (shd) {
_SG_VALIDATE(shd->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_PIPELINEDESC_SHADER);
bool attrs_cont = true;
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
const sg_vertex_attr_state* a_state = &desc->layout.attrs[attr_index];
if (a_state->format == SG_VERTEXFORMAT_INVALID) {
attrs_cont = false;
continue;
}
_SG_VALIDATE(attrs_cont, VALIDATE_PIPELINEDESC_NO_CONT_ATTRS);
SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS);
#if defined(SOKOL_D3D11)
// on D3D11, semantic names (and semantic indices) must be provided
_SG_VALIDATE(!_sg_strempty(&shd->d3d11.attrs[attr_index].sem_name), VALIDATE_PIPELINEDESC_ATTR_SEMANTICS);
#endif
}
}
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_attachments_desc(const sg_attachments_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
SOKOL_ASSERT(desc);
_sg_validate_begin();
_SG_VALIDATE(desc->_start_canary == 0, VALIDATE_ATTACHMENTSDESC_CANARY);
_SG_VALIDATE(desc->_end_canary == 0, VALIDATE_ATTACHMENTSDESC_CANARY);
bool atts_cont = true;
int color_width = -1, color_height = -1, color_sample_count = -1;
bool has_color_atts = false;
for (int att_index = 0; att_index < SG_MAX_COLOR_ATTACHMENTS; att_index++) {
const sg_attachment_desc* att = &desc->colors[att_index];
if (att->image.id == SG_INVALID_ID) {
atts_cont = false;
continue;
}
_SG_VALIDATE(atts_cont, VALIDATE_ATTACHMENTSDESC_NO_CONT_COLOR_ATTS);
has_color_atts = true;
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, att->image.id);
_SG_VALIDATE(img, VALIDATE_ATTACHMENTSDESC_IMAGE);
if (0 != img) {
_SG_VALIDATE(img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_ATTACHMENTSDESC_IMAGE);
_SG_VALIDATE(img->cmn.render_target, VALIDATE_ATTACHMENTSDESC_IMAGE_NO_RT);
_SG_VALIDATE(att->mip_level < img->cmn.num_mipmaps, VALIDATE_ATTACHMENTSDESC_MIPLEVEL);
if (img->cmn.type == SG_IMAGETYPE_CUBE) {
_SG_VALIDATE(att->slice < 6, VALIDATE_ATTACHMENTSDESC_FACE);
} else if (img->cmn.type == SG_IMAGETYPE_ARRAY) {
_SG_VALIDATE(att->slice < img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_LAYER);
} else if (img->cmn.type == SG_IMAGETYPE_3D) {
_SG_VALIDATE(att->slice < img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_SLICE);
}
if (att_index == 0) {
color_width = _sg_miplevel_dim(img->cmn.width, att->mip_level);
color_height = _sg_miplevel_dim(img->cmn.height, att->mip_level);
color_sample_count = img->cmn.sample_count;
} else {
_SG_VALIDATE(color_width == _sg_miplevel_dim(img->cmn.width, att->mip_level), VALIDATE_ATTACHMENTSDESC_IMAGE_SIZES);
_SG_VALIDATE(color_height == _sg_miplevel_dim(img->cmn.height, att->mip_level), VALIDATE_ATTACHMENTSDESC_IMAGE_SIZES);
_SG_VALIDATE(color_sample_count == img->cmn.sample_count, VALIDATE_ATTACHMENTSDESC_IMAGE_SAMPLE_COUNTS);
}
_SG_VALIDATE(_sg_is_valid_rendertarget_color_format(img->cmn.pixel_format), VALIDATE_ATTACHMENTSDESC_COLOR_INV_PIXELFORMAT);
// check resolve attachment
const sg_attachment_desc* res_att = &desc->resolves[att_index];
if (res_att->image.id != SG_INVALID_ID) {
// associated color attachment must be MSAA
_SG_VALIDATE(img->cmn.sample_count > 1, VALIDATE_ATTACHMENTSDESC_RESOLVE_COLOR_IMAGE_MSAA);
const _sg_image_t* res_img = _sg_lookup_image(&_sg.pools, res_att->image.id);
_SG_VALIDATE(res_img, VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE);
if (res_img != 0) {
_SG_VALIDATE(res_img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE);
_SG_VALIDATE(res_img->cmn.render_target, VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_NO_RT);
_SG_VALIDATE(res_img->cmn.sample_count == 1, VALIDATE_ATTACHMENTSDESC_RESOLVE_SAMPLE_COUNT);
_SG_VALIDATE(res_att->mip_level < res_img->cmn.num_mipmaps, VALIDATE_ATTACHMENTSDESC_RESOLVE_MIPLEVEL);
if (res_img->cmn.type == SG_IMAGETYPE_CUBE) {
_SG_VALIDATE(res_att->slice < 6, VALIDATE_ATTACHMENTSDESC_RESOLVE_FACE);
} else if (res_img->cmn.type == SG_IMAGETYPE_ARRAY) {
_SG_VALIDATE(res_att->slice < res_img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_RESOLVE_LAYER);
} else if (res_img->cmn.type == SG_IMAGETYPE_3D) {
_SG_VALIDATE(res_att->slice < res_img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_RESOLVE_SLICE);
}
_SG_VALIDATE(img->cmn.pixel_format == res_img->cmn.pixel_format, VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_FORMAT);
_SG_VALIDATE(color_width == _sg_miplevel_dim(res_img->cmn.width, res_att->mip_level), VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_SIZES);
_SG_VALIDATE(color_height == _sg_miplevel_dim(res_img->cmn.height, res_att->mip_level), VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_SIZES);
}
}
}
}
bool has_depth_stencil_att = false;
if (desc->depth_stencil.image.id != SG_INVALID_ID) {
const sg_attachment_desc* att = &desc->depth_stencil;
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, att->image.id);
_SG_VALIDATE(img, VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE);
has_depth_stencil_att = true;
if (img) {
_SG_VALIDATE(img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE);
_SG_VALIDATE(att->mip_level < img->cmn.num_mipmaps, VALIDATE_ATTACHMENTSDESC_DEPTH_MIPLEVEL);
if (img->cmn.type == SG_IMAGETYPE_CUBE) {
_SG_VALIDATE(att->slice < 6, VALIDATE_ATTACHMENTSDESC_DEPTH_FACE);
} else if (img->cmn.type == SG_IMAGETYPE_ARRAY) {
_SG_VALIDATE(att->slice < img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_DEPTH_LAYER);
} else if (img->cmn.type == SG_IMAGETYPE_3D) {
// NOTE: this can't actually happen because of VALIDATE_IMAGEDESC_DEPTH_3D_IMAGE
_SG_VALIDATE(att->slice < img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_DEPTH_SLICE);
}
_SG_VALIDATE(img->cmn.render_target, VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_NO_RT);
_SG_VALIDATE((color_width == -1) || (color_width == _sg_miplevel_dim(img->cmn.width, att->mip_level)), VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_SIZES);
_SG_VALIDATE((color_height == -1) || (color_height == _sg_miplevel_dim(img->cmn.height, att->mip_level)), VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_SIZES);
_SG_VALIDATE((color_sample_count == -1) || (color_sample_count == img->cmn.sample_count), VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_SAMPLE_COUNT);
_SG_VALIDATE(_sg_is_valid_rendertarget_depth_format(img->cmn.pixel_format), VALIDATE_ATTACHMENTSDESC_DEPTH_INV_PIXELFORMAT);
}
}
_SG_VALIDATE(has_color_atts || has_depth_stencil_att, VALIDATE_ATTACHMENTSDESC_NO_ATTACHMENTS);
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_begin_pass(const sg_pass* pass) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(pass);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
_sg_validate_begin();
_SG_VALIDATE(pass->_start_canary == 0, VALIDATE_BEGINPASS_CANARY);
_SG_VALIDATE(pass->_end_canary == 0, VALIDATE_BEGINPASS_CANARY);
if (pass->attachments.id == SG_INVALID_ID) {
// this is a swapchain pass
_SG_VALIDATE(pass->swapchain.width > 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_WIDTH);
_SG_VALIDATE(pass->swapchain.height > 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_HEIGHT);
_SG_VALIDATE(pass->swapchain.sample_count > 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_SAMPLECOUNT);
_SG_VALIDATE(pass->swapchain.color_format > SG_PIXELFORMAT_NONE, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_COLORFORMAT);
// NOTE: depth buffer is optional, so depth_format is allowed to be invalid
// NOTE: the GL framebuffer handle may actually be 0
#if defined(SOKOL_METAL)
_SG_VALIDATE(pass->swapchain.metal.current_drawable != 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_CURRENTDRAWABLE);
if (pass->swapchain.depth_format == SG_PIXELFORMAT_NONE) {
_SG_VALIDATE(pass->swapchain.metal.depth_stencil_texture == 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_DEPTHSTENCILTEXTURE_NOTSET);
} else {
_SG_VALIDATE(pass->swapchain.metal.depth_stencil_texture != 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_DEPTHSTENCILTEXTURE);
}
if (pass->swapchain.sample_count > 1) {
_SG_VALIDATE(pass->swapchain.metal.msaa_color_texture != 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_MSAACOLORTEXTURE);
} else {
_SG_VALIDATE(pass->swapchain.metal.msaa_color_texture == 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_MSAACOLORTEXTURE_NOTSET);
}
#elif defined(SOKOL_D3D11)
_SG_VALIDATE(pass->swapchain.d3d11.render_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RENDERVIEW);
if (pass->swapchain.depth_format == SG_PIXELFORMAT_NONE) {
_SG_VALIDATE(pass->swapchain.d3d11.depth_stencil_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_DEPTHSTENCILVIEW_NOTSET);
} else {
_SG_VALIDATE(pass->swapchain.d3d11.depth_stencil_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_DEPTHSTENCILVIEW);
}
if (pass->swapchain.sample_count > 1) {
_SG_VALIDATE(pass->swapchain.d3d11.resolve_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RESOLVEVIEW);
} else {
_SG_VALIDATE(pass->swapchain.d3d11.resolve_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RESOLVEVIEW_NOTSET);
}
#elif defined(SOKOL_WGPU)
_SG_VALIDATE(pass->swapchain.wgpu.render_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RENDERVIEW);
if (pass->swapchain.depth_format == SG_PIXELFORMAT_NONE) {
_SG_VALIDATE(pass->swapchain.wgpu.depth_stencil_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_DEPTHSTENCILVIEW_NOTSET);
} else {
_SG_VALIDATE(pass->swapchain.wgpu.depth_stencil_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_DEPTHSTENCILVIEW);
}
if (pass->swapchain.sample_count > 1) {
_SG_VALIDATE(pass->swapchain.wgpu.resolve_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RESOLVEVIEW);
} else {
_SG_VALIDATE(pass->swapchain.wgpu.resolve_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RESOLVEVIEW_NOTSET);
}
#endif
} else {
// this is an 'offscreen pass'
const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, pass->attachments.id);
if (atts) {
_SG_VALIDATE(atts->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_BEGINPASS_ATTACHMENTS_VALID);
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
const _sg_attachment_common_t* color_att = &atts->cmn.colors[i];
const _sg_image_t* color_img = _sg_attachments_color_image(atts, i);
if (color_img) {
_SG_VALIDATE(color_img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_BEGINPASS_COLOR_ATTACHMENT_IMAGE);
_SG_VALIDATE(color_img->slot.id == color_att->image_id.id, VALIDATE_BEGINPASS_COLOR_ATTACHMENT_IMAGE);
}
const _sg_attachment_common_t* resolve_att = &atts->cmn.resolves[i];
const _sg_image_t* resolve_img = _sg_attachments_resolve_image(atts, i);
if (resolve_img) {
_SG_VALIDATE(resolve_img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_BEGINPASS_RESOLVE_ATTACHMENT_IMAGE);
_SG_VALIDATE(resolve_img->slot.id == resolve_att->image_id.id, VALIDATE_BEGINPASS_RESOLVE_ATTACHMENT_IMAGE);
}
}
const _sg_image_t* ds_img = _sg_attachments_ds_image(atts);
if (ds_img) {
const _sg_attachment_common_t* att = &atts->cmn.depth_stencil;
_SG_VALIDATE(ds_img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_BEGINPASS_DEPTHSTENCIL_ATTACHMENT_IMAGE);
_SG_VALIDATE(ds_img->slot.id == att->image_id.id, VALIDATE_BEGINPASS_DEPTHSTENCIL_ATTACHMENT_IMAGE);
}
} else {
_SG_VALIDATE(atts != 0, VALIDATE_BEGINPASS_ATTACHMENTS_EXISTS);
}
// swapchain params must be all zero!
_SG_VALIDATE(pass->swapchain.width == 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_WIDTH_NOTSET);
_SG_VALIDATE(pass->swapchain.height == 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_HEIGHT_NOTSET);
_SG_VALIDATE(pass->swapchain.sample_count == 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_SAMPLECOUNT_NOTSET);
_SG_VALIDATE(pass->swapchain.color_format == _SG_PIXELFORMAT_DEFAULT, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_COLORFORMAT_NOTSET);
_SG_VALIDATE(pass->swapchain.depth_format == _SG_PIXELFORMAT_DEFAULT, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_DEPTHFORMAT_NOTSET);
#if defined(SOKOL_METAL)
_SG_VALIDATE(pass->swapchain.metal.current_drawable == 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_CURRENTDRAWABLE_NOTSET);
_SG_VALIDATE(pass->swapchain.metal.depth_stencil_texture == 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_DEPTHSTENCILTEXTURE_NOTSET);
_SG_VALIDATE(pass->swapchain.metal.msaa_color_texture == 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_MSAACOLORTEXTURE_NOTSET);
#elif defined(SOKOL_D3D11)
_SG_VALIDATE(pass->swapchain.d3d11.render_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RENDERVIEW_NOTSET);
_SG_VALIDATE(pass->swapchain.d3d11.depth_stencil_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_DEPTHSTENCILVIEW_NOTSET);
_SG_VALIDATE(pass->swapchain.d3d11.resolve_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RESOLVEVIEW_NOTSET);
#elif defined(SOKOL_WGPU)
_SG_VALIDATE(pass->swapchain.wgpu.render_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RENDERVIEW_NOTSET);
_SG_VALIDATE(pass->swapchain.wgpu.depth_stencil_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_DEPTHSTENCILVIEW_NOTSET);
_SG_VALIDATE(pass->swapchain.wgpu.resolve_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RESOLVEVIEW_NOTSET);
#elif defined(_SOKOL_ANY_GL)
_SG_VALIDATE(pass->swapchain.gl.framebuffer == 0, VALIDATE_BEGINPASS_SWAPCHAIN_GL_EXPECT_FRAMEBUFFER_NOTSET);
#endif
}
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_apply_pipeline(sg_pipeline pip_id) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(pip_id);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
_sg_validate_begin();
// the pipeline object must be alive and valid
_SG_VALIDATE(pip_id.id != SG_INVALID_ID, VALIDATE_APIP_PIPELINE_VALID_ID);
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
_SG_VALIDATE(pip != 0, VALIDATE_APIP_PIPELINE_EXISTS);
if (!pip) {
return _sg_validate_end();
}
_SG_VALIDATE(pip->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_APIP_PIPELINE_VALID);
// the pipeline's shader must be alive and valid
SOKOL_ASSERT(pip->shader);
_SG_VALIDATE(pip->shader->slot.id == pip->cmn.shader_id.id, VALIDATE_APIP_SHADER_EXISTS);
_SG_VALIDATE(pip->shader->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_APIP_SHADER_VALID);
// check that pipeline attributes match current pass attributes
if (_sg.cur_pass.atts_id.id != SG_INVALID_ID) {
// an offscreen pass
const _sg_attachments_t* atts = _sg.cur_pass.atts;
SOKOL_ASSERT(atts);
_SG_VALIDATE(atts->slot.id == _sg.cur_pass.atts_id.id, VALIDATE_APIP_CURPASS_ATTACHMENTS_EXISTS);
_SG_VALIDATE(atts->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_APIP_CURPASS_ATTACHMENTS_VALID);
_SG_VALIDATE(pip->cmn.color_count == atts->cmn.num_colors, VALIDATE_APIP_ATT_COUNT);
for (int i = 0; i < pip->cmn.color_count; i++) {
const _sg_image_t* att_img = _sg_attachments_color_image(atts, i);
_SG_VALIDATE(pip->cmn.colors[i].pixel_format == att_img->cmn.pixel_format, VALIDATE_APIP_COLOR_FORMAT);
_SG_VALIDATE(pip->cmn.sample_count == att_img->cmn.sample_count, VALIDATE_APIP_SAMPLE_COUNT);
}
const _sg_image_t* att_dsimg = _sg_attachments_ds_image(atts);
if (att_dsimg) {
_SG_VALIDATE(pip->cmn.depth.pixel_format == att_dsimg->cmn.pixel_format, VALIDATE_APIP_DEPTH_FORMAT);
} else {
_SG_VALIDATE(pip->cmn.depth.pixel_format == SG_PIXELFORMAT_NONE, VALIDATE_APIP_DEPTH_FORMAT);
}
} else {
// default pass
_SG_VALIDATE(pip->cmn.color_count == 1, VALIDATE_APIP_ATT_COUNT);
_SG_VALIDATE(pip->cmn.colors[0].pixel_format == _sg.cur_pass.swapchain.color_fmt, VALIDATE_APIP_COLOR_FORMAT);
_SG_VALIDATE(pip->cmn.depth.pixel_format == _sg.cur_pass.swapchain.depth_fmt, VALIDATE_APIP_DEPTH_FORMAT);
_SG_VALIDATE(pip->cmn.sample_count == _sg.cur_pass.swapchain.sample_count, VALIDATE_APIP_SAMPLE_COUNT);
}
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_apply_bindings(const sg_bindings* bindings) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(bindings);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
_sg_validate_begin();
// a pipeline object must have been applied
_SG_VALIDATE(_sg.cur_pipeline.id != SG_INVALID_ID, VALIDATE_ABND_PIPELINE);
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id);
_SG_VALIDATE(pip != 0, VALIDATE_ABND_PIPELINE_EXISTS);
if (!pip) {
return _sg_validate_end();
}
_SG_VALIDATE(pip->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_ABND_PIPELINE_VALID);
SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id));
// has expected vertex buffers, and vertex buffers still exist
for (int i = 0; i < SG_MAX_VERTEX_BUFFERS; i++) {
if (bindings->vertex_buffers[i].id != SG_INVALID_ID) {
_SG_VALIDATE(pip->cmn.vertex_buffer_layout_active[i], VALIDATE_ABND_VBS);
// buffers in vertex-buffer-slots must be of type SG_BUFFERTYPE_VERTEXBUFFER
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, bindings->vertex_buffers[i].id);
_SG_VALIDATE(buf != 0, VALIDATE_ABND_VB_EXISTS);
if (buf && buf->slot.state == SG_RESOURCESTATE_VALID) {
_SG_VALIDATE(SG_BUFFERTYPE_VERTEXBUFFER == buf->cmn.type, VALIDATE_ABND_VB_TYPE);
_SG_VALIDATE(!buf->cmn.append_overflow, VALIDATE_ABND_VB_OVERFLOW);
}
} else {
// vertex buffer provided in a slot which has no vertex layout in pipeline
_SG_VALIDATE(!pip->cmn.vertex_buffer_layout_active[i], VALIDATE_ABND_VBS);
}
}
// index buffer expected or not, and index buffer still exists
if (pip->cmn.index_type == SG_INDEXTYPE_NONE) {
// pipeline defines non-indexed rendering, but index buffer provided
_SG_VALIDATE(bindings->index_buffer.id == SG_INVALID_ID, VALIDATE_ABND_IB);
} else {
// pipeline defines indexed rendering, but no index buffer provided
_SG_VALIDATE(bindings->index_buffer.id != SG_INVALID_ID, VALIDATE_ABND_NO_IB);
}
if (bindings->index_buffer.id != SG_INVALID_ID) {
// buffer in index-buffer-slot must be of type SG_BUFFERTYPE_INDEXBUFFER
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, bindings->index_buffer.id);
_SG_VALIDATE(buf != 0, VALIDATE_ABND_IB_EXISTS);
if (buf && buf->slot.state == SG_RESOURCESTATE_VALID) {
_SG_VALIDATE(SG_BUFFERTYPE_INDEXBUFFER == buf->cmn.type, VALIDATE_ABND_IB_TYPE);
_SG_VALIDATE(!buf->cmn.append_overflow, VALIDATE_ABND_IB_OVERFLOW);
}
}
// has expected vertex shader images
for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++) {
const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_VS];
if (stage->images[i].image_type != _SG_IMAGETYPE_DEFAULT) {
_SG_VALIDATE(bindings->vs.images[i].id != SG_INVALID_ID, VALIDATE_ABND_VS_EXPECTED_IMAGE_BINDING);
if (bindings->vs.images[i].id != SG_INVALID_ID) {
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, bindings->vs.images[i].id);
_SG_VALIDATE(img != 0, VALIDATE_ABND_VS_IMG_EXISTS);
if (img && img->slot.state == SG_RESOURCESTATE_VALID) {
_SG_VALIDATE(img->cmn.type == stage->images[i].image_type, VALIDATE_ABND_VS_IMAGE_TYPE_MISMATCH);
_SG_VALIDATE(img->cmn.sample_count == 1, VALIDATE_ABND_VS_IMAGE_MSAA);
const _sg_pixelformat_info_t* info = &_sg.formats[img->cmn.pixel_format];
switch (stage->images[i].sample_type) {
case SG_IMAGESAMPLETYPE_FLOAT:
_SG_VALIDATE(info->filter, VALIDATE_ABND_VS_EXPECTED_FILTERABLE_IMAGE);
break;
case SG_IMAGESAMPLETYPE_DEPTH:
_SG_VALIDATE(info->depth, VALIDATE_ABND_VS_EXPECTED_DEPTH_IMAGE);
break;
default:
break;
}
}
}
} else {
_SG_VALIDATE(bindings->vs.images[i].id == SG_INVALID_ID, VALIDATE_ABND_VS_UNEXPECTED_IMAGE_BINDING);
}
}
// has expected vertex shader image samplers
for (int i = 0; i < SG_MAX_SHADERSTAGE_SAMPLERS; i++) {
const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_VS];
if (stage->samplers[i].sampler_type != _SG_SAMPLERTYPE_DEFAULT) {
_SG_VALIDATE(bindings->vs.samplers[i].id != SG_INVALID_ID, VALIDATE_ABND_VS_EXPECTED_SAMPLER_BINDING);
if (bindings->vs.samplers[i].id != SG_INVALID_ID) {
const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, bindings->vs.samplers[i].id);
_SG_VALIDATE(smp != 0, VALIDATE_ABND_VS_SMP_EXISTS);
if (smp) {
if (stage->samplers[i].sampler_type == SG_SAMPLERTYPE_COMPARISON) {
_SG_VALIDATE(smp->cmn.compare != SG_COMPAREFUNC_NEVER, VALIDATE_ABND_VS_UNEXPECTED_SAMPLER_COMPARE_NEVER);
} else {
_SG_VALIDATE(smp->cmn.compare == SG_COMPAREFUNC_NEVER, VALIDATE_ABND_VS_EXPECTED_SAMPLER_COMPARE_NEVER);
}
if (stage->samplers[i].sampler_type == SG_SAMPLERTYPE_NONFILTERING) {
const bool nonfiltering = (smp->cmn.min_filter != SG_FILTER_LINEAR)
&& (smp->cmn.mag_filter != SG_FILTER_LINEAR)
&& (smp->cmn.mipmap_filter != SG_FILTER_LINEAR);
_SG_VALIDATE(nonfiltering, VALIDATE_ABND_VS_EXPECTED_NONFILTERING_SAMPLER);
}
}
}
} else {
_SG_VALIDATE(bindings->vs.samplers[i].id == SG_INVALID_ID, VALIDATE_ABND_VS_UNEXPECTED_SAMPLER_BINDING);
}
}
// has expected vertex shader storage buffers
for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++) {
const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_VS];
if (stage->storage_buffers[i].used) {
_SG_VALIDATE(bindings->vs.storage_buffers[i].id != SG_INVALID_ID, VALIDATE_ABND_VS_EXPECTED_STORAGEBUFFER_BINDING);
if (bindings->vs.storage_buffers[i].id != SG_INVALID_ID) {
const _sg_buffer_t* sbuf = _sg_lookup_buffer(&_sg.pools, bindings->vs.storage_buffers[i].id);
_SG_VALIDATE(sbuf != 0, VALIDATE_ABND_VS_STORAGEBUFFER_EXISTS);
if (sbuf) {
_SG_VALIDATE(sbuf->cmn.type == SG_BUFFERTYPE_STORAGEBUFFER, VALIDATE_ABND_VS_STORAGEBUFFER_BINDING_BUFFERTYPE);
}
}
} else {
_SG_VALIDATE(bindings->vs.storage_buffers[i].id == SG_INVALID_ID, VALIDATE_ABND_VS_UNEXPECTED_STORAGEBUFFER_BINDING);
}
}
// has expected fragment shader images
for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++) {
const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_FS];
if (stage->images[i].image_type != _SG_IMAGETYPE_DEFAULT) {
_SG_VALIDATE(bindings->fs.images[i].id != SG_INVALID_ID, VALIDATE_ABND_FS_EXPECTED_IMAGE_BINDING);
if (bindings->fs.images[i].id != SG_INVALID_ID) {
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, bindings->fs.images[i].id);
_SG_VALIDATE(img != 0, VALIDATE_ABND_FS_IMG_EXISTS);
if (img && img->slot.state == SG_RESOURCESTATE_VALID) {
_SG_VALIDATE(img->cmn.type == stage->images[i].image_type, VALIDATE_ABND_FS_IMAGE_TYPE_MISMATCH);
_SG_VALIDATE(img->cmn.sample_count == 1, VALIDATE_ABND_FS_IMAGE_MSAA);
const _sg_pixelformat_info_t* info = &_sg.formats[img->cmn.pixel_format];
switch (stage->images[i].sample_type) {
case SG_IMAGESAMPLETYPE_FLOAT:
_SG_VALIDATE(info->filter, VALIDATE_ABND_FS_EXPECTED_FILTERABLE_IMAGE);
break;
case SG_IMAGESAMPLETYPE_DEPTH:
_SG_VALIDATE(info->depth, VALIDATE_ABND_FS_EXPECTED_DEPTH_IMAGE);
break;
default:
break;
}
}
}
} else {
_SG_VALIDATE(bindings->fs.images[i].id == SG_INVALID_ID, VALIDATE_ABND_FS_UNEXPECTED_IMAGE_BINDING);
}
}
// has expected fragment shader samplers
for (int i = 0; i < SG_MAX_SHADERSTAGE_SAMPLERS; i++) {
const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_FS];
if (stage->samplers[i].sampler_type != _SG_SAMPLERTYPE_DEFAULT) {
_SG_VALIDATE(bindings->fs.samplers[i].id != SG_INVALID_ID, VALIDATE_ABND_FS_EXPECTED_SAMPLER_BINDING);
if (bindings->fs.samplers[i].id != SG_INVALID_ID) {
const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, bindings->fs.samplers[i].id);
_SG_VALIDATE(smp != 0, VALIDATE_ABND_FS_SMP_EXISTS);
if (smp) {
if (stage->samplers[i].sampler_type == SG_SAMPLERTYPE_COMPARISON) {
_SG_VALIDATE(smp->cmn.compare != SG_COMPAREFUNC_NEVER, VALIDATE_ABND_FS_UNEXPECTED_SAMPLER_COMPARE_NEVER);
} else {
_SG_VALIDATE(smp->cmn.compare == SG_COMPAREFUNC_NEVER, VALIDATE_ABND_FS_EXPECTED_SAMPLER_COMPARE_NEVER);
}
if (stage->samplers[i].sampler_type == SG_SAMPLERTYPE_NONFILTERING) {
const bool nonfiltering = (smp->cmn.min_filter != SG_FILTER_LINEAR)
&& (smp->cmn.mag_filter != SG_FILTER_LINEAR)
&& (smp->cmn.mipmap_filter != SG_FILTER_LINEAR);
_SG_VALIDATE(nonfiltering, VALIDATE_ABND_FS_EXPECTED_NONFILTERING_SAMPLER);
}
}
}
} else {
_SG_VALIDATE(bindings->fs.samplers[i].id == SG_INVALID_ID, VALIDATE_ABND_FS_UNEXPECTED_SAMPLER_BINDING);
}
}
// has expected fragment shader storage buffers
for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++) {
const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_FS];
if (stage->storage_buffers[i].used) {
_SG_VALIDATE(bindings->fs.storage_buffers[i].id != SG_INVALID_ID, VALIDATE_ABND_FS_EXPECTED_STORAGEBUFFER_BINDING);
if (bindings->fs.storage_buffers[i].id != SG_INVALID_ID) {
const _sg_buffer_t* sbuf = _sg_lookup_buffer(&_sg.pools, bindings->fs.storage_buffers[i].id);
_SG_VALIDATE(sbuf != 0, VALIDATE_ABND_FS_STORAGEBUFFER_EXISTS);
if (sbuf) {
_SG_VALIDATE(sbuf->cmn.type == SG_BUFFERTYPE_STORAGEBUFFER, VALIDATE_ABND_FS_STORAGEBUFFER_BINDING_BUFFERTYPE);
}
}
} else {
_SG_VALIDATE(bindings->fs.storage_buffers[i].id == SG_INVALID_ID, VALIDATE_ABND_FS_UNEXPECTED_STORAGEBUFFER_BINDING);
}
}
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(stage_index);
_SOKOL_UNUSED(ub_index);
_SOKOL_UNUSED(data);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
SOKOL_ASSERT((stage_index == SG_SHADERSTAGE_VS) || (stage_index == SG_SHADERSTAGE_FS));
SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS));
_sg_validate_begin();
_SG_VALIDATE(_sg.cur_pipeline.id != SG_INVALID_ID, VALIDATE_AUB_NO_PIPELINE);
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id);
SOKOL_ASSERT(pip && (pip->slot.id == _sg.cur_pipeline.id));
SOKOL_ASSERT(pip->shader && (pip->shader->slot.id == pip->cmn.shader_id.id));
// check that there is a uniform block at 'stage' and 'ub_index'
const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[stage_index];
_SG_VALIDATE(ub_index < stage->num_uniform_blocks, VALIDATE_AUB_NO_UB_AT_SLOT);
// check that the provided data size matches the uniform block size
_SG_VALIDATE(data->size == stage->uniform_blocks[ub_index].size, VALIDATE_AUB_SIZE);
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_update_buffer(const _sg_buffer_t* buf, const sg_range* data) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(buf);
_SOKOL_UNUSED(data);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
SOKOL_ASSERT(buf && data && data->ptr);
_sg_validate_begin();
_SG_VALIDATE(buf->cmn.usage != SG_USAGE_IMMUTABLE, VALIDATE_UPDATEBUF_USAGE);
_SG_VALIDATE(buf->cmn.size >= (int)data->size, VALIDATE_UPDATEBUF_SIZE);
_SG_VALIDATE(buf->cmn.update_frame_index != _sg.frame_index, VALIDATE_UPDATEBUF_ONCE);
_SG_VALIDATE(buf->cmn.append_frame_index != _sg.frame_index, VALIDATE_UPDATEBUF_APPEND);
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_append_buffer(const _sg_buffer_t* buf, const sg_range* data) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(buf);
_SOKOL_UNUSED(data);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
SOKOL_ASSERT(buf && data && data->ptr);
_sg_validate_begin();
_SG_VALIDATE(buf->cmn.usage != SG_USAGE_IMMUTABLE, VALIDATE_APPENDBUF_USAGE);
_SG_VALIDATE(buf->cmn.size >= (buf->cmn.append_pos + (int)data->size), VALIDATE_APPENDBUF_SIZE);
_SG_VALIDATE(buf->cmn.update_frame_index != _sg.frame_index, VALIDATE_APPENDBUF_UPDATE);
return _sg_validate_end();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_update_image(const _sg_image_t* img, const sg_image_data* data) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(img);
_SOKOL_UNUSED(data);
return true;
#else
if (_sg.desc.disable_validation) {
return true;
}
SOKOL_ASSERT(img && data);
_sg_validate_begin();
_SG_VALIDATE(img->cmn.usage != SG_USAGE_IMMUTABLE, VALIDATE_UPDIMG_USAGE);
_SG_VALIDATE(img->cmn.upd_frame_index != _sg.frame_index, VALIDATE_UPDIMG_ONCE);
_sg_validate_image_data(data,
img->cmn.pixel_format,
img->cmn.width,
img->cmn.height,
(img->cmn.type == SG_IMAGETYPE_CUBE) ? 6 : 1,
img->cmn.num_mipmaps,
img->cmn.num_slices);
return _sg_validate_end();
#endif
}
// ██████ ███████ ███████ ██████ ██ ██ ██████ ██████ ███████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ █████ ███████ ██ ██ ██ ██ ██████ ██ █████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ███████ ██████ ██████ ██ ██ ██████ ███████ ███████
//
// >>resources
_SOKOL_PRIVATE sg_buffer_desc _sg_buffer_desc_defaults(const sg_buffer_desc* desc) {
sg_buffer_desc def = *desc;
def.type = _sg_def(def.type, SG_BUFFERTYPE_VERTEXBUFFER);
def.usage = _sg_def(def.usage, SG_USAGE_IMMUTABLE);
if (def.size == 0) {
def.size = def.data.size;
} else if (def.data.size == 0) {
def.data.size = def.size;
}
return def;
}
_SOKOL_PRIVATE sg_image_desc _sg_image_desc_defaults(const sg_image_desc* desc) {
sg_image_desc def = *desc;
def.type = _sg_def(def.type, SG_IMAGETYPE_2D);
def.num_slices = _sg_def(def.num_slices, 1);
def.num_mipmaps = _sg_def(def.num_mipmaps, 1);
def.usage = _sg_def(def.usage, SG_USAGE_IMMUTABLE);
if (desc->render_target) {
def.pixel_format = _sg_def(def.pixel_format, _sg.desc.environment.defaults.color_format);
def.sample_count = _sg_def(def.sample_count, _sg.desc.environment.defaults.sample_count);
} else {
def.pixel_format = _sg_def(def.pixel_format, SG_PIXELFORMAT_RGBA8);
def.sample_count = _sg_def(def.sample_count, 1);
}
return def;
}
_SOKOL_PRIVATE sg_sampler_desc _sg_sampler_desc_defaults(const sg_sampler_desc* desc) {
sg_sampler_desc def = *desc;
def.min_filter = _sg_def(def.min_filter, SG_FILTER_NEAREST);
def.mag_filter = _sg_def(def.mag_filter, SG_FILTER_NEAREST);
def.mipmap_filter = _sg_def(def.mipmap_filter, SG_FILTER_NEAREST);
def.wrap_u = _sg_def(def.wrap_u, SG_WRAP_REPEAT);
def.wrap_v = _sg_def(def.wrap_v, SG_WRAP_REPEAT);
def.wrap_w = _sg_def(def.wrap_w, SG_WRAP_REPEAT);
def.max_lod = _sg_def_flt(def.max_lod, FLT_MAX);
def.border_color = _sg_def(def.border_color, SG_BORDERCOLOR_OPAQUE_BLACK);
def.compare = _sg_def(def.compare, SG_COMPAREFUNC_NEVER);
def.max_anisotropy = _sg_def(def.max_anisotropy, 1);
return def;
}
_SOKOL_PRIVATE sg_shader_desc _sg_shader_desc_defaults(const sg_shader_desc* desc) {
sg_shader_desc def = *desc;
#if defined(SOKOL_METAL)
def.vs.entry = _sg_def(def.vs.entry, "_main");
def.fs.entry = _sg_def(def.fs.entry, "_main");
#else
def.vs.entry = _sg_def(def.vs.entry, "main");
def.fs.entry = _sg_def(def.fs.entry, "main");
#endif
#if defined(SOKOL_D3D11)
if (def.vs.source) {
def.vs.d3d11_target = _sg_def(def.vs.d3d11_target, "vs_4_0");
}
if (def.fs.source) {
def.fs.d3d11_target = _sg_def(def.fs.d3d11_target, "ps_4_0");
}
#endif
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &def.vs : &def.fs;
for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) {
sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index];
if (0 == ub_desc->size) {
break;
}
ub_desc->layout = _sg_def(ub_desc->layout, SG_UNIFORMLAYOUT_NATIVE);
for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) {
sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index];
if (u_desc->type == SG_UNIFORMTYPE_INVALID) {
break;
}
u_desc->array_count = _sg_def(u_desc->array_count, 1);
}
}
for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) {
sg_shader_image_desc* img_desc = &stage_desc->images[img_index];
if (!img_desc->used) {
break;
}
img_desc->image_type = _sg_def(img_desc->image_type, SG_IMAGETYPE_2D);
img_desc->sample_type = _sg_def(img_desc->sample_type, SG_IMAGESAMPLETYPE_FLOAT);
}
for (int smp_index = 0; smp_index < SG_MAX_SHADERSTAGE_SAMPLERS; smp_index++) {
sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[smp_index];
if (!smp_desc->used) {
break;
}
smp_desc->sampler_type = _sg_def(smp_desc->sampler_type, SG_SAMPLERTYPE_FILTERING);
}
}
return def;
}
_SOKOL_PRIVATE sg_pipeline_desc _sg_pipeline_desc_defaults(const sg_pipeline_desc* desc) {
sg_pipeline_desc def = *desc;
def.primitive_type = _sg_def(def.primitive_type, SG_PRIMITIVETYPE_TRIANGLES);
def.index_type = _sg_def(def.index_type, SG_INDEXTYPE_NONE);
def.cull_mode = _sg_def(def.cull_mode, SG_CULLMODE_NONE);
def.face_winding = _sg_def(def.face_winding, SG_FACEWINDING_CW);
def.sample_count = _sg_def(def.sample_count, _sg.desc.environment.defaults.sample_count);
def.stencil.front.compare = _sg_def(def.stencil.front.compare, SG_COMPAREFUNC_ALWAYS);
def.stencil.front.fail_op = _sg_def(def.stencil.front.fail_op, SG_STENCILOP_KEEP);
def.stencil.front.depth_fail_op = _sg_def(def.stencil.front.depth_fail_op, SG_STENCILOP_KEEP);
def.stencil.front.pass_op = _sg_def(def.stencil.front.pass_op, SG_STENCILOP_KEEP);
def.stencil.back.compare = _sg_def(def.stencil.back.compare, SG_COMPAREFUNC_ALWAYS);
def.stencil.back.fail_op = _sg_def(def.stencil.back.fail_op, SG_STENCILOP_KEEP);
def.stencil.back.depth_fail_op = _sg_def(def.stencil.back.depth_fail_op, SG_STENCILOP_KEEP);
def.stencil.back.pass_op = _sg_def(def.stencil.back.pass_op, SG_STENCILOP_KEEP);
def.depth.compare = _sg_def(def.depth.compare, SG_COMPAREFUNC_ALWAYS);
def.depth.pixel_format = _sg_def(def.depth.pixel_format, _sg.desc.environment.defaults.depth_format);
if (def.colors[0].pixel_format == SG_PIXELFORMAT_NONE) {
// special case depth-only rendering, enforce a color count of 0
def.color_count = 0;
} else {
def.color_count = _sg_def(def.color_count, 1);
}
if (def.color_count > SG_MAX_COLOR_ATTACHMENTS) {
def.color_count = SG_MAX_COLOR_ATTACHMENTS;
}
for (int i = 0; i < def.color_count; i++) {
sg_color_target_state* cs = &def.colors[i];
cs->pixel_format = _sg_def(cs->pixel_format, _sg.desc.environment.defaults.color_format);
cs->write_mask = _sg_def(cs->write_mask, SG_COLORMASK_RGBA);
sg_blend_state* bs = &def.colors[i].blend;
bs->src_factor_rgb = _sg_def(bs->src_factor_rgb, SG_BLENDFACTOR_ONE);
bs->dst_factor_rgb = _sg_def(bs->dst_factor_rgb, SG_BLENDFACTOR_ZERO);
bs->op_rgb = _sg_def(bs->op_rgb, SG_BLENDOP_ADD);
bs->src_factor_alpha = _sg_def(bs->src_factor_alpha, SG_BLENDFACTOR_ONE);
bs->dst_factor_alpha = _sg_def(bs->dst_factor_alpha, SG_BLENDFACTOR_ZERO);
bs->op_alpha = _sg_def(bs->op_alpha, SG_BLENDOP_ADD);
}
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
sg_vertex_attr_state* a_state = &def.layout.attrs[attr_index];
if (a_state->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS);
sg_vertex_buffer_layout_state* l_state = &def.layout.buffers[a_state->buffer_index];
l_state->step_func = _sg_def(l_state->step_func, SG_VERTEXSTEP_PER_VERTEX);
l_state->step_rate = _sg_def(l_state->step_rate, 1);
}
// resolve vertex layout strides and offsets
int auto_offset[SG_MAX_VERTEX_BUFFERS];
_sg_clear(auto_offset, sizeof(auto_offset));
bool use_auto_offset = true;
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
// to use computed offsets, *all* attr offsets must be 0
if (def.layout.attrs[attr_index].offset != 0) {
use_auto_offset = false;
}
}
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
sg_vertex_attr_state* a_state = &def.layout.attrs[attr_index];
if (a_state->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS);
if (use_auto_offset) {
a_state->offset = auto_offset[a_state->buffer_index];
}
auto_offset[a_state->buffer_index] += _sg_vertexformat_bytesize(a_state->format);
}
// compute vertex strides if needed
for (int buf_index = 0; buf_index < SG_MAX_VERTEX_BUFFERS; buf_index++) {
sg_vertex_buffer_layout_state* l_state = &def.layout.buffers[buf_index];
if (l_state->stride == 0) {
l_state->stride = auto_offset[buf_index];
}
}
return def;
}
_SOKOL_PRIVATE sg_attachments_desc _sg_attachments_desc_defaults(const sg_attachments_desc* desc) {
sg_attachments_desc def = *desc;
return def;
}
_SOKOL_PRIVATE sg_buffer _sg_alloc_buffer(void) {
sg_buffer res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.buffer_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&_sg.pools.buffer_pool, &_sg.pools.buffers[slot_index].slot, slot_index);
} else {
res.id = SG_INVALID_ID;
_SG_ERROR(BUFFER_POOL_EXHAUSTED);
}
return res;
}
_SOKOL_PRIVATE sg_image _sg_alloc_image(void) {
sg_image res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.image_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&_sg.pools.image_pool, &_sg.pools.images[slot_index].slot, slot_index);
} else {
res.id = SG_INVALID_ID;
_SG_ERROR(IMAGE_POOL_EXHAUSTED);
}
return res;
}
_SOKOL_PRIVATE sg_sampler _sg_alloc_sampler(void) {
sg_sampler res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.sampler_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&_sg.pools.sampler_pool, &_sg.pools.samplers[slot_index].slot, slot_index);
} else {
res.id = SG_INVALID_ID;
_SG_ERROR(SAMPLER_POOL_EXHAUSTED);
}
return res;
}
_SOKOL_PRIVATE sg_shader _sg_alloc_shader(void) {
sg_shader res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.shader_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&_sg.pools.shader_pool, &_sg.pools.shaders[slot_index].slot, slot_index);
} else {
res.id = SG_INVALID_ID;
_SG_ERROR(SHADER_POOL_EXHAUSTED);
}
return res;
}
_SOKOL_PRIVATE sg_pipeline _sg_alloc_pipeline(void) {
sg_pipeline res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.pipeline_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id =_sg_slot_alloc(&_sg.pools.pipeline_pool, &_sg.pools.pipelines[slot_index].slot, slot_index);
} else {
res.id = SG_INVALID_ID;
_SG_ERROR(PIPELINE_POOL_EXHAUSTED);
}
return res;
}
_SOKOL_PRIVATE sg_attachments _sg_alloc_attachments(void) {
sg_attachments res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.attachments_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&_sg.pools.attachments_pool, &_sg.pools.attachments[slot_index].slot, slot_index);
} else {
res.id = SG_INVALID_ID;
_SG_ERROR(PASS_POOL_EXHAUSTED);
}
return res;
}
_SOKOL_PRIVATE void _sg_dealloc_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf && (buf->slot.state == SG_RESOURCESTATE_ALLOC) && (buf->slot.id != SG_INVALID_ID));
_sg_pool_free_index(&_sg.pools.buffer_pool, _sg_slot_index(buf->slot.id));
_sg_reset_slot(&buf->slot);
}
_SOKOL_PRIVATE void _sg_dealloc_image(_sg_image_t* img) {
SOKOL_ASSERT(img && (img->slot.state == SG_RESOURCESTATE_ALLOC) && (img->slot.id != SG_INVALID_ID));
_sg_pool_free_index(&_sg.pools.image_pool, _sg_slot_index(img->slot.id));
_sg_reset_slot(&img->slot);
}
_SOKOL_PRIVATE void _sg_dealloc_sampler(_sg_sampler_t* smp) {
SOKOL_ASSERT(smp && (smp->slot.state == SG_RESOURCESTATE_ALLOC) && (smp->slot.id != SG_INVALID_ID));
_sg_pool_free_index(&_sg.pools.sampler_pool, _sg_slot_index(smp->slot.id));
_sg_reset_slot(&smp->slot);
}
_SOKOL_PRIVATE void _sg_dealloc_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd && (shd->slot.state == SG_RESOURCESTATE_ALLOC) && (shd->slot.id != SG_INVALID_ID));
_sg_pool_free_index(&_sg.pools.shader_pool, _sg_slot_index(shd->slot.id));
_sg_reset_slot(&shd->slot);
}
_SOKOL_PRIVATE void _sg_dealloc_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip && (pip->slot.state == SG_RESOURCESTATE_ALLOC) && (pip->slot.id != SG_INVALID_ID));
_sg_pool_free_index(&_sg.pools.pipeline_pool, _sg_slot_index(pip->slot.id));
_sg_reset_slot(&pip->slot);
}
_SOKOL_PRIVATE void _sg_dealloc_attachments(_sg_attachments_t* atts) {
SOKOL_ASSERT(atts && (atts->slot.state == SG_RESOURCESTATE_ALLOC) && (atts->slot.id != SG_INVALID_ID));
_sg_pool_free_index(&_sg.pools.attachments_pool, _sg_slot_index(atts->slot.id));
_sg_reset_slot(&atts->slot);
}
_SOKOL_PRIVATE void _sg_init_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && (buf->slot.state == SG_RESOURCESTATE_ALLOC));
SOKOL_ASSERT(desc);
if (_sg_validate_buffer_desc(desc)) {
_sg_buffer_common_init(&buf->cmn, desc);
buf->slot.state = _sg_create_buffer(buf, desc);
} else {
buf->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((buf->slot.state == SG_RESOURCESTATE_VALID)||(buf->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE void _sg_init_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && (img->slot.state == SG_RESOURCESTATE_ALLOC));
SOKOL_ASSERT(desc);
if (_sg_validate_image_desc(desc)) {
_sg_image_common_init(&img->cmn, desc);
img->slot.state = _sg_create_image(img, desc);
} else {
img->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((img->slot.state == SG_RESOURCESTATE_VALID)||(img->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE void _sg_init_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) {
SOKOL_ASSERT(smp && (smp->slot.state == SG_RESOURCESTATE_ALLOC));
SOKOL_ASSERT(desc);
if (_sg_validate_sampler_desc(desc)) {
_sg_sampler_common_init(&smp->cmn, desc);
smp->slot.state = _sg_create_sampler(smp, desc);
} else {
smp->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((smp->slot.state == SG_RESOURCESTATE_VALID)||(smp->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE void _sg_init_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && (shd->slot.state == SG_RESOURCESTATE_ALLOC));
SOKOL_ASSERT(desc);
if (_sg_validate_shader_desc(desc)) {
_sg_shader_common_init(&shd->cmn, desc);
shd->slot.state = _sg_create_shader(shd, desc);
} else {
shd->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((shd->slot.state == SG_RESOURCESTATE_VALID)||(shd->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE void _sg_init_pipeline(_sg_pipeline_t* pip, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && (pip->slot.state == SG_RESOURCESTATE_ALLOC));
SOKOL_ASSERT(desc);
if (_sg_validate_pipeline_desc(desc)) {
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, desc->shader.id);
if (shd && (shd->slot.state == SG_RESOURCESTATE_VALID)) {
_sg_pipeline_common_init(&pip->cmn, desc);
pip->slot.state = _sg_create_pipeline(pip, shd, desc);
} else {
pip->slot.state = SG_RESOURCESTATE_FAILED;
}
} else {
pip->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((pip->slot.state == SG_RESOURCESTATE_VALID)||(pip->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE void _sg_init_attachments(_sg_attachments_t* atts, const sg_attachments_desc* desc) {
SOKOL_ASSERT(atts && atts->slot.state == SG_RESOURCESTATE_ALLOC);
SOKOL_ASSERT(desc);
if (_sg_validate_attachments_desc(desc)) {
// lookup pass attachment image pointers
_sg_image_t* color_images[SG_MAX_COLOR_ATTACHMENTS] = { 0 };
_sg_image_t* resolve_images[SG_MAX_COLOR_ATTACHMENTS] = { 0 };
_sg_image_t* ds_image = 0;
// NOTE: validation already checked that all surfaces are same width/height
int width = 0;
int height = 0;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (desc->colors[i].image.id) {
color_images[i] = _sg_lookup_image(&_sg.pools, desc->colors[i].image.id);
if (!(color_images[i] && color_images[i]->slot.state == SG_RESOURCESTATE_VALID)) {
atts->slot.state = SG_RESOURCESTATE_FAILED;
return;
}
const int mip_level = desc->colors[i].mip_level;
width = _sg_miplevel_dim(color_images[i]->cmn.width, mip_level);
height = _sg_miplevel_dim(color_images[i]->cmn.height, mip_level);
}
if (desc->resolves[i].image.id) {
resolve_images[i] = _sg_lookup_image(&_sg.pools, desc->resolves[i].image.id);
if (!(resolve_images[i] && resolve_images[i]->slot.state == SG_RESOURCESTATE_VALID)) {
atts->slot.state = SG_RESOURCESTATE_FAILED;
return;
}
}
}
if (desc->depth_stencil.image.id) {
ds_image = _sg_lookup_image(&_sg.pools, desc->depth_stencil.image.id);
if (!(ds_image && ds_image->slot.state == SG_RESOURCESTATE_VALID)) {
atts->slot.state = SG_RESOURCESTATE_FAILED;
return;
}
const int mip_level = desc->depth_stencil.mip_level;
width = _sg_miplevel_dim(ds_image->cmn.width, mip_level);
height = _sg_miplevel_dim(ds_image->cmn.height, mip_level);
}
_sg_attachments_common_init(&atts->cmn, desc, width, height);
atts->slot.state = _sg_create_attachments(atts, color_images, resolve_images, ds_image, desc);
} else {
atts->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((atts->slot.state == SG_RESOURCESTATE_VALID)||(atts->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE void _sg_uninit_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf && ((buf->slot.state == SG_RESOURCESTATE_VALID) || (buf->slot.state == SG_RESOURCESTATE_FAILED)));
_sg_discard_buffer(buf);
_sg_reset_buffer_to_alloc_state(buf);
}
_SOKOL_PRIVATE void _sg_uninit_image(_sg_image_t* img) {
SOKOL_ASSERT(img && ((img->slot.state == SG_RESOURCESTATE_VALID) || (img->slot.state == SG_RESOURCESTATE_FAILED)));
_sg_discard_image(img);
_sg_reset_image_to_alloc_state(img);
}
_SOKOL_PRIVATE void _sg_uninit_sampler(_sg_sampler_t* smp) {
SOKOL_ASSERT(smp && ((smp->slot.state == SG_RESOURCESTATE_VALID) || (smp->slot.state == SG_RESOURCESTATE_FAILED)));
_sg_discard_sampler(smp);
_sg_reset_sampler_to_alloc_state(smp);
}
_SOKOL_PRIVATE void _sg_uninit_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd && ((shd->slot.state == SG_RESOURCESTATE_VALID) || (shd->slot.state == SG_RESOURCESTATE_FAILED)));
_sg_discard_shader(shd);
_sg_reset_shader_to_alloc_state(shd);
}
_SOKOL_PRIVATE void _sg_uninit_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip && ((pip->slot.state == SG_RESOURCESTATE_VALID) || (pip->slot.state == SG_RESOURCESTATE_FAILED)));
_sg_discard_pipeline(pip);
_sg_reset_pipeline_to_alloc_state(pip);
}
_SOKOL_PRIVATE void _sg_uninit_attachments(_sg_attachments_t* atts) {
SOKOL_ASSERT(atts && ((atts->slot.state == SG_RESOURCESTATE_VALID) || (atts->slot.state == SG_RESOURCESTATE_FAILED)));
_sg_discard_attachments(atts);
_sg_reset_attachments_to_alloc_state(atts);
}
_SOKOL_PRIVATE void _sg_setup_commit_listeners(const sg_desc* desc) {
SOKOL_ASSERT(desc->max_commit_listeners > 0);
SOKOL_ASSERT(0 == _sg.commit_listeners.items);
SOKOL_ASSERT(0 == _sg.commit_listeners.num);
SOKOL_ASSERT(0 == _sg.commit_listeners.upper);
_sg.commit_listeners.num = desc->max_commit_listeners;
const size_t size = (size_t)_sg.commit_listeners.num * sizeof(sg_commit_listener);
_sg.commit_listeners.items = (sg_commit_listener*)_sg_malloc_clear(size);
}
_SOKOL_PRIVATE void _sg_discard_commit_listeners(void) {
SOKOL_ASSERT(0 != _sg.commit_listeners.items);
_sg_free(_sg.commit_listeners.items);
_sg.commit_listeners.items = 0;
}
_SOKOL_PRIVATE void _sg_notify_commit_listeners(void) {
SOKOL_ASSERT(_sg.commit_listeners.items);
for (int i = 0; i < _sg.commit_listeners.upper; i++) {
const sg_commit_listener* listener = &_sg.commit_listeners.items[i];
if (listener->func) {
listener->func(listener->user_data);
}
}
}
_SOKOL_PRIVATE bool _sg_add_commit_listener(const sg_commit_listener* new_listener) {
SOKOL_ASSERT(new_listener && new_listener->func);
SOKOL_ASSERT(_sg.commit_listeners.items);
// first check if the listener hadn't been added already
for (int i = 0; i < _sg.commit_listeners.upper; i++) {
const sg_commit_listener* slot = &_sg.commit_listeners.items[i];
if ((slot->func == new_listener->func) && (slot->user_data == new_listener->user_data)) {
_SG_ERROR(IDENTICAL_COMMIT_LISTENER);
return false;
}
}
// first try to plug a hole
sg_commit_listener* slot = 0;
for (int i = 0; i < _sg.commit_listeners.upper; i++) {
if (_sg.commit_listeners.items[i].func == 0) {
slot = &_sg.commit_listeners.items[i];
break;
}
}
if (!slot) {
// append to end
if (_sg.commit_listeners.upper < _sg.commit_listeners.num) {
slot = &_sg.commit_listeners.items[_sg.commit_listeners.upper++];
}
}
if (!slot) {
_SG_ERROR(COMMIT_LISTENER_ARRAY_FULL);
return false;
}
*slot = *new_listener;
return true;
}
_SOKOL_PRIVATE bool _sg_remove_commit_listener(const sg_commit_listener* listener) {
SOKOL_ASSERT(listener && listener->func);
SOKOL_ASSERT(_sg.commit_listeners.items);
for (int i = 0; i < _sg.commit_listeners.upper; i++) {
sg_commit_listener* slot = &_sg.commit_listeners.items[i];
// both the function pointer and user data must match!
if ((slot->func == listener->func) && (slot->user_data == listener->user_data)) {
slot->func = 0;
slot->user_data = 0;
// NOTE: since _sg_add_commit_listener() already catches duplicates,
// we don't need to worry about them here
return true;
}
}
return false;
}
_SOKOL_PRIVATE sg_desc _sg_desc_defaults(const sg_desc* desc) {
/*
NOTE: on WebGPU, the default color pixel format MUST be provided,
cannot be a default compile-time constant.
*/
sg_desc res = *desc;
#if defined(SOKOL_WGPU)
SOKOL_ASSERT(SG_PIXELFORMAT_NONE < res.environment.defaults.color_format);
#elif defined(SOKOL_METAL) || defined(SOKOL_D3D11)
res.environment.defaults.color_format = _sg_def(res.environment.defaults.color_format, SG_PIXELFORMAT_BGRA8);
#else
res.environment.defaults.color_format = _sg_def(res.environment.defaults.color_format, SG_PIXELFORMAT_RGBA8);
#endif
res.environment.defaults.depth_format = _sg_def(res.environment.defaults.depth_format, SG_PIXELFORMAT_DEPTH_STENCIL);
res.environment.defaults.sample_count = _sg_def(res.environment.defaults.sample_count, 1);
res.buffer_pool_size = _sg_def(res.buffer_pool_size, _SG_DEFAULT_BUFFER_POOL_SIZE);
res.image_pool_size = _sg_def(res.image_pool_size, _SG_DEFAULT_IMAGE_POOL_SIZE);
res.sampler_pool_size = _sg_def(res.sampler_pool_size, _SG_DEFAULT_SAMPLER_POOL_SIZE);
res.shader_pool_size = _sg_def(res.shader_pool_size, _SG_DEFAULT_SHADER_POOL_SIZE);
res.pipeline_pool_size = _sg_def(res.pipeline_pool_size, _SG_DEFAULT_PIPELINE_POOL_SIZE);
res.attachments_pool_size = _sg_def(res.attachments_pool_size, _SG_DEFAULT_ATTACHMENTS_POOL_SIZE);
res.uniform_buffer_size = _sg_def(res.uniform_buffer_size, _SG_DEFAULT_UB_SIZE);
res.max_commit_listeners = _sg_def(res.max_commit_listeners, _SG_DEFAULT_MAX_COMMIT_LISTENERS);
res.wgpu_bindgroups_cache_size = _sg_def(res.wgpu_bindgroups_cache_size, _SG_DEFAULT_WGPU_BINDGROUP_CACHE_SIZE);
return res;
}
_SOKOL_PRIVATE sg_pass _sg_pass_defaults(const sg_pass* pass) {
sg_pass res = *pass;
if (res.attachments.id == SG_INVALID_ID) {
// this is a swapchain-pass
res.swapchain.sample_count = _sg_def(res.swapchain.sample_count, _sg.desc.environment.defaults.sample_count);
res.swapchain.color_format = _sg_def(res.swapchain.color_format, _sg.desc.environment.defaults.color_format);
res.swapchain.depth_format = _sg_def(res.swapchain.depth_format, _sg.desc.environment.defaults.depth_format);
}
res.action = _sg_pass_action_defaults(&res.action);
return res;
}
// ██████ ██ ██ ██████ ██ ██ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ██ ██████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██████ ██████ ███████ ██ ██████
//
// >>public
SOKOL_API_IMPL void sg_setup(const sg_desc* desc) {
SOKOL_ASSERT(desc);
SOKOL_ASSERT((desc->_start_canary == 0) && (desc->_end_canary == 0));
SOKOL_ASSERT((desc->allocator.alloc_fn && desc->allocator.free_fn) || (!desc->allocator.alloc_fn && !desc->allocator.free_fn));
_SG_CLEAR_ARC_STRUCT(_sg_state_t, _sg);
_sg.desc = _sg_desc_defaults(desc);
_sg_setup_pools(&_sg.pools, &_sg.desc);
_sg_setup_commit_listeners(&_sg.desc);
_sg.frame_index = 1;
_sg.stats_enabled = true;
_sg_setup_backend(&_sg.desc);
_sg.valid = true;
}
SOKOL_API_IMPL void sg_shutdown(void) {
_sg_discard_all_resources(&_sg.pools);
_sg_discard_backend();
_sg_discard_commit_listeners();
_sg_discard_pools(&_sg.pools);
_SG_CLEAR_ARC_STRUCT(_sg_state_t, _sg);
}
SOKOL_API_IMPL bool sg_isvalid(void) {
return _sg.valid;
}
SOKOL_API_IMPL sg_desc sg_query_desc(void) {
SOKOL_ASSERT(_sg.valid);
return _sg.desc;
}
SOKOL_API_IMPL sg_backend sg_query_backend(void) {
SOKOL_ASSERT(_sg.valid);
return _sg.backend;
}
SOKOL_API_IMPL sg_features sg_query_features(void) {
SOKOL_ASSERT(_sg.valid);
return _sg.features;
}
SOKOL_API_IMPL sg_limits sg_query_limits(void) {
SOKOL_ASSERT(_sg.valid);
return _sg.limits;
}
SOKOL_API_IMPL sg_pixelformat_info sg_query_pixelformat(sg_pixel_format fmt) {
SOKOL_ASSERT(_sg.valid);
int fmt_index = (int) fmt;
SOKOL_ASSERT((fmt_index > SG_PIXELFORMAT_NONE) && (fmt_index < _SG_PIXELFORMAT_NUM));
const _sg_pixelformat_info_t* src = &_sg.formats[fmt_index];
sg_pixelformat_info res;
_sg_clear(&res, sizeof(res));
res.sample = src->sample;
res.filter = src->filter;
res.render = src->render;
res.blend = src->blend;
res.msaa = src->msaa;
res.depth = src->depth;
res.compressed = _sg_is_compressed_pixel_format(fmt);
if (!res.compressed) {
res.bytes_per_pixel = _sg_pixelformat_bytesize(fmt);
}
return res;
}
SOKOL_API_IMPL int sg_query_row_pitch(sg_pixel_format fmt, int width, int row_align_bytes) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(width > 0);
SOKOL_ASSERT((row_align_bytes > 0) && _sg_ispow2(row_align_bytes));
SOKOL_ASSERT(((int)fmt > SG_PIXELFORMAT_NONE) && ((int)fmt < _SG_PIXELFORMAT_NUM));
return _sg_row_pitch(fmt, width, row_align_bytes);
}
SOKOL_API_IMPL int sg_query_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align_bytes) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT((width > 0) && (height > 0));
SOKOL_ASSERT((row_align_bytes > 0) && _sg_ispow2(row_align_bytes));
SOKOL_ASSERT(((int)fmt > SG_PIXELFORMAT_NONE) && ((int)fmt < _SG_PIXELFORMAT_NUM));
return _sg_surface_pitch(fmt, width, height, row_align_bytes);
}
SOKOL_API_IMPL sg_frame_stats sg_query_frame_stats(void) {
SOKOL_ASSERT(_sg.valid);
return _sg.prev_stats;
}
SOKOL_API_IMPL sg_trace_hooks sg_install_trace_hooks(const sg_trace_hooks* trace_hooks) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(trace_hooks);
_SOKOL_UNUSED(trace_hooks);
#if defined(SOKOL_TRACE_HOOKS)
sg_trace_hooks old_hooks = _sg.hooks;
_sg.hooks = *trace_hooks;
#else
static sg_trace_hooks old_hooks;
_SG_WARN(TRACE_HOOKS_NOT_ENABLED);
#endif
return old_hooks;
}
SOKOL_API_IMPL sg_buffer sg_alloc_buffer(void) {
SOKOL_ASSERT(_sg.valid);
sg_buffer res = _sg_alloc_buffer();
_SG_TRACE_ARGS(alloc_buffer, res);
return res;
}
SOKOL_API_IMPL sg_image sg_alloc_image(void) {
SOKOL_ASSERT(_sg.valid);
sg_image res = _sg_alloc_image();
_SG_TRACE_ARGS(alloc_image, res);
return res;
}
SOKOL_API_IMPL sg_sampler sg_alloc_sampler(void) {
SOKOL_ASSERT(_sg.valid);
sg_sampler res = _sg_alloc_sampler();
_SG_TRACE_ARGS(alloc_sampler, res);
return res;
}
SOKOL_API_IMPL sg_shader sg_alloc_shader(void) {
SOKOL_ASSERT(_sg.valid);
sg_shader res = _sg_alloc_shader();
_SG_TRACE_ARGS(alloc_shader, res);
return res;
}
SOKOL_API_IMPL sg_pipeline sg_alloc_pipeline(void) {
SOKOL_ASSERT(_sg.valid);
sg_pipeline res = _sg_alloc_pipeline();
_SG_TRACE_ARGS(alloc_pipeline, res);
return res;
}
SOKOL_API_IMPL sg_attachments sg_alloc_attachments(void) {
SOKOL_ASSERT(_sg.valid);
sg_attachments res = _sg_alloc_attachments();
_SG_TRACE_ARGS(alloc_attachments, res);
return res;
}
SOKOL_API_IMPL void sg_dealloc_buffer(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
if (buf->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_buffer(buf);
} else {
_SG_ERROR(DEALLOC_BUFFER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(dealloc_buffer, buf_id);
}
SOKOL_API_IMPL void sg_dealloc_image(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
if (img->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_image(img);
} else {
_SG_ERROR(DEALLOC_IMAGE_INVALID_STATE);
}
}
_SG_TRACE_ARGS(dealloc_image, img_id);
}
SOKOL_API_IMPL void sg_dealloc_sampler(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
_sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
if (smp->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_sampler(smp);
} else {
_SG_ERROR(DEALLOC_SAMPLER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(dealloc_sampler, smp_id);
}
SOKOL_API_IMPL void sg_dealloc_shader(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
if (shd->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_shader(shd);
} else {
_SG_ERROR(DEALLOC_SHADER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(dealloc_shader, shd_id);
}
SOKOL_API_IMPL void sg_dealloc_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
if (pip->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_pipeline(pip);
} else {
_SG_ERROR(DEALLOC_PIPELINE_INVALID_STATE);
}
}
_SG_TRACE_ARGS(dealloc_pipeline, pip_id);
}
SOKOL_API_IMPL void sg_dealloc_attachments(sg_attachments atts_id) {
SOKOL_ASSERT(_sg.valid);
_sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
if (atts) {
if (atts->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_attachments(atts);
} else {
_SG_ERROR(DEALLOC_ATTACHMENTS_INVALID_STATE);
}
}
_SG_TRACE_ARGS(dealloc_attachments, atts_id);
}
SOKOL_API_IMPL void sg_init_buffer(sg_buffer buf_id, const sg_buffer_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_buffer_desc desc_def = _sg_buffer_desc_defaults(desc);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
if (buf->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_init_buffer(buf, &desc_def);
SOKOL_ASSERT((buf->slot.state == SG_RESOURCESTATE_VALID) || (buf->slot.state == SG_RESOURCESTATE_FAILED));
} else {
_SG_ERROR(INIT_BUFFER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(init_buffer, buf_id, &desc_def);
}
SOKOL_API_IMPL void sg_init_image(sg_image img_id, const sg_image_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_image_desc desc_def = _sg_image_desc_defaults(desc);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
if (img->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_init_image(img, &desc_def);
SOKOL_ASSERT((img->slot.state == SG_RESOURCESTATE_VALID) || (img->slot.state == SG_RESOURCESTATE_FAILED));
} else {
_SG_ERROR(INIT_IMAGE_INVALID_STATE);
}
}
_SG_TRACE_ARGS(init_image, img_id, &desc_def);
}
SOKOL_API_IMPL void sg_init_sampler(sg_sampler smp_id, const sg_sampler_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_sampler_desc desc_def = _sg_sampler_desc_defaults(desc);
_sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
if (smp->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_init_sampler(smp, &desc_def);
SOKOL_ASSERT((smp->slot.state == SG_RESOURCESTATE_VALID) || (smp->slot.state == SG_RESOURCESTATE_FAILED));
} else {
_SG_ERROR(INIT_SAMPLER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(init_sampler, smp_id, &desc_def);
}
SOKOL_API_IMPL void sg_init_shader(sg_shader shd_id, const sg_shader_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_shader_desc desc_def = _sg_shader_desc_defaults(desc);
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
if (shd->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_init_shader(shd, &desc_def);
SOKOL_ASSERT((shd->slot.state == SG_RESOURCESTATE_VALID) || (shd->slot.state == SG_RESOURCESTATE_FAILED));
} else {
_SG_ERROR(INIT_SHADER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(init_shader, shd_id, &desc_def);
}
SOKOL_API_IMPL void sg_init_pipeline(sg_pipeline pip_id, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_pipeline_desc desc_def = _sg_pipeline_desc_defaults(desc);
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
if (pip->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_init_pipeline(pip, &desc_def);
SOKOL_ASSERT((pip->slot.state == SG_RESOURCESTATE_VALID) || (pip->slot.state == SG_RESOURCESTATE_FAILED));
} else {
_SG_ERROR(INIT_PIPELINE_INVALID_STATE);
}
}
_SG_TRACE_ARGS(init_pipeline, pip_id, &desc_def);
}
SOKOL_API_IMPL void sg_init_attachments(sg_attachments atts_id, const sg_attachments_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_attachments_desc desc_def = _sg_attachments_desc_defaults(desc);
_sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
if (atts) {
if (atts->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_init_attachments(atts, &desc_def);
SOKOL_ASSERT((atts->slot.state == SG_RESOURCESTATE_VALID) || (atts->slot.state == SG_RESOURCESTATE_FAILED));
} else {
_SG_ERROR(INIT_ATTACHMENTS_INVALID_STATE);
}
}
_SG_TRACE_ARGS(init_attachments, atts_id, &desc_def);
}
SOKOL_API_IMPL void sg_uninit_buffer(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
if ((buf->slot.state == SG_RESOURCESTATE_VALID) || (buf->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_buffer(buf);
SOKOL_ASSERT(buf->slot.state == SG_RESOURCESTATE_ALLOC);
} else {
_SG_ERROR(UNINIT_BUFFER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(uninit_buffer, buf_id);
}
SOKOL_API_IMPL void sg_uninit_image(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
if ((img->slot.state == SG_RESOURCESTATE_VALID) || (img->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_image(img);
SOKOL_ASSERT(img->slot.state == SG_RESOURCESTATE_ALLOC);
} else {
_SG_ERROR(UNINIT_IMAGE_INVALID_STATE);
}
}
_SG_TRACE_ARGS(uninit_image, img_id);
}
SOKOL_API_IMPL void sg_uninit_sampler(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
_sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
if ((smp->slot.state == SG_RESOURCESTATE_VALID) || (smp->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_sampler(smp);
SOKOL_ASSERT(smp->slot.state == SG_RESOURCESTATE_ALLOC);
} else {
_SG_ERROR(UNINIT_SAMPLER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(uninit_sampler, smp_id);
}
SOKOL_API_IMPL void sg_uninit_shader(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
if ((shd->slot.state == SG_RESOURCESTATE_VALID) || (shd->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_shader(shd);
SOKOL_ASSERT(shd->slot.state == SG_RESOURCESTATE_ALLOC);
} else {
_SG_ERROR(UNINIT_SHADER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(uninit_shader, shd_id);
}
SOKOL_API_IMPL void sg_uninit_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
if ((pip->slot.state == SG_RESOURCESTATE_VALID) || (pip->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_pipeline(pip);
SOKOL_ASSERT(pip->slot.state == SG_RESOURCESTATE_ALLOC);
} else {
_SG_ERROR(UNINIT_PIPELINE_INVALID_STATE);
}
}
_SG_TRACE_ARGS(uninit_pipeline, pip_id);
}
SOKOL_API_IMPL void sg_uninit_attachments(sg_attachments atts_id) {
SOKOL_ASSERT(_sg.valid);
_sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
if (atts) {
if ((atts->slot.state == SG_RESOURCESTATE_VALID) || (atts->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_attachments(atts);
SOKOL_ASSERT(atts->slot.state == SG_RESOURCESTATE_ALLOC);
} else {
_SG_ERROR(UNINIT_ATTACHMENTS_INVALID_STATE);
}
}
_SG_TRACE_ARGS(uninit_attachments, atts_id);
}
SOKOL_API_IMPL void sg_fail_buffer(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
if (buf->slot.state == SG_RESOURCESTATE_ALLOC) {
buf->slot.state = SG_RESOURCESTATE_FAILED;
} else {
_SG_ERROR(FAIL_BUFFER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(fail_buffer, buf_id);
}
SOKOL_API_IMPL void sg_fail_image(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
if (img->slot.state == SG_RESOURCESTATE_ALLOC) {
img->slot.state = SG_RESOURCESTATE_FAILED;
} else {
_SG_ERROR(FAIL_IMAGE_INVALID_STATE);
}
}
_SG_TRACE_ARGS(fail_image, img_id);
}
SOKOL_API_IMPL void sg_fail_sampler(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
_sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
if (smp->slot.state == SG_RESOURCESTATE_ALLOC) {
smp->slot.state = SG_RESOURCESTATE_FAILED;
} else {
_SG_ERROR(FAIL_SAMPLER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(fail_sampler, smp_id);
}
SOKOL_API_IMPL void sg_fail_shader(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
if (shd->slot.state == SG_RESOURCESTATE_ALLOC) {
shd->slot.state = SG_RESOURCESTATE_FAILED;
} else {
_SG_ERROR(FAIL_SHADER_INVALID_STATE);
}
}
_SG_TRACE_ARGS(fail_shader, shd_id);
}
SOKOL_API_IMPL void sg_fail_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
if (pip->slot.state == SG_RESOURCESTATE_ALLOC) {
pip->slot.state = SG_RESOURCESTATE_FAILED;
} else {
_SG_ERROR(FAIL_PIPELINE_INVALID_STATE);
}
}
_SG_TRACE_ARGS(fail_pipeline, pip_id);
}
SOKOL_API_IMPL void sg_fail_attachments(sg_attachments atts_id) {
SOKOL_ASSERT(_sg.valid);
_sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
if (atts) {
if (atts->slot.state == SG_RESOURCESTATE_ALLOC) {
atts->slot.state = SG_RESOURCESTATE_FAILED;
} else {
_SG_ERROR(FAIL_ATTACHMENTS_INVALID_STATE);
}
}
_SG_TRACE_ARGS(fail_attachments, atts_id);
}
SOKOL_API_IMPL sg_resource_state sg_query_buffer_state(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
sg_resource_state res = buf ? buf->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
SOKOL_API_IMPL sg_resource_state sg_query_image_state(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
sg_resource_state res = img ? img->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
SOKOL_API_IMPL sg_resource_state sg_query_sampler_state(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
_sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
sg_resource_state res = smp ? smp->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
SOKOL_API_IMPL sg_resource_state sg_query_shader_state(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
sg_resource_state res = shd ? shd->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
SOKOL_API_IMPL sg_resource_state sg_query_pipeline_state(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
sg_resource_state res = pip ? pip->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
SOKOL_API_IMPL sg_resource_state sg_query_attachments_state(sg_attachments atts_id) {
SOKOL_ASSERT(_sg.valid);
_sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
sg_resource_state res = atts ? atts->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
SOKOL_API_IMPL sg_buffer sg_make_buffer(const sg_buffer_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_buffer_desc desc_def = _sg_buffer_desc_defaults(desc);
sg_buffer buf_id = _sg_alloc_buffer();
if (buf_id.id != SG_INVALID_ID) {
_sg_buffer_t* buf = _sg_buffer_at(&_sg.pools, buf_id.id);
SOKOL_ASSERT(buf && (buf->slot.state == SG_RESOURCESTATE_ALLOC));
_sg_init_buffer(buf, &desc_def);
SOKOL_ASSERT((buf->slot.state == SG_RESOURCESTATE_VALID) || (buf->slot.state == SG_RESOURCESTATE_FAILED));
}
_SG_TRACE_ARGS(make_buffer, &desc_def, buf_id);
return buf_id;
}
SOKOL_API_IMPL sg_image sg_make_image(const sg_image_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_image_desc desc_def = _sg_image_desc_defaults(desc);
sg_image img_id = _sg_alloc_image();
if (img_id.id != SG_INVALID_ID) {
_sg_image_t* img = _sg_image_at(&_sg.pools, img_id.id);
SOKOL_ASSERT(img && (img->slot.state == SG_RESOURCESTATE_ALLOC));
_sg_init_image(img, &desc_def);
SOKOL_ASSERT((img->slot.state == SG_RESOURCESTATE_VALID) || (img->slot.state == SG_RESOURCESTATE_FAILED));
}
_SG_TRACE_ARGS(make_image, &desc_def, img_id);
return img_id;
}
SOKOL_API_IMPL sg_sampler sg_make_sampler(const sg_sampler_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_sampler_desc desc_def = _sg_sampler_desc_defaults(desc);
sg_sampler smp_id = _sg_alloc_sampler();
if (smp_id.id != SG_INVALID_ID) {
_sg_sampler_t* smp = _sg_sampler_at(&_sg.pools, smp_id.id);
SOKOL_ASSERT(smp && (smp->slot.state == SG_RESOURCESTATE_ALLOC));
_sg_init_sampler(smp, &desc_def);
SOKOL_ASSERT((smp->slot.state == SG_RESOURCESTATE_VALID) || (smp->slot.state == SG_RESOURCESTATE_FAILED));
}
_SG_TRACE_ARGS(make_sampler, &desc_def, smp_id);
return smp_id;
}
SOKOL_API_IMPL sg_shader sg_make_shader(const sg_shader_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_shader_desc desc_def = _sg_shader_desc_defaults(desc);
sg_shader shd_id = _sg_alloc_shader();
if (shd_id.id != SG_INVALID_ID) {
_sg_shader_t* shd = _sg_shader_at(&_sg.pools, shd_id.id);
SOKOL_ASSERT(shd && (shd->slot.state == SG_RESOURCESTATE_ALLOC));
_sg_init_shader(shd, &desc_def);
SOKOL_ASSERT((shd->slot.state == SG_RESOURCESTATE_VALID) || (shd->slot.state == SG_RESOURCESTATE_FAILED));
}
_SG_TRACE_ARGS(make_shader, &desc_def, shd_id);
return shd_id;
}
SOKOL_API_IMPL sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_pipeline_desc desc_def = _sg_pipeline_desc_defaults(desc);
sg_pipeline pip_id = _sg_alloc_pipeline();
if (pip_id.id != SG_INVALID_ID) {
_sg_pipeline_t* pip = _sg_pipeline_at(&_sg.pools, pip_id.id);
SOKOL_ASSERT(pip && (pip->slot.state == SG_RESOURCESTATE_ALLOC));
_sg_init_pipeline(pip, &desc_def);
SOKOL_ASSERT((pip->slot.state == SG_RESOURCESTATE_VALID) || (pip->slot.state == SG_RESOURCESTATE_FAILED));
}
_SG_TRACE_ARGS(make_pipeline, &desc_def, pip_id);
return pip_id;
}
SOKOL_API_IMPL sg_attachments sg_make_attachments(const sg_attachments_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_attachments_desc desc_def = _sg_attachments_desc_defaults(desc);
sg_attachments atts_id = _sg_alloc_attachments();
if (atts_id.id != SG_INVALID_ID) {
_sg_attachments_t* atts = _sg_attachments_at(&_sg.pools, atts_id.id);
SOKOL_ASSERT(atts && (atts->slot.state == SG_RESOURCESTATE_ALLOC));
_sg_init_attachments(atts, &desc_def);
SOKOL_ASSERT((atts->slot.state == SG_RESOURCESTATE_VALID) || (atts->slot.state == SG_RESOURCESTATE_FAILED));
}
_SG_TRACE_ARGS(make_attachments, &desc_def, atts_id);
return atts_id;
}
SOKOL_API_IMPL void sg_destroy_buffer(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_buffer, buf_id);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
if ((buf->slot.state == SG_RESOURCESTATE_VALID) || (buf->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_buffer(buf);
SOKOL_ASSERT(buf->slot.state == SG_RESOURCESTATE_ALLOC);
}
if (buf->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_buffer(buf);
SOKOL_ASSERT(buf->slot.state == SG_RESOURCESTATE_INITIAL);
}
}
}
SOKOL_API_IMPL void sg_destroy_image(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_image, img_id);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
if ((img->slot.state == SG_RESOURCESTATE_VALID) || (img->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_image(img);
SOKOL_ASSERT(img->slot.state == SG_RESOURCESTATE_ALLOC);
}
if (img->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_image(img);
SOKOL_ASSERT(img->slot.state == SG_RESOURCESTATE_INITIAL);
}
}
}
SOKOL_API_IMPL void sg_destroy_sampler(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_sampler, smp_id);
_sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
if ((smp->slot.state == SG_RESOURCESTATE_VALID) || (smp->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_sampler(smp);
SOKOL_ASSERT(smp->slot.state == SG_RESOURCESTATE_ALLOC);
}
if (smp->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_sampler(smp);
SOKOL_ASSERT(smp->slot.state == SG_RESOURCESTATE_INITIAL);
}
}
}
SOKOL_API_IMPL void sg_destroy_shader(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_shader, shd_id);
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
if ((shd->slot.state == SG_RESOURCESTATE_VALID) || (shd->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_shader(shd);
SOKOL_ASSERT(shd->slot.state == SG_RESOURCESTATE_ALLOC);
}
if (shd->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_shader(shd);
SOKOL_ASSERT(shd->slot.state == SG_RESOURCESTATE_INITIAL);
}
}
}
SOKOL_API_IMPL void sg_destroy_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_pipeline, pip_id);
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
if ((pip->slot.state == SG_RESOURCESTATE_VALID) || (pip->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_pipeline(pip);
SOKOL_ASSERT(pip->slot.state == SG_RESOURCESTATE_ALLOC);
}
if (pip->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_pipeline(pip);
SOKOL_ASSERT(pip->slot.state == SG_RESOURCESTATE_INITIAL);
}
}
}
SOKOL_API_IMPL void sg_destroy_attachments(sg_attachments atts_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_attachments, atts_id);
_sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
if (atts) {
if ((atts->slot.state == SG_RESOURCESTATE_VALID) || (atts->slot.state == SG_RESOURCESTATE_FAILED)) {
_sg_uninit_attachments(atts);
SOKOL_ASSERT(atts->slot.state == SG_RESOURCESTATE_ALLOC);
}
if (atts->slot.state == SG_RESOURCESTATE_ALLOC) {
_sg_dealloc_attachments(atts);
SOKOL_ASSERT(atts->slot.state == SG_RESOURCESTATE_INITIAL);
}
}
}
SOKOL_API_IMPL void sg_begin_pass(const sg_pass* pass) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(!_sg.cur_pass.valid);
SOKOL_ASSERT(!_sg.cur_pass.in_pass);
SOKOL_ASSERT(pass);
SOKOL_ASSERT((pass->_start_canary == 0) && (pass->_end_canary == 0));
const sg_pass pass_def = _sg_pass_defaults(pass);
if (!_sg_validate_begin_pass(&pass_def)) {
return;
}
if (pass_def.attachments.id != SG_INVALID_ID) {
// an offscreen pass
SOKOL_ASSERT(_sg.cur_pass.atts == 0);
_sg.cur_pass.atts = _sg_lookup_attachments(&_sg.pools, pass_def.attachments.id);
if (0 == _sg.cur_pass.atts) {
_SG_ERROR(BEGINPASS_ATTACHMENT_INVALID);
return;
}
_sg.cur_pass.atts_id = pass_def.attachments;
_sg.cur_pass.width = _sg.cur_pass.atts->cmn.width;
_sg.cur_pass.height = _sg.cur_pass.atts->cmn.height;
} else {
// a swapchain pass
SOKOL_ASSERT(pass_def.swapchain.width > 0);
SOKOL_ASSERT(pass_def.swapchain.height > 0);
SOKOL_ASSERT(pass_def.swapchain.color_format > SG_PIXELFORMAT_NONE);
SOKOL_ASSERT(pass_def.swapchain.sample_count > 0);
_sg.cur_pass.width = pass_def.swapchain.width;
_sg.cur_pass.height = pass_def.swapchain.height;
_sg.cur_pass.swapchain.color_fmt = pass_def.swapchain.color_format;
_sg.cur_pass.swapchain.depth_fmt = pass_def.swapchain.depth_format;
_sg.cur_pass.swapchain.sample_count = pass_def.swapchain.sample_count;
}
_sg.cur_pass.valid = true; // may be overruled by backend begin-pass functions
_sg.cur_pass.in_pass = true;
_sg_begin_pass(&pass_def);
_SG_TRACE_ARGS(begin_pass, &pass_def);
}
SOKOL_API_IMPL void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(_sg.cur_pass.in_pass);
_sg_stats_add(num_apply_viewport, 1);
if (!_sg.cur_pass.valid) {
return;
}
_sg_apply_viewport(x, y, width, height, origin_top_left);
_SG_TRACE_ARGS(apply_viewport, x, y, width, height, origin_top_left);
}
SOKOL_API_IMPL void sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left) {
sg_apply_viewport((int)x, (int)y, (int)width, (int)height, origin_top_left);
}
SOKOL_API_IMPL void sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(_sg.cur_pass.in_pass);
_sg_stats_add(num_apply_scissor_rect, 1);
if (!_sg.cur_pass.valid) {
return;
}
_sg_apply_scissor_rect(x, y, width, height, origin_top_left);
_SG_TRACE_ARGS(apply_scissor_rect, x, y, width, height, origin_top_left);
}
SOKOL_API_IMPL void sg_apply_scissor_rectf(float x, float y, float width, float height, bool origin_top_left) {
sg_apply_scissor_rect((int)x, (int)y, (int)width, (int)height, origin_top_left);
}
SOKOL_API_IMPL void sg_apply_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(_sg.cur_pass.in_pass);
_sg_stats_add(num_apply_pipeline, 1);
if (!_sg_validate_apply_pipeline(pip_id)) {
_sg.next_draw_valid = false;
return;
}
if (!_sg.cur_pass.valid) {
return;
}
_sg.cur_pipeline = pip_id;
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
SOKOL_ASSERT(pip);
_sg.next_draw_valid = (SG_RESOURCESTATE_VALID == pip->slot.state);
SOKOL_ASSERT(pip->shader && (pip->shader->slot.id == pip->cmn.shader_id.id));
_sg_apply_pipeline(pip);
_SG_TRACE_ARGS(apply_pipeline, pip_id);
}
SOKOL_API_IMPL void sg_apply_bindings(const sg_bindings* bindings) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(_sg.cur_pass.in_pass);
SOKOL_ASSERT(bindings);
SOKOL_ASSERT((bindings->_start_canary == 0) && (bindings->_end_canary==0));
_sg_stats_add(num_apply_bindings, 1);
if (!_sg_validate_apply_bindings(bindings)) {
_sg.next_draw_valid = false;
return;
}
if (!_sg.cur_pass.valid) {
return;
}
_sg_bindings_t bnd;
_sg_clear(&bnd, sizeof(bnd));
bnd.pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id);
if (0 == bnd.pip) {
_sg.next_draw_valid = false;
}
for (int i = 0; i < SG_MAX_VERTEX_BUFFERS; i++, bnd.num_vbs++) {
if (bindings->vertex_buffers[i].id) {
bnd.vbs[i] = _sg_lookup_buffer(&_sg.pools, bindings->vertex_buffers[i].id);
bnd.vb_offsets[i] = bindings->vertex_buffer_offsets[i];
if (bnd.vbs[i]) {
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.vbs[i]->slot.state);
_sg.next_draw_valid &= !bnd.vbs[i]->cmn.append_overflow;
} else {
_sg.next_draw_valid = false;
}
} else {
break;
}
}
if (bindings->index_buffer.id) {
bnd.ib = _sg_lookup_buffer(&_sg.pools, bindings->index_buffer.id);
bnd.ib_offset = bindings->index_buffer_offset;
if (bnd.ib) {
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.ib->slot.state);
_sg.next_draw_valid &= !bnd.ib->cmn.append_overflow;
} else {
_sg.next_draw_valid = false;
}
}
for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++, bnd.num_vs_imgs++) {
if (bindings->vs.images[i].id) {
bnd.vs_imgs[i] = _sg_lookup_image(&_sg.pools, bindings->vs.images[i].id);
if (bnd.vs_imgs[i]) {
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.vs_imgs[i]->slot.state);
} else {
_sg.next_draw_valid = false;
}
} else {
break;
}
}
for (int i = 0; i < SG_MAX_SHADERSTAGE_SAMPLERS; i++, bnd.num_vs_smps++) {
if (bindings->vs.samplers[i].id) {
bnd.vs_smps[i] = _sg_lookup_sampler(&_sg.pools, bindings->vs.samplers[i].id);
if (bnd.vs_smps[i]) {
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.vs_smps[i]->slot.state);
} else {
_sg.next_draw_valid = false;
}
} else {
break;
}
}
for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++, bnd.num_vs_sbufs++) {
if (bindings->vs.storage_buffers[i].id) {
bnd.vs_sbufs[i] = _sg_lookup_buffer(&_sg.pools, bindings->vs.storage_buffers[i].id);
if (bnd.vs_sbufs[i]) {
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.vs_sbufs[i]->slot.state);
} else {
_sg.next_draw_valid = false;
}
} else {
break;
}
}
for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++, bnd.num_fs_imgs++) {
if (bindings->fs.images[i].id) {
bnd.fs_imgs[i] = _sg_lookup_image(&_sg.pools, bindings->fs.images[i].id);
if (bnd.fs_imgs[i]) {
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.fs_imgs[i]->slot.state);
} else {
_sg.next_draw_valid = false;
}
} else {
break;
}
}
for (int i = 0; i < SG_MAX_SHADERSTAGE_SAMPLERS; i++, bnd.num_fs_smps++) {
if (bindings->fs.samplers[i].id) {
bnd.fs_smps[i] = _sg_lookup_sampler(&_sg.pools, bindings->fs.samplers[i].id);
if (bnd.fs_smps[i]) {
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.fs_smps[i]->slot.state);
} else {
_sg.next_draw_valid = false;
}
} else {
break;
}
}
for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++, bnd.num_fs_sbufs++) {
if (bindings->fs.storage_buffers[i].id) {
bnd.fs_sbufs[i] = _sg_lookup_buffer(&_sg.pools, bindings->fs.storage_buffers[i].id);
if (bnd.fs_sbufs[i]) {
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.fs_sbufs[i]->slot.state);
} else {
_sg.next_draw_valid = false;
}
} else {
break;
}
}
if (_sg.next_draw_valid) {
_sg.next_draw_valid &= _sg_apply_bindings(&bnd);
_SG_TRACE_ARGS(apply_bindings, bindings);
}
}
SOKOL_API_IMPL void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range* data) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(_sg.cur_pass.in_pass);
SOKOL_ASSERT((stage == SG_SHADERSTAGE_VS) || (stage == SG_SHADERSTAGE_FS));
SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS));
SOKOL_ASSERT(data && data->ptr && (data->size > 0));
_sg_stats_add(num_apply_uniforms, 1);
_sg_stats_add(size_apply_uniforms, (uint32_t)data->size);
if (!_sg_validate_apply_uniforms(stage, ub_index, data)) {
_sg.next_draw_valid = false;
return;
}
if (!_sg.cur_pass.valid) {
return;
}
if (!_sg.next_draw_valid) {
return;
}
_sg_apply_uniforms(stage, ub_index, data);
_SG_TRACE_ARGS(apply_uniforms, stage, ub_index, data);
}
SOKOL_API_IMPL void sg_draw(int base_element, int num_elements, int num_instances) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(_sg.cur_pass.in_pass);
SOKOL_ASSERT(base_element >= 0);
SOKOL_ASSERT(num_elements >= 0);
SOKOL_ASSERT(num_instances >= 0);
_sg_stats_add(num_draw, 1);
if (!_sg.cur_pass.valid) {
return;
}
if (!_sg.next_draw_valid) {
return;
}
/* attempting to draw with zero elements or instances is not technically an
error, but might be handled as an error in the backend API (e.g. on Metal)
*/
if ((0 == num_elements) || (0 == num_instances)) {
return;
}
_sg_draw(base_element, num_elements, num_instances);
_SG_TRACE_ARGS(draw, base_element, num_elements, num_instances);
}
SOKOL_API_IMPL void sg_end_pass(void) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(_sg.cur_pass.in_pass);
_sg_stats_add(num_passes, 1);
// NOTE: don't exit early if !_sg.cur_pass.valid
_sg_end_pass();
_sg.cur_pipeline.id = SG_INVALID_ID;
_sg_clear(&_sg.cur_pass, sizeof(_sg.cur_pass));
_SG_TRACE_NOARGS(end_pass);
}
SOKOL_API_IMPL void sg_commit(void) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(!_sg.cur_pass.valid);
SOKOL_ASSERT(!_sg.cur_pass.in_pass);
_sg_commit();
_sg.stats.frame_index = _sg.frame_index;
_sg.prev_stats = _sg.stats;
_sg_clear(&_sg.stats, sizeof(_sg.stats));
_sg_notify_commit_listeners();
_SG_TRACE_NOARGS(commit);
_sg.frame_index++;
}
SOKOL_API_IMPL void sg_reset_state_cache(void) {
SOKOL_ASSERT(_sg.valid);
_sg_reset_state_cache();
_SG_TRACE_NOARGS(reset_state_cache);
}
SOKOL_API_IMPL void sg_update_buffer(sg_buffer buf_id, const sg_range* data) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(data && data->ptr && (data->size > 0));
_sg_stats_add(num_update_buffer, 1);
_sg_stats_add(size_update_buffer, (uint32_t)data->size);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if ((data->size > 0) && buf && (buf->slot.state == SG_RESOURCESTATE_VALID)) {
if (_sg_validate_update_buffer(buf, data)) {
SOKOL_ASSERT(data->size <= (size_t)buf->cmn.size);
// only one update allowed per buffer and frame
SOKOL_ASSERT(buf->cmn.update_frame_index != _sg.frame_index);
// update and append on same buffer in same frame not allowed
SOKOL_ASSERT(buf->cmn.append_frame_index != _sg.frame_index);
_sg_update_buffer(buf, data);
buf->cmn.update_frame_index = _sg.frame_index;
}
}
_SG_TRACE_ARGS(update_buffer, buf_id, data);
}
SOKOL_API_IMPL int sg_append_buffer(sg_buffer buf_id, const sg_range* data) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(data && data->ptr);
_sg_stats_add(num_append_buffer, 1);
_sg_stats_add(size_append_buffer, (uint32_t)data->size);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
int result;
if (buf) {
// rewind append cursor in a new frame
if (buf->cmn.append_frame_index != _sg.frame_index) {
buf->cmn.append_pos = 0;
buf->cmn.append_overflow = false;
}
if (((size_t)buf->cmn.append_pos + data->size) > (size_t)buf->cmn.size) {
buf->cmn.append_overflow = true;
}
const int start_pos = buf->cmn.append_pos;
// NOTE: the multiple-of-4 requirement for the buffer offset is coming
// from WebGPU, but we want identical behaviour between backends
SOKOL_ASSERT(_sg_multiple_u64((uint64_t)start_pos, 4));
if (buf->slot.state == SG_RESOURCESTATE_VALID) {
if (_sg_validate_append_buffer(buf, data)) {
if (!buf->cmn.append_overflow && (data->size > 0)) {
// update and append on same buffer in same frame not allowed
SOKOL_ASSERT(buf->cmn.update_frame_index != _sg.frame_index);
_sg_append_buffer(buf, data, buf->cmn.append_frame_index != _sg.frame_index);
buf->cmn.append_pos += (int) _sg_roundup_u64(data->size, 4);
buf->cmn.append_frame_index = _sg.frame_index;
}
}
}
result = start_pos;
} else {
// FIXME: should we return -1 here?
result = 0;
}
_SG_TRACE_ARGS(append_buffer, buf_id, data, result);
return result;
}
SOKOL_API_IMPL bool sg_query_buffer_overflow(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
bool result = buf ? buf->cmn.append_overflow : false;
return result;
}
SOKOL_API_IMPL bool sg_query_buffer_will_overflow(sg_buffer buf_id, size_t size) {
SOKOL_ASSERT(_sg.valid);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
bool result = false;
if (buf) {
int append_pos = buf->cmn.append_pos;
// rewind append cursor in a new frame
if (buf->cmn.append_frame_index != _sg.frame_index) {
append_pos = 0;
}
if ((append_pos + _sg_roundup((int)size, 4)) > buf->cmn.size) {
result = true;
}
}
return result;
}
SOKOL_API_IMPL void sg_update_image(sg_image img_id, const sg_image_data* data) {
SOKOL_ASSERT(_sg.valid);
_sg_stats_add(num_update_image, 1);
for (int face_index = 0; face_index < SG_CUBEFACE_NUM; face_index++) {
for (int mip_index = 0; mip_index < SG_MAX_MIPMAPS; mip_index++) {
if (data->subimage[face_index][mip_index].size == 0) {
break;
}
_sg_stats_add(size_update_image, (uint32_t)data->subimage[face_index][mip_index].size);
}
}
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img && img->slot.state == SG_RESOURCESTATE_VALID) {
if (_sg_validate_update_image(img, data)) {
SOKOL_ASSERT(img->cmn.upd_frame_index != _sg.frame_index);
_sg_update_image(img, data);
img->cmn.upd_frame_index = _sg.frame_index;
}
}
_SG_TRACE_ARGS(update_image, img_id, data);
}
SOKOL_API_IMPL void sg_push_debug_group(const char* name) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(name);
_sg_push_debug_group(name);
_SG_TRACE_ARGS(push_debug_group, name);
}
SOKOL_API_IMPL void sg_pop_debug_group(void) {
SOKOL_ASSERT(_sg.valid);
_sg_pop_debug_group();
_SG_TRACE_NOARGS(pop_debug_group);
}
SOKOL_API_IMPL bool sg_add_commit_listener(sg_commit_listener listener) {
SOKOL_ASSERT(_sg.valid);
return _sg_add_commit_listener(&listener);
}
SOKOL_API_IMPL bool sg_remove_commit_listener(sg_commit_listener listener) {
SOKOL_ASSERT(_sg.valid);
return _sg_remove_commit_listener(&listener);
}
SOKOL_API_IMPL void sg_enable_frame_stats(void) {
SOKOL_ASSERT(_sg.valid);
_sg.stats_enabled = true;
}
SOKOL_API_IMPL void sg_disable_frame_stats(void) {
SOKOL_ASSERT(_sg.valid);
_sg.stats_enabled = false;
}
SOKOL_API_IMPL bool sg_frame_stats_enabled(void) {
return _sg.stats_enabled;
}
SOKOL_API_IMPL sg_buffer_info sg_query_buffer_info(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
sg_buffer_info info;
_sg_clear(&info, sizeof(info));
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
info.slot.state = buf->slot.state;
info.slot.res_id = buf->slot.id;
info.update_frame_index = buf->cmn.update_frame_index;
info.append_frame_index = buf->cmn.append_frame_index;
info.append_pos = buf->cmn.append_pos;
info.append_overflow = buf->cmn.append_overflow;
#if defined(SOKOL_D3D11)
info.num_slots = 1;
info.active_slot = 0;
#else
info.num_slots = buf->cmn.num_slots;
info.active_slot = buf->cmn.active_slot;
#endif
}
return info;
}
SOKOL_API_IMPL sg_image_info sg_query_image_info(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
sg_image_info info;
_sg_clear(&info, sizeof(info));
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
info.slot.state = img->slot.state;
info.slot.res_id = img->slot.id;
info.upd_frame_index = img->cmn.upd_frame_index;
#if defined(SOKOL_D3D11)
info.num_slots = 1;
info.active_slot = 0;
#else
info.num_slots = img->cmn.num_slots;
info.active_slot = img->cmn.active_slot;
#endif
}
return info;
}
SOKOL_API_IMPL sg_sampler_info sg_query_sampler_info(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
sg_sampler_info info;
_sg_clear(&info, sizeof(info));
const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
info.slot.state = smp->slot.state;
info.slot.res_id = smp->slot.id;
}
return info;
}
SOKOL_API_IMPL sg_shader_info sg_query_shader_info(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
sg_shader_info info;
_sg_clear(&info, sizeof(info));
const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
info.slot.state = shd->slot.state;
info.slot.res_id = shd->slot.id;
}
return info;
}
SOKOL_API_IMPL sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
sg_pipeline_info info;
_sg_clear(&info, sizeof(info));
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
info.slot.state = pip->slot.state;
info.slot.res_id = pip->slot.id;
}
return info;
}
SOKOL_API_IMPL sg_attachments_info sg_query_attachments_info(sg_attachments atts_id) {
SOKOL_ASSERT(_sg.valid);
sg_attachments_info info;
_sg_clear(&info, sizeof(info));
const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
if (atts) {
info.slot.state = atts->slot.state;
info.slot.res_id = atts->slot.id;
}
return info;
}
SOKOL_API_IMPL sg_buffer_desc sg_query_buffer_desc(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
sg_buffer_desc desc;
_sg_clear(&desc, sizeof(desc));
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
desc.size = (size_t)buf->cmn.size;
desc.type = buf->cmn.type;
desc.usage = buf->cmn.usage;
}
return desc;
}
SOKOL_API_IMPL sg_image_desc sg_query_image_desc(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
sg_image_desc desc;
_sg_clear(&desc, sizeof(desc));
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
desc.type = img->cmn.type;
desc.render_target = img->cmn.render_target;
desc.width = img->cmn.width;
desc.height = img->cmn.height;
desc.num_slices = img->cmn.num_slices;
desc.num_mipmaps = img->cmn.num_mipmaps;
desc.usage = img->cmn.usage;
desc.pixel_format = img->cmn.pixel_format;
desc.sample_count = img->cmn.sample_count;
}
return desc;
}
SOKOL_API_IMPL sg_sampler_desc sg_query_sampler_desc(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
sg_sampler_desc desc;
_sg_clear(&desc, sizeof(desc));
const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
desc.min_filter = smp->cmn.min_filter;
desc.mag_filter = smp->cmn.mag_filter;
desc.mipmap_filter = smp->cmn.mipmap_filter;
desc.wrap_u = smp->cmn.wrap_u;
desc.wrap_v = smp->cmn.wrap_v;
desc.wrap_w = smp->cmn.wrap_w;
desc.min_lod = smp->cmn.min_lod;
desc.max_lod = smp->cmn.max_lod;
desc.border_color = smp->cmn.border_color;
desc.compare = smp->cmn.compare;
desc.max_anisotropy = smp->cmn.max_anisotropy;
}
return desc;
}
SOKOL_API_IMPL sg_shader_desc sg_query_shader_desc(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
sg_shader_desc desc;
_sg_clear(&desc, sizeof(desc));
const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
for (int stage_idx = 0; stage_idx < SG_NUM_SHADER_STAGES; stage_idx++) {
sg_shader_stage_desc* stage_desc = (stage_idx == 0) ? &desc.vs : &desc.fs;
const _sg_shader_stage_t* stage = &shd->cmn.stage[stage_idx];
for (int ub_idx = 0; ub_idx < stage->num_uniform_blocks; ub_idx++) {
sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_idx];
const _sg_shader_uniform_block_t* ub = &stage->uniform_blocks[ub_idx];
ub_desc->size = ub->size;
}
for (int img_idx = 0; img_idx < stage->num_images; img_idx++) {
sg_shader_image_desc* img_desc = &stage_desc->images[img_idx];
const _sg_shader_image_t* img = &stage->images[img_idx];
img_desc->used = true;
img_desc->image_type = img->image_type;
img_desc->sample_type = img->sample_type;
img_desc->multisampled = img->multisampled;
}
for (int smp_idx = 0; smp_idx < stage->num_samplers; smp_idx++) {
sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[smp_idx];
const _sg_shader_sampler_t* smp = &stage->samplers[smp_idx];
smp_desc->used = true;
smp_desc->sampler_type = smp->sampler_type;
}
for (int img_smp_idx = 0; img_smp_idx < stage->num_image_samplers; img_smp_idx++) {
sg_shader_image_sampler_pair_desc* img_smp_desc = &stage_desc->image_sampler_pairs[img_smp_idx];
const _sg_shader_image_sampler_t* img_smp = &stage->image_samplers[img_smp_idx];
img_smp_desc->used = true;
img_smp_desc->image_slot = img_smp->image_slot;
img_smp_desc->sampler_slot = img_smp->sampler_slot;
img_smp_desc->glsl_name = 0;
}
}
}
return desc;
}
SOKOL_API_IMPL sg_pipeline_desc sg_query_pipeline_desc(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
sg_pipeline_desc desc;
_sg_clear(&desc, sizeof(desc));
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
desc.shader = pip->cmn.shader_id;
desc.layout = pip->cmn.layout;
desc.depth = pip->cmn.depth;
desc.stencil = pip->cmn.stencil;
desc.color_count = pip->cmn.color_count;
for (int i = 0; i < pip->cmn.color_count; i++) {
desc.colors[i] = pip->cmn.colors[i];
}
desc.primitive_type = pip->cmn.primitive_type;
desc.index_type = pip->cmn.index_type;
desc.cull_mode = pip->cmn.cull_mode;
desc.face_winding = pip->cmn.face_winding;
desc.sample_count = pip->cmn.sample_count;
desc.blend_color = pip->cmn.blend_color;
desc.alpha_to_coverage_enabled = pip->cmn.alpha_to_coverage_enabled;
}
return desc;
}
SOKOL_API_IMPL sg_attachments_desc sg_query_attachments_desc(sg_attachments atts_id) {
SOKOL_ASSERT(_sg.valid);
sg_attachments_desc desc;
_sg_clear(&desc, sizeof(desc));
const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
if (atts) {
for (int i = 0; i < atts->cmn.num_colors; i++) {
desc.colors[i].image = atts->cmn.colors[i].image_id;
desc.colors[i].mip_level = atts->cmn.colors[i].mip_level;
desc.colors[i].slice = atts->cmn.colors[i].slice;
}
desc.depth_stencil.image = atts->cmn.depth_stencil.image_id;
desc.depth_stencil.mip_level = atts->cmn.depth_stencil.mip_level;
desc.depth_stencil.slice = atts->cmn.depth_stencil.slice;
}
return desc;
}
SOKOL_API_IMPL sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_buffer_desc_defaults(desc);
}
SOKOL_API_IMPL sg_image_desc sg_query_image_defaults(const sg_image_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_image_desc_defaults(desc);
}
SOKOL_API_IMPL sg_sampler_desc sg_query_sampler_defaults(const sg_sampler_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_sampler_desc_defaults(desc);
}
SOKOL_API_IMPL sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_shader_desc_defaults(desc);
}
SOKOL_API_IMPL sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_pipeline_desc_defaults(desc);
}
SOKOL_API_IMPL sg_attachments_desc sg_query_attachments_defaults(const sg_attachments_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_attachments_desc_defaults(desc);
}
SOKOL_API_IMPL const void* sg_d3d11_device(void) {
#if defined(SOKOL_D3D11)
return (const void*) _sg.d3d11.dev;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sg_d3d11_device_context(void) {
#if defined(SOKOL_D3D11)
return (const void*) _sg.d3d11.ctx;
#else
return 0;
#endif
}
SOKOL_API_IMPL sg_d3d11_buffer_info sg_d3d11_query_buffer_info(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
sg_d3d11_buffer_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_D3D11)
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
res.buf = (const void*) buf->d3d11.buf;
}
#else
_SOKOL_UNUSED(buf_id);
#endif
return res;
}
SOKOL_API_IMPL sg_d3d11_image_info sg_d3d11_query_image_info(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
sg_d3d11_image_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_D3D11)
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
res.tex2d = (const void*) img->d3d11.tex2d;
res.tex3d = (const void*) img->d3d11.tex3d;
res.res = (const void*) img->d3d11.res;
res.srv = (const void*) img->d3d11.srv;
}
#else
_SOKOL_UNUSED(img_id);
#endif
return res;
}
SOKOL_API_IMPL sg_d3d11_sampler_info sg_d3d11_query_sampler_info(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
sg_d3d11_sampler_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_D3D11)
const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
res.smp = (const void*) smp->d3d11.smp;
}
#else
_SOKOL_UNUSED(smp_id);
#endif
return res;
}
SOKOL_API_IMPL sg_d3d11_shader_info sg_d3d11_query_shader_info(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
sg_d3d11_shader_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_D3D11)
const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
for (int i = 0; i < SG_MAX_SHADERSTAGE_UBS; i++) {
res.vs_cbufs[i] = (const void*) shd->d3d11.stage[SG_SHADERSTAGE_VS].cbufs[i];
res.fs_cbufs[i] = (const void*) shd->d3d11.stage[SG_SHADERSTAGE_FS].cbufs[i];
}
res.vs = (const void*) shd->d3d11.vs;
res.fs = (const void*) shd->d3d11.fs;
}
#else
_SOKOL_UNUSED(shd_id);
#endif
return res;
}
SOKOL_API_IMPL sg_d3d11_pipeline_info sg_d3d11_query_pipeline_info(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
sg_d3d11_pipeline_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_D3D11)
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
res.il = (const void*) pip->d3d11.il;
res.rs = (const void*) pip->d3d11.rs;
res.dss = (const void*) pip->d3d11.dss;
res.bs = (const void*) pip->d3d11.bs;
}
#else
_SOKOL_UNUSED(pip_id);
#endif
return res;
}
SOKOL_API_IMPL sg_d3d11_attachments_info sg_d3d11_query_attachments_info(sg_attachments atts_id) {
SOKOL_ASSERT(_sg.valid);
sg_d3d11_attachments_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_D3D11)
const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
if (atts) {
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
res.color_rtv[i] = (const void*) atts->d3d11.colors[i].view.rtv;
res.resolve_rtv[i] = (const void*) atts->d3d11.resolves[i].view.rtv;
}
res.dsv = (const void*) atts->d3d11.depth_stencil.view.dsv;
}
#else
_SOKOL_UNUSED(atts_id);
#endif
return res;
}
SOKOL_API_IMPL const void* sg_mtl_device(void) {
#if defined(SOKOL_METAL)
if (nil != _sg.mtl.device) {
return (__bridge const void*) _sg.mtl.device;
} else {
return 0;
}
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sg_mtl_render_command_encoder(void) {
#if defined(SOKOL_METAL)
if (nil != _sg.mtl.cmd_encoder) {
return (__bridge const void*) _sg.mtl.cmd_encoder;
} else {
return 0;
}
#else
return 0;
#endif
}
SOKOL_API_IMPL sg_mtl_buffer_info sg_mtl_query_buffer_info(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
sg_mtl_buffer_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_METAL)
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
if (buf->mtl.buf[i] != 0) {
res.buf[i] = (__bridge void*) _sg_mtl_id(buf->mtl.buf[i]);
}
}
res.active_slot = buf->cmn.active_slot;
}
#else
_SOKOL_UNUSED(buf_id);
#endif
return res;
}
SOKOL_API_IMPL sg_mtl_image_info sg_mtl_query_image_info(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
sg_mtl_image_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_METAL)
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
if (img->mtl.tex[i] != 0) {
res.tex[i] = (__bridge void*) _sg_mtl_id(img->mtl.tex[i]);
}
}
res.active_slot = img->cmn.active_slot;
}
#else
_SOKOL_UNUSED(img_id);
#endif
return res;
}
SOKOL_API_IMPL sg_mtl_sampler_info sg_mtl_query_sampler_info(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
sg_mtl_sampler_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_METAL)
const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
if (smp->mtl.sampler_state != 0) {
res.smp = (__bridge void*) _sg_mtl_id(smp->mtl.sampler_state);
}
}
#else
_SOKOL_UNUSED(smp_id);
#endif
return res;
}
SOKOL_API_IMPL sg_mtl_shader_info sg_mtl_query_shader_info(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
sg_mtl_shader_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_METAL)
const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
const int vs_lib = shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_lib;
const int vs_func = shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func;
const int fs_lib = shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_lib;
const int fs_func = shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func;
if (vs_lib != 0) {
res.vs_lib = (__bridge void*) _sg_mtl_id(vs_lib);
}
if (fs_lib != 0) {
res.fs_lib = (__bridge void*) _sg_mtl_id(fs_lib);
}
if (vs_func != 0) {
res.vs_func = (__bridge void*) _sg_mtl_id(vs_func);
}
if (fs_func != 0) {
res.fs_func = (__bridge void*) _sg_mtl_id(fs_func);
}
}
#else
_SOKOL_UNUSED(shd_id);
#endif
return res;
}
SOKOL_API_IMPL sg_mtl_pipeline_info sg_mtl_query_pipeline_info(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
sg_mtl_pipeline_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_METAL)
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
if (pip->mtl.rps != 0) {
res.rps = (__bridge void*) _sg_mtl_id(pip->mtl.rps);
}
if (pip->mtl.dss != 0) {
res.dss = (__bridge void*) _sg_mtl_id(pip->mtl.dss);
}
}
#else
_SOKOL_UNUSED(pip_id);
#endif
return res;
}
SOKOL_API_IMPL const void* sg_wgpu_device(void) {
#if defined(SOKOL_WGPU)
return (const void*) _sg.wgpu.dev;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sg_wgpu_queue(void) {
#if defined(SOKOL_WGPU)
return (const void*) _sg.wgpu.queue;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sg_wgpu_command_encoder(void) {
#if defined(SOKOL_WGPU)
return (const void*) _sg.wgpu.cmd_enc;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sg_wgpu_render_pass_encoder(void) {
#if defined(SOKOL_WGPU)
return (const void*) _sg.wgpu.pass_enc;
#else
return 0;
#endif
}
SOKOL_API_IMPL sg_wgpu_buffer_info sg_wgpu_query_buffer_info(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
sg_wgpu_buffer_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_WGPU)
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
res.buf = (const void*) buf->wgpu.buf;
}
#else
_SOKOL_UNUSED(buf_id);
#endif
return res;
}
SOKOL_API_IMPL sg_wgpu_image_info sg_wgpu_query_image_info(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
sg_wgpu_image_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_WGPU)
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
res.tex = (const void*) img->wgpu.tex;
res.view = (const void*) img->wgpu.view;
}
#else
_SOKOL_UNUSED(img_id);
#endif
return res;
}
SOKOL_API_IMPL sg_wgpu_sampler_info sg_wgpu_query_sampler_info(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
sg_wgpu_sampler_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_WGPU)
const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
res.smp = (const void*) smp->wgpu.smp;
}
#else
_SOKOL_UNUSED(smp_id);
#endif
return res;
}
SOKOL_API_IMPL sg_wgpu_shader_info sg_wgpu_query_shader_info(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
sg_wgpu_shader_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_WGPU)
const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
res.vs_mod = (const void*) shd->wgpu.stage[SG_SHADERSTAGE_VS].module;
res.fs_mod = (const void*) shd->wgpu.stage[SG_SHADERSTAGE_FS].module;
res.bgl = (const void*) shd->wgpu.bind_group_layout;
}
#else
_SOKOL_UNUSED(shd_id);
#endif
return res;
}
SOKOL_API_IMPL sg_wgpu_pipeline_info sg_wgpu_query_pipeline_info(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
sg_wgpu_pipeline_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_WGPU)
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
res.pip = (const void*) pip->wgpu.pip;
}
#else
_SOKOL_UNUSED(pip_id);
#endif
return res;
}
SOKOL_API_IMPL sg_wgpu_attachments_info sg_wgpu_query_attachments_info(sg_attachments atts_id) {
SOKOL_ASSERT(_sg.valid);
sg_wgpu_attachments_info res;
_sg_clear(&res, sizeof(res));
#if defined(SOKOL_WGPU)
const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
if (atts) {
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
res.color_view[i] = (const void*) atts->wgpu.colors[i].view;
res.resolve_view[i] = (const void*) atts->wgpu.resolves[i].view;
}
res.ds_view = (const void*) atts->wgpu.depth_stencil.view;
}
#else
_SOKOL_UNUSED(atts_id);
#endif
return res;
}
SOKOL_API_IMPL sg_gl_buffer_info sg_gl_query_buffer_info(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
sg_gl_buffer_info res;
_sg_clear(&res, sizeof(res));
#if defined(_SOKOL_ANY_GL)
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
res.buf[i] = buf->gl.buf[i];
}
res.active_slot = buf->cmn.active_slot;
}
#else
_SOKOL_UNUSED(buf_id);
#endif
return res;
}
SOKOL_API_IMPL sg_gl_image_info sg_gl_query_image_info(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
sg_gl_image_info res;
_sg_clear(&res, sizeof(res));
#if defined(_SOKOL_ANY_GL)
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
res.tex[i] = img->gl.tex[i];
}
res.tex_target = img->gl.target;
res.msaa_render_buffer = img->gl.msaa_render_buffer;
res.active_slot = img->cmn.active_slot;
}
#else
_SOKOL_UNUSED(img_id);
#endif
return res;
}
SOKOL_API_IMPL sg_gl_sampler_info sg_gl_query_sampler_info(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
sg_gl_sampler_info res;
_sg_clear(&res, sizeof(res));
#if defined(_SOKOL_ANY_GL)
const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id);
if (smp) {
res.smp = smp->gl.smp;
}
#else
_SOKOL_UNUSED(smp_id);
#endif
return res;
}
SOKOL_API_IMPL sg_gl_shader_info sg_gl_query_shader_info(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
sg_gl_shader_info res;
_sg_clear(&res, sizeof(res));
#if defined(_SOKOL_ANY_GL)
const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
res.prog = shd->gl.prog;
}
#else
_SOKOL_UNUSED(shd_id);
#endif
return res;
}
SOKOL_API_IMPL sg_gl_attachments_info sg_gl_query_attachments_info(sg_attachments atts_id) {
SOKOL_ASSERT(_sg.valid);
sg_gl_attachments_info res;
_sg_clear(&res, sizeof(res));
#if defined(_SOKOL_ANY_GL)
const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id);
if (atts) {
res.framebuffer = atts->gl.fb;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
res.msaa_resolve_framebuffer[i] = atts->gl.msaa_resolve_framebuffer[i];
}
}
#else
_SOKOL_UNUSED(atts_id);
#endif
return res;
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif // SOKOL_GFX_IMPL
|
0 |
repos
|
repos/sokol/README.md
|
<p align="center">
<img src="assets/logo_full_large.png" style="width: 60%" /><br/><br/>Simple
<a href="https://github.com/nothings/stb/blob/master/docs/stb_howto.txt">STB-style</a>
cross-platform libraries for C and C++, written in C.<br/><br/>
</p>
# Sokol
[**See what's new**](https://github.com/floooh/sokol/blob/master/CHANGELOG.md) (**02-Sep-2024** in sokol_gfx.h, SG_FILTER_NONE has been removed)
[](/../../actions/workflows/main.yml) [](/../../actions/workflows/gen_bindings.yml) [](https://github.com/floooh/sokol-zig/actions/workflows/main.yml) [](https://github.com/floooh/sokol-nim/actions/workflows/main.yml) [](https://github.com/floooh/sokol-odin/actions/workflows/main.yml)[](https://github.com/floooh/sokol-rust/actions/workflows/main.yml)[](https://github.com/kassane/sokol-d/actions/workflows/build.yml)
## Examples and Related Projects
- [Live Samples](https://floooh.github.io/sokol-html5/index.html) via WASM ([source](https://github.com/floooh/sokol-samples))
- [Doom Shareware](https://floooh.github.io/doom-sokol/) ported to the Sokol headers ([source](https://github.com/floooh/doom-sokol))
- [Everybody Wants to Crank the World](https://aras-p.github.io/demo-pd-cranktheworld/) demo by
Aras Pranckevičius, PC/web port via sokol ([source](https://github.com/aras-p/demo-pd-cranktheworld)).
- [sokol_gp.h](https://github.com/edubart/sokol_gp) a 2D shape drawing library on top of sokol_gfx.h
- [LearnOpenGL examples ported to sokol-gfx](https://zeromake.github.io/learnopengl-examples/) ([git repo](https://github.com/zeromake/learnopengl-examples))
- [Dear ImGui starterkit](https://github.com/floooh/cimgui-sokol-starterkit) a self-contained starterkit for writing Dear ImGui apps in C.
- [qoiview](https://github.com/floooh/qoiview) a basic viewer for the new QOI image file format
- [Tiny 8-bit emulators](https://floooh.github.io/tiny8bit/)
- A 'single-file' [Pacman clone in C99](https://github.com/floooh/pacman.c/), also available in [Zig](https://github.com/floooh/pacman.zig/)
- [Solar Storm](https://store.steampowered.com/app/2754920/Solar_Storm/), a turn-based scifi artillery game built with Odin and Sokol, released on Steam.
- [Spanking Runners (Samogonki)](https://store.steampowered.com/app/2599800/Spanking_Runners/), arcade racing in a bright and unusual world, released on Steam.
- [MEG-4](https://bztsrc.gitlab.io/meg4) a virtual fantasy console emulator in C89, ported to sokol
- A [Minigolf game](https://mgerdes.github.io/minigolf.html) ([source](https://github.com/mgerdes/minigolf)).
- [hIghQube](https://github.com/RuiVarela/hIghQube) A game demo that used sokol rendering extensively
- [Senos](https://github.com/RuiVarela/Senos) A music app that uses sokol as backend
- ['Dealer's Dungeon'](https://dealers-dungeon.com/demo/) ([lower graphics quality](https://dealers-dungeon.com/demo/?q=3),
[source](https://github.com/bqqbarbhg/spear))
- [Command line tools](https://github.com/floooh/sokol-tools) (shader compiler)
- [How to build without a build system](https://github.com/floooh/sokol-samples#how-to-build-without-a-build-system):
useful details for integrating the Sokol headers into your own project with your favourite C/C++ build system
## Core libraries
- [**sokol\_gfx.h**](https://github.com/floooh/sokol/blob/master/sokol_gfx.h): 3D-API wrapper (GL/GLES3/WebGL2 + Metal + D3D11 + WebGPU)
- [**sokol\_app.h**](https://github.com/floooh/sokol/blob/master/sokol_app.h): app framework wrapper (entry + window + 3D-context + input)
- [**sokol\_time.h**](https://github.com/floooh/sokol/blob/master/sokol_time.h): time measurement
- [**sokol\_audio.h**](https://github.com/floooh/sokol/blob/master/sokol_audio.h): minimal buffer-streaming audio playback
- [**sokol\_fetch.h**](https://github.com/floooh/sokol/blob/master/sokol_fetch.h): asynchronous data streaming from HTTP and local filesystem
- [**sokol\_args.h**](https://github.com/floooh/sokol/blob/master/sokol_args.h): unified cmdline/URL arg parser for web and native apps
- [**sokol\_log.h**](https://github.com/floooh/sokol/blob/master/sokol_log.h): provides a standard logging callback for the other sokol headers
## Utility libraries
- [**sokol\_imgui.h**](https://github.com/floooh/sokol/blob/master/util/sokol_imgui.h): sokol_gfx.h rendering backend for [Dear ImGui](https://github.com/ocornut/imgui)
- [**sokol\_nuklear.h**](https://github.com/floooh/sokol/blob/master/util/sokol_nuklear.h): sokol_gfx.h rendering backend for [Nuklear](https://github.com/Immediate-Mode-UI/Nuklear)
- [**sokol\_gl.h**](https://github.com/floooh/sokol/blob/master/util/sokol_gl.h): OpenGL 1.x style immediate-mode rendering API on top of sokol_gfx.h
- [**sokol\_fontstash.h**](https://github.com/floooh/sokol/blob/master/util/sokol_fontstash.h): sokol_gl.h rendering backend for [fontstash](https://github.com/memononen/fontstash)
- [**sokol\_gfx\_imgui.h**](https://github.com/floooh/sokol/blob/master/util/sokol_gfx_imgui.h): debug-inspection UI for sokol_gfx.h (implemented with Dear ImGui)
- [**sokol\_debugtext.h**](https://github.com/floooh/sokol/blob/master/util/sokol_debugtext.h): a simple text renderer using vintage home computer fonts
- [**sokol\_memtrack.h**](https://github.com/floooh/sokol/blob/master/util/sokol_memtrack.h): easily track memory allocations in sokol headers
- [**sokol\_shape.h**](https://github.com/floooh/sokol/blob/master/util/sokol_shape.h): generate simple shapes and plug them into sokol-gfx resource creation structs
- [**sokol\_color.h**](https://github.com/floooh/sokol/blob/master/util/sokol_color.h): X11 style color constants and functions for creating sg_color objects
- [**sokol\_spine.h**](https://github.com/floooh/sokol/blob/master/util/sokol_spine.h): a sokol-style wrapper around the Spine C runtime (http://en.esotericsoftware.com/spine-in-depth)
## 'Official' Language Bindings
These are automatically updated on changes to the C headers:
- [sokol-zig](https://github.com/floooh/sokol-zig)
- [sokol-odin](https://github.com/floooh/sokol-odin)
- [sokol-nim](https://github.com/floooh/sokol-nim)
- [sokol-rust](https://github.com/floooh/sokol-rust)
- [sokol-d](https://github.com/kassane/sokol-d)
- [sokol-jai](https://github.com/colinbellino/sokol-jai)
## Notes
WebAssembly is a 'first-class citizen', one important motivation for the
Sokol headers is to provide a collection of cross-platform APIs with a
minimal footprint on the web platform while still being useful.
The core headers are standalone and can be used independently from each other.
### Why C:
- easier integration with other languages
- easier integration into other projects
- adds only minimal size overhead to executables
A blog post with more background info: [A Tour of sokol_gfx.h](http://floooh.github.io/2017/07/29/sokol-gfx-tour.html)
# sokol_gfx.h:
- simple, modern wrapper around GLES3/WebGL2, GL3.3, D3D11, Metal, and WebGPU
- buffers, images, shaders, pipeline-state-objects and render-passes
- does *not* handle window creation or 3D API context initialization
- does *not* provide shader dialect cross-translation (**BUT** there's now an 'official' shader-cross-compiler solution which
seamlessly integrates with sokol_gfx.h and IDEs: [see here for details](https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md)
# sokol_app.h
A minimal cross-platform application-wrapper library:
- unified application entry
- single window or canvas for 3D rendering
- 3D context initialization
- event-based keyboard, mouse and touch input
- supported platforms: Win32, MacOS, Linux (X11), iOS, WASM, Android, UWP
- supported 3D-APIs: GL3.3 (GLX/WGL), Metal, D3D11, GLES3/WebGL2
The vanilla Hello-Triangle using sokol_gfx.h, sokol_app.h and the
sokol-shdc shader compiler (shader code not shown):
```c
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_log.h"
#include "sokol_glue.h"
#include "triangle-sapp.glsl.h"
static struct {
sg_pipeline pip;
sg_bindings bind;
sg_pass_action pass_action;
} state;
static void init(void) {
sg_setup(&(sg_desc){
.environment = sglue_environment(),
.logger.func = slog_func,
});
float vertices[] = {
0.0f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f
};
state.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.data = SG_RANGE(vertices),
});
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
.shader = sg_make_shader(triangle_shader_desc(sg_query_backend())),
.layout = {
.attrs = {
[ATTR_vs_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_color0].format = SG_VERTEXFORMAT_FLOAT4
}
},
});
state.pass_action = (sg_pass_action) {
.colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.0f, 0.0f, 0.0f, 1.0f } }
};
}
void frame(void) {
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_draw(0, 3, 1);
sg_end_pass();
sg_commit();
}
void cleanup(void) {
sg_shutdown();
}
sapp_desc sokol_main(int argc, char* argv[]) {
(void)argc; (void)argv;
return (sapp_desc){
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.width = 640,
.height = 480,
.window_title = "Triangle",
.icon.sokol_default = true,
.logger.func = slog_func,
};
}
```
# sokol_audio.h
A minimal audio-streaming API:
- you provide a mono- or stereo-stream of 32-bit float samples which sokol_audio.h forwards into platform-specific backends
- two ways to provide the data:
1. directly fill backend audio buffer from your callback function running in the audio thread
2. alternatively push small packets of audio data from your main loop,
or a separate thread created by you
- platform backends:
- Windows: WASAPI
- macOS/iOS: CoreAudio
- Linux: ALSA
- emscripten: WebAudio + ScriptProcessorNode (doesn't use the emscripten-provided OpenAL or SDL Audio wrappers)
A simple mono square-wave generator using the callback model:
```c
// the sample callback, running in audio thread
static void stream_cb(float* buffer, int num_frames, int num_channels) {
assert(1 == num_channels);
static uint32_t count = 0;
for (int i = 0; i < num_frames; i++) {
buffer[i] = (count++ & (1<<3)) ? 0.5f : -0.5f;
}
}
int main() {
// init sokol-audio with default params
saudio_setup(&(saudio_desc){
.stream_cb = stream_cb,
.logger.func = slog_func,
});
// run main loop
...
// shutdown sokol-audio
saudio_shutdown();
return 0;
```
The same code using the push-model
```c
#define BUF_SIZE (32)
int main() {
// init sokol-audio with default params, no callback
saudio_setup(&(saudio_desc){
.logger.func = slog_func,
});
assert(saudio_channels() == 1);
// a small intermediate buffer so we don't need to push
// individual samples, which would be quite inefficient
float buf[BUF_SIZE];
int buf_pos = 0;
uint32_t count = 0;
// push samples from main loop
bool done = false;
while (!done) {
// generate and push audio samples...
int num_frames = saudio_expect();
for (int i = 0; i < num_frames; i++) {
// simple square wave generator
buf[buf_pos++] = (count++ & (1<<3)) ? 0.5f : -0.5f;
if (buf_pos == BUF_SIZE) {
buf_pos = 0;
saudio_push(buf, BUF_SIZE);
}
}
// handle other per-frame stuff...
...
}
// shutdown sokol-audio
saudio_shutdown();
return 0;
}
```
# sokol_fetch.h
Load entire files, or stream data asynchronously over HTTP (emscripten/wasm)
or the local filesystem (all native platforms).
Simple C99 example loading a file into a static buffer:
```c
#include "sokol_fetch.h"
#include "sokol_log.h"
static void response_callback(const sfetch_response*);
#define MAX_FILE_SIZE (1024*1024)
static uint8_t buffer[MAX_FILE_SIZE];
// application init
static void init(void) {
...
// setup sokol-fetch with default config:
sfetch_setup(&(sfetch_desc_t){ .logger.func = slog_func });
// start loading a file into a statically allocated buffer:
sfetch_send(&(sfetch_request_t){
.path = "hello_world.txt",
.callback = response_callback
.buffer_ptr = buffer,
.buffer_size = sizeof(buffer)
});
}
// per frame...
static void frame(void) {
...
// need to call sfetch_dowork() once per frame to 'turn the gears':
sfetch_dowork();
...
}
// the response callback is where the interesting stuff happens:
static void response_callback(const sfetch_response_t* response) {
if (response->fetched) {
// data has been loaded into the provided buffer, do something
// with the data...
const void* data = response->buffer_ptr;
uint64_t data_size = response->fetched_size;
}
// the finished flag is set both on success and failure
if (response->failed) {
// oops, something went wrong
switch (response->error_code) {
SFETCH_ERROR_FILE_NOT_FOUND: ...
SFETCH_ERROR_BUFFER_TOO_SMALL: ...
...
}
}
}
// application shutdown
static void shutdown(void) {
...
sfetch_shutdown();
...
}
```
# sokol_time.h:
Simple cross-platform time measurement:
```c
#include "sokol_time.h"
...
/* initialize sokol_time */
stm_setup();
/* take start timestamp */
uint64_t start = stm_now();
...some code to measure...
/* compute elapsed time */
uint64_t elapsed = stm_since(start);
/* convert to time units */
double seconds = stm_sec(elapsed);
double milliseconds = stm_ms(elapsed);
double microseconds = stm_us(elapsed);
double nanoseconds = stm_ns(elapsed);
/* difference between 2 time stamps */
uint64_t start = stm_now();
...
uint64_t end = stm_now();
uint64_t elapsed = stm_diff(end, start);
/* compute a 'lap time' (e.g. for fps) */
uint64_t last_time = 0;
while (!done) {
...render something...
double frame_time_ms = stm_ms(stm_laptime(&last_time));
}
```
# sokol_args.h
Unified argument parsing for web and native apps. Uses argc/argv on native
platforms and the URL query string on the web.
Example URL with one arg:
https://floooh.github.io/tiny8bit/kc85.html?type=kc85_4
The same as command line app:
> kc85 type=kc85_4
Parsed like this:
```c
#include "sokol_args.h"
int main(int argc, char* argv[]) {
sargs_setup(&(sargs_desc){ .argc=argc, .argv=argv });
if (sargs_exists("type")) {
if (sargs_equals("type", "kc85_4")) {
// start as KC85/4
}
else if (sargs_equals("type", "kc85_3")) {
// start as KC85/3
}
else {
// start as KC85/2
}
}
sargs_shutdown();
return 0;
}
```
See the sokol_args.h header for a more complete documentation, and the [Tiny
Emulators](https://floooh.github.io/tiny8bit/) for more interesting usage examples.
|
0 |
repos
|
repos/sokol/sokol_audio.h
|
#if defined(SOKOL_IMPL) && !defined(SOKOL_AUDIO_IMPL)
#define SOKOL_AUDIO_IMPL
#endif
#ifndef SOKOL_AUDIO_INCLUDED
/*
sokol_audio.h -- cross-platform audio-streaming API
Project URL: https://github.com/floooh/sokol
Do this:
#define SOKOL_IMPL or
#define SOKOL_AUDIO_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
Optionally provide the following defines with your own implementations:
SOKOL_DUMMY_BACKEND - use a dummy backend
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_AUDIO_API_DECL- public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_AUDIO_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
SAUDIO_RING_MAX_SLOTS - max number of slots in the push-audio ring buffer (default 1024)
SAUDIO_OSX_USE_SYSTEM_HEADERS - define this to force inclusion of system headers on
macOS instead of using embedded CoreAudio declarations
SAUDIO_ANDROID_AAUDIO - on Android, select the AAudio backend (default)
SAUDIO_ANDROID_SLES - on Android, select the OpenSLES backend
If sokol_audio.h is compiled as a DLL, define the following before
including the declaration or implementation:
SOKOL_DLL
On Windows, SOKOL_DLL will define SOKOL_AUDIO_API_DECL as __declspec(dllexport)
or __declspec(dllimport) as needed.
Link with the following libraries:
- on macOS: AudioToolbox
- on iOS: AudioToolbox, AVFoundation
- on FreeBSD: asound
- on Linux: asound
- on Android: link with OpenSLES or aaudio
- on Windows with MSVC or Clang toolchain: no action needed, libs are defined in-source via pragma-comment-lib
- on Windows with MINGW/MSYS2 gcc: compile with '-mwin32' and link with -lole32
FEATURE OVERVIEW
================
You provide a mono- or stereo-stream of 32-bit float samples, which
Sokol Audio feeds into platform-specific audio backends:
- Windows: WASAPI
- Linux: ALSA
- FreeBSD: ALSA
- macOS: CoreAudio
- iOS: CoreAudio+AVAudioSession
- emscripten: WebAudio with ScriptProcessorNode
- Android: AAudio (default) or OpenSLES, select at build time
Sokol Audio will not do any buffer mixing or volume control, if you have
multiple independent input streams of sample data you need to perform the
mixing yourself before forwarding the data to Sokol Audio.
There are two mutually exclusive ways to provide the sample data:
1. Callback model: You provide a callback function, which will be called
when Sokol Audio needs new samples. On all platforms except emscripten,
this function is called from a separate thread.
2. Push model: Your code pushes small blocks of sample data from your
main loop or a thread you created. The pushed data is stored in
a ring buffer where it is pulled by the backend code when
needed.
The callback model is preferred because it is the most direct way to
feed sample data into the audio backends and also has less moving parts
(there is no ring buffer between your code and the audio backend).
Sometimes it is not possible to generate the audio stream directly in a
callback function running in a separate thread, for such cases Sokol Audio
provides the push-model as a convenience.
SOKOL AUDIO, SOLOUD AND MINIAUDIO
=================================
The WASAPI, ALSA, OpenSLES and CoreAudio backend code has been taken from the
SoLoud library (with some modifications, so any bugs in there are most
likely my fault). If you need a more fully-featured audio solution, check
out SoLoud, it's excellent:
https://github.com/jarikomppa/soloud
Another alternative which feature-wise is somewhere inbetween SoLoud and
sokol-audio might be MiniAudio:
https://github.com/mackron/miniaudio
GLOSSARY
========
- stream buffer:
The internal audio data buffer, usually provided by the backend API. The
size of the stream buffer defines the base latency, smaller buffers have
lower latency but may cause audio glitches. Bigger buffers reduce or
eliminate glitches, but have a higher base latency.
- stream callback:
Optional callback function which is called by Sokol Audio when it
needs new samples. On Windows, macOS/iOS and Linux, this is called in
a separate thread, on WebAudio, this is called per-frame in the
browser thread.
- channel:
A discrete track of audio data, currently 1-channel (mono) and
2-channel (stereo) is supported and tested.
- sample:
The magnitude of an audio signal on one channel at a given time. In
Sokol Audio, samples are 32-bit float numbers in the range -1.0 to
+1.0.
- frame:
The tightly packed set of samples for all channels at a given time.
For mono 1 frame is 1 sample. For stereo, 1 frame is 2 samples.
- packet:
In Sokol Audio, a small chunk of audio data that is moved from the
main thread to the audio streaming thread in order to decouple the
rate at which the main thread provides new audio data, and the
streaming thread consuming audio data.
WORKING WITH SOKOL AUDIO
========================
First call saudio_setup() with your preferred audio playback options.
In most cases you can stick with the default values, these provide
a good balance between low-latency and glitch-free playback
on all audio backends.
You should always provide a logging callback to be aware of any
warnings and errors. The easiest way is to use sokol_log.h for this:
#include "sokol_log.h"
// ...
saudio_setup(&(saudio_desc){
.logger = {
.func = slog_func,
}
});
If you want to use the callback-model, you need to provide a stream
callback function either in saudio_desc.stream_cb or saudio_desc.stream_userdata_cb,
otherwise keep both function pointers zero-initialized.
Use push model and default playback parameters:
saudio_setup(&(saudio_desc){ .logger.func = slog_func });
Use stream callback model and default playback parameters:
saudio_setup(&(saudio_desc){
.stream_cb = my_stream_callback
.logger.func = slog_func,
});
The standard stream callback doesn't have a user data argument, if you want
that, use the alternative stream_userdata_cb and also set the user_data pointer:
saudio_setup(&(saudio_desc){
.stream_userdata_cb = my_stream_callback,
.user_data = &my_data
.logger.func = slog_func,
});
The following playback parameters can be provided through the
saudio_desc struct:
General parameters (both for stream-callback and push-model):
int sample_rate -- the sample rate in Hz, default: 44100
int num_channels -- number of channels, default: 1 (mono)
int buffer_frames -- number of frames in streaming buffer, default: 2048
The stream callback prototype (either with or without userdata):
void (*stream_cb)(float* buffer, int num_frames, int num_channels)
void (*stream_userdata_cb)(float* buffer, int num_frames, int num_channels, void* user_data)
Function pointer to the user-provide stream callback.
Push-model parameters:
int packet_frames -- number of frames in a packet, default: 128
int num_packets -- number of packets in ring buffer, default: 64
The sample_rate and num_channels parameters are only hints for the audio
backend, it isn't guaranteed that those are the values used for actual
playback.
To get the actual parameters, call the following functions after
saudio_setup():
int saudio_sample_rate(void)
int saudio_channels(void);
It's unlikely that the number of channels will be different than requested,
but a different sample rate isn't uncommon.
(NOTE: there's an yet unsolved issue when an audio backend might switch
to a different sample rate when switching output devices, for instance
plugging in a bluetooth headset, this case is currently not handled in
Sokol Audio).
You can check if audio initialization was successful with
saudio_isvalid(). If backend initialization failed for some reason
(for instance when there's no audio device in the machine), this
will return false. Not checking for success won't do any harm, all
Sokol Audio function will silently fail when called after initialization
has failed, so apart from missing audio output, nothing bad will happen.
Before your application exits, you should call
saudio_shutdown();
This stops the audio thread (on Linux, Windows and macOS/iOS) and
properly shuts down the audio backend.
THE STREAM CALLBACK MODEL
=========================
To use Sokol Audio in stream-callback-mode, provide a callback function
like this in the saudio_desc struct when calling saudio_setup():
void stream_cb(float* buffer, int num_frames, int num_channels) {
...
}
Or the alternative version with a user-data argument:
void stream_userdata_cb(float* buffer, int num_frames, int num_channels, void* user_data) {
my_data_t* my_data = (my_data_t*) user_data;
...
}
The job of the callback function is to fill the *buffer* with 32-bit
float sample values.
To output silence, fill the buffer with zeros:
void stream_cb(float* buffer, int num_frames, int num_channels) {
const int num_samples = num_frames * num_channels;
for (int i = 0; i < num_samples; i++) {
buffer[i] = 0.0f;
}
}
For stereo output (num_channels == 2), the samples for the left
and right channel are interleaved:
void stream_cb(float* buffer, int num_frames, int num_channels) {
assert(2 == num_channels);
for (int i = 0; i < num_frames; i++) {
buffer[2*i + 0] = ...; // left channel
buffer[2*i + 1] = ...; // right channel
}
}
Please keep in mind that the stream callback function is running in a
separate thread, if you need to share data with the main thread you need
to take care yourself to make the access to the shared data thread-safe!
THE PUSH MODEL
==============
To use the push-model for providing audio data, simply don't set (keep
zero-initialized) the stream_cb field in the saudio_desc struct when
calling saudio_setup().
To provide sample data with the push model, call the saudio_push()
function at regular intervals (for instance once per frame). You can
call the saudio_expect() function to ask Sokol Audio how much room is
in the ring buffer, but if you provide a continuous stream of data
at the right sample rate, saudio_expect() isn't required (it's a simple
way to sync/throttle your sample generation code with the playback
rate though).
With saudio_push() you may need to maintain your own intermediate sample
buffer, since pushing individual sample values isn't very efficient.
The following example is from the MOD player sample in
sokol-samples (https://github.com/floooh/sokol-samples):
const int num_frames = saudio_expect();
if (num_frames > 0) {
const int num_samples = num_frames * saudio_channels();
read_samples(flt_buf, num_samples);
saudio_push(flt_buf, num_frames);
}
Another option is to ignore saudio_expect(), and just push samples as they
are generated in small batches. In this case you *need* to generate the
samples at the right sample rate:
The following example is taken from the Tiny Emulators project
(https://github.com/floooh/chips-test), this is for mono playback,
so (num_samples == num_frames):
// tick the sound generator
if (ay38910_tick(&sys->psg)) {
// new sample is ready
sys->sample_buffer[sys->sample_pos++] = sys->psg.sample;
if (sys->sample_pos == sys->num_samples) {
// new sample packet is ready
saudio_push(sys->sample_buffer, sys->num_samples);
sys->sample_pos = 0;
}
}
THE WEBAUDIO BACKEND
====================
The WebAudio backend is currently using a ScriptProcessorNode callback to
feed the sample data into WebAudio. ScriptProcessorNode has been
deprecated for a while because it is running from the main thread, with
the default initialization parameters it works 'pretty well' though.
Ultimately Sokol Audio will use Audio Worklets, but this requires a few
more things to fall into place (Audio Worklets implemented everywhere,
SharedArrayBuffers enabled again, and I need to figure out a 'low-cost'
solution in terms of implementation effort, since Audio Worklets are
a lot more complex than ScriptProcessorNode if the audio data needs to come
from the main thread).
The WebAudio backend is automatically selected when compiling for
emscripten (__EMSCRIPTEN__ define exists).
https://developers.google.com/web/updates/2017/12/audio-worklet
https://developers.google.com/web/updates/2018/06/audio-worklet-design-pattern
"Blob URLs": https://www.html5rocks.com/en/tutorials/workers/basics/
Also see: https://blog.paul.cx/post/a-wait-free-spsc-ringbuffer-for-the-web/
THE COREAUDIO BACKEND
=====================
The CoreAudio backend is selected on macOS and iOS (__APPLE__ is defined).
Since the CoreAudio API is implemented in C (not Objective-C) on macOS the
implementation part of Sokol Audio can be included into a C source file.
However on iOS, Sokol Audio must be compiled as Objective-C due to it's
reliance on the AVAudioSession object. The iOS code path support both
being compiled with or without ARC (Automatic Reference Counting).
For thread synchronisation, the CoreAudio backend will use the
pthread_mutex_* functions.
The incoming floating point samples will be directly forwarded to
CoreAudio without further conversion.
macOS and iOS applications that use Sokol Audio need to link with
the AudioToolbox framework.
THE WASAPI BACKEND
==================
The WASAPI backend is automatically selected when compiling on Windows
(_WIN32 is defined).
For thread synchronisation a Win32 critical section is used.
WASAPI may use a different size for its own streaming buffer then requested,
so the base latency may be slightly bigger. The current backend implementation
converts the incoming floating point sample values to signed 16-bit
integers.
The required Windows system DLLs are linked with #pragma comment(lib, ...),
so you shouldn't need to add additional linker libs in the build process
(otherwise this is a bug which should be fixed in sokol_audio.h).
THE ALSA BACKEND
================
The ALSA backend is automatically selected when compiling on Linux
('linux' is defined).
For thread synchronisation, the pthread_mutex_* functions are used.
Samples are directly forwarded to ALSA in 32-bit float format, no
further conversion is taking place.
You need to link with the 'asound' library, and the <alsa/asoundlib.h>
header must be present (usually both are installed with some sort
of ALSA development package).
MEMORY ALLOCATION OVERRIDE
==========================
You can override the memory allocation functions at initialization time
like this:
void* my_alloc(size_t size, void* user_data) {
return malloc(size);
}
void my_free(void* ptr, void* user_data) {
free(ptr);
}
...
saudio_setup(&(saudio_desc){
// ...
.allocator = {
.alloc_fn = my_alloc,
.free_fn = my_free,
.user_data = ...,
}
});
...
If no overrides are provided, malloc and free will be used.
This only affects memory allocation calls done by sokol_audio.h
itself though, not any allocations in OS libraries.
Memory allocation will only happen on the same thread where saudio_setup()
was called, so you don't need to worry about thread-safety.
ERROR REPORTING AND LOGGING
===========================
To get any logging information at all you need to provide a logging callback in the setup call
the easiest way is to use sokol_log.h:
#include "sokol_log.h"
saudio_setup(&(saudio_desc){ .logger.func = slog_func });
To override logging with your own callback, first write a logging function like this:
void my_log(const char* tag, // e.g. 'saudio'
uint32_t log_level, // 0=panic, 1=error, 2=warn, 3=info
uint32_t log_item_id, // SAUDIO_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_audio.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data)
{
...
}
...and then setup sokol-audio like this:
saudio_setup(&(saudio_desc){
.logger = {
.func = my_log,
.user_data = my_user_data,
}
});
The provided logging function must be reentrant (e.g. be callable from
different threads).
If you don't want to provide your own custom logger it is highly recommended to use
the standard logger in sokol_log.h instead, otherwise you won't see any warnings or
errors.
LICENSE
=======
zlib/libpng license
Copyright (c) 2018 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define SOKOL_AUDIO_INCLUDED (1)
#include <stddef.h> // size_t
#include <stdint.h>
#include <stdbool.h>
#if defined(SOKOL_API_DECL) && !defined(SOKOL_AUDIO_API_DECL)
#define SOKOL_AUDIO_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_AUDIO_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_AUDIO_IMPL)
#define SOKOL_AUDIO_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_AUDIO_API_DECL __declspec(dllimport)
#else
#define SOKOL_AUDIO_API_DECL extern
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
saudio_log_item
Log items are defined via X-Macros, and expanded to an
enum 'saudio_log_item', and in debug mode only,
corresponding strings.
Used as parameter in the logging callback.
*/
#define _SAUDIO_LOG_ITEMS \
_SAUDIO_LOGITEM_XMACRO(OK, "Ok") \
_SAUDIO_LOGITEM_XMACRO(MALLOC_FAILED, "memory allocation failed") \
_SAUDIO_LOGITEM_XMACRO(ALSA_SND_PCM_OPEN_FAILED, "snd_pcm_open() failed") \
_SAUDIO_LOGITEM_XMACRO(ALSA_FLOAT_SAMPLES_NOT_SUPPORTED, "floating point sample format not supported") \
_SAUDIO_LOGITEM_XMACRO(ALSA_REQUESTED_BUFFER_SIZE_NOT_SUPPORTED, "requested buffer size not supported") \
_SAUDIO_LOGITEM_XMACRO(ALSA_REQUESTED_CHANNEL_COUNT_NOT_SUPPORTED, "requested channel count not supported") \
_SAUDIO_LOGITEM_XMACRO(ALSA_SND_PCM_HW_PARAMS_SET_RATE_NEAR_FAILED, "snd_pcm_hw_params_set_rate_near() failed") \
_SAUDIO_LOGITEM_XMACRO(ALSA_SND_PCM_HW_PARAMS_FAILED, "snd_pcm_hw_params() failed") \
_SAUDIO_LOGITEM_XMACRO(ALSA_PTHREAD_CREATE_FAILED, "pthread_create() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_CREATE_EVENT_FAILED, "CreateEvent() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_CREATE_DEVICE_ENUMERATOR_FAILED, "CoCreateInstance() for IMMDeviceEnumerator failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_GET_DEFAULT_AUDIO_ENDPOINT_FAILED, "IMMDeviceEnumerator.GetDefaultAudioEndpoint() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_DEVICE_ACTIVATE_FAILED, "IMMDevice.Activate() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_AUDIO_CLIENT_INITIALIZE_FAILED, "IAudioClient.Initialize() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_AUDIO_CLIENT_GET_BUFFER_SIZE_FAILED, "IAudioClient.GetBufferSize() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_AUDIO_CLIENT_GET_SERVICE_FAILED, "IAudioClient.GetService() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_AUDIO_CLIENT_SET_EVENT_HANDLE_FAILED, "IAudioClient.SetEventHandle() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_CREATE_THREAD_FAILED, "CreateThread() failed") \
_SAUDIO_LOGITEM_XMACRO(AAUDIO_STREAMBUILDER_OPEN_STREAM_FAILED, "AAudioStreamBuilder_openStream() failed") \
_SAUDIO_LOGITEM_XMACRO(AAUDIO_PTHREAD_CREATE_FAILED, "pthread_create() failed after AAUDIO_ERROR_DISCONNECTED") \
_SAUDIO_LOGITEM_XMACRO(AAUDIO_RESTARTING_STREAM_AFTER_ERROR, "restarting AAudio stream after error") \
_SAUDIO_LOGITEM_XMACRO(USING_AAUDIO_BACKEND, "using AAudio backend") \
_SAUDIO_LOGITEM_XMACRO(AAUDIO_CREATE_STREAMBUILDER_FAILED, "AAudio_createStreamBuilder() failed") \
_SAUDIO_LOGITEM_XMACRO(USING_SLES_BACKEND, "using OpenSLES backend") \
_SAUDIO_LOGITEM_XMACRO(SLES_CREATE_ENGINE_FAILED, "slCreateEngine() failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_ENGINE_GET_ENGINE_INTERFACE_FAILED, "GetInterface() for SL_IID_ENGINE failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_CREATE_OUTPUT_MIX_FAILED, "CreateOutputMix() failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_MIXER_GET_VOLUME_INTERFACE_FAILED, "GetInterface() for SL_IID_VOLUME failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_ENGINE_CREATE_AUDIO_PLAYER_FAILED, "CreateAudioPlayer() failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_PLAYER_GET_PLAY_INTERFACE_FAILED, "GetInterface() for SL_IID_PLAY failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_PLAYER_GET_VOLUME_INTERFACE_FAILED, "GetInterface() for SL_IID_VOLUME failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_PLAYER_GET_BUFFERQUEUE_INTERFACE_FAILED, "GetInterface() for SL_IID_ANDROIDSIMPLEBUFFERQUEUE failed") \
_SAUDIO_LOGITEM_XMACRO(COREAUDIO_NEW_OUTPUT_FAILED, "AudioQueueNewOutput() failed") \
_SAUDIO_LOGITEM_XMACRO(COREAUDIO_ALLOCATE_BUFFER_FAILED, "AudioQueueAllocateBuffer() failed") \
_SAUDIO_LOGITEM_XMACRO(COREAUDIO_START_FAILED, "AudioQueueStart() failed") \
_SAUDIO_LOGITEM_XMACRO(BACKEND_BUFFER_SIZE_ISNT_MULTIPLE_OF_PACKET_SIZE, "backend buffer size isn't multiple of packet size") \
#define _SAUDIO_LOGITEM_XMACRO(item,msg) SAUDIO_LOGITEM_##item,
typedef enum saudio_log_item {
_SAUDIO_LOG_ITEMS
} saudio_log_item;
#undef _SAUDIO_LOGITEM_XMACRO
/*
saudio_logger
Used in saudio_desc to provide a custom logging and error reporting
callback to sokol-audio.
*/
typedef struct saudio_logger {
void (*func)(
const char* tag, // always "saudio"
uint32_t log_level, // 0=panic, 1=error, 2=warning, 3=info
uint32_t log_item_id, // SAUDIO_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_audio.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data);
void* user_data;
} saudio_logger;
/*
saudio_allocator
Used in saudio_desc to provide custom memory-alloc and -free functions
to sokol_audio.h. If memory management should be overridden, both the
alloc_fn and free_fn function must be provided (e.g. it's not valid to
override one function but not the other).
*/
typedef struct saudio_allocator {
void* (*alloc_fn)(size_t size, void* user_data);
void (*free_fn)(void* ptr, void* user_data);
void* user_data;
} saudio_allocator;
typedef struct saudio_desc {
int sample_rate; // requested sample rate
int num_channels; // number of channels, default: 1 (mono)
int buffer_frames; // number of frames in streaming buffer
int packet_frames; // number of frames in a packet
int num_packets; // number of packets in packet queue
void (*stream_cb)(float* buffer, int num_frames, int num_channels); // optional streaming callback (no user data)
void (*stream_userdata_cb)(float* buffer, int num_frames, int num_channels, void* user_data); //... and with user data
void* user_data; // optional user data argument for stream_userdata_cb
saudio_allocator allocator; // optional allocation override functions
saudio_logger logger; // optional logging function (default: NO LOGGING!)
} saudio_desc;
/* setup sokol-audio */
SOKOL_AUDIO_API_DECL void saudio_setup(const saudio_desc* desc);
/* shutdown sokol-audio */
SOKOL_AUDIO_API_DECL void saudio_shutdown(void);
/* true after setup if audio backend was successfully initialized */
SOKOL_AUDIO_API_DECL bool saudio_isvalid(void);
/* return the saudio_desc.user_data pointer */
SOKOL_AUDIO_API_DECL void* saudio_userdata(void);
/* return a copy of the original saudio_desc struct */
SOKOL_AUDIO_API_DECL saudio_desc saudio_query_desc(void);
/* actual sample rate */
SOKOL_AUDIO_API_DECL int saudio_sample_rate(void);
/* return actual backend buffer size in number of frames */
SOKOL_AUDIO_API_DECL int saudio_buffer_frames(void);
/* actual number of channels */
SOKOL_AUDIO_API_DECL int saudio_channels(void);
/* return true if audio context is currently suspended (only in WebAudio backend, all other backends return false) */
SOKOL_AUDIO_API_DECL bool saudio_suspended(void);
/* get current number of frames to fill packet queue */
SOKOL_AUDIO_API_DECL int saudio_expect(void);
/* push sample frames from main thread, returns number of frames actually pushed */
SOKOL_AUDIO_API_DECL int saudio_push(const float* frames, int num_frames);
#ifdef __cplusplus
} /* extern "C" */
/* reference-based equivalents for c++ */
inline void saudio_setup(const saudio_desc& desc) { return saudio_setup(&desc); }
#endif
#endif // SOKOL_AUDIO_INCLUDED
// ██ ███ ███ ██████ ██ ███████ ███ ███ ███████ ███ ██ ████████ █████ ████████ ██ ██████ ███ ██
// ██ ████ ████ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
// ██ ██ ████ ██ ██████ ██ █████ ██ ████ ██ █████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ███████ ███████ ██ ██ ███████ ██ ████ ██ ██ ██ ██ ██ ██████ ██ ████
//
// >>implementation
#ifdef SOKOL_AUDIO_IMPL
#define SOKOL_AUDIO_IMPL_INCLUDED (1)
#if defined(SOKOL_MALLOC) || defined(SOKOL_CALLOC) || defined(SOKOL_FREE)
#error "SOKOL_MALLOC/CALLOC/FREE macros are no longer supported, please use saudio_desc.allocator to override memory allocation functions"
#endif
#include <stdlib.h> // alloc, free
#include <string.h> // memset, memcpy
#include <stddef.h> // size_t
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
#ifndef SOKOL_DEBUG
#ifndef NDEBUG
#define SOKOL_DEBUG
#endif
#endif
#ifndef SOKOL_ASSERT
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
#ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static
#else
#define _SOKOL_PRIVATE static
#endif
#endif
#ifndef _SOKOL_UNUSED
#define _SOKOL_UNUSED(x) (void)(x)
#endif
// platform detection defines
#if defined(SOKOL_DUMMY_BACKEND)
// nothing
#elif defined(__APPLE__)
#define _SAUDIO_APPLE (1)
#include <TargetConditionals.h>
#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
#define _SAUDIO_IOS (1)
#else
#define _SAUDIO_MACOS (1)
#endif
#elif defined(__EMSCRIPTEN__)
#define _SAUDIO_EMSCRIPTEN (1)
#elif defined(_WIN32)
#define _SAUDIO_WINDOWS (1)
#include <winapifamily.h>
#if (defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP))
#error "sokol_audio.h no longer supports UWP"
#endif
#elif defined(__ANDROID__)
#define _SAUDIO_ANDROID (1)
#if !defined(SAUDIO_ANDROID_SLES) && !defined(SAUDIO_ANDROID_AAUDIO)
#define SAUDIO_ANDROID_AAUDIO (1)
#endif
#elif defined(__linux__) || defined(__unix__)
#define _SAUDIO_LINUX (1)
#else
#error "sokol_audio.h: Unknown platform"
#endif
// platform-specific headers and definitions
#if defined(SOKOL_DUMMY_BACKEND)
#define _SAUDIO_NOTHREADS (1)
#elif defined(_SAUDIO_WINDOWS)
#define _SAUDIO_WINTHREADS (1)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <synchapi.h>
#pragma comment (lib, "kernel32")
#pragma comment (lib, "ole32")
#ifndef CINTERFACE
#define CINTERFACE
#endif
#ifndef COBJMACROS
#define COBJMACROS
#endif
#ifndef CONST_VTABLE
#define CONST_VTABLE
#endif
#include <mmdeviceapi.h>
#include <audioclient.h>
static const IID _saudio_IID_IAudioClient = { 0x1cb9ad4c, 0xdbfa, 0x4c32, {0xb1, 0x78, 0xc2, 0xf5, 0x68, 0xa7, 0x03, 0xb2} };
static const IID _saudio_IID_IMMDeviceEnumerator = { 0xa95664d2, 0x9614, 0x4f35, {0xa7, 0x46, 0xde, 0x8d, 0xb6, 0x36, 0x17, 0xe6} };
static const CLSID _saudio_CLSID_IMMDeviceEnumerator = { 0xbcde0395, 0xe52f, 0x467c, {0x8e, 0x3d, 0xc4, 0x57, 0x92, 0x91, 0x69, 0x2e} };
static const IID _saudio_IID_IAudioRenderClient = { 0xf294acfc, 0x3146, 0x4483, {0xa7, 0xbf, 0xad, 0xdc, 0xa7, 0xc2, 0x60, 0xe2} };
static const IID _saudio_IID_Devinterface_Audio_Render = { 0xe6327cad, 0xdcec, 0x4949, {0xae, 0x8a, 0x99, 0x1e, 0x97, 0x6a, 0x79, 0xd2} };
static const IID _saudio_IID_IActivateAudioInterface_Completion_Handler = { 0x94ea2b94, 0xe9cc, 0x49e0, {0xc0, 0xff, 0xee, 0x64, 0xca, 0x8f, 0x5b, 0x90} };
static const GUID _saudio_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { 0x00000003, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71} };
#if defined(__cplusplus)
#define _SOKOL_AUDIO_WIN32COM_ID(x) (x)
#else
#define _SOKOL_AUDIO_WIN32COM_ID(x) (&x)
#endif
/* fix for Visual Studio 2015 SDKs */
#ifndef AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM
#define AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM 0x80000000
#endif
#ifndef AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY
#define AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY 0x08000000
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4505) /* unreferenced local function has been removed */
#endif
#elif defined(_SAUDIO_APPLE)
#define _SAUDIO_PTHREADS (1)
#include <pthread.h>
#if defined(_SAUDIO_IOS)
// always use system headers on iOS (for now at least)
#if !defined(SAUDIO_OSX_USE_SYSTEM_HEADERS)
#define SAUDIO_OSX_USE_SYSTEM_HEADERS (1)
#endif
#if !defined(__cplusplus)
#if __has_feature(objc_arc) && !__has_feature(objc_arc_fields)
#error "sokol_audio.h on iOS requires __has_feature(objc_arc_field) if ARC is enabled (use a more recent compiler version)"
#endif
#endif
#include <AudioToolbox/AudioToolbox.h>
#include <AVFoundation/AVFoundation.h>
#else
#if defined(SAUDIO_OSX_USE_SYSTEM_HEADERS)
#include <AudioToolbox/AudioToolbox.h>
#endif
#endif
#elif defined(_SAUDIO_ANDROID)
#define _SAUDIO_PTHREADS (1)
#include <pthread.h>
#if defined(SAUDIO_ANDROID_SLES)
#include "SLES/OpenSLES_Android.h"
#elif defined(SAUDIO_ANDROID_AAUDIO)
#include "aaudio/AAudio.h"
#endif
#elif defined(_SAUDIO_LINUX)
#if !defined(__FreeBSD__)
#include <alloca.h>
#endif
#define _SAUDIO_PTHREADS (1)
#include <pthread.h>
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>
#elif defined(__EMSCRIPTEN__)
#define _SAUDIO_NOTHREADS (1)
#include <emscripten/emscripten.h>
#endif
#define _saudio_def(val, def) (((val) == 0) ? (def) : (val))
#define _saudio_def_flt(val, def) (((val) == 0.0f) ? (def) : (val))
#define _SAUDIO_DEFAULT_SAMPLE_RATE (44100)
#define _SAUDIO_DEFAULT_BUFFER_FRAMES (2048)
#define _SAUDIO_DEFAULT_PACKET_FRAMES (128)
#define _SAUDIO_DEFAULT_NUM_PACKETS ((_SAUDIO_DEFAULT_BUFFER_FRAMES/_SAUDIO_DEFAULT_PACKET_FRAMES)*4)
#ifndef SAUDIO_RING_MAX_SLOTS
#define SAUDIO_RING_MAX_SLOTS (1024)
#endif
// ███████ ████████ ██████ ██ ██ ██████ ████████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██████ ██ ██ ██ ██ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██ ██ ██████ ██████ ██ ███████
//
// >>structs
#if defined(_SAUDIO_PTHREADS)
typedef struct {
pthread_mutex_t mutex;
} _saudio_mutex_t;
#elif defined(_SAUDIO_WINTHREADS)
typedef struct {
CRITICAL_SECTION critsec;
} _saudio_mutex_t;
#elif defined(_SAUDIO_NOTHREADS)
typedef struct {
int dummy_mutex;
} _saudio_mutex_t;
#endif
#if defined(SOKOL_DUMMY_BACKEND)
typedef struct {
int dummy;
} _saudio_dummy_backend_t;
#elif defined(_SAUDIO_APPLE)
#if defined(SAUDIO_OSX_USE_SYSTEM_HEADERS)
typedef AudioQueueRef _saudio_AudioQueueRef;
typedef AudioQueueBufferRef _saudio_AudioQueueBufferRef;
typedef AudioStreamBasicDescription _saudio_AudioStreamBasicDescription;
typedef OSStatus _saudio_OSStatus;
#define _saudio_kAudioFormatLinearPCM (kAudioFormatLinearPCM)
#define _saudio_kLinearPCMFormatFlagIsFloat (kLinearPCMFormatFlagIsFloat)
#define _saudio_kAudioFormatFlagIsPacked (kAudioFormatFlagIsPacked)
#else
#ifdef __cplusplus
extern "C" {
#endif
// embedded AudioToolbox declarations
typedef uint32_t _saudio_AudioFormatID;
typedef uint32_t _saudio_AudioFormatFlags;
typedef int32_t _saudio_OSStatus;
typedef uint32_t _saudio_SMPTETimeType;
typedef uint32_t _saudio_SMPTETimeFlags;
typedef uint32_t _saudio_AudioTimeStampFlags;
typedef void* _saudio_CFRunLoopRef;
typedef void* _saudio_CFStringRef;
typedef void* _saudio_AudioQueueRef;
#define _saudio_kAudioFormatLinearPCM ('lpcm')
#define _saudio_kLinearPCMFormatFlagIsFloat (1U << 0)
#define _saudio_kAudioFormatFlagIsPacked (1U << 3)
typedef struct _saudio_AudioStreamBasicDescription {
double mSampleRate;
_saudio_AudioFormatID mFormatID;
_saudio_AudioFormatFlags mFormatFlags;
uint32_t mBytesPerPacket;
uint32_t mFramesPerPacket;
uint32_t mBytesPerFrame;
uint32_t mChannelsPerFrame;
uint32_t mBitsPerChannel;
uint32_t mReserved;
} _saudio_AudioStreamBasicDescription;
typedef struct _saudio_AudioStreamPacketDescription {
int64_t mStartOffset;
uint32_t mVariableFramesInPacket;
uint32_t mDataByteSize;
} _saudio_AudioStreamPacketDescription;
typedef struct _saudio_SMPTETime {
int16_t mSubframes;
int16_t mSubframeDivisor;
uint32_t mCounter;
_saudio_SMPTETimeType mType;
_saudio_SMPTETimeFlags mFlags;
int16_t mHours;
int16_t mMinutes;
int16_t mSeconds;
int16_t mFrames;
} _saudio_SMPTETime;
typedef struct _saudio_AudioTimeStamp {
double mSampleTime;
uint64_t mHostTime;
double mRateScalar;
uint64_t mWordClockTime;
_saudio_SMPTETime mSMPTETime;
_saudio_AudioTimeStampFlags mFlags;
uint32_t mReserved;
} _saudio_AudioTimeStamp;
typedef struct _saudio_AudioQueueBuffer {
const uint32_t mAudioDataBytesCapacity;
void* const mAudioData;
uint32_t mAudioDataByteSize;
void * mUserData;
const uint32_t mPacketDescriptionCapacity;
_saudio_AudioStreamPacketDescription* const mPacketDescriptions;
uint32_t mPacketDescriptionCount;
} _saudio_AudioQueueBuffer;
typedef _saudio_AudioQueueBuffer* _saudio_AudioQueueBufferRef;
typedef void (*_saudio_AudioQueueOutputCallback)(void* user_data, _saudio_AudioQueueRef inAQ, _saudio_AudioQueueBufferRef inBuffer);
extern _saudio_OSStatus AudioQueueNewOutput(const _saudio_AudioStreamBasicDescription* inFormat, _saudio_AudioQueueOutputCallback inCallbackProc, void* inUserData, _saudio_CFRunLoopRef inCallbackRunLoop, _saudio_CFStringRef inCallbackRunLoopMode, uint32_t inFlags, _saudio_AudioQueueRef* outAQ);
extern _saudio_OSStatus AudioQueueDispose(_saudio_AudioQueueRef inAQ, bool inImmediate);
extern _saudio_OSStatus AudioQueueAllocateBuffer(_saudio_AudioQueueRef inAQ, uint32_t inBufferByteSize, _saudio_AudioQueueBufferRef* outBuffer);
extern _saudio_OSStatus AudioQueueEnqueueBuffer(_saudio_AudioQueueRef inAQ, _saudio_AudioQueueBufferRef inBuffer, uint32_t inNumPacketDescs, const _saudio_AudioStreamPacketDescription* inPacketDescs);
extern _saudio_OSStatus AudioQueueStart(_saudio_AudioQueueRef inAQ, const _saudio_AudioTimeStamp * inStartTime);
extern _saudio_OSStatus AudioQueueStop(_saudio_AudioQueueRef inAQ, bool inImmediate);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // SAUDIO_OSX_USE_SYSTEM_HEADERS
typedef struct {
_saudio_AudioQueueRef ca_audio_queue;
#if defined(_SAUDIO_IOS)
id ca_interruption_handler;
#endif
} _saudio_apple_backend_t;
#elif defined(_SAUDIO_LINUX)
typedef struct {
snd_pcm_t* device;
float* buffer;
int buffer_byte_size;
int buffer_frames;
pthread_t thread;
bool thread_stop;
} _saudio_alsa_backend_t;
#elif defined(SAUDIO_ANDROID_SLES)
#define SAUDIO_SLES_NUM_BUFFERS (2)
typedef struct {
pthread_mutex_t mutex;
pthread_cond_t cond;
int count;
} _saudio_sles_semaphore_t;
typedef struct {
SLObjectItf engine_obj;
SLEngineItf engine;
SLObjectItf output_mix_obj;
SLVolumeItf output_mix_vol;
SLDataLocator_OutputMix out_locator;
SLDataSink dst_data_sink;
SLObjectItf player_obj;
SLPlayItf player;
SLVolumeItf player_vol;
SLAndroidSimpleBufferQueueItf player_buffer_queue;
int16_t* output_buffers[SAUDIO_SLES_NUM_BUFFERS];
float* src_buffer;
int active_buffer;
_saudio_sles_semaphore_t buffer_sem;
pthread_t thread;
volatile int thread_stop;
SLDataLocator_AndroidSimpleBufferQueue in_locator;
} _saudio_sles_backend_t;
#elif defined(SAUDIO_ANDROID_AAUDIO)
typedef struct {
AAudioStreamBuilder* builder;
AAudioStream* stream;
pthread_t thread;
pthread_mutex_t mutex;
} _saudio_aaudio_backend_t;
#elif defined(_SAUDIO_WINDOWS)
typedef struct {
HANDLE thread_handle;
HANDLE buffer_end_event;
bool stop;
UINT32 dst_buffer_frames;
int src_buffer_frames;
int src_buffer_byte_size;
int src_buffer_pos;
float* src_buffer;
} _saudio_wasapi_thread_data_t;
typedef struct {
IMMDeviceEnumerator* device_enumerator;
IMMDevice* device;
IAudioClient* audio_client;
IAudioRenderClient* render_client;
_saudio_wasapi_thread_data_t thread;
} _saudio_wasapi_backend_t;
#elif defined(_SAUDIO_EMSCRIPTEN)
typedef struct {
uint8_t* buffer;
} _saudio_web_backend_t;
#else
#error "unknown platform"
#endif
#if defined(SOKOL_DUMMY_BACKEND)
typedef _saudio_dummy_backend_t _saudio_backend_t;
#elif defined(_SAUDIO_APPLE)
typedef _saudio_apple_backend_t _saudio_backend_t;
#elif defined(_SAUDIO_EMSCRIPTEN)
typedef _saudio_web_backend_t _saudio_backend_t;
#elif defined(_SAUDIO_WINDOWS)
typedef _saudio_wasapi_backend_t _saudio_backend_t;
#elif defined(SAUDIO_ANDROID_SLES)
typedef _saudio_sles_backend_t _saudio_backend_t;
#elif defined(SAUDIO_ANDROID_AAUDIO)
typedef _saudio_aaudio_backend_t _saudio_backend_t;
#elif defined(_SAUDIO_LINUX)
typedef _saudio_alsa_backend_t _saudio_backend_t;
#endif
/* a ringbuffer structure */
typedef struct {
int head; // next slot to write to
int tail; // next slot to read from
int num; // number of slots in queue
int queue[SAUDIO_RING_MAX_SLOTS];
} _saudio_ring_t;
/* a packet FIFO structure */
typedef struct {
bool valid;
int packet_size; /* size of a single packets in bytes(!) */
int num_packets; /* number of packet in fifo */
uint8_t* base_ptr; /* packet memory chunk base pointer (dynamically allocated) */
int cur_packet; /* current write-packet */
int cur_offset; /* current byte-offset into current write packet */
_saudio_mutex_t mutex; /* mutex for thread-safe access */
_saudio_ring_t read_queue; /* buffers with data, ready to be streamed */
_saudio_ring_t write_queue; /* empty buffers, ready to be pushed to */
} _saudio_fifo_t;
/* sokol-audio state */
typedef struct {
bool valid;
bool setup_called;
void (*stream_cb)(float* buffer, int num_frames, int num_channels);
void (*stream_userdata_cb)(float* buffer, int num_frames, int num_channels, void* user_data);
void* user_data;
int sample_rate; /* sample rate */
int buffer_frames; /* number of frames in streaming buffer */
int bytes_per_frame; /* filled by backend */
int packet_frames; /* number of frames in a packet */
int num_packets; /* number of packets in packet queue */
int num_channels; /* actual number of channels */
saudio_desc desc;
_saudio_fifo_t fifo;
_saudio_backend_t backend;
} _saudio_state_t;
_SOKOL_PRIVATE _saudio_state_t _saudio;
_SOKOL_PRIVATE bool _saudio_has_callback(void) {
return (_saudio.stream_cb || _saudio.stream_userdata_cb);
}
_SOKOL_PRIVATE void _saudio_stream_callback(float* buffer, int num_frames, int num_channels) {
if (_saudio.stream_cb) {
_saudio.stream_cb(buffer, num_frames, num_channels);
}
else if (_saudio.stream_userdata_cb) {
_saudio.stream_userdata_cb(buffer, num_frames, num_channels, _saudio.user_data);
}
}
// ██ ██████ ██████ ██████ ██ ███ ██ ██████
// ██ ██ ██ ██ ██ ██ ████ ██ ██
// ██ ██ ██ ██ ███ ██ ███ ██ ██ ██ ██ ██ ███
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██████ ██████ ██████ ██ ██ ████ ██████
//
// >>logging
#if defined(SOKOL_DEBUG)
#define _SAUDIO_LOGITEM_XMACRO(item,msg) #item ": " msg,
static const char* _saudio_log_messages[] = {
_SAUDIO_LOG_ITEMS
};
#undef _SAUDIO_LOGITEM_XMACRO
#endif // SOKOL_DEBUG
#define _SAUDIO_PANIC(code) _saudio_log(SAUDIO_LOGITEM_ ##code, 0, __LINE__)
#define _SAUDIO_ERROR(code) _saudio_log(SAUDIO_LOGITEM_ ##code, 1, __LINE__)
#define _SAUDIO_WARN(code) _saudio_log(SAUDIO_LOGITEM_ ##code, 2, __LINE__)
#define _SAUDIO_INFO(code) _saudio_log(SAUDIO_LOGITEM_ ##code, 3, __LINE__)
static void _saudio_log(saudio_log_item log_item, uint32_t log_level, uint32_t line_nr) {
if (_saudio.desc.logger.func) {
#if defined(SOKOL_DEBUG)
const char* filename = __FILE__;
const char* message = _saudio_log_messages[log_item];
#else
const char* filename = 0;
const char* message = 0;
#endif
_saudio.desc.logger.func("saudio", log_level, log_item, message, line_nr, filename, _saudio.desc.logger.user_data);
}
else {
// for log level PANIC it would be 'undefined behaviour' to continue
if (log_level == 0) {
abort();
}
}
}
// ███ ███ ███████ ███ ███ ██████ ██████ ██ ██
// ████ ████ ██ ████ ████ ██ ██ ██ ██ ██ ██
// ██ ████ ██ █████ ██ ████ ██ ██ ██ ██████ ████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ██ ██ ██████ ██ ██ ██
//
// >>memory
_SOKOL_PRIVATE void _saudio_clear(void* ptr, size_t size) {
SOKOL_ASSERT(ptr && (size > 0));
memset(ptr, 0, size);
}
_SOKOL_PRIVATE void* _saudio_malloc(size_t size) {
SOKOL_ASSERT(size > 0);
void* ptr;
if (_saudio.desc.allocator.alloc_fn) {
ptr = _saudio.desc.allocator.alloc_fn(size, _saudio.desc.allocator.user_data);
} else {
ptr = malloc(size);
}
if (0 == ptr) {
_SAUDIO_PANIC(MALLOC_FAILED);
}
return ptr;
}
_SOKOL_PRIVATE void* _saudio_malloc_clear(size_t size) {
void* ptr = _saudio_malloc(size);
_saudio_clear(ptr, size);
return ptr;
}
_SOKOL_PRIVATE void _saudio_free(void* ptr) {
if (_saudio.desc.allocator.free_fn) {
_saudio.desc.allocator.free_fn(ptr, _saudio.desc.allocator.user_data);
} else {
free(ptr);
}
}
// ███ ███ ██ ██ ████████ ███████ ██ ██
// ████ ████ ██ ██ ██ ██ ██ ██
// ██ ████ ██ ██ ██ ██ █████ ███
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██████ ██ ███████ ██ ██
//
// >>mutex
#if defined(_SAUDIO_NOTHREADS)
_SOKOL_PRIVATE void _saudio_mutex_init(_saudio_mutex_t* m) { (void)m; }
_SOKOL_PRIVATE void _saudio_mutex_destroy(_saudio_mutex_t* m) { (void)m; }
_SOKOL_PRIVATE void _saudio_mutex_lock(_saudio_mutex_t* m) { (void)m; }
_SOKOL_PRIVATE void _saudio_mutex_unlock(_saudio_mutex_t* m) { (void)m; }
#elif defined(_SAUDIO_PTHREADS)
_SOKOL_PRIVATE void _saudio_mutex_init(_saudio_mutex_t* m) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutex_init(&m->mutex, &attr);
}
_SOKOL_PRIVATE void _saudio_mutex_destroy(_saudio_mutex_t* m) {
pthread_mutex_destroy(&m->mutex);
}
_SOKOL_PRIVATE void _saudio_mutex_lock(_saudio_mutex_t* m) {
pthread_mutex_lock(&m->mutex);
}
_SOKOL_PRIVATE void _saudio_mutex_unlock(_saudio_mutex_t* m) {
pthread_mutex_unlock(&m->mutex);
}
#elif defined(_SAUDIO_WINTHREADS)
_SOKOL_PRIVATE void _saudio_mutex_init(_saudio_mutex_t* m) {
InitializeCriticalSection(&m->critsec);
}
_SOKOL_PRIVATE void _saudio_mutex_destroy(_saudio_mutex_t* m) {
DeleteCriticalSection(&m->critsec);
}
_SOKOL_PRIVATE void _saudio_mutex_lock(_saudio_mutex_t* m) {
EnterCriticalSection(&m->critsec);
}
_SOKOL_PRIVATE void _saudio_mutex_unlock(_saudio_mutex_t* m) {
LeaveCriticalSection(&m->critsec);
}
#else
#error "sokol_audio.h: unknown platform!"
#endif
// ██████ ██ ███ ██ ██████ ██████ ██ ██ ███████ ███████ ███████ ██████
// ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ██ ██ ██ ██ ███ ██████ ██ ██ █████ █████ █████ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ████ ██████ ██████ ██████ ██ ██ ███████ ██ ██
//
// >>ringbuffer
_SOKOL_PRIVATE int _saudio_ring_idx(_saudio_ring_t* ring, int i) {
return (i % ring->num);
}
_SOKOL_PRIVATE void _saudio_ring_init(_saudio_ring_t* ring, int num_slots) {
SOKOL_ASSERT((num_slots + 1) <= SAUDIO_RING_MAX_SLOTS);
ring->head = 0;
ring->tail = 0;
/* one slot reserved to detect 'full' vs 'empty' */
ring->num = num_slots + 1;
}
_SOKOL_PRIVATE bool _saudio_ring_full(_saudio_ring_t* ring) {
return _saudio_ring_idx(ring, ring->head + 1) == ring->tail;
}
_SOKOL_PRIVATE bool _saudio_ring_empty(_saudio_ring_t* ring) {
return ring->head == ring->tail;
}
_SOKOL_PRIVATE int _saudio_ring_count(_saudio_ring_t* ring) {
int count;
if (ring->head >= ring->tail) {
count = ring->head - ring->tail;
}
else {
count = (ring->head + ring->num) - ring->tail;
}
SOKOL_ASSERT(count < ring->num);
return count;
}
_SOKOL_PRIVATE void _saudio_ring_enqueue(_saudio_ring_t* ring, int val) {
SOKOL_ASSERT(!_saudio_ring_full(ring));
ring->queue[ring->head] = val;
ring->head = _saudio_ring_idx(ring, ring->head + 1);
}
_SOKOL_PRIVATE int _saudio_ring_dequeue(_saudio_ring_t* ring) {
SOKOL_ASSERT(!_saudio_ring_empty(ring));
int val = ring->queue[ring->tail];
ring->tail = _saudio_ring_idx(ring, ring->tail + 1);
return val;
}
// ███████ ██ ███████ ██████
// ██ ██ ██ ██ ██
// █████ ██ █████ ██ ██
// ██ ██ ██ ██ ██
// ██ ██ ██ ██████
//
// >>fifo
_SOKOL_PRIVATE void _saudio_fifo_init_mutex(_saudio_fifo_t* fifo) {
/* this must be called before initializing both the backend and the fifo itself! */
_saudio_mutex_init(&fifo->mutex);
}
_SOKOL_PRIVATE void _saudio_fifo_destroy_mutex(_saudio_fifo_t* fifo) {
_saudio_mutex_destroy(&fifo->mutex);
}
_SOKOL_PRIVATE void _saudio_fifo_init(_saudio_fifo_t* fifo, int packet_size, int num_packets) {
/* NOTE: there's a chicken-egg situation during the init phase where the
streaming thread must be started before the fifo is actually initialized,
thus the fifo init must already be protected from access by the fifo_read() func.
*/
_saudio_mutex_lock(&fifo->mutex);
SOKOL_ASSERT((packet_size > 0) && (num_packets > 0));
fifo->packet_size = packet_size;
fifo->num_packets = num_packets;
fifo->base_ptr = (uint8_t*) _saudio_malloc((size_t)(packet_size * num_packets));
fifo->cur_packet = -1;
fifo->cur_offset = 0;
_saudio_ring_init(&fifo->read_queue, num_packets);
_saudio_ring_init(&fifo->write_queue, num_packets);
for (int i = 0; i < num_packets; i++) {
_saudio_ring_enqueue(&fifo->write_queue, i);
}
SOKOL_ASSERT(_saudio_ring_full(&fifo->write_queue));
SOKOL_ASSERT(_saudio_ring_count(&fifo->write_queue) == num_packets);
SOKOL_ASSERT(_saudio_ring_empty(&fifo->read_queue));
SOKOL_ASSERT(_saudio_ring_count(&fifo->read_queue) == 0);
fifo->valid = true;
_saudio_mutex_unlock(&fifo->mutex);
}
_SOKOL_PRIVATE void _saudio_fifo_shutdown(_saudio_fifo_t* fifo) {
SOKOL_ASSERT(fifo->base_ptr);
_saudio_free(fifo->base_ptr);
fifo->base_ptr = 0;
fifo->valid = false;
}
_SOKOL_PRIVATE int _saudio_fifo_writable_bytes(_saudio_fifo_t* fifo) {
_saudio_mutex_lock(&fifo->mutex);
int num_bytes = (_saudio_ring_count(&fifo->write_queue) * fifo->packet_size);
if (fifo->cur_packet != -1) {
num_bytes += fifo->packet_size - fifo->cur_offset;
}
_saudio_mutex_unlock(&fifo->mutex);
SOKOL_ASSERT((num_bytes >= 0) && (num_bytes <= (fifo->num_packets * fifo->packet_size)));
return num_bytes;
}
/* write new data to the write queue, this is called from main thread */
_SOKOL_PRIVATE int _saudio_fifo_write(_saudio_fifo_t* fifo, const uint8_t* ptr, int num_bytes) {
/* returns the number of bytes written, this will be smaller then requested
if the write queue runs full
*/
int all_to_copy = num_bytes;
while (all_to_copy > 0) {
/* need to grab a new packet? */
if (fifo->cur_packet == -1) {
_saudio_mutex_lock(&fifo->mutex);
if (!_saudio_ring_empty(&fifo->write_queue)) {
fifo->cur_packet = _saudio_ring_dequeue(&fifo->write_queue);
}
_saudio_mutex_unlock(&fifo->mutex);
SOKOL_ASSERT(fifo->cur_offset == 0);
}
/* append data to current write packet */
if (fifo->cur_packet != -1) {
int to_copy = all_to_copy;
const int max_copy = fifo->packet_size - fifo->cur_offset;
if (to_copy > max_copy) {
to_copy = max_copy;
}
uint8_t* dst = fifo->base_ptr + fifo->cur_packet * fifo->packet_size + fifo->cur_offset;
memcpy(dst, ptr, (size_t)to_copy);
ptr += to_copy;
fifo->cur_offset += to_copy;
all_to_copy -= to_copy;
SOKOL_ASSERT(fifo->cur_offset <= fifo->packet_size);
SOKOL_ASSERT(all_to_copy >= 0);
}
else {
/* early out if we're starving */
int bytes_copied = num_bytes - all_to_copy;
SOKOL_ASSERT((bytes_copied >= 0) && (bytes_copied < num_bytes));
return bytes_copied;
}
/* if write packet is full, push to read queue */
if (fifo->cur_offset == fifo->packet_size) {
_saudio_mutex_lock(&fifo->mutex);
_saudio_ring_enqueue(&fifo->read_queue, fifo->cur_packet);
_saudio_mutex_unlock(&fifo->mutex);
fifo->cur_packet = -1;
fifo->cur_offset = 0;
}
}
SOKOL_ASSERT(all_to_copy == 0);
return num_bytes;
}
/* read queued data, this is called form the stream callback (maybe separate thread) */
_SOKOL_PRIVATE int _saudio_fifo_read(_saudio_fifo_t* fifo, uint8_t* ptr, int num_bytes) {
/* NOTE: fifo_read might be called before the fifo is properly initialized */
_saudio_mutex_lock(&fifo->mutex);
int num_bytes_copied = 0;
if (fifo->valid) {
SOKOL_ASSERT(0 == (num_bytes % fifo->packet_size));
SOKOL_ASSERT(num_bytes <= (fifo->packet_size * fifo->num_packets));
const int num_packets_needed = num_bytes / fifo->packet_size;
uint8_t* dst = ptr;
/* either pull a full buffer worth of data, or nothing */
if (_saudio_ring_count(&fifo->read_queue) >= num_packets_needed) {
for (int i = 0; i < num_packets_needed; i++) {
int packet_index = _saudio_ring_dequeue(&fifo->read_queue);
_saudio_ring_enqueue(&fifo->write_queue, packet_index);
const uint8_t* src = fifo->base_ptr + packet_index * fifo->packet_size;
memcpy(dst, src, (size_t)fifo->packet_size);
dst += fifo->packet_size;
num_bytes_copied += fifo->packet_size;
}
SOKOL_ASSERT(num_bytes == num_bytes_copied);
}
}
_saudio_mutex_unlock(&fifo->mutex);
return num_bytes_copied;
}
// ██████ ██ ██ ███ ███ ███ ███ ██ ██
// ██ ██ ██ ██ ████ ████ ████ ████ ██ ██
// ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██████ ██ ██ ██ ██ ██
//
// >>dummy
#if defined(SOKOL_DUMMY_BACKEND)
_SOKOL_PRIVATE bool _saudio_dummy_backend_init(void) {
_saudio.bytes_per_frame = _saudio.num_channels * (int)sizeof(float);
return true;
};
_SOKOL_PRIVATE void _saudio_dummy_backend_shutdown(void) { };
// █████ ██ ███████ █████
// ██ ██ ██ ██ ██ ██
// ███████ ██ ███████ ███████
// ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ███████ ██ ██
//
// >>alsa
#elif defined(_SAUDIO_LINUX)
/* the streaming callback runs in a separate thread */
_SOKOL_PRIVATE void* _saudio_alsa_cb(void* param) {
_SOKOL_UNUSED(param);
while (!_saudio.backend.thread_stop) {
/* snd_pcm_writei() will be blocking until it needs data */
int write_res = snd_pcm_writei(_saudio.backend.device, _saudio.backend.buffer, (snd_pcm_uframes_t)_saudio.backend.buffer_frames);
if (write_res < 0) {
/* underrun occurred */
snd_pcm_prepare(_saudio.backend.device);
}
else {
/* fill the streaming buffer with new data */
if (_saudio_has_callback()) {
_saudio_stream_callback(_saudio.backend.buffer, _saudio.backend.buffer_frames, _saudio.num_channels);
}
else {
if (0 == _saudio_fifo_read(&_saudio.fifo, (uint8_t*)_saudio.backend.buffer, _saudio.backend.buffer_byte_size)) {
/* not enough read data available, fill the entire buffer with silence */
_saudio_clear(_saudio.backend.buffer, (size_t)_saudio.backend.buffer_byte_size);
}
}
}
}
return 0;
}
_SOKOL_PRIVATE bool _saudio_alsa_backend_init(void) {
int dir; uint32_t rate;
int rc = snd_pcm_open(&_saudio.backend.device, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
_SAUDIO_ERROR(ALSA_SND_PCM_OPEN_FAILED);
return false;
}
/* configuration works by restricting the 'configuration space' step
by step, we require all parameters except the sample rate to
match perfectly
*/
snd_pcm_hw_params_t* params = 0;
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(_saudio.backend.device, params);
snd_pcm_hw_params_set_access(_saudio.backend.device, params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (0 > snd_pcm_hw_params_set_format(_saudio.backend.device, params, SND_PCM_FORMAT_FLOAT_LE)) {
_SAUDIO_ERROR(ALSA_FLOAT_SAMPLES_NOT_SUPPORTED);
goto error;
}
if (0 > snd_pcm_hw_params_set_buffer_size(_saudio.backend.device, params, (snd_pcm_uframes_t)_saudio.buffer_frames)) {
_SAUDIO_ERROR(ALSA_REQUESTED_BUFFER_SIZE_NOT_SUPPORTED);
goto error;
}
if (0 > snd_pcm_hw_params_set_channels(_saudio.backend.device, params, (uint32_t)_saudio.num_channels)) {
_SAUDIO_ERROR(ALSA_REQUESTED_CHANNEL_COUNT_NOT_SUPPORTED);
goto error;
}
/* let ALSA pick a nearby sampling rate */
rate = (uint32_t) _saudio.sample_rate;
dir = 0;
if (0 > snd_pcm_hw_params_set_rate_near(_saudio.backend.device, params, &rate, &dir)) {
_SAUDIO_ERROR(ALSA_SND_PCM_HW_PARAMS_SET_RATE_NEAR_FAILED);
goto error;
}
if (0 > snd_pcm_hw_params(_saudio.backend.device, params)) {
_SAUDIO_ERROR(ALSA_SND_PCM_HW_PARAMS_FAILED);
goto error;
}
/* read back actual sample rate and channels */
_saudio.sample_rate = (int)rate;
_saudio.bytes_per_frame = _saudio.num_channels * (int)sizeof(float);
/* allocate the streaming buffer */
_saudio.backend.buffer_byte_size = _saudio.buffer_frames * _saudio.bytes_per_frame;
_saudio.backend.buffer_frames = _saudio.buffer_frames;
_saudio.backend.buffer = (float*) _saudio_malloc_clear((size_t)_saudio.backend.buffer_byte_size);
/* create the buffer-streaming start thread */
if (0 != pthread_create(&_saudio.backend.thread, 0, _saudio_alsa_cb, 0)) {
_SAUDIO_ERROR(ALSA_PTHREAD_CREATE_FAILED);
goto error;
}
return true;
error:
if (_saudio.backend.device) {
snd_pcm_close(_saudio.backend.device);
_saudio.backend.device = 0;
}
return false;
};
_SOKOL_PRIVATE void _saudio_alsa_backend_shutdown(void) {
SOKOL_ASSERT(_saudio.backend.device);
_saudio.backend.thread_stop = true;
pthread_join(_saudio.backend.thread, 0);
snd_pcm_drain(_saudio.backend.device);
snd_pcm_close(_saudio.backend.device);
_saudio_free(_saudio.backend.buffer);
};
// ██ ██ █████ ███████ █████ ██████ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ █ ██ ███████ ███████ ███████ ██████ ██
// ██ ███ ██ ██ ██ ██ ██ ██ ██ ██
// ███ ███ ██ ██ ███████ ██ ██ ██ ██
//
// >>wasapi
#elif defined(_SAUDIO_WINDOWS)
/* fill intermediate buffer with new data and reset buffer_pos */
_SOKOL_PRIVATE void _saudio_wasapi_fill_buffer(void) {
if (_saudio_has_callback()) {
_saudio_stream_callback(_saudio.backend.thread.src_buffer, _saudio.backend.thread.src_buffer_frames, _saudio.num_channels);
}
else {
if (0 == _saudio_fifo_read(&_saudio.fifo, (uint8_t*)_saudio.backend.thread.src_buffer, _saudio.backend.thread.src_buffer_byte_size)) {
/* not enough read data available, fill the entire buffer with silence */
_saudio_clear(_saudio.backend.thread.src_buffer, (size_t)_saudio.backend.thread.src_buffer_byte_size);
}
}
}
_SOKOL_PRIVATE int _saudio_wasapi_min(int a, int b) {
return (a < b) ? a : b;
}
_SOKOL_PRIVATE void _saudio_wasapi_submit_buffer(int num_frames) {
BYTE* wasapi_buffer = 0;
if (FAILED(IAudioRenderClient_GetBuffer(_saudio.backend.render_client, num_frames, &wasapi_buffer))) {
return;
}
SOKOL_ASSERT(wasapi_buffer);
/* copy samples to WASAPI buffer, refill source buffer if needed */
int num_remaining_samples = num_frames * _saudio.num_channels;
int buffer_pos = _saudio.backend.thread.src_buffer_pos;
const int buffer_size_in_samples = _saudio.backend.thread.src_buffer_byte_size / (int)sizeof(float);
float* dst = (float*)wasapi_buffer;
const float* dst_end = dst + num_remaining_samples;
_SOKOL_UNUSED(dst_end); // suppress unused warning in release mode
const float* src = _saudio.backend.thread.src_buffer;
while (num_remaining_samples > 0) {
if (0 == buffer_pos) {
_saudio_wasapi_fill_buffer();
}
const int samples_to_copy = _saudio_wasapi_min(num_remaining_samples, buffer_size_in_samples - buffer_pos);
SOKOL_ASSERT((buffer_pos + samples_to_copy) <= buffer_size_in_samples);
SOKOL_ASSERT((dst + samples_to_copy) <= dst_end);
memcpy(dst, &src[buffer_pos], (size_t)samples_to_copy * sizeof(float));
num_remaining_samples -= samples_to_copy;
SOKOL_ASSERT(num_remaining_samples >= 0);
buffer_pos += samples_to_copy;
dst += samples_to_copy;
SOKOL_ASSERT(buffer_pos <= buffer_size_in_samples);
if (buffer_pos == buffer_size_in_samples) {
buffer_pos = 0;
}
}
_saudio.backend.thread.src_buffer_pos = buffer_pos;
IAudioRenderClient_ReleaseBuffer(_saudio.backend.render_client, num_frames, 0);
}
_SOKOL_PRIVATE DWORD WINAPI _saudio_wasapi_thread_fn(LPVOID param) {
(void)param;
_saudio_wasapi_submit_buffer(_saudio.backend.thread.src_buffer_frames);
IAudioClient_Start(_saudio.backend.audio_client);
while (!_saudio.backend.thread.stop) {
WaitForSingleObject(_saudio.backend.thread.buffer_end_event, INFINITE);
UINT32 padding = 0;
if (FAILED(IAudioClient_GetCurrentPadding(_saudio.backend.audio_client, &padding))) {
continue;
}
SOKOL_ASSERT(_saudio.backend.thread.dst_buffer_frames >= padding);
int num_frames = (int)_saudio.backend.thread.dst_buffer_frames - (int)padding;
if (num_frames > 0) {
_saudio_wasapi_submit_buffer(num_frames);
}
}
return 0;
}
_SOKOL_PRIVATE void _saudio_wasapi_release(void) {
if (_saudio.backend.thread.src_buffer) {
_saudio_free(_saudio.backend.thread.src_buffer);
_saudio.backend.thread.src_buffer = 0;
}
if (_saudio.backend.render_client) {
IAudioRenderClient_Release(_saudio.backend.render_client);
_saudio.backend.render_client = 0;
}
if (_saudio.backend.audio_client) {
IAudioClient_Release(_saudio.backend.audio_client);
_saudio.backend.audio_client = 0;
}
if (_saudio.backend.device) {
IMMDevice_Release(_saudio.backend.device);
_saudio.backend.device = 0;
}
if (_saudio.backend.device_enumerator) {
IMMDeviceEnumerator_Release(_saudio.backend.device_enumerator);
_saudio.backend.device_enumerator = 0;
}
if (0 != _saudio.backend.thread.buffer_end_event) {
CloseHandle(_saudio.backend.thread.buffer_end_event);
_saudio.backend.thread.buffer_end_event = 0;
}
}
_SOKOL_PRIVATE bool _saudio_wasapi_backend_init(void) {
REFERENCE_TIME dur;
/* CoInitializeEx could have been called elsewhere already, in which
case the function returns with S_FALSE (thus it does not make much
sense to check the result)
*/
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
_SOKOL_UNUSED(hr);
_saudio.backend.thread.buffer_end_event = CreateEvent(0, FALSE, FALSE, 0);
if (0 == _saudio.backend.thread.buffer_end_event) {
_SAUDIO_ERROR(WASAPI_CREATE_EVENT_FAILED);
goto error;
}
if (FAILED(CoCreateInstance(_SOKOL_AUDIO_WIN32COM_ID(_saudio_CLSID_IMMDeviceEnumerator),
0, CLSCTX_ALL,
_SOKOL_AUDIO_WIN32COM_ID(_saudio_IID_IMMDeviceEnumerator),
(void**)&_saudio.backend.device_enumerator)))
{
_SAUDIO_ERROR(WASAPI_CREATE_DEVICE_ENUMERATOR_FAILED);
goto error;
}
if (FAILED(IMMDeviceEnumerator_GetDefaultAudioEndpoint(_saudio.backend.device_enumerator,
eRender, eConsole,
&_saudio.backend.device)))
{
_SAUDIO_ERROR(WASAPI_GET_DEFAULT_AUDIO_ENDPOINT_FAILED);
goto error;
}
if (FAILED(IMMDevice_Activate(_saudio.backend.device,
_SOKOL_AUDIO_WIN32COM_ID(_saudio_IID_IAudioClient),
CLSCTX_ALL, 0,
(void**)&_saudio.backend.audio_client)))
{
_SAUDIO_ERROR(WASAPI_DEVICE_ACTIVATE_FAILED);
goto error;
}
WAVEFORMATEXTENSIBLE fmtex;
_saudio_clear(&fmtex, sizeof(fmtex));
fmtex.Format.nChannels = (WORD)_saudio.num_channels;
fmtex.Format.nSamplesPerSec = (DWORD)_saudio.sample_rate;
fmtex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
fmtex.Format.wBitsPerSample = 32;
fmtex.Format.nBlockAlign = (fmtex.Format.nChannels * fmtex.Format.wBitsPerSample) / 8;
fmtex.Format.nAvgBytesPerSec = fmtex.Format.nSamplesPerSec * fmtex.Format.nBlockAlign;
fmtex.Format.cbSize = 22; /* WORD + DWORD + GUID */
fmtex.Samples.wValidBitsPerSample = 32;
if (_saudio.num_channels == 1) {
fmtex.dwChannelMask = SPEAKER_FRONT_CENTER;
}
else {
fmtex.dwChannelMask = SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT;
}
fmtex.SubFormat = _saudio_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
dur = (REFERENCE_TIME)
(((double)_saudio.buffer_frames) / (((double)_saudio.sample_rate) * (1.0/10000000.0)));
if (FAILED(IAudioClient_Initialize(_saudio.backend.audio_client,
AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_EVENTCALLBACK|AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM|AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY,
dur, 0, (WAVEFORMATEX*)&fmtex, 0)))
{
_SAUDIO_ERROR(WASAPI_AUDIO_CLIENT_INITIALIZE_FAILED);
goto error;
}
if (FAILED(IAudioClient_GetBufferSize(_saudio.backend.audio_client, &_saudio.backend.thread.dst_buffer_frames))) {
_SAUDIO_ERROR(WASAPI_AUDIO_CLIENT_GET_BUFFER_SIZE_FAILED);
goto error;
}
if (FAILED(IAudioClient_GetService(_saudio.backend.audio_client,
_SOKOL_AUDIO_WIN32COM_ID(_saudio_IID_IAudioRenderClient),
(void**)&_saudio.backend.render_client)))
{
_SAUDIO_ERROR(WASAPI_AUDIO_CLIENT_GET_SERVICE_FAILED);
goto error;
}
if (FAILED(IAudioClient_SetEventHandle(_saudio.backend.audio_client, _saudio.backend.thread.buffer_end_event))) {
_SAUDIO_ERROR(WASAPI_AUDIO_CLIENT_SET_EVENT_HANDLE_FAILED);
goto error;
}
_saudio.bytes_per_frame = _saudio.num_channels * (int)sizeof(float);
_saudio.backend.thread.src_buffer_frames = _saudio.buffer_frames;
_saudio.backend.thread.src_buffer_byte_size = _saudio.backend.thread.src_buffer_frames * _saudio.bytes_per_frame;
/* allocate an intermediate buffer for sample format conversion */
_saudio.backend.thread.src_buffer = (float*) _saudio_malloc((size_t)_saudio.backend.thread.src_buffer_byte_size);
/* create streaming thread */
_saudio.backend.thread.thread_handle = CreateThread(NULL, 0, _saudio_wasapi_thread_fn, 0, 0, 0);
if (0 == _saudio.backend.thread.thread_handle) {
_SAUDIO_ERROR(WASAPI_CREATE_THREAD_FAILED);
goto error;
}
return true;
error:
_saudio_wasapi_release();
return false;
}
_SOKOL_PRIVATE void _saudio_wasapi_backend_shutdown(void) {
if (_saudio.backend.thread.thread_handle) {
_saudio.backend.thread.stop = true;
SetEvent(_saudio.backend.thread.buffer_end_event);
WaitForSingleObject(_saudio.backend.thread.thread_handle, INFINITE);
CloseHandle(_saudio.backend.thread.thread_handle);
_saudio.backend.thread.thread_handle = 0;
}
if (_saudio.backend.audio_client) {
IAudioClient_Stop(_saudio.backend.audio_client);
}
_saudio_wasapi_release();
CoUninitialize();
}
// ██ ██ ███████ ██████ █████ ██ ██ ██████ ██ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ █ ██ █████ ██████ ███████ ██ ██ ██ ██ ██ ██ ██
// ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███ ███ ███████ ██████ ██ ██ ██████ ██████ ██ ██████
//
// >>webaudio
#elif defined(_SAUDIO_EMSCRIPTEN)
#ifdef __cplusplus
extern "C" {
#endif
EMSCRIPTEN_KEEPALIVE int _saudio_emsc_pull(int num_frames) {
SOKOL_ASSERT(_saudio.backend.buffer);
if (num_frames == _saudio.buffer_frames) {
if (_saudio_has_callback()) {
_saudio_stream_callback((float*)_saudio.backend.buffer, num_frames, _saudio.num_channels);
}
else {
const int num_bytes = num_frames * _saudio.bytes_per_frame;
if (0 == _saudio_fifo_read(&_saudio.fifo, _saudio.backend.buffer, num_bytes)) {
/* not enough read data available, fill the entire buffer with silence */
_saudio_clear(_saudio.backend.buffer, (size_t)num_bytes);
}
}
int res = (int) _saudio.backend.buffer;
return res;
}
else {
return 0;
}
}
#ifdef __cplusplus
} /* extern "C" */
#endif
/* setup the WebAudio context and attach a ScriptProcessorNode */
EM_JS(int, saudio_js_init, (int sample_rate, int num_channels, int buffer_size), {
Module._saudio_context = null;
Module._saudio_node = null;
if (typeof AudioContext !== 'undefined') {
Module._saudio_context = new AudioContext({
sampleRate: sample_rate,
latencyHint: 'interactive',
});
}
else {
Module._saudio_context = null;
console.log('sokol_audio.h: no WebAudio support');
}
if (Module._saudio_context) {
console.log('sokol_audio.h: sample rate ', Module._saudio_context.sampleRate);
Module._saudio_node = Module._saudio_context.createScriptProcessor(buffer_size, 0, num_channels);
Module._saudio_node.onaudioprocess = (event) => {
const num_frames = event.outputBuffer.length;
const ptr = __saudio_emsc_pull(num_frames);
if (ptr) {
const num_channels = event.outputBuffer.numberOfChannels;
for (let chn = 0; chn < num_channels; chn++) {
const chan = event.outputBuffer.getChannelData(chn);
for (let i = 0; i < num_frames; i++) {
chan[i] = HEAPF32[(ptr>>2) + ((num_channels*i)+chn)]
}
}
}
};
Module._saudio_node.connect(Module._saudio_context.destination);
// in some browsers, WebAudio needs to be activated on a user action
const resume_webaudio = () => {
if (Module._saudio_context) {
if (Module._saudio_context.state === 'suspended') {
Module._saudio_context.resume();
}
}
};
document.addEventListener('click', resume_webaudio, {once:true});
document.addEventListener('touchend', resume_webaudio, {once:true});
document.addEventListener('keydown', resume_webaudio, {once:true});
return 1;
}
else {
return 0;
}
});
/* shutdown the WebAudioContext and ScriptProcessorNode */
EM_JS(void, saudio_js_shutdown, (void), {
\x2F\x2A\x2A @suppress {missingProperties} \x2A\x2F
const ctx = Module._saudio_context;
if (ctx !== null) {
if (Module._saudio_node) {
Module._saudio_node.disconnect();
}
ctx.close();
Module._saudio_context = null;
Module._saudio_node = null;
}
});
/* get the actual sample rate back from the WebAudio context */
EM_JS(int, saudio_js_sample_rate, (void), {
if (Module._saudio_context) {
return Module._saudio_context.sampleRate;
}
else {
return 0;
}
});
/* get the actual buffer size in number of frames */
EM_JS(int, saudio_js_buffer_frames, (void), {
if (Module._saudio_node) {
return Module._saudio_node.bufferSize;
}
else {
return 0;
}
});
/* return 1 if the WebAudio context is currently suspended, else 0 */
EM_JS(int, saudio_js_suspended, (void), {
if (Module._saudio_context) {
if (Module._saudio_context.state === 'suspended') {
return 1;
}
else {
return 0;
}
}
});
_SOKOL_PRIVATE bool _saudio_webaudio_backend_init(void) {
if (saudio_js_init(_saudio.sample_rate, _saudio.num_channels, _saudio.buffer_frames)) {
_saudio.bytes_per_frame = (int)sizeof(float) * _saudio.num_channels;
_saudio.sample_rate = saudio_js_sample_rate();
_saudio.buffer_frames = saudio_js_buffer_frames();
const size_t buf_size = (size_t) (_saudio.buffer_frames * _saudio.bytes_per_frame);
_saudio.backend.buffer = (uint8_t*) _saudio_malloc(buf_size);
return true;
}
else {
return false;
}
}
_SOKOL_PRIVATE void _saudio_webaudio_backend_shutdown(void) {
saudio_js_shutdown();
if (_saudio.backend.buffer) {
_saudio_free(_saudio.backend.buffer);
_saudio.backend.buffer = 0;
}
}
// █████ █████ ██ ██ ██████ ██ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ███████ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██████ ██████ ██ ██████
//
// >>aaudio
#elif defined(SAUDIO_ANDROID_AAUDIO)
_SOKOL_PRIVATE aaudio_data_callback_result_t _saudio_aaudio_data_callback(AAudioStream* stream, void* user_data, void* audio_data, int32_t num_frames) {
_SOKOL_UNUSED(user_data);
_SOKOL_UNUSED(stream);
if (_saudio_has_callback()) {
_saudio_stream_callback((float*)audio_data, (int)num_frames, _saudio.num_channels);
}
else {
uint8_t* ptr = (uint8_t*)audio_data;
int num_bytes = _saudio.bytes_per_frame * num_frames;
if (0 == _saudio_fifo_read(&_saudio.fifo, ptr, num_bytes)) {
// not enough read data available, fill the entire buffer with silence
memset(ptr, 0, (size_t)num_bytes);
}
}
return AAUDIO_CALLBACK_RESULT_CONTINUE;
}
_SOKOL_PRIVATE bool _saudio_aaudio_start_stream(void) {
if (AAudioStreamBuilder_openStream(_saudio.backend.builder, &_saudio.backend.stream) != AAUDIO_OK) {
_SAUDIO_ERROR(AAUDIO_STREAMBUILDER_OPEN_STREAM_FAILED);
return false;
}
AAudioStream_requestStart(_saudio.backend.stream);
return true;
}
_SOKOL_PRIVATE void _saudio_aaudio_stop_stream(void) {
if (_saudio.backend.stream) {
AAudioStream_requestStop(_saudio.backend.stream);
AAudioStream_close(_saudio.backend.stream);
_saudio.backend.stream = 0;
}
}
_SOKOL_PRIVATE void* _saudio_aaudio_restart_stream_thread_fn(void* param) {
_SOKOL_UNUSED(param);
_SAUDIO_WARN(AAUDIO_RESTARTING_STREAM_AFTER_ERROR);
pthread_mutex_lock(&_saudio.backend.mutex);
_saudio_aaudio_stop_stream();
_saudio_aaudio_start_stream();
pthread_mutex_unlock(&_saudio.backend.mutex);
return 0;
}
_SOKOL_PRIVATE void _saudio_aaudio_error_callback(AAudioStream* stream, void* user_data, aaudio_result_t error) {
_SOKOL_UNUSED(stream);
_SOKOL_UNUSED(user_data);
if (error == AAUDIO_ERROR_DISCONNECTED) {
if (0 != pthread_create(&_saudio.backend.thread, 0, _saudio_aaudio_restart_stream_thread_fn, 0)) {
_SAUDIO_ERROR(AAUDIO_PTHREAD_CREATE_FAILED);
}
}
}
_SOKOL_PRIVATE void _saudio_aaudio_backend_shutdown(void) {
pthread_mutex_lock(&_saudio.backend.mutex);
_saudio_aaudio_stop_stream();
pthread_mutex_unlock(&_saudio.backend.mutex);
if (_saudio.backend.builder) {
AAudioStreamBuilder_delete(_saudio.backend.builder);
_saudio.backend.builder = 0;
}
pthread_mutex_destroy(&_saudio.backend.mutex);
}
_SOKOL_PRIVATE bool _saudio_aaudio_backend_init(void) {
_SAUDIO_INFO(USING_AAUDIO_BACKEND);
_saudio.bytes_per_frame = _saudio.num_channels * (int)sizeof(float);
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutex_init(&_saudio.backend.mutex, &attr);
if (AAudio_createStreamBuilder(&_saudio.backend.builder) != AAUDIO_OK) {
_SAUDIO_ERROR(AAUDIO_CREATE_STREAMBUILDER_FAILED);
_saudio_aaudio_backend_shutdown();
return false;
}
AAudioStreamBuilder_setFormat(_saudio.backend.builder, AAUDIO_FORMAT_PCM_FLOAT);
AAudioStreamBuilder_setSampleRate(_saudio.backend.builder, _saudio.sample_rate);
AAudioStreamBuilder_setChannelCount(_saudio.backend.builder, _saudio.num_channels);
AAudioStreamBuilder_setBufferCapacityInFrames(_saudio.backend.builder, _saudio.buffer_frames * 2);
AAudioStreamBuilder_setFramesPerDataCallback(_saudio.backend.builder, _saudio.buffer_frames);
AAudioStreamBuilder_setDataCallback(_saudio.backend.builder, _saudio_aaudio_data_callback, 0);
AAudioStreamBuilder_setErrorCallback(_saudio.backend.builder, _saudio_aaudio_error_callback, 0);
if (!_saudio_aaudio_start_stream()) {
_saudio_aaudio_backend_shutdown();
return false;
}
return true;
}
// ██████ ██████ ███████ ███ ██ ███████ ██ ███████ ███████
// ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██
// ██ ██ ██████ █████ ██ ██ ██ ███████ ██ █████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ███████ ██ ████ ███████ ███████ ███████ ███████
//
// >>opensles
// >>sles
#elif defined(SAUDIO_ANDROID_SLES)
_SOKOL_PRIVATE void _saudio_sles_semaphore_init(_saudio_sles_semaphore_t* sem) {
sem->count = 0;
int r = pthread_mutex_init(&sem->mutex, NULL);
SOKOL_ASSERT(r == 0);
r = pthread_cond_init(&sem->cond, NULL);
SOKOL_ASSERT(r == 0);
(void)(r);
}
_SOKOL_PRIVATE void _saudio_sles_semaphore_destroy(_saudio_sles_semaphore_t* sem) {
pthread_cond_destroy(&sem->cond);
pthread_mutex_destroy(&sem->mutex);
}
_SOKOL_PRIVATE void _saudio_sles_semaphore_post(_saudio_sles_semaphore_t* sem, int count) {
int r = pthread_mutex_lock(&sem->mutex);
SOKOL_ASSERT(r == 0);
for (int ii = 0; ii < count; ii++) {
r = pthread_cond_signal(&sem->cond);
SOKOL_ASSERT(r == 0);
}
sem->count += count;
r = pthread_mutex_unlock(&sem->mutex);
SOKOL_ASSERT(r == 0);
(void)(r);
}
_SOKOL_PRIVATE bool _saudio_sles_semaphore_wait(_saudio_sles_semaphore_t* sem) {
int r = pthread_mutex_lock(&sem->mutex);
SOKOL_ASSERT(r == 0);
while (r == 0 && sem->count <= 0) {
r = pthread_cond_wait(&sem->cond, &sem->mutex);
}
bool ok = (r == 0);
if (ok) {
--sem->count;
}
r = pthread_mutex_unlock(&sem->mutex);
(void)(r);
return ok;
}
/* fill intermediate buffer with new data and reset buffer_pos */
_SOKOL_PRIVATE void _saudio_sles_fill_buffer(void) {
int src_buffer_frames = _saudio.buffer_frames;
if (_saudio_has_callback()) {
_saudio_stream_callback(_saudio.backend.src_buffer, src_buffer_frames, _saudio.num_channels);
}
else {
const int src_buffer_byte_size = src_buffer_frames * _saudio.num_channels * (int)sizeof(float);
if (0 == _saudio_fifo_read(&_saudio.fifo, (uint8_t*)_saudio.backend.src_buffer, src_buffer_byte_size)) {
/* not enough read data available, fill the entire buffer with silence */
_saudio_clear(_saudio.backend.src_buffer, (size_t)src_buffer_byte_size);
}
}
}
_SOKOL_PRIVATE void SLAPIENTRY _saudio_sles_play_cb(SLPlayItf player, void *context, SLuint32 event) {
_SOKOL_UNUSED(context);
_SOKOL_UNUSED(player);
if (event & SL_PLAYEVENT_HEADATEND) {
_saudio_sles_semaphore_post(&_saudio.backend.buffer_sem, 1);
}
}
_SOKOL_PRIVATE void* _saudio_sles_thread_fn(void* param) {
_SOKOL_UNUSED(param);
while (!_saudio.backend.thread_stop) {
/* get next output buffer, advance, next buffer. */
int16_t* out_buffer = _saudio.backend.output_buffers[_saudio.backend.active_buffer];
_saudio.backend.active_buffer = (_saudio.backend.active_buffer + 1) % SAUDIO_SLES_NUM_BUFFERS;
int16_t* next_buffer = _saudio.backend.output_buffers[_saudio.backend.active_buffer];
/* queue this buffer */
const int buffer_size_bytes = _saudio.buffer_frames * _saudio.num_channels * (int)sizeof(short);
(*_saudio.backend.player_buffer_queue)->Enqueue(_saudio.backend.player_buffer_queue, out_buffer, (SLuint32)buffer_size_bytes);
/* fill the next buffer */
_saudio_sles_fill_buffer();
const int num_samples = _saudio.num_channels * _saudio.buffer_frames;
for (int i = 0; i < num_samples; ++i) {
next_buffer[i] = (int16_t) (_saudio.backend.src_buffer[i] * 0x7FFF);
}
_saudio_sles_semaphore_wait(&_saudio.backend.buffer_sem);
}
return 0;
}
_SOKOL_PRIVATE void _saudio_sles_backend_shutdown(void) {
_saudio.backend.thread_stop = 1;
pthread_join(_saudio.backend.thread, 0);
if (_saudio.backend.player_obj) {
(*_saudio.backend.player_obj)->Destroy(_saudio.backend.player_obj);
}
if (_saudio.backend.output_mix_obj) {
(*_saudio.backend.output_mix_obj)->Destroy(_saudio.backend.output_mix_obj);
}
if (_saudio.backend.engine_obj) {
(*_saudio.backend.engine_obj)->Destroy(_saudio.backend.engine_obj);
}
for (int i = 0; i < SAUDIO_SLES_NUM_BUFFERS; i++) {
_saudio_free(_saudio.backend.output_buffers[i]);
}
_saudio_free(_saudio.backend.src_buffer);
}
_SOKOL_PRIVATE bool _saudio_sles_backend_init(void) {
_SAUDIO_INFO(USING_SLES_BACKEND);
_saudio.bytes_per_frame = (int)sizeof(float) * _saudio.num_channels;
for (int i = 0; i < SAUDIO_SLES_NUM_BUFFERS; ++i) {
const int buffer_size_bytes = (int)sizeof(int16_t) * _saudio.num_channels * _saudio.buffer_frames;
_saudio.backend.output_buffers[i] = (int16_t*) _saudio_malloc_clear((size_t)buffer_size_bytes);
}
{
const int buffer_size_bytes = _saudio.bytes_per_frame * _saudio.buffer_frames;
_saudio.backend.src_buffer = (float*) _saudio_malloc_clear((size_t)buffer_size_bytes);
}
/* Create engine */
const SLEngineOption opts[] = { { SL_ENGINEOPTION_THREADSAFE, SL_BOOLEAN_TRUE } };
if (slCreateEngine(&_saudio.backend.engine_obj, 1, opts, 0, NULL, NULL ) != SL_RESULT_SUCCESS) {
_SAUDIO_ERROR(SLES_CREATE_ENGINE_FAILED);
_saudio_sles_backend_shutdown();
return false;
}
(*_saudio.backend.engine_obj)->Realize(_saudio.backend.engine_obj, SL_BOOLEAN_FALSE);
if ((*_saudio.backend.engine_obj)->GetInterface(_saudio.backend.engine_obj, SL_IID_ENGINE, &_saudio.backend.engine) != SL_RESULT_SUCCESS) {
_SAUDIO_ERROR(SLES_ENGINE_GET_ENGINE_INTERFACE_FAILED);
_saudio_sles_backend_shutdown();
return false;
}
/* Create output mix. */
{
const SLInterfaceID ids[] = { SL_IID_VOLUME };
const SLboolean req[] = { SL_BOOLEAN_FALSE };
if ((*_saudio.backend.engine)->CreateOutputMix(_saudio.backend.engine, &_saudio.backend.output_mix_obj, 1, ids, req) != SL_RESULT_SUCCESS) {
_SAUDIO_ERROR(SLES_CREATE_OUTPUT_MIX_FAILED);
_saudio_sles_backend_shutdown();
return false;
}
(*_saudio.backend.output_mix_obj)->Realize(_saudio.backend.output_mix_obj, SL_BOOLEAN_FALSE);
if ((*_saudio.backend.output_mix_obj)->GetInterface(_saudio.backend.output_mix_obj, SL_IID_VOLUME, &_saudio.backend.output_mix_vol) != SL_RESULT_SUCCESS) {
_SAUDIO_WARN(SLES_MIXER_GET_VOLUME_INTERFACE_FAILED);
}
}
/* android buffer queue */
_saudio.backend.in_locator.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
_saudio.backend.in_locator.numBuffers = SAUDIO_SLES_NUM_BUFFERS;
/* data format */
SLDataFormat_PCM format;
format.formatType = SL_DATAFORMAT_PCM;
format.numChannels = (SLuint32)_saudio.num_channels;
format.samplesPerSec = (SLuint32) (_saudio.sample_rate * 1000);
format.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
format.containerSize = 16;
format.endianness = SL_BYTEORDER_LITTLEENDIAN;
if (_saudio.num_channels == 2) {
format.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
} else {
format.channelMask = SL_SPEAKER_FRONT_CENTER;
}
SLDataSource src;
src.pLocator = &_saudio.backend.in_locator;
src.pFormat = &format;
/* Output mix. */
_saudio.backend.out_locator.locatorType = SL_DATALOCATOR_OUTPUTMIX;
_saudio.backend.out_locator.outputMix = _saudio.backend.output_mix_obj;
_saudio.backend.dst_data_sink.pLocator = &_saudio.backend.out_locator;
_saudio.backend.dst_data_sink.pFormat = NULL;
/* setup player */
{
const SLInterfaceID ids[] = { SL_IID_VOLUME, SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
const SLboolean req[] = { SL_BOOLEAN_FALSE, SL_BOOLEAN_TRUE };
if ((*_saudio.backend.engine)->CreateAudioPlayer(_saudio.backend.engine, &_saudio.backend.player_obj, &src, &_saudio.backend.dst_data_sink, sizeof(ids) / sizeof(ids[0]), ids, req) != SL_RESULT_SUCCESS)
{
_SAUDIO_ERROR(SLES_ENGINE_CREATE_AUDIO_PLAYER_FAILED);
_saudio_sles_backend_shutdown();
return false;
}
(*_saudio.backend.player_obj)->Realize(_saudio.backend.player_obj, SL_BOOLEAN_FALSE);
if ((*_saudio.backend.player_obj)->GetInterface(_saudio.backend.player_obj, SL_IID_PLAY, &_saudio.backend.player) != SL_RESULT_SUCCESS) {
_SAUDIO_ERROR(SLES_PLAYER_GET_PLAY_INTERFACE_FAILED);
_saudio_sles_backend_shutdown();
return false;
}
if ((*_saudio.backend.player_obj)->GetInterface(_saudio.backend.player_obj, SL_IID_VOLUME, &_saudio.backend.player_vol) != SL_RESULT_SUCCESS) {
_SAUDIO_ERROR(SLES_PLAYER_GET_VOLUME_INTERFACE_FAILED);
}
if ((*_saudio.backend.player_obj)->GetInterface(_saudio.backend.player_obj, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &_saudio.backend.player_buffer_queue) != SL_RESULT_SUCCESS) {
_SAUDIO_ERROR(SLES_PLAYER_GET_BUFFERQUEUE_INTERFACE_FAILED);
_saudio_sles_backend_shutdown();
return false;
}
}
/* begin */
{
const int buffer_size_bytes = (int)sizeof(int16_t) * _saudio.num_channels * _saudio.buffer_frames;
(*_saudio.backend.player_buffer_queue)->Enqueue(_saudio.backend.player_buffer_queue, _saudio.backend.output_buffers[0], (SLuint32)buffer_size_bytes);
_saudio.backend.active_buffer = (_saudio.backend.active_buffer + 1) % SAUDIO_SLES_NUM_BUFFERS;
(*_saudio.backend.player)->RegisterCallback(_saudio.backend.player, _saudio_sles_play_cb, NULL);
(*_saudio.backend.player)->SetCallbackEventsMask(_saudio.backend.player, SL_PLAYEVENT_HEADATEND);
(*_saudio.backend.player)->SetPlayState(_saudio.backend.player, SL_PLAYSTATE_PLAYING);
}
/* create the buffer-streaming start thread */
if (0 != pthread_create(&_saudio.backend.thread, 0, _saudio_sles_thread_fn, 0)) {
_saudio_sles_backend_shutdown();
return false;
}
return true;
}
// ██████ ██████ ██████ ███████ █████ ██ ██ ██████ ██ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██████ █████ ███████ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██████ ██ ██ ███████ ██ ██ ██████ ██████ ██ ██████
//
// >>coreaudio
#elif defined(_SAUDIO_APPLE)
#if defined(_SAUDIO_IOS)
#if __has_feature(objc_arc)
#define _SAUDIO_OBJC_RELEASE(obj) { obj = nil; }
#else
#define _SAUDIO_OBJC_RELEASE(obj) { [obj release]; obj = nil; }
#endif
@interface _saudio_interruption_handler : NSObject { }
@end
@implementation _saudio_interruption_handler
-(id)init {
self = [super init];
AVAudioSession* session = [AVAudioSession sharedInstance];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handle_interruption:) name:AVAudioSessionInterruptionNotification object:session];
return self;
}
-(void)dealloc {
[self remove_handler];
#if !__has_feature(objc_arc)
[super dealloc];
#endif
}
-(void)remove_handler {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVAudioSessionInterruptionNotification" object:nil];
}
-(void)handle_interruption:(NSNotification*)notification {
AVAudioSession* session = [AVAudioSession sharedInstance];
SOKOL_ASSERT(session);
NSDictionary* dict = notification.userInfo;
SOKOL_ASSERT(dict);
NSInteger type = [[dict valueForKey:AVAudioSessionInterruptionTypeKey] integerValue];
switch (type) {
case AVAudioSessionInterruptionTypeBegan:
if (_saudio.backend.ca_audio_queue) {
AudioQueuePause(_saudio.backend.ca_audio_queue);
}
[session setActive:false error:nil];
break;
case AVAudioSessionInterruptionTypeEnded:
[session setActive:true error:nil];
if (_saudio.backend.ca_audio_queue) {
AudioQueueStart(_saudio.backend.ca_audio_queue, NULL);
}
break;
default:
break;
}
}
@end
#endif // _SAUDIO_IOS
/* NOTE: the buffer data callback is called on a separate thread! */
_SOKOL_PRIVATE void _saudio_coreaudio_callback(void* user_data, _saudio_AudioQueueRef queue, _saudio_AudioQueueBufferRef buffer) {
_SOKOL_UNUSED(user_data);
if (_saudio_has_callback()) {
const int num_frames = (int)buffer->mAudioDataByteSize / _saudio.bytes_per_frame;
const int num_channels = _saudio.num_channels;
_saudio_stream_callback((float*)buffer->mAudioData, num_frames, num_channels);
}
else {
uint8_t* ptr = (uint8_t*)buffer->mAudioData;
int num_bytes = (int) buffer->mAudioDataByteSize;
if (0 == _saudio_fifo_read(&_saudio.fifo, ptr, num_bytes)) {
/* not enough read data available, fill the entire buffer with silence */
_saudio_clear(ptr, (size_t)num_bytes);
}
}
AudioQueueEnqueueBuffer(queue, buffer, 0, NULL);
}
_SOKOL_PRIVATE void _saudio_coreaudio_backend_shutdown(void) {
if (_saudio.backend.ca_audio_queue) {
AudioQueueStop(_saudio.backend.ca_audio_queue, true);
AudioQueueDispose(_saudio.backend.ca_audio_queue, false);
_saudio.backend.ca_audio_queue = 0;
}
#if defined(_SAUDIO_IOS)
/* remove interruption handler */
if (_saudio.backend.ca_interruption_handler != nil) {
[_saudio.backend.ca_interruption_handler remove_handler];
_SAUDIO_OBJC_RELEASE(_saudio.backend.ca_interruption_handler);
}
/* deactivate audio session */
AVAudioSession* session = [AVAudioSession sharedInstance];
SOKOL_ASSERT(session);
[session setActive:false error:nil];;
#endif // _SAUDIO_IOS
}
_SOKOL_PRIVATE bool _saudio_coreaudio_backend_init(void) {
SOKOL_ASSERT(0 == _saudio.backend.ca_audio_queue);
#if defined(_SAUDIO_IOS)
/* activate audio session */
AVAudioSession* session = [AVAudioSession sharedInstance];
SOKOL_ASSERT(session != nil);
[session setCategory: AVAudioSessionCategoryPlayback error:nil];
[session setActive:true error:nil];
/* create interruption handler */
_saudio.backend.ca_interruption_handler = [[_saudio_interruption_handler alloc] init];
#endif
/* create an audio queue with fp32 samples */
_saudio_AudioStreamBasicDescription fmt;
_saudio_clear(&fmt, sizeof(fmt));
fmt.mSampleRate = (double) _saudio.sample_rate;
fmt.mFormatID = _saudio_kAudioFormatLinearPCM;
fmt.mFormatFlags = _saudio_kLinearPCMFormatFlagIsFloat | _saudio_kAudioFormatFlagIsPacked;
fmt.mFramesPerPacket = 1;
fmt.mChannelsPerFrame = (uint32_t) _saudio.num_channels;
fmt.mBytesPerFrame = (uint32_t)sizeof(float) * (uint32_t)_saudio.num_channels;
fmt.mBytesPerPacket = fmt.mBytesPerFrame;
fmt.mBitsPerChannel = 32;
_saudio_OSStatus res = AudioQueueNewOutput(&fmt, _saudio_coreaudio_callback, 0, NULL, NULL, 0, &_saudio.backend.ca_audio_queue);
if (0 != res) {
_SAUDIO_ERROR(COREAUDIO_NEW_OUTPUT_FAILED);
return false;
}
SOKOL_ASSERT(_saudio.backend.ca_audio_queue);
/* create 2 audio buffers */
for (int i = 0; i < 2; i++) {
_saudio_AudioQueueBufferRef buf = NULL;
const uint32_t buf_byte_size = (uint32_t)_saudio.buffer_frames * fmt.mBytesPerFrame;
res = AudioQueueAllocateBuffer(_saudio.backend.ca_audio_queue, buf_byte_size, &buf);
if (0 != res) {
_SAUDIO_ERROR(COREAUDIO_ALLOCATE_BUFFER_FAILED);
_saudio_coreaudio_backend_shutdown();
return false;
}
buf->mAudioDataByteSize = buf_byte_size;
_saudio_clear(buf->mAudioData, buf->mAudioDataByteSize);
AudioQueueEnqueueBuffer(_saudio.backend.ca_audio_queue, buf, 0, NULL);
}
/* init or modify actual playback parameters */
_saudio.bytes_per_frame = (int)fmt.mBytesPerFrame;
/* ...and start playback */
res = AudioQueueStart(_saudio.backend.ca_audio_queue, NULL);
if (0 != res) {
_SAUDIO_ERROR(COREAUDIO_START_FAILED);
_saudio_coreaudio_backend_shutdown();
return false;
}
return true;
}
#else
#error "unsupported platform"
#endif
bool _saudio_backend_init(void) {
#if defined(SOKOL_DUMMY_BACKEND)
return _saudio_dummy_backend_init();
#elif defined(_SAUDIO_LINUX)
return _saudio_alsa_backend_init();
#elif defined(_SAUDIO_WINDOWS)
return _saudio_wasapi_backend_init();
#elif defined(_SAUDIO_EMSCRIPTEN)
return _saudio_webaudio_backend_init();
#elif defined(SAUDIO_ANDROID_AAUDIO)
return _saudio_aaudio_backend_init();
#elif defined(SAUDIO_ANDROID_SLES)
return _saudio_sles_backend_init();
#elif defined(_SAUDIO_APPLE)
return _saudio_coreaudio_backend_init();
#else
#error "unknown platform"
#endif
}
void _saudio_backend_shutdown(void) {
#if defined(SOKOL_DUMMY_BACKEND)
_saudio_dummy_backend_shutdown();
#elif defined(_SAUDIO_LINUX)
_saudio_alsa_backend_shutdown();
#elif defined(_SAUDIO_WINDOWS)
_saudio_wasapi_backend_shutdown();
#elif defined(_SAUDIO_EMSCRIPTEN)
_saudio_webaudio_backend_shutdown();
#elif defined(SAUDIO_ANDROID_AAUDIO)
_saudio_aaudio_backend_shutdown();
#elif defined(SAUDIO_ANDROID_SLES)
_saudio_sles_backend_shutdown();
#elif defined(_SAUDIO_APPLE)
return _saudio_coreaudio_backend_shutdown();
#else
#error "unknown platform"
#endif
}
// ██████ ██ ██ ██████ ██ ██ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ██ ██████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██████ ██████ ███████ ██ ██████
//
// >>public
SOKOL_API_IMPL void saudio_setup(const saudio_desc* desc) {
SOKOL_ASSERT(!_saudio.valid);
SOKOL_ASSERT(!_saudio.setup_called);
SOKOL_ASSERT(desc);
SOKOL_ASSERT((desc->allocator.alloc_fn && desc->allocator.free_fn) || (!desc->allocator.alloc_fn && !desc->allocator.free_fn));
_saudio_clear(&_saudio, sizeof(_saudio));
_saudio.setup_called = true;
_saudio.desc = *desc;
_saudio.stream_cb = desc->stream_cb;
_saudio.stream_userdata_cb = desc->stream_userdata_cb;
_saudio.user_data = desc->user_data;
_saudio.sample_rate = _saudio_def(_saudio.desc.sample_rate, _SAUDIO_DEFAULT_SAMPLE_RATE);
_saudio.buffer_frames = _saudio_def(_saudio.desc.buffer_frames, _SAUDIO_DEFAULT_BUFFER_FRAMES);
_saudio.packet_frames = _saudio_def(_saudio.desc.packet_frames, _SAUDIO_DEFAULT_PACKET_FRAMES);
_saudio.num_packets = _saudio_def(_saudio.desc.num_packets, _SAUDIO_DEFAULT_NUM_PACKETS);
_saudio.num_channels = _saudio_def(_saudio.desc.num_channels, 1);
_saudio_fifo_init_mutex(&_saudio.fifo);
if (_saudio_backend_init()) {
/* the backend might not support the requested exact buffer size,
make sure the actual buffer size is still a multiple of
the requested packet size
*/
if (0 != (_saudio.buffer_frames % _saudio.packet_frames)) {
_SAUDIO_ERROR(BACKEND_BUFFER_SIZE_ISNT_MULTIPLE_OF_PACKET_SIZE);
_saudio_backend_shutdown();
return;
}
SOKOL_ASSERT(_saudio.bytes_per_frame > 0);
_saudio_fifo_init(&_saudio.fifo, _saudio.packet_frames * _saudio.bytes_per_frame, _saudio.num_packets);
_saudio.valid = true;
}
else {
_saudio_fifo_destroy_mutex(&_saudio.fifo);
}
}
SOKOL_API_IMPL void saudio_shutdown(void) {
SOKOL_ASSERT(_saudio.setup_called);
_saudio.setup_called = false;
if (_saudio.valid) {
_saudio_backend_shutdown();
_saudio_fifo_shutdown(&_saudio.fifo);
_saudio_fifo_destroy_mutex(&_saudio.fifo);
_saudio.valid = false;
}
}
SOKOL_API_IMPL bool saudio_isvalid(void) {
return _saudio.valid;
}
SOKOL_API_IMPL void* saudio_userdata(void) {
SOKOL_ASSERT(_saudio.setup_called);
return _saudio.desc.user_data;
}
SOKOL_API_IMPL saudio_desc saudio_query_desc(void) {
SOKOL_ASSERT(_saudio.setup_called);
return _saudio.desc;
}
SOKOL_API_IMPL int saudio_sample_rate(void) {
SOKOL_ASSERT(_saudio.setup_called);
return _saudio.sample_rate;
}
SOKOL_API_IMPL int saudio_buffer_frames(void) {
SOKOL_ASSERT(_saudio.setup_called);
return _saudio.buffer_frames;
}
SOKOL_API_IMPL int saudio_channels(void) {
SOKOL_ASSERT(_saudio.setup_called);
return _saudio.num_channels;
}
SOKOL_API_IMPL bool saudio_suspended(void) {
SOKOL_ASSERT(_saudio.setup_called);
#if defined(_SAUDIO_EMSCRIPTEN)
if (_saudio.valid) {
return 1 == saudio_js_suspended();
}
else {
return false;
}
#else
return false;
#endif
}
SOKOL_API_IMPL int saudio_expect(void) {
SOKOL_ASSERT(_saudio.setup_called);
if (_saudio.valid) {
const int num_frames = _saudio_fifo_writable_bytes(&_saudio.fifo) / _saudio.bytes_per_frame;
return num_frames;
}
else {
return 0;
}
}
SOKOL_API_IMPL int saudio_push(const float* frames, int num_frames) {
SOKOL_ASSERT(_saudio.setup_called);
SOKOL_ASSERT(frames && (num_frames > 0));
if (_saudio.valid) {
const int num_bytes = num_frames * _saudio.bytes_per_frame;
const int num_written = _saudio_fifo_write(&_saudio.fifo, (const uint8_t*)frames, num_bytes);
return num_written / _saudio.bytes_per_frame;
}
else {
return 0;
}
}
#undef _saudio_def
#undef _saudio_def_flt
#if defined(_SAUDIO_WINDOWS)
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif
#endif /* SOKOL_AUDIO_IMPL */
|
0 |
repos
|
repos/sokol/sokol_log.h
|
#if defined(SOKOL_IMPL) && !defined(SOKOL_LOG_IMPL)
#define SOKOL_LOG_IMPL
#endif
#ifndef SOKOL_LOG_INCLUDED
/*
sokol_log.h -- common logging callback for sokol headers
Project URL: https://github.com/floooh/sokol
Example code: https://github.com/floooh/sokol-samples
Do this:
#define SOKOL_IMPL or
#define SOKOL_LOG_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
Optionally provide the following defines when building the implementation:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false))
SOKOL_LOG_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_GFX_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
Optionally define the following for verbose output:
SOKOL_DEBUG - by default this is defined if _DEBUG is defined
OVERVIEW
========
sokol_log.h provides a default logging callback for other sokol headers.
To use the default log callback, just include sokol_log.h and provide
a function pointer to the 'slog_func' function when setting up the
sokol library:
For instance with sokol_audio.h:
#include "sokol_log.h"
...
saudio_setup(&(saudio_desc){ .logger.func = slog_func });
Logging output goes to stderr and/or a platform specific logging subsystem
(which means that in some scenarios you might see logging messages duplicated):
- Windows: stderr + OutputDebugStringA()
- macOS/iOS/Linux: stderr + syslog()
- Emscripten: console.info()/warn()/error()
- Android: __android_log_write()
On Windows with sokol_app.h also note the runtime config items to make
stdout/stderr output visible on the console for WinMain() applications
via sapp_desc.win32_console_attach or sapp_desc.win32_console_create,
however when running in a debugger on Windows, the logging output should
show up on the debug output UI panel.
In debug mode, a log message might look like this:
[sspine][error][id:12] /Users/floh/projects/sokol/util/sokol_spine.h:3472:0:
SKELETON_DESC_NO_ATLAS: no atlas object provided in sspine_skeleton_desc.atlas
The source path and line number is formatted like compiler errors, in some IDEs (like VSCode)
such error messages are clickable.
In release mode, logging is less verbose as to not bloat the executable with string data, but you still get
enough information to identify the type and location of an error:
[sspine][error][id:12][line:3472]
RULES FOR WRITING YOUR OWN LOGGING FUNCTION
===========================================
- must be re-entrant because it might be called from different threads
- must treat **all** provided string pointers as optional (can be null)
- don't store the string pointers, copy the string data instead
- must not return for log level panic
LICENSE
=======
zlib/libpng license
Copyright (c) 2023 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define SOKOL_LOG_INCLUDED (1)
#include <stdint.h>
#if defined(SOKOL_API_DECL) && !defined(SOKOL_LOG_API_DECL)
#define SOKOL_LOG_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_LOG_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_LOG_IMPL)
#define SOKOL_LOG_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_LOG_API_DECL __declspec(dllimport)
#else
#define SOKOL_LOG_API_DECL extern
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
Plug this function into the 'logger.func' struct item when initializing any of the sokol
headers. For instance for sokol_audio.h it would loom like this:
saudio_setup(&(saudio_desc){
.logger = {
.func = slog_func
}
});
*/
SOKOL_LOG_API_DECL void slog_func(const char* tag, uint32_t log_level, uint32_t log_item, const char* message, uint32_t line_nr, const char* filename, void* user_data);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // SOKOL_LOG_INCLUDED
// ██ ███ ███ ██████ ██ ███████ ███ ███ ███████ ███ ██ ████████ █████ ████████ ██ ██████ ███ ██
// ██ ████ ████ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
// ██ ██ ████ ██ ██████ ██ █████ ██ ████ ██ █████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ███████ ███████ ██ ██ ███████ ██ ████ ██ ██ ██ ██ ██ ██████ ██ ████
//
// >>implementation
#ifdef SOKOL_LOG_IMPL
#define SOKOL_LOG_IMPL_INCLUDED (1)
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
#ifndef SOKOL_DEBUG
#ifndef NDEBUG
#define SOKOL_DEBUG
#endif
#endif
#ifndef SOKOL_ASSERT
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
#ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static
#else
#define _SOKOL_PRIVATE static
#endif
#endif
#ifndef _SOKOL_UNUSED
#define _SOKOL_UNUSED(x) (void)(x)
#endif
// platform detection
#if defined(__APPLE__)
#define _SLOG_APPLE (1)
#elif defined(__EMSCRIPTEN__)
#define _SLOG_EMSCRIPTEN (1)
#elif defined(_WIN32)
#define _SLOG_WINDOWS (1)
#elif defined(__ANDROID__)
#define _SLOG_ANDROID (1)
#elif defined(__linux__) || defined(__unix__)
#define _SLOG_LINUX (1)
#else
#error "sokol_log.h: unknown platform"
#endif
#include <stdlib.h> // abort
#include <stdio.h> // fputs
#include <stddef.h> // size_t
#if defined(_SLOG_EMSCRIPTEN)
#include <emscripten/emscripten.h>
#elif defined(_SLOG_WINDOWS)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#elif defined(_SLOG_ANDROID)
#include <android/log.h>
#elif defined(_SLOG_LINUX) || defined(_SLOG_APPLE)
#include <syslog.h>
#endif
// size of line buffer (on stack!) in bytes including terminating zero
#define _SLOG_LINE_LENGTH (512)
_SOKOL_PRIVATE char* _slog_append(const char* str, char* dst, char* end) {
if (str) {
char c;
while (((c = *str++) != 0) && (dst < (end - 1))) {
*dst++ = c;
}
}
*dst = 0;
return dst;
}
_SOKOL_PRIVATE char* _slog_itoa(uint32_t x, char* buf, size_t buf_size) {
const size_t max_digits_and_null = 11;
if (buf_size < max_digits_and_null) {
return 0;
}
char* p = buf + max_digits_and_null;
*--p = 0;
do {
*--p = '0' + (x % 10);
x /= 10;
} while (x != 0);
return p;
}
#if defined(_SLOG_EMSCRIPTEN)
EM_JS(void, slog_js_log, (uint32_t level, const char* c_str), {
const str = UTF8ToString(c_str);
switch (level) {
case 0: console.error(str); break;
case 1: console.error(str); break;
case 2: console.warn(str); break;
default: console.info(str); break;
}
});
#endif
SOKOL_API_IMPL void slog_func(const char* tag, uint32_t log_level, uint32_t log_item, const char* message, uint32_t line_nr, const char* filename, void* user_data) {
_SOKOL_UNUSED(user_data);
const char* log_level_str;
switch (log_level) {
case 0: log_level_str = "panic"; break;
case 1: log_level_str = "error"; break;
case 2: log_level_str = "warning"; break;
default: log_level_str = "info"; break;
}
// build log output line
char line_buf[_SLOG_LINE_LENGTH];
char* str = line_buf;
char* end = line_buf + sizeof(line_buf);
char num_buf[32];
if (tag) {
str = _slog_append("[", str, end);
str = _slog_append(tag, str, end);
str = _slog_append("]", str, end);
}
str = _slog_append("[", str, end);
str = _slog_append(log_level_str, str, end);
str = _slog_append("]", str, end);
str = _slog_append("[id:", str, end);
str = _slog_append(_slog_itoa(log_item, num_buf, sizeof(num_buf)), str, end);
str = _slog_append("]", str, end);
// if a filename is provided, build a clickable log message that's compatible with compiler error messages
if (filename) {
str = _slog_append(" ", str, end);
#if defined(_MSC_VER)
// MSVC compiler error format
str = _slog_append(filename, str, end);
str = _slog_append("(", str, end);
str = _slog_append(_slog_itoa(line_nr, num_buf, sizeof(num_buf)), str, end);
str = _slog_append("): ", str, end);
#else
// gcc/clang compiler error format
str = _slog_append(filename, str, end);
str = _slog_append(":", str, end);
str = _slog_append(_slog_itoa(line_nr, num_buf, sizeof(num_buf)), str, end);
str = _slog_append(":0: ", str, end);
#endif
}
else {
str = _slog_append("[line:", str, end);
str = _slog_append(_slog_itoa(line_nr, num_buf, sizeof(num_buf)), str, end);
str = _slog_append("] ", str, end);
}
if (message) {
str = _slog_append("\n\t", str, end);
str = _slog_append(message, str, end);
}
str = _slog_append("\n\n", str, end);
if (0 == log_level) {
str = _slog_append("ABORTING because of [panic]\n", str, end);
(void)str;
}
// print to stderr?
#if defined(_SLOG_LINUX) || defined(_SLOG_WINDOWS) || defined(_SLOG_APPLE)
fputs(line_buf, stderr);
#endif
// platform specific logging calls
#if defined(_SLOG_WINDOWS)
OutputDebugStringA(line_buf);
#elif defined(_SLOG_ANDROID)
int prio;
switch (log_level) {
case 0: prio = ANDROID_LOG_FATAL; break;
case 1: prio = ANDROID_LOG_ERROR; break;
case 2: prio = ANDROID_LOG_WARN; break;
default: prio = ANDROID_LOG_INFO; break;
}
__android_log_write(prio, "SOKOL", line_buf);
#elif defined(_SLOG_EMSCRIPTEN)
slog_js_log(log_level, line_buf);
#elif defined(_SLOG_LINUX) || defined(_SLOG_APPLE)
int prio;
switch (log_level) {
case 0: prio = LOG_CRIT; break;
case 1: prio = LOG_ERR; break;
case 2: prio = LOG_WARNING; break;
default: prio = LOG_INFO; break;
}
syslog(prio, "%s", line_buf);
#endif
if (0 == log_level) {
abort();
}
}
#endif // SOKOL_LOG_IMPL
|
0 |
repos
|
repos/sokol/sokol_fetch.h
|
#if defined(SOKOL_IMPL) && !defined(SOKOL_FETCH_IMPL)
#define SOKOL_FETCH_IMPL
#endif
#ifndef SOKOL_FETCH_INCLUDED
/*
sokol_fetch.h -- asynchronous data loading/streaming
Project URL: https://github.com/floooh/sokol
Do this:
#define SOKOL_IMPL or
#define SOKOL_FETCH_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
Optionally provide the following defines with your own implementations:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false))
SOKOL_FETCH_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_FETCH_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
SFETCH_MAX_PATH - max length of UTF-8 filesystem path / URL (default: 1024 bytes)
SFETCH_MAX_USERDATA_UINT64 - max size of embedded userdata in number of uint64_t, userdata
will be copied into an 8-byte aligned memory region associated
with each in-flight request, default value is 16 (== 128 bytes)
SFETCH_MAX_CHANNELS - max number of IO channels (default is 16, also see sfetch_desc_t.num_channels)
If sokol_fetch.h is compiled as a DLL, define the following before
including the declaration or implementation:
SOKOL_DLL
On Windows, SOKOL_DLL will define SOKOL_FETCH_API_DECL as __declspec(dllexport)
or __declspec(dllimport) as needed.
NOTE: The following documentation talks a lot about "IO threads". Actual
threads are only used on platforms where threads are available. The web
version (emscripten/wasm) doesn't use POSIX-style threads, but instead
asynchronous Javascript calls chained together by callbacks. The actual
source code differences between the two approaches have been kept to
a minimum though.
FEATURE OVERVIEW
================
- Asynchronously load complete files, or stream files incrementally via
HTTP (on web platform), or the local file system (on native platforms)
- Request / response-callback model, user code sends a request
to initiate a file-load, sokol_fetch.h calls the response callback
on the same thread when data is ready or user-code needs
to respond otherwise
- Not limited to the main-thread or a single thread: A sokol-fetch
"context" can live on any thread, and multiple contexts
can operate side-by-side on different threads.
- Memory management for data buffers is under full control of user code.
sokol_fetch.h won't allocate memory after it has been setup.
- Automatic rate-limiting guarantees that only a maximum number of
requests is processed at any one time, allowing a zero-allocation
model, where all data is streamed into fixed-size, pre-allocated
buffers.
- Active Requests can be paused, continued and cancelled from anywhere
in the user-thread which sent this request.
TL;DR EXAMPLE CODE
==================
This is the most-simple example code to load a single data file with a
known maximum size:
(1) initialize sokol-fetch with default parameters (but NOTE that the
default setup parameters provide a safe-but-slow "serialized"
operation). In order to see any logging output in case or errors
you should always provide a logging function
(such as 'slog_func' from sokol_log.h):
sfetch_setup(&(sfetch_desc_t){ .logger.func = slog_func });
(2) send a fetch-request to load a file from the current directory
into a buffer big enough to hold the entire file content:
static uint8_t buf[MAX_FILE_SIZE];
sfetch_send(&(sfetch_request_t){
.path = "my_file.txt",
.callback = response_callback,
.buffer = {
.ptr = buf,
.size = sizeof(buf)
}
});
If 'buf' is a value (e.g. an array or struct item), the .buffer item can
be initialized with the SFETCH_RANGE() helper macro:
sfetch_send(&(sfetch_request_t){
.path = "my_file.txt",
.callback = response_callback,
.buffer = SFETCH_RANGE(buf)
});
(3) write a 'response-callback' function, this will be called whenever
the user-code must respond to state changes of the request
(most importantly when data has been loaded):
void response_callback(const sfetch_response_t* response) {
if (response->fetched) {
// data has been loaded, and is available via the
// sfetch_range_t struct item 'data':
const void* ptr = response->data.ptr;
size_t num_bytes = response->data.size;
}
if (response->finished) {
// the 'finished'-flag is the catch-all flag for when the request
// is finished, no matter if loading was successful or failed,
// so any cleanup-work should happen here...
...
if (response->failed) {
// 'failed' is true in (addition to 'finished') if something
// went wrong (file doesn't exist, or less bytes could be
// read from the file than expected)
}
}
}
(4) pump the sokol-fetch message queues, and invoke response callbacks
by calling:
sfetch_dowork();
In an event-driven app this should be called in the event loop. If you
use sokol-app this would be in your frame_cb function.
(5) finally, call sfetch_shutdown() at the end of the application:
There's many other loading-scenarios, for instance one doesn't have to
provide a buffer upfront, this can also happen in the response callback.
Or it's possible to stream huge files into small fixed-size buffer,
complete with pausing and continuing the download.
It's also possible to improve the 'pipeline throughput' by fetching
multiple files in parallel, but at the same time limit the maximum
number of requests that can be 'in-flight'.
For how this all works, please read the following documentation sections :)
API DOCUMENTATION
=================
void sfetch_setup(const sfetch_desc_t* desc)
--------------------------------------------
First call sfetch_setup(const sfetch_desc_t*) on any thread before calling
any other sokol-fetch functions on the same thread.
sfetch_setup() takes a pointer to an sfetch_desc_t struct with setup
parameters. Parameters which should use their default values must
be zero-initialized:
- max_requests (uint32_t):
The maximum number of requests that can be alive at any time, the
default is 128.
- num_channels (uint32_t):
The number of "IO channels" used to parallelize and prioritize
requests, the default is 1.
- num_lanes (uint32_t):
The number of "lanes" on a single channel. Each request which is
currently 'inflight' on a channel occupies one lane until the
request is finished. This is used for automatic rate-limiting
(search below for CHANNELS AND LANES for more details). The
default number of lanes is 1.
For example, to setup sokol-fetch for max 1024 active requests, 4 channels,
and 8 lanes per channel in C99:
sfetch_setup(&(sfetch_desc_t){
.max_requests = 1024,
.num_channels = 4,
.num_lanes = 8
});
sfetch_setup() is the only place where sokol-fetch will allocate memory.
NOTE that the default setup parameters of 1 channel and 1 lane per channel
has a very poor 'pipeline throughput' since this essentially serializes
IO requests (a new request will only be processed when the last one has
finished), and since each request needs at least one roundtrip between
the user- and IO-thread the throughput will be at most one request per
frame. Search for LATENCY AND THROUGHPUT below for more information on
how to increase throughput.
NOTE that you can call sfetch_setup() on multiple threads, each thread
will get its own thread-local sokol-fetch instance, which will work
independently from sokol-fetch instances on other threads.
void sfetch_shutdown(void)
--------------------------
Call sfetch_shutdown() at the end of the application to stop any
IO threads and free all memory that was allocated in sfetch_setup().
sfetch_handle_t sfetch_send(const sfetch_request_t* request)
------------------------------------------------------------
Call sfetch_send() to start loading data, the function takes a pointer to an
sfetch_request_t struct with request parameters and returns a
sfetch_handle_t identifying the request for later calls. At least
a path/URL and callback must be provided:
sfetch_handle_t h = sfetch_send(&(sfetch_request_t){
.path = "my_file.txt",
.callback = my_response_callback
});
sfetch_send() will return an invalid handle if no request can be allocated
from the internal pool because all available request items are 'in-flight'.
The sfetch_request_t struct contains the following parameters (optional
parameters that are not provided must be zero-initialized):
- path (const char*, required)
Pointer to an UTF-8 encoded C string describing the filesystem
path or HTTP URL. The string will be copied into an internal data
structure, and passed "as is" (apart from any required
encoding-conversions) to fopen(), CreateFileW() or
XMLHttpRequest. The maximum length of the string is defined by
the SFETCH_MAX_PATH configuration define, the default is 1024 bytes
including the 0-terminator byte.
- callback (sfetch_callback_t, required)
Pointer to a response-callback function which is called when the
request needs "user code attention". Search below for REQUEST
STATES AND THE RESPONSE CALLBACK for detailed information about
handling responses in the response callback.
- channel (uint32_t, optional)
Index of the IO channel where the request should be processed.
Channels are used to parallelize and prioritize requests relative
to each other. Search below for CHANNELS AND LANES for more
information. The default channel is 0.
- chunk_size (uint32_t, optional)
The chunk_size member is used for streaming data incrementally
in small chunks. After 'chunk_size' bytes have been loaded into
to the streaming buffer, the response callback will be called
with the buffer containing the fetched data for the current chunk.
If chunk_size is 0 (the default), than the whole file will be loaded.
Please search below for CHUNK SIZE AND HTTP COMPRESSION for
important information how streaming works if the web server
is serving compressed data.
- buffer (sfetch_range_t)
This is a optional pointer/size pair describing a chunk of memory where
data will be loaded into (if no buffer is provided upfront, this
must happen in the response callback). If a buffer is provided,
it must be big enough to either hold the entire file (if chunk_size
is zero), or the *uncompressed* data for one downloaded chunk
(if chunk_size is > 0).
- user_data (sfetch_range_t)
The user_data ptr/size range struct describe an optional POD blob
(plain-old-data) associated with the request which will be copied(!)
into an internal memory block. The maximum default size of this
memory block is 128 bytes (but can be overridden by defining
SFETCH_MAX_USERDATA_UINT64 before including the notification, note
that this define is in "number of uint64_t", not number of bytes).
The user-data block is 8-byte aligned, and will be copied via
memcpy() (so don't put any C++ "smart members" in there).
NOTE that request handles are strictly thread-local and only unique
within the thread the handle was created on, and all function calls
involving a request handle must happen on that same thread.
bool sfetch_handle_valid(sfetch_handle_t request)
-------------------------------------------------
This checks if the provided request handle is valid, and is associated with
a currently active request. It will return false if:
- sfetch_send() returned an invalid handle because it couldn't allocate
a new request from the internal request pool (because they're all
in flight)
- the request associated with the handle is no longer alive (because
it either finished successfully, or the request failed for some
reason)
void sfetch_dowork(void)
------------------------
Call sfetch_dowork(void) in regular intervals (for instance once per frame)
on the same thread as sfetch_setup() to "turn the gears". If you are sending
requests but never hear back from them in the response callback function, then
the most likely reason is that you forgot to add the call to sfetch_dowork()
in the per-frame function.
sfetch_dowork() roughly performs the following work:
- any new requests that have been sent with sfetch_send() since the
last call to sfetch_dowork() will be dispatched to their IO channels
and assigned a free lane. If all lanes on that channel are occupied
by requests 'in flight', incoming requests must wait until
a lane becomes available
- for all new requests which have been enqueued on a channel which
don't already have a buffer assigned the response callback will be
called with (response->dispatched == true) so that the response
callback can inspect the dynamically assigned lane and bind a buffer
to the request (search below for CHANNELS AND LANE for more info)
- a state transition from "user side" to "IO thread side" happens for
each new request that has been dispatched to a channel.
- requests dispatched to a channel are either forwarded into that
channel's worker thread (on native platforms), or cause an HTTP
request to be sent via an asynchronous XMLHttpRequest (on the web
platform)
- for all requests which have finished their current IO operation a
state transition from "IO thread side" to "user side" happens,
and the response callback is called so that the fetched data
can be processed.
- requests which are completely finished (either because the entire
file content has been loaded, or they are in the FAILED state) are
freed (this just changes their state in the 'request pool', no actual
memory is freed)
- requests which are not yet finished are fed back into the
'incoming' queue of their channel, and the cycle starts again, this
only happens for requests which perform data streaming (not load
the entire file at once).
void sfetch_cancel(sfetch_handle_t request)
-------------------------------------------
This cancels a request in the next sfetch_dowork() call and invokes the
response callback with (response.failed == true) and (response.finished
== true) to give user-code a chance to do any cleanup work for the
request. If sfetch_cancel() is called for a request that is no longer
alive, nothing bad will happen (the call will simply do nothing).
void sfetch_pause(sfetch_handle_t request)
------------------------------------------
This pauses an active request in the next sfetch_dowork() call and puts
it into the PAUSED state. For all requests in PAUSED state, the response
callback will be called in each call to sfetch_dowork() to give user-code
a chance to CONTINUE the request (by calling sfetch_continue()). Pausing
a request makes sense for dynamic rate-limiting in streaming scenarios
(like video/audio streaming with a fixed number of streaming buffers. As
soon as all available buffers are filled with download data, downloading
more data must be prevented to allow video/audio playback to catch up and
free up empty buffers for new download data.
void sfetch_continue(sfetch_handle_t request)
---------------------------------------------
Continues a paused request, counterpart to the sfetch_pause() function.
void sfetch_bind_buffer(sfetch_handle_t request, sfetch_range_t buffer)
----------------------------------------------------------------------------------------
This "binds" a new buffer (as pointer/size pair) to an active request. The
function *must* be called from inside the response-callback, and there
must not already be another buffer bound.
void* sfetch_unbind_buffer(sfetch_handle_t request)
---------------------------------------------------
This removes the current buffer binding from the request and returns
a pointer to the previous buffer (useful if the buffer was dynamically
allocated and it must be freed).
sfetch_unbind_buffer() *must* be called from inside the response callback.
The usual code sequence to bind a different buffer in the response
callback might look like this:
void response_callback(const sfetch_response_t* response) {
if (response.fetched) {
...
// switch to a different buffer (in the FETCHED state it is
// guaranteed that the request has a buffer, otherwise it
// would have gone into the FAILED state
void* old_buf_ptr = sfetch_unbind_buffer(response.handle);
free(old_buf_ptr);
void* new_buf_ptr = malloc(new_buf_size);
sfetch_bind_buffer(response.handle, new_buf_ptr, new_buf_size);
}
if (response.finished) {
// unbind and free the currently associated buffer,
// the buffer pointer could be null if the request has failed
// NOTE that it is legal to call free() with a nullptr,
// this happens if the request failed to open its file
// and never goes into the OPENED state
void* buf_ptr = sfetch_unbind_buffer(response.handle);
free(buf_ptr);
}
}
sfetch_desc_t sfetch_desc(void)
-------------------------------
sfetch_desc() returns a copy of the sfetch_desc_t struct passed to
sfetch_setup(), with zero-initialized values replaced with
their default values.
int sfetch_max_userdata_bytes(void)
-----------------------------------
This returns the value of the SFETCH_MAX_USERDATA_UINT64 config
define, but in number of bytes (so SFETCH_MAX_USERDATA_UINT64*8).
int sfetch_max_path(void)
-------------------------
Returns the value of the SFETCH_MAX_PATH config define.
REQUEST STATES AND THE RESPONSE CALLBACK
========================================
A request goes through a number of states during its lifetime. Depending
on the current state of a request, it will be 'owned' either by the
"user-thread" (where the request was sent) or an IO thread.
You can think of a request as "ping-ponging" between the IO thread and
user thread, any actual IO work is done on the IO thread, while
invocations of the response-callback happen on the user-thread.
All state transitions and callback invocations happen inside the
sfetch_dowork() function.
An active request goes through the following states:
ALLOCATED (user-thread)
The request has been allocated in sfetch_send() and is
waiting to be dispatched into its IO channel. When this
happens, the request will transition into the DISPATCHED state.
DISPATCHED (IO thread)
The request has been dispatched into its IO channel, and a
lane has been assigned to the request.
If a buffer was provided in sfetch_send() the request will
immediately transition into the FETCHING state and start loading
data into the buffer.
If no buffer was provided in sfetch_send(), the response
callback will be called with (response->dispatched == true),
so that the response callback can bind a buffer to the
request. Binding the buffer in the response callback makes
sense if the buffer isn't dynamically allocated, but instead
a pre-allocated buffer must be selected from the request's
channel and lane.
Note that it isn't possible to get a file size in the response callback
which would help with allocating a buffer of the right size, this is
because it isn't possible in HTTP to query the file size before the
entire file is downloaded (...when the web server serves files compressed).
If opening the file failed, the request will transition into
the FAILED state with the error code SFETCH_ERROR_FILE_NOT_FOUND.
FETCHING (IO thread)
While a request is in the FETCHING state, data will be loaded into
the user-provided buffer.
If no buffer was provided, the request will go into the FAILED
state with the error code SFETCH_ERROR_NO_BUFFER.
If a buffer was provided, but it is too small to contain the
fetched data, the request will go into the FAILED state with
error code SFETCH_ERROR_BUFFER_TOO_SMALL.
If less data can be read from the file than expected, the request
will go into the FAILED state with error code SFETCH_ERROR_UNEXPECTED_EOF.
If loading data into the provided buffer works as expected, the
request will go into the FETCHED state.
FETCHED (user thread)
The request goes into the FETCHED state either when the entire file
has been loaded into the provided buffer (when request.chunk_size == 0),
or a chunk has been loaded (and optionally decompressed) into the
buffer (when request.chunk_size > 0).
The response callback will be called so that the user-code can
process the loaded data using the following sfetch_response_t struct members:
- data.ptr: pointer to the start of fetched data
- data.size: the number of bytes in the provided buffer
- data_offset: the byte offset of the loaded data chunk in the
overall file (this is only set to a non-zero value in a streaming
scenario)
Once all file data has been loaded, the 'finished' flag will be set
in the response callback's sfetch_response_t argument.
After the user callback returns, and all file data has been loaded
(response.finished flag is set) the request has reached its end-of-life
and will recycled.
Otherwise, if there's still data to load (because streaming was
requested by providing a non-zero request.chunk_size), the request
will switch back to the FETCHING state to load the next chunk of data.
Note that it is ok to associate a different buffer or buffer-size
with the request by calling sfetch_bind_buffer() in the response-callback.
To check in the response callback for the FETCHED state, and
independently whether the request is finished:
void response_callback(const sfetch_response_t* response) {
if (response->fetched) {
// request is in FETCHED state, the loaded data is available
// in .data.ptr, and the number of bytes that have been
// loaded in .data.size:
const void* data = response->data.ptr;
size_t num_bytes = response->data.size;
}
if (response->finished) {
// the finished flag is set either when all data
// has been loaded, the request has been cancelled,
// or the file operation has failed, this is where
// any required per-request cleanup work should happen
}
}
FAILED (user thread)
A request will transition into the FAILED state in the following situations:
- if the file doesn't exist or couldn't be opened for other
reasons (SFETCH_ERROR_FILE_NOT_FOUND)
- if no buffer is associated with the request in the FETCHING state
(SFETCH_ERROR_NO_BUFFER)
- if the provided buffer is too small to hold the entire file
(if request.chunk_size == 0), or the (potentially decompressed)
partial data chunk (SFETCH_ERROR_BUFFER_TOO_SMALL)
- if less bytes could be read from the file then expected
(SFETCH_ERROR_UNEXPECTED_EOF)
- if a request has been cancelled via sfetch_cancel()
(SFETCH_ERROR_CANCELLED)
The response callback will be called once after a request goes into
the FAILED state, with the 'response->finished' and
'response->failed' flags set to true.
This gives the user-code a chance to cleanup any resources associated
with the request.
To check for the failed state in the response callback:
void response_callback(const sfetch_response_t* response) {
if (response->failed) {
// specifically check for the failed state...
}
// or you can do a catch-all check via the finished-flag:
if (response->finished) {
if (response->failed) {
// if more detailed error handling is needed:
switch (response->error_code) {
...
}
}
}
}
PAUSED (user thread)
A request will transition into the PAUSED state after user-code
calls the function sfetch_pause() on the request's handle. Usually
this happens from within the response-callback in streaming scenarios
when the data streaming needs to wait for a data decoder (like
a video/audio player) to catch up.
While a request is in PAUSED state, the response-callback will be
called in each sfetch_dowork(), so that the user-code can either
continue the request by calling sfetch_continue(), or cancel
the request by calling sfetch_cancel().
When calling sfetch_continue() on a paused request, the request will
transition into the FETCHING state. Otherwise if sfetch_cancel() is
called, the request will switch into the FAILED state.
To check for the PAUSED state in the response callback:
void response_callback(const sfetch_response_t* response) {
if (response->paused) {
// we can check here whether the request should
// continue to load data:
if (should_continue(response->handle)) {
sfetch_continue(response->handle);
}
}
}
CHUNK SIZE AND HTTP COMPRESSION
===============================
TL;DR: for streaming scenarios, the provided chunk-size must be smaller
than the provided buffer-size because the web server may decide to
serve the data compressed and the chunk-size must be given in 'compressed
bytes' while the buffer receives 'uncompressed bytes'. It's not possible
in HTTP to query the uncompressed size for a compressed download until
that download has finished.
With vanilla HTTP, it is not possible to query the actual size of a file
without downloading the entire file first (the Content-Length response
header only provides the compressed size). Furthermore, for HTTP
range-requests, the range is given on the compressed data, not the
uncompressed data. So if the web server decides to server the data
compressed, the content-length and range-request parameters don't
correspond to the uncompressed data that's arriving in the sokol-fetch
buffers, and there's no way from JS or WASM to either force uncompressed
downloads (e.g. by setting the Accept-Encoding field), or access the
compressed data.
This has some implications for sokol_fetch.h, most notably that buffers
can't be provided in the exactly right size, because that size can't
be queried from HTTP before the data is actually downloaded.
When downloading whole files at once, it is basically expected that you
know the maximum files size upfront through other means (for instance
through a separate meta-data-file which contains the file sizes and
other meta-data for each file that needs to be loaded).
For streaming downloads the situation is a bit more complicated. These
use HTTP range-requests, and those ranges are defined on the (potentially)
compressed data which the JS/WASM side doesn't have access to. However,
the JS/WASM side only ever sees the uncompressed data, and it's not possible
to query the uncompressed size of a range request before that range request
has finished.
If the provided buffer is too small to contain the uncompressed data,
the request will fail with error code SFETCH_ERROR_BUFFER_TOO_SMALL.
CHANNELS AND LANES
==================
Channels and lanes are (somewhat artificial) concepts to manage
parallelization, prioritization and rate-limiting.
Channels can be used to parallelize message processing for better 'pipeline
throughput', and to prioritize requests: user-code could reserve one
channel for streaming downloads which need to run in parallel to other
requests, another channel for "regular" downloads and yet another
high-priority channel which would only be used for small files which need
to start loading immediately.
Each channel comes with its own IO thread and message queues for pumping
messages in and out of the thread. The channel where a request is
processed is selected manually when sending a message:
sfetch_send(&(sfetch_request_t){
.path = "my_file.txt",
.callback = my_response_callback,
.channel = 2
});
The number of channels is configured at startup in sfetch_setup() and
cannot be changed afterwards.
Channels are completely separate from each other, and a request will
never "hop" from one channel to another.
Each channel consists of a fixed number of "lanes" for automatic rate
limiting:
When a request is sent to a channel via sfetch_send(), a "free lane" will
be picked and assigned to the request. The request will occupy this lane
for its entire life time (also while it is paused). If all lanes of a
channel are currently occupied, new requests will need to wait until a
lane becomes unoccupied.
Since the number of channels and lanes is known upfront, it is guaranteed
that there will never be more than "num_channels * num_lanes" requests
in flight at any one time.
This guarantee eliminates unexpected load- and memory-spikes when
many requests are sent in very short time, and it allows to pre-allocate
a fixed number of memory buffers which can be reused for the entire
"lifetime" of a sokol-fetch context.
In the most simple scenario - when a maximum file size is known - buffers
can be statically allocated like this:
uint8_t buffer[NUM_CHANNELS][NUM_LANES][MAX_FILE_SIZE];
Then in the user callback pick a buffer by channel and lane,
and associate it with the request like this:
void response_callback(const sfetch_response_t* response) {
if (response->dispatched) {
void* ptr = buffer[response->channel][response->lane];
sfetch_bind_buffer(response->handle, ptr, MAX_FILE_SIZE);
}
...
}
NOTES ON OPTIMIZING PIPELINE LATENCY AND THROUGHPUT
===================================================
With the default configuration of 1 channel and 1 lane per channel,
sokol_fetch.h will appear to have a shockingly bad loading performance
if several files are loaded.
This has two reasons:
(1) all parallelization when loading data has been disabled. A new
request will only be processed, when the last request has finished.
(2) every invocation of the response-callback adds one frame of latency
to the request, because callbacks will only be called from within
sfetch_dowork()
sokol-fetch takes a few shortcuts to improve step (2) and reduce
the 'inherent latency' of a request:
- if a buffer is provided upfront, the response-callback won't be
called in the DISPATCHED state, but start right with the FETCHED state
where data has already been loaded into the buffer
- there is no separate CLOSED state where the callback is invoked
separately when loading has finished (or the request has failed),
instead the finished and failed flags will be set as part of
the last FETCHED invocation
This means providing a big-enough buffer to fit the entire file is the
best case, the response callback will only be called once, ideally in
the next frame (or two calls to sfetch_dowork()).
If no buffer is provided upfront, one frame of latency is added because
the response callback needs to be invoked in the DISPATCHED state so that
the user code can bind a buffer.
This means the best case for a request without an upfront-provided
buffer is 2 frames (or 3 calls to sfetch_dowork()).
That's about what can be done to improve the latency for a single request,
but the really important step is to improve overall throughput. If you
need to load thousands of files you don't want that to be completely
serialized.
The most important action to increase throughput is to increase the
number of lanes per channel. This defines how many requests can be
'in flight' on a single channel at the same time. The guiding decision
factor for how many lanes you can "afford" is the memory size you want
to set aside for buffers. Each lane needs its own buffer so that
the data loaded for one request doesn't scribble over the data
loaded for another request.
Here's a simple example of sending 4 requests without upfront buffer
on a channel with 1, 2 and 4 lanes, each line is one frame:
1 LANE (8 frames):
Lane 0:
-------------
REQ 0 DISPATCHED
REQ 0 FETCHED
REQ 1 DISPATCHED
REQ 1 FETCHED
REQ 2 DISPATCHED
REQ 2 FETCHED
REQ 3 DISPATCHED
REQ 3 FETCHED
Note how the request don't overlap, so they can all use the same buffer.
2 LANES (4 frames):
Lane 0: Lane 1:
------------------------------------
REQ 0 DISPATCHED REQ 1 DISPATCHED
REQ 0 FETCHED REQ 1 FETCHED
REQ 2 DISPATCHED REQ 3 DISPATCHED
REQ 2 FETCHED REQ 3 FETCHED
This reduces the overall time to 4 frames, but now you need 2 buffers so
that requests don't scribble over each other.
4 LANES (2 frames):
Lane 0: Lane 1: Lane 2: Lane 3:
----------------------------------------------------------------------------
REQ 0 DISPATCHED REQ 1 DISPATCHED REQ 2 DISPATCHED REQ 3 DISPATCHED
REQ 0 FETCHED REQ 1 FETCHED REQ 2 FETCHED REQ 3 FETCHED
Now we're down to the same 'best-case' latency as sending a single
request.
Apart from the memory requirements for the streaming buffers (which is
under your control), you can be generous with the number of lanes,
they don't add any processing overhead.
The last option for tweaking latency and throughput is channels. Each
channel works independently from other channels, so while one
channel is busy working through a large number of requests (or one
very long streaming download), you can set aside a high-priority channel
for requests that need to start as soon as possible.
On platforms with threading support, each channel runs on its own
thread, but this is mainly an implementation detail to work around
the blocking traditional file IO functions, not for performance reasons.
MEMORY ALLOCATION OVERRIDE
==========================
You can override the memory allocation functions at initialization time
like this:
void* my_alloc(size_t size, void* user_data) {
return malloc(size);
}
void my_free(void* ptr, void* user_data) {
free(ptr);
}
...
sfetch_setup(&(sfetch_desc_t){
// ...
.allocator = {
.alloc_fn = my_alloc,
.free_fn = my_free,
.user_data = ...,
}
});
...
If no overrides are provided, malloc and free will be used.
This only affects memory allocation calls done by sokol_fetch.h
itself though, not any allocations in OS libraries.
Memory allocation will only happen on the same thread where sfetch_setup()
was called, so you don't need to worry about thread-safety.
ERROR REPORTING AND LOGGING
===========================
To get any logging information at all you need to provide a logging callback in the setup call,
the easiest way is to use sokol_log.h:
#include "sokol_log.h"
sfetch_setup(&(sfetch_desc_t){
// ...
.logger.func = slog_func
});
To override logging with your own callback, first write a logging function like this:
void my_log(const char* tag, // e.g. 'sfetch'
uint32_t log_level, // 0=panic, 1=error, 2=warn, 3=info
uint32_t log_item_id, // SFETCH_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_fetch.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data)
{
...
}
...and then setup sokol-fetch like this:
sfetch_setup(&(sfetch_desc_t){
.logger = {
.func = my_log,
.user_data = my_user_data,
}
});
The provided logging function must be reentrant (e.g. be callable from
different threads).
If you don't want to provide your own custom logger it is highly recommended to use
the standard logger in sokol_log.h instead, otherwise you won't see any warnings or
errors.
FUTURE PLANS / V2.0 IDEA DUMP
=============================
- An optional polling API (as alternative to callback API)
- Move buffer-management into the API? The "manual management"
can be quite tricky especially for dynamic allocation scenarios,
API support for buffer management would simplify cases like
preventing that requests scribble over each other's buffers, or
an automatic garbage collection for dynamically allocated buffers,
or automatically falling back to dynamic allocation if static
buffers aren't big enough.
- Pluggable request handlers to load data from other "sources"
(especially HTTP downloads on native platforms via e.g. libcurl
would be useful)
- I'm currently not happy how the user-data block is handled, this
should getting and updating the user-data should be wrapped by
API functions (similar to bind/unbind buffer)
LICENSE
=======
zlib/libpng license
Copyright (c) 2019 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define SOKOL_FETCH_INCLUDED (1)
#include <stddef.h> // size_t
#include <stdint.h>
#include <stdbool.h>
#if defined(SOKOL_API_DECL) && !defined(SOKOL_FETCH_API_DECL)
#define SOKOL_FETCH_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_FETCH_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_FETCH_IMPL)
#define SOKOL_FETCH_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_FETCH_API_DECL __declspec(dllimport)
#else
#define SOKOL_FETCH_API_DECL extern
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
sfetch_log_item_t
Log items are defined via X-Macros, and expanded to an
enum 'sfetch_log_item', and in debug mode only,
corresponding strings.
Used as parameter in the logging callback.
*/
#define _SFETCH_LOG_ITEMS \
_SFETCH_LOGITEM_XMACRO(OK, "Ok") \
_SFETCH_LOGITEM_XMACRO(MALLOC_FAILED, "memory allocation failed") \
_SFETCH_LOGITEM_XMACRO(FILE_PATH_UTF8_DECODING_FAILED, "failed converting file path from UTF8 to wide") \
_SFETCH_LOGITEM_XMACRO(SEND_QUEUE_FULL, "send queue full (adjust via sfetch_desc_t.max_requests)") \
_SFETCH_LOGITEM_XMACRO(REQUEST_CHANNEL_INDEX_TOO_BIG, "channel index too big (adjust via sfetch_desc_t.num_channels)") \
_SFETCH_LOGITEM_XMACRO(REQUEST_PATH_IS_NULL, "file path is nullptr (sfetch_request_t.path)") \
_SFETCH_LOGITEM_XMACRO(REQUEST_PATH_TOO_LONG, "file path is too long (SFETCH_MAX_PATH)") \
_SFETCH_LOGITEM_XMACRO(REQUEST_CALLBACK_MISSING, "no callback provided (sfetch_request_t.callback)") \
_SFETCH_LOGITEM_XMACRO(REQUEST_CHUNK_SIZE_GREATER_BUFFER_SIZE, "chunk size is greater buffer size (sfetch_request_t.chunk_size vs .buffer.size)") \
_SFETCH_LOGITEM_XMACRO(REQUEST_USERDATA_PTR_IS_SET_BUT_USERDATA_SIZE_IS_NULL, "user data ptr is set but user data size is null (sfetch_request_t.user_data.ptr vs .size)") \
_SFETCH_LOGITEM_XMACRO(REQUEST_USERDATA_PTR_IS_NULL_BUT_USERDATA_SIZE_IS_NOT, "user data ptr is null but size is not (sfetch_request_t.user_data.ptr vs .size)") \
_SFETCH_LOGITEM_XMACRO(REQUEST_USERDATA_SIZE_TOO_BIG, "user data size too big (see SFETCH_MAX_USERDATA_UINT64)") \
_SFETCH_LOGITEM_XMACRO(CLAMPING_NUM_CHANNELS_TO_MAX_CHANNELS, "clamping num channels to SFETCH_MAX_CHANNELS") \
_SFETCH_LOGITEM_XMACRO(REQUEST_POOL_EXHAUSTED, "request pool exhausted (tweak via sfetch_desc_t.max_requests)") \
#define _SFETCH_LOGITEM_XMACRO(item,msg) SFETCH_LOGITEM_##item,
typedef enum sfetch_log_item_t {
_SFETCH_LOG_ITEMS
} sfetch_log_item_t;
#undef _SFETCH_LOGITEM_XMACRO
/*
sfetch_logger_t
Used in sfetch_desc_t to provide a custom logging and error reporting
callback to sokol-fetch.
*/
typedef struct sfetch_logger_t {
void (*func)(
const char* tag, // always "sfetch"
uint32_t log_level, // 0=panic, 1=error, 2=warning, 3=info
uint32_t log_item_id, // SFETCH_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_fetch.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data);
void* user_data;
} sfetch_logger_t;
/*
sfetch_range_t
A pointer-size pair struct to pass memory ranges into and out of sokol-fetch.
When initialized from a value type (array or struct) you can use the
SFETCH_RANGE() helper macro to build an sfetch_range_t struct.
*/
typedef struct sfetch_range_t {
const void* ptr;
size_t size;
} sfetch_range_t;
// disabling this for every includer isn't great, but the warnings are also quite pointless
#if defined(_MSC_VER)
#pragma warning(disable:4221) // /W4 only: nonstandard extension used: 'x': cannot be initialized using address of automatic variable 'y'
#pragma warning(disable:4204) // VS2015: nonstandard extension used: non-constant aggregate initializer
#endif
#if defined(__cplusplus)
#define SFETCH_RANGE(x) sfetch_range_t{ &x, sizeof(x) }
#else
#define SFETCH_RANGE(x) (sfetch_range_t){ &x, sizeof(x) }
#endif
/*
sfetch_allocator_t
Used in sfetch_desc_t to provide custom memory-alloc and -free functions
to sokol_fetch.h. If memory management should be overridden, both the
alloc and free function must be provided (e.g. it's not valid to
override one function but not the other).
*/
typedef struct sfetch_allocator_t {
void* (*alloc_fn)(size_t size, void* user_data);
void (*free_fn)(void* ptr, void* user_data);
void* user_data;
} sfetch_allocator_t;
/* configuration values for sfetch_setup() */
typedef struct sfetch_desc_t {
uint32_t max_requests; // max number of active requests across all channels (default: 128)
uint32_t num_channels; // number of channels to fetch requests in parallel (default: 1)
uint32_t num_lanes; // max number of requests active on the same channel (default: 1)
sfetch_allocator_t allocator; // optional memory allocation overrides (default: malloc/free)
sfetch_logger_t logger; // optional log function overrides (default: NO LOGGING!)
} sfetch_desc_t;
/* a request handle to identify an active fetch request, returned by sfetch_send() */
typedef struct sfetch_handle_t { uint32_t id; } sfetch_handle_t;
/* error codes */
typedef enum sfetch_error_t {
SFETCH_ERROR_NO_ERROR,
SFETCH_ERROR_FILE_NOT_FOUND,
SFETCH_ERROR_NO_BUFFER,
SFETCH_ERROR_BUFFER_TOO_SMALL,
SFETCH_ERROR_UNEXPECTED_EOF,
SFETCH_ERROR_INVALID_HTTP_STATUS,
SFETCH_ERROR_CANCELLED
} sfetch_error_t;
/* the response struct passed to the response callback */
typedef struct sfetch_response_t {
sfetch_handle_t handle; // request handle this response belongs to
bool dispatched; // true when request is in DISPATCHED state (lane has been assigned)
bool fetched; // true when request is in FETCHED state (fetched data is available)
bool paused; // request is currently in paused state
bool finished; // this is the last response for this request
bool failed; // request has failed (always set together with 'finished')
bool cancelled; // request was cancelled (always set together with 'finished')
sfetch_error_t error_code; // more detailed error code when failed is true
uint32_t channel; // the channel which processes this request
uint32_t lane; // the lane this request occupies on its channel
const char* path; // the original filesystem path of the request
void* user_data; // pointer to read/write user-data area
uint32_t data_offset; // current offset of fetched data chunk in the overall file data
sfetch_range_t data; // the fetched data as ptr/size pair (data.ptr == buffer.ptr, data.size <= buffer.size)
sfetch_range_t buffer; // the user-provided buffer which holds the fetched data
} sfetch_response_t;
/* request parameters passed to sfetch_send() */
typedef struct sfetch_request_t {
uint32_t channel; // index of channel this request is assigned to (default: 0)
const char* path; // filesystem path or HTTP URL (required)
void (*callback) (const sfetch_response_t*); // response callback function pointer (required)
uint32_t chunk_size; // number of bytes to load per stream-block (optional)
sfetch_range_t buffer; // a memory buffer where the data will be loaded into (optional)
sfetch_range_t user_data; // ptr/size of a POD user data block which will be memcpy'd (optional)
} sfetch_request_t;
/* setup sokol-fetch (can be called on multiple threads) */
SOKOL_FETCH_API_DECL void sfetch_setup(const sfetch_desc_t* desc);
/* discard a sokol-fetch context */
SOKOL_FETCH_API_DECL void sfetch_shutdown(void);
/* return true if sokol-fetch has been setup */
SOKOL_FETCH_API_DECL bool sfetch_valid(void);
/* get the desc struct that was passed to sfetch_setup() */
SOKOL_FETCH_API_DECL sfetch_desc_t sfetch_desc(void);
/* return the max userdata size in number of bytes (SFETCH_MAX_USERDATA_UINT64 * sizeof(uint64_t)) */
SOKOL_FETCH_API_DECL int sfetch_max_userdata_bytes(void);
/* return the value of the SFETCH_MAX_PATH implementation config value */
SOKOL_FETCH_API_DECL int sfetch_max_path(void);
/* send a fetch-request, get handle to request back */
SOKOL_FETCH_API_DECL sfetch_handle_t sfetch_send(const sfetch_request_t* request);
/* return true if a handle is valid *and* the request is alive */
SOKOL_FETCH_API_DECL bool sfetch_handle_valid(sfetch_handle_t h);
/* do per-frame work, moves requests into and out of IO threads, and invokes response-callbacks */
SOKOL_FETCH_API_DECL void sfetch_dowork(void);
/* bind a data buffer to a request (request must not currently have a buffer bound, must be called from response callback */
SOKOL_FETCH_API_DECL void sfetch_bind_buffer(sfetch_handle_t h, sfetch_range_t buffer);
/* clear the 'buffer binding' of a request, returns previous buffer pointer (can be 0), must be called from response callback */
SOKOL_FETCH_API_DECL void* sfetch_unbind_buffer(sfetch_handle_t h);
/* cancel a request that's in flight (will call response callback with .cancelled + .finished) */
SOKOL_FETCH_API_DECL void sfetch_cancel(sfetch_handle_t h);
/* pause a request (will call response callback each frame with .paused) */
SOKOL_FETCH_API_DECL void sfetch_pause(sfetch_handle_t h);
/* continue a paused request */
SOKOL_FETCH_API_DECL void sfetch_continue(sfetch_handle_t h);
#ifdef __cplusplus
} /* extern "C" */
/* reference-based equivalents for c++ */
inline void sfetch_setup(const sfetch_desc_t& desc) { return sfetch_setup(&desc); }
inline sfetch_handle_t sfetch_send(const sfetch_request_t& request) { return sfetch_send(&request); }
#endif
#endif // SOKOL_FETCH_INCLUDED
// ██ ███ ███ ██████ ██ ███████ ███ ███ ███████ ███ ██ ████████ █████ ████████ ██ ██████ ███ ██
// ██ ████ ████ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
// ██ ██ ████ ██ ██████ ██ █████ ██ ████ ██ █████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ███████ ███████ ██ ██ ███████ ██ ████ ██ ██ ██ ██ ██ ██████ ██ ████
//
// >>implementation
#ifdef SOKOL_FETCH_IMPL
#define SOKOL_FETCH_IMPL_INCLUDED (1)
#if defined(SOKOL_MALLOC) || defined(SOKOL_CALLOC) || defined(SOKOL_FREE)
#error "SOKOL_MALLOC/CALLOC/FREE macros are no longer supported, please use sfetch_desc_t.allocator to override memory allocation functions"
#endif
#include <stdlib.h> /* malloc, free */
#include <string.h> /* memset, memcpy */
#ifndef SFETCH_MAX_PATH
#define SFETCH_MAX_PATH (1024)
#endif
#ifndef SFETCH_MAX_USERDATA_UINT64
#define SFETCH_MAX_USERDATA_UINT64 (16)
#endif
#ifndef SFETCH_MAX_CHANNELS
#define SFETCH_MAX_CHANNELS (16)
#endif
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
#ifndef SOKOL_DEBUG
#ifndef NDEBUG
#define SOKOL_DEBUG
#endif
#endif
#ifndef SOKOL_ASSERT
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
#ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static
#else
#define _SOKOL_PRIVATE static
#endif
#endif
#ifndef _SOKOL_UNUSED
#define _SOKOL_UNUSED(x) (void)(x)
#endif
#if defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h>
#define _SFETCH_PLATFORM_EMSCRIPTEN (1)
#define _SFETCH_PLATFORM_WINDOWS (0)
#define _SFETCH_PLATFORM_POSIX (0)
#define _SFETCH_HAS_THREADS (0)
#elif defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#define _SFETCH_PLATFORM_WINDOWS (1)
#define _SFETCH_PLATFORM_EMSCRIPTEN (0)
#define _SFETCH_PLATFORM_POSIX (0)
#define _SFETCH_HAS_THREADS (1)
#else
#include <pthread.h>
#include <stdio.h> /* fopen, fread, fseek, fclose */
#define _SFETCH_PLATFORM_POSIX (1)
#define _SFETCH_PLATFORM_EMSCRIPTEN (0)
#define _SFETCH_PLATFORM_WINDOWS (0)
#define _SFETCH_HAS_THREADS (1)
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4724) // potential mod by 0
#endif
// ███████ ████████ ██████ ██ ██ ██████ ████████ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██████ ██ ██ ██ ██ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██ ██ ██ ██████ ██████ ██ ███████
//
// >>structs
typedef struct _sfetch_path_t {
char buf[SFETCH_MAX_PATH];
} _sfetch_path_t;
/* a thread with incoming and outgoing message queue syncing */
#if _SFETCH_PLATFORM_POSIX
typedef struct {
pthread_t thread;
pthread_cond_t incoming_cond;
pthread_mutex_t incoming_mutex;
pthread_mutex_t outgoing_mutex;
pthread_mutex_t running_mutex;
pthread_mutex_t stop_mutex;
bool stop_requested;
bool valid;
} _sfetch_thread_t;
#elif _SFETCH_PLATFORM_WINDOWS
typedef struct {
HANDLE thread;
HANDLE incoming_event;
CRITICAL_SECTION incoming_critsec;
CRITICAL_SECTION outgoing_critsec;
CRITICAL_SECTION running_critsec;
CRITICAL_SECTION stop_critsec;
bool stop_requested;
bool valid;
} _sfetch_thread_t;
#endif
/* file handle abstraction */
#if _SFETCH_PLATFORM_POSIX
typedef FILE* _sfetch_file_handle_t;
#define _SFETCH_INVALID_FILE_HANDLE (0)
typedef void*(*_sfetch_thread_func_t)(void*);
#elif _SFETCH_PLATFORM_WINDOWS
typedef HANDLE _sfetch_file_handle_t;
#define _SFETCH_INVALID_FILE_HANDLE (INVALID_HANDLE_VALUE)
typedef LPTHREAD_START_ROUTINE _sfetch_thread_func_t;
#endif
/* user-side per-request state */
typedef struct {
bool pause; /* switch item to PAUSED state if true */
bool cont; /* switch item back to FETCHING if true */
bool cancel; /* cancel the request, switch into FAILED state */
/* transfer IO => user thread */
uint32_t fetched_offset; /* number of bytes fetched so far */
uint32_t fetched_size; /* size of last fetched chunk */
sfetch_error_t error_code;
bool finished;
/* user thread only */
size_t user_data_size;
uint64_t user_data[SFETCH_MAX_USERDATA_UINT64];
} _sfetch_item_user_t;
/* thread-side per-request state */
typedef struct {
/* transfer IO => user thread */
uint32_t fetched_offset;
uint32_t fetched_size;
sfetch_error_t error_code;
bool failed;
bool finished;
/* IO thread only */
#if _SFETCH_PLATFORM_EMSCRIPTEN
uint32_t http_range_offset;
#else
_sfetch_file_handle_t file_handle;
#endif
uint32_t content_size;
} _sfetch_item_thread_t;
/* a request goes through the following states, ping-ponging between IO and user thread */
typedef enum _sfetch_state_t {
_SFETCH_STATE_INITIAL, /* internal: request has just been initialized */
_SFETCH_STATE_ALLOCATED, /* internal: request has been allocated from internal pool */
_SFETCH_STATE_DISPATCHED, /* user thread: request has been dispatched to its IO channel */
_SFETCH_STATE_FETCHING, /* IO thread: waiting for data to be fetched */
_SFETCH_STATE_FETCHED, /* user thread: fetched data available */
_SFETCH_STATE_PAUSED, /* user thread: request has been paused via sfetch_pause() */
_SFETCH_STATE_FAILED, /* user thread: follow state or FETCHING if something went wrong */
} _sfetch_state_t;
/* an internal request item */
#define _SFETCH_INVALID_LANE (0xFFFFFFFF)
typedef struct {
sfetch_handle_t handle;
_sfetch_state_t state;
uint32_t channel;
uint32_t lane;
uint32_t chunk_size;
void (*callback) (const sfetch_response_t*);
sfetch_range_t buffer;
/* updated by IO-thread, off-limits to user thread */
_sfetch_item_thread_t thread;
/* accessible by user-thread, off-limits to IO thread */
_sfetch_item_user_t user;
/* big stuff at the end */
_sfetch_path_t path;
} _sfetch_item_t;
/* a pool of internal per-request items */
typedef struct {
uint32_t size;
uint32_t free_top;
_sfetch_item_t* items;
uint32_t* free_slots;
uint32_t* gen_ctrs;
bool valid;
} _sfetch_pool_t;
/* a ringbuffer for pool-slot ids */
typedef struct {
uint32_t head;
uint32_t tail;
uint32_t num;
uint32_t* buf;
} _sfetch_ring_t;
/* an IO channel with its own IO thread */
struct _sfetch_t;
typedef struct {
struct _sfetch_t* ctx; // back-pointer to thread-local _sfetch state pointer, since this isn't accessible from the IO threads
_sfetch_ring_t free_lanes;
_sfetch_ring_t user_sent;
_sfetch_ring_t user_incoming;
_sfetch_ring_t user_outgoing;
#if _SFETCH_HAS_THREADS
_sfetch_ring_t thread_incoming;
_sfetch_ring_t thread_outgoing;
_sfetch_thread_t thread;
#endif
void (*request_handler)(struct _sfetch_t* ctx, uint32_t slot_id);
bool valid;
} _sfetch_channel_t;
/* the sfetch global state */
typedef struct _sfetch_t {
bool setup;
bool valid;
bool in_callback;
sfetch_desc_t desc;
_sfetch_pool_t pool;
_sfetch_channel_t chn[SFETCH_MAX_CHANNELS];
} _sfetch_t;
#if _SFETCH_HAS_THREADS
#if defined(_MSC_VER)
static __declspec(thread) _sfetch_t* _sfetch;
#else
static __thread _sfetch_t* _sfetch;
#endif
#else
static _sfetch_t* _sfetch;
#endif
#define _sfetch_def(val, def) (((val) == 0) ? (def) : (val))
// ██ ██████ ██████ ██████ ██ ███ ██ ██████
// ██ ██ ██ ██ ██ ██ ████ ██ ██
// ██ ██ ██ ██ ███ ██ ███ ██ ██ ██ ██ ██ ███
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████ ██████ ██████ ██████ ██ ██ ████ ██████
//
// >>logging
#if defined(SOKOL_DEBUG)
#define _SFETCH_LOGITEM_XMACRO(item,msg) #item ": " msg,
static const char* _sfetch_log_messages[] = {
_SFETCH_LOG_ITEMS
};
#undef _SFETCH_LOGITEM_XMACRO
#endif // SOKOL_DEBUG
#define _SFETCH_PANIC(code) _sfetch_log(SFETCH_LOGITEM_ ##code, 0, __LINE__)
#define _SFETCH_ERROR(code) _sfetch_log(SFETCH_LOGITEM_ ##code, 1, __LINE__)
#define _SFETCH_WARN(code) _sfetch_log(SFETCH_LOGITEM_ ##code, 2, __LINE__)
#define _SFETCH_INFO(code) _sfetch_log(SFETCH_LOGITEM_ ##code, 3, __LINE__)
static void _sfetch_log(sfetch_log_item_t log_item, uint32_t log_level, uint32_t line_nr) {
if (_sfetch->desc.logger.func) {
#if defined(SOKOL_DEBUG)
const char* filename = __FILE__;
const char* message = _sfetch_log_messages[log_item];
#else
const char* filename = 0;
const char* message = 0;
#endif
_sfetch->desc.logger.func("sfetch", log_level, log_item, message, line_nr, filename, _sfetch->desc.logger.user_data);
}
else {
// for log level PANIC it would be 'undefined behaviour' to continue
if (log_level == 0) {
abort();
}
}
}
// ███ ███ ███████ ███ ███ ██████ ██████ ██ ██
// ████ ████ ██ ████ ████ ██ ██ ██ ██ ██ ██
// ██ ████ ██ █████ ██ ████ ██ ██ ██ ██████ ████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ██ ██ ██████ ██ ██ ██
//
// >>memory
_SOKOL_PRIVATE void _sfetch_clear(void* ptr, size_t size) {
SOKOL_ASSERT(ptr && (size > 0));
memset(ptr, 0, size);
}
_SOKOL_PRIVATE void* _sfetch_malloc_with_allocator(const sfetch_allocator_t* allocator, size_t size) {
SOKOL_ASSERT(size > 0);
void* ptr;
if (allocator->alloc_fn) {
ptr = allocator->alloc_fn(size, allocator->user_data);
} else {
ptr = malloc(size);
}
if (0 == ptr) {
_SFETCH_PANIC(MALLOC_FAILED);
}
return ptr;
}
_SOKOL_PRIVATE void* _sfetch_malloc(size_t size) {
return _sfetch_malloc_with_allocator(&_sfetch->desc.allocator, size);
}
_SOKOL_PRIVATE void* _sfetch_malloc_clear(size_t size) {
void* ptr = _sfetch_malloc(size);
_sfetch_clear(ptr, size);
return ptr;
}
_SOKOL_PRIVATE void _sfetch_free(void* ptr) {
if (_sfetch->desc.allocator.free_fn) {
_sfetch->desc.allocator.free_fn(ptr, _sfetch->desc.allocator.user_data);
} else {
free(ptr);
}
}
_SOKOL_PRIVATE _sfetch_t* _sfetch_ctx(void) {
return _sfetch;
}
_SOKOL_PRIVATE void _sfetch_path_copy(_sfetch_path_t* dst, const char* src) {
SOKOL_ASSERT(dst);
if (src && (strlen(src) < SFETCH_MAX_PATH)) {
#if defined(_MSC_VER)
strncpy_s(dst->buf, SFETCH_MAX_PATH, src, (SFETCH_MAX_PATH-1));
#else
strncpy(dst->buf, src, SFETCH_MAX_PATH);
#endif
dst->buf[SFETCH_MAX_PATH-1] = 0;
}
else {
_sfetch_clear(dst->buf, SFETCH_MAX_PATH);
}
}
_SOKOL_PRIVATE _sfetch_path_t _sfetch_path_make(const char* str) {
_sfetch_path_t res;
_sfetch_path_copy(&res, str);
return res;
}
// ███ ███ ███████ ███████ ███████ █████ ██████ ███████ ██████ ██ ██ ███████ ██ ██ ███████
// ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ████ ██ █████ ███████ ███████ ███████ ██ ███ █████ ██ ██ ██ ██ █████ ██ ██ █████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ▄▄ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ███████ ███████ ██ ██ ██████ ███████ ██████ ██████ ███████ ██████ ███████
// ▀▀
// >>message queue
_SOKOL_PRIVATE uint32_t _sfetch_ring_wrap(const _sfetch_ring_t* rb, uint32_t i) {
return i % rb->num;
}
_SOKOL_PRIVATE void _sfetch_ring_discard(_sfetch_ring_t* rb) {
SOKOL_ASSERT(rb);
if (rb->buf) {
_sfetch_free(rb->buf);
rb->buf = 0;
}
rb->head = 0;
rb->tail = 0;
rb->num = 0;
}
_SOKOL_PRIVATE bool _sfetch_ring_init(_sfetch_ring_t* rb, uint32_t num_slots) {
SOKOL_ASSERT(rb && (num_slots > 0));
SOKOL_ASSERT(0 == rb->buf);
rb->head = 0;
rb->tail = 0;
/* one slot reserved to detect full vs empty */
rb->num = num_slots + 1;
const size_t queue_size = rb->num * sizeof(sfetch_handle_t);
rb->buf = (uint32_t*) _sfetch_malloc_clear(queue_size);
if (rb->buf) {
return true;
}
else {
_sfetch_ring_discard(rb);
return false;
}
}
_SOKOL_PRIVATE bool _sfetch_ring_full(const _sfetch_ring_t* rb) {
SOKOL_ASSERT(rb && rb->buf);
return _sfetch_ring_wrap(rb, rb->head + 1) == rb->tail;
}
_SOKOL_PRIVATE bool _sfetch_ring_empty(const _sfetch_ring_t* rb) {
SOKOL_ASSERT(rb && rb->buf);
return rb->head == rb->tail;
}
_SOKOL_PRIVATE uint32_t _sfetch_ring_count(const _sfetch_ring_t* rb) {
SOKOL_ASSERT(rb && rb->buf);
uint32_t count;
if (rb->head >= rb->tail) {
count = rb->head - rb->tail;
}
else {
count = (rb->head + rb->num) - rb->tail;
}
SOKOL_ASSERT(count < rb->num);
return count;
}
_SOKOL_PRIVATE void _sfetch_ring_enqueue(_sfetch_ring_t* rb, uint32_t slot_id) {
SOKOL_ASSERT(rb && rb->buf);
SOKOL_ASSERT(!_sfetch_ring_full(rb));
SOKOL_ASSERT(rb->head < rb->num);
rb->buf[rb->head] = slot_id;
rb->head = _sfetch_ring_wrap(rb, rb->head + 1);
}
_SOKOL_PRIVATE uint32_t _sfetch_ring_dequeue(_sfetch_ring_t* rb) {
SOKOL_ASSERT(rb && rb->buf);
SOKOL_ASSERT(!_sfetch_ring_empty(rb));
SOKOL_ASSERT(rb->tail < rb->num);
uint32_t slot_id = rb->buf[rb->tail];
rb->tail = _sfetch_ring_wrap(rb, rb->tail + 1);
return slot_id;
}
_SOKOL_PRIVATE uint32_t _sfetch_ring_peek(const _sfetch_ring_t* rb, uint32_t index) {
SOKOL_ASSERT(rb && rb->buf);
SOKOL_ASSERT(!_sfetch_ring_empty(rb));
SOKOL_ASSERT(index < _sfetch_ring_count(rb));
uint32_t rb_index = _sfetch_ring_wrap(rb, rb->tail + index);
return rb->buf[rb_index];
}
// ██████ ███████ ██████ ██ ██ ███████ ███████ ████████ ██████ ██████ ██████ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ █████ ██ ██ ██ ██ █████ ███████ ██ ██████ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ▄▄ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ██████ ██████ ███████ ███████ ██ ██ ██████ ██████ ███████
// ▀▀
// >>request pool
_SOKOL_PRIVATE uint32_t _sfetch_make_id(uint32_t index, uint32_t gen_ctr) {
return (gen_ctr<<16) | (index & 0xFFFF);
}
_SOKOL_PRIVATE sfetch_handle_t _sfetch_make_handle(uint32_t slot_id) {
sfetch_handle_t h;
h.id = slot_id;
return h;
}
_SOKOL_PRIVATE uint32_t _sfetch_slot_index(uint32_t slot_id) {
return slot_id & 0xFFFF;
}
_SOKOL_PRIVATE void _sfetch_item_init(_sfetch_item_t* item, uint32_t slot_id, const sfetch_request_t* request) {
SOKOL_ASSERT(item && (0 == item->handle.id));
SOKOL_ASSERT(request && request->path);
_sfetch_clear(item, sizeof(_sfetch_item_t));
item->handle.id = slot_id;
item->state = _SFETCH_STATE_INITIAL;
item->channel = request->channel;
item->chunk_size = request->chunk_size;
item->lane = _SFETCH_INVALID_LANE;
item->callback = request->callback;
item->buffer = request->buffer;
item->path = _sfetch_path_make(request->path);
#if !_SFETCH_PLATFORM_EMSCRIPTEN
item->thread.file_handle = _SFETCH_INVALID_FILE_HANDLE;
#endif
if (request->user_data.ptr &&
(request->user_data.size > 0) &&
(request->user_data.size <= (SFETCH_MAX_USERDATA_UINT64*8)))
{
item->user.user_data_size = request->user_data.size;
memcpy(item->user.user_data, request->user_data.ptr, request->user_data.size);
}
}
_SOKOL_PRIVATE void _sfetch_item_discard(_sfetch_item_t* item) {
SOKOL_ASSERT(item && (0 != item->handle.id));
_sfetch_clear(item, sizeof(_sfetch_item_t));
}
_SOKOL_PRIVATE void _sfetch_pool_discard(_sfetch_pool_t* pool) {
SOKOL_ASSERT(pool);
if (pool->free_slots) {
_sfetch_free(pool->free_slots);
pool->free_slots = 0;
}
if (pool->gen_ctrs) {
_sfetch_free(pool->gen_ctrs);
pool->gen_ctrs = 0;
}
if (pool->items) {
_sfetch_free(pool->items);
pool->items = 0;
}
pool->size = 0;
pool->free_top = 0;
pool->valid = false;
}
_SOKOL_PRIVATE bool _sfetch_pool_init(_sfetch_pool_t* pool, uint32_t num_items) {
SOKOL_ASSERT(pool && (num_items > 0) && (num_items < ((1<<16)-1)));
SOKOL_ASSERT(0 == pool->items);
/* NOTE: item slot 0 is reserved for the special "invalid" item index 0*/
pool->size = num_items + 1;
pool->free_top = 0;
const size_t items_size = pool->size * sizeof(_sfetch_item_t);
pool->items = (_sfetch_item_t*) _sfetch_malloc_clear(items_size);
/* generation counters indexable by pool slot index, slot 0 is reserved */
const size_t gen_ctrs_size = sizeof(uint32_t) * pool->size;
pool->gen_ctrs = (uint32_t*) _sfetch_malloc_clear(gen_ctrs_size);
SOKOL_ASSERT(pool->gen_ctrs);
/* NOTE: it's not a bug to only reserve num_items here */
const size_t free_slots_size = num_items * sizeof(int);
pool->free_slots = (uint32_t*) _sfetch_malloc_clear(free_slots_size);
if (pool->items && pool->free_slots) {
/* never allocate the 0-th item, this is the reserved 'invalid item' */
for (uint32_t i = pool->size - 1; i >= 1; i--) {
pool->free_slots[pool->free_top++] = i;
}
pool->valid = true;
}
else {
/* allocation error */
_sfetch_pool_discard(pool);
}
return pool->valid;
}
_SOKOL_PRIVATE uint32_t _sfetch_pool_item_alloc(_sfetch_pool_t* pool, const sfetch_request_t* request) {
SOKOL_ASSERT(pool && pool->valid);
if (pool->free_top > 0) {
uint32_t slot_index = pool->free_slots[--pool->free_top];
SOKOL_ASSERT((slot_index > 0) && (slot_index < pool->size));
uint32_t slot_id = _sfetch_make_id(slot_index, ++pool->gen_ctrs[slot_index]);
_sfetch_item_init(&pool->items[slot_index], slot_id, request);
pool->items[slot_index].state = _SFETCH_STATE_ALLOCATED;
return slot_id;
}
else {
/* pool exhausted, return the 'invalid handle' */
return _sfetch_make_id(0, 0);
}
}
_SOKOL_PRIVATE void _sfetch_pool_item_free(_sfetch_pool_t* pool, uint32_t slot_id) {
SOKOL_ASSERT(pool && pool->valid);
uint32_t slot_index = _sfetch_slot_index(slot_id);
SOKOL_ASSERT((slot_index > 0) && (slot_index < pool->size));
SOKOL_ASSERT(pool->items[slot_index].handle.id == slot_id);
#if defined(SOKOL_DEBUG)
/* debug check against double-free */
for (uint32_t i = 0; i < pool->free_top; i++) {
SOKOL_ASSERT(pool->free_slots[i] != slot_index);
}
#endif
_sfetch_item_discard(&pool->items[slot_index]);
pool->free_slots[pool->free_top++] = slot_index;
SOKOL_ASSERT(pool->free_top <= (pool->size - 1));
}
/* return pointer to item by handle without matching id check */
_SOKOL_PRIVATE _sfetch_item_t* _sfetch_pool_item_at(_sfetch_pool_t* pool, uint32_t slot_id) {
SOKOL_ASSERT(pool && pool->valid);
uint32_t slot_index = _sfetch_slot_index(slot_id);
SOKOL_ASSERT((slot_index > 0) && (slot_index < pool->size));
return &pool->items[slot_index];
}
/* return pointer to item by handle with matching id check */
_SOKOL_PRIVATE _sfetch_item_t* _sfetch_pool_item_lookup(_sfetch_pool_t* pool, uint32_t slot_id) {
SOKOL_ASSERT(pool && pool->valid);
if (0 != slot_id) {
_sfetch_item_t* item = _sfetch_pool_item_at(pool, slot_id);
if (item->handle.id == slot_id) {
return item;
}
}
return 0;
}
// ██████ ██████ ███████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ██ ███████ ██ ███
// ██ ██ ██ ██ ██ ██ ██
// ██ ██████ ███████ ██ ██ ██
//
// >>posix
#if _SFETCH_PLATFORM_POSIX
_SOKOL_PRIVATE _sfetch_file_handle_t _sfetch_file_open(const _sfetch_path_t* path) {
return fopen(path->buf, "rb");
}
_SOKOL_PRIVATE void _sfetch_file_close(_sfetch_file_handle_t h) {
fclose(h);
}
_SOKOL_PRIVATE bool _sfetch_file_handle_valid(_sfetch_file_handle_t h) {
return h != _SFETCH_INVALID_FILE_HANDLE;
}
_SOKOL_PRIVATE uint32_t _sfetch_file_size(_sfetch_file_handle_t h) {
fseek(h, 0, SEEK_END);
return (uint32_t) ftell(h);
}
_SOKOL_PRIVATE bool _sfetch_file_read(_sfetch_file_handle_t h, uint32_t offset, uint32_t num_bytes, void* ptr) {
fseek(h, (long)offset, SEEK_SET);
return num_bytes == fread(ptr, 1, num_bytes, h);
}
_SOKOL_PRIVATE bool _sfetch_thread_init(_sfetch_thread_t* thread, _sfetch_thread_func_t thread_func, void* thread_arg) {
SOKOL_ASSERT(thread && !thread->valid && !thread->stop_requested);
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutex_init(&thread->incoming_mutex, &attr);
pthread_mutexattr_destroy(&attr);
pthread_mutexattr_init(&attr);
pthread_mutex_init(&thread->outgoing_mutex, &attr);
pthread_mutexattr_destroy(&attr);
pthread_mutexattr_init(&attr);
pthread_mutex_init(&thread->running_mutex, &attr);
pthread_mutexattr_destroy(&attr);
pthread_mutexattr_init(&attr);
pthread_mutex_init(&thread->stop_mutex, &attr);
pthread_mutexattr_destroy(&attr);
pthread_condattr_t cond_attr;
pthread_condattr_init(&cond_attr);
pthread_cond_init(&thread->incoming_cond, &cond_attr);
pthread_condattr_destroy(&cond_attr);
/* FIXME: in debug mode, the threads should be named */
pthread_mutex_lock(&thread->running_mutex);
int res = pthread_create(&thread->thread, 0, thread_func, thread_arg);
thread->valid = (0 == res);
pthread_mutex_unlock(&thread->running_mutex);
return thread->valid;
}
_SOKOL_PRIVATE void _sfetch_thread_request_stop(_sfetch_thread_t* thread) {
pthread_mutex_lock(&thread->stop_mutex);
thread->stop_requested = true;
pthread_mutex_unlock(&thread->stop_mutex);
}
_SOKOL_PRIVATE bool _sfetch_thread_stop_requested(_sfetch_thread_t* thread) {
pthread_mutex_lock(&thread->stop_mutex);
bool stop_requested = thread->stop_requested;
pthread_mutex_unlock(&thread->stop_mutex);
return stop_requested;
}
_SOKOL_PRIVATE void _sfetch_thread_join(_sfetch_thread_t* thread) {
SOKOL_ASSERT(thread);
if (thread->valid) {
pthread_mutex_lock(&thread->incoming_mutex);
_sfetch_thread_request_stop(thread);
pthread_cond_signal(&thread->incoming_cond);
pthread_mutex_unlock(&thread->incoming_mutex);
pthread_join(thread->thread, 0);
thread->valid = false;
}
pthread_mutex_destroy(&thread->stop_mutex);
pthread_mutex_destroy(&thread->running_mutex);
pthread_mutex_destroy(&thread->incoming_mutex);
pthread_mutex_destroy(&thread->outgoing_mutex);
pthread_cond_destroy(&thread->incoming_cond);
}
/* called when the thread-func is entered, this blocks the thread func until
the _sfetch_thread_t object is fully initialized
*/
_SOKOL_PRIVATE void _sfetch_thread_entered(_sfetch_thread_t* thread) {
pthread_mutex_lock(&thread->running_mutex);
}
/* called by the thread-func right before it is left */
_SOKOL_PRIVATE void _sfetch_thread_leaving(_sfetch_thread_t* thread) {
pthread_mutex_unlock(&thread->running_mutex);
}
_SOKOL_PRIVATE void _sfetch_thread_enqueue_incoming(_sfetch_thread_t* thread, _sfetch_ring_t* incoming, _sfetch_ring_t* src) {
/* called from user thread */
SOKOL_ASSERT(thread && thread->valid);
SOKOL_ASSERT(incoming && incoming->buf);
SOKOL_ASSERT(src && src->buf);
if (!_sfetch_ring_empty(src)) {
pthread_mutex_lock(&thread->incoming_mutex);
while (!_sfetch_ring_full(incoming) && !_sfetch_ring_empty(src)) {
_sfetch_ring_enqueue(incoming, _sfetch_ring_dequeue(src));
}
pthread_cond_signal(&thread->incoming_cond);
pthread_mutex_unlock(&thread->incoming_mutex);
}
}
_SOKOL_PRIVATE uint32_t _sfetch_thread_dequeue_incoming(_sfetch_thread_t* thread, _sfetch_ring_t* incoming) {
/* called from thread function */
SOKOL_ASSERT(thread && thread->valid);
SOKOL_ASSERT(incoming && incoming->buf);
pthread_mutex_lock(&thread->incoming_mutex);
while (_sfetch_ring_empty(incoming) && !thread->stop_requested) {
pthread_cond_wait(&thread->incoming_cond, &thread->incoming_mutex);
}
uint32_t item = 0;
if (!thread->stop_requested) {
item = _sfetch_ring_dequeue(incoming);
}
pthread_mutex_unlock(&thread->incoming_mutex);
return item;
}
_SOKOL_PRIVATE bool _sfetch_thread_enqueue_outgoing(_sfetch_thread_t* thread, _sfetch_ring_t* outgoing, uint32_t item) {
/* called from thread function */
SOKOL_ASSERT(thread && thread->valid);
SOKOL_ASSERT(outgoing && outgoing->buf);
SOKOL_ASSERT(0 != item);
pthread_mutex_lock(&thread->outgoing_mutex);
bool result = false;
if (!_sfetch_ring_full(outgoing)) {
_sfetch_ring_enqueue(outgoing, item);
}
pthread_mutex_unlock(&thread->outgoing_mutex);
return result;
}
_SOKOL_PRIVATE void _sfetch_thread_dequeue_outgoing(_sfetch_thread_t* thread, _sfetch_ring_t* outgoing, _sfetch_ring_t* dst) {
/* called from user thread */
SOKOL_ASSERT(thread && thread->valid);
SOKOL_ASSERT(outgoing && outgoing->buf);
SOKOL_ASSERT(dst && dst->buf);
pthread_mutex_lock(&thread->outgoing_mutex);
while (!_sfetch_ring_full(dst) && !_sfetch_ring_empty(outgoing)) {
_sfetch_ring_enqueue(dst, _sfetch_ring_dequeue(outgoing));
}
pthread_mutex_unlock(&thread->outgoing_mutex);
}
#endif /* _SFETCH_PLATFORM_POSIX */
// ██ ██ ██ ███ ██ ██████ ██████ ██ ██ ███████
// ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██
// ██ █ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █ ██ ███████
// ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██
// ███ ███ ██ ██ ████ ██████ ██████ ███ ███ ███████
//
// >>windows
#if _SFETCH_PLATFORM_WINDOWS
_SOKOL_PRIVATE bool _sfetch_win32_utf8_to_wide(const char* src, wchar_t* dst, int dst_num_bytes) {
SOKOL_ASSERT(src && dst && (dst_num_bytes > 1));
_sfetch_clear(dst, (size_t)dst_num_bytes);
const int dst_chars = dst_num_bytes / (int)sizeof(wchar_t);
const int dst_needed = MultiByteToWideChar(CP_UTF8, 0, src, -1, 0, 0);
if ((dst_needed > 0) && (dst_needed < dst_chars)) {
MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, dst_chars);
return true;
}
else {
/* input string doesn't fit into destination buffer */
return false;
}
}
_SOKOL_PRIVATE _sfetch_file_handle_t _sfetch_file_open(const _sfetch_path_t* path) {
wchar_t w_path[SFETCH_MAX_PATH];
if (!_sfetch_win32_utf8_to_wide(path->buf, w_path, sizeof(w_path))) {
_SFETCH_ERROR(FILE_PATH_UTF8_DECODING_FAILED);
return 0;
}
_sfetch_file_handle_t h = CreateFileW(
w_path, /* lpFileName */
GENERIC_READ, /* dwDesiredAccess */
FILE_SHARE_READ, /* dwShareMode */
NULL, /* lpSecurityAttributes */
OPEN_EXISTING, /* dwCreationDisposition */
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN, /* dwFlagsAndAttributes */
NULL); /* hTemplateFile */
return h;
}
_SOKOL_PRIVATE void _sfetch_file_close(_sfetch_file_handle_t h) {
CloseHandle(h);
}
_SOKOL_PRIVATE bool _sfetch_file_handle_valid(_sfetch_file_handle_t h) {
return h != _SFETCH_INVALID_FILE_HANDLE;
}
_SOKOL_PRIVATE uint32_t _sfetch_file_size(_sfetch_file_handle_t h) {
return GetFileSize(h, NULL);
}
_SOKOL_PRIVATE bool _sfetch_file_read(_sfetch_file_handle_t h, uint32_t offset, uint32_t num_bytes, void* ptr) {
LARGE_INTEGER offset_li;
offset_li.QuadPart = offset;
BOOL seek_res = SetFilePointerEx(h, offset_li, NULL, FILE_BEGIN);
if (seek_res) {
DWORD bytes_read = 0;
BOOL read_res = ReadFile(h, ptr, (DWORD)num_bytes, &bytes_read, NULL);
return read_res && (bytes_read == num_bytes);
}
else {
return false;
}
}
_SOKOL_PRIVATE bool _sfetch_thread_init(_sfetch_thread_t* thread, _sfetch_thread_func_t thread_func, void* thread_arg) {
SOKOL_ASSERT(thread && !thread->valid && !thread->stop_requested);
thread->incoming_event = CreateEventA(NULL, FALSE, FALSE, NULL);
SOKOL_ASSERT(NULL != thread->incoming_event);
InitializeCriticalSection(&thread->incoming_critsec);
InitializeCriticalSection(&thread->outgoing_critsec);
InitializeCriticalSection(&thread->running_critsec);
InitializeCriticalSection(&thread->stop_critsec);
EnterCriticalSection(&thread->running_critsec);
const SIZE_T stack_size = 512 * 1024;
thread->thread = CreateThread(NULL, stack_size, thread_func, thread_arg, 0, NULL);
thread->valid = (NULL != thread->thread);
LeaveCriticalSection(&thread->running_critsec);
return thread->valid;
}
_SOKOL_PRIVATE void _sfetch_thread_request_stop(_sfetch_thread_t* thread) {
EnterCriticalSection(&thread->stop_critsec);
thread->stop_requested = true;
LeaveCriticalSection(&thread->stop_critsec);
}
_SOKOL_PRIVATE bool _sfetch_thread_stop_requested(_sfetch_thread_t* thread) {
EnterCriticalSection(&thread->stop_critsec);
bool stop_requested = thread->stop_requested;
LeaveCriticalSection(&thread->stop_critsec);
return stop_requested;
}
_SOKOL_PRIVATE void _sfetch_thread_join(_sfetch_thread_t* thread) {
if (thread->valid) {
EnterCriticalSection(&thread->incoming_critsec);
_sfetch_thread_request_stop(thread);
BOOL set_event_res = SetEvent(thread->incoming_event);
_SOKOL_UNUSED(set_event_res);
SOKOL_ASSERT(set_event_res);
LeaveCriticalSection(&thread->incoming_critsec);
WaitForSingleObject(thread->thread, INFINITE);
CloseHandle(thread->thread);
thread->valid = false;
}
CloseHandle(thread->incoming_event);
DeleteCriticalSection(&thread->stop_critsec);
DeleteCriticalSection(&thread->running_critsec);
DeleteCriticalSection(&thread->outgoing_critsec);
DeleteCriticalSection(&thread->incoming_critsec);
}
_SOKOL_PRIVATE void _sfetch_thread_entered(_sfetch_thread_t* thread) {
EnterCriticalSection(&thread->running_critsec);
}
/* called by the thread-func right before it is left */
_SOKOL_PRIVATE void _sfetch_thread_leaving(_sfetch_thread_t* thread) {
LeaveCriticalSection(&thread->running_critsec);
}
_SOKOL_PRIVATE void _sfetch_thread_enqueue_incoming(_sfetch_thread_t* thread, _sfetch_ring_t* incoming, _sfetch_ring_t* src) {
/* called from user thread */
SOKOL_ASSERT(thread && thread->valid);
SOKOL_ASSERT(incoming && incoming->buf);
SOKOL_ASSERT(src && src->buf);
if (!_sfetch_ring_empty(src)) {
EnterCriticalSection(&thread->incoming_critsec);
while (!_sfetch_ring_full(incoming) && !_sfetch_ring_empty(src)) {
_sfetch_ring_enqueue(incoming, _sfetch_ring_dequeue(src));
}
LeaveCriticalSection(&thread->incoming_critsec);
BOOL set_event_res = SetEvent(thread->incoming_event);
_SOKOL_UNUSED(set_event_res);
SOKOL_ASSERT(set_event_res);
}
}
_SOKOL_PRIVATE uint32_t _sfetch_thread_dequeue_incoming(_sfetch_thread_t* thread, _sfetch_ring_t* incoming) {
/* called from thread function */
SOKOL_ASSERT(thread && thread->valid);
SOKOL_ASSERT(incoming && incoming->buf);
EnterCriticalSection(&thread->incoming_critsec);
while (_sfetch_ring_empty(incoming) && !thread->stop_requested) {
LeaveCriticalSection(&thread->incoming_critsec);
WaitForSingleObject(thread->incoming_event, INFINITE);
EnterCriticalSection(&thread->incoming_critsec);
}
uint32_t item = 0;
if (!thread->stop_requested) {
item = _sfetch_ring_dequeue(incoming);
}
LeaveCriticalSection(&thread->incoming_critsec);
return item;
}
_SOKOL_PRIVATE bool _sfetch_thread_enqueue_outgoing(_sfetch_thread_t* thread, _sfetch_ring_t* outgoing, uint32_t item) {
/* called from thread function */
SOKOL_ASSERT(thread && thread->valid);
SOKOL_ASSERT(outgoing && outgoing->buf);
EnterCriticalSection(&thread->outgoing_critsec);
bool result = false;
if (!_sfetch_ring_full(outgoing)) {
_sfetch_ring_enqueue(outgoing, item);
}
LeaveCriticalSection(&thread->outgoing_critsec);
return result;
}
_SOKOL_PRIVATE void _sfetch_thread_dequeue_outgoing(_sfetch_thread_t* thread, _sfetch_ring_t* outgoing, _sfetch_ring_t* dst) {
/* called from user thread */
SOKOL_ASSERT(thread && thread->valid);
SOKOL_ASSERT(outgoing && outgoing->buf);
SOKOL_ASSERT(dst && dst->buf);
EnterCriticalSection(&thread->outgoing_critsec);
while (!_sfetch_ring_full(dst) && !_sfetch_ring_empty(outgoing)) {
_sfetch_ring_enqueue(dst, _sfetch_ring_dequeue(outgoing));
}
LeaveCriticalSection(&thread->outgoing_critsec);
}
#endif /* _SFETCH_PLATFORM_WINDOWS */
// ██████ ██ ██ █████ ███ ██ ███ ██ ███████ ██ ███████
// ██ ██ ██ ██ ██ ████ ██ ████ ██ ██ ██ ██
// ██ ███████ ███████ ██ ██ ██ ██ ██ ██ █████ ██ ███████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ██ ██ ██ ██ ████ ██ ████ ███████ ███████ ███████
//
// >>channels
/* per-channel request handler for native platforms accessing the local filesystem */
#if _SFETCH_HAS_THREADS
_SOKOL_PRIVATE void _sfetch_request_handler(_sfetch_t* ctx, uint32_t slot_id) {
_sfetch_state_t state;
_sfetch_path_t* path;
_sfetch_item_thread_t* thread;
sfetch_range_t* buffer;
uint32_t chunk_size;
{
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, slot_id);
if (!item) {
return;
}
state = item->state;
SOKOL_ASSERT((state == _SFETCH_STATE_FETCHING) ||
(state == _SFETCH_STATE_PAUSED) ||
(state == _SFETCH_STATE_FAILED));
path = &item->path;
thread = &item->thread;
buffer = &item->buffer;
chunk_size = item->chunk_size;
}
if (thread->failed) {
return;
}
if (state == _SFETCH_STATE_FETCHING) {
if ((buffer->ptr == 0) || (buffer->size == 0)) {
thread->error_code = SFETCH_ERROR_NO_BUFFER;
thread->failed = true;
}
else {
/* open file if not happened yet */
if (!_sfetch_file_handle_valid(thread->file_handle)) {
SOKOL_ASSERT(path->buf[0]);
SOKOL_ASSERT(thread->fetched_offset == 0);
SOKOL_ASSERT(thread->fetched_size == 0);
thread->file_handle = _sfetch_file_open(path);
if (_sfetch_file_handle_valid(thread->file_handle)) {
thread->content_size = _sfetch_file_size(thread->file_handle);
}
else {
thread->error_code = SFETCH_ERROR_FILE_NOT_FOUND;
thread->failed = true;
}
}
if (!thread->failed) {
uint32_t read_offset = 0;
uint32_t bytes_to_read = 0;
if (chunk_size == 0) {
/* load entire file */
if (thread->content_size <= buffer->size) {
bytes_to_read = thread->content_size;
read_offset = 0;
}
else {
/* provided buffer to small to fit entire file */
thread->error_code = SFETCH_ERROR_BUFFER_TOO_SMALL;
thread->failed = true;
}
}
else {
if (chunk_size <= buffer->size) {
bytes_to_read = chunk_size;
read_offset = thread->fetched_offset;
if ((read_offset + bytes_to_read) > thread->content_size) {
bytes_to_read = thread->content_size - read_offset;
}
}
else {
/* provided buffer to small to fit next chunk */
thread->error_code = SFETCH_ERROR_BUFFER_TOO_SMALL;
thread->failed = true;
}
}
if (!thread->failed) {
if (_sfetch_file_read(thread->file_handle, read_offset, bytes_to_read, (void*)buffer->ptr)) {
thread->fetched_size = bytes_to_read;
thread->fetched_offset += bytes_to_read;
}
else {
thread->error_code = SFETCH_ERROR_UNEXPECTED_EOF;
thread->failed = true;
}
}
}
}
SOKOL_ASSERT(thread->fetched_offset <= thread->content_size);
if (thread->failed || (thread->fetched_offset == thread->content_size)) {
if (_sfetch_file_handle_valid(thread->file_handle)) {
_sfetch_file_close(thread->file_handle);
thread->file_handle = _SFETCH_INVALID_FILE_HANDLE;
}
thread->finished = true;
}
}
/* ignore items in PAUSED or FAILED state */
}
#if _SFETCH_PLATFORM_WINDOWS
_SOKOL_PRIVATE DWORD WINAPI _sfetch_channel_thread_func(LPVOID arg) {
#else
_SOKOL_PRIVATE void* _sfetch_channel_thread_func(void* arg) {
#endif
_sfetch_channel_t* chn = (_sfetch_channel_t*) arg;
_sfetch_thread_entered(&chn->thread);
while (!_sfetch_thread_stop_requested(&chn->thread)) {
/* block until work arrives */
uint32_t slot_id = _sfetch_thread_dequeue_incoming(&chn->thread, &chn->thread_incoming);
/* slot_id will be invalid if the thread was woken up to join */
if (!_sfetch_thread_stop_requested(&chn->thread)) {
SOKOL_ASSERT(0 != slot_id);
chn->request_handler(chn->ctx, slot_id);
SOKOL_ASSERT(!_sfetch_ring_full(&chn->thread_outgoing));
_sfetch_thread_enqueue_outgoing(&chn->thread, &chn->thread_outgoing, slot_id);
}
}
_sfetch_thread_leaving(&chn->thread);
return 0;
}
#endif /* _SFETCH_HAS_THREADS */
#if _SFETCH_PLATFORM_EMSCRIPTEN
EM_JS(void, sfetch_js_send_head_request, (uint32_t slot_id, const char* path_cstr), {
const path_str = UTF8ToString(path_cstr);
const req = new XMLHttpRequest();
req.open('HEAD', path_str);
req.onreadystatechange = function() {
if (req.readyState == XMLHttpRequest.DONE) {
if (req.status == 200) {
const content_length = req.getResponseHeader('Content-Length');
__sfetch_emsc_head_response(slot_id, content_length);
}
else {
__sfetch_emsc_failed_http_status(slot_id, req.status);
}
}
};
req.send();
});
/* if bytes_to_read != 0, a range-request will be sent, otherwise a normal request */
EM_JS(void, sfetch_js_send_get_request, (uint32_t slot_id, const char* path_cstr, uint32_t offset, uint32_t bytes_to_read, void* buf_ptr, uint32_t buf_size), {
const path_str = UTF8ToString(path_cstr);
const req = new XMLHttpRequest();
req.open('GET', path_str);
req.responseType = 'arraybuffer';
const need_range_request = (bytes_to_read > 0);
if (need_range_request) {
req.setRequestHeader('Range', 'bytes='+offset+'-'+(offset+bytes_to_read-1));
}
req.onreadystatechange = function() {
if (req.readyState == XMLHttpRequest.DONE) {
if ((req.status == 206) || ((req.status == 200) && !need_range_request)) {
const u8_array = new Uint8Array(\x2F\x2A\x2A @type {!ArrayBuffer} \x2A\x2F (req.response));
const content_fetched_size = u8_array.length;
if (content_fetched_size <= buf_size) {
HEAPU8.set(u8_array, buf_ptr);
__sfetch_emsc_get_response(slot_id, bytes_to_read, content_fetched_size);
}
else {
__sfetch_emsc_failed_buffer_too_small(slot_id);
}
}
else {
__sfetch_emsc_failed_http_status(slot_id, req.status);
}
}
};
req.send();
});
/*=== emscripten specific C helper functions =================================*/
#ifdef __cplusplus
extern "C" {
#endif
void _sfetch_emsc_send_get_request(uint32_t slot_id, _sfetch_item_t* item) {
if ((item->buffer.ptr == 0) || (item->buffer.size == 0)) {
item->thread.error_code = SFETCH_ERROR_NO_BUFFER;
item->thread.failed = true;
}
else {
uint32_t offset = 0;
uint32_t bytes_to_read = 0;
if (item->chunk_size > 0) {
/* send HTTP range request */
SOKOL_ASSERT(item->thread.content_size > 0);
SOKOL_ASSERT(item->thread.http_range_offset < item->thread.content_size);
bytes_to_read = item->thread.content_size - item->thread.http_range_offset;
if (bytes_to_read > item->chunk_size) {
bytes_to_read = item->chunk_size;
}
SOKOL_ASSERT(bytes_to_read > 0);
offset = item->thread.http_range_offset;
}
sfetch_js_send_get_request(slot_id, item->path.buf, offset, bytes_to_read, (void*)item->buffer.ptr, item->buffer.size);
}
}
/* called by JS when an initial HEAD request finished successfully (only when streaming chunks) */
EMSCRIPTEN_KEEPALIVE void _sfetch_emsc_head_response(uint32_t slot_id, uint32_t content_length) {
_sfetch_t* ctx = _sfetch_ctx();
if (ctx && ctx->valid) {
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, slot_id);
if (item) {
SOKOL_ASSERT(item->buffer.ptr && (item->buffer.size > 0));
item->thread.content_size = content_length;
_sfetch_emsc_send_get_request(slot_id, item);
}
}
}
/* called by JS when a followup GET request finished successfully */
EMSCRIPTEN_KEEPALIVE void _sfetch_emsc_get_response(uint32_t slot_id, uint32_t range_fetched_size, uint32_t content_fetched_size) {
_sfetch_t* ctx = _sfetch_ctx();
if (ctx && ctx->valid) {
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, slot_id);
if (item) {
item->thread.fetched_size = content_fetched_size;
item->thread.fetched_offset += content_fetched_size;
item->thread.http_range_offset += range_fetched_size;
if (item->chunk_size == 0) {
item->thread.finished = true;
}
else if (item->thread.http_range_offset >= item->thread.content_size) {
item->thread.finished = true;
}
_sfetch_ring_enqueue(&ctx->chn[item->channel].user_outgoing, slot_id);
}
}
}
/* called by JS when an error occurred */
EMSCRIPTEN_KEEPALIVE void _sfetch_emsc_failed_http_status(uint32_t slot_id, uint32_t http_status) {
_sfetch_t* ctx = _sfetch_ctx();
if (ctx && ctx->valid) {
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, slot_id);
if (item) {
if (http_status == 404) {
item->thread.error_code = SFETCH_ERROR_FILE_NOT_FOUND;
}
else {
item->thread.error_code = SFETCH_ERROR_INVALID_HTTP_STATUS;
}
item->thread.failed = true;
item->thread.finished = true;
_sfetch_ring_enqueue(&ctx->chn[item->channel].user_outgoing, slot_id);
}
}
}
EMSCRIPTEN_KEEPALIVE void _sfetch_emsc_failed_buffer_too_small(uint32_t slot_id) {
_sfetch_t* ctx = _sfetch_ctx();
if (ctx && ctx->valid) {
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, slot_id);
if (item) {
item->thread.error_code = SFETCH_ERROR_BUFFER_TOO_SMALL;
item->thread.failed = true;
item->thread.finished = true;
_sfetch_ring_enqueue(&ctx->chn[item->channel].user_outgoing, slot_id);
}
}
}
#ifdef __cplusplus
} /* extern "C" */
#endif
_SOKOL_PRIVATE void _sfetch_request_handler(_sfetch_t* ctx, uint32_t slot_id) {
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, slot_id);
if (!item) {
return;
}
if (item->state == _SFETCH_STATE_FETCHING) {
if ((item->chunk_size > 0) && (item->thread.content_size == 0)) {
/* if streaming download is requested, and the content-length isn't known
yet, need to send a HEAD request first
*/
sfetch_js_send_head_request(slot_id, item->path.buf);
}
else {
/* otherwise, this is either a request to load the entire file, or
to load the next streaming chunk
*/
_sfetch_emsc_send_get_request(slot_id, item);
}
}
else {
/* just move all other items (e.g. paused or cancelled)
into the outgoing queue, so they won't get lost
*/
_sfetch_ring_enqueue(&ctx->chn[item->channel].user_outgoing, slot_id);
}
if (item->thread.failed) {
item->thread.finished = true;
}
}
#endif /* _SFETCH_PLATFORM_EMSCRIPTEN */
_SOKOL_PRIVATE void _sfetch_channel_discard(_sfetch_channel_t* chn) {
SOKOL_ASSERT(chn);
#if _SFETCH_HAS_THREADS
if (chn->valid) {
_sfetch_thread_join(&chn->thread);
}
_sfetch_ring_discard(&chn->thread_incoming);
_sfetch_ring_discard(&chn->thread_outgoing);
#endif
_sfetch_ring_discard(&chn->free_lanes);
_sfetch_ring_discard(&chn->user_sent);
_sfetch_ring_discard(&chn->user_incoming);
_sfetch_ring_discard(&chn->user_outgoing);
_sfetch_ring_discard(&chn->free_lanes);
chn->valid = false;
}
_SOKOL_PRIVATE bool _sfetch_channel_init(_sfetch_channel_t* chn, _sfetch_t* ctx, uint32_t num_items, uint32_t num_lanes, void (*request_handler)(_sfetch_t* ctx, uint32_t)) {
SOKOL_ASSERT(chn && (num_items > 0) && request_handler);
SOKOL_ASSERT(!chn->valid);
bool valid = true;
chn->request_handler = request_handler;
chn->ctx = ctx;
valid &= _sfetch_ring_init(&chn->free_lanes, num_lanes);
for (uint32_t lane = 0; lane < num_lanes; lane++) {
_sfetch_ring_enqueue(&chn->free_lanes, lane);
}
valid &= _sfetch_ring_init(&chn->user_sent, num_items);
valid &= _sfetch_ring_init(&chn->user_incoming, num_lanes);
valid &= _sfetch_ring_init(&chn->user_outgoing, num_lanes);
#if _SFETCH_HAS_THREADS
valid &= _sfetch_ring_init(&chn->thread_incoming, num_lanes);
valid &= _sfetch_ring_init(&chn->thread_outgoing, num_lanes);
#endif
if (valid) {
chn->valid = true;
#if _SFETCH_HAS_THREADS
_sfetch_thread_init(&chn->thread, _sfetch_channel_thread_func, chn);
#endif
return true;
}
else {
_sfetch_channel_discard(chn);
return false;
}
}
/* put a request into the channels sent-queue, this is where all new requests
are stored until a lane becomes free.
*/
_SOKOL_PRIVATE bool _sfetch_channel_send(_sfetch_channel_t* chn, uint32_t slot_id) {
SOKOL_ASSERT(chn && chn->valid);
if (!_sfetch_ring_full(&chn->user_sent)) {
_sfetch_ring_enqueue(&chn->user_sent, slot_id);
return true;
}
else {
_SFETCH_ERROR(SEND_QUEUE_FULL);
return false;
}
}
_SOKOL_PRIVATE void _sfetch_invoke_response_callback(_sfetch_item_t* item) {
sfetch_response_t response;
_sfetch_clear(&response, sizeof(response));
response.handle = item->handle;
response.dispatched = (item->state == _SFETCH_STATE_DISPATCHED);
response.fetched = (item->state == _SFETCH_STATE_FETCHED);
response.paused = (item->state == _SFETCH_STATE_PAUSED);
response.finished = item->user.finished;
response.failed = (item->state == _SFETCH_STATE_FAILED);
response.cancelled = item->user.cancel;
response.error_code = item->user.error_code;
response.channel = item->channel;
response.lane = item->lane;
response.path = item->path.buf;
response.user_data = item->user.user_data;
response.data_offset = item->user.fetched_offset - item->user.fetched_size;
response.data.ptr = item->buffer.ptr;
response.data.size = item->user.fetched_size;
response.buffer = item->buffer;
item->callback(&response);
}
_SOKOL_PRIVATE void _sfetch_cancel_item(_sfetch_item_t* item) {
item->state = _SFETCH_STATE_FAILED;
item->user.finished = true;
item->user.error_code = SFETCH_ERROR_CANCELLED;
}
/* per-frame channel stuff: move requests in and out of the IO threads, call response callbacks */
_SOKOL_PRIVATE void _sfetch_channel_dowork(_sfetch_channel_t* chn, _sfetch_pool_t* pool) {
/* move items from sent- to incoming-queue permitting free lanes */
const uint32_t num_sent = _sfetch_ring_count(&chn->user_sent);
const uint32_t avail_lanes = _sfetch_ring_count(&chn->free_lanes);
const uint32_t num_move = (num_sent < avail_lanes) ? num_sent : avail_lanes;
for (uint32_t i = 0; i < num_move; i++) {
const uint32_t slot_id = _sfetch_ring_dequeue(&chn->user_sent);
_sfetch_item_t* item = _sfetch_pool_item_lookup(pool, slot_id);
SOKOL_ASSERT(item);
SOKOL_ASSERT(item->state == _SFETCH_STATE_ALLOCATED);
// if the item was cancelled early, kick it out immediately
if (item->user.cancel) {
_sfetch_cancel_item(item);
_sfetch_invoke_response_callback(item);
_sfetch_pool_item_free(pool, slot_id);
continue;
}
item->state = _SFETCH_STATE_DISPATCHED;
item->lane = _sfetch_ring_dequeue(&chn->free_lanes);
// if no buffer provided yet, invoke response callback to do so
if (0 == item->buffer.ptr) {
_sfetch_invoke_response_callback(item);
}
_sfetch_ring_enqueue(&chn->user_incoming, slot_id);
}
/* prepare incoming items for being moved into the IO thread */
const uint32_t num_incoming = _sfetch_ring_count(&chn->user_incoming);
for (uint32_t i = 0; i < num_incoming; i++) {
const uint32_t slot_id = _sfetch_ring_peek(&chn->user_incoming, i);
_sfetch_item_t* item = _sfetch_pool_item_lookup(pool, slot_id);
SOKOL_ASSERT(item);
SOKOL_ASSERT(item->state != _SFETCH_STATE_INITIAL);
SOKOL_ASSERT(item->state != _SFETCH_STATE_FETCHING);
/* transfer input params from user- to thread-data */
if (item->user.pause) {
item->state = _SFETCH_STATE_PAUSED;
item->user.pause = false;
}
if (item->user.cont) {
if (item->state == _SFETCH_STATE_PAUSED) {
item->state = _SFETCH_STATE_FETCHED;
}
item->user.cont = false;
}
if (item->user.cancel) {
_sfetch_cancel_item(item);
}
switch (item->state) {
case _SFETCH_STATE_DISPATCHED:
case _SFETCH_STATE_FETCHED:
item->state = _SFETCH_STATE_FETCHING;
break;
default: break;
}
}
#if _SFETCH_HAS_THREADS
/* move new items into the IO threads and processed items out of IO threads */
_sfetch_thread_enqueue_incoming(&chn->thread, &chn->thread_incoming, &chn->user_incoming);
_sfetch_thread_dequeue_outgoing(&chn->thread, &chn->thread_outgoing, &chn->user_outgoing);
#else
/* without threading just directly dequeue items from the user_incoming queue and
call the request handler, the user_outgoing queue will be filled as the
asynchronous HTTP requests sent by the request handler are completed
*/
while (!_sfetch_ring_empty(&chn->user_incoming)) {
uint32_t slot_id = _sfetch_ring_dequeue(&chn->user_incoming);
_sfetch_request_handler(chn->ctx, slot_id);
}
#endif
/* drain the outgoing queue, prepare items for invoking the response
callback, and finally call the response callback, free finished items
*/
while (!_sfetch_ring_empty(&chn->user_outgoing)) {
const uint32_t slot_id = _sfetch_ring_dequeue(&chn->user_outgoing);
SOKOL_ASSERT(slot_id);
_sfetch_item_t* item = _sfetch_pool_item_lookup(pool, slot_id);
SOKOL_ASSERT(item && item->callback);
SOKOL_ASSERT(item->state != _SFETCH_STATE_INITIAL);
SOKOL_ASSERT(item->state != _SFETCH_STATE_ALLOCATED);
SOKOL_ASSERT(item->state != _SFETCH_STATE_DISPATCHED);
SOKOL_ASSERT(item->state != _SFETCH_STATE_FETCHED);
/* transfer output params from thread- to user-data */
item->user.fetched_offset = item->thread.fetched_offset;
item->user.fetched_size = item->thread.fetched_size;
if (item->user.cancel) {
_sfetch_cancel_item(item);
}
else {
item->user.error_code = item->thread.error_code;
}
if (item->thread.finished) {
item->user.finished = true;
}
/* state transition */
if (item->thread.failed) {
item->state = _SFETCH_STATE_FAILED;
}
else if (item->state == _SFETCH_STATE_FETCHING) {
item->state = _SFETCH_STATE_FETCHED;
}
_sfetch_invoke_response_callback(item);
/* when the request is finished, free the lane for another request,
otherwise feed it back into the incoming queue
*/
if (item->user.finished) {
_sfetch_ring_enqueue(&chn->free_lanes, item->lane);
_sfetch_pool_item_free(pool, slot_id);
}
else {
_sfetch_ring_enqueue(&chn->user_incoming, slot_id);
}
}
}
_SOKOL_PRIVATE bool _sfetch_validate_request(_sfetch_t* ctx, const sfetch_request_t* req) {
if (req->channel >= ctx->desc.num_channels) {
_SFETCH_ERROR(REQUEST_CHANNEL_INDEX_TOO_BIG);
return false;
}
if (!req->path) {
_SFETCH_ERROR(REQUEST_PATH_IS_NULL);
return false;
}
if (strlen(req->path) >= (SFETCH_MAX_PATH-1)) {
_SFETCH_ERROR(REQUEST_PATH_TOO_LONG);
return false;
}
if (!req->callback) {
_SFETCH_ERROR(REQUEST_CALLBACK_MISSING);
return false;
}
if (req->chunk_size > req->buffer.size) {
_SFETCH_ERROR(REQUEST_CHUNK_SIZE_GREATER_BUFFER_SIZE);
return false;
}
if (req->user_data.ptr && (req->user_data.size == 0)) {
_SFETCH_ERROR(REQUEST_USERDATA_PTR_IS_SET_BUT_USERDATA_SIZE_IS_NULL);
return false;
}
if (!req->user_data.ptr && (req->user_data.size > 0)) {
_SFETCH_ERROR(REQUEST_USERDATA_PTR_IS_NULL_BUT_USERDATA_SIZE_IS_NOT);
return false;
}
if (req->user_data.size > SFETCH_MAX_USERDATA_UINT64 * sizeof(uint64_t)) {
_SFETCH_ERROR(REQUEST_USERDATA_SIZE_TOO_BIG);
return false;
}
return true;
}
_SOKOL_PRIVATE sfetch_desc_t _sfetch_desc_defaults(const sfetch_desc_t* desc) {
SOKOL_ASSERT((desc->allocator.alloc_fn && desc->allocator.free_fn) || (!desc->allocator.alloc_fn && !desc->allocator.free_fn));
sfetch_desc_t res = *desc;
res.max_requests = _sfetch_def(desc->max_requests, 128);
res.num_channels = _sfetch_def(desc->num_channels, 1);
res.num_lanes = _sfetch_def(desc->num_lanes, 1);
return res;
}
// ██████ ██ ██ ██████ ██ ██ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ██ ██████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██████ ██████ ███████ ██ ██████
//
// >>public
SOKOL_API_IMPL void sfetch_setup(const sfetch_desc_t* desc_) {
SOKOL_ASSERT(desc_);
SOKOL_ASSERT(0 == _sfetch);
sfetch_desc_t desc = _sfetch_desc_defaults(desc_);
_sfetch = (_sfetch_t*) _sfetch_malloc_with_allocator(&desc.allocator, sizeof(_sfetch_t));
SOKOL_ASSERT(_sfetch);
_sfetch_t* ctx = _sfetch_ctx();
_sfetch_clear(ctx, sizeof(_sfetch_t));
ctx->desc = desc;
ctx->setup = true;
ctx->valid = true;
/* replace zero-init items with default values */
if (ctx->desc.num_channels > SFETCH_MAX_CHANNELS) {
ctx->desc.num_channels = SFETCH_MAX_CHANNELS;
_SFETCH_WARN(CLAMPING_NUM_CHANNELS_TO_MAX_CHANNELS);
}
/* setup the global request item pool */
ctx->valid &= _sfetch_pool_init(&ctx->pool, ctx->desc.max_requests);
/* setup IO channels (one thread per channel) */
for (uint32_t i = 0; i < ctx->desc.num_channels; i++) {
ctx->valid &= _sfetch_channel_init(&ctx->chn[i], ctx, ctx->desc.max_requests, ctx->desc.num_lanes, _sfetch_request_handler);
}
}
SOKOL_API_IMPL void sfetch_shutdown(void) {
_sfetch_t* ctx = _sfetch_ctx();
SOKOL_ASSERT(ctx && ctx->setup);
ctx->valid = false;
/* IO threads must be shutdown first */
for (uint32_t i = 0; i < ctx->desc.num_channels; i++) {
if (ctx->chn[i].valid) {
_sfetch_channel_discard(&ctx->chn[i]);
}
}
_sfetch_pool_discard(&ctx->pool);
ctx->setup = false;
_sfetch_free(ctx);
_sfetch = 0;
}
SOKOL_API_IMPL bool sfetch_valid(void) {
_sfetch_t* ctx = _sfetch_ctx();
return ctx && ctx->valid;
}
SOKOL_API_IMPL sfetch_desc_t sfetch_desc(void) {
_sfetch_t* ctx = _sfetch_ctx();
SOKOL_ASSERT(ctx && ctx->valid);
return ctx->desc;
}
SOKOL_API_IMPL int sfetch_max_userdata_bytes(void) {
return SFETCH_MAX_USERDATA_UINT64 * 8;
}
SOKOL_API_IMPL int sfetch_max_path(void) {
return SFETCH_MAX_PATH;
}
SOKOL_API_IMPL bool sfetch_handle_valid(sfetch_handle_t h) {
_sfetch_t* ctx = _sfetch_ctx();
SOKOL_ASSERT(ctx && ctx->valid);
/* shortcut invalid handle */
if (h.id == 0) {
return false;
}
return 0 != _sfetch_pool_item_lookup(&ctx->pool, h.id);
}
SOKOL_API_IMPL sfetch_handle_t sfetch_send(const sfetch_request_t* request) {
_sfetch_t* ctx = _sfetch_ctx();
SOKOL_ASSERT(ctx && ctx->setup);
const sfetch_handle_t invalid_handle = _sfetch_make_handle(0);
if (!ctx->valid) {
return invalid_handle;
}
if (!_sfetch_validate_request(ctx, request)) {
return invalid_handle;
}
SOKOL_ASSERT(request->channel < ctx->desc.num_channels);
uint32_t slot_id = _sfetch_pool_item_alloc(&ctx->pool, request);
if (0 == slot_id) {
_SFETCH_WARN(REQUEST_POOL_EXHAUSTED);
return invalid_handle;
}
if (!_sfetch_channel_send(&ctx->chn[request->channel], slot_id)) {
/* send failed because the channels sent-queue overflowed */
_sfetch_pool_item_free(&ctx->pool, slot_id);
return invalid_handle;
}
return _sfetch_make_handle(slot_id);
}
SOKOL_API_IMPL void sfetch_dowork(void) {
_sfetch_t* ctx = _sfetch_ctx();
SOKOL_ASSERT(ctx && ctx->setup);
if (!ctx->valid) {
return;
}
/* we're pumping each channel 2x so that unfinished request items coming out the
IO threads can be moved back into the IO-thread immediately without
having to wait a frame
*/
ctx->in_callback = true;
for (int pass = 0; pass < 2; pass++) {
for (uint32_t chn_index = 0; chn_index < ctx->desc.num_channels; chn_index++) {
_sfetch_channel_dowork(&ctx->chn[chn_index], &ctx->pool);
}
}
ctx->in_callback = false;
}
SOKOL_API_IMPL void sfetch_bind_buffer(sfetch_handle_t h, sfetch_range_t buffer) {
_sfetch_t* ctx = _sfetch_ctx();
SOKOL_ASSERT(ctx && ctx->valid);
SOKOL_ASSERT(ctx->in_callback);
SOKOL_ASSERT(buffer.ptr && (buffer.size > 0));
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, h.id);
if (item) {
SOKOL_ASSERT((0 == item->buffer.ptr) && (0 == item->buffer.size));
item->buffer = buffer;
}
}
SOKOL_API_IMPL void* sfetch_unbind_buffer(sfetch_handle_t h) {
_sfetch_t* ctx = _sfetch_ctx();
SOKOL_ASSERT(ctx && ctx->valid);
SOKOL_ASSERT(ctx->in_callback);
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, h.id);
if (item) {
void* prev_buf_ptr = (void*)item->buffer.ptr;
item->buffer.ptr = 0;
item->buffer.size = 0;
return prev_buf_ptr;
}
else {
return 0;
}
}
SOKOL_API_IMPL void sfetch_pause(sfetch_handle_t h) {
_sfetch_t* ctx = _sfetch_ctx();
SOKOL_ASSERT(ctx && ctx->valid);
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, h.id);
if (item) {
item->user.pause = true;
item->user.cont = false;
}
}
SOKOL_API_IMPL void sfetch_continue(sfetch_handle_t h) {
_sfetch_t* ctx = _sfetch_ctx();
SOKOL_ASSERT(ctx && ctx->valid);
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, h.id);
if (item) {
item->user.cont = true;
item->user.pause = false;
}
}
SOKOL_API_IMPL void sfetch_cancel(sfetch_handle_t h) {
_sfetch_t* ctx = _sfetch_ctx();
SOKOL_ASSERT(ctx && ctx->valid);
_sfetch_item_t* item = _sfetch_pool_item_lookup(&ctx->pool, h.id);
if (item) {
item->user.cont = false;
item->user.pause = false;
item->user.cancel = true;
}
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif /* SOKOL_FETCH_IMPL */
|
0 |
repos/sokol
|
repos/sokol/tests/test_linux.sh
|
#!/usr/bin/env bash
set -e
source test_common.sh
build linux_gl_debug linux_gl_debug
build linux_gl_release linux_gl_release
build linux_gles3_debug linux_gles3_debug
build linux_gles3_release linux_gles3_release
build linux_gl_egl_debug linux_gl_egl_debug
build linux_gl_egl_release linux_gl_egl_release
runtest linux_gl_debug
|
0 |
repos/sokol
|
repos/sokol/tests/test_android.sh
|
#!/usr/bin/env bash
set -e
source test_common.sh
setup_android
build android_debug android_debug
build android_release android_release
build android_sles_debug android_sles_debug
build android_sles_release android_sles_release
|
0 |
repos/sokol
|
repos/sokol/tests/analyze_macos.sh
|
set -e
source test_common.sh
build macos_gl_analyze macos_gl_analyze
build macos_metal_analyze macos_metal_analyze
build macos_arc_gl_analyze macos_arc_gl_analyze
build macos_arc_metal_analyze macos_arc_metal_analyze
|
0 |
repos/sokol
|
repos/sokol/tests/analyze_linux.sh
|
#!/usr/bin/env bash
set -e
source test_common.sh
build linux_gl_analyze linux_gl_analyze
build linux_gles3_analyze linux_gles3_analyze
|
0 |
repos/sokol
|
repos/sokol/tests/test_macos.sh
|
set -e
source test_common.sh
build macos_gl_debug macos_gl_debug
build macos_gl_release macos_gl_release
build macos_metal_debug macos_metal_debug
build macos_metal_release macos_metal_release
build macos_arc_gl_debug macos_arc_gl_debug
build macos_arc_gl_release macos_arc_gl_release
build macos_arc_metal_debug macos_arc_metal_debug
build macos_arc_metal_release macos_arc_metal_release
runtest macos_gl_debug
|
0 |
repos/sokol
|
repos/sokol/tests/CMakeLists.txt
|
cmake_minimum_required(VERSION 3.20)
project(sokol-test)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
# SOKOL_GLCORE, SOKOL_GLES3, SOKOL_D3D11, SOKOL_METAL, SOKOL_WGPU, SOKOL_DUMMY
set(SOKOL_BACKEND "SOKOL_DUMMY_BACKEND" CACHE STRING "Select 3D backend API")
set_property(CACHE SOKOL_BACKEND PROPERTY STRINGS SOKOL_GLCORE SOKOL_METAL SOKOL_D3D11 SOKOL_DUMMY_BACKEND)
option(SOKOL_FORCE_EGL "Force EGL with GLCORE backend" OFF)
option(SOKOL_FORCE_SLES "Force SLES in sokol-audio Android backend" OFF)
option(USE_ARC "Enable/disable ARC" OFF)
option(USE_ANALYZER "Enable/disable clang analyzer" OFF)
if (CMAKE_SYSTEM_NAME STREQUAL Emscripten)
set(EMSCRIPTEN 1)
elseif (CMAKE_SYSTEM_NAME STREQUAL iOS)
set(OSX_IOS 1)
elseif (CMAKE_SYSTEM_NAME STREQUAL Android)
set(ANDROID 1)
elseif (CMAKE_SYSTEM_NAME STREQUAL Linux)
set(LINUX 1)
elseif (CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(OSX_MACOS 1)
elseif (CMAKE_SYSTEM_NAME STREQUAL Windows)
set(WINDOWS 1)
else()
message(FATAL_ERROR "Unrecognized CMAKE_SYSTEM_NAME")
endif()
message(">> CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")
message(">> SOKOL_BACKEND: ${SOKOL_BACKEND}")
message(">> SOKOL_FORCE_EGL: ${SOKOL_FORCE_EGL}")
message(">> SOKOL_FORCE_SLES: ${SOKOL_FORCE_SLES}")
if (OSX_IOS OR OSX_MACOS)
if (USE_ARC)
message(">> ObjC ARC ENABLED")
else()
message(">> ObjC ARC DISABLED")
endif()
endif()
message(">> BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
message(">> TOOLCHAIN: ${CMAKE_TOOLCHAIN_FILE}")
set(c_flags)
set(cxx_flags)
set(link_flags)
set(system_libs)
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(c_flags ${c_flags} /W4 /WX /D_CRT_SECURE_NO_WARNINGS)
set(cxx_flags ${cxx_flags} /W4 /WX /EHsc /D_CRT_SECURE_NO_WARNINGS)
else()
set(c_flags ${c_flags} -Wall -Wextra -Werror -Wsign-conversion -Wstrict-prototypes)
set(cxx_flags ${cxx_flags} -Wall -Wextra -Werror -Wsign-conversion -fno-rtti -fno-exceptions)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(c_flags ${c_flags} -Wno-missing-field-initializers)
set(cxx_flags ${cxx_flags} -Wno-missing-field-initializers)
endif()
if (USE_ANALYZER)
# FIXME: consider using clang-tidy via CMAKE_CXX_CLANG_TIDY: https://ortogonal.github.io/cmake-clang-tidy/
# with the default settings this spams the output with irrelevant C++ coding style warnings in 3rd party libs though
message(">> Configuring for static code analysis")
set(c_flags ${c_flags} --analyze -Xanalyzer -analyzer-opt-analyze-headers)
set(cxx_flags ${cxx_flags} --analyze -Xanalyzer -analyzer-opt-analyze-headers)
set(link_flags ${link_flags} --analyze -Wno-unused-command-line-argument)
endif()
endif()
if (EMSCRIPTEN)
set(CMAKE_EXECUTABLE_SUFFIX ".html")
set(link_flags ${link_flags} -sNO_FILESYSTEM=1 -sASSERTIONS=0 -sMALLOC=emmalloc -sINITIAL_MEMORY=33554432 --closure=1)
if (SOKOL_BACKEND STREQUAL SOKOL_WGPU)
set(link_flags ${link_flags} -sUSE_WEBGPU=1)
else()
set(link_flags ${link_flags} -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2)
endif()
elseif (OSX_IOS)
set(exe_type MACOSX_BUNDLE)
if (USE_ARC)
set(c_flags ${c_flags} -fobjc-arc)
set(cxx_flags ${cxx_flags} -fobjc-arc)
endif()
set(system_libs ${system_libs} "-framework Foundation" "-framework UIKit" "-framework AudioToolbox" "-framework AVFoundation")
if (SOKOL_BACKEND STREQUAL SOKOL_METAL)
set(system_libs ${system_libs} "-framework Metal" "-framework MetalKit")
else()
set(system_libs ${system_libs} "-framework OpenGLES" "-framework GLKit")
endif()
elseif (ANDROID)
if (SOKOL_FORCE_SLES)
set(system_libs ${system_libs} GLESv3 EGL OpenSLES log android)
else()
set(system_libs ${system_libs} GLESv3 EGL aaudio log android)
endif()
elseif (LINUX)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
if ((SOKOL_BACKEND STREQUAL SOKOL_GLES3) OR SOKOL_FORCE_EGL)
set(system_libs ${system_libs} X11 Xi Xcursor EGL GL asound dl m Threads::Threads)
else()
set(system_libs ${system_libs} X11 Xi Xcursor GL asound dl m Threads::Threads)
endif()
elseif (OSX_MACOS)
set(exe_type MACOSX_BUNDLE)
if (USE_ARC)
set(c_flags ${c_flags} -fobjc-arc)
set(cxx_flags ${cxx_flags} -fobjc-arc)
endif()
set(system_libs ${system_libs} "-framework QuartzCore" "-framework Cocoa" "-framework AudioToolbox")
if (SOKOL_BACKEND STREQUAL SOKOL_METAL)
set(system_libs ${system_libs} "-framework MetalKit" "-framework Metal")
else()
set(system_libs ${system_libs} "-framework OpenGL")
endif()
elseif (WINDOWS)
set(exe_type WIN32)
endif()
macro(configure_common target)
if (SOKOL_FORCE_EGL)
target_compile_definitions(${target} PRIVATE SOKOL_FORCE_EGL)
endif()
if (SOKOL_FORCE_SLES)
target_compile_definitions(${target} PRIVATE SAUDIO_ANDROID_SLES)
endif()
target_compile_definitions(${target} PRIVATE ${SOKOL_BACKEND})
target_link_options(${target} PRIVATE ${link_flags})
target_link_libraries(${target} PRIVATE ${system_libs})
target_include_directories(${target} PRIVATE ../.. ../../util)
target_include_directories(${target} PRIVATE ../ext)
endmacro()
macro(configure_osx_properties target)
if (OSX_IOS)
target_compile_definitions(${target} PRIVATE GLES_SILENCE_DEPRECATION)
endif()
set_target_properties(${target} PROPERTIES XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "${target}")
set_target_properties(${target} PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER "${target}")
set_target_properties(${target} PROPERTIES MACOSX_BUNDLE_PRODUCT_NAME "${target}")
set_target_properties(${target} PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "${target}")
endmacro()
macro(configure_c target)
configure_common(${target})
target_compile_options(${target} PRIVATE ${c_flags})
if (OSX_MACOS OR OSX_IOS)
target_compile_options(${target} PRIVATE -x objective-c)
configure_osx_properties(${target})
endif()
endmacro()
macro(configure_cxx target)
configure_common(${target})
target_compile_options(${target} PRIVATE ${cxx_flags})
if (OSX_MACOS OR OSX_IOS)
target_compile_options(${target} PRIVATE -x objective-c++)
configure_osx_properties(${target})
endif()
endmacro()
add_subdirectory(ext)
add_subdirectory(compile)
add_subdirectory(functional)
|
0 |
repos/sokol
|
repos/sokol/tests/test_common.sh
|
setup_emsdk() {
if [ ! -d "build/emsdk" ] ; then
mkdir -p build && cd build
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
cd ../..
fi
source build/emsdk/emsdk_env.sh
}
setup_android() {
if [ ! -d "build/android_sdk" ] ; then
mkdir -p build/android_sdk && cd build/android_sdk
sdk_file="sdk-tools-linux-3859397.zip"
wget --no-verbose https://dl.google.com/android/repository/$sdk_file
unzip -q $sdk_file
cd tools/bin
yes | ./sdkmanager "platforms;android-28" >/dev/null
yes | ./sdkmanager "build-tools;29.0.3" >/dev/null
yes | ./sdkmanager "platform-tools" >/dev/null
yes | ./sdkmanager "ndk-bundle" >/dev/null
cd ../../../..
fi
}
build() {
gen_preset=$1
build_preset=$2
cmake --preset $gen_preset
cmake --build --preset $build_preset
}
analyze() {
cfg=$1
backend=$2
mode=$3
mkdir -p build/$cfg && cd build/$cfg
cmake -GNinja -DSOKOL_BACKEND=$backend -DCMAKE_BUILD_TYPE=$mode -DUSE_ANALYZER=ON -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ ../..
cmake --build .
cd ../..
}
runtest() {
cfg=$1
cd build/$cfg
./sokol-test
cd ../../..
}
|
0 |
repos/sokol
|
repos/sokol/tests/CMakePresets.json
|
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 21,
"patch": 0
},
"configurePresets": [
{
"name": "macos_gl_debug",
"generator": "Ninja",
"binaryDir": "build/macos_gl_debug",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "macos_gl_release",
"generator": "Ninja",
"binaryDir": "build/macos_gl_release",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "macos_gl_analyze",
"generator": "Ninja",
"binaryDir": "build/macos_gl_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"CMAKE_BUILD_TYPE": "Debug",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "macos_metal_debug",
"generator": "Ninja",
"binaryDir": "build/macos_metal_debug",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_METAL",
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "macos_metal_release",
"generator": "Ninja",
"binaryDir": "build/macos_metal_release",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_METAL",
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "macos_metal_analyze",
"generator": "Ninja",
"binaryDir": "build/macos_metal_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_METAL",
"CMAKE_BUILD_TYPE": "Debug",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "macos_arc_gl_debug",
"generator": "Ninja",
"binaryDir": "build/macos_arc_gl_debug",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"USE_ARC": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "macos_arc_gl_release",
"generator": "Ninja",
"binaryDir": "build/macos_arc_gl_release",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"USE_ARC": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "macos_arc_gl_analyze",
"generator": "Ninja",
"binaryDir": "build/macos_arc_gl_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"USE_ARC": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": "Debug",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "macos_arc_metal_debug",
"generator": "Ninja",
"binaryDir": "build/macos_arc_metal_debug",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_METAL",
"USE_ARC": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "macos_arc_metal_release",
"generator": "Ninja",
"binaryDir": "build/macos_arc_metal_release",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_METAL",
"USE_ARC": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "macos_arc_metal_analyze",
"generator": "Ninja",
"binaryDir": "build/macos_arc_metal_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_METAL",
"USE_ARC": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": "Debug",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "ios_gl",
"generator": "Xcode",
"binaryDir": "build/ios_gl",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"CMAKE_SYSTEM_NAME": "iOS"
}
},
{
"name": "ios_gl_analyze",
"generator": "Ninja",
"binaryDir": "build/ios_gl_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_SYSTEM_NAME": "iOS",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "ios_metal",
"generator": "Xcode",
"binaryDir": "build/ios_metal",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_METAL",
"CMAKE_SYSTEM_NAME": "iOS"
}
},
{
"name": "ios_metal_analyze",
"generator": "Ninja",
"binaryDir": "build/ios_metal_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_METAL",
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_SYSTEM_NAME": "iOS",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "ios_arc_gl",
"generator": "Xcode",
"binaryDir": "build/ios_arc_gl",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"USE_ARC": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_SYSTEM_NAME": "iOS"
}
},
{
"name": "ios_arc_gl_analyze",
"generator": "Ninja",
"binaryDir": "build/ios_arc_gl_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"USE_ARC": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_SYSTEM_NAME": "iOS",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "ios_arc_metal",
"generator": "Xcode",
"binaryDir": "build/ios_arc_metal",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_METAL",
"USE_ARC": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_SYSTEM_NAME": "iOS"
}
},
{
"name": "ios_arc_metal_analyze",
"generator": "Ninja",
"binaryDir": "build/ios_arc_metal_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_METAL",
"USE_ARC": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_SYSTEM_NAME": "iOS",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "linux_gl_debug",
"generator": "Ninja",
"binaryDir": "build/linux_gl_debug",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "linux_gl_release",
"generator": "Ninja",
"binaryDir": "build/linux_gl_release",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "linux_gl_analyze",
"generator": "Ninja",
"binaryDir": "build/linux_gl_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"CMAKE_BUILD_TYPE": "Debug",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "linux_gles3_debug",
"generator": "Ninja",
"binaryDir": "build/linux_gles3_debug",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "linux_gles3_release",
"generator": "Ninja",
"binaryDir": "build/linux_gles3_release",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "linux_gles3_analyze",
"generator": "Ninja",
"binaryDir": "build/linux_gles3_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"CMAKE_BUILD_TYPE": "Debug",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "linux_gl_egl_debug",
"generator": "Ninja",
"binaryDir": "build/linux_gl_egl_debug",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"SOKOL_FORCE_EGL": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "linux_gl_egl_release",
"generator": "Ninja",
"binaryDir": "build/linux_gl_egl_release",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"SOKOL_FORCE_EGL": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "emsc_webgl2_debug",
"generator": "Ninja",
"binaryDir": "build/emsc_webgl2_debug",
"toolchainFile": "build/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "emsc_webgl2_release",
"generator": "Ninja",
"binaryDir": "build/emsc_webgl2_release",
"toolchainFile": "build/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "emsc_wgpu_debug",
"generator": "Ninja",
"binaryDir": "build/emsc_wgpu_debug",
"toolchainFile": "build/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_WGPU",
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "emsc_wgpu_release",
"generator": "Ninja",
"binaryDir": "build/emsc_wgpu_release",
"toolchainFile": "build/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_WGPU",
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "android_debug",
"generator": "Ninja",
"binaryDir": "build/android_debug",
"toolchainFile": "build/android_sdk/ndk-bundle/build/cmake/android.toolchain.cmake",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"ANDROID_ABI": "armeabi-v7a",
"ANDROID_PLATFORM": "android-28",
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "android_release",
"generator": "Ninja",
"binaryDir": "build/android_release",
"toolchainFile": "build/android_sdk/ndk-bundle/build/cmake/android.toolchain.cmake",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"ANDROID_ABI": "armeabi-v7a",
"ANDROID_PLATFORM": "android-28",
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "android_sles_debug",
"generator": "Ninja",
"binaryDir": "build/android_sles_debug",
"toolchainFile": "build/android_sdk/ndk-bundle/build/cmake/android.toolchain.cmake",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"ANDROID_ABI": "armeabi-v7a",
"ANDROID_PLATFORM": "android-28",
"CMAKE_BUILD_TYPE": "Debug",
"SOKOL_FORCE_SLES": {
"type": "BOOL",
"value": "ON"
}
}
},
{
"name": "android_sles_release",
"generator": "Ninja",
"binaryDir": "build/android_sles_release",
"toolchainFile": "build/android_sdk/ndk-bundle/build/cmake/android.toolchain.cmake",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLES3",
"ANDROID_ABI": "armeabi-v7a",
"ANDROID_PLATFORM": "android-28",
"CMAKE_BUILD_TYPE": "Release",
"SOKOL_FORCE_SLES": {
"type": "BOOL",
"value": "ON"
}
}
},
{
"name": "win_gl",
"binaryDir": "build/win_gl",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE"
}
},
{
"name": "win_gl_analyze",
"generator": "Ninja",
"binaryDir": "build/win_gl_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_GLCORE",
"CMAKE_BUILD_TYPE": "Debug",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "win_d3d11",
"binaryDir": "build/win_d3d11",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_D3D11"
}
},
{
"name": "win_d3d11_analyze",
"generator": "Ninja",
"binaryDir": "build/win_d3d11_analyze",
"cacheVariables": {
"SOKOL_BACKEND": "SOKOL_D3D11",
"CMAKE_BUILD_TYPE": "Debug",
"USE_ANALYZER": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
}
],
"buildPresets": [
{
"name": "macos_gl_debug",
"configurePreset": "macos_gl_debug"
},
{
"name": "macos_gl_release",
"configurePreset": "macos_gl_release"
},
{
"name": "macos_gl_analyze",
"configurePreset": "macos_gl_analyze"
},
{
"name": "macos_metal_debug",
"configurePreset": "macos_metal_debug"
},
{
"name": "macos_metal_release",
"configurePreset": "macos_metal_release"
},
{
"name": "macos_metal_analyze",
"configurePreset": "macos_metal_analyze"
},
{
"name": "macos_arc_gl_debug",
"configurePreset": "macos_arc_gl_debug"
},
{
"name": "macos_arc_gl_release",
"configurePreset": "macos_arc_gl_release"
},
{
"name": "macos_arc_gl_analyze",
"configurePreset": "macos_arc_gl_analyze"
},
{
"name": "macos_arc_metal_debug",
"configurePreset": "macos_arc_metal_debug"
},
{
"name": "macos_arc_metal_release",
"configurePreset": "macos_arc_metal_release"
},
{
"name": "macos_arc_metal_analyze",
"configurePreset": "macos_arc_metal_analyze"
},
{
"name": "ios_gl_debug",
"configurePreset": "ios_gl",
"configuration": "Debug",
"nativeToolOptions": [ "CODE_SIGN_IDENTITY=\"\"", "CODE_SIGNING_REQUIRED=NO", "CODE_SIGNING_ALLOWED=NO" ]
},
{
"name": "ios_gl_release",
"configurePreset": "ios_gl",
"configuration": "Release",
"nativeToolOptions": [ "CODE_SIGN_IDENTITY=\"\"", "CODE_SIGNING_REQUIRED=NO", "CODE_SIGNING_ALLOWED=NO" ]
},
{
"name": "ios_gl_analyze",
"configurePreset": "ios_gl_analyze"
},
{
"name": "ios_metal_debug",
"configurePreset": "ios_metal",
"configuration": "Debug",
"nativeToolOptions": [ "CODE_SIGN_IDENTITY=\"\"", "CODE_SIGNING_REQUIRED=NO", "CODE_SIGNING_ALLOWED=NO" ]
},
{
"name": "ios_metal_release",
"configurePreset": "ios_metal",
"configuration": "Release",
"nativeToolOptions": [ "CODE_SIGN_IDENTITY=\"\"", "CODE_SIGNING_REQUIRED=NO", "CODE_SIGNING_ALLOWED=NO" ]
},
{
"name": "ios_metal_analyze",
"configurePreset": "ios_metal_analyze"
},
{
"name": "ios_arc_gl_debug",
"configurePreset": "ios_arc_gl",
"configuration": "Debug",
"nativeToolOptions": [ "CODE_SIGN_IDENTITY=\"\"", "CODE_SIGNING_REQUIRED=NO", "CODE_SIGNING_ALLOWED=NO" ]
},
{
"name": "ios_arc_gl_release",
"configurePreset": "ios_arc_gl",
"configuration": "Release",
"nativeToolOptions": [ "CODE_SIGN_IDENTITY=\"\"", "CODE_SIGNING_REQUIRED=NO", "CODE_SIGNING_ALLOWED=NO" ]
},
{
"name": "ios_arc_gl_analyze",
"configurePreset": "ios_arc_gl_analyze"
},
{
"name": "ios_arc_metal_debug",
"configurePreset": "ios_arc_metal",
"configuration": "Debug",
"nativeToolOptions": [ "CODE_SIGN_IDENTITY=\"\"", "CODE_SIGNING_REQUIRED=NO", "CODE_SIGNING_ALLOWED=NO" ]
},
{
"name": "ios_arc_metal_release",
"configurePreset": "ios_arc_metal",
"configuration": "Release",
"nativeToolOptions": [ "CODE_SIGN_IDENTITY=\"\"", "CODE_SIGNING_REQUIRED=NO", "CODE_SIGNING_ALLOWED=NO" ]
},
{
"name": "ios_arc_metal_analyze",
"configurePreset": "ios_arc_metal_analyze"
},
{
"name": "linux_gl_debug",
"configurePreset": "linux_gl_debug"
},
{
"name": "linux_gl_release",
"configurePreset": "linux_gl_release"
},
{
"name": "linux_gl_analyze",
"configurePreset": "linux_gl_analyze"
},
{
"name": "linux_gles3_debug",
"configurePreset": "linux_gles3_debug"
},
{
"name": "linux_gles3_release",
"configurePreset": "linux_gles3_release"
},
{
"name": "linux_gles3_analyze",
"configurePreset": "linux_gles3_analyze"
},
{
"name": "linux_gl_egl_debug",
"configurePreset": "linux_gl_egl_debug"
},
{
"name": "linux_gl_egl_release",
"configurePreset": "linux_gl_egl_release"
},
{
"name": "emsc_webgl2_debug",
"configurePreset": "emsc_webgl2_debug"
},
{
"name": "emsc_webgl2_release",
"configurePreset": "emsc_webgl2_release"
},
{
"name": "emsc_wgpu_debug",
"configurePreset": "emsc_wgpu_debug"
},
{
"name": "emsc_wgpu_release",
"configurePreset": "emsc_wgpu_release"
},
{
"name": "android_debug",
"configurePreset": "android_debug"
},
{
"name": "android_release",
"configurePreset": "android_release"
},
{
"name": "android_sles_debug",
"configurePreset": "android_sles_debug"
},
{
"name": "android_sles_release",
"configurePreset": "android_sles_release"
},
{
"name": "win_gl_debug",
"configurePreset": "win_gl",
"configuration": "Debug"
},
{
"name": "win_gl_release",
"configurePreset": "win_gl",
"configuration": "Release"
},
{
"name": "win_gl_analyze",
"configurePreset": "win_gl_analyze"
},
{
"name": "win_d3d11_debug",
"configurePreset": "win_d3d11",
"configuration": "Debug"
},
{
"name": "win_d3d11_release",
"configurePreset": "win_d3d11",
"configuration": "Release"
},
{
"name": "win_d3d11_analyze",
"configurePreset": "win_d3d11_analyze"
}
]
}
|
0 |
repos/sokol
|
repos/sokol/tests/test_emscripten.sh
|
#!/usr/bin/env bash
set -e
source test_common.sh
setup_emsdk
build emsc_webgl2_debug emsc_webgl2_debug
build emsc_webgl2_release emsc_webgl2_release
build emsc_wgpu_debug emsc_wgpu_debug
build emsc_wgpu_release emsc_wgpu_release
|
0 |
repos/sokol
|
repos/sokol/tests/analyze_ios.sh
|
set -e
source test_common.sh
build ios_gl_analyze ios_gl_analyze
build ios_metal_analyze ios_metal_analyze
build ios_arc_gl_analyze ios_arc_gl_analyze
build ios_arc_metal_analyze ios_arc_metal_analyze
|
0 |
repos/sokol
|
repos/sokol/tests/test_ios.sh
|
set -e
source test_common.sh
build ios_gl ios_gl_debug
build ios_gl ios_gl_release
build ios_metal ios_metal_debug
build ios_metal ios_metal_release
build ios_arc_gl ios_arc_gl_debug
build ios_arc_gl ios_arc_gl_release
build ios_arc_metal ios_arc_metal_debug
build ios_arc_metal ios_arc_metal_release
|
0 |
repos/sokol/tests
|
repos/sokol/tests/compile/sokol_fetch.c
|
#define SOKOL_IMPL
#include "sokol_fetch.h"
void use_fetch_impl(void) {
sfetch_setup(&(sfetch_desc_t){0});
}
|
0 |
repos/sokol/tests
|
repos/sokol/tests/compile/sokol_debugtext.c
|
#include "sokol_gfx.h"
#define SOKOL_IMPL
#include "sokol_debugtext.h"
void use_debugtext_impl(void) {
sdtx_setup(&(sdtx_desc_t){0});
}
|
0 |
repos/sokol/tests
|
repos/sokol/tests/compile/sokol_time.c
|
#define SOKOL_IMPL
#include "sokol_time.h"
void use_time_impl(void) {
stm_setup();
}
|
0 |
repos/sokol/tests
|
repos/sokol/tests/compile/sokol_shape.c
|
#include "sokol_gfx.h"
#define SOKOL_IMPL
#include "sokol_shape.h"
void use_shape_impl(void) {
sshape_plane_sizes(10);
}
|
0 |
repos/sokol/tests
|
repos/sokol/tests/compile/sokol_glue.c
|
#include "sokol_app.h"
#include "sokol_gfx.h"
#define SOKOL_IMPL
#include "sokol_glue.h"
void use_glue_impl(void) {
const sg_environment env = sglue_environment();
(void)env;
}
|
0 |
repos/sokol/tests
|
repos/sokol/tests/compile/sokol_fontstash.c
|
#include "sokol_gfx.h"
#include "sokol_gl.h"
#define FONTSTASH_IMPLEMENTATION
#if defined(_MSC_VER )
#pragma warning(disable:4996) // strncpy use in fontstash.h
#endif
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
#include <stdlib.h> // malloc/free
#include "fontstash.h"
#define SOKOL_IMPL
#include "sokol_fontstash.h"
void use_fontstash_impl(void) {
FONScontext* ctx = sfons_create(&(sfons_desc_t){ 0 });
sfons_destroy(ctx);
}
|
0 |
repos/sokol/tests
|
repos/sokol/tests/compile/sokol_args.c
|
#define SOKOL_IMPL
#include "sokol_args.h"
void use_args_impl(void) {
sargs_setup(&(sargs_desc){0});
}
|
0 |
repos/sokol/tests
|
repos/sokol/tests/compile/sokol_log.c
|
#define SOKOL_IMPL
#include "sokol_log.h"
void use_sokol_log(void) {
slog_func("bla", 1, 123, "123", 42, "bla.c", 0);
}
|
0 |
repos/sokol/tests
|
repos/sokol/tests/compile/sokol_imgui.c
|
#include "sokol_app.h"
#include "sokol_gfx.h"
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#if defined(_MSC_VER )
#pragma warning(disable:4201) // nonstandard extension used: nameless struct/union
#pragma warning(disable:4214) // nonstandard extension used: bit field types other than int
#endif
#include "cimgui/cimgui.h"
#define SOKOL_IMPL
#if defined(SOKOL_DUMMY_BACKEND)
#define SOKOL_IMGUI_NO_SOKOL_APP
#endif
#include "sokol_imgui.h"
void use_imgui_impl(void) {
simgui_setup(&(simgui_desc_t){0});
}
|
0 |
repos/sokol/tests
|
repos/sokol/tests/compile/sokol_nuklear.c
|
#include "sokol_app.h"
#include "sokol_gfx.h"
// include nuklear.h before the sokol_nuklear.h implementation
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_STANDARD_VARARGS
#include "nuklear.h"
#define SOKOL_IMPL
#if defined(SOKOL_DUMMY_BACKEND)
#define SOKOL_NUKLEAR_NO_SOKOL_APP
#endif
#include "sokol_nuklear.h"
void use_nuklear_impl(void) {
snk_setup(&(snk_desc_t){0});
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.