2 * Copyright 2009, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // This file contains the defintions of the functions in the
34 // conditioner namespace, which do the bulk of the work of
35 // conditioning a Collada file for use in O3D.
37 #include "import/cross/precompile.h"
40 #import <Cocoa/Cocoa.h>
42 #include "base/base_paths.h"
43 #include "base/file_path.h"
44 #include "base/path_service.h"
45 #include "core/cross/error.h"
46 #include "import/cross/collada.h"
47 #include "import/cross/collada_conditioner.h"
48 #include "utils/cross/file_path_utils.h"
51 bool ColladaConditioner::CompileHLSL(const String& shader_source,
52 const String& vs_entry,
53 const String& ps_entry) {
54 // The HLSL compiler isn't available on Mac.
58 // Find cgc executable, and run it on the input so that we can get a
59 // preprocessed version out.
60 bool ColladaConditioner::PreprocessShaderFile(const FilePath& in_filename,
61 const FilePath& out_filename) {
62 FilePath executable_path;
63 if (!PathService::Get(base::DIR_EXE, &executable_path)) {
64 O3D_ERROR(service_locator_) << "Couldn't get executable path.";
67 executable_path = executable_path.Append(FILE_PATH_LITERAL("cgc"));
69 // Have to convert the file paths to UTF-8
70 std::string executable_path_utf8 = FilePathToUTF8(executable_path);
71 std::string in_filename_utf8 = FilePathToUTF8(in_filename);
72 std::string out_filename_utf8 = FilePathToUTF8(out_filename);
74 DLOG(INFO) << "Now invoking '"
75 << executable_path_utf8 << " -E "
76 << in_filename_utf8 << " -o "
77 << out_filename_utf8 << "'";
79 // Now we have to convert the UTF-8 file paths to NSStrings.
80 NSString *executable_path_ns =
81 [[[NSString alloc] initWithUTF8String:executable_path_utf8.c_str()]
83 NSString *in_filename_ns =
84 [[[NSString alloc] initWithUTF8String:in_filename_utf8.c_str()]
86 NSString *out_filename_ns =
87 [[[NSString alloc] initWithUTF8String:out_filename_utf8.c_str()]
90 NSTask *task = [[[NSTask alloc] init] autorelease];
91 [task setLaunchPath:executable_path_ns];
92 [task setArguments:[NSArray arrayWithObjects:@"-E", in_filename_ns,
93 @"-o", out_filename_ns, nil]];
95 // We send the output to a pipe, even though the tool doesn't really
96 // output anything. This is so that the invocation of the command
97 // will be synchronous, and we can be sure that the output file has
98 // been written by the time we exit this function.
99 NSPipe *pipe = [NSPipe pipe];
100 [task setStandardOutput:pipe];
102 [[pipe fileHandleForReading] readDataToEndOfFile];
105 } // end namespace o3d