Skip to main content

Renderer

Struct Renderer 

Source
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

Source

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.

Source

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.

Source

pub fn gpu_timing(&self) -> bool

Whether the passes are timed on the GPU: Renderer::set_gpu_timing.

Source

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.

Source

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 in variable 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 vec4 at location 0 and index 0, and writes no gl_FragColor or gl_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.

Source

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.

Source

pub fn upscale(&self, size: UVec2) -> Upscale

Where the virtual image lands in an output of size.

Source

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.

Source

pub fn screen_to_world( &self, camera: &Camera2D, size: UVec2, screen: Vec2, ) -> Vec2

The world point under screen, in pixels from the top left of an output of size, as camera shows it: for aiming with the mouse.

Source

pub fn world_to_screen( &self, camera: &Camera2D, size: UVec2, world: Vec2, ) -> Vec2

Where world is in an output of size, in pixels from its top left, as camera shows it.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.