pub struct Renderer { /* private fields */ }Expand description
The 2D renderer.
Each frame, Renderer::render draws the sprites, texts and tile
layers into a render target of the virtual resolution: the sprites and
the texts’ glyphs sorted by layer and depth and batched by texture and
program, each tile layer at its own layer and depth among them, a chunk
at a time. It then scales that image up into the output by the largest
whole factor that fits, with bars in the clear color around it, and
draws the frame’s UiList on the output, at its
resolution.
Implementations§
Source§impl Renderer
impl Renderer
Sourcepub fn new(
device: &mut Device,
config: &RenderConfig,
) -> Result<Self, RenderError>
pub fn new( device: &mut Device, config: &RenderConfig, ) -> Result<Self, RenderError>
Makes the renderer’s pipelines, buffers and virtual image.
§Errors
RenderError::BadConfig for an empty virtual resolution, and
RenderError::Gfx if the device refuses, for instance a virtual
resolution over nadir_gfx::Caps::max_texture_size.
Sourcepub fn set_gpu_timing(&mut self, on: bool)
pub fn set_gpu_timing(&mut self, on: bool)
Times the passes of each Renderer::render on the GPU, or stops:
off at first. On a device without
Caps::timer_queries nothing is
timed.
Each render’s times are read back by a later render, two later
while the GPU keeps up, so that reading never waits for the GPU:
Renderer::gpu_times. A render is not timed while the GPU has not
done the work of the one four renders before it. Timing makes the
device’s timers at its first renders, two a render for four renders,
which the device keeps until it is dropped. Stopping forgets the
renders that wait.
A device runs one timer at a time: while timing is on, a timer of
the caller’s running around render makes it fail with
GfxError::FrameOrder.
Sourcepub fn gpu_timing(&self) -> bool
pub fn gpu_timing(&self) -> bool
Whether the passes are timed on the GPU: Renderer::set_gpu_timing.
Sourcepub fn gpu_times(&self) -> &[GpuTimes]
pub fn gpu_times(&self) -> &[GpuTimes]
The GPU’s times over the passes of earlier renders that the last
Renderer::render read back, oldest first: none while timing is
off (Renderer::set_gpu_timing), on a device without timer
queries, and when no render had all its results yet. Each says how
many renders back it was drawn (GpuTimes::renders_ago).
A time longer than the time that passed since its render started is the driver’s mistake (llvmpipe gives one for a context’s first drawing), and is dropped, with a warning the first time.
Sourcepub fn reload_program(
&mut self,
device: &mut Device,
program: Program,
source: ShaderSource<'_>,
) -> Result<(), RenderError>
pub fn reload_program( &mut self, device: &mut Device, program: Program, source: ShaderSource<'_>, ) -> Result<(), RenderError>
Builds program again from source, for shaders edited while the
game runs: the next Renderer::render draws with it, and the
program it replaces is destroyed. Call it between frames, or in one
before render.
The sources may change anything but the program’s interface, which
the sources it was compiled with declare, its files under
SHADER_DIR as they were:
- each
invariable of the vertex shader is one of the program’s attributes, with its location and no other layout qualifier, its type and its name; - each uniform block, in either shader, is one of its blocks, whose members must stay as they are: the device checks a block by its size alone;
- each other uniform is one of its samplers, a
sampler2D, or has a value of its own (= …); - the fragment shader has one output, an
out vec4at location 0 and index 0, and writes nogl_FragColororgl_FragData.
An attribute, a block or a sampler the sources leave out is taken.
The declarations are read as written, before a macro is expanded or
a branch of an #if chosen: a declaration a macro makes is not
seen, and every branch is read. Sources whose braces do not pair up,
or whose branches do not each hold whole declarations, are refused.
The renderer reads no file: the caller reads the sources, and gives them here. A device without Metal reads only the GLSL.
§Errors
RenderError::Gfx with GfxError::Interface saying what differs
when the sources declare another interface, or a uniform block of
another size; with GfxError::Shader and the driver’s log when
they do not compile or link; with another GfxError when the
device refuses a call. The renderer keeps the program it had, and
the next frame draws as the last did.
Sourcepub fn render(
&mut self,
device: &mut Device,
assets: &AssetServer,
tiles: &TileLayers,
frame: &RenderFrame,
output: Output,
) -> Result<FrameStats, RenderError>
pub fn render( &mut self, device: &mut Device, assets: &AssetServer, tiles: &TileLayers, frame: &RenderFrame, output: Output, ) -> Result<FrameStats, RenderError>
Draws frame into output, with the tile layers its
TileLayerDraws name in tiles. Call it
inside a frame of the device (begin_frame and end_frame, or their
offscreen forms).
Each text is laid out in its Font, whose atlas file
and pages it finds by their paths in assets: a text whose font or
atlas is not ready is not drawn, nor a glyph whose page is not. A
font whose atlas or page is not loaded at all, or whose atlas has no
sprite of its name or too few frames, is logged once; so is each
character a font lacks, which draws the font’s fallback. A glyph on
a page that failed shows the placeholder, as a sprite does, with the
sprite program.
Sprites whose texture is still loading are not drawn. A texture that
failed to load shows its placeholder, all of it, whatever the
sprite’s uv, when the server shows placeholders
(OnFailure::Placeholder); otherwise its
sprites are not drawn either. Tile layers do the same with their tile
set’s texture, every tile showing all of a placeholder.
A tile layer that is not in tiles is not drawn. The renderer keeps
each chunk of a layer on the GPU once the view has shown it, packs it
again only after one of its tiles changes, and frees a layer’s chunks
at the first frame after it leaves tiles.
The UI is drawn last, on the output, in the order its
UiList says, each sprite and text in its view’s
UiSpace for an output of output.size and cut to its clip; its
textures and fonts are found as the world’s are.
§Errors
RenderError::Gfx if the device refuses a call.
Sourcepub fn upscale(&self, size: UVec2) -> Upscale
pub fn upscale(&self, size: UVec2) -> Upscale
Where the virtual image lands in an output of size.
Sourcepub fn ui_space(&self, scale: UiScale, size: UVec2) -> UiSpace
pub fn ui_space(&self, scale: UiScale, size: UVec2) -> UiSpace
Where the UI at scale lands in an output of size, as it is drawn
there: UiSpace::new with the renderer’s virtual resolution. Its
UiSpace::to_ui gives the UI point under the pointer.