Commit graph

1940 commits

Author SHA1 Message Date
VVishion
52fd626fed Expose wgpu's StencilOperation with bevy (#2819)
# Objective

Bevy should expose all wgpu types needed for building rendering pipelines.
Closes #2818

## Solution

Add wgpu's StencilOperation to bevy_render2::render_resource's export.
2021-09-16 07:17:07 +00:00
Carter Anderson
11b41206eb Add upstream bevy_ecs and prepare for custom-shaders merge (#2815)
This updates the `pipelined-rendering` branch to use the latest `bevy_ecs` from `main`. This accomplishes a couple of goals:

1. prepares for upcoming `custom-shaders` branch changes, which were what drove many of the recent bevy_ecs changes on `main`
2. prepares for the soon-to-happen merge of `pipelined-rendering` into `main`. By including bevy_ecs changes now, we make that merge simpler / easier to review. 

I split this up into 3 commits:

1. **add upstream bevy_ecs**: please don't bother reviewing this content. it has already received thorough review on `main` and is a literal copy/paste of the relevant folders (the old folders were deleted so the directories are literally exactly the same as `main`).
2. **support manual buffer application in stages**: this is used to enable the Extract step. we've already reviewed this once on the `pipelined-rendering` branch, but its worth looking at one more time in the new context of (1).
3. **support manual archetype updates in QueryState**: same situation as (2).
2021-09-14 06:14:19 +00:00
Loch Wansbrough
59bfbd3295 Add Deref implementation for ComputePipeline (#2759)
# Objective

Fixes a usability problem where the user is unable to use their reference to a ComputePipeline in their compute pass.

## Solution

Implements Deref, allowing the user to obtain the reference to the underlying wgpu::ComputePipeline
2021-08-31 20:33:21 +00:00
Robert Swain
045f324e97 Use the infinite reverse right-handed perspective projection (#2543)
# Objective

Forward perspective projections have poor floating point precision distribution over the depth range. Reverse projections fair much better, and instead of having to have a far plane, with the reverse projection, using an infinite far plane is not a problem. The infinite reverse perspective projection has become the industry standard. The renderer rework is a great time to migrate to it.

## Solution

All perspective projections, including point lights, have been moved to using `glam::Mat4::perspective_infinite_reverse_rh()` and so have no far plane. As various depth textures are shared between orthographic and perspective projections, a quirk of this PR is that the near and far planes of the orthographic projection are swapped when the Mat4 is computed. This has no impact on 2D/3D orthographic projection usage, and provides consistency in shaders, texture clear values, etc. throughout the codebase.

## Known issues

For some reason, when looking along -Z, all geometry is black. The camera can be translated up/down / strafed left/right and geometry will still be black. Moving forward/backward or rotating the camera away from looking exactly along -Z causes everything to work as expected.

I have tried to debug this issue but both in macOS and Windows I get crashes when doing pixel debugging. If anyone could reproduce this and debug it I would be very grateful. Otherwise I will have to try to debug it further without pixel debugging, though the projections and such all looked fine to me.
2021-08-27 20:15:09 +00:00
Zicklag
cea7db1050 Remove Need for Sprite Size Sync System (#2632)
# Objective

- Prevent the need to have a system that synchronizes sprite sizes with their images

## Solution

- Read the sprite size from the image asset when rendering the sprite
- Replace the `size` and `resize_mode` fields of `Sprite` with a `custom_size: Option<Vec2>` that will modify the sprite's rendered size to be different than the image size, but only if it is `Some(Vec2)`
2021-08-26 00:53:51 +00:00
Robert Swain
dd32cd029d Pipelined separate shadow vertex shader (#2727)
# Objective

- Avoid unnecessary work in the vertex shader of the numerous shadow passes
- Have the natural order of bind groups in the pbr shader: view, material, mesh

## Solution

- Separate out the vertex stage of pbr.wgsl into depth.wgsl
- Remove the unnecessary calculation of uv and normal, as well as removing the unnecessary vertex inputs and outputs
- Use the depth.wgsl for shadow passes
- Reorder the bind groups in pbr.wgsl and PbrShaders to be 0 - view, 1 - material, 2 - mesh in decreasing order of rebind frequency
2021-08-25 20:10:43 +00:00
Robert Swain
f4aa3284a8 bevy_pbr2: Add support for not casting/receiving shadows (#2726)
# Objective

Allow marking meshes as not casting / receiving shadows.

## Solution

- Added `NotShadowCaster` and `NotShadowReceiver` zero-sized type components.
- Extract these components into `bool`s in `ExtractedMesh`
- Only generate `DrawShadowMesh` `Drawable`s for meshes _without_ `NotShadowCaster`
- Add a `u32` bit `flags` member to `MeshUniform` with one flag indicating whether the mesh is a shadow receiver
- If a mesh does _not_ have the `NotShadowReceiver` component, then it is a shadow receiver, and so the bit in the `MeshUniform` is set, otherwise it is not set.
- Added an example illustrating the functionality.

NOTE: I wanted to have the default state of a mesh as being a shadow caster and shadow receiver, hence the `Not*` components. However, I am on the fence about this. I don't want to have a negative performance impact, nor have people wondering why their custom meshes don't have shadows because they forgot to add `ShadowCaster` and `ShadowReceiver` components, but I also really don't like the double negatives the `Not*` approach incurs. What do you think?

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-08-25 19:44:20 +00:00
Robert Swain
f368bf7fc7 bevy_pbr2: Add support for configurable shadow map sizes (#2700)
# Objective

Add support for configurable shadow map sizes

## Solution

- Add `DirectionalLightShadowMap` and `PointLightShadowMap` resources, which just have size members, to the app world, and add `Extracted*` counterparts to the render world
- Use the configured sizes when rendering shadow maps
- Default sizes remain the same - 4096 for directional light shadow maps, 1024 for point light shadow maps (which are cube maps so 6 faces at 1024x1024 per light)
2021-08-25 05:57:57 +00:00
Carter Anderson
9898469e9e Sub app label changes (#2717)
Makes some tweaks to the SubApp labeling introduced in #2695:

* Ergonomics improvements
* Removes unnecessary allocation when retrieving subapp label
* Removes the newly added "app macros" crate in favor of bevy_derive
* renamed RenderSubApp to RenderApp

@zicklag (for reference)
2021-08-24 06:37:28 +00:00
Zicklag
e290a7e29c Implement Sub-App Labels (#2695)
This is a rather simple but wide change, and it involves adding a new `bevy_app_macros` crate. Let me know if there is a better way to do any of this!

---

# Objective

- Allow adding and accessing sub-apps by using a label instead of an index

## Solution

- Migrate the bevy label implementation and derive code to the `bevy_utils` and `bevy_macro_utils` crates and then add a new `SubAppLabel` trait to the `bevy_app` crate that is used when adding or getting a sub-app from an app.
2021-08-24 00:31:21 +00:00
Robert Swain
c3d3ae7f92 bevy_pbr2: Improve lighting units and documentation (#2704)
# Objective

A question was raised on Discord about the units of the `PointLight` `intensity` member.

After digging around in the bevy_pbr2 source code and [Google Filament documentation](https://google.github.io/filament/Filament.html#mjx-eqn-pointLightLuminousPower) I discovered that the intention by Filament was that the 'intensity' value for point lights would be in lumens. This makes a lot of sense as these are quite relatable units given basically all light bulbs I've seen sold over the past years are rated in lumens as people move away from thinking about how bright a bulb is relative to a non-halogen incandescent bulb.

However, it seems that the derivation of the conversion between luminous power (lumens, denoted `Φ` in the Filament formulae) and luminous intensity (lumens per steradian, `I` in the Filament formulae) was missed and I can see why as it is tucked right under equation 58 at the link above. As such, while the formula states that for a point light, `I = Φ / 4 π` we have been using `intensity` as if it were luminous intensity `I`.

Before this PR, the intensity field is luminous intensity in lumens per steradian. After this PR, the intensity field is luminous power in lumens, [as suggested by Filament](https://google.github.io/filament/Filament.html#table_lighttypesunits) (unfortunately the link jumps to the table's caption so scroll up to see the actual table).

I appreciate that it may be confusing to call this an intensity, but I think this is intended as more of a non-scientific, human-relatable general term with a bit of hand waving so that most light types can just have an intensity field and for most of them it works in the same way or at least with some relatable value. I'm inclined to think this is reasonable rather than throwing terms like luminous power, luminous intensity, blah at users.

## Solution

- Documented the `PointLight` `intensity` member as 'luminous power' in units of lumens.
- Added a table of examples relating from various types of household lighting to lumen values.
- Added in the mapping from luminous power to luminous intensity when premultiplying the intensity into the colour before it is made into a graphics uniform.
- Updated the documentation in `pbr.wgsl` to clarify the earlier confusion about the missing `/ 4 π`.
- Bumped the intensity of the point lights in `3d_scene_pipelined` to 1600 lumens.

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-08-23 23:48:11 +00:00
Robert Swain
993ce84bc7 bevy_render2: Make default near plane more sensible at 0.1 (#2703)
# Objective

The default perspective projection near plane being at 1 unit feels very far away if one considers units to directly map to real world units such as metres. Not being able to see anything that is closer than 1m is unnecessarily limiting. Using a default of 0.1 makes more sense as it is difficult to even focus on things closer than 10cm in the real world.

## Solution

- Changed the default perspective projection near plane to 0.1.
2021-08-23 23:28:45 +00:00
Zicklag
3faca93f7b Add ClearColor Resource to Pipelined Renderer (#2631)
# Objective

- Allow the user to set the clear color when using the pipelined renderer

## Solution

- Add a `ClearColor` resource that can be added to the world to configure the clear color

## Remaining Issues

Currently the `ClearColor` resource is cloned from the app world to the render world every frame. There are two ways I can think of around this:

1. Figure out why `app_world.is_resource_changed::<ClearColor>()` always returns `true` in the `extract` step and fix it so that we are only updating the resource when it changes
2. Require the users to add the `ClearColor` resource to the render sub-app instead of the parent app. This is currently sub-optimal until we have labled sub-apps, and probably a helper funciton on `App` such as `app.with_sub_app(RenderApp, |app| { ... })`. Even if we had that, I think it would be more than we want the user to have to think about. They shouldn't have to know about the render sub-app I don't think.

I think the first option is the best, but I could really use some help figuring out the nuance of why `is_resource_changed` is always returning true in that context.
2021-08-19 20:34:31 +00:00
Dimev
5fa0b5b498 Added ComputePipelineDescriptor (#2628)
# Objective
Fix ComputePipelineDescriptor missing from WGPU exports

## Solution
Added it to the pub use wgpu::{ ... }


Co-authored-by: Dimas <skythedragon@outlook.com>
2021-08-11 20:28:50 +00:00
Zicklag
c8330037e7 Re-implement Automatic Sprite Sizing (#2613)
# Objective

- Prevent the need to specify a sprite size when using the pipelined sprite renderer

## Solution

- Re-introduce the sprite auto resize system from the old renderer
2021-08-10 02:26:06 +00:00
Zicklag
b5b9a95981 Add missing bytemuck feature (#2625)
# Objective

- Allow you to compile Bevy with the `bevy_sprite2` feature, but without the `bevy_pbr2` feature.
  - This currently fails because the `bevy_sprite2` crate does not require the `derive` feature of the `bytemuck` crate in its `Cargo.toml`, even though it is required to compile.

## Solution

- Add the `derive` feature of `bytemuck` to the `bevy_sprite2` crate
2021-08-10 01:37:41 +00:00
Dimev
2f32a2a861 Added StorageTextureAccess to the exposed wgpu API (#2614)
# Objective
This fixes not having access to StorageTextureAccess in the API, which is needed for using storage textures

## Solution
Added it to the use in render_resource module


Co-authored-by: Dimas <skythedragon@outlook.com>
2021-08-08 19:10:39 +00:00
John
115b170d1f Add sprite atlases into the new renderer. (#2560)
# Objective
Restore the functionality of sprite atlases in the new renderer.

### **Note:** This PR relies on #2555 

## Solution
Mostly just a copy paste of the existing sprite atlas implementation, however I unified the rendering between sprites and atlases.

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-08-04 01:16:25 +00:00
Robert Swain
ae4f809a52 Port bevy_gltf to pipelined-rendering (#2537)
# Objective

Port bevy_gltf to the pipelined-rendering branch.

## Solution

crates/bevy_gltf has been copied and pasted into pipelined/bevy_gltf2 and modifications were made to work with the pipelined-rendering branch. Notably vertex tangents and vertex colours are not supported.

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-07-30 03:37:34 +00:00
Carter Anderson
7b336fd779 fix nightly clippy lints (#2568)
Fix new nightly clippy lints on `pipelined-rendering`
2021-07-30 03:17:27 +00:00
Boxy
0b800e547b Fix some nightly clippy lints (#2522)
on nightly these two clippy lints fail:
- [needless_borrow](https://rust-lang.github.io/rust-clippy/master/#needless_borrow)
- [unused_unit](https://rust-lang.github.io/rust-clippy/master/#unused_unit)
2021-07-29 19:36:39 -07:00
Robert Swain
6944d38a14 bevy_render2: Log adapter info on initialization (#2542)
It is useful to see which adapter is being used and which wgpu backend.
2021-07-29 00:02:10 +00:00
Carter Anderson
3ec6b3f9a0 move bevy_core_pipeline to its own plugin (#2552)
This decouples the opinionated "core pipeline" from the new (less opinionated) bevy_render crate. The "core pipeline" is intended to be used by crates like bevy_sprites, bevy_pbr, bevy_ui, and 3rd party crates that extends core rendering functionality.
2021-07-28 21:29:32 +00:00
Carter Anderson
0973d40a9f Add RenderWorld to Extract step (#2555)
Makes the "Render App World" directly available to Extract step systems as a `RenderWorld` resource. Prior to this, there was no way to directly read / write render world state during the Extract step. The only way to make changes was through Commands (which were applied at the end of the stage).

```rust
// `thing` is an "app world resource".
fn extract_thing(thing: Res<Thing>, mut render_world: ResMut<RenderWorld>) {
  render_world.insert_resource(ExtractedThing::from(thing));
}
```

RenderWorld makes a number of scenarios possible:

* When an extract system does big allocations, it is now possible to reuse them across frames by retrieving old values from RenderWorld (at the cost of reduced parallelism from unique RenderWorld borrows).
* Enables inserting into the same resource across multiple extract systems
* Enables using past RenderWorld state to inform future extract state (this should generally be avoided)

Ultimately this is just a subset of the functionality we want. In the future, it would be great to have "multi-world schedules" to enable fine grained parallelism on the render world during the extract step. But that is a research project that almost certainly won't make it into 0.6. This is a good interim solution that should easily port over to multi-world schedules if/when they land.
2021-07-28 17:52:24 +00:00
Carter Anderson
2e99d84cdc remove .system from pipelined code (#2538)
Now that we have main features, lets use them!
2021-07-26 23:44:23 +00:00
Carter Anderson
955c79f299 adapt to upstream changes 2021-07-24 16:43:37 -07:00
Robert Swain
618c9e94f0 Scale normal bias by texel size (#26)
* 3d_scene_pipelined: Use a shallower directional light angle to provoke acne

* cornell_box_pipelined: Remove bias tweaks

* bevy_pbr2: Simplify shadow biases by moving them to linear depth

* bevy_pbr2: Do not use DepthBiasState

* bevy_pbr2: Do not use bilinear filtering for sampling depth textures

* pbr.wgsl: Remove unnecessary comment

* bevy_pbr2: Do manual shadow map depth comparisons for more flexibility

* examples: Add shadow_biases_pipelined example

This is useful for stress testing biases.

* bevy_pbr2: Scale the point light normal bias by the shadow map texel size

This allows the normal bias to be small close to the light source where the
shadow map texel to screen texel ratio is high, but is appropriately large
further away from the light source where the shadow map texel can easily cover
multiple screen texels.

* shadow_biases_pipelined: Add support for toggling directional / point light

* shadow_biases_pipelined: Cleanup

* bevy_pbr2: Scale the directional light normal bias by the shadow map texel size

* shadow_biases_pipelined: Fit the orthographic projection around the scene

* bevy_pbr2: Directional lights should have no shadows outside their projection

Before this change, sampling a fragment position from outside the ndc volume
would result in the return sample being clamped to the edge in x,y or possibly
always casting a shadow for fragment positions past the orthographic
projection's far plane.

* bevy_pbr2: Fix the default directional light normal bias

* Revert "bevy_pbr2: Do manual shadow map depth comparisons for more flexibility"

This reverts commit 7df1bab38a42d8a33bc50ca583d4be37bd9c9f0d.

* shadow_biases_pipelined: Adjust directional light normal bias in 0.1 increments

* pbr.wgsl: Add a couple of clarifying comments

* Revert "bevy_pbr2: Do not use bilinear filtering for sampling depth textures"

This reverts commit f53baab0232ce218866a45cad6902b470f4cf2c4.

* shadow_biases_pipelined: Print usage to terminal
2021-07-24 16:43:37 -07:00
Robert Swain
44df4c1fae Better depth biases (#23)
* 3d_scene_pipelined: Use a shallower directional light angle to provoke acne

* cornell_box_pipelined: Remove bias tweaks

* bevy_pbr2: Simplify shadow biases by moving them to linear depth
2021-07-24 16:43:37 -07:00
Jakob Hellermann
30b8324672 expose subgraph iterator (#25) 2021-07-24 16:43:37 -07:00
John
7bbeb11ed7 Fixed issue with how texture arrays were uploaded with write_texture. (#24) 2021-07-24 16:43:37 -07:00
Carter Anderson
be8d67d0b0 switch sprites back to wgsl shader 2021-07-24 16:43:37 -07:00
Carter Anderson
e0205019ef fmt + clippy 2021-07-24 16:43:37 -07:00
Carter Anderson
3542ddcd0c add glsl support 2021-07-24 16:43:37 -07:00
Robert Swain
326b20643f Directional light and shadow (#6)
Directional light and shadow
2021-07-24 16:43:37 -07:00
James Liu
a2cac6a0d5 Small Fixes (#20)
* Fix writing less than the full capacity to the staging buffer

* Add missing TrackedRenderPass::draw command
2021-07-24 16:43:37 -07:00
Carter Anderson
ac6b27925e fix clippy 2021-07-24 16:43:37 -07:00
Carter Anderson
bc769d9641 omni light -> point light 2021-07-24 16:43:37 -07:00
Jonas Matser
4099ef6aa2 Omnilight shadow map wgsl (#15) 2021-07-24 16:43:37 -07:00
Robert Swain
858065ef8d Do not queue sprites/meshes if there are no views (#13)
* bevy_sprite2/_pbr2: Do not queue sprites/meshes if there are no views
2021-07-24 16:43:37 -07:00
Robert Swain
a9937190cd bevy_pbr2: pbr.wgsl: Fix the orthographic projection check (#17) 2021-07-24 16:43:37 -07:00
bilsen
e1e4055a51 Allows resizing of windows (#8) 2021-07-24 16:43:37 -07:00
John
5d0655f84c Fixed issue where transform buffer wasn't creating new bindings on resize. (#7) 2021-07-24 16:43:37 -07:00
Robert Swain
7c57725a63 bevy_render2/bevy_pbr2: Update to wgpu 0.9 / naga 0.5 (#19) 2021-07-24 16:43:37 -07:00
Robert Swain
3f70f92bff wgsl PBR fixes (#12)
bevy_pbr2: Fix light uniforms
2021-07-24 16:43:37 -07:00
TheRawMeatball
7792b29aa4 Force main thread for prepare_windows system (#11)
Force main thread for prepare_windows system
2021-07-24 16:43:37 -07:00
Carter Anderson
61c8475069 Begin WGSL port (sprites work, pbr lights are broken) 2021-07-24 16:43:37 -07:00
Carter Anderson
829f723bf3 Add log crate compatibility to bevy_log 2021-07-24 16:43:37 -07:00
Robert Swain
b1a91a823f bevy_pbr2: Add support for most of the StandardMaterial textures (#4)
* bevy_pbr2: Add support for most of the StandardMaterial textures

Normal maps are not included here as they require tangents in a vertex attribute.

* bevy_pbr2: Ensure RenderCommandQueue is ready for PbrShaders init

* texture_pipelined: Add a light to the scene so we can see stuff

* WIP bevy_pbr2: back to front sorting hack

* bevy_pbr2: Uniform control flow for texture sampling in pbr.frag

From 'fintelia' on the Bevy Render Rework Round 2 discussion:

"My understanding is that GPUs these days never use the "execute both branches
and select the result" strategy. Rather, what they do is evaluate the branch
condition on all threads of a warp, and jump over it if all of them evaluate to
false. If even a single thread needs to execute the if statement body, however,
then the remaining threads are paused until that is completed."

* bevy_pbr2: Simplify texture and sampler names

The StandardMaterial_ prefix is no longer needed

* bevy_pbr2: Match default 'AmbientColor' of current bevy_pbr for now

* bevy_pbr2: Convert from non-linear to linear sRGB for the color uniform

* bevy_pbr2: Add pbr_pipelined example

* Fix view vector in pbr frag to work in ortho

* bevy_pbr2: Use a 90 degree y fov and light range projection for lights

* bevy_pbr2: Add AmbientLight resource

* bevy_pbr2: Convert PointLight color to linear sRGB for use in fragment shader

* bevy_pbr2: pbr.frag: Rename PointLight.projection to view_projection

The uniform contains the view_projection matrix so this was incorrect.

* bevy_pbr2: PointLight is an OmniLight as it has a radius

* bevy_pbr2: Factoring out duplicated code

* bevy_pbr2: Implement RenderAsset for StandardMaterial

* Remove unnecessary texture and sampler clones

* fix comment formatting

* remove redundant Buffer:from

* Don't extract meshes when their material textures aren't ready

* make missing textures in the queue step an error

Co-authored-by: Aevyrie <aevyrie@gmail.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-07-24 16:43:37 -07:00
Carter Anderson
25de2d1819 Port Mesh to RenderAsset, add Slab and FrameSlabMap garbage collection for Bind Groups 2021-07-24 16:43:37 -07:00
Carter Anderson
3ef951dcbc RenderAssetPlugin 2021-07-24 16:43:37 -07:00