Initial commit of all the NXT integration.
More like squashed history, contributors were: - Kai Ninomiya - Corentin Wallez
This commit is contained in:
commit
f07e3bd4c9
|
@ -0,0 +1,27 @@
|
|||
[submodule "external/glfw"]
|
||||
path = third_party/glfw
|
||||
url = https://github.com/glfw/glfw.git
|
||||
[submodule "external/googletest"]
|
||||
path = third_party/googletest
|
||||
url = https://github.com/google/googletest.git
|
||||
[submodule "external/glslang"]
|
||||
path = third_party/glslang
|
||||
url = https://github.com/google/glslang.git
|
||||
[submodule "external/shaderc"]
|
||||
path = third_party/shaderc
|
||||
url = https://github.com/google/shaderc.git
|
||||
[submodule "external/spirv-tools"]
|
||||
path = third_party/spirv-tools
|
||||
url = https://github.com/KhronosGroup/SPIRV-Tools.git
|
||||
[submodule "external/spirv-headers"]
|
||||
path = third_party/spirv-headers
|
||||
url = https://github.com/KhronosGroup/SPIRV-Headers.git
|
||||
[submodule "external/spirv-cross"]
|
||||
path = third_party/spirv-cross
|
||||
url = https://github.com/KhronosGroup/SPIRV-Cross.git
|
||||
[submodule "external/stb"]
|
||||
path = third_party/stb
|
||||
url = https://github.com/nothings/stb.git
|
||||
[submodule "external/glm"]
|
||||
path = third_party/glm
|
||||
url = https://github.com/g-truc/glm.git
|
|
@ -0,0 +1,6 @@
|
|||
# This is the list of NXT authors for copyright purposes.
|
||||
#
|
||||
# This does not necessarily list everyone who has contributed code, since in
|
||||
# some cases, their employer may be the copyright holder. To see the full list
|
||||
# of contributors, see the revision history in source control.
|
||||
Google Inc.
|
|
@ -0,0 +1,75 @@
|
|||
# Copyright 2017 The NXT Authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
project(nxt C CXX)
|
||||
|
||||
function(SetCXX14 Target)
|
||||
if(MSVC)
|
||||
set_property(TARGET ${Target} APPEND PROPERTY COMPILE_OPTIONS "/std:c++14")
|
||||
else()
|
||||
set_property(TARGET ${Target} APPEND PROPERTY COMPILE_OPTIONS "-std=c++14")
|
||||
endif()
|
||||
endfunction()
|
||||
function(SetPIC Target)
|
||||
if(MSVC)
|
||||
else()
|
||||
set_property(TARGET ${Target} APPEND PROPERTY COMPILE_OPTIONS "-fPIC")
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
add_subdirectory(third_party)
|
||||
add_subdirectory(generator)
|
||||
|
||||
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/include)
|
||||
|
||||
Generate(
|
||||
LIB_NAME nxt
|
||||
LIB_TYPE SHARED
|
||||
PRINT_NAME libNXT
|
||||
COMMAND_LINE_ARGS
|
||||
${GENERATOR_COMMON_ARGS}
|
||||
-T nxt
|
||||
)
|
||||
target_include_directories(nxt PUBLIC ${GENERATED_DIR})
|
||||
|
||||
Generate(
|
||||
LIB_NAME nxtcpp
|
||||
LIB_TYPE SHARED
|
||||
PRINT_NAME libNXT++
|
||||
COMMAND_LINE_ARGS
|
||||
${GENERATOR_COMMON_ARGS}
|
||||
-T nxtcpp
|
||||
)
|
||||
target_include_directories(nxtcpp PUBLIC ${GENERATED_DIR} PUBLIC ${INCLUDE_DIR})
|
||||
target_link_libraries(nxtcpp nxt)
|
||||
SetCXX14(nxtcpp)
|
||||
|
||||
Generate(
|
||||
LIB_NAME mock_nxt
|
||||
LIB_TYPE SHARED
|
||||
PRINT_NAME libMockNXT
|
||||
COMMAND_LINE_ARGS
|
||||
${GENERATOR_COMMON_ARGS}
|
||||
-T mock_nxt
|
||||
)
|
||||
target_include_directories(mock_nxt PUBLIC ${GENERATED_DIR})
|
||||
target_link_libraries(mock_nxt nxt gtest)
|
||||
SetCXX14(mock_nxt)
|
||||
|
||||
add_subdirectory(src/backend)
|
||||
add_subdirectory(src/wire)
|
||||
add_subdirectory(src/tests)
|
||||
|
||||
add_subdirectory(examples)
|
|
@ -0,0 +1,24 @@
|
|||
# How to contribute
|
||||
|
||||
We'd love to accept your patches and contributions to this project. There are
|
||||
just a few small guidelines you need to follow.
|
||||
|
||||
## Contributor License Agreement
|
||||
|
||||
Contributions to this project must be accompanied by a Contributor License
|
||||
Agreement. You (or your employer) retain the copyright to your contribution,
|
||||
this simply gives us permission to use and redistribute your contributions as
|
||||
part of the project. Head over to <https://cla.developers.google.com/> to see
|
||||
your current agreements on file or to sign a new one.
|
||||
|
||||
You generally only need to submit a CLA once, so if you've already submitted one
|
||||
(even if it was for a different project), you probably don't need to do it
|
||||
again.
|
||||
|
||||
## Code reviews
|
||||
|
||||
All submissions, including submissions by project members, require review. We
|
||||
use GitHub pull requests for this purpose. Consult
|
||||
[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
|
||||
information on using pull requests.
|
||||
|
|
@ -0,0 +1,202 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
|
@ -0,0 +1,121 @@
|
|||
# NXT, standalone part
|
||||
|
||||
NXT is an unoriginal name for Chromium’s investigation and prototyping of a NeXT generation graphics API for the Web. This repository contains a native library implementing NXT on multiple backends, as well as some code generators used for the integration in Chromium. NXT is not an official Google product.
|
||||
|
||||
We focused on efforts on two axis:
|
||||
|
||||
- An investigation of the constraints coming from the Web and in particular portability, for which we looked at the intersection of the designs of D3D12, Metal, Vulkan, OpenGL and D3D11. See links to some of our investigations below.
|
||||
- A prototype API inspired by all of D3D12, Metal and Vulkan, but none in particular. The API works on two backends: OpenGL and Metal and is usable from native code (think WebAssembly) and from Javascript inside of Chrome. Our focus was not to have a complete API but to show the breadth of potential usage.
|
||||
|
||||
We’re making our investigation and prototype public to provide another example for the upcoming discussion in the “GPU for the Web” W3C community group.
|
||||
|
||||
NXT currently has the following features:
|
||||
|
||||
- Command buffers, graphics and compute pipelines
|
||||
- Textures, samplers, vertex / index / uniform / storage buffers.
|
||||
- Descriptor sets (called bind groups) and push constants
|
||||
- SPIRV for the shading language
|
||||
- Validation
|
||||
|
||||
NXT is missing a lot of things to be usable for anything else than prototyping:
|
||||
|
||||
- Render-targets / render passes
|
||||
- Most of the fixed function pipeline state
|
||||
- Barriers / resource transitions and GPU - CPU synchronization
|
||||
- Buffer mapping
|
||||
- ...
|
||||
|
||||
We chose to use SPIRV in our prototype because it was the only language that had translators to other shading languages, thanks to SPIRV-Cross which saved us a ton of work. [SPIRV-Cross](https://github.com/KhronosGroup/SPIRV-Cross) doesn’t have an HLSL backend so we didn’t attempt a D3D12 backend (and D3D11 was too limiting for our prototype API). Only the Metal and the OpenGL backends are functional at this time. The OpenGL backend let us integrate in Chromium very easily.
|
||||
|
||||
## Links
|
||||
|
||||
Some of the investigations we made on the design of potential backend APIs:
|
||||
|
||||
- [Binding model investigation](https://drive.google.com/open?id=1_xeTnk6DlN7YmePQQAlnHndA043rgwBzUYtatk5y7kQ)
|
||||
- [Data uploads investigation](https://drive.google.com/open?id=1Mi9l14zG8HzJ5Z6107SdPhON0mq4d-3SUI8iS631nek)
|
||||
- [Resource creation investigation](https://drive.google.com/open?id=1hK1SkTFkXJXPjyla0EEl1fOIwJSc6T41AV2mGiovyFU)
|
||||
- [Vertex setup investigation](https://drive.google.com/open?id=1SIUpdg-6Xm5FFF1ktdBfnR5oRKjyPAfXir7Drui4cYM)
|
||||
|
||||
[Another presentation](https://drive.google.com/open?id=1mLQEM__twfivV7nJLDBIomS9pegOYkJQWyM6lTse4PQ) about our work with more details on the architecture of the prototype, and a [video](https://youtu.be/ThlZ5K4hJvo) of the demo we showed.
|
||||
|
||||
TODO: add a link to the NXT-chromium repo once it is uploaded.
|
||||
|
||||
## Key elements of the prototype’s architecture
|
||||
|
||||
### Builder pattern for object creation
|
||||
|
||||
In NXT, object creation is done through builder objects that gather initialization parameters with a fluent interface and return the initialized object when GetResult() is called.
|
||||
|
||||
In addition to the improved type-safety and subjective prettiness compared to giant constructors, this style enables additional optimizations. For example this removes the need for any check for an object being built and allows backend to forget parameters it might not care about.
|
||||
|
||||
Here’s an example of buffer creation:
|
||||
|
||||
```cpp
|
||||
nxt::Buffer buffer = device.CreateBufferBuilder()
|
||||
.SetUsage(nxt::BufferUsageBit::Uniform | nxt::BufferUsageBit::Mapped)
|
||||
.SetSize(42)
|
||||
.GetResult();
|
||||
```
|
||||
|
||||
### The wire client-server and error handling
|
||||
What we call the wire is an API command buffer for NXT. The name was chosen to avoid conflict with the “command buffer” concept in graphics APIs.
|
||||
|
||||
Originally OpenGL was designed as a client-server architecture with asynchronous errors and objects that could be used by the client before they were created by server. Over time more client state-tracking was added but the core asynchronous structure remained. This enabled OpenGL ES 2 / WebGL to be implemented in Chromium in which the web page and the GPU driver live in different processes. In addition to security this separation helps with performance in CPU-bound WebGL apps.
|
||||
|
||||
For this reason we built NXT as a network-transparent API so that it could integrate nicely in the Chromium architecture, and we believe any next-generation Web API would have to be network-transparent too.
|
||||
|
||||
In NXT, as in OpenGL, API objects can be used immediately after they have been created on the client, even if the server hasn’t seen the creation command yet. If object creation succeeds, everything happens transparently otherwise the object is tagged as being an error. NXT calls with error-tagged objects use the following rules:
|
||||
|
||||
- Functions result in a noop.
|
||||
- Functions returning an object return an error value.
|
||||
- Builder methods mark the builder as an error value.
|
||||
|
||||
The idea is that a whole bunch of object creation can be done when the application loads, then all the objects checked once for any error. The concept presented above is similar to [promise pipelining](http://www.erights.org/elib/distrib/pipeline.html) and to the [Maybe monad](https://en.wikipedia.org/wiki/Monad_(functional_programming)#The_Maybe_monad).
|
||||
|
||||
Currently the wire only has client to server communication and there is no way to know the error status of objects or read API data like the content of buffers. In our prototype the wire is responsible for object lifetime validation.
|
||||
|
||||
### Code generation
|
||||
|
||||
Our prototype heavily relies on code generation. It greatly improved iteration time on the API as the generators kept the Javascript bindings, IDL files, wire, C++ bindings and friends up to date. But it reduced flexibility in the API shape as adding as changing the shape required modifying all generators in non-trivial ways.
|
||||
|
||||
For example, NXT can only return objects which prevents mapping buffers or even reading back single pixel values. There is currently no way to know the error status on the client side. These improvements, and more, are planned, and contributions are welcome.
|
||||
|
||||
Other generators include:
|
||||
|
||||
- A C-header with the definition of nxtProcTable that is the “real” underlying NXT API exposed by the backends.
|
||||
- Glue code generating nxtProcTable for a backend with simple validation included (enum value checks etc.)
|
||||
- A mock API for testing
|
||||
|
||||
## Structure of the code
|
||||
|
||||
Here are the main files and directories:
|
||||
|
||||
```
|
||||
/next.json - the JSON file describing the API that is used by the code generators
|
||||
/examples - example code that was also used for end2end testing (it is not possible to do automated testing without being able to read back data)
|
||||
/generator - The code generator and its templates
|
||||
/generator/templates - The code generator templates
|
||||
/generator/templates/blink - Templates used in the integration with Chromium
|
||||
/src - Non-generator code for the ANGLE-like library
|
||||
/src/backend
|
||||
/src/backend/common - Handles all the state tracking and validation
|
||||
/src/backend/metal - the Metal backend
|
||||
/src/backend/opengl - the OpenGL backend
|
||||
/src/wire - Glue code and interfaces for the wire
|
||||
/third_party - external dependencies
|
||||
```
|
||||
|
||||
## Getting and building the code
|
||||
|
||||
NXT standalone is a CMake project with git submodules. To download and build it, do the following:
|
||||
|
||||
```sh
|
||||
git clone --recursive <insert github git repo url here>
|
||||
cd <directory name>
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
make
|
||||
# Run executables in examples/, --help will provide the options to choose the backend (compute only works on Metal on OSX) and the command buffer.
|
||||
```
|
||||
|
||||
It is currently known to compile on Linux and OSX, and has some warnings on Windows when using MSVC (it doesn’t handle code reachability in enum class switches correctly).
|
|
@ -0,0 +1,163 @@
|
|||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "Utils.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
nxt::Device device;
|
||||
nxt::Queue queue;
|
||||
nxt::Pipeline pipeline;
|
||||
|
||||
float RandomFloat(float min, float max) {
|
||||
float zeroOne = rand() / float(RAND_MAX);
|
||||
return zeroOne * (max - min) + min;
|
||||
}
|
||||
|
||||
struct ShaderData {
|
||||
float scale;
|
||||
float time;
|
||||
float offsetX;
|
||||
float offsetY;
|
||||
float scalar;
|
||||
float scalarOffset;
|
||||
};
|
||||
|
||||
static std::vector<ShaderData> shaderData;
|
||||
|
||||
void init() {
|
||||
nxtProcTable procs;
|
||||
GetProcTableAndDevice(&procs, &device);
|
||||
nxtSetProcs(&procs);
|
||||
|
||||
queue = device.CreateQueueBuilder().GetResult();
|
||||
|
||||
nxt::ShaderModule vsModule = CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
|
||||
#version 450
|
||||
|
||||
layout(push_constant) uniform ConstantsBlock {
|
||||
float scale;
|
||||
float time;
|
||||
float offsetX;
|
||||
float offsetY;
|
||||
float scalar;
|
||||
float scalarOffset;
|
||||
} c;
|
||||
|
||||
out vec4 v_color;
|
||||
|
||||
const vec4 positions[3] = vec4[3](
|
||||
vec4( 0.0f, 0.1f, 0.0f, 1.0f),
|
||||
vec4(-0.1f, -0.1f, 0.0f, 1.0f),
|
||||
vec4( 0.1f, -0.1f, 0.0f, 1.0f)
|
||||
);
|
||||
|
||||
const vec4 colors[3] = vec4[3](
|
||||
vec4(1.0f, 0.0f, 0.0f, 1.0f),
|
||||
vec4(0.0f, 1.0f, 0.0f, 1.0f),
|
||||
vec4(0.0f, 0.0f, 1.0f, 1.0f)
|
||||
);
|
||||
|
||||
void main() {
|
||||
vec4 position = positions[gl_VertexIndex];
|
||||
vec4 color = colors[gl_VertexIndex];
|
||||
|
||||
float fade = mod(c.scalarOffset + c.time * c.scalar / 10.0, 1.0);
|
||||
if (fade < 0.5) {
|
||||
fade = fade * 2.0;
|
||||
} else {
|
||||
fade = (1.0 - fade) * 2.0;
|
||||
}
|
||||
float xpos = position.x * c.scale;
|
||||
float ypos = position.y * c.scale;
|
||||
float angle = 3.14159 * 2.0 * fade;
|
||||
float xrot = xpos * cos(angle) - ypos * sin(angle);
|
||||
float yrot = xpos * sin(angle) + ypos * cos(angle);
|
||||
xpos = xrot + c.offsetX;
|
||||
ypos = yrot + c.offsetY;
|
||||
v_color = vec4(fade, 1.0 - fade, 0.0, 1.0) + color;
|
||||
gl_Position = vec4(xpos, ypos, 0.0, 1.0);
|
||||
})"
|
||||
);
|
||||
|
||||
nxt::ShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
||||
#version 450
|
||||
out vec4 fragColor;
|
||||
in vec4 v_color;
|
||||
void main() {
|
||||
fragColor = v_color;
|
||||
})"
|
||||
);
|
||||
|
||||
pipeline = device.CreatePipelineBuilder()
|
||||
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
||||
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
||||
.GetResult();
|
||||
|
||||
shaderData.resize(10000);
|
||||
for (auto& data : shaderData) {
|
||||
data.scale = RandomFloat(0.2, 0.4);
|
||||
data.time = 0.0;
|
||||
data.offsetX = RandomFloat(-0.9, 0.9);
|
||||
data.offsetY = RandomFloat(-0.9, 0.9);
|
||||
data.scalar = RandomFloat(0.5, 2.0);
|
||||
data.scalarOffset = RandomFloat(0.0, 10.0);
|
||||
}
|
||||
}
|
||||
|
||||
void frame() {
|
||||
static int f = 0;
|
||||
f++;
|
||||
|
||||
size_t i = 0;
|
||||
|
||||
std::vector<nxt::CommandBuffer> commands(50);
|
||||
for (int j = 0; j < 50; j++) {
|
||||
|
||||
nxt::CommandBufferBuilder builder = device.CreateCommandBufferBuilder()
|
||||
.SetPipeline(pipeline)
|
||||
.Clone();
|
||||
|
||||
for (int k = 0; k < 200; k++) {
|
||||
|
||||
shaderData[i].time = f / 60.0f;
|
||||
builder.SetPushConstants(nxt::ShaderStageBit::Vertex, 0, 6, reinterpret_cast<uint32_t*>(&shaderData[i]))
|
||||
.DrawArrays(3, 1, 0, 0);
|
||||
i++;
|
||||
}
|
||||
|
||||
commands[j] = builder.GetResult();
|
||||
}
|
||||
|
||||
queue.Submit(50, commands.data());
|
||||
SwapBuffers();
|
||||
fprintf(stderr, "frame %i\n", f);
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
if (!InitUtils(argc, argv)) {
|
||||
return 1;
|
||||
}
|
||||
init();
|
||||
|
||||
while (!ShouldQuit()) {
|
||||
frame();
|
||||
usleep(16000);
|
||||
}
|
||||
|
||||
// TODO release stuff
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef UTILS_BACKENDBINDING_H_
|
||||
#define UTILS_BACKENDBINDING_H_
|
||||
|
||||
#include <nxt/nxt.h>
|
||||
|
||||
struct GLFWwindow;
|
||||
|
||||
class BackendBinding {
|
||||
public:
|
||||
virtual void SetupGLFWWindowHints() = 0;
|
||||
virtual void GetProcAndDevice(nxtProcTable* procs, nxtDevice* device) = 0;
|
||||
virtual void SwapBuffers() = 0;
|
||||
|
||||
void SetWindow(GLFWwindow* window) {this->window = window;}
|
||||
|
||||
protected:
|
||||
GLFWwindow* window = nullptr;
|
||||
};
|
||||
|
||||
#endif // UTILS_BACKENDBINDING_H_
|
|
@ -0,0 +1,71 @@
|
|||
# Copyright 2017 The NXT Authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
list(APPEND UTILS_SOURCES
|
||||
Utils.h
|
||||
Utils.cpp
|
||||
BackendBinding.h
|
||||
)
|
||||
|
||||
if (APPLE)
|
||||
list(APPEND UTILS_SOURCES
|
||||
MetalBinding.mm
|
||||
)
|
||||
endif()
|
||||
|
||||
add_library(utils SHARED ${UTILS_SOURCES})
|
||||
target_link_libraries(utils nxt_backend nxt_wire shaderc nxtcpp nxt)
|
||||
SetCXX14(utils)
|
||||
|
||||
add_executable(CHelloTriangle HelloTriangle.c)
|
||||
target_link_libraries(CHelloTriangle utils)
|
||||
|
||||
add_executable(CppHelloTriangle HelloTriangle.cpp)
|
||||
target_link_libraries(CppHelloTriangle utils)
|
||||
SetCXX14(CppHelloTriangle)
|
||||
|
||||
add_executable(ComputeBoids ComputeBoids.cpp)
|
||||
target_link_libraries(ComputeBoids utils)
|
||||
target_include_directories(ComputeBoids PUBLIC ../ ${GLM_INCLUDE_DIR})
|
||||
SetCXX14(ComputeBoids)
|
||||
|
||||
add_executable(HelloVertices HelloVertices.cpp)
|
||||
target_link_libraries(HelloVertices utils)
|
||||
SetCXX14(HelloVertices)
|
||||
|
||||
add_executable(HelloInstancing HelloInstancing.cpp)
|
||||
target_link_libraries(HelloInstancing utils)
|
||||
SetCXX14(HelloInstancing)
|
||||
|
||||
add_executable(HelloIndices HelloIndices.cpp)
|
||||
target_link_libraries(HelloIndices utils)
|
||||
SetCXX14(HelloIndices)
|
||||
|
||||
add_executable(HelloUBO HelloUBO.cpp)
|
||||
target_link_libraries(HelloUBO utils)
|
||||
SetCXX14(HelloUBO)
|
||||
|
||||
add_executable(HelloCompute HelloCompute.cpp)
|
||||
target_link_libraries(HelloCompute utils)
|
||||
SetCXX14(HelloCompute)
|
||||
|
||||
add_executable(Animometer Animometer.cpp)
|
||||
target_link_libraries(Animometer utils)
|
||||
SetCXX14(Animometer)
|
||||
|
||||
add_executable(SpirvTest SpirvTest.cpp)
|
||||
target_link_libraries(SpirvTest shaderc spirv-cross nxtcpp)
|
||||
SetCXX14(SpirvTest)
|
||||
|
||||
add_subdirectory(glTFViewer)
|
|
@ -0,0 +1,324 @@
|
|||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "Utils.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
nxt::Device device;
|
||||
nxt::Queue queue;
|
||||
|
||||
nxt::Buffer modelBuffer;
|
||||
std::array<nxt::Buffer, 2> particleBuffers;
|
||||
|
||||
nxt::Pipeline renderPipeline;
|
||||
|
||||
nxt::Buffer updateParams;
|
||||
nxt::Pipeline updatePipeline;
|
||||
std::array<nxt::BindGroup, 2> updateBGs;
|
||||
|
||||
std::array<nxt::CommandBuffer, 2> commandBuffers;
|
||||
|
||||
size_t pingpong = 0;
|
||||
|
||||
static const uint32_t kNumParticles = 1000;
|
||||
|
||||
struct Particle {
|
||||
glm::vec2 pos;
|
||||
glm::vec2 vel;
|
||||
};
|
||||
|
||||
struct SimParams {
|
||||
float deltaT;
|
||||
float rule1Distance;
|
||||
float rule2Distance;
|
||||
float rule3Distance;
|
||||
float rule1Scale;
|
||||
float rule2Scale;
|
||||
float rule3Scale;
|
||||
int particleCount;
|
||||
};
|
||||
|
||||
void initBuffers() {
|
||||
glm::vec2 model[3] = {
|
||||
{-0.01, -0.02},
|
||||
{0.01, -0.02},
|
||||
{0.00, 0.02},
|
||||
};
|
||||
modelBuffer = device.CreateBufferBuilder()
|
||||
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Vertex)
|
||||
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
|
||||
.SetSize(sizeof(model))
|
||||
.GetResult();
|
||||
modelBuffer.SetSubData(0, sizeof(model) / sizeof(uint32_t),
|
||||
reinterpret_cast<uint32_t*>(model));
|
||||
modelBuffer.FreezeUsage(nxt::BufferUsageBit::Vertex);
|
||||
|
||||
SimParams params = { 0.04, 0.1, 0.025, 0.025, 0.02, 0.05, 0.005, kNumParticles };
|
||||
updateParams = device.CreateBufferBuilder()
|
||||
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Uniform)
|
||||
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
|
||||
.SetSize(sizeof(SimParams))
|
||||
.GetResult();
|
||||
updateParams.SetSubData(0, sizeof(SimParams) / sizeof(uint32_t),
|
||||
reinterpret_cast<uint32_t*>(¶ms));
|
||||
updateParams.FreezeUsage(nxt::BufferUsageBit::Uniform);
|
||||
|
||||
std::vector<Particle> initialParticles(kNumParticles);
|
||||
{
|
||||
std::mt19937 generator;
|
||||
std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
|
||||
for (auto& p : initialParticles)
|
||||
{
|
||||
p.pos = glm::vec2(dist(generator), dist(generator));
|
||||
p.vel = glm::vec2(dist(generator), dist(generator)) * 0.1f;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
particleBuffers[i] = device.CreateBufferBuilder()
|
||||
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Vertex | nxt::BufferUsageBit::Storage)
|
||||
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
|
||||
.SetSize(sizeof(Particle) * kNumParticles)
|
||||
.GetResult();
|
||||
|
||||
particleBuffers[i].SetSubData(0,
|
||||
sizeof(Particle) * kNumParticles / sizeof(uint32_t),
|
||||
reinterpret_cast<uint32_t*>(initialParticles.data()));
|
||||
}
|
||||
}
|
||||
|
||||
void initRender() {
|
||||
nxt::ShaderModule vsModule = CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
|
||||
#version 450
|
||||
layout(location = 0) in vec2 a_particlePos;
|
||||
layout(location = 1) in vec2 a_particleVel;
|
||||
layout(location = 2) in vec2 a_pos;
|
||||
void main() {
|
||||
float angle = -atan(a_particleVel.x, a_particleVel.y);
|
||||
vec2 pos = vec2(a_pos.x * cos(angle) - a_pos.y * sin(angle),
|
||||
a_pos.x * sin(angle) + a_pos.y * cos(angle));
|
||||
gl_Position = vec4(pos + a_particlePos, 0, 1);
|
||||
}
|
||||
)");
|
||||
|
||||
nxt::ShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
||||
#version 450
|
||||
out vec4 fragColor;
|
||||
void main() {
|
||||
fragColor = vec4(1.0);
|
||||
}
|
||||
)");
|
||||
|
||||
nxt::InputState inputState = device.CreateInputStateBuilder()
|
||||
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32, offsetof(Particle, pos))
|
||||
.SetAttribute(1, 0, nxt::VertexFormat::FloatR32G32, offsetof(Particle, vel))
|
||||
.SetInput(0, sizeof(Particle), nxt::InputStepMode::Instance)
|
||||
.SetAttribute(2, 1, nxt::VertexFormat::FloatR32G32, 0)
|
||||
.SetInput(1, sizeof(glm::vec2), nxt::InputStepMode::Vertex)
|
||||
.GetResult();
|
||||
|
||||
renderPipeline = device.CreatePipelineBuilder()
|
||||
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
||||
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
||||
.SetInputState(inputState)
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
void initSim() {
|
||||
nxt::ShaderModule module = CreateShaderModule(device, nxt::ShaderStage::Compute, R"(
|
||||
#version 450
|
||||
|
||||
struct Particle {
|
||||
vec2 pos;
|
||||
vec2 vel;
|
||||
};
|
||||
|
||||
layout(std140, set = 0, binding = 0) uniform SimParams {
|
||||
float deltaT;
|
||||
float rule1Distance;
|
||||
float rule2Distance;
|
||||
float rule3Distance;
|
||||
float rule1Scale;
|
||||
float rule2Scale;
|
||||
float rule3Scale;
|
||||
int particleCount;
|
||||
} params;
|
||||
|
||||
layout(std140, set = 0, binding = 1) buffer ParticlesA {
|
||||
Particle particlesA[1000];
|
||||
};
|
||||
|
||||
layout(std140, set = 0, binding = 2) buffer ParticlesB {
|
||||
Particle particlesB[1000];
|
||||
};
|
||||
|
||||
void main() {
|
||||
// https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
|
||||
|
||||
uint index = gl_GlobalInvocationID.x;
|
||||
if (index >= params.particleCount) { return; }
|
||||
|
||||
vec2 vPos = particlesA[index].pos;
|
||||
vec2 vVel = particlesA[index].vel;
|
||||
|
||||
vec2 cMass = vec2(0.0, 0.0);
|
||||
vec2 cVel = vec2(0.0, 0.0);
|
||||
vec2 colVel = vec2(0.0, 0.0);
|
||||
int cMassCount = 0;
|
||||
int cVelCount = 0;
|
||||
|
||||
vec2 pos;
|
||||
vec2 vel;
|
||||
for (int i = 0; i < params.particleCount; ++i) {
|
||||
if (i == index) { continue; }
|
||||
pos = particlesA[i].pos.xy;
|
||||
vel = particlesA[i].vel.xy;
|
||||
|
||||
if (distance(pos, vPos) < params.rule1Distance) {
|
||||
cMass += pos;
|
||||
cMassCount++;
|
||||
}
|
||||
if (distance(pos, vPos) < params.rule2Distance) {
|
||||
colVel -= (pos - vPos);
|
||||
}
|
||||
if (distance(pos, vPos) < params.rule3Distance) {
|
||||
cVel += vel;
|
||||
cVelCount++;
|
||||
}
|
||||
}
|
||||
if (cMassCount > 0) {
|
||||
cMass = cMass / cMassCount - vPos;
|
||||
}
|
||||
if (cVelCount > 0) {
|
||||
cVel = cVel / cVelCount;
|
||||
}
|
||||
|
||||
vVel += cMass * params.rule1Scale + colVel * params.rule2Scale + cVel * params.rule3Scale;
|
||||
|
||||
// clamp velocity for a more pleasing simulation.
|
||||
vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
|
||||
|
||||
// kinematic update
|
||||
vPos += vVel * params.deltaT;
|
||||
|
||||
// Wrap around boundary
|
||||
if (vPos.x < -1.0) vPos.x = 1.0;
|
||||
if (vPos.x > 1.0) vPos.x = -1.0;
|
||||
if (vPos.y < -1.0) vPos.y = 1.0;
|
||||
if (vPos.y > 1.0) vPos.y = -1.0;
|
||||
|
||||
particlesB[index].pos = vPos;
|
||||
|
||||
// Write back
|
||||
particlesB[index].vel = vVel;
|
||||
}
|
||||
)");
|
||||
|
||||
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
|
||||
.SetBindingsType(nxt::ShaderStageBit::Compute, nxt::BindingType::UniformBuffer, 0, 1)
|
||||
.SetBindingsType(nxt::ShaderStageBit::Compute, nxt::BindingType::StorageBuffer, 1, 2)
|
||||
.GetResult();
|
||||
|
||||
nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
|
||||
.SetBindGroupLayout(0, bgl)
|
||||
.GetResult();
|
||||
|
||||
updatePipeline = device.CreatePipelineBuilder()
|
||||
.SetLayout(pl)
|
||||
.SetStage(nxt::ShaderStage::Compute, module, "main")
|
||||
.GetResult();
|
||||
|
||||
nxt::BufferView updateParamsView = updateParams.CreateBufferViewBuilder()
|
||||
.SetExtent(0, sizeof(SimParams))
|
||||
.GetResult();
|
||||
|
||||
std::array<nxt::BufferView, 2> views;
|
||||
for (uint32_t i = 0; i < 2; ++i) {
|
||||
views[i] = particleBuffers[i].CreateBufferViewBuilder()
|
||||
.SetExtent(0, kNumParticles * sizeof(Particle))
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < 2; ++i) {
|
||||
updateBGs[i] = device.CreateBindGroupBuilder()
|
||||
.SetLayout(bgl)
|
||||
.SetUsage(nxt::BindGroupUsage::Frozen)
|
||||
.SetBufferViews(0, 1, &updateParamsView)
|
||||
.SetBufferViews(1, 1, &views[i])
|
||||
.SetBufferViews(2, 1, &views[(i + 1) % 2])
|
||||
.GetResult();
|
||||
}
|
||||
}
|
||||
|
||||
void initCommandBuffers() {
|
||||
static const uint32_t zeroOffsets[1] = {0};
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
auto& bufferSrc = particleBuffers[i];
|
||||
auto& bufferDst = particleBuffers[(i + 1) % 2];
|
||||
commandBuffers[i] = device.CreateCommandBufferBuilder()
|
||||
.SetPipeline(updatePipeline)
|
||||
.TransitionBufferUsage(bufferSrc, nxt::BufferUsageBit::Storage)
|
||||
.TransitionBufferUsage(bufferDst, nxt::BufferUsageBit::Storage)
|
||||
.SetBindGroup(0, updateBGs[i])
|
||||
.Dispatch(kNumParticles, 1, 1)
|
||||
|
||||
.SetPipeline(renderPipeline)
|
||||
.TransitionBufferUsage(bufferDst, nxt::BufferUsageBit::Vertex)
|
||||
.SetVertexBuffers(0, 1, &bufferDst, zeroOffsets)
|
||||
.SetVertexBuffers(1, 1, &modelBuffer, zeroOffsets)
|
||||
.DrawArrays(3, kNumParticles, 0, 0)
|
||||
|
||||
.GetResult();
|
||||
}
|
||||
}
|
||||
|
||||
void init() {
|
||||
nxtProcTable procs;
|
||||
GetProcTableAndDevice(&procs, &device);
|
||||
nxtSetProcs(&procs);
|
||||
|
||||
queue = device.CreateQueueBuilder().GetResult();
|
||||
|
||||
initBuffers();
|
||||
initRender();
|
||||
initSim();
|
||||
initCommandBuffers();
|
||||
}
|
||||
|
||||
void frame() {
|
||||
queue.Submit(1, &commandBuffers[pingpong]);
|
||||
SwapBuffers();
|
||||
|
||||
pingpong = (pingpong + 1) % 2;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
if (!InitUtils(argc, argv)) {
|
||||
return 1;
|
||||
}
|
||||
init();
|
||||
|
||||
while (!ShouldQuit()) {
|
||||
frame();
|
||||
usleep(16000);
|
||||
}
|
||||
|
||||
// TODO release stuff
|
||||
}
|
|
@ -0,0 +1,154 @@
|
|||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "Utils.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
nxt::Device device;
|
||||
nxt::Queue queue;
|
||||
nxt::Buffer buffer;
|
||||
nxt::Pipeline renderPipeline;
|
||||
nxt::BindGroup renderBindGroup;
|
||||
nxt::Pipeline computePipeline;
|
||||
nxt::BindGroup computeBindGroup;
|
||||
|
||||
void init() {
|
||||
nxtProcTable procs;
|
||||
GetProcTableAndDevice(&procs, &device);
|
||||
nxtSetProcs(&procs);
|
||||
|
||||
queue = device.CreateQueueBuilder().GetResult();
|
||||
|
||||
struct {uint32_t a; float b;} s;
|
||||
memset(&s, sizeof(s), 0);
|
||||
buffer = device.CreateBufferBuilder()
|
||||
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Uniform | nxt::BufferUsageBit::Storage)
|
||||
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
|
||||
.SetSize(sizeof(s))
|
||||
.GetResult();
|
||||
buffer.SetSubData(0, sizeof(s) / sizeof(uint32_t), reinterpret_cast<uint32_t*>(&s));
|
||||
|
||||
nxt::BufferView view = buffer.CreateBufferViewBuilder()
|
||||
.SetExtent(0, sizeof(s))
|
||||
.GetResult();
|
||||
|
||||
{
|
||||
nxt::ShaderModule module = CreateShaderModule(device, nxt::ShaderStage::Compute, R"(
|
||||
#version 450
|
||||
layout(set = 0, binding = 0) buffer myBlock {
|
||||
int a;
|
||||
float b;
|
||||
} myStorage;
|
||||
void main() {
|
||||
myStorage.a = (myStorage.a + 1) % 256;
|
||||
myStorage.b = mod((myStorage.b + 0.02), 1.0);
|
||||
})"
|
||||
);
|
||||
|
||||
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
|
||||
.SetBindingsType(nxt::ShaderStageBit::Compute, nxt::BindingType::StorageBuffer, 0, 1)
|
||||
.GetResult();
|
||||
|
||||
nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
|
||||
.SetBindGroupLayout(0, bgl)
|
||||
.GetResult();
|
||||
|
||||
computePipeline = device.CreatePipelineBuilder()
|
||||
.SetLayout(pl)
|
||||
.SetStage(nxt::ShaderStage::Compute, module, "main")
|
||||
.GetResult();
|
||||
|
||||
computeBindGroup = device.CreateBindGroupBuilder()
|
||||
.SetLayout(bgl)
|
||||
.SetUsage(nxt::BindGroupUsage::Frozen)
|
||||
.SetBufferViews(0, 1, &view)
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
{
|
||||
nxt::ShaderModule vsModule = CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
|
||||
#version 450
|
||||
const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));
|
||||
void main() {
|
||||
gl_Position = vec4(pos[gl_VertexIndex], 0.5, 1.0);
|
||||
})"
|
||||
);
|
||||
|
||||
nxt::ShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
||||
#version 450
|
||||
layout(set = 0, binding = 0) uniform myBlock {
|
||||
int a;
|
||||
float b;
|
||||
} myUbo;
|
||||
out vec4 fragColor;
|
||||
void main() {
|
||||
fragColor = vec4(1.0, myUbo.a / 255.0, myUbo.b, 1.0);
|
||||
})"
|
||||
);
|
||||
|
||||
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
|
||||
.SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::UniformBuffer, 0, 1)
|
||||
.GetResult();
|
||||
|
||||
nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
|
||||
.SetBindGroupLayout(0, bgl)
|
||||
.GetResult();
|
||||
|
||||
renderPipeline = device.CreatePipelineBuilder()
|
||||
.SetLayout(pl)
|
||||
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
||||
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
||||
.GetResult();
|
||||
|
||||
renderBindGroup = device.CreateBindGroupBuilder()
|
||||
.SetLayout(bgl)
|
||||
.SetUsage(nxt::BindGroupUsage::Frozen)
|
||||
.SetBufferViews(0, 1, &view)
|
||||
.GetResult();
|
||||
}
|
||||
}
|
||||
|
||||
void frame() {
|
||||
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
|
||||
.SetPipeline(computePipeline)
|
||||
.TransitionBufferUsage(buffer, nxt::BufferUsageBit::Storage)
|
||||
.SetBindGroup(0, computeBindGroup)
|
||||
.Dispatch(1, 1, 1)
|
||||
|
||||
.SetPipeline(renderPipeline)
|
||||
.TransitionBufferUsage(buffer, nxt::BufferUsageBit::Uniform)
|
||||
.SetBindGroup(0, renderBindGroup)
|
||||
.DrawArrays(3, 1, 0, 0)
|
||||
|
||||
.GetResult();
|
||||
|
||||
queue.Submit(1, &commands);
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
if (!InitUtils(argc, argv)) {
|
||||
return 1;
|
||||
}
|
||||
init();
|
||||
|
||||
while (!ShouldQuit()) {
|
||||
frame();
|
||||
usleep(16000);
|
||||
}
|
||||
|
||||
// TODO release stuff
|
||||
}
|
|
@ -0,0 +1,117 @@
|
|||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "Utils.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
nxt::Device device;
|
||||
|
||||
nxt::Buffer indexBuffer;
|
||||
nxt::Buffer vertexBuffer;
|
||||
|
||||
nxt::Queue queue;
|
||||
nxt::Pipeline pipeline;
|
||||
|
||||
void initBuffers() {
|
||||
static const uint32_t indexData[3] = {
|
||||
0, 1, 2,
|
||||
};
|
||||
indexBuffer = device.CreateBufferBuilder()
|
||||
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Index)
|
||||
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
|
||||
.SetSize(sizeof(indexData))
|
||||
.GetResult();
|
||||
indexBuffer.SetSubData(0, sizeof(indexData) / sizeof(uint32_t), indexData);
|
||||
indexBuffer.FreezeUsage(nxt::BufferUsageBit::Index);
|
||||
|
||||
static const float vertexData[12] = {
|
||||
0.0f, 0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.0f, 1.0f,
|
||||
};
|
||||
vertexBuffer = device.CreateBufferBuilder()
|
||||
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Vertex)
|
||||
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
|
||||
.SetSize(sizeof(vertexData))
|
||||
.GetResult();
|
||||
vertexBuffer.SetSubData(0, sizeof(vertexData) / sizeof(uint32_t),
|
||||
reinterpret_cast<const uint32_t*>(vertexData));
|
||||
vertexBuffer.FreezeUsage(nxt::BufferUsageBit::Vertex);
|
||||
}
|
||||
|
||||
void init() {
|
||||
nxtProcTable procs;
|
||||
GetProcTableAndDevice(&procs, &device);
|
||||
nxtSetProcs(&procs);
|
||||
|
||||
queue = device.CreateQueueBuilder().GetResult();
|
||||
|
||||
initBuffers();
|
||||
|
||||
nxt::ShaderModule vsModule = CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
|
||||
#version 450
|
||||
layout(location = 0) in vec4 pos;
|
||||
void main() {
|
||||
gl_Position = pos;
|
||||
})"
|
||||
);
|
||||
|
||||
nxt::ShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
||||
#version 450
|
||||
out vec4 fragColor;
|
||||
void main() {
|
||||
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
})"
|
||||
);
|
||||
|
||||
auto inputState = device.CreateInputStateBuilder()
|
||||
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
|
||||
.SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
|
||||
.GetResult();
|
||||
|
||||
pipeline = device.CreatePipelineBuilder()
|
||||
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
||||
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
||||
.SetInputState(inputState)
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
void frame() {
|
||||
static const uint32_t vertexBufferOffsets[1] = {0};
|
||||
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
|
||||
.SetPipeline(pipeline)
|
||||
.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
|
||||
.SetIndexBuffer(indexBuffer, 0, nxt::IndexFormat::Uint32)
|
||||
.DrawElements(3, 1, 0, 0)
|
||||
.GetResult();
|
||||
|
||||
queue.Submit(1, &commands);
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
if (!InitUtils(argc, argv)) {
|
||||
return 1;
|
||||
}
|
||||
init();
|
||||
|
||||
while (!ShouldQuit()) {
|
||||
frame();
|
||||
usleep(16000);
|
||||
}
|
||||
|
||||
// TODO release stuff
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "Utils.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
nxt::Device device;
|
||||
|
||||
nxt::Buffer vertexBuffer;
|
||||
nxt::Buffer instanceBuffer;
|
||||
|
||||
nxt::Queue queue;
|
||||
nxt::Pipeline pipeline;
|
||||
|
||||
void initBuffers() {
|
||||
static const float vertexData[12] = {
|
||||
0.0f, 0.1f, 0.0f, 1.0f,
|
||||
-0.1f, -0.1f, 0.0f, 1.0f,
|
||||
0.1f, -0.1f, 0.0f, 1.0f,
|
||||
};
|
||||
vertexBuffer = device.CreateBufferBuilder()
|
||||
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Vertex)
|
||||
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
|
||||
.SetSize(sizeof(vertexData))
|
||||
.GetResult();
|
||||
vertexBuffer.SetSubData(0, sizeof(vertexData) / sizeof(uint32_t),
|
||||
reinterpret_cast<const uint32_t*>(vertexData));
|
||||
vertexBuffer.FreezeUsage(nxt::BufferUsageBit::Vertex);
|
||||
|
||||
static const float instanceData[8] = {
|
||||
-0.5f, -0.5f,
|
||||
-0.5f, 0.5f,
|
||||
0.5f, -0.5f,
|
||||
0.5f, 0.5f,
|
||||
};
|
||||
instanceBuffer = device.CreateBufferBuilder()
|
||||
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Vertex)
|
||||
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
|
||||
.SetSize(sizeof(instanceData))
|
||||
.GetResult();
|
||||
instanceBuffer.SetSubData(0, sizeof(instanceData) / sizeof(uint32_t),
|
||||
reinterpret_cast<const uint32_t*>(instanceData));
|
||||
instanceBuffer.FreezeUsage(nxt::BufferUsageBit::Vertex);
|
||||
}
|
||||
|
||||
void init() {
|
||||
nxtProcTable procs;
|
||||
GetProcTableAndDevice(&procs, &device);
|
||||
nxtSetProcs(&procs);
|
||||
|
||||
queue = device.CreateQueueBuilder().GetResult();
|
||||
|
||||
initBuffers();
|
||||
|
||||
nxt::ShaderModule vsModule = CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
|
||||
#version 450
|
||||
layout(location = 0) in vec4 pos;
|
||||
layout(location = 1) in vec2 instance;
|
||||
void main() {
|
||||
gl_Position = vec4(pos.xy + instance, pos.zw);
|
||||
})"
|
||||
);
|
||||
|
||||
nxt::ShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
||||
#version 450
|
||||
out vec4 fragColor;
|
||||
void main() {
|
||||
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
})"
|
||||
);
|
||||
|
||||
auto inputState = device.CreateInputStateBuilder()
|
||||
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
|
||||
.SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
|
||||
.SetAttribute(1, 1, nxt::VertexFormat::FloatR32G32, 0)
|
||||
.SetInput(1, 2 * sizeof(float), nxt::InputStepMode::Instance)
|
||||
.GetResult();
|
||||
|
||||
pipeline = device.CreatePipelineBuilder()
|
||||
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
||||
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
||||
.SetInputState(inputState)
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
void frame() {
|
||||
static const uint32_t vertexBufferOffsets[1] = {0};
|
||||
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
|
||||
.SetPipeline(pipeline)
|
||||
.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
|
||||
.SetVertexBuffers(1, 1, &instanceBuffer, vertexBufferOffsets)
|
||||
.DrawArrays(3, 4, 0, 0)
|
||||
.GetResult();
|
||||
|
||||
queue.Submit(1, &commands);
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
if (!InitUtils(argc, argv)) {
|
||||
return 1;
|
||||
}
|
||||
init();
|
||||
|
||||
while (!ShouldQuit()) {
|
||||
frame();
|
||||
usleep(16000);
|
||||
}
|
||||
|
||||
// TODO release stuff
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "Utils.h"
|
||||
|
||||