示例#1
0
OutputVertex RunShader(const InputVertex& input, int num_attributes)
{
    VertexShaderState state;

    const u32* main = &shader_memory[registers.vs_main_offset];
    state.program_counter = (u32*)main;
    state.debug.max_offset = 0;
    state.debug.max_opdesc_id = 0;

    // Setup input register table
    const auto& attribute_register_map = registers.vs_input_register_map;
    float24 dummy_register;
    std::fill(&state.input_register_table[0], &state.input_register_table[16], &dummy_register);
    if(num_attributes > 0) state.input_register_table[attribute_register_map.attribute0_register] = &input.attr[0].x;
    if(num_attributes > 1) state.input_register_table[attribute_register_map.attribute1_register] = &input.attr[1].x;
    if(num_attributes > 2) state.input_register_table[attribute_register_map.attribute2_register] = &input.attr[2].x;
    if(num_attributes > 3) state.input_register_table[attribute_register_map.attribute3_register] = &input.attr[3].x;
    if(num_attributes > 4) state.input_register_table[attribute_register_map.attribute4_register] = &input.attr[4].x;
    if(num_attributes > 5) state.input_register_table[attribute_register_map.attribute5_register] = &input.attr[5].x;
    if(num_attributes > 6) state.input_register_table[attribute_register_map.attribute6_register] = &input.attr[6].x;
    if(num_attributes > 7) state.input_register_table[attribute_register_map.attribute7_register] = &input.attr[7].x;
    if(num_attributes > 8) state.input_register_table[attribute_register_map.attribute8_register] = &input.attr[8].x;
    if(num_attributes > 9) state.input_register_table[attribute_register_map.attribute9_register] = &input.attr[9].x;
    if(num_attributes > 10) state.input_register_table[attribute_register_map.attribute10_register] = &input.attr[10].x;
    if(num_attributes > 11) state.input_register_table[attribute_register_map.attribute11_register] = &input.attr[11].x;
    if(num_attributes > 12) state.input_register_table[attribute_register_map.attribute12_register] = &input.attr[12].x;
    if(num_attributes > 13) state.input_register_table[attribute_register_map.attribute13_register] = &input.attr[13].x;
    if(num_attributes > 14) state.input_register_table[attribute_register_map.attribute14_register] = &input.attr[14].x;
    if(num_attributes > 15) state.input_register_table[attribute_register_map.attribute15_register] = &input.attr[15].x;

    // Setup output register table
    OutputVertex ret;
    for (int i = 0; i < 7; ++i) {
        const auto& output_register_map = registers.vs_output_attributes[i];

        u32 semantics[4] = {
            output_register_map.map_x, output_register_map.map_y,
            output_register_map.map_z, output_register_map.map_w
        };

        for (int comp = 0; comp < 4; ++comp)
            state.output_register_table[4*i+comp] = ((float24*)&ret) + semantics[comp];
    }

    state.status_registers[0] = false;
    state.status_registers[1] = false;
    std::fill(state.call_stack, state.call_stack + sizeof(state.call_stack) / sizeof(state.call_stack[0]),
              VertexShaderState::INVALID_ADDRESS);
    state.call_stack_pointer = &state.call_stack[0];

    ProcessShaderCode(state);
    DebugUtils::DumpShader(shader_memory, state.debug.max_offset, swizzle_data,
                           state.debug.max_opdesc_id, registers.vs_main_offset,
                           registers.vs_output_attributes);

    DEBUG_LOG(GPU, "Output vertex: pos (%.2f, %.2f, %.2f, %.2f), col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f)",
        ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
        ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
        ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32());

    return ret;
}
/*
* \brief Load our shaders into our shader object
*/
const bool Shader::Load(
		const char *vertexShader,							//!< Name of the vertex shader
		const char *fragmentShader							//!< Name of the fragment shader
	)
{
	m_vertex_shader = glCreateShader(GL_VERTEX_SHADER);

    std::string vertex_shader_string = tyga::stringFromFile(vertexShader);

	if (!ProcessShaderCode(vertexShader, vertex_shader_string))
		return false;

    const char *vertex_shader_code = vertex_shader_string.c_str();

    glShaderSource(
		m_vertex_shader,
		1,
        (const GLchar **) &vertex_shader_code, 
		NULL
	);

	int compile_status = 0;
    glCompileShader(m_vertex_shader);
    glGetShaderiv(
		m_vertex_shader,
        GL_COMPILE_STATUS,
		&compile_status
	);

    if (compile_status != GL_TRUE) {
        static const int string_length = 1024;
        GLchar log[string_length]= "";
        glGetShaderInfoLog(
			m_vertex_shader,
            string_length,
            NULL,
            log
		);
        std::cerr << "Vertex Shader: " << log << std::endl;
		return false;
    }
 
    m_fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    std::string fragment_shader_string = tyga::stringFromFile(fragmentShader);

	if (!ProcessShaderCode(fragmentShader, fragment_shader_string))
		return false;

    const char *const fragment_shader_code = fragment_shader_string.c_str();

    glShaderSource(
		m_fragment_shader, 
		1,
		(const GLchar **) &fragment_shader_code, 
		NULL
	);

    glCompileShader(m_fragment_shader);
    
	glGetShaderiv(
		m_fragment_shader,
        GL_COMPILE_STATUS,
        &compile_status
	);
    
	if (compile_status != GL_TRUE) {
        static const int string_length = 1024;
        GLchar log[string_length]= "";
        glGetShaderInfoLog(
			m_fragment_shader,
            string_length,
            NULL,
            log
		);

        std::cerr << "Fragment Shader: " << log << std::endl;
		return false;
    }

	m_program = glCreateProgram();

	return true;
}