Ejemplo n.º 1
0
void LightsourceSimplePass::setup(GraphicContext &gc)
{
	Size viewport_size = viewport->get_size();
	if (fb.is_null() || !gc.is_frame_buffer_owner(fb) || final_color.updated() || zbuffer.updated() || diffuse_color_gbuffer.updated())
	{
		final_color.set(Texture2D(gc, viewport->get_width(), viewport->get_height(), tf_rgba16f));

		fb = FrameBuffer(gc);
		fb.attach_color(0, final_color.get());
		fb.attach_depth(zbuffer.get());

		BlendStateDescription blend_desc;
		blend_desc.enable_blending(true);
		blend_desc.set_blend_function(blend_one, blend_one, blend_one, blend_one);
		blend_state = BlendState(gc, blend_desc);

		DepthStencilStateDescription icosahedron_depth_stencil_desc;
		icosahedron_depth_stencil_desc.enable_depth_write(false);
		icosahedron_depth_stencil_desc.enable_depth_test(true);
		icosahedron_depth_stencil_desc.set_depth_compare_function(compare_lequal);
		icosahedron_depth_stencil_state = DepthStencilState(gc, icosahedron_depth_stencil_desc);

		RasterizerStateDescription icosahedron_rasterizer_desc;
		icosahedron_rasterizer_desc.set_culled(true);
		icosahedron_rasterizer_state = RasterizerState(gc, icosahedron_rasterizer_desc);

		DepthStencilStateDescription rect_depth_stencil_desc;
		rect_depth_stencil_desc.enable_depth_write(false);
		rect_depth_stencil_desc.enable_depth_test(false);
		rect_depth_stencil_state = DepthStencilState(gc, rect_depth_stencil_desc);

		RasterizerStateDescription rect_rasterizer_desc;
		rect_rasterizer_desc.set_culled(false);
		rect_rasterizer_state = RasterizerState(gc, rect_rasterizer_desc);

		uniforms = UniformVector<Uniforms>(gc, 1);

		icosahedron_prim_array = PrimitivesArray(gc);
		icosahedron_prim_array.set_attributes(0, icosahedron->vertices);

		Vec4f positions[6] =
		{
			Vec4f(-1.0f, -1.0f, 1.0f, 1.0f),
			Vec4f( 1.0f, -1.0f, 1.0f, 1.0f),
			Vec4f(-1.0f,  1.0f, 1.0f, 1.0f),
			Vec4f( 1.0f, -1.0f, 1.0f, 1.0f),
			Vec4f(-1.0f,  1.0f, 1.0f, 1.0f),
			Vec4f( 1.0f,  1.0f, 1.0f, 1.0f)
		};
		rect_positions = VertexArrayVector<Vec4f>(gc, positions, 6);

		rect_prim_array  = PrimitivesArray(gc);
		rect_prim_array.set_attributes(0, rect_positions);
	}
}
Ejemplo n.º 2
0
void RenderBatchPoint::flush(GraphicContext &gc)
{
	if (position > 0)
	{
		gc.set_program_object(program_color_only);

		int gpu_index;
		VertexArrayVector<PointVertex> gpu_vertices(batch_buffer->get_vertex_buffer(gc, gpu_index));

		if (prim_array[gpu_index].is_null())
		{
			prim_array[gpu_index] = PrimitivesArray(gc);
			prim_array[gpu_index].set_attributes(0, gpu_vertices, cl_offsetof(PointVertex, position));
			prim_array[gpu_index].set_attributes(1, gpu_vertices, cl_offsetof(PointVertex, color));
		}

		gpu_vertices.upload_data(gc, 0, vertices, position);

		gc.draw_primitives(type_points, position, prim_array[gpu_index]);

		gc.reset_program_object();

		position = 0;
	}
}
Ejemplo n.º 3
0
void RenderBatchTriangle::flush(GraphicContext &gc)
{
	if (position > 0)
	{
		gc.set_program_object(program_sprite);

		int gpu_index;
		VertexArrayVector<SpriteVertex> gpu_vertices(batch_buffer->get_vertex_buffer(gc, gpu_index));

		if (prim_array[gpu_index].is_null())
		{
			prim_array[gpu_index] = PrimitivesArray(gc);
			prim_array[gpu_index].set_attributes(0, gpu_vertices, cl_offsetof(SpriteVertex, position));
			prim_array[gpu_index].set_attributes(1, gpu_vertices, cl_offsetof(SpriteVertex, color));
			prim_array[gpu_index].set_attributes(2, gpu_vertices, cl_offsetof(SpriteVertex, texcoord));
			prim_array[gpu_index].set_attributes(3, gpu_vertices, cl_offsetof(SpriteVertex, texindex));

			if (glyph_blend.is_null())
			{
				BlendStateDescription blend_desc;
				blend_desc.set_blend_function(blend_constant_color, blend_one_minus_src_color, blend_zero, blend_one);
				glyph_blend = BlendState(gc, blend_desc);
			}
		}

		gpu_vertices.upload_data(gc, 0, vertices, position);

		for (int i = 0; i < num_current_textures; i++)
			gc.set_texture(i, current_textures[i]);

		if (use_glyph_program)
		{
			gc.set_blend_state(glyph_blend, constant_color);
			gc.draw_primitives(type_triangles, position, prim_array[gpu_index]);
			gc.reset_blend_state();
		}
		else
		{
			gc.draw_primitives(type_triangles, position, prim_array[gpu_index]);
		}

		for (int i = 0; i < num_current_textures; i++)
			gc.reset_texture(i);

		gc.reset_program_object();

		position = 0;
		for (int i = 0; i < num_current_textures; i++)
			current_textures[i] = Texture2D();
		num_current_textures = 0;

	}
}
Ejemplo n.º 4
0
void ShaderEffect_Impl::create_primitives_array(GraphicContext &gc, const ShaderEffectDescription_Impl *description)
{
	prim_array = PrimitivesArray(gc);
	
	int index = 0;
	for (auto it = description->attributes.begin(); it != description->attributes.end(); ++it, index++)
	{
		if (it->second.attribute_type == ShaderEffectDescription_Impl::attribute_type_buffer)
		{
			VertexArrayBuffer buffer = it->second.buffer;
			prim_array.set_attributes(index, buffer, it->second.size, it->second.type, it->second.offset, it->second.stride, it->second.normalize);
			attributes.push_back(buffer);

			num_vertices = description->draw_count;
		}
		else if (it->second.attribute_type == ShaderEffectDescription_Impl::attribute_type_screen_quad)
		{
			Vec4f screen_quad[6] =
			{
				Vec4f(-1.0f,-1.0f, 0.0f, 1.0f),
				Vec4f( 1.0f,-1.0f, 0.0f, 1.0f),
				Vec4f(-1.0f, 1.0f, 0.0f, 1.0f),
				Vec4f( 1.0f, 1.0f, 0.0f, 1.0f),
				Vec4f( 1.0f,-1.0f, 0.0f, 1.0f),
				Vec4f(-1.0f, 1.0f, 0.0f, 1.0f)
			};
			VertexArrayVector<Vec4f> gpu_screen_quad(gc, screen_quad, 6);
			prim_array.set_attributes(index, gpu_screen_quad);
			attributes.push_back(gpu_screen_quad);

			num_vertices = 6;
		}
		else if (it->second.attribute_type == ShaderEffectDescription_Impl::attribute_type_uv_quad)
		{
			Vec4f uv_quad[6] =
			{
				Vec4f( 0.0f, 0.0f, 0.0f, 1.0f),
				Vec4f( 1.0f, 0.0f, 0.0f, 1.0f),
				Vec4f( 0.0f, 1.0f, 0.0f, 1.0f),
				Vec4f( 1.0f, 1.0f, 0.0f, 1.0f),
				Vec4f( 1.0f, 0.0f, 0.0f, 1.0f),
				Vec4f( 0.0f, 1.0f, 0.0f, 1.0f)
			};
			VertexArrayVector<Vec4f> gpu_uv_quad(gc, uv_quad, 6);
			prim_array.set_attributes(index, gpu_uv_quad);
			attributes.push_back(gpu_uv_quad);

			num_vertices = 6;
		}
	}
}
Ejemplo n.º 5
0
void ParticleEmitterPass::setup(GraphicContext &gc)
{
	if (program.is_null())
	{
		std::string vertex_filename = PathHelp::combine(shader_path, "ParticleEmitter/vertex.hlsl");
		std::string fragment_filename = PathHelp::combine(shader_path, "ParticleEmitter/fragment.hlsl");
		program = ProgramObject::load(gc, vertex_filename, fragment_filename);
		program.bind_attribute_location(0, "AttrPosition");
		program.bind_frag_data_location(0, "FragColor");
		if (!program.link())
			throw Exception(string_format("Particle emitter program failed to link: %1", program.get_info_log()));
		program.set_uniform_buffer_index("Uniforms", 0);
		program.set_uniform1i("NormalZTexture", 0);
		program.set_uniform1i("InstanceTexture", 1);
		program.set_uniform1i("ParticleTexture", 2);
		program.set_uniform1i("ParticleSampler", 2);
		program.set_uniform1i("ColorGradientTexture", 3);
		program.set_uniform1i("ColorGradientSampler", 3);

		billboard_positions = VertexArrayVector<Vec3f>(gc, cpu_billboard_positions, 6);
	}

	Size viewport_size = viewport->get_size();
	if (fb.is_null() || !gc.is_frame_buffer_owner(fb) || final_color.updated() || zbuffer.updated())
	{
		fb = FrameBuffer(gc);
		fb.attach_color(0, final_color.get());
		fb.attach_depth(zbuffer.get());

		BlendStateDescription blend_desc;
		blend_desc.enable_blending(true);
		blend_desc.set_blend_function(blend_src_alpha, blend_one_minus_src_alpha, blend_zero, blend_zero);
		blend_state = BlendState(gc, blend_desc);

		DepthStencilStateDescription depth_stencil_desc;
		depth_stencil_desc.enable_depth_write(false);
		depth_stencil_desc.enable_depth_test(true);
		depth_stencil_desc.set_depth_compare_function(compare_lequal);
		depth_stencil_state = DepthStencilState(gc, depth_stencil_desc);

		RasterizerStateDescription rasterizer_desc;
		rasterizer_desc.set_culled(false);
		rasterizer_state = RasterizerState(gc, rasterizer_desc);

		prim_array = PrimitivesArray(gc);
		prim_array.set_attributes(0, billboard_positions);
	}
}
Ejemplo n.º 6
0
ModelLOD::ModelLOD(GraphicContext &gc, int model_index, std::shared_ptr<ModelData> model_data)
{
	mesh_buffers.reserve(model_data->meshes.size());
	for (size_t i = 0; i < model_data->meshes.size(); i++)
	{
		ModelMeshBuffers buffers;
		buffers.primitives_array = PrimitivesArray(gc);
		buffers.vertices = upload_vector(gc, buffers.primitives_array, 0, model_data->meshes[i].vertices);
		buffers.normals = upload_vector(gc, buffers.primitives_array, 1, model_data->meshes[i].normals);
		buffers.bitangents = upload_vector(gc, buffers.primitives_array, 2, model_data->meshes[i].bitangents);
		buffers.tangents = upload_vector(gc, buffers.primitives_array, 3, model_data->meshes[i].tangents);
		buffers.bone_weights = upload_vector(gc, buffers.primitives_array, 4, model_data->meshes[i].bone_weights, true);
		buffers.bone_selectors = upload_vector(gc, buffers.primitives_array, 5, model_data->meshes[i].bone_selectors, false);
		buffers.colors = upload_vector(gc, buffers.primitives_array, 6, model_data->meshes[i].colors, true);

		for (size_t channel = 0; channel < model_data->meshes[i].channels.size(); channel++)
		{
			buffers.channels.push_back(upload_vector(gc, buffers.primitives_array, 7 + channel, model_data->meshes[i].channels[channel]));
		}

		buffers.elements = ElementArrayVector<unsigned int>(gc, model_data->meshes[i].elements);

		size_t num_materials = model_data->meshes[i].draw_ranges.size();
		for (size_t j = 0; j < num_materials; j++)
		{
			const ModelDataDrawRange &material_range = model_data->meshes[i].draw_ranges[j];

			ModelMaterialUniforms block;

			block.material_ambient = Vec4f(material_range.ambient.get_single_value(), 0.0f);
			block.material_diffuse = Vec4f(material_range.diffuse.get_single_value(), 0.0f);
			block.material_specular = Vec4f(material_range.specular.get_single_value(), 0.0f);
			block.material_glossiness = material_range.glossiness.get_single_value();
			block.material_specular_level = material_range.specular_level.get_single_value();

			block.model_index = model_index;
			block.vectors_per_instance = Model::instance_base_vectors + model_data->bones.size() * Model::vectors_per_bone + num_materials * Model::vectors_per_material;
			block.material_offset = Model::instance_base_vectors + model_data->bones.size() * Model::vectors_per_bone + j * Model::vectors_per_material;

			buffers.uniforms.push_back(UniformVector<ModelMaterialUniforms>(gc, &block, 1));
		}

		mesh_buffers.push_back(buffers);
	}
}
Ejemplo n.º 7
0
BloomPass::BloomPass(GraphicContext &gc, const std::string &shader_path, ResourceContainer &inout)
{
    viewport = inout.get<Rect>("Viewport");
    final_color = inout.get<Texture2D>("FinalColor");
    bloom_contribution = inout.get<Texture2D>("BloomContribution");

    if (gc.get_shader_language() == shader_glsl)
    {
        bloom_shader = ShaderSetup::compile(gc, "", PathHelp::combine(shader_path, "Final/vertex_present.glsl"), PathHelp::combine(shader_path, "Bloom/fragment_bloom_extract.glsl"), "");
        bloom_shader.bind_frag_data_location(0, "FragColor");
    }
    else
    {
        bloom_shader = ShaderSetup::compile(gc, "", PathHelp::combine(shader_path, "Final/vertex_present.hlsl"), PathHelp::combine(shader_path, "Bloom/fragment_bloom_extract.hlsl"), "");
    }

    ShaderSetup::link(bloom_shader, "bloom extract program");

    bloom_shader.bind_attribute_location(0, "PositionInProjection");
    bloom_shader.set_uniform1i("FinalColors", 0);
    bloom_shader.set_uniform1i("FinalColorsSampler", 0);
    bloom_shader.set_uniform1i("LogAverageLight", 1);

    Vec4f positions[6] =
    {
        Vec4f(-1.0f, -1.0f, 1.0f, 1.0f),
        Vec4f( 1.0f, -1.0f, 1.0f, 1.0f),
        Vec4f(-1.0f,  1.0f, 1.0f, 1.0f),
        Vec4f( 1.0f, -1.0f, 1.0f, 1.0f),
        Vec4f(-1.0f,  1.0f, 1.0f, 1.0f),
        Vec4f( 1.0f,  1.0f, 1.0f, 1.0f)
    };
    rect_positions = VertexArrayVector<Vec4f>(gc, positions, 6);
    rect_primarray = PrimitivesArray(gc);
    rect_primarray.set_attributes(0, rect_positions);

    bloom_blur.input = bloom_contribution;

    BlendStateDescription blend_desc;
    blend_desc.enable_blending(false);
    blend_state = BlendState(gc, blend_desc);
}