| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #pragma once
- #include "Color4.h"
- #include "Matrix4x4.h"
- #include "CommandBuffer.h"
- #include "ConstantBuffer.h"
- #include "RendererTypes.h"
- #include "Log.h"
- namespace crown
- {
- #define MAX_RENDER_LAYERS 32
- #define MAX_RENDER_STATES 1024
- // State flags
- #define STATE_NONE 0x0000000000000000
- #define STATE_DEPTH_WRITE 0x0000000000000001
- #define STATE_COLOR_WRITE 0x0000000000000002
- #define STATE_ALPHA_WRITE 0x0000000000000004
- #define STATE_CULL_CW 0x0000000000000010
- #define STATE_CULL_CCW 0x0000000000000020
- #define STATE_TEXTURE_0 0x0000000000000100
- #define STATE_TEXTURE_1 0x0000000000000200
- #define STATE_TEXTURE_2 0x0000000000000400
- #define STATE_TEXTURE_3 0x0000000000000800
- #define STATE_TEXTURE_MASK 0x0000000000000F00
- #define STATE_MAX_TEXTURES 4
- #define STATE_PRIMITIVE_TRIANGLES 0x0000000000000000
- #define STATE_PRIMITIVE_POINTS 0x0000000000001000
- #define STATE_PRIMITIVE_LINES 0x0000000000002000
- #define STATE_PRIMITIVE_MASK 0x000000000000F000
- #define STATE_PRIMITIVE_SHIFT 12
- #define STATE_BLEND_FUNC_ZERO 0x0000000000010000
- #define STATE_BLEND_FUNC_ONE 0x0000000000020000
- #define STATE_BLEND_FUNC_SRC_COLOR 0x0000000000030000
- #define STATE_BLEND_FUNC_ONE_MINUS_SRC_COLOR 0x0000000000040000
- #define STATE_BLEND_FUNC_DST_COLOR 0x0000000000050000
- #define STATE_BLEND_FUNC_ONE_MINUS_DST_COLOR 0x0000000000060000
- #define STATE_BLEND_FUNC_SRC_ALPHA 0x0000000000070000
- #define STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA 0x0000000000080000
- #define STATE_BLEND_FUNC_DST_ALPHA 0x0000000000090000
- #define STATE_BLEND_FUNC_ONE_MINUS_DST_ALPHA 0x00000000000A0000
- #define STATE_BLEND_FUNC_MASK 0x0000000000FF0000
- #define STATE_BLEND_FUNC_SHIFT 16
- #define STATE_BLEND_EQUATION_ADD 0x0000000001000000
- #define STATE_BLEND_EQUATION_SUBTRACT 0x0000000002000000
- #define STATE_BLEND_EQUATION_REVERSE_SUBTRACT 0x0000000003000000
- #define STATE_BLEND_EQUATION_MASK 0x0000000003000000
- #define STATE_BLEND_EQUATION_SHIFT 24
- #define STATE_DEPTH_TEST_NEVER 0x0000000010000000
- #define STATE_DEPTH_TEST_LESS 0x0000000020000000
- #define STATE_DEPTH_TEST_EQUAL 0x0000000030000000
- #define STATE_DEPTH_TEST_LEQUAL 0x0000000040000000
- #define STATE_DEPTH_TEST_GREATER 0x0000000050000000
- #define STATE_DEPTH_TEST_NOTEQUAL 0x0000000060000000
- #define STATE_DEPTH_TEST_GEQUAL 0x0000000070000000
- #define STATE_DEPTH_TEST_ALWAYS 0x0000000080000000
- #define STATE_DEPTH_TEST_MASK 0x00000000F0000000
- #define STATE_DEPTH_TEST_SHIFT 28
- #define STATE_BLEND_FUNC(src, dst) (uint64_t(src << 4) | uint64_t(dst))
- #define CLEAR_COLOR 0x1
- #define CLEAR_DEPTH 0x2
- // Texture flags
- #define TEXTURE_FILTER_NEAREST 0x00000001
- #define TEXTURE_FILTER_LINEAR 0x00000002
- #define TEXTURE_FILTER_BILINEAR 0x00000003
- #define TEXTURE_FILTER_TRILINEAR 0x00000004
- #define TEXTURE_FILTER_MASK 0x0000000F
- #define TEXTURE_FILTER_SHIFT 0
- #define TEXTURE_WRAP_CLAMP_EDGE 0x00000010
- #define TEXTURE_WRAP_CLAMP_REPEAT 0x00000020
- #define TEXTURE_WRAP_MASK 0x000000F0
- #define TEXTURE_WRAP_SHIFT 4
- // Sampler flags
- #define SAMPLER_TEXTURE 0x10000000
- #define SAMPLER_MASK 0xF0000000
- #define SAMPLER_SHIFT 28
- struct ViewRect
- {
- void clear()
- {
- m_x = 0;
- m_y = 0;
- m_width = 0;
- m_height = 0;
- }
- uint32_t area() const
- {
- return (m_width - m_x) * (m_height - m_y);
- }
- uint16_t m_x;
- uint16_t m_y;
- uint16_t m_width;
- uint16_t m_height;
- };
- struct ClearState
- {
- void clear()
- {
- m_color = Color4::GRAY;
- m_depth = 1.0f;
- }
- public:
- uint8_t m_flags;
- Color4 m_color;
- float m_depth;
- };
- struct Sampler
- {
- Id sampler_id;
- uint32_t flags;
- };
- struct RenderState
- {
- void clear()
- {
- m_flags = STATE_NONE;
- pose = Matrix4x4::IDENTITY;
- program.id = INVALID_ID;
- vb.id = INVALID_ID;
- ib.id = INVALID_ID;
- for (uint32_t i = 0; i < STATE_MAX_TEXTURES; i++)
- {
- samplers[i].sampler_id.id = INVALID_ID;
- samplers[i].flags = SAMPLER_TEXTURE;
- }
- }
- public:
- uint64_t m_flags;
- Matrix4x4 pose;
- GPUProgramId program;
- VertexBufferId vb;
- IndexBufferId ib;
- Sampler samplers[STATE_MAX_TEXTURES];
- };
- struct RenderKey
- {
- void clear()
- {
- m_layer = 0;
- }
- uint64_t encode()
- {
- return uint64_t(m_layer) << 56;
- }
- void decode(uint64_t key)
- {
- m_layer = (key >> 56) & 0xFF;
- }
- public:
- uint8_t m_layer;
- };
- struct RenderContext
- {
- RenderContext()
- {
- clear();
- }
- void set_state(uint64_t flags)
- {
- m_state.m_flags = flags;
- }
- void set_pose(const Matrix4x4& pose)
- {
- m_state.pose = pose;
- }
- void set_program(GPUProgramId program)
- {
- m_state.program = program;
- }
- void set_vertex_buffer(VertexBufferId vb)
- {
- m_state.vb = vb;
- }
- void set_index_buffer(IndexBufferId ib)
- {
- m_state.ib = ib;
- }
- void set_uniform(UniformId id, UniformType::Enum type, const void* value, uint8_t num)
- {
- m_constants.write_constant(id, type, value, num);
- }
- void set_texture(uint8_t unit, UniformId sampler_uniform, TextureId texture, uint32_t flags)
- {
- m_flags |= STATE_TEXTURE_0 << unit;
- Sampler& sampler = m_state.samplers[unit];
- sampler.sampler_id = texture;
- sampler.flags |= SAMPLER_TEXTURE | flags;
- const uint32_t val = unit;
- set_uniform(sampler_uniform, UniformType::INTEGER_1, &val, 1);
- }
- void set_layer_render_target(uint8_t layer, RenderTargetId target)
- {
- CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
- m_targets[layer] = target;
- }
- void set_layer_clear(uint8_t layer, uint8_t flags, const Color4& color, float depth)
- {
- CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
- m_clears[layer].m_flags = flags;
- m_clears[layer].m_color = color;
- m_clears[layer].m_depth = depth;
- }
- void set_layer_view(uint8_t layer, const Matrix4x4& view)
- {
- CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
- m_view_matrices[layer] = view;
- }
- void set_layer_projection(uint8_t layer, const Matrix4x4& projection)
- {
- CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
- m_projection_matrices[layer] = projection;
- }
- void set_layer_viewport(uint8_t layer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
- {
- CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
- m_viewports[layer].m_x = x;
- m_viewports[layer].m_y = y;
- m_viewports[layer].m_width = width;
- m_viewports[layer].m_height = height;
- }
- void set_layer_scissor(uint8_t layer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
- {
- CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
- m_scissors[layer].m_x = x;
- m_scissors[layer].m_y = y;
- m_scissors[layer].m_width = width;
- m_scissors[layer].m_height = height;
- }
- void commit(uint8_t layer)
- {
- m_render_key.m_layer = layer;
- m_states[m_num_states] = m_state;
- m_keys[m_num_states] = m_render_key.encode();
- m_num_states++;
- m_render_key.clear();
- m_state.clear();
- }
- void clear()
- {
- m_flags = STATE_NONE;
- m_render_key.clear();
- m_num_states = 0;
- m_state.clear();
- }
- void push()
- {
- m_commands.commit();
- m_constants.commit();
- }
- public:
- uint64_t m_flags;
- RenderKey m_render_key;
- RenderState m_state;
- // Per-state data
- uint32_t m_num_states;
- RenderState m_states[MAX_RENDER_STATES];
- ClearState m_clears[MAX_RENDER_STATES];
- uint64_t m_keys[MAX_RENDER_STATES];
- // Per-layer data
- RenderTargetId m_targets[MAX_RENDER_LAYERS];
- Matrix4x4 m_view_matrices[MAX_RENDER_LAYERS];
- Matrix4x4 m_projection_matrices[MAX_RENDER_LAYERS];
- ViewRect m_viewports[MAX_RENDER_LAYERS];
- ViewRect m_scissors[MAX_RENDER_LAYERS];
- CommandBuffer m_commands;
- ConstantBuffer m_constants;
- };
- } // namespace crown
|