learn-wgpu/showcase/alignment/index.html

29 lines
14 KiB
HTML
Raw Normal View History

2020-11-24 06:49:43 +00:00
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Memory Layout in GLSL | Learn Wgpu</title>
<meta name="description" content="">
<meta name="generator" content="VuePress 1.4.0">
<meta property="article:modified_time" content="2020-11-21T20:16:07.000Z"><meta property="og:site_name" content="Learn Wgpu"><meta property="og:title" content="Memory Layout in GLSL"><meta property="og:type" content="website"><meta property="og:url" content="/showcase/alignment/"><meta name="twitter:title" content="Memory Layout in GLSL"><meta name="twitter:url" content="/showcase/alignment/"><meta name="twitter:card" content="summary_large_image"><meta name="twitter:label1" content="Written by"><meta name="twitter:data2" content="Benjamin R Hansen"><meta name="twitter:creator" content="https://twitter.com/sotrh760">
<link rel="preload" href="/learn-wgpu/assets/css/0.styles.7acc6ef6.css" as="style"><link rel="preload" href="/learn-wgpu/assets/js/app.10252059.js" as="script"><link rel="preload" href="/learn-wgpu/assets/js/2.6a9f728b.js" as="script"><link rel="preload" href="/learn-wgpu/assets/js/28.b9bf1b57.js" as="script"><link rel="prefetch" href="/learn-wgpu/assets/js/10.de8cadfb.js"><link rel="prefetch" href="/learn-wgpu/assets/js/11.53581734.js"><link rel="prefetch" href="/learn-wgpu/assets/js/12.8c46a0c8.js"><link rel="prefetch" href="/learn-wgpu/assets/js/13.4723ba5d.js"><link rel="prefetch" href="/learn-wgpu/assets/js/14.90ed5917.js"><link rel="prefetch" href="/learn-wgpu/assets/js/15.3056a06c.js"><link rel="prefetch" href="/learn-wgpu/assets/js/16.0af06f1e.js"><link rel="prefetch" href="/learn-wgpu/assets/js/17.1d99e854.js"><link rel="prefetch" href="/learn-wgpu/assets/js/18.9923f29d.js"><link rel="prefetch" href="/learn-wgpu/assets/js/19.dab68ffd.js"><link rel="prefetch" href="/learn-wgpu/assets/js/20.70e640cc.js"><link rel="prefetch" href="/learn-wgpu/assets/js/21.1dba0934.js"><link rel="prefetch" href="/learn-wgpu/assets/js/22.2132e362.js"><link rel="prefetch" href="/learn-wgpu/assets/js/23.1a88f225.js"><link rel="prefetch" href="/learn-wgpu/assets/js/24.585372be.js"><link rel="prefetch" href="/learn-wgpu/assets/js/25.022f80fd.js"><link rel="prefetch" href="/learn-wgpu/assets/js/26.8484ba66.js"><link rel="prefetch" href="/learn-wgpu/assets/js/27.8b0a41bf.js"><link rel="prefetch" href="/learn-wgpu/assets/js/29.5c3f77ba.js"><link rel="prefetch" href="/learn-wgpu/assets/js/3.df177b88.js"><link rel="prefetch" href="/learn-wgpu/assets/js/30.dc887be9.js"><link rel="prefetch" href="/learn-wgpu/assets/js/4.89cbdecc.js"><link rel="prefetch" href="/learn-wgpu/assets/js/5.c2eff02b.js"><link rel="prefetch" href="/learn-wgpu/assets/js/6.086069e3.js"><link rel="prefetch" href="/learn-wgpu/assets/js/7.65364b9b.js"><link rel="prefetch" href="/learn-wgpu/assets/js/8.1fd4cc1c.js"><link rel="prefetch" href="/learn-wgpu/assets/js/9.ecb6e79c.js">
<link rel="stylesheet" href="/learn-wgpu/assets/css/0.styles.7acc6ef6.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="inner"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/learn-wgpu/" class="home-link router-link-active"><!----> <span class="site-name">Learn Wgpu</span></a> <div class="links"><!----> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div></div></header> <div class="sidebar-mask"></div> <div class="docs-layout"><aside class="sidebar"><!----> <ul class="sidebar-links"><li><a href="/learn-wgpu/" class="sidebar-link">Introduction</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Beginner</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/learn-wgpu/beginner/tutorial1-window/" class="sidebar-link">Dependencies and the window</a></li><li><a href="/learn-wgpu/beginner/tutorial2-swapchain/" class="sidebar-link">The Swapchain</a></li><li><a href="/learn-wgpu/beginner/tutorial3-pipeline/" class="sidebar-link">The Pipeline</a></li><li><a href="/learn-wgpu/beginner/tutorial4-buffer/" class="sidebar-link">Buffers and Indices</a></li><li><a href="/learn-wgpu/beginner/tutorial5-textures/" class="sidebar-link">Textures and bind groups</a></li><li><a href="/learn-wgpu/beginner/tutorial6-uniforms/" class="sidebar-link">Uniform buffers and a 3d camera</a></li><li><a href="/learn-wgpu/beginner/tutorial7-instancing/" class="sidebar-link">Instancing</a></li><li><a href="/learn-wgpu/beginner/tutorial8-depth/" class="sidebar-link">The Depth Buffer</a></li><li><a href="/learn-wgpu/beginner/tutorial9-models/" class="sidebar-link">Model Loading</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Intermediate</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/learn-wgpu/intermediate/tutorial10-lighting/" class="sidebar-link">Working with Lights</a></li><li><a href="/learn-wgpu/intermediate/tutorial11-normals/" class="sidebar-link">Normal Mapping</a></li><li><a href="/learn-wgpu/intermediate/tutorial12-camera/" class="sidebar-link">A Better Camera</a></li><li><a href="/learn-wgpu/intermediate/tutorial13-threading/" class="sidebar-link">Multi-threading with Wgpu and Rayon</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Showcase</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/learn-wgpu/showcase/" class="sidebar-link">Foreward</a></li><li><a href="/learn-wgpu/showcase/windowless/" class="sidebar-link">Wgpu without a window</a></li><li><a href="/learn-wgpu/showcase/gifs/" class="sidebar-link">Creating gifs</a></li><li><a href="/learn-wgpu/showcase/pong/" class="sidebar-link">Pong</a></li><li><a href="/learn-wgpu/showcase/compute/" class="sidebar-link">Compute Example: Tangents and Bitangents</a></li><li><a href="/learn-wgpu/showcase/alignment/" class="active sidebar-link">Memory Layout in GLSL</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/learn-wgpu/showcase/alignment/#alignments" class="sidebar-link">Alignments</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/showcase/alignment/#in-wgpu" class="sidebar-link">In WGPU</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/showcase/alignment/#std140-since-glsl-1-4-opengl-3-1" class="sidebar-link">std140 (since GLSL 1.4, OpenGL 3.1)</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/showcase/alignment/#std430-since-glsl-4-3-opengl-
<em>Uniform structs</em> have to be std140.
<em>Storage structs</em> have to be std430.</p> <p><em>Storage buffers</em> for compute shaders can be std140 or std430.</p> <h2 id="std140-since-glsl-1-4-opengl-3-1"><a href="#std140-since-glsl-1-4-opengl-3-1" class="header-anchor">#</a> std140 (since GLSL 1.4, OpenGL 3.1)</h2> <p>Array:</p> <ul><li>determine the alignment of the member type and lets name it <code>align_member</code></li> <li>the alignment of the array is maximum(<code>align_member</code>, 16)</li></ul> <p>Struct:</p> <ul><li>determine the member with the largest alignment and lets name it <code>max_member_align</code></li> <li>the alignment of the array is maximum(<code>max_member_align</code>, 16)</li></ul> <h3 id="crates-to-make-your-struct-compatible-with-std140"><a href="#crates-to-make-your-struct-compatible-with-std140" class="header-anchor">#</a> crates to make your struct compatible with std140</h3> <p><a href="https://github.com/rustgd/glsl-layout" target="_blank" rel="noopener noreferrer">glsl_layout<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> <a href="https://github.com/LPGhatguy/crevice" target="_blank" rel="noopener noreferrer">crevice<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h2 id="std430-since-glsl-4-3-opengl-4-3"><a href="#std430-since-glsl-4-3-opengl-4-3" class="header-anchor">#</a> std430 (since GLSL 4.3, OpenGL 4.3)</h2> <p>more space efficient for structs and arrays</p> <p>Array</p> <ul><li>alignment is the alignment of the element type</li></ul> <p>Struct</p> <ul><li>alignment is the alignment of the member with the largest alignment</li></ul> <h3 id="crates-to-make-yuor-struct-compatible-with-std430"><a href="#crates-to-make-yuor-struct-compatible-with-std430" class="header-anchor">#</a> crates to make yuor struct compatible with std430</h3> <p><a href="https://github.com/rustgd/glsl-layout/issues/9" target="_blank" rel="noopener noreferrer">glsl_layout issue<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> <a href="https://github.com/LPGhatguy/crevice/issues/1" target="_blank" rel="noopener noreferrer">crevice issue<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <hr></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">11/21/2020, 1:16:07 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
<a href="/learn-wgpu/showcase/compute/" class="prev">
Compute Example: Tangents and Bitangents
</a></span> <span class="next"><a href="/learn-wgpu/showcase/imgui-demo/">
Basic Imgui Demo
</a>
</span></p></div> </main></div></div><div class="global-ui"><!----></div></div>
<script src="/learn-wgpu/assets/js/app.10252059.js" defer></script><script src="/learn-wgpu/assets/js/2.6a9f728b.js" defer></script><script src="/learn-wgpu/assets/js/28.b9bf1b57.js" defer></script>
</body>
</html>